diff -urN sql.orig/Makefile.in sql/Makefile.in
--- sql.orig/Makefile.in	Tue Aug  3 17:16:28 2004
+++ sql/Makefile.in	Mon Aug  9 17:21:17 2004
@@ -46,6 +46,10 @@
 DIRS		+= pgsql
 endif
 
+ifdef MOZ_ENABLE_SQLITE
+DIRS            += sqlite
+endif
+
 DIRS		+= build
 
 ifdef ENABLE_TESTS
diff -urN sql.orig/base/src/mozSqlResult.cpp sql/base/src/mozSqlResult.cpp
--- sql.orig/base/src/mozSqlResult.cpp	Tue Aug  3 17:16:28 2004
+++ sql/base/src/mozSqlResult.cpp	Mon Aug  9 17:21:17 2004
@@ -1198,7 +1198,7 @@
 
   nsCOMPtr<mozISqlResult> result;
   nsresult rv = mConnection->ExecuteQuery(query, getter_AddRefs(result));
-
+  
   if (NS_FAILED(rv)) {
     mConnection->GetErrorMessage(mErrorMessage);
     return rv;
diff -urN sql.orig/build/Makefile.in sql/build/Makefile.in
--- sql.orig/build/Makefile.in	Tue Aug  3 17:16:28 2004
+++ sql/build/Makefile.in	Mon Aug  9 17:21:17 2004
@@ -52,5 +52,6 @@
 	cd $(DIST); zip -r $(XPI_FILE) \
 	bin/components/sql.xpt \
 	bin/components/sqlpgsql.xpt \
+	bin/components/sqlsqlite.xpt \
 	bin/components/$(LIB_PREFIX)sql$(DLL_SUFFIX) \
 	bin/chrome/sql.jar
diff -urN sql.orig/build/src/Makefile.in sql/build/src/Makefile.in
--- sql.orig/build/src/Makefile.in	Tue Aug  3 17:16:28 2004
+++ sql/build/src/Makefile.in	Mon Aug  9 17:21:17 2004
@@ -68,8 +68,19 @@
 EXTRA_DSO_LDOPTS    	+= -L$(MOZ_PGSQL_LIBS) -lpq
 endif
 
+ifdef MOZ_ENABLE_SQLITE
+DEFINES					+= -DMOZ_ENABLE_SQLITE
+SHARED_LIBRARY_LIBS     += $(DIST)/lib/$(LIB_PREFIX)sqlsqlite_s.$(LIB_SUFFIX)
+EXTRA_DSO_LDOPTS        += -L$(MOZ_SQLITE_LIBS) -lsqlite3
+endif
+
 include $(topsrcdir)/config/rules.mk
 
 ifdef MOZ_ENABLE_PGSQL
 INCLUDES		+= -I$(MOZ_PGSQL_INCLUDES)
 endif
+
+ifdef MOZ_ENABLE_SQLITE
+INCLUDES        += -I$(MOZ_SQLITE_INCLUDES)
+endif
+
diff -urN sql.orig/build/src/mozSqlModule.cpp sql/build/src/mozSqlModule.cpp
--- sql.orig/build/src/mozSqlModule.cpp	Tue Aug  3 17:16:28 2004
+++ sql/build/src/mozSqlModule.cpp	Mon Aug  9 17:21:17 2004
@@ -38,11 +38,17 @@
 #ifdef MOZ_ENABLE_PGSQL
 #include "mozSqlConnectionPgsql.h"
 #endif
+#ifdef MOZ_ENABLE_SQLITE
+#include "mozSqlConnectionSqlite.h"
+#endif
 
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(mozSqlService, Init)
 #ifdef MOZ_ENABLE_PGSQL
 NS_GENERIC_FACTORY_CONSTRUCTOR(mozSqlConnectionPgsql)
 #endif
+#ifdef MOZ_ENABLE_SQLITE
+NS_GENERIC_FACTORY_CONSTRUCTOR(mozSqlConnectionSqlite)
+#endif
 
 static nsModuleComponentInfo components[] =
 {
@@ -55,12 +61,19 @@
     MOZ_SQLSERVICE_CID,
     MOZ_SQLDATASOURCE_CONTRACTID,
     mozSqlServiceConstructor
-  },
+  }
 #ifdef MOZ_ENABLE_PGSQL
-  { MOZ_SQLCONNECTIONPGSQL_CLASSNAME,
+  ,{ MOZ_SQLCONNECTIONPGSQL_CLASSNAME,
     MOZ_SQLCONNECTIONPGSQL_CID,
     MOZ_SQLCONNECTIONPGSQL_CONTRACTID,
     mozSqlConnectionPgsqlConstructor
+  }
+#endif
+#ifdef MOZ_ENABLE_SQLITE
+  ,{ MOZ_SQLCONNECTIONSQLITE_CLASSNAME,
+    MOZ_SQLCONNECTIONSQLITE_CID,
+    MOZ_SQLCONNECTIONSQLITE_CONTRACTID,
+    mozSqlConnectionSqliteConstructor
   }
 #endif
 };
