Landing ODBC support for the sql extension. Bug 254924. Patch by Dorando <bugzilla-mozilla@dorando.at>

This commit is contained in:
Jan.Varga%gmail.com 2006-06-05 20:02:59 +00:00
parent 644d3762a9
commit 264e33fad7
13 changed files with 862 additions and 0 deletions

View File

@ -55,6 +55,10 @@ ifdef MOZ_ENABLE_MYSQL
DIRS += mysql
endif
ifdef MOZ_ENABLE_ODBC
DIRS += odbc
endif
DIRS += build
ifdef ENABLE_TESTS

View File

@ -55,5 +55,6 @@ xpi:
bin/components/sqlpgsql.xpt \
bin/components/sqlsqlite.xpt \
bin/components/sqlmysql.xpt \
bin/components/sqlodbc.xpt \
bin/components/$(LIB_PREFIX)sql$(DLL_SUFFIX) \
bin/chrome/sql.jar

View File

@ -83,6 +83,12 @@ SHARED_LIBRARY_LIBS += ../../mysql/src/$(LIB_PREFIX)sqlmysql_s.$(LIB_SUFFIX)
EXTRA_DSO_LDOPTS += -L$(MOZ_MYSQL_LIBS) -lmysqlclient -lz
endif
ifdef MOZ_ENABLE_ODBC
DEFINES += -DMOZ_ENABLE_ODBC
SHARED_LIBRARY_LIBS += ../../odbc/src/$(LIB_PREFIX)sqlodbc_s.$(LIB_SUFFIX)
EXTRA_DSO_LDOPTS += -L$(MOZ_MYSQL_LIBS) -lodbc32 ODBC32.LIB
endif
include $(topsrcdir)/config/rules.mk
ifdef MOZ_ENABLE_PGSQL
@ -96,3 +102,7 @@ endif
ifdef MOZ_ENABLE_MYSQL
INCLUDES += -I$(MOZ_MYSQL_INCLUDES)
endif
ifdef MOZ_ENABLE_ODBC
INCLUDES += -I$(MOZ_ODBC_INCLUDES)
endif

View File

@ -45,6 +45,9 @@
#ifdef MOZ_ENABLE_MYSQL
#include "mozSqlConnectionMysql.h"
#endif
#ifdef MOZ_ENABLE_ODBC
#include "mozSqlConnectionODBC.h"
#endif
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(mozSqlService, Init)
#ifdef MOZ_ENABLE_PGSQL
@ -56,6 +59,9 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(mozSqlConnectionSqlite)
#ifdef MOZ_ENABLE_MYSQL
NS_GENERIC_FACTORY_CONSTRUCTOR(mozSqlConnectionMysql)
#endif
#ifdef MOZ_ENABLE_ODBC
NS_GENERIC_FACTORY_CONSTRUCTOR(mozSqlConnectionODBC)
#endif
static nsModuleComponentInfo components[] =
{
@ -90,6 +96,13 @@ static nsModuleComponentInfo components[] =
mozSqlConnectionMysqlConstructor
}
#endif
#ifdef MOZ_ENABLE_ODBC
,{ MOZ_SQLCONNECTIONODBC_CLASSNAME,
MOZ_SQLCONNECTIONODBC_CID,
MOZ_SQLCONNECTIONODBC_CONTRACTID,
mozSqlConnectionODBCConstructor
}
#endif
};
NS_IMPL_NSGETMODULE(mozSqlModule, components)

View File

@ -0,0 +1,45 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is mozilla.org
# Portions created by the Initial Developer are Copyright (C) 2006
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK ***** */
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
DIRS = \
public \
src
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,49 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is mozilla.org
# Portions created by the Initial Developer are Copyright (C) 2006
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK ***** */
DEPTH = ../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
MODULE = sql
XPIDL_MODULE = sqlodbc
XPIDLSRCS = \
mozISqlConnectionODBC.idl \
mozISqlResultODBC.idl \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,43 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is mozilla.org
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
[scriptable, uuid(e90b765a-3346-4ebd-b0a4-6235be91307e)]
interface mozISqlConnectionODBC : nsISupports
{
};

