From 90c02f2de199da4c0c960ffb07d2200351d36dc0 Mon Sep 17 00:00:00 2001 From: "varga%nixcorp.com" Date: Mon, 29 Nov 2004 17:39:09 +0000 Subject: [PATCH] Fix for bug 196576. Add MySQL support. r=me patch by Neil Deakin git-svn-id: svn://10.0.0.236/trunk@165889 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/extensions/sql/Makefile.in | 4 + .../extensions/sql/base/src/mozSqlResult.cpp | 32 ++- .../extensions/sql/base/src/mozSqlResult.h | 12 +- mozilla/extensions/sql/build/Makefile.in | 1 + mozilla/extensions/sql/build/src/Makefile.in | 9 + .../extensions/sql/build/src/mozSqlModule.cpp | 13 + mozilla/extensions/sql/mysql/Makefile.in | 45 ++++ .../extensions/sql/mysql/public/Makefile.in | 49 ++++ .../mysql/public/mozISqlConnectionMysql.idl | 43 ++++ .../sql/mysql/public/mozISqlResultMysql.idl | 43 ++++ mozilla/extensions/sql/mysql/src/Makefile.in | 65 +++++ .../sql/mysql/src/mozSqlConnectionMysql.cpp | 188 +++++++++++++++ .../sql/mysql/src/mozSqlConnectionMysql.h | 85 +++++++ .../sql/mysql/src/mozSqlResultMysql.cpp | 224 ++++++++++++++++++ .../sql/mysql/src/mozSqlResultMysql.h | 75 ++++++ .../sql/pgsql/src/mozSqlResultPgsql.cpp | 2 +- .../sql/sqlite/src/mozSqlResultSqlite.cpp | 2 +- 17 files changed, 873 insertions(+), 19 deletions(-) create mode 100644 mozilla/extensions/sql/mysql/Makefile.in create mode 100644 mozilla/extensions/sql/mysql/public/Makefile.in create mode 100644 mozilla/extensions/sql/mysql/public/mozISqlConnectionMysql.idl create mode 100644 mozilla/extensions/sql/mysql/public/mozISqlResultMysql.idl create mode 100644 mozilla/extensions/sql/mysql/src/Makefile.in create mode 100644 mozilla/extensions/sql/mysql/src/mozSqlConnectionMysql.cpp create mode 100644 mozilla/extensions/sql/mysql/src/mozSqlConnectionMysql.h create mode 100644 mozilla/extensions/sql/mysql/src/mozSqlResultMysql.cpp create mode 100644 mozilla/extensions/sql/mysql/src/mozSqlResultMysql.h diff --git a/mozilla/extensions/sql/Makefile.in b/mozilla/extensions/sql/Makefile.in index 0218af88b7b..7d7c4a4f96d 100644 --- a/mozilla/extensions/sql/Makefile.in +++ b/mozilla/extensions/sql/Makefile.in @@ -51,6 +51,10 @@ ifdef MOZ_ENABLE_SQLITE DIRS += sqlite endif +ifdef MOZ_ENABLE_MYSQL +DIRS += mysql +endif + DIRS += build ifdef ENABLE_TESTS diff --git a/mozilla/extensions/sql/base/src/mozSqlResult.cpp b/mozilla/extensions/sql/base/src/mozSqlResult.cpp index 1d3214f53cb..537ba80b8e8 100644 --- a/mozilla/extensions/sql/base/src/mozSqlResult.cpp +++ b/mozilla/extensions/sql/base/src/mozSqlResult.cpp @@ -1295,22 +1295,28 @@ mozSqlResult::InsertRow(Row* aSrcRow, PRInt32* _retval) return rv; } - nsCOMPtr result; - rv = GetValues(aSrcRow, getter_AddRefs(result), PR_TRUE); - if (NS_FAILED(rv)) - return rv; + nsAutoString IDName; + ((mozSqlConnection*)mConnection.get())->GetIDName(IDName); - PRInt32 rowCount; - result->GetRowCount(&rowCount); - if (rowCount == 0) { - *_retval = 0; - return NS_OK; + // assume that if the IDName is empty that we don't need to re-get the last row + if (!IDName.IsEmpty()){ + nsCOMPtr result; + rv = GetValues(aSrcRow, getter_AddRefs(result), PR_TRUE); + if (NS_FAILED(rv)) + return rv; + + PRInt32 rowCount; + result->GetRowCount(&rowCount); + if (rowCount == 0) { + *_retval = 0; + return NS_OK; + } + + rv = CopyValues(result, aSrcRow); + if (NS_FAILED(rv)) + return rv; } - rv = CopyValues(result, aSrcRow); - if (NS_FAILED(rv)) - return rv; - nsCOMPtr resource; gRDFService->GetAnonymousResource(getter_AddRefs(resource)); diff --git a/mozilla/extensions/sql/base/src/mozSqlResult.h b/mozilla/extensions/sql/base/src/mozSqlResult.h index d10c9f65001..7c352fa4e38 100644 --- a/mozilla/extensions/sql/base/src/mozSqlResult.h +++ b/mozilla/extensions/sql/base/src/mozSqlResult.h @@ -70,9 +70,10 @@ class ColumnInfo { PRInt32 aType, PRInt32 aSize, PRInt32 aMod, + PRBool aIsPrimaryKey, nsIRDFResource* aProperty) { void* place = aAllocator.Alloc(sizeof(ColumnInfo)); - return place ? ::new(place) ColumnInfo(aName, aType, aSize, aMod, aProperty) : nsnull; + return place ? ::new(place) ColumnInfo(aName, aType, aSize, aMod, aIsPrimaryKey, aProperty) : nsnull; } static void @@ -81,11 +82,13 @@ class ColumnInfo { aAllocator.Free(aColumnInfo, sizeof(ColumnInfo)); } - ColumnInfo(PRUnichar* aName, PRInt32 aType, PRInt32 aSize, PRInt32 aMod, nsIRDFResource* aProperty) + ColumnInfo(PRUnichar* aName, PRInt32 aType, PRInt32 aSize, PRInt32 aMod, + PRBool aIsPrimaryKey, nsIRDFResource* aProperty) : mName(aName), mType(aType), mSize(aSize), mMod(aMod), + mIsPrimaryKey(aIsPrimaryKey), mProperty(aProperty) { NS_IF_ADDREF(mProperty); } @@ -100,6 +103,7 @@ class ColumnInfo { PRInt32 mType; PRInt32 mSize; PRInt32 mMod; + PRBool mIsPrimaryKey; nsIRDFResource* mProperty; private: @@ -337,10 +341,10 @@ class mozSqlResult : public mozISqlResult, void ClearRows(); nsresult EnsureTableName(); - nsresult EnsurePrimaryKeys(); + virtual nsresult EnsurePrimaryKeys(); void AppendValue(Cell* aCell, nsAutoString& aValues); - nsresult AppendKeys(Row* aRow, nsAutoString& aKeys); + virtual nsresult AppendKeys(Row* aRow, nsAutoString& aKeys); nsresult GetValues(Row* aRow, mozISqlResult** aResult, PRBool aUseID); nsresult CopyValues(mozISqlResult* aResult, Row* aRow); diff --git a/mozilla/extensions/sql/build/Makefile.in b/mozilla/extensions/sql/build/Makefile.in index ae8dad26d62..536bca35676 100644 --- a/mozilla/extensions/sql/build/Makefile.in +++ b/mozilla/extensions/sql/build/Makefile.in @@ -54,5 +54,6 @@ xpi: bin/components/sql.xpt \ bin/components/sqlpgsql.xpt \ bin/components/sqlsqlite.xpt \ + bin/components/sqlmysql.xpt \ bin/components/$(LIB_PREFIX)sql$(DLL_SUFFIX) \ bin/chrome/sql.jar diff --git a/mozilla/extensions/sql/build/src/Makefile.in b/mozilla/extensions/sql/build/src/Makefile.in index 08b457c5b4d..9f7de8d8764 100644 --- a/mozilla/extensions/sql/build/src/Makefile.in +++ b/mozilla/extensions/sql/build/src/Makefile.in @@ -75,6 +75,12 @@ SHARED_LIBRARY_LIBS += $(DIST)/lib/$(LIB_PREFIX)sqlsqlite_s.$(LIB_SUFFIX) EXTRA_DSO_LDOPTS += -L$(MOZ_SQLITE_LIBS) -lsqlite3 endif +ifdef MOZ_ENABLE_MYSQL +DEFINES += -DMOZ_ENABLE_MYSQL +SHARED_LIBRARY_LIBS += $(DIST)/lib/$(LIB_PREFIX)sqlmysql_s.$(LIB_SUFFIX) +EXTRA_DSO_LDOPTS += -L$(MOZ_MYSQL_LIBS) -lmysqlclient -lz +endif + include $(topsrcdir)/config/rules.mk ifdef MOZ_ENABLE_PGSQL @@ -85,3 +91,6 @@ ifdef MOZ_ENABLE_SQLITE INCLUDES += -I$(MOZ_SQLITE_INCLUDES) endif +ifdef MOZ_ENABLE_MYSQL +INCLUDES += -I$(MOZ_MYSQL_INCLUDES) +endif diff --git a/mozilla/extensions/sql/build/src/mozSqlModule.cpp b/mozilla/extensions/sql/build/src/mozSqlModule.cpp index f9025af0011..706f522f3df 100644 --- a/mozilla/extensions/sql/build/src/mozSqlModule.cpp +++ b/mozilla/extensions/sql/build/src/mozSqlModule.cpp @@ -42,6 +42,9 @@ #ifdef MOZ_ENABLE_SQLITE #include "mozSqlConnectionSqlite.h" #endif +#ifdef MOZ_ENABLE_MYSQL +#include "mozSqlConnectionMysql.h" +#endif NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(mozSqlService, Init) #ifdef MOZ_ENABLE_PGSQL @@ -50,6 +53,9 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(mozSqlConnectionPgsql) #ifdef MOZ_ENABLE_SQLITE NS_GENERIC_FACTORY_CONSTRUCTOR(mozSqlConnectionSqlite) #endif +#ifdef MOZ_ENABLE_MYSQL +NS_GENERIC_FACTORY_CONSTRUCTOR(mozSqlConnectionMysql) +#endif static nsModuleComponentInfo components[] = { @@ -77,6 +83,13 @@ static nsModuleComponentInfo components[] = mozSqlConnectionSqliteConstructor } #endif +#ifdef MOZ_ENABLE_MYSQL + ,{ MOZ_SQLCONNECTIONMYSQL_CLASSNAME, + MOZ_SQLCONNECTIONMYSQL_CID, + MOZ_SQLCONNECTIONMYSQL_CONTRACTID, + mozSqlConnectionMysqlConstructor + } +#endif }; NS_IMPL_NSGETMODULE("mozSqlModule", components) diff --git a/mozilla/extensions/sql/mysql/Makefile.in b/mozilla/extensions/sql/mysql/Makefile.in new file mode 100644 index 00000000000..d00a024cb89 --- /dev/null +++ b/mozilla/extensions/sql/mysql/Makefile.in @@ -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 Jan Varga +# Portions created by the Initial Developer are Copyright (C) 2003 +# 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 diff --git a/mozilla/extensions/sql/mysql/public/Makefile.in b/mozilla/extensions/sql/mysql/public/Makefile.in new file mode 100644 index 00000000000..91692cc2dad --- /dev/null +++ b/mozilla/extensions/sql/mysql/public/Makefile.in @@ -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 Jan Varga +# Portions created by the Initial Developer are Copyright (C) 2003 +# 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 = sqlmysql + +XPIDLSRCS = \ + mozISqlConnectionMysql.idl \ + mozISqlResultMysql.idl \ + $(NULL) + +include $(topsrcdir)/config/rules.mk diff --git a/mozilla/extensions/sql/mysql/public/mozISqlConnectionMysql.idl b/mozilla/extensions/sql/mysql/public/mozISqlConnectionMysql.idl new file mode 100644 index 00000000000..0f3e04fa4e0 --- /dev/null +++ b/mozilla/extensions/sql/mysql/public/mozISqlConnectionMysql.idl @@ -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 Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2004 + * 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(47327928-c789-11d8-9fa2-aa29858f77e5)] + +interface mozISqlConnectionMysql : nsISupports +{ + +}; diff --git a/mozilla/extensions/sql/mysql/public/mozISqlResultMysql.idl b/mozilla/extensions/sql/mysql/public/mozISqlResultMysql.idl new file mode 100644 index 00000000000..0c49adad0de --- /dev/null +++ b/mozilla/extensions/sql/mysql/public/mozISqlResultMysql.idl @@ -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 Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2004 + * 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(4587d956-c789-11d8-96e2-bb3b01bace14)] + +interface mozISqlResultMysql : nsISupports +{ + +}; diff --git a/mozilla/extensions/sql/mysql/src/Makefile.in b/mozilla/extensions/sql/mysql/src/Makefile.in new file mode 100644 index 00000000000..c1956dc27dd --- /dev/null +++ b/mozilla/extensions/sql/mysql/src/Makefile.in @@ -0,0 +1,65 @@ +# ***** 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 Jan Varga +# Portions created by the Initial Developer are Copyright (C) 2003 +# 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 = sqlmysql_s +REQUIRES = xpcom \ + string \ + locale \ + rdf \ + dom \ + layout \ + $(NULL) + +CPPSRCS = \ + mozSqlConnectionMysql.cpp \ + mozSqlResultMysql.cpp + +EXPORTS = \ + mozSqlConnectionMysql.h \ + mozSqlResultMysql.h + +FORCE_STATIC_LIB=1 + +include $(topsrcdir)/config/rules.mk + +INCLUDES += -I$(MOZ_MYSQL_INCLUDES) diff --git a/mozilla/extensions/sql/mysql/src/mozSqlConnectionMysql.cpp b/mozilla/extensions/sql/mysql/src/mozSqlConnectionMysql.cpp new file mode 100644 index 00000000000..be01b9f6827 --- /dev/null +++ b/mozilla/extensions/sql/mysql/src/mozSqlConnectionMysql.cpp @@ -0,0 +1,188 @@ +/* ***** 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 Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2004 + * 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 "mozSqlConnectionMysql.h" +#include "mozSqlResultMysql.h" + +mozSqlConnectionMysql::mozSqlConnectionMysql() + : mConnection(nsnull) +{ +} + +mozSqlConnectionMysql::~mozSqlConnectionMysql() +{ + if (mConnection) + mysql_close(mConnection); +} + +NS_IMPL_ADDREF_INHERITED(mozSqlConnectionMysql, mozSqlConnection) +NS_IMPL_RELEASE_INHERITED(mozSqlConnectionMysql, mozSqlConnection) + +// QueryInterface +NS_INTERFACE_MAP_BEGIN(mozSqlConnectionMysql) + NS_INTERFACE_MAP_ENTRY(mozISqlConnectionMysql) +NS_INTERFACE_MAP_END_INHERITING(mozSqlConnection) + +NS_IMETHODIMP +mozSqlConnectionMysql::Init(const nsAString &aHost, PRInt32 aPort, + const nsAString &aDatabase, const nsAString &aUsername, + const nsAString &aPassword) +{ + if (mConnection) + return NS_OK; + + if (aPort == -1) + aPort = 0; + + mConnection = mysql_init((MYSQL *) nsnull); + if (!mConnection){ + return NS_ERROR_FAILURE; + } + + if (!mysql_real_connect(mConnection, + NS_ConvertUCS2toUTF8(aHost).get(), + NS_ConvertUCS2toUTF8(aUsername).get(), + NS_ConvertUCS2toUTF8(aPassword).get(), + NS_ConvertUCS2toUTF8(aDatabase).get(), + aPort, nsnull, 0)){ + mErrorMessage.Assign(NS_ConvertUTF8toUCS2(mysql_error(mConnection))); + mConnection = nsnull; + + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +NS_IMETHODIMP +mozSqlConnectionMysql::GetServerVersion(nsAString &aServerVersion) +{ + if (!mConnection) + return NS_ERROR_NOT_INITIALIZED; + + if (mServerVersion.IsEmpty()){ + mServerVersion.Assign(NS_ConvertUTF8toUCS2(mysql_get_server_info(mConnection))); + } + aServerVersion.Assign(mServerVersion); + + return NS_OK; +} + +NS_IMETHODIMP +mozSqlConnectionMysql::GetPrimaryKeys(const nsAString& aSchema, + const nsAString& aTable, + mozISqlResult** aResult) +{ + // XXX this can be done with 'show columns from ', but it can't be + // filtered just for primary keys, which are listed in column 3 + + return NS_ERROR_NOT_IMPLEMENTED; +} + +nsresult +mozSqlConnectionMysql::RealExec(const nsAString& aQuery, + mozISqlResult** aResult, PRInt32* aAffectedRows) +{ + if (!mConnection) + return NS_ERROR_NOT_INITIALIZED; + + if (mysql_query(mConnection, NS_ConvertUCS2toUTF8(aQuery).get())){ + mErrorMessage.Assign(NS_ConvertUTF8toUCS2(mysql_error(mConnection))); + + return NS_ERROR_FAILURE; + } + + if (!aResult){ + if (!aAffectedRows) + return NS_ERROR_NULL_POINTER; + + my_ulonglong numrows = (PRInt32)mysql_affected_rows(mConnection); + *aAffectedRows = ((numrows == (my_ulonglong)-1) ? 0 : (PRInt32)numrows); + + return NS_OK; + } + + *aResult = nsnull; + + MYSQL_RES *rowresults = mysql_store_result(mConnection); + if (!rowresults){ + mErrorMessage.Assign(NS_ConvertUTF8toUCS2(mysql_error(mConnection))); + + return NS_ERROR_FAILURE; + } + + mozSqlResult *mozresult = new mozSqlResultMysql(this, aQuery); + if (!mozresult){ + mysql_free_result(rowresults); + return NS_ERROR_OUT_OF_MEMORY; + } + + ((mozSqlResultMysql*)mozresult)->SetResult(rowresults); + nsresult rv = mozresult->Init(); + if (NS_FAILED(rv)){ + delete mozresult; + return rv; + } + + *aResult = mozresult; + NS_ADDREF(*aResult); + + return NS_OK; +} + +nsresult +mozSqlConnectionMysql::CancelExec() +{ + unsigned long id = mysql_thread_id(mConnection); + mysql_kill(mConnection, id); + + if (mysql_errno(mConnection)) { + mErrorMessage.Assign(NS_ConvertUTF8toUCS2(mysql_error(mConnection))); + return NS_ERROR_FAILURE; + } + + mysql_ping(mConnection); + + return NS_OK; +} + +nsresult +mozSqlConnectionMysql::GetIDName(nsAString& aIDName) +{ + aIDName.Truncate(); + + return NS_OK; +} diff --git a/mozilla/extensions/sql/mysql/src/mozSqlConnectionMysql.h b/mozilla/extensions/sql/mysql/src/mozSqlConnectionMysql.h new file mode 100644 index 00000000000..a716ded5049 --- /dev/null +++ b/mozilla/extensions/sql/mysql/src/mozSqlConnectionMysql.h @@ -0,0 +1,85 @@ +/* ***** 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 Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2004 + * 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 mozSqlConnectionMysql_h +#define mozSqlConnectionMysql_h + +#include "mysql.h" + +#include "mozSqlConnection.h" +#include "mozISqlConnectionMysql.h" + +#define MOZ_SQLCONNECTIONMYSQL_CLASSNAME "Mysql SQL Connection" + +#define MOZ_SQLCONNECTIONMYSQL_CID \ +{0x582dcad4, 0xc789, 0x11d8, {0x89, 0xf4, 0xd0, 0x5c, 0x55, 0x80, 0x13, 0xcc }} + +#define MOZ_SQLCONNECTIONMYSQL_CONTRACTID "@mozilla.org/sql/connection;1?type=mysql" + +class mozSqlConnectionMysql : public mozSqlConnection, + public mozISqlConnectionMysql +{ + public: + mozSqlConnectionMysql(); + virtual ~mozSqlConnectionMysql(); + + NS_DECL_ISUPPORTS + + NS_IMETHOD Init(const nsAString& aHost, PRInt32 aPort, + const nsAString& aDatabase, const nsAString& aUsername, + const nsAString& aPassword); + + NS_IMETHODIMP GetServerVersion(nsAString &aServerVersion); + + NS_IMETHOD GetPrimaryKeys(const nsAString& aSchema, const nsAString& aTable, mozISqlResult** _retval); + + NS_DECL_MOZISQLCONNECTIONMYSQL + + MYSQL *mConnection; + + protected: + + virtual nsresult RealExec(const nsAString& aQuery, + mozISqlResult** aResult, PRInt32* aAffectedRows); + + virtual nsresult CancelExec(); + + virtual nsresult GetIDName(nsAString& aIDName); + + private: + // MysqlConnection* mConnection; +}; + +#endif // mozSqlConnectionMysql_h diff --git a/mozilla/extensions/sql/mysql/src/mozSqlResultMysql.cpp b/mozilla/extensions/sql/mysql/src/mozSqlResultMysql.cpp new file mode 100644 index 00000000000..a45127bc37c --- /dev/null +++ b/mozilla/extensions/sql/mysql/src/mozSqlResultMysql.cpp @@ -0,0 +1,224 @@ +/* ***** 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 Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2004 + * 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 "mozSqlResultMysql.h" + +mozSqlResultMysql::mozSqlResultMysql(mozISqlConnection* aConnection, + const nsAString& aQuery) + : mozSqlResult(aConnection, aQuery), + mResult(nsnull) +{ +} + +void +mozSqlResultMysql::SetResult(MYSQL_RES *aResult) +{ + if (mResult){ + mysql_free_result(mResult); + } + + mResult = aResult; +} + +mozSqlResultMysql::~mozSqlResultMysql() +{ + ClearNativeResult(); +} + +NS_IMPL_ADDREF_INHERITED(mozSqlResultMysql, mozSqlResult) +NS_IMPL_RELEASE_INHERITED(mozSqlResultMysql, mozSqlResult) + +// QueryInterface +NS_INTERFACE_MAP_BEGIN(mozSqlResultMysql) + NS_INTERFACE_MAP_ENTRY(mozISqlResultMysql) +NS_INTERFACE_MAP_END_INHERITING(mozSqlResult) + +PRInt32 +mozSqlResultMysql::GetColType(MYSQL_FIELD *aField) +{ + switch (aField->type){ + case FIELD_TYPE_TINY: + case FIELD_TYPE_SHORT: + case FIELD_TYPE_LONG: + case FIELD_TYPE_INT24: + case FIELD_TYPE_LONGLONG: + return mozISqlResult::TYPE_INT; + + case FIELD_TYPE_DECIMAL: + return mozISqlResult::TYPE_DECIMAL; + + case FIELD_TYPE_FLOAT: + case FIELD_TYPE_DOUBLE: + return mozISqlResult::TYPE_FLOAT; + + case FIELD_TYPE_DATE: + return mozISqlResult::TYPE_DATE; + + case FIELD_TYPE_TIME: + return mozISqlResult::TYPE_TIME; + + case FIELD_TYPE_DATETIME: + return mozISqlResult::TYPE_DATETIME; + + default: + // handles these types: + // FIELD_TYPE_TIMESTAMP, FIELD_TYPE_YEAR, FIELD_TYPE_STRING, + // FIELD_TYPE_VAR_STRING, FIELD_TYPE_BLOB, FIELD_TYPE_SET, + // FIELD_TYPE_ENUM, FIELD_TYPE_NULL, FIELD_TYPE_CHAR + + return mozISqlResult::TYPE_STRING; + } +} + +nsresult +mozSqlResultMysql::BuildColumnInfo() +{ + MYSQL_FIELD *field; + + while ((field = mysql_fetch_field(mResult))){ + PRUnichar* name = UTF8ToNewUnicode(nsDependentCString(field->name)); + PRInt32 type = GetColType(field); + + nsCAutoString uri(NS_LITERAL_CSTRING("http://www.mozilla.org/SQL-rdf#")); + uri.Append(field->name); + + nsCOMPtr property; + gRDFService->GetResource(uri, getter_AddRefs(property)); + + ColumnInfo* columnInfo = ColumnInfo::Create(mAllocator, name, type, + (PRInt32)field->length, (PRInt32)field->length, + (field->flags & PRI_KEY_FLAG), property); + mColumnInfo.AppendElement(columnInfo); + } + + return NS_OK; +} + +nsresult +mozSqlResultMysql::BuildRows() +{ + MYSQL_ROW resultrow; + while ((resultrow = mysql_fetch_row(mResult))){ + nsCOMPtr 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++) { + char* value = resultrow[j]; + if (value){ + Cell* cell = row->mCells[j]; + cell->SetNull(PR_FALSE); + PRInt32 type = cell->GetType(); + if (type == mozISqlResult::TYPE_STRING) + cell->SetString(UTF8ToNewUnicode(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 +mozSqlResultMysql::ClearNativeResult() +{ + if (mResult) { + mysql_free_result(mResult); + mResult = nsnull; + } +} + +nsresult +mozSqlResultMysql::EnsurePrimaryKeys() +{ + return NS_OK; +} + +nsresult +mozSqlResultMysql::AppendKeys(Row* aRow, nsAutoString& aKeys) +{ + PRInt32 i; + for (i = 0; i < mColumnInfo.Count(); i++) { + if (((ColumnInfo*)mColumnInfo[i])->mIsPrimaryKey){ + if (i){ + aKeys.Append(NS_LITERAL_STRING(" AND ")); + } + aKeys.Append(((ColumnInfo*)mColumnInfo[i])->mName); + aKeys.Append(PRUnichar('=')); + + Cell* cell = aRow->mCells[i]; + AppendValue(cell, aKeys); + } + } + + return NS_OK; +} + +nsresult +mozSqlResultMysql::CanInsert(PRBool* _retval) +{ + return PR_TRUE; +} + +nsresult +mozSqlResultMysql::CanUpdate(PRBool* _retval) +{ + return PR_TRUE; +} + +nsresult +mozSqlResultMysql::CanDelete(PRBool* _retval) +{ + return PR_TRUE; +} diff --git a/mozilla/extensions/sql/mysql/src/mozSqlResultMysql.h b/mozilla/extensions/sql/mysql/src/mozSqlResultMysql.h new file mode 100644 index 00000000000..90b98f41b30 --- /dev/null +++ b/mozilla/extensions/sql/mysql/src/mozSqlResultMysql.h @@ -0,0 +1,75 @@ +/* ***** 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 Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2004 + * 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 mozSqlResultMysql_h +#define mozSqlResultMysql_h + +#include "mysql.h" + +#include "mozSqlResult.h" +#include "mozISqlResultMysql.h" + +class mozSqlResultMysql : public mozSqlResult, + public mozISqlResultMysql +{ + public: + mozSqlResultMysql(mozISqlConnection* aConnection, + const nsAString& aQuery); + void SetResult(MYSQL_RES *aResult); + virtual ~mozSqlResultMysql(); + + NS_DECL_ISUPPORTS + + NS_DECL_MOZISQLRESULTMYSQL + + protected: + PRInt32 GetColType(MYSQL_FIELD *aField); + + virtual nsresult BuildColumnInfo(); + virtual nsresult BuildRows(); + virtual void ClearNativeResult(); + + nsresult EnsurePrimaryKeys(); + nsresult AppendKeys(Row* aRow, nsAutoString& aKeys); + + virtual nsresult CanInsert(PRBool* _retval); + virtual nsresult CanUpdate(PRBool* _retval); + virtual nsresult CanDelete(PRBool* _retval); + + private: + MYSQL_RES *mResult; +}; + +#endif // mozSqlResultMysql_h diff --git a/mozilla/extensions/sql/pgsql/src/mozSqlResultPgsql.cpp b/mozilla/extensions/sql/pgsql/src/mozSqlResultPgsql.cpp index 1e33967d7c7..30f20809424 100644 --- a/mozilla/extensions/sql/pgsql/src/mozSqlResultPgsql.cpp +++ b/mozilla/extensions/sql/pgsql/src/mozSqlResultPgsql.cpp @@ -116,7 +116,7 @@ mozSqlResultPgsql::BuildColumnInfo() nsCOMPtr property; gRDFService->GetResource(uri, getter_AddRefs(property)); - ColumnInfo* columnInfo = ColumnInfo::Create(mAllocator, name, type, size, mod, property); + ColumnInfo* columnInfo = ColumnInfo::Create(mAllocator, name, type, size, mod, PR_FALSE, property); mColumnInfo.AppendElement(columnInfo); } diff --git a/mozilla/extensions/sql/sqlite/src/mozSqlResultSqlite.cpp b/mozilla/extensions/sql/sqlite/src/mozSqlResultSqlite.cpp index 1605950333b..b66284ff180 100644 --- a/mozilla/extensions/sql/sqlite/src/mozSqlResultSqlite.cpp +++ b/mozilla/extensions/sql/sqlite/src/mozSqlResultSqlite.cpp @@ -92,7 +92,7 @@ mozSqlResultSqlite::BuildColumnInfo() gRDFService->GetResource(uri, getter_AddRefs(property)); ColumnInfo* columnInfo = ColumnInfo::Create(mAllocator, name, type, size, - mod, property); + mod, PR_FALSE, property); mColumnInfo.AppendElement(columnInfo); }