diff -urN sql.orig/sqlite/Makefile.in sql/sqlite/Makefile.in
--- sql.orig/sqlite/Makefile.in	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/Makefile.in	Mon Aug  9 17:21:17 2004
@@ -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 -urN sql.orig/sqlite/public/Makefile.in sql/sqlite/public/Makefile.in
--- sql.orig/sqlite/public/Makefile.in	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/public/Makefile.in	Mon Aug  9 17:21:17 2004
@@ -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	= sqlsqlite
+
+XPIDLSRCS	= \
+		mozISqlConnectionSqlite.idl \
+		mozISqlResultSqlite.idl \
+		$(NULL)
+
+include $(topsrcdir)/config/rules.mk
diff -urN sql.orig/sqlite/public/mozISqlConnectionSqlite.idl sql/sqlite/public/mozISqlConnectionSqlite.idl
--- sql.orig/sqlite/public/mozISqlConnectionSqlite.idl	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/public/mozISqlConnectionSqlite.idl	Mon Aug  9 17:21:17 2004
@@ -0,0 +1,44 @@
+/* ***** 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):
+ *  Valia Vaneeva <fattie@altlinux.ru>
+ *
+ * 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(b00f42b4-8902-428c-ab13-9d066ff15aed)]
+
+interface mozISqlConnectionSqlite : nsISupports
+{
+
+};
diff -urN sql.orig/sqlite/public/mozISqlResultSqlite.idl sql/sqlite/public/mozISqlResultSqlite.idl
--- sql.orig/sqlite/public/mozISqlResultSqlite.idl	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/public/mozISqlResultSqlite.idl	Mon Aug  9 17:21:17 2004
@@ -0,0 +1,44 @@
+/* ***** 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):
+ *  Valia Vaneeva <fattie@altlinux.ru>
+ *
+ * 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(96d1248b-608c-4b63-a718-b214200e4738)]
+
+interface mozISqlResultSqlite : nsISupports
+{
+
+};
diff -urN sql.orig/sqlite/src/Makefile.in sql/sqlite/src/Makefile.in
--- sql.orig/sqlite/src/Makefile.in	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/src/Makefile.in	Mon Aug  9 17:21:17 2004
@@ -0,0 +1,66 @@
+# ***** 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	= sqlsqlite_s
+
+REQUIRES	= xpcom \
+		  string \
+		  locale \
+		  rdf \
+		  dom \
+		  layout \
+		  $(NULL)
+
+CPPSRCS		= \
+		mozSqlConnectionSqlite.cpp \
+		mozSqlResultSqlite.cpp
+
+EXPORTS		= \
+		mozSqlConnectionSqlite.h \
+		mozSqlResultSqlite.h
+
+FORCE_STATIC_LIB=1
+
+include $(topsrcdir)/config/rules.mk
+
+INCLUDES	+= -I$(MOZ_SQLITE_INCLUDES)
diff -urN sql.orig/sqlite/src/mozSqlConnectionSqlite.cpp sql/sqlite/src/mozSqlConnectionSqlite.cpp
--- sql.orig/sqlite/src/mozSqlConnectionSqlite.cpp	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/src/mozSqlConnectionSqlite.cpp	Mon Aug  9 17:21:17 2004
@@ -0,0 +1,347 @@
+/* ***** 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):
+ *  Valia Vaneeva <fattie@altlinux.ru>
+ *
+ * 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 "mozSqlConnectionSqlite.h"
+#include "mozSqlResultSqlite.h"
+#include <ctype.h>
+#include "nsLocalFile.h"
+#include "nsAppDirectoryServiceDefs.h"
+#include "unicharutil/nsUnicharUtils.h"
+
+mozSqlConnectionSqlite::mozSqlConnectionSqlite()
+  : mConnection(nsnull)
+{
+}
+
+mozSqlConnectionSqlite::~mozSqlConnectionSqlite()
+{
+  if (mConnection)
+    sqlite3_close(mConnection);
+}
+
+NS_IMPL_ADDREF_INHERITED(mozSqlConnectionSqlite, mozSqlConnection)
+NS_IMPL_RELEASE_INHERITED(mozSqlConnectionSqlite, mozSqlConnection)
+
+// QueryInterface
+NS_INTERFACE_MAP_BEGIN(mozSqlConnectionSqlite)
+  NS_INTERFACE_MAP_ENTRY(mozISqlConnectionSqlite)
+NS_INTERFACE_MAP_END_INHERITING(mozSqlConnection)
+
+NS_IMETHODIMP
+mozSqlConnectionSqlite::Init(const nsAString& aHost, PRInt32 aPort,
+                    const nsAString& aDatabase, const nsAString& aUsername,
+		    const nsAString& aPassword)
+{
+  if (mConnection)
+    return NS_OK;
+
+  nsAString::const_iterator start;
+  nsresult rv;
+  nsAutoString aPath;
+  aDatabase.BeginReading(start);
+
+  nsCOMPtr<nsILocalFile> aFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID,
+  												 &rv));
+  if (NS_FAILED(rv))
+    return rv;
+// someone need to check if I do the following right; tested for macosx and
+// linux. hope it works for winxp
+#if defined(XP_WIN) || defined(XP_OS2)
+  if (*++start == ':') {
+#elif defined(XP_UNIX) || defined(XP_BEOS) || defined(XP_MACOSX)
+  if (*start == '/') {
+#elif defined(XP_MAC)
+  PRBool hasColon = PR_FALSE;
+  nsAString::const_iterator end;
+  aDatabase.EndReading(end);
+  --end;
+  while (end != start && *end != ':')
+  	--end;
+  if (*end == ':')
+  	hasColon = PR_TRUE;
+  if (*start != ':' && hasColon) {
+#else
+#error Do not know how to parse other platforms paths
+#endif
+    rv = aFile->InitWithPath(aDatabase);
+    if (NS_FAILED(rv))
+  	  return NS_ERROR_FAILURE;
+  	aPath = aDatabase;  }
+  else {
+    nsCOMPtr<nsIProperties> directoryService = do_GetService(
+    										NS_DIRECTORY_SERVICE_CONTRACTID,
+    										&rv);
+    if (NS_FAILED(rv))
+      return rv;
+    rv = directoryService->Get(NS_APP_USER_PROFILE_50_DIR,
+  							   NS_GET_IID(nsILocalFile), getter_AddRefs(aFile));
+    if (NS_FAILED(rv))
+      return rv;
+    rv = aFile->Append(aDatabase);
+    if (NS_FAILED(rv))
+      return rv;
+    aFile->GetPath(aPath);
+    if (aPath.IsEmpty())
+      return NS_ERROR_FAILURE;
+  }
+
+  PRBool aWritable = PR_TRUE;
+  rv = aFile->IsWritable(&aWritable);
+  if (NS_FAILED(rv))
+  	return NS_ERROR_FAILURE;
+  	
+  rv = sqlite3_open(NS_ConvertUCS2toUTF8(aPath).get(), &mConnection);
+  if (rv != SQLITE_OK)
+	return rv;
+
+  return Setup();
+}
+
+NS_IMETHODIMP
+mozSqlConnectionSqlite::GetPrimaryKeys(const nsAString& aSchema, const nsAString& aTable, mozISqlResult** _retval)
+{
+  if (! mConnection)
+    return NS_ERROR_NOT_INITIALIZED;
+    
+  char **r, *errmsg;
+  PRInt32 stat, nrow, ncolumn;
+  nsAutoString preselect, select1, select2, uni, select, common;
+  
+  common.AssignLiteral("select NULL as TABLE_SCHEM, '");
+  select1.AssignLiteral("' as COLUMN_NAME, ");
+  select2.AssignLiteral(" as KEY_SEQ, NULL as PK_NAME");
+  uni.AssignLiteral(" UNION ");
+  
+  preselect.AssignLiteral("select sql from sqlite_master where type='table' and name='");
+  if (!aTable.IsEmpty()) {
+    preselect.Append(aTable);
+    common.Append(aTable);
+  }
+  else
+    return NS_ERROR_FAILURE;
+  preselect.Append(NS_LITERAL_STRING("';"));
+  common.Append(NS_LITERAL_STRING("' as TABLE_NAME, '"));
+  
+  stat = sqlite3_get_table(mConnection, NS_ConvertUCS2toUTF8(preselect).get(), &r, &nrow, &ncolumn, &errmsg);
+  if (stat != SQLITE_OK) {
+    CopyUTF8toUTF16(errmsg, mErrorMessage);
+    sqlite3_free_table(r);
+    return NS_ERROR_FAILURE;
+  }
+  
+  nsAutoString aToken = NS_ConvertUTF8toUCS2(nsDependentCString("PRIMARY KEY")), aKeyColumn;
+  NS_ConvertUTF8toUCS2 buffer(r[1]);
+  nsAString::const_iterator start, end, iter, iter2;
+  buffer.BeginReading(start);
+  buffer.EndReading(end);
+  
+  if (CaseInsensitiveFindInReadable(aToken, start, end))    iter = end;
+  else
+  	return NS_ERROR_FAILURE;
+  buffer.BeginReading(start);
+  buffer.EndReading(end);
+  PRInt32 count = 1;
+  while (iter != end && (*iter == PRUnichar(' ') || *iter == PRUnichar('\t') || *iter == PRUnichar('\n')))
+    ++iter;
+  if (*iter == PRUnichar('(')) {
+    char str[16];
+    ++iter;
+    while (iter != end && *iter != PRUnichar(')')) {
+	  while ((*iter == PRUnichar(' ') || *iter == PRUnichar('\n') || *iter == PRUnichar('\t') || *iter == PRUnichar(',')) &&  *iter != PRUnichar(')') && iter != end)
+	    ++iter;
+      if (iter != end && *iter != PRUnichar(')')) {
+      	if (count > 1)
+  	  	  select.Append(uni);
+        select.Append(common);
+		iter2 = iter;
+    	while (iter2 != end && *iter2 != PRUnichar(',') &&
+    		   *iter2 != PRUnichar(' ') && *iter2 != PRUnichar('\n') &&
+    		   *iter2 != PRUnichar('\t') && *iter2 != PRUnichar(')'))
+      	  ++iter2;
+    	aKeyColumn = Substring(iter,iter2);
+    	select.Append(aKeyColumn);
+    	iter = iter2;
+      	select.Append(select1);
+  		PRInt32 i = 0, j, tmp, cnt = count;
+  		do { 		  str[i++] = cnt % 10 + 48;		  str[i] = '\0';		} while ((cnt /= 10) > 0);
+   		for (i = 0, j = strlen(str) - 1; i < j; i++, j--) {		  tmp = str[i];
+          str[i] = str[j];          str[j] = tmp;    	}
+      	select.Append(UTF8ToNewUnicode(nsDependentCString(str)));
+      	select.Append(select2);
+      	count++;
+      }
+  	}
+  }
+  else {
+  	PRInt32 openParenth = 0;
+  	while (iter != start && (*iter != PRUnichar(',') && openParenth == 0)) {
+  	  if (*iter == PRUnichar(')'))
+  	  	openParenth++;
+  	  else if (*iter == PRUnichar('('))
+  	  	openParenth--;
+  	  --iter;
+  	}
+  	if (iter == start) {
+  	  while (*iter != PRUnichar('(') && iter != end)
+  	    ++iter;
+  	}
+  	++iter;
+  	while ((*iter == PRUnichar(' ') || *iter == PRUnichar('\n') || *iter == PRUnichar('\t')) && iter != end)
+  	  ++iter;
+  	select.Append(common);
+	iter2 = iter;
+      while (iter2 != end && *iter2 != PRUnichar(' ') && *iter2 != PRUnichar('\n') && *iter2 != PRUnichar('\t'))
+      	++iter2;
+    aKeyColumn = Substring(iter,iter2);
+    select.Append(aKeyColumn);
+  	select.Append(select1);
+    select.Append(PRUnichar('1'));
+    select.Append(select2);
+  }
+  select.Append(PRUnichar(';'));
+  
+  sqlite3_free_table(r);
+
+  return RealExec(select, _retval, nsnull);
+}
+
+nsresult
+mozSqlConnectionSqlite::Setup()
+{
+  if (sqlite3_errcode(mConnection) != SQLITE_OK) {
+  	CopyUTF8toUTF16(sqlite3_errmsg(mConnection), mErrorMessage);
+    
+    mConnection = nsnull;
+    return NS_ERROR_FAILURE;
+  }
+
+  NS_ConvertUTF8toUCS2 buffer(sqlite3_version);
+  nsAString::const_iterator start, end, iter;
+  PRInt32 numbers[3] = {0,0,0};
+  buffer.BeginReading(iter);
+  buffer.EndReading(end);
+  for (PRInt32 i = 0; i < 3; i++) {
+    start = iter;
+    while (iter != end && *iter != PRUnichar('.'))
+      ++iter;
+    nsAutoString v(Substring(start,iter));
+    PRInt32 err;
+    numbers[i] = v.ToInteger(&err);
+    while (iter != end && *iter == PRUnichar('.'))
+      ++iter;
+  }
+  mVersion = SERVER_VERSION(numbers[0], numbers[1], numbers[2]);
+
+  if (mVersion < SERVER_VERSION(3,0,2))
+    return NS_ERROR_FAILURE;
+
+  return NS_OK;
+}
+
+nsresult
+mozSqlConnectionSqlite::RealExec(const nsAString& aQuery,
+                                mozISqlResult** aResult, PRInt32* aAffectedRows)
+{
+  //sqlite3_changes doesn't reset its count to 0 after selects
+  static PRInt32 oldChange = 0;
+  
+  if (! mConnection)
+    return NS_ERROR_NOT_INITIALIZED;
+
+  char **r, *errmsg;
+  PRInt32 stat, nrow, ncolumn;
+
+  stat = sqlite3_get_table(mConnection, NS_ConvertUCS2toUTF8(aQuery).get(), &r, &nrow, &ncolumn, &errmsg);
+  PRInt32 changed = sqlite3_total_changes(mConnection) - oldChange;
+  oldChange += changed;
+
+  if (stat == SQLITE_OK && !changed) {
+    if (!aResult)
+      return NS_ERROR_NULL_POINTER;
+    
+    if (*aResult) {
+      ((mozSqlResultSqlite*)*aResult)->SetResult(r, nrow, ncolumn, aWritable);
+      nsresult rv = ((mozSqlResult*)*aResult)->Rebuild();
+      if (NS_FAILED(rv))
+        return rv;
+      NS_ADDREF(*aResult);
+    }
+    else {
+      mozSqlResult* result = new mozSqlResultSqlite(this, aQuery);
+      if (!result)
+        return NS_ERROR_OUT_OF_MEMORY;
+      ((mozSqlResultSqlite*)result)->SetResult(r, nrow, ncolumn, aWritable);
+      nsresult rv = result->Init();
+      if (NS_FAILED(rv))
+        return rv;
+      NS_ADDREF(*aResult = result);
+    }
+  }
+  else if (stat == SQLITE_OK && changed) {
+    if (!aAffectedRows)
+      return NS_ERROR_NULL_POINTER;
+    *aAffectedRows = changed;
+    mLastID = sqlite3_last_insert_rowid(mConnection);
+  }
+  else {
+    CopyUTF8toUTF16(errmsg, mErrorMessage);
+    sqlite3_free_table(r);
+    return NS_ERROR_FAILURE;
+  }
+
+  return NS_OK;
+}
+
+nsresult
+mozSqlConnectionSqlite::CancelExec()
+{
+  sqlite3_interrupt(mConnection);
+  
+  if (sqlite3_errcode(mConnection) != SQLITE_OK) {
+  	CopyUTF8toUTF16(sqlite3_errmsg(mConnection), mErrorMessage);
+    return NS_ERROR_FAILURE;
+  }
+
+  return NS_OK;
+}
+
+nsresult
+mozSqlConnectionSqlite::GetIDName(nsAString& aIDName)
+{
+  aIDName.AssignLiteral("OID");
+  return NS_OK;
+}
diff -urN sql.orig/sqlite/src/mozSqlConnectionSqlite.h sql/sqlite/src/mozSqlConnectionSqlite.h
--- sql.orig/sqlite/src/mozSqlConnectionSqlite.h	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/src/mozSqlConnectionSqlite.h	Mon Aug  9 17:21:17 2004
@@ -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 Jan Varga
+ * Portions created by the Initial Developer are Copyright (C) 2003
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *  Valia Vaneeva <fattie@altlinux.ru>
+ *
+ * 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 mozSqlConnectionSqlite_h
+#define mozSqlConnectionSqlite_h                                                  
+
+#include "sqlite3.h"
+#include "mozSqlConnection.h"
+#include "mozISqlConnectionSqlite.h"
+
+#define MOZ_SQLCONNECTIONSQLITE_CLASSNAME "SQLite SQL Connection"
+#define MOZ_SQLCONNECTIONSQLITE_CID \
+{0xb00f42b4, 0x8902, 0x428c, {0xab, 0x13, 0x9d, 0x06, 0x6f, 0xf1, 0x5a, 0xed }}
+
+#define MOZ_SQLCONNECTIONSQLITE_CONTRACTID "@mozilla.org/sql/connection;1?type=sqlite"
+
+#define SERVER_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
+
+class mozSqlConnectionSqlite : public mozSqlConnection,
+                              public mozISqlConnectionSqlite
+{
+  public:
+    mozSqlConnectionSqlite();
+    virtual ~mozSqlConnectionSqlite();
+
+    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_MOZISQLCONNECTIONSQLITE
+
+  protected:
+    nsresult Setup();
+
+    virtual nsresult RealExec(const nsAString& aQuery,
+                              mozISqlResult** aResult, PRInt32* aAffectedRows);
+
+    virtual nsresult CancelExec();
+
+    virtual nsresult GetIDName(nsAString& aIDName);
+
+  private:
+    sqlite3 *           mConnection;
+    PRInt32             mVersion;
+    PRBool				aWritable;
+};
+
+#endif // mozSqlConnectionSqlite_h
diff -urN sql.orig/sqlite/src/mozSqlConnectionSqlite2.cpp sql/sqlite/src/mozSqlConnectionSqlite2.cpp
--- sql.orig/sqlite/src/mozSqlConnectionSqlite2.cpp	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/src/mozSqlConnectionSqlite2.cpp	Mon Aug  9 17:21:17 2004
@@ -0,0 +1,353 @@
+/* ***** 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):
+ *  Valia Vaneeva <fattie@altlinux.ru>
+ *
+ * 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 "mozSqlConnectionSqlite.h"
+#include "mozSqlResultSqlite.h"
+#include <ctype.h>
+#include "nsLocalFile.h"
+#include "nsAppDirectoryServiceDefs.h"
+
+mozSqlConnectionSqlite::mozSqlConnectionSqlite()
+  : mConnection(nsnull)
+{
+}
+
+mozSqlConnectionSqlite::~mozSqlConnectionSqlite()
+{
+  if (mConnection)
+    sqlite3_close(mConnection);
+}
+
+NS_IMPL_ADDREF_INHERITED(mozSqlConnectionSqlite, mozSqlConnection)
+NS_IMPL_RELEASE_INHERITED(mozSqlConnectionSqlite, mozSqlConnection)
+
+// QueryInterface
+NS_INTERFACE_MAP_BEGIN(mozSqlConnectionSqlite)
+  NS_INTERFACE_MAP_ENTRY(mozISqlConnectionSqlite)
+NS_INTERFACE_MAP_END_INHERITING(mozSqlConnection)
+
+NS_IMETHODIMP
+mozSqlConnectionSqlite::Init(const nsAString& aHost, PRInt32 aPort,
+                    const nsAString& aDatabase, const nsAString& aUsername,
+		    const nsAString& aPassword)
+{
+  if (mConnection)
+    return NS_OK;
+
+  nsAString::const_iterator start;
+  nsresult rv;
+  nsAutoString aPath;
+  aDatabase.BeginReading(start);
+
+  nsCOMPtr<nsILocalFile> aFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID,
+  												 &rv));
+  if (NS_FAILED(rv))
+    return rv;
+// someone need to check if I do the following right; tested for macosx and
+// linux. hope it works for winxp
+#if defined(XP_WIN) || defined(XP_OS2)
+  if (*++start == ':') {
+#elif defined(XP_UNIX) || defined(XP_BEOS) || defined(XP_MACOSX)
+  if (*start == '/') {
+#elif defined(XP_MAC)
+  PRBool hasColon = PR_FALSE;
+  nsAString::const_iterator end;
+  aDatabase.EndReading(end);
+  --end;
+  while (end != start && *end != ':')
+  	--end;
+  if (*end == ':')
+  	hasColon = PR_TRUE;
+  if (*start != ':' && hasColon) {
+#else
+#error Do not know how to parse other platforms paths
+#endif
+    rv = aFile->InitWithPath(aDatabase);
+    if (NS_FAILED(rv))
+  	  return NS_ERROR_FAILURE;
+  	aPath = aDatabase;  }
+  else {
+    nsCOMPtr<nsIProperties> directoryService = do_GetService(
+    										NS_DIRECTORY_SERVICE_CONTRACTID,
+    										&rv);
+    if (NS_FAILED(rv))
+      return rv;
+    rv = directoryService->Get(NS_APP_USER_PROFILE_50_DIR,
+  							   NS_GET_IID(nsILocalFile), getter_AddRefs(aFile));
+    if (NS_FAILED(rv))
+      return rv;
+    rv = aFile->Append(aDatabase);
+    if (NS_FAILED(rv))
+      return rv;
+    aFile->GetPath(aPath);
+    if (aPath.IsEmpty())
+      return NS_ERROR_FAILURE;
+  }
+
+  PRBool aWritable = PR_TRUE;
+  rv = aFile->IsWritable(&aWritable);
+  if (NS_FAILED(rv))
+  	return NS_ERROR_FAILURE;
+  	
+  rv = sqlite3_open(NS_ConvertUCS2toUTF8(aPath).get(), &mConnection);
+  if (rv != SQLITE_OK)
+	return rv;
+
+  return Setup();
+}
+
+NS_IMETHODIMP
+mozSqlConnectionSqlite::GetPrimaryKeys(const nsAString& aSchema, const nsAString& aTable, mozISqlResult** _retval)
+{
+  if (! mConnection)
+    return NS_ERROR_NOT_INITIALIZED;
+    
+  char **r, *errmsg;
+  PRInt32 stat, nrow, ncolumn;
+  nsAutoString preselect, select1, select2, uni, select, common;
+  
+  common.AssignLiteral("select NULL as TABLE_SCHEM, '");
+  select1.AssignLiteral("' as COLUMN_NAME, ");
+  select2.AssignLiteral(" as KEY_SEQ, NULL as PK_NAME");
+  uni.AssignLiteral(" UNION ");
+  
+  preselect.AssignLiteral("select sql from sqlite_master where type='table' and name='");
+  if (!aTable.IsEmpty()) {
+    preselect.Append(aTable);
+    common.Append(aTable);
+  }
+  else
+    return NS_ERROR_FAILURE;
+  preselect.Append(NS_LITERAL_STRING("';"));
+  common.Append(NS_LITERAL_STRING("' as TABLE_NAME, '"));
+  
+  stat = sqlite3_get_table(mConnection, NS_ConvertUCS2toUTF8(preselect).get(), &r, &nrow, &ncolumn, &errmsg);
+  if (stat != SQLITE_OK) {
+    CopyUTF8toUTF16(errmsg, mErrorMessage);
+    sqlite3_free_table(r);
+    return NS_ERROR_FAILURE;
+  }
+  
+//  const char *aToken = "primary key";
+//  char *ptr;
+  nsAutoString aToken = NS_LITERAL_STRING("primary key");
+  NS_ConvertUTF8toUCS2 buffer(r[1]);
+  nsAString::const_iterator start, end, iter;
+  buffer.BeginReading(start);
+  buffer.EndReading(end);
+  
+//  ptr = strcasestr(r[1], aToken);
+//  if (!ptr)
+//  	return NS_ERROR_FAILURE;
+  if (FindInReadable(aToken, start, end))    iter = end;
+  else
+  	return NS_ERROR_FAILURE;
+  buffer.EndReading(end);
+  PRInt32 count = 1;
+//  ptr += 11;
+//  unsigned int ind = ptr - r[1];
+//  while ((r[1][ind] == ' ' || r[1][ind] == '\n' || r[1][ind] == '\t') && r[1][ind] != '\0')
+//  	ind++;
+  while (iter != end && (*iter == PRUnichar(' ') || *iter == PRUnichar('\t') || *iter == PRUnichar('\n')))
+      ++iter;
+//  if (r[1][ind] == '(') {
+  if (*iter == PRUnichar('(') {
+//  	PRInt32 q = ind + 1;
+  ++iter;
+//  	char str[16];
+//  	while (r[1][q] != '\0' && r[1][q] != ')') {
+  while (iter != end && *iter != PRUnichar(')') {
+//  	  while ((r[1][q] == ' ' || r[1][q] == '\n' || r[1][q] == '\t' || r[1][q] == ',') && r[1][q] != '\0' && r[1][q] != ')')
+//        q++;
+	while ((*iter == PRUnichar(' ') || *iter == PRUnichar('\n') || *iter == PRUnichar('\t')) &&  *iter != PRUnichar(')') && iter != end)
+	  ++iter;
+    if (r[1][q] != ' ' && r[1][q] != '\n' && r[1][q] != '\t' && r[1][q] != ',' && r[1][q] != '\0' && r[1][q] != ')') {
+      	if (count > 1)
+  	  	  select.Append(uni);
+        select.Append(common);
+       	select.Append(PRUnichar(r[1][q++]));
+  	    while (r[1][q] != ' ' && r[1][q] != '\n' && r[1][q] != '\t' && r[1][q] != ',' && r[1][q] != '\0' && r[1][q] != ')')
+  	      select.Append(PRUnichar(r[1][q++]));
+      	select.Append(select1);
+  		int i = 0, j, tmp, cnt = count;
+  		do { 		  str[i++] = cnt % 10 + 48;		  str[i] = '\0';		} while ((cnt /= 10) > 0);
+   		for (i = 0, j = strlen(str) - 1; i < j; i++, j--) {		  tmp = str[i];
+          str[i] = str[j];          str[j] = tmp;    	}
+      	select.Append(UTF8ToNewUnicode(nsDependentCString(str)));
+      	select.Append(select2);
+      	count++;
+      }
+  	}
+  }
+  else {
+  	PRInt32 q = ind;
+  	PRInt32 openParenth = 0;
+  	while (q >= 0 || (r[1][q] != ',' && openParenth == 0)) {
+  	  if (r[1][q] == ')')
+  	  	openParenth++;
+  	  else if (r[1][q] == '(')
+  	  	openParenth--;
+  	  q--;
+  	}
+  	q++;
+  	if (q == 0) {
+  	  while (r[1][q] != '(' && r[1][q] != '\0')
+  	    q++;
+  	}
+  	q++;
+  	while ((r[1][q] == ' ' || r[1][q] == '\n' || r[1][q] == '\t') && r[1][q] != '\0')
+  	  q++;
+  	select.Append(common);
+  	while (r[1][q] != ' ' && r[1][q] != '\n' && r[1][q] != '\t' && r[1][q] != '\0')
+  	  select.Append(PRUnichar(r[1][q++]));
+  	select.Append(select1);
+    select.Append(PRUnichar('1'));
+    select.Append(select2);
+  }
+  select.Append(PRUnichar(';'));
+  
+  sqlite3_free_table(r);
+
+  return RealExec(select, _retval, nsnull);
+}
+
+nsresult
+mozSqlConnectionSqlite::Setup()
+{
+  if (sqlite3_errcode(mConnection) != SQLITE_OK) {
+  	CopyUTF8toUTF16(sqlite3_errmsg(mConnection), mErrorMessage);
+    
+    mConnection = nsnull;
+    return NS_ERROR_FAILURE;
+  }
+
+  NS_ConvertUTF8toUCS2 buffer(sqlite3_version);
+  nsAString::const_iterator start, end, iter;
+  PRInt32 numbers[3] = {0,0,0};
+  buffer.BeginReading(iter);
+  buffer.EndReading(end);
+  for (PRInt32 i = 0; i < 3; i++) {
+    start = iter;
+    while (iter != end && *iter != PRUnichar('.'))
+      ++iter;
+    nsAutoString v(Substring(start,iter));
+    PRInt32 err;
+    numbers[i] = v.ToInteger(&err);
+    while (iter != end && *iter == PRUnichar('.'))
+      ++iter;
+  }
+  mVersion = SERVER_VERSION(numbers[0], numbers[1], numbers[2]);
+
+  if (mVersion < SERVER_VERSION(3,0,2))
+    return NS_ERROR_FAILURE;
+
+  return NS_OK;
+}
+
+nsresult
+mozSqlConnectionSqlite::RealExec(const nsAString& aQuery,
+                                mozISqlResult** aResult, PRInt32* aAffectedRows)
+{
+  //sqlite3_changes doesn't reset its count to 0 after selects
+  static PRInt32 oldChange = 0;
+  
+  if (! mConnection)
+    return NS_ERROR_NOT_INITIALIZED;
+
+  char **r, *errmsg;
+  PRInt32 stat, nrow, ncolumn;
+
+  stat = sqlite3_get_table(mConnection, NS_ConvertUCS2toUTF8(aQuery).get(), &r, &nrow, &ncolumn, &errmsg);
+  PRInt32 changed = sqlite3_total_changes(mConnection) - oldChange;
+  oldChange += changed;
+
+  if (stat == SQLITE_OK && !changed) {
+    if (!aResult)
+      return NS_ERROR_NULL_POINTER;
+    
+    if (*aResult) {
+      ((mozSqlResultSqlite*)*aResult)->SetResult(r, nrow, ncolumn, aWritable);
+      nsresult rv = ((mozSqlResult*)*aResult)->Rebuild();
+      if (NS_FAILED(rv))
+        return rv;
+      NS_ADDREF(*aResult);
+    }
+    else {
+      mozSqlResult* result = new mozSqlResultSqlite(this, aQuery);
+      if (!result)
+        return NS_ERROR_OUT_OF_MEMORY;
+      ((mozSqlResultSqlite*)result)->SetResult(r, nrow, ncolumn, aWritable);
+      nsresult rv = result->Init();
+      if (NS_FAILED(rv))
+        return rv;
+      NS_ADDREF(*aResult = result);
+    }
+  }
+  else if (stat == SQLITE_OK && changed) {
+    if (!aAffectedRows)
+      return NS_ERROR_NULL_POINTER;
+    *aAffectedRows = changed;
+    mLastID = sqlite3_last_insert_rowid(mConnection);
+  }
+  else {
+    CopyUTF8toUTF16(errmsg, mErrorMessage);
+    sqlite3_free_table(r);
+    return NS_ERROR_FAILURE;
+  }
+
+  return NS_OK;
+}
+
+nsresult
+mozSqlConnectionSqlite::CancelExec()
+{
+  sqlite3_interrupt(mConnection);
+  
+  if (sqlite3_errcode(mConnection) != SQLITE_OK) {
+  	CopyUTF8toUTF16(sqlite3_errmsg(mConnection), mErrorMessage);
+    return NS_ERROR_FAILURE;
+  }
+
+  return NS_OK;
+}
+
+nsresult
+mozSqlConnectionSqlite::GetIDName(nsAString& aIDName)
+{
+  aIDName.AssignLiteral("OID");
+  return NS_OK;
+}
diff -urN sql.orig/sqlite/src/mozSqlResultSqlite.cpp sql/sqlite/src/mozSqlResultSqlite.cpp
--- sql.orig/sqlite/src/mozSqlResultSqlite.cpp	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/src/mozSqlResultSqlite.cpp	Mon Aug  9 17:21:17 2004
@@ -0,0 +1,201 @@
+/* ***** 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):
+ *  Valia Vaneeva <fattie@altlinux.ru>
+ *
+ * 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 "mozSqlResultSqlite.h"
+
+mozSqlResultSqlite::mozSqlResultSqlite(mozISqlConnection* aConnection,
+                                     const nsAString& aQuery)
+  : mozSqlResult(aConnection, aQuery),
+    mResult(nsnull),
+    mColumn(0),
+    mRow(0),
+    mWritable(PR_FALSE)
+{
+}
+
+void
+mozSqlResultSqlite::SetResult(char** aResult, PRInt32 nrow, PRInt32 ncolumn, PRBool aWritable)
+{
+  mResult = aResult;
+  mColumn = ncolumn;
+  mRow = nrow;
+  mWritable = aWritable;
+}
+
+mozSqlResultSqlite::~mozSqlResultSqlite()
+{
+  ClearNativeResult();
+}
+
+NS_IMPL_ADDREF_INHERITED(mozSqlResultSqlite, mozSqlResult)
+NS_IMPL_RELEASE_INHERITED(mozSqlResultSqlite, mozSqlResult)
+
+// QueryInterface
+NS_INTERFACE_MAP_BEGIN(mozSqlResultSqlite)
+  NS_INTERFACE_MAP_ENTRY(mozISqlResultSqlite)
+NS_INTERFACE_MAP_END_INHERITING(mozSqlResult)
+
+PRInt32
+mozSqlResultSqlite::GetColType(PRInt32 aColumnIndex)
+{
+  return mozISqlResult::TYPE_STRING;
+}
+
+nsresult
+mozSqlResultSqlite::BuildColumnInfo()
+{
+  for (PRInt32 i = 0; i < mColumn; i++) {
+    char* n = mResult[i];
+    PRUnichar* name = UTF8ToNewUnicode(nsDependentCString(n));
+    PRInt32 type = GetColType(i);
+    PRUint32 max = 1;
+
+    for (PRInt32 j = 1; j <= mRow; j++) {
+      if (mResult[i+j*mColumn])
+	{
+  	  if (max < strlen(mResult[i+j*mColumn]))
+	    max = strlen(mResult[i+j*mColumn]);
+	}
+    }
+    PRInt32 size = max;
+    PRInt32 mod = -1;
+
+    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, size, mod, property);
+    mColumnInfo.AppendElement(columnInfo); 
+  }
+
+  return NS_OK;
+}
+
+nsresult
+mozSqlResultSqlite::BuildRows()
+{
+  for(PRInt32 i = 1; i <= mRow; i++) {
+    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++) {
+      if (mResult[j+i*mColumn]) {
+        char* value = mResult[j+i*mColumn];
+        Cell* cell = row->mCells[j];
+        cell->SetNull(PR_FALSE);
+		cell->SetString(UTF8ToNewUnicode(nsDependentCString(value)));
+      }
+    }
+
+    mRows.AppendElement(row);
+    nsVoidKey key(resource);
+    mSources.Put(&key, row);
+  }
+
+  return NS_OK;
+}
+
+void
+mozSqlResultSqlite::ClearNativeResult()
+{
+  if (mResult) {
+    sqlite3_free_table(mResult);
+    mResult = nsnull;
+  }
+}
+
+nsresult
+mozSqlResultSqlite::EnsureTablePrivileges()
+{
+  mCanInsert = mWritable;
+  mCanUpdate = mWritable;
+  mCanDelete = mWritable;
+  
+  return NS_OK;
+}
+
+nsresult
+mozSqlResultSqlite::CanInsert(PRBool* _retval)
+{
+  if (mCanInsert >= 0) {
+    *_retval = mCanInsert;
+    return NS_OK;
+  }
+
+  nsresult rv = EnsureTablePrivileges();
+  if (NS_FAILED(rv))
+    return rv;
+  *_retval = mCanInsert;
+
+  return NS_OK;
+}
+
+nsresult
+mozSqlResultSqlite::CanUpdate(PRBool* _retval)
+{
+  if (mCanUpdate >= 0) {
+    *_retval = mCanUpdate;
+    return NS_OK;
+  }
+
+  nsresult rv = EnsureTablePrivileges();
+  if (NS_FAILED(rv))
+    return rv;
+  *_retval = mCanUpdate;
+
+  return NS_OK;
+}
+
+nsresult
+mozSqlResultSqlite::CanDelete(PRBool* _retval)
+{
+  if (mCanDelete >= 0) {
+    *_retval = mCanDelete;
+    return NS_OK;
+  }
+
+  nsresult rv = EnsureTablePrivileges();
+  if (NS_FAILED(rv))
+    return rv;
+  *_retval = mCanDelete;
+
+  return NS_OK;
+}
diff -urN sql.orig/sqlite/src/mozSqlResultSqlite.h sql/sqlite/src/mozSqlResultSqlite.h
--- sql.orig/sqlite/src/mozSqlResultSqlite.h	Thu Jan  1 07:00:00 1970
+++ sql/sqlite/src/mozSqlResultSqlite.h	Mon Aug  9 17:21:17 2004
@@ -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 Jan Varga
+ * Portions created by the Initial Developer are Copyright (C) 2003
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *  Valia Vaneeva <fattie@altlinux.ru>
+ *
+ * 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 mozSqlResultSqlite_h
+#define mozSqlResultSqlite_h                                                  
+
+#include "sqlite3.h"
+#include "mozSqlResult.h"
+#include "mozISqlResultSqlite.h"
+
+class mozSqlResultSqlite : public mozSqlResult,
+                          public mozISqlResultSqlite
+{
+  public:
+    mozSqlResultSqlite(mozISqlConnection* aConnection,
+                      const nsAString& aQuery);
+    void SetResult(char** aResult, PRInt32 nrow, PRInt32 ncolumn, PRBool aWritable);
+    virtual ~mozSqlResultSqlite();
+
+    NS_DECL_ISUPPORTS
+
+    NS_DECL_MOZISQLRESULTSQLITE 
+
+  protected:
+    PRInt32 GetColType(PRInt32 aColumnIndex);
+
+    virtual nsresult BuildColumnInfo();
+    virtual nsresult BuildRows();
+    virtual void ClearNativeResult();
+
+    nsresult EnsureTablePrivileges();
+    virtual nsresult CanInsert(PRBool* _retval);
+    virtual nsresult CanUpdate(PRBool* _retval);
+    virtual nsresult CanDelete(PRBool* _retval);
+
+  private:
+    char**   	 mResult;
+    PRInt32      mColumn;
+    PRInt32		 mRow;
+    PRInt32		 mWritable;
+};
+
+#endif // mozSqlResultSqlite_h