View File

@ -0,0 +1,43 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is mozilla.org
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
[scriptable, uuid(21d73033-6123-4460-86cf-d84ca85f1ef2)]
interface mozISqlResultODBC : nsISupports
{
};

View File

@ -0,0 +1,69 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is mozilla.org
# Portions created by the Initial Developer are Copyright (C) 2006
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK ***** */
DEPTH = ../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = sql
LIBRARY_NAME = sqlodbc_s
MOZILLA_INTERNAL_API = 1
REQUIRES = \
xpcom \
string \
locale \
rdf \
dom \
unicharutil \
layout \
$(NULL)
CPPSRCS = \
mozSqlConnectionODBC.cpp \
mozSqlResultODBC.cpp
EXPORTS = \
mozSqlConnectionODBC.h \
mozSqlResultODBC.h
FORCE_STATIC_LIB=1
include $(topsrcdir)/config/rules.mk
INCLUDES += -I$(MOZ_ODBC_INCLUDES)

View File

@ -0,0 +1,219 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is mozilla.org
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsReadableUtils.h"
#include "mozSqlConnectionODBC.h"
#include "mozSqlResultODBC.h"
mozSqlConnectionODBC::mozSqlConnectionODBC()
: mConnection(nsnull)
{
}
mozSqlConnectionODBC::~mozSqlConnectionODBC()
{
if (mConnection) {
SQLDisconnect(mConnection);
SQLFreeHandle(SQL_HANDLE_DBC, mConnection);
SQLFreeHandle(SQL_HANDLE_ENV, mEnv);
}
}
NS_IMPL_ADDREF_INHERITED(mozSqlConnectionODBC, mozSqlConnection)
NS_IMPL_RELEASE_INHERITED(mozSqlConnectionODBC, mozSqlConnection)
// QueryInterface
NS_INTERFACE_MAP_BEGIN(mozSqlConnectionODBC)
NS_INTERFACE_MAP_ENTRY(mozISqlConnectionODBC)
NS_INTERFACE_MAP_END_INHERITING(mozSqlConnection)
NS_IMETHODIMP
mozSqlConnectionODBC::Init(const nsAString &aHost, PRInt32 aPort,
const nsAString &aDatabase, const nsAString &aUsername,
const nsAString &aPassword)
{
if (mConnection)
return NS_OK;
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &mEnv);
SQLSetEnvAttr(mEnv, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, mEnv, &mConnection);
if(!SQL_SUCCEEDED(SQLConnect(mConnection,
(SQLCHAR*) NS_LossyConvertUTF16toASCII(aDatabase).get(),SQL_NTS,
(SQLCHAR*) NS_LossyConvertUTF16toASCII(aUsername).get(),SQL_NTS,
(SQLCHAR*) NS_LossyConvertUTF16toASCII(aPassword).get(),SQL_NTS))){
SetError(mConnection, SQL_HANDLE_DBC);
return NS_ERROR_FAILURE;
}
return NS_OK;
}
NS_IMETHODIMP
mozSqlConnectionODBC::GetPrimaryKeys(const nsAString& aSchema,
const nsAString& aTable,
mozISqlResult** _retval)
{
//SQLPrimaryKeys isn't implemented in most drivers
SQLHSTMT hstmt;
SQLAllocHandle(SQL_HANDLE_STMT, mConnection, &hstmt);
if(!SQL_SUCCEEDED(SQLSpecialColumns(hstmt, SQL_BEST_ROWID, NULL, 0,
(SQLCHAR*) NULL, SQL_NTS,
(SQLCHAR*) NS_LossyConvertUTF16toASCII(aTable).get(), SQL_NTS, 0, 0))){
SetError(hstmt, SQL_HANDLE_STMT);
return NS_ERROR_FAILURE;
}
nsAutoString select;
PRInt32 i = 0;
select.AssignLiteral("");
while (SQL_SUCCEEDED(SQLFetch(hstmt))) {
i++;
if(i > 1) select.AppendLiteral(" UNION ");
select.AppendLiteral("select NULL as TABLE_SCHEM, '");
select.Append(aTable);
select.AppendLiteral("' as TABLE_NAME, '");
SQLCHAR colname[512];
SQLGetData(hstmt, 2, SQL_C_CHAR, colname, sizeof(colname), NULL);
select.AppendASCII((char*)colname);
select.AppendLiteral("' as COLUMN_NAME, ");
char str[16];
sprintf(str, "%d", i);
select.AppendLiteral(str);
select.AppendLiteral(" as KEY_SEQ, NULL as PK_NAME");
}
if(select.IsEmpty()) {
select.AppendLiteral("SELECT * FROM ");
select.Append(aTable);
select.AppendLiteral(" WHERE 1 = 2");
}
select.AppendLiteral(";");
return RealExec(select, _retval, nsnull);
}
nsresult
mozSqlConnectionODBC::RealExec(const nsAString& aQuery,
mozISqlResult** aResult, PRInt32* aAffectedRows)
{
if (!mConnection)
return NS_ERROR_NOT_INITIALIZED;
SQLHSTMT hstmt;
SQLAllocHandle(SQL_HANDLE_STMT, mConnection, &hstmt);
if(!SQL_SUCCEEDED(SQLExecDirect(hstmt, (SQLCHAR*) NS_LossyConvertUTF16toASCII(aQuery).get(), SQL_NTS))){
SetError(hstmt, SQL_HANDLE_STMT);
return NS_ERROR_FAILURE;
}
if (!aResult){
if (!aAffectedRows)
return NS_ERROR_NULL_POINTER;
SQLINTEGER count;
SQLRowCount(hstmt, &count);
*aAffectedRows = (PRInt32) count;
return NS_OK;
}
mozSqlResult *mozresult = new mozSqlResultODBC(this, aQuery);
if (!mozresult){
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
return NS_ERROR_OUT_OF_MEMORY;
}
((mozSqlResultODBC*)mozresult)->SetResult(hstmt);
nsresult rv = mozresult->Init();
if (NS_FAILED(rv)){
delete mozresult;
return rv;
}
*aResult = mozresult;
NS_ADDREF(*aResult);
return NS_OK;
}
nsresult
mozSqlConnectionODBC::CancelExec()
{
// SQLCancel needs the SQLHSTMT, other ways?
return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult
mozSqlConnectionODBC::GetIDName(nsAString& aIDName)
{
// not sure what to return here
aIDName.AssignLiteral("");
return NS_OK;
}
void
mozSqlConnectionODBC::SetError(SQLHANDLE aHandle, SQLSMALLINT aType)
{
SQLCHAR state[6], text[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER native;
char err[256];
SQLINTEGER i = 1;
mErrorMessage.Assign(NS_LITERAL_STRING(""));
while (SQL_SUCCEEDED(SQLGetDiagRec(aType, aHandle, i++, state, &native, text, sizeof(text), NULL))) {
sprintf((char *)err,"%s\nState: %s; Native Error: %lX\n", text, state, native);
mErrorMessage.Append(ToNewUnicode(nsDependentCString((char *)err)));
}
}

View File

@ -0,0 +1,90 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is mozilla.org
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef mozSqlConnectionODBC_h
#define mozSqlConnectionODBC_h
#ifdef XP_WIN
#include <windows.h>
#endif
#include <sql.h>
#include <sqlext.h>
#include "mozSqlConnection.h"
#include "mozISqlConnectionODBC.h"
#define MOZ_SQLCONNECTIONODBC_CLASSNAME "ODBC SQL Connection"
#define MOZ_SQLCONNECTIONODBC_CID \
{0xe90b765a, 0x3346, 0x4ebd, {0xb0, 0xa4, 0x62, 0x35, 0xbe, 0x91, 0x30, 0x7e }}
#define MOZ_SQLCONNECTIONODBC_CONTRACTID "@mozilla.org/sql/connection;1?type=odbc"
class mozSqlConnectionODBC : public mozSqlConnection,
public mozISqlConnectionODBC
{
public:
mozSqlConnectionODBC();
virtual ~mozSqlConnectionODBC();
NS_DECL_ISUPPORTS
NS_IMETHOD Init(const nsAString& aHost, PRInt32 aPort,
const nsAString& aDatabase, const nsAString& aUsername,
const nsAString& aPassword);
NS_IMETHOD GetPrimaryKeys(const nsAString& aSchema, const nsAString& aTable, mozISqlResult** _retval);
NS_DECL_MOZISQLCONNECTIONODBC
protected:
virtual nsresult RealExec(const nsAString& aQuery,
mozISqlResult** aResult, PRInt32* aAffectedRows);
virtual nsresult CancelExec();
virtual nsresult GetIDName(nsAString& aIDName);
private:
SQLHDBC mConnection;
SQLHENV mEnv;
void SetError(SQLHANDLE aHandle, SQLSMALLINT aType);
};
#endif // mozSqlConnectionODBC_h

View File

@ -0,0 +1,200 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is mozilla.org
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "prprf.h"
#include "nsReadableUtils.h"
#include "mozSqlResultODBC.h"
mozSqlResultODBC::mozSqlResultODBC(mozISqlConnection* aConnection,
const nsAString& aQuery)
: mozSqlResult(aConnection, aQuery),
mResult(nsnull)
{
}
void
mozSqlResultODBC::SetResult(SQLHSTMT aResult)
{
mResult = aResult;
}
mozSqlResultODBC::~mozSqlResultODBC()
{
ClearNativeResult();
}
NS_IMPL_ADDREF_INHERITED(mozSqlResultODBC, mozSqlResult)
NS_IMPL_RELEASE_INHERITED(mozSqlResultODBC, mozSqlResult)
// QueryInterface
NS_INTERFACE_MAP_BEGIN(mozSqlResultODBC)
NS_INTERFACE_MAP_ENTRY(mozISqlResultODBC)
NS_INTERFACE_MAP_END_INHERITING(mozSqlResult)
PRInt32
mozSqlResultODBC::GetColType(PRInt32 aColumnIndex)
{
switch (aColumnIndex){
case SQL_INTEGER:
case SQL_NUMERIC:
case SQL_SMALLINT:
case SQL_REAL:
return mozISqlResult::TYPE_INT;
case SQL_FLOAT:
case SQL_DOUBLE:
return mozISqlResult::TYPE_FLOAT;
case SQL_DECIMAL:
return mozISqlResult::TYPE_DECIMAL;
case SQL_TYPE_DATE:
return mozISqlResult::TYPE_DATE;
case SQL_TYPE_TIME:
return mozISqlResult::TYPE_TIME;
case SQL_DATETIME:
return mozISqlResult::TYPE_DATETIME;
default:
return mozISqlResult::TYPE_STRING;
}
}
nsresult
mozSqlResultODBC::BuildColumnInfo()
{
SQLSMALLINT count;
SQLNumResultCols(mResult,&count);
for (PRInt32 i = 0, l = count; i < l; i++) {
SQLCHAR colname[512];
SQLSMALLINT coltype;
SQLDescribeCol(mResult, i + 1, colname, sizeof(colname), NULL, &coltype, NULL, NULL, NULL);
char* n = (char*) colname;
PRUnichar* name = ToNewUnicode(nsDependentCString(n));
PRInt32 type = GetColType(coltype);
nsCAutoString uri(NS_LITERAL_CSTRING("http://www.mozilla.org/SQL-rdf#"));
uri.Append(n);
nsCOMPtr<nsIRDFResource> property;
gRDFService->GetResource(uri, getter_AddRefs(property));
ColumnInfo* columnInfo = ColumnInfo::Create(mAllocator, name, type,
(PRInt32) 0-1, (PRInt32) 0-1,
(PRInt32) 0-1, property);
mColumnInfo.AppendElement(columnInfo);
}
return NS_OK;
}
nsresult
mozSqlResultODBC::BuildRows()
{
while (SQL_SUCCEEDED(SQLFetch(mResult))) {
nsCOMPtr<nsIRDFResource> resource;
nsresult rv = gRDFService->GetAnonymousResource(getter_AddRefs(resource));
if (NS_FAILED(rv)) return rv;
Row* row = Row::Create(mAllocator, resource, mColumnInfo);
for (PRInt32 j = 0; j < mColumnInfo.Count(); j++) {
SQLINTEGER indicator;
SQLCHAR buf[512];
if(SQL_SUCCEEDED(SQLGetData(mResult, j+1, SQL_C_TCHAR, buf, sizeof(buf), &indicator)) && indicator != SQL_NULL_DATA) {
char* value = (char*)buf;
Cell* cell = row->mCells[j];
cell->SetNull(PR_FALSE);
PRInt32 type = cell->GetType();
if (type == mozISqlResult::TYPE_STRING)
cell->SetString(ToNewUnicode(nsDependentCString(value)));
else if (type == mozISqlResult::TYPE_INT)
PR_sscanf(value, "%d", &cell->mInt);
else if (type == mozISqlResult::TYPE_FLOAT)
PR_sscanf(value, "%f", &cell->mFloat);
else if (type == mozISqlResult::TYPE_DECIMAL)
PR_sscanf(value, "%f", &cell->mFloat);
else if (type == mozISqlResult::TYPE_DATE ||
type == mozISqlResult::TYPE_TIME ||
type == mozISqlResult::TYPE_DATETIME)
PR_ParseTimeString(value, PR_FALSE, &cell->mDate);
else if (type == mozISqlResult::TYPE_BOOL)
cell->mBool = !strcmp(value, "t");
}
}
mRows.AppendElement(row);
nsVoidKey key(resource);
mSources.Put(&key, row);
}
return NS_OK;
}
void
mozSqlResultODBC::ClearNativeResult()
{
if (mResult) {
SQLFreeHandle(SQL_HANDLE_STMT, mResult);
mResult = nsnull;
}
}
nsresult
mozSqlResultODBC::CanInsert(PRBool* _retval)
{
return PR_TRUE;
}
nsresult
mozSqlResultODBC::CanUpdate(PRBool* _retval)
{
return PR_TRUE;
}
nsresult
mozSqlResultODBC::CanDelete(PRBool* _retval)
{
return PR_TRUE;
}

View File

@ -0,0 +1,76 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is mozilla.org
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef mozSqlResultODBC_h
#define mozSqlResultODBC_h
#ifdef XP_WIN
#include <windows.h>
#endif
#include <sql.h>
#include <sqlext.h>
#include "mozSqlResult.h"
#include "mozISqlResultODBC.h"
class mozSqlResultODBC : public mozSqlResult,
public mozISqlResultODBC
{
public:
mozSqlResultODBC(mozISqlConnection* aConnection,
const nsAString& aQuery);
void SetResult(SQLHSTMT aResult);
virtual ~mozSqlResultODBC();
NS_DECL_ISUPPORTS
NS_DECL_MOZISQLRESULTODBC
SQLHSTMT mResult;
protected:
PRInt32 GetColType(PRInt32 aColumnIndex);
virtual nsresult BuildColumnInfo();
virtual nsresult BuildRows();
virtual void ClearNativeResult();
virtual nsresult CanInsert(PRBool* _retval);
virtual nsresult CanUpdate(PRBool* _retval);
virtual nsresult CanDelete(PRBool* _retval);
};
#endif // mozSqlResultODBC_h