diff --git a/mozilla/storage/Makefile.in b/mozilla/storage/Makefile.in new file mode 100644 index 00000000000..19c027638c3 --- /dev/null +++ b/mozilla/storage/Makefile.in @@ -0,0 +1,48 @@ +# +# ***** 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 Initial Developer of the Original Code is +# Oracle Corporation +# Portions created by the Initial Developer are Copyright (C) 2004 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Vladimir Vukicevic +# +# Alternatively, the contents of this file may be used under the terms of +# either of 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 = storage +DIRS = public src build test + +include $(topsrcdir)/config/rules.mk + diff --git a/mozilla/storage/build/Makefile.in b/mozilla/storage/build/Makefile.in new file mode 100644 index 00000000000..17275699604 --- /dev/null +++ b/mozilla/storage/build/Makefile.in @@ -0,0 +1,77 @@ +# +# ***** 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 Initial Developer of the Original Code is +# Oracle Corporation +# Portions created by the Initial Developer are Copyright (C) 2004 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Vladimir Vukicevic +# +# Alternatively, the contents of this file may be used under the terms of +# either of 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 = storagecomps +LIBRARY_NAME = storagecomps +SHORT_LIBNAME = strgcmps +EXPORT_LIBRARY = 1 +IS_COMPONENT = 1 +MODULE_NAME = mozStorageModule + +REQUIRES = \ + storage \ + sqlite3 \ + xpcom \ + string \ + $(NULL) + +EXPORTS = mozStorageCID.h + +CPPSRCS = mozStorageModule.cpp + +LOCAL_INCLUDES = \ + -I$(srcdir)/../src + +SHARED_LIBRARY_LIBS = \ + $(DIST)/lib/$(LIB_PREFIX)storage_s.$(LIB_SUFFIX) \ + $(DIST)/lib/$(LIB_PREFIX)sqlite3_s.$(LIB_SUFFIX) \ + $(NULL) + +EXTRA_DSO_LDOPTS += \ + $(LIBS_DIR) \ + $(EXTRA_DSO_LIBS) \ + $(MOZ_COMPONENT_LIBS) \ + $(NULL) + +include $(topsrcdir)/config/rules.mk + diff --git a/mozilla/storage/build/mozStorageCID.h b/mozilla/storage/build/mozStorageCID.h new file mode 100644 index 00000000000..51d123622ab --- /dev/null +++ b/mozilla/storage/build/mozStorageCID.h @@ -0,0 +1,57 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 _MOZSTORAGECID_H_ +#define _MOZSTORAGECID_H_ + +#define MOZ_STORAGE_CONTRACTID_PREFIX "@mozilla.org/storage" + + +/* b71a1f84-3a70-4d37-a348-f1ba0e27eead */ +#define MOZ_STORAGE_CONNECTION_CID \ +{ 0xb71a1f84, 0x3a70, 0x4d37, {0xa3, 0x48, 0xf1, 0xba, 0x0e, 0x27, 0xee, 0xad} } + +#define MOZ_STORAGE_CONNECTION_CONTRACTID MOZ_STORAGE_CONTRACTID_PREFIX "/connection;1" + +/* bbbb1d61-438f-4436-92ed-8308e5830fb0 */ +#define MOZ_STORAGE_SERVICE_CID \ +{ 0xbbbb1d61, 0x438f, 0x4436, {0x92, 0xed, 0x83, 0x08, 0xe5, 0x83, 0x0f, 0xb0} } + +#define MOZ_STORAGE_SERVICE_CONTRACTID MOZ_STORAGE_CONTRACTID_PREFIX "/service;1" + +#endif /* _MOZSTORAGECID_H_ */ diff --git a/mozilla/storage/build/mozStorageModule.cpp b/mozilla/storage/build/mozStorageModule.cpp new file mode 100644 index 00000000000..1be5d6c4a38 --- /dev/null +++ b/mozilla/storage/build/mozStorageModule.cpp @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 "nsCOMPtr.h" +#include "nsIGenericFactory.h" +#include "nsIModule.h" + +#include "mozStorageService.h" +#include "mozStorageConnection.h" + +#include "mozStorageCID.h" + +NS_GENERIC_FACTORY_CONSTRUCTOR(mozStorageService) +NS_GENERIC_FACTORY_CONSTRUCTOR(mozStorageConnection) + +static const nsModuleComponentInfo components[] = +{ + { "Unified Data Store Service", + MOZ_STORAGE_SERVICE_CID, + MOZ_STORAGE_SERVICE_CONTRACTID, + mozStorageServiceConstructor + }, + + { "Unified Data Store Connection", + MOZ_STORAGE_CONNECTION_CID, + MOZ_STORAGE_CONNECTION_CONTRACTID, + mozStorageConnectionConstructor + } +}; + +NS_IMPL_NSGETMODULE(mozStorageModule, components); diff --git a/mozilla/storage/public/Makefile.in b/mozilla/storage/public/Makefile.in new file mode 100644 index 00000000000..1f7562c2cd6 --- /dev/null +++ b/mozilla/storage/public/Makefile.in @@ -0,0 +1,59 @@ +# +# ***** 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 Oracle Corporation code. +# +# The Initial Developer of the Original Code is +# Oracle Corporation +# Portions created by the Initial Developer are Copyright (C) 2004 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Vladimir Vukicevic +# +# Alternatively, the contents of this file may be used under the terms of +# either of 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 = storage +XPIDL_MODULE = storage +GRE_MODULE = 1 + +XPIDLSRCS = \ + mozIStorageService.idl \ + mozIStorageConnection.idl \ + mozIStorageFunction.idl \ + mozIStorageStatement.idl \ + mozIStorageDataSet.idl \ + mozIStorageValueArray.idl \ + $(NULL) + +include $(topsrcdir)/config/rules.mk diff --git a/mozilla/storage/public/mozIStorageConnection.idl b/mozilla/storage/public/mozIStorageConnection.idl new file mode 100644 index 00000000000..a8cc065bc87 --- /dev/null +++ b/mozilla/storage/public/mozIStorageConnection.idl @@ -0,0 +1,190 @@ +/* -*- Mode: idl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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" + +%{C++ +#include +%} + +interface mozIStorageFunction; +interface mozIStorageStatement; +interface nsIFile; + +[ptr] native sqlite3ptr(sqlite3); + +[scriptable, uuid(623b8b2e-c9f9-4cc3-b15a-f3c96df2cc1c)] +interface mozIStorageConnection : nsISupports { + /* + * Initialization and status + */ + + /** + * whether the database is open or not + */ + readonly attribute boolean connectionReady; + + /** + * the current database filename + */ + readonly attribute AUTF8String databaseName; + + /** + * lastInsertRowID returns the row ID from the last INSERT + * operation. + */ + readonly attribute long long lastInsertRowID; + + /** + * lastError returns the last error code + */ + readonly attribute long lastError; + + /** + * lastErrorString returns the last error as a string + */ + readonly attribute AUTF8String lastErrorString; + + /* + * Statement creation + */ + + /** + * Create a mozIStorageStatement for the given SQL expression. + * The expression may use ?0 ?1 etc. to indicate numbered arguments. + * + * @param aSQLStatement The SQL statement to execute + * + * @returns a new mozIStorageStatement + */ + mozIStorageStatement createStatement (in AUTF8String aSQLStatement); + + /** + * Execute a SQL expression, expecting no arguments. + * + * @param aSQLStatement The SQL statement to execute + */ + void executeSimpleSQL (in AUTF8String aSQLStatement); + + /* + * Transactions + */ + + /** + * Returns true if a transaction is active on this connection. + */ + readonly attribute boolean transactionInProgress; + + /** + * Begin a new transaction. If a transaction is active, + * @throws NS_ERROR_STORAGE_INVALID_OPERATION. + */ + void beginTransaction (); + + /** + * Commits the current transaction. If no transaction is active, + * @throws NS_ERROR_STORAGE_NO_TRANSACTION. + */ + void commitTransaction (); + + /** + * Rolls back the current transaction. If no transaction is active, + * @throws NS_ERROR_STORAGE_NO_TRANSACTION. + */ + void rollbackTransaction (); + + /* + * Tables + */ + + /** + * Create the table with the given name and schema. If the table + * already exists with the given schema, the non-error + * NS_STORAGE_TABLE_EXISTS is returned. If the table exists but with + * a different schema, NS_ERROR_STORAGE_TABLE_CONFLICT (?) is thrown. + * + * @param aID the owner of this table -- XXXtodo + * + * @param aTableName the table name to be created, consisting of [A-Za-z0-9_], and beginning with a letter. + * + * @param aTableSchema the schema of the table; what would normally go between the parens in a CREATE TABLE + * statement: "foo INTEGER, bar STRING". + */ + void createTable(in string aTableName, + in string aTableSchema); + + /* + * Functions and Triggers + */ + + /** + * Create a new SQLite function + */ + void createFunction (in string aFunctionName, + in long aNumArguments, + in mozIStorageFunction aFunction); + + /** + * Create a new trigger. Note that we only support AFTER triggers here, + * not BEFORE (so that we can support IPC notification of triggers). + * All triggers created this way are TEMPORARY triggers, i.e. they are only + * valid for the duration of this connection. Without this, triggers would + * persist between connections, even if the functions they refer to may no + * longer be defined; this would be bad. + * + * aParameters is a comma-separated list of parameters, referencing + * either "new.*" or "old.*", as appropriate for the trigger event + * type. + */ + const long TRIGGER_EVENT_DELETE = 1; + const long TRIGGER_EVENT_INSERT = 2; + const long TRIGGER_EVENT_UPDATE = 3; + + void createTrigger (in string aTriggerName, + in long aTriggerType, + in string aTableName, + in string aTriggerFunction, + in string aParameters); + + void removeTrigger (in string aTriggerName); + + /* + * native support + */ + [noscript] readonly attribute sqlite3ptr sqliteHandle; +}; diff --git a/mozilla/storage/public/mozIStorageDataSet.idl b/mozilla/storage/public/mozIStorageDataSet.idl new file mode 100644 index 00000000000..82afbb45322 --- /dev/null +++ b/mozilla/storage/public/mozIStorageDataSet.idl @@ -0,0 +1,57 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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" + +interface nsIArray; +interface nsISimpleEnumerator; + +[scriptable, uuid(57826606-3c8a-4243-9f2f-cb3fe6e91148)] +interface mozIStorageDataSet : nsISupports { + /** + * All the rows in this data set, as a nsIArray + */ + readonly attribute nsIArray dataRows; + + /** + * Get an enumerator for the result set rows. + * @returns a nsISimpleEnumerator of mozIStorageValueArray. + */ + nsISimpleEnumerator getRowEnumerator(); +}; + diff --git a/mozilla/storage/public/mozIStorageFunction.idl b/mozilla/storage/public/mozIStorageFunction.idl new file mode 100644 index 00000000000..631740a5e16 --- /dev/null +++ b/mozilla/storage/public/mozIStorageFunction.idl @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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" + +interface mozIStorageConnection; +interface mozIStorageValueArray; +interface nsIArray; + +[scriptable, uuid(898d4189-7012-4ae9-a2af-435491cfa114)] +interface mozIStorageFunction : nsISupports { + /** + * onFunctionCall is called when execution of a custom + * function should occur. There are no return values. + * + * @param aNumArguments The number of arguments + * @param aFunctionArguments The arguments passed in to the function + */ + + void onFunctionCall (in mozIStorageValueArray aFunctionArguments); +}; diff --git a/mozilla/storage/public/mozIStorageService.idl b/mozilla/storage/public/mozIStorageService.idl new file mode 100644 index 00000000000..e6df04baa46 --- /dev/null +++ b/mozilla/storage/public/mozIStorageService.idl @@ -0,0 +1,69 @@ +/* -*- Mode: idl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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" + +interface mozIStorageConnection; +interface nsIFile; + +[scriptable, uuid(22cff01c-1a5a-4b11-9e27-f8f832226489)] +interface mozIStorageService : nsISupports { + /** + * Get a connection to the local profile storage. + * + * @param aStorageKey a string key identifying the type of storage + * requested. Valid values include: "profile", "cert". + * + * @returns a mozIStorageConnection corresponding to + * the appropriate storage for the current thread. + * (XXX - always returns same one for now, need to add threading) + */ + mozIStorageConnection getProfileStorage(in string aStorageKey); + + /** + * Open a connection to the specified file. + * + * @param aDatabaseFile a nsIFile of the database to open. + * + * @returns a mozIStorageConnection corresponding to + * the appropriate storage for the current thread. + * (XXX - always returns same one for now, need to add threading) + */ + mozIStorageConnection openDatabase(in nsIFile aDatabaseFile); + +}; \ No newline at end of file diff --git a/mozilla/storage/public/mozIStorageStatement.idl b/mozilla/storage/public/mozIStorageStatement.idl new file mode 100644 index 00000000000..0551dad69c0 --- /dev/null +++ b/mozilla/storage/public/mozIStorageStatement.idl @@ -0,0 +1,115 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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" +#include "mozIStorageValueArray.idl" + +interface mozIStorageConnection; +interface mozIStorageDataSet; +interface nsISimpleEnumerator; + +[scriptable, uuid(656aa634-36e2-4977-802e-79bce39c1024)] +interface mozIStorageStatement : mozIStorageValueArray { + /** + * Initialize this query with the given SQL statement. + * + */ + void initialize (in mozIStorageConnection aDBConnection, in AUTF8String aSQLStatement); + + mozIStorageStatement clone (); + + /* + * Number of parameters + */ + readonly attribute unsigned long parameterCount; + + /* + * Reset parameters/statement execution + */ + void reset (); + + /* + * Parameter binding + */ + void bindCStringParameter (in unsigned long aParamIndex, in string aValue); + void bindUTF8StringParameter (in unsigned long aParamIndex, in AUTF8String aValue); + + void bindWStringParameter (in unsigned long aParamIndex, in wstring aValue); + void bindStringParameter (in unsigned long aParamIndex, in AString aValue); + + void bindDoubleParameter (in unsigned long aParamIndex, in double aValue); + void bindInt32Parameter (in unsigned long aParamIndex, in long aValue); + void bindInt64Parameter (in unsigned long aParamIndex, in long long aValue); + + void bindNullParameter (in unsigned long aParamIndex); + + void bindDataParameter (in unsigned long aParamIndex, [array,size_is(aValueSize)] in octet aValue, in unsigned long aValueSize); + + // void bindParameters (in string aFormatString, ...); + + /** + * Execute the query, ignoring any results. This is accomplished + * by calling step() once, and then calling reset(). + * + * Error and last insert info, etc. are available from + * the mozStorageConnection. + */ + void execute (); + + /** + * Execute the query, using any currently-bound parameters. + * + * @returns a mozIStorageDataSet containing all the rows of the query + * result. + */ + mozIStorageDataSet executeDataSet (); + + /** + * Execute a query, using any currently-bound parameters. Reset + * must be called on the statement after the last call of + * executeStep. + * + * @returns a boolean indicating whether there are more rows or not; + * row data may be accessed using mozIStorageValueArray methods on + * the statement. + * + */ + boolean executeStep (); + +}; + diff --git a/mozilla/storage/public/mozIStorageValueArray.idl b/mozilla/storage/public/mozIStorageValueArray.idl new file mode 100644 index 00000000000..dd1a843f81c --- /dev/null +++ b/mozilla/storage/public/mozIStorageValueArray.idl @@ -0,0 +1,82 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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(44fc1d3b-dc91-4d17-8bc5-2069d8fd3cca)] +interface mozIStorageValueArray : nsISupports { + + const long VALUE_TYPE_NULL = 0; + const long VALUE_TYPE_INTEGER = 1; + const long VALUE_TYPE_FLOAT = 2; + const long VALUE_TYPE_TEXT = 3; + const long VALUE_TYPE_BLOB = 4; + + /** + * numColumns + * + * number of columns in the array + */ + readonly attribute unsigned long numColumns; + + long getTypeOfIndex (in unsigned long aIndex); + + /* Due to SQLite's type conversion rules, any of these is valid */ + + long getAsInt32(in unsigned long aIndex); + long long getAsInt64(in unsigned long aIndex); + double getAsDouble(in unsigned long aIndex); + string getAsCString(in unsigned long aIndex); + AUTF8String getAsUTF8String(in unsigned long aIndex); + AString getAsString(in unsigned long aIndex); + void getAsBlob(in unsigned long aIndex, [array,size_is(aDataSize)] out octet aData, out unsigned long aDataSize); + boolean getIsNull (in unsigned long aIndex); + + /* XXX handle blobs! */ + + /* Native stuff */ + [notxpcom] long asInt32(in unsigned long aIndex); + [notxpcom] long long asInt64(in unsigned long aIndex); + [notxpcom] double asDouble(in unsigned long aIndex); + [notxpcom] string asSharedCString(in unsigned long aIndex, out unsigned long aLength); + [notxpcom] wstring asSharedWString(in unsigned long aIndex, out unsigned long aLength); + + [noscript] void asSharedBlob(in unsigned long aIndex, [shared] out voidPtr aData, out unsigned long aDataSize); + + [notxpcom] boolean isNull(in unsigned long aIndex); +}; diff --git a/mozilla/storage/public/mozStorage.h b/mozilla/storage/public/mozStorage.h new file mode 100644 index 00000000000..b024579b900 --- /dev/null +++ b/mozilla/storage/public/mozStorage.h @@ -0,0 +1,45 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 _MOZSTORAGE_H_ +#define _MOZSTORAGE_H_ + +#define MOZ_ERROR_STORAGE_OPEN_FAILED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_STORAGE,1) +#define MOZ_ERROR_STORAGE_BUSY NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_STORAGE,2) + +#endif /* _MOZSTORAGE_H_ */ diff --git a/mozilla/storage/src/Makefile.in b/mozilla/storage/src/Makefile.in new file mode 100644 index 00000000000..2a1d3cb91a3 --- /dev/null +++ b/mozilla/storage/src/Makefile.in @@ -0,0 +1,64 @@ +# +# ***** 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 Oracle Corporation code. +# +# The Initial Developer of the Original Code is +# Oracle Corporation +# Portions created by the Initial Developer are Copyright (C) 2004 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Vladimir Vukicevic +# +# Alternatively, the contents of this file may be used under the terms of +# either of 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 = storage +LIBRARY_NAME = storage_s +MODULE_NAME = mozStorageModule +FORCE_STATIC_LIB = 1 +GRE_MODULE = 1 + +REQUIRES = xpcom \ + string \ + sqlite3 \ + $(NULL) + +CPPSRCS = \ + mozStorageService.cpp \ + mozStorageConnection.cpp \ + mozStorageStatement.cpp \ + mozStorageValueArray.cpp \ + $(NULL) + +include $(topsrcdir)/config/rules.mk diff --git a/mozilla/storage/src/mozStorageConnection.cpp b/mozilla/storage/src/mozStorageConnection.cpp new file mode 100644 index 00000000000..af59e9d03f7 --- /dev/null +++ b/mozilla/storage/src/mozStorageConnection.cpp @@ -0,0 +1,440 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 + +#include "nsError.h" +#include "nsArray.h" +#include "nsIFile.h" + +#include "mozIStorageFunction.h" + +#include "mozStorageConnection.h" +#include "mozStorageStatement.h" +#include "mozStorageValueArray.h" + +#include "prlog.h" +#include "prprf.h" + +#ifdef PR_LOGGING +PRLogModuleInfo* gStorageLog = nsnull; +#endif + +NS_IMPL_ISUPPORTS1(mozStorageConnection, mozIStorageConnection) + +mozStorageConnection::mozStorageConnection() + : mDBConn(nsnull), mTransactionInProgress(PR_FALSE) +{ + +} + +mozStorageConnection::~mozStorageConnection() +{ + if (mDBConn) { + int srv = sqlite3_close (mDBConn); + if (srv != SQLITE_OK) { + NS_WARNING("sqlite3_close failed. There are probably outstanding statements!"); + } + } +} + +#ifdef PR_LOGGING +void tracefunc (void *closure, const char *stmt) +{ + PR_LOG(gStorageLog, PR_LOG_DEBUG, ("%s", stmt)); +} +#endif + +NS_IMETHODIMP +mozStorageConnection::Initialize(nsIFile *aDatabaseFile) +{ + NS_ENSURE_ARG_POINTER(aDatabaseFile); + + NS_ASSERTION (!mDBConn, "Initialize called on already opened database!"); + + int srv; + nsresult rv; + + rv = aDatabaseFile->GetNativeLeafName(mDatabaseName); + NS_ENSURE_SUCCESS(rv, rv); + + nsCAutoString nativePath; + rv = aDatabaseFile->GetNativePath(nativePath); + NS_ENSURE_SUCCESS(rv, rv); + + srv = sqlite3_open (nativePath.get(), &mDBConn); + if (srv != SQLITE_OK) { + mDBConn = nsnull; + return NS_ERROR_FAILURE; // XXX error code + } + +#ifdef PR_LOGGING + if (! gStorageLog) + gStorageLog = PR_NewLogModule("mozStorage"); + + sqlite3_trace (mDBConn, tracefunc, nsnull); +#endif + + rv = NS_NewArray(getter_AddRefs(mFunctions)); + if (NS_FAILED(rv)) return rv; + + return NS_OK; +} + +/***************************************************************************** + ** mozIStorageConnection interface + *****************************************************************************/ + +/** + ** Core status/initialization + **/ + +NS_IMETHODIMP +mozStorageConnection::GetConnectionReady(PRBool *aConnectionReady) +{ + *aConnectionReady = (mDBConn != nsnull); + return NS_OK; +} + +NS_IMETHODIMP +mozStorageConnection::GetDatabaseName(nsACString& aDatabaseName) +{ + NS_ASSERTION(mDBConn, "connection not initialized"); + + aDatabaseName = mDatabaseName; + + return NS_OK; +} + +NS_IMETHODIMP +mozStorageConnection::GetLastInsertRowID(PRInt64 *aLastInsertRowID) +{ + NS_ASSERTION(mDBConn, "connection not initialized"); + + sqlite_int64 id = sqlite3_last_insert_rowid(mDBConn); + *aLastInsertRowID = id; + + return NS_OK; +} + +NS_IMETHODIMP +mozStorageConnection::GetLastError(PRInt32 *aLastError) +{ + NS_ASSERTION(mDBConn, "connection not initialized"); + + *aLastError = sqlite3_errcode(mDBConn); + + return NS_OK; +} + +NS_IMETHODIMP +mozStorageConnection::GetLastErrorString(nsACString& aLastErrorString) +{ + NS_ASSERTION(mDBConn, "connection not initialized"); + + const char *serr = sqlite3_errmsg(mDBConn); + aLastErrorString.Assign(serr); + + return NS_OK; +} + +/** + ** Statements & Queries + **/ + +NS_IMETHODIMP +mozStorageConnection::CreateStatement(const nsACString& aSQLStatement, + mozIStorageStatement **_retval) +{ + NS_ENSURE_ARG_POINTER(_retval); + NS_ASSERTION(mDBConn, "connection not initialized"); + + mozStorageStatement *statement = new mozStorageStatement(); + NS_ADDREF(statement); + + nsresult rv = statement->Initialize (this, aSQLStatement); + if (NS_FAILED(rv)) { + NS_RELEASE(statement); + return NS_ERROR_FAILURE; // XXX error code + } + + *_retval = statement; + return NS_OK; +} + +NS_IMETHODIMP +mozStorageConnection::ExecuteSimpleSQL(const nsACString& aSQLStatement) +{ + NS_ENSURE_ARG_POINTER(mDBConn); + + char *errMsg = NULL; + int srv = sqlite3_exec (mDBConn, PromiseFlatCString(aSQLStatement).get(), + NULL, NULL, NULL); + if (srv != SQLITE_OK) { + HandleSqliteError(nsPromiseFlatCString(aSQLStatement).get()); + return NS_ERROR_FAILURE; // XXX error code + } + + return NS_OK; +} + +/** + ** Transactions + **/ + +NS_IMETHODIMP +mozStorageConnection::GetTransactionInProgress(PRInt32 *_retval) +{ + *_retval = mTransactionInProgress; + return NS_OK; +} + +// XXX do we want to just store compiled statements for these? +NS_IMETHODIMP +mozStorageConnection::BeginTransaction() +{ + if (mTransactionInProgress) + return NS_ERROR_FAILURE; // XXX error code + nsresult rv = ExecuteSimpleSQL (NS_LITERAL_CSTRING("BEGIN TRANSACTION")); + if (NS_SUCCEEDED(rv)) + mTransactionInProgress = PR_TRUE; + return rv; +} + +NS_IMETHODIMP +mozStorageConnection::CommitTransaction() +{ + if (!mTransactionInProgress) + return NS_ERROR_FAILURE; + nsresult rv = ExecuteSimpleSQL (NS_LITERAL_CSTRING("COMMIT TRANSACTION")); + // even if the commit fails, the transaction is aborted + mTransactionInProgress = PR_FALSE; + return rv; +} + +NS_IMETHODIMP +mozStorageConnection::RollbackTransaction() +{ + if (!mTransactionInProgress) + return NS_ERROR_FAILURE; + nsresult rv = ExecuteSimpleSQL (NS_LITERAL_CSTRING("ROLLBACK TRANSACTION")); + mTransactionInProgress = PR_FALSE; + return rv; +} + +/** + ** Table creation + **/ + +NS_IMETHODIMP +mozStorageConnection::CreateTable(/*const nsID& aID,*/ + const char *aTableName, + const char *aTableSchema) +{ + int srv; + char *buf; + int buflen = 0; + +#if 0 + buflen = snprintf(nsnull, 0, "CREATE TABLE %s (%s)"); + if (buflen <= 0) + return NS_ERROR_FAILURE; + + buf = nsMemory::Alloc(buflen + 1); + if (!buf) + return NS_ERROR_OUT_OF_MEMORY; + + buflen = sprintf(buf, buflen, "CREATE TABLE %s (%s)"); + if (buflen <= 0) { + nsMemory::Free(buf); + return NS_ERROR_FAILURE; + } + + srv = sqlite3_exec (mDBConn, buf, + NULL, NULL, NULL); + nsMemory::Free(buf); + + if (srv != SQLITE_OK) { + return NS_ERROR_FAILURE; // XXX SQL_ERROR_TABLE_EXISTS + } +#endif + return NS_OK; +} + +/** + ** Functions and Triggers + **/ + +static void +mozStorageSqlFuncHelper (sqlite3_context *ctx, + int argc, + sqlite3_value **argv) +{ + fprintf (stderr, "mozStorageSqlFuncHelper: %p %d %p\n", ctx, argc, argv); + + void *userData = sqlite3_user_data (ctx); + // We don't want to QI here, because this will be called a -lot- + mozIStorageFunction *userFunction = NS_STATIC_CAST(mozIStorageFunction *, userData); + + nsCOMPtr ava = new mozStorageArgvValueArray (argc, argv); + nsresult rv = userFunction->OnFunctionCall (ava); + if (NS_FAILED(rv)) { + NS_WARNING("mozIStorageConnection: User function returned error code!\n"); + } +} + +NS_IMETHODIMP +mozStorageConnection::CreateFunction(const char *aFunctionName, + PRInt32 aNumArguments, + mozIStorageFunction *aFunction) +{ + nsresult rv; + + // do we already have this function defined? + // XXX check for name as well + PRUint32 idx; + rv = mFunctions->IndexOf (0, aFunction, &idx); + if (rv != NS_ERROR_FAILURE) { + // already exists + return NS_ERROR_FAILURE; + } + + int srv = sqlite3_create_function (mDBConn, + aFunctionName, + aNumArguments, + SQLITE_ANY, + aFunction, + mozStorageSqlFuncHelper, + nsnull, + nsnull); + if (srv != SQLITE_OK) { + HandleSqliteError(nsnull); + return NS_ERROR_FAILURE; + } + + rv = mFunctions->AppendElement (aFunction, PR_FALSE); + if (NS_FAILED(rv)) return rv; + + return NS_OK; +} + +NS_IMETHODIMP +mozStorageConnection::CreateTrigger(const char *aTriggerName, + PRInt32 aTriggerType, + const char *aTableName, + const char *aTriggerFunction, + const char *aParameters) +{ + nsresult rv; + +#if 0 + /* We don't need to split this until we need to generate + * our own IPC trigger + */ + nsCStringArray *cstr = new nsCStringArray(); + if (!cstr) + return NS_ERROR_OUT_OF_MEMORY; + + rv = cstr->ParseString (aParameters, ","); + if (NS_FAILED(rv)) return rv; +#endif + + char *event = nsnull; + if (aTriggerType == TRIGGER_EVENT_DELETE) + event = "DELETE"; + else if (aTriggerType == TRIGGER_EVENT_INSERT) + event = "INSERT"; + else if (aTriggerType == TRIGGER_EVENT_UPDATE) + event = "UPDATE"; + else + return NS_ERROR_FAILURE; + + char *sql = PR_sprintf_append + (nsnull, + "CREATE TEMPORARY TRIGGER %s AFTER %s ON %s FOR EACH ROW BEGIN SELECT %s(%s); END;", + aTriggerName, + event, + aTableName, + aTriggerFunction, + aParameters); + if (!sql) + return NS_ERROR_OUT_OF_MEMORY; + + /* Now create the trigger */ + int srv = sqlite3_exec (mDBConn, + sql, + nsnull, + nsnull, + nsnull); + if (srv != SQLITE_OK) { + HandleSqliteError(nsnull); + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +NS_IMETHODIMP +mozStorageConnection::RemoveTrigger(const char *aTriggerName) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +/** + ** Native support + **/ +NS_IMETHODIMP +mozStorageConnection::GetSqliteHandle(sqlite3 **aSqliteHandle) +{ + *aSqliteHandle = mDBConn; + return NS_OK; +} + +/** + ** Other bits + **/ +void +mozStorageConnection::HandleSqliteError(const char *aSqlStatement) +{ + // an error just occured! +#ifdef PR_LOGGING + PR_LOG(gStorageLog, PR_LOG_DEBUG, ("Sqlite error: %d '%s'", sqlite3_errcode(mDBConn), sqlite3_errmsg(mDBConn))); + if (aSqlStatement) + PR_LOG(gStorageLog, PR_LOG_DEBUG, ("Statement was: %s", aSqlStatement)); +#endif +} diff --git a/mozilla/storage/src/mozStorageConnection.h b/mozilla/storage/src/mozStorageConnection.h new file mode 100644 index 00000000000..9a9a1ba3304 --- /dev/null +++ b/mozilla/storage/src/mozStorageConnection.h @@ -0,0 +1,76 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 _MOZSTORAGECONNECTION_H_ +#define _MOZSTORAGECONNECTION_H_ + +#include "nsString.h" +#include "mozIStorageConnection.h" + +#include "nsIArray.h" + +#include + +class nsIFile; + +class mozStorageConnection : public mozIStorageConnection +{ +public: + + mozStorageConnection(); + + NS_IMETHOD mozStorageConnection::Initialize(nsIFile *aDatabaseFile); + + // interfaces + NS_DECL_ISUPPORTS + NS_DECL_MOZISTORAGECONNECTION + +private: + ~mozStorageConnection(); + +protected: + void HandleSqliteError(const char *aSqlStatement); + + sqlite3 *mDBConn; + nsCString mDatabaseName; + PRBool mTransactionInProgress; + + nsCOMPtr mFunctions; +}; + +#endif /* _MOZSTORAGECONNECTION_H_ */ diff --git a/mozilla/storage/src/mozStorageService.cpp b/mozilla/storage/src/mozStorageService.cpp new file mode 100644 index 00000000000..0951cdd2f74 --- /dev/null +++ b/mozilla/storage/src/mozStorageService.cpp @@ -0,0 +1,98 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 "mozStorageService.h" +#include "mozStorageConnection.h" + +#include "sqlite3.h" + +NS_IMPL_ISUPPORTS1(mozStorageService, mozIStorageService) + +// XXX this sucks that we have to pull in nsIFile and all that +// just to use NS_GetSpecialDirectory + +mozStorageService::mozStorageService() +{ + +} + +mozStorageService::~mozStorageService() +{ + /* destructor code */ +} + +#ifndef NS_APP_STORAGE_50_FILE +#define NS_APP_STORAGE_50_FILE "UStor" +#endif + +/* mozIStorageConnection getProfileStorage (in string aStorageKey); */ +NS_IMETHODIMP +mozStorageService::GetProfileStorage(const char *aStorageKey, mozIStorageConnection **_retval) +{ + nsresult rv; + + nsCOMPtr storageFile; + rv = NS_GetSpecialDirectory(NS_APP_STORAGE_50_FILE, getter_AddRefs(storageFile)); + if (NS_FAILED(rv)) { + // teh wtf? + return rv; + } + + nsCOMPtr conn = new mozStorageConnection(); + rv = conn->Initialize (storageFile); + if (NS_FAILED(rv)) return rv; + + *_retval = conn; + NS_ADDREF(*_retval); + return NS_OK; +} + +/* mozIStorageConnection openDatabase(in nsIFile aDatabaseFile); */ +NS_IMETHODIMP +mozStorageService::OpenDatabase(nsIFile *aDatabaseFile, mozIStorageConnection **_retval) +{ + nsresult rv; + + nsCOMPtr conn = new mozStorageConnection(); + rv = conn->Initialize (aDatabaseFile); + if (NS_FAILED(rv)) return rv; + + *_retval = conn; + NS_ADDREF(*_retval); + return NS_OK; +} diff --git a/mozilla/storage/src/mozStorageService.h b/mozilla/storage/src/mozStorageService.h new file mode 100644 index 00000000000..5e4024b9036 --- /dev/null +++ b/mozilla/storage/src/mozStorageService.h @@ -0,0 +1,64 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 _MOZSTORAGESERVICE_H_ +#define _MOZSTORAGESERVICE_H_ + +#include "nsCOMPtr.h" +#include "nsIFile.h" + +#include "mozIStorageService.h" + +class mozStorageService : public mozIStorageService +{ +public: + mozStorageService(); + + // nsISupports + NS_DECL_ISUPPORTS + + // mozIStorageService + NS_DECL_MOZISTORAGESERVICE + +private: + ~mozStorageService(); +protected: + nsCOMPtr mProfileStorageFile; +}; + +#endif /* _MOZSTORAGESERVICE_H_ */ diff --git a/mozilla/storage/src/mozStorageStatement.cpp b/mozilla/storage/src/mozStorageStatement.cpp new file mode 100644 index 00000000000..1da31c216bf --- /dev/null +++ b/mozilla/storage/src/mozStorageStatement.cpp @@ -0,0 +1,663 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 + +#include "nsError.h" +#include "nsISimpleEnumerator.h" + +#include "mozStorageStatement.h" +#include "mozStorageValueArray.h" + +#include "prlog.h" + +#ifdef PR_LOGGING +extern PRLogModuleInfo* gStorageLog; +#endif + +/** + ** mozStorageStatementRowEnumerator + **/ +class mozStorageStatementRowEnumerator : public nsISimpleEnumerator { +public: + // this expects a statement that has NOT had step called on it yet + mozStorageStatementRowEnumerator (sqlite3_stmt *aDBStatement); + + // nsISupports interface + NS_DECL_ISUPPORTS + + // nsISimpleEnumerator interface + NS_DECL_NSISIMPLEENUMERATOR + +private: + ~mozStorageStatementRowEnumerator (); +protected: + sqlite3_stmt *mDBStatement; + PRBool mHasMore; + PRBool mDidStep; + + void DoRealStep(); +}; + +NS_IMPL_ISUPPORTS1(mozStorageStatementRowEnumerator, nsISimpleEnumerator) + +mozStorageStatementRowEnumerator::mozStorageStatementRowEnumerator (sqlite3_stmt *aDBStatement) + : mDBStatement (aDBStatement) +{ + NS_ASSERTION (aDBStatement != nsnull, "Null statement!"); + + // do the first step + DoRealStep (); + mDidStep = PR_TRUE; +} + +void +mozStorageStatementRowEnumerator::DoRealStep () +{ + int srv = sqlite3_step (mDBStatement); + + switch (srv) { + case SQLITE_ROW: + mHasMore = PR_TRUE; + break; + case SQLITE_DONE: + mHasMore = PR_FALSE; + break; + case SQLITE_BUSY: // XXX!!! + case SQLITE_MISUSE: + case SQLITE_ERROR: + default: + mHasMore = PR_FALSE; + break; + } +} + +mozStorageStatementRowEnumerator::~mozStorageStatementRowEnumerator () +{ +} + +/* nsISimpleEnumerator interface */ +NS_IMETHODIMP +mozStorageStatementRowEnumerator::HasMoreElements (PRBool *_retval) +{ + // step if we haven't already + if (!mDidStep) { + DoRealStep(); + mDidStep = PR_TRUE; + } + + *_retval = mHasMore; + return NS_OK; +} + +NS_IMETHODIMP +mozStorageStatementRowEnumerator::GetNext (nsISupports **_retval) +{ + if (!mHasMore) + return NS_ERROR_FAILURE; + + if (!mDidStep) + DoRealStep(); + mDidStep = PR_FALSE; + + // assume this is SQLITE_ROW + int numColumns = sqlite3_data_count (mDBStatement); + + mozStorageStatementRowValueArray *mssrva = new mozStorageStatementRowValueArray(mDBStatement); + NS_ADDREF(mssrva); + *_retval = mssrva; + + return NS_OK; +} + +/** + ** mozStorageStatement + **/ + +NS_IMPL_ISUPPORTS2(mozStorageStatement, mozIStorageStatement, mozIStorageValueArray) + +mozStorageStatement::mozStorageStatement() + : mDBConnection (nsnull), mDBStatement(nsnull) +{ +} + +NS_IMETHODIMP +mozStorageStatement::Initialize(mozIStorageConnection *aDBConnection, const nsACString & aSQLStatement) +{ + int srv; + nsresult rv; + + sqlite3 *db = nsnull; + rv = aDBConnection->GetSqliteHandle(&db); + NS_ENSURE_SUCCESS(rv, rv); + + srv = sqlite3_prepare (db, nsPromiseFlatCString(aSQLStatement).get(), aSQLStatement.Length(), &mDBStatement, NULL); + if (srv != SQLITE_OK) { + fprintf (stderr, "SQLITE ERROR: Statement: '%s'\n", nsPromiseFlatCString(aSQLStatement).get()); + fprintf (stderr, "SQLITE ERROR: %d '%s'\n", srv, sqlite3_errmsg(db)); + return NS_ERROR_FAILURE; + } + + mDBConnection = aDBConnection; + mStatementString.Assign (aSQLStatement); + mParamCount = sqlite3_bind_parameter_count (mDBStatement); + mResultColumnCount = sqlite3_column_count (mDBStatement); + + // doing a sqlite3_prepare sets up the execution engine + // for that statement; doing a create_function after that + // results in badness, because there's a selected statement. + // use this hack to clear it out -- this may be a bug. + sqlite3_exec (db, "", 0, 0, 0); + + return NS_OK; +} + +mozStorageStatement::~mozStorageStatement() +{ + if (mDBStatement) + sqlite3_finalize (mDBStatement); +} + +/* mozIStorageStatement clone (); */ +NS_IMETHODIMP +mozStorageStatement::Clone(mozIStorageStatement **_retval) +{ + mozStorageStatement *mss = new mozStorageStatement(); + NS_ADDREF(mss); + + nsresult rv = mss->Initialize (mDBConnection, mStatementString); + if (NS_FAILED(rv)) { + NS_RELEASE(mss); + return NS_ERROR_FAILURE; // XXX error code + } + + *_retval = mss; + return NS_OK; +} + +/* readonly attribute unsigned long parameterCount; */ +NS_IMETHODIMP +mozStorageStatement::GetParameterCount(PRUint32 *aParameterCount) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* void reset (); */ +NS_IMETHODIMP +mozStorageStatement::Reset() +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + + sqlite3_reset(mDBStatement); + + return NS_OK; +} + +/* void bindCStringParameter (in unsigned long aParamIndex, in string aValue); */ +NS_IMETHODIMP +mozStorageStatement::BindCStringParameter(PRUint32 aParamIndex, const char *aValue) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + if (aParamIndex < 0 || aParamIndex >= mParamCount) + return NS_ERROR_FAILURE; // XXXerror + + int srv = sqlite3_bind_text (mDBStatement, aParamIndex + 1, aValue, -1, SQLITE_TRANSIENT); + + return NS_OK; +} + +/* void bindUTF8StringParameter (in unsigned long aParamIndex, in AUTF8String aValue); */ +NS_IMETHODIMP +mozStorageStatement::BindUTF8StringParameter(PRUint32 aParamIndex, const nsACString & aValue) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + if (aParamIndex < 0 || aParamIndex >= mParamCount) + return NS_ERROR_FAILURE; // XXXerror + + int srv = sqlite3_bind_text (mDBStatement, aParamIndex + 1, + nsPromiseFlatCString(aValue).get(), aValue.Length(), + SQLITE_TRANSIENT); + + return NS_OK; +} + +/* void bindWStringParameter (in unsigned long aParamIndex, in wstring aValue); */ +NS_IMETHODIMP +mozStorageStatement::BindWStringParameter(PRUint32 aParamIndex, const PRUnichar *aValue) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + if (aParamIndex < 0 || aParamIndex >= mParamCount) + return NS_ERROR_FAILURE; // XXXerror + + int srv = sqlite3_bind_text16 (mDBStatement, aParamIndex + 1, aValue, -1, SQLITE_TRANSIENT); + + return NS_OK; +} + +/* void bindStringParameter (in unsigned long aParamIndex, in AString aValue); */ +NS_IMETHODIMP +mozStorageStatement::BindStringParameter(PRUint32 aParamIndex, const nsAString & aValue) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + if (aParamIndex < 0 || aParamIndex >= mParamCount) + return NS_ERROR_FAILURE; // XXXerror + + int srv = sqlite3_bind_text16 (mDBStatement, aParamIndex + 1, + nsPromiseFlatString(aValue).get(), aValue.Length() * 2, + SQLITE_TRANSIENT); + + return NS_OK; +} + +/* void bindDoubleParameter (in unsigned long aParamIndex, in double aValue); */ +NS_IMETHODIMP +mozStorageStatement::BindDoubleParameter(PRUint32 aParamIndex, double aValue) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + if (aParamIndex < 0 || aParamIndex >= mParamCount) + return NS_ERROR_FAILURE; // XXXerror + + int srv = sqlite3_bind_double (mDBStatement, aParamIndex + 1, aValue); + + return NS_OK; +} + +/* void bindInt32Parameter (in unsigned long aParamIndex, in long aValue); */ +NS_IMETHODIMP +mozStorageStatement::BindInt32Parameter(PRUint32 aParamIndex, PRInt32 aValue) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + if (aParamIndex < 0 || aParamIndex >= mParamCount) + return NS_ERROR_FAILURE; // XXXerror + + int srv = sqlite3_bind_int (mDBStatement, aParamIndex + 1, aValue); + + return NS_OK; +} + +/* void bindInt64Parameter (in unsigned long aParamIndex, in long long aValue); */ +NS_IMETHODIMP +mozStorageStatement::BindInt64Parameter(PRUint32 aParamIndex, PRInt64 aValue) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + if (aParamIndex < 0 || aParamIndex >= mParamCount) + return NS_ERROR_FAILURE; // XXXerror + + int srv = sqlite3_bind_int64 (mDBStatement, aParamIndex + 1, aValue); + + return NS_OK; +} + +/* void bindNullParameter (in unsigned long aParamIndex); */ +NS_IMETHODIMP +mozStorageStatement::BindNullParameter(PRUint32 aParamIndex) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + if (aParamIndex < 0 || aParamIndex >= mParamCount) + return NS_ERROR_FAILURE; // XXXerror + + int srv = sqlite3_bind_null (mDBStatement, aParamIndex + 1); + + return NS_OK; +} + +/* void bindDataParameter (in unsigned long aParamIndex, [array, size_is (aValueSize)] in octet aValue, in unsigned long aValueSize); */ +NS_IMETHODIMP +mozStorageStatement::BindDataParameter(PRUint32 aParamIndex, PRUint8 *aValue, PRUint32 aValueSize) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + if (aParamIndex < 0 || aParamIndex >= mParamCount) + return NS_ERROR_FAILURE; // XXXerror + + int srv = sqlite3_bind_blob (mDBStatement, aParamIndex + 1, aValue, aValueSize, SQLITE_TRANSIENT); + + return NS_OK; +} + +/* void execute (); */ +NS_IMETHODIMP +mozStorageStatement::Execute() +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + + int srv = sqlite3_step (mDBStatement); + if (srv == SQLITE_MISUSE || srv == SQLITE_ERROR) { +#ifdef PR_LOGGING + sqlite3 *sqh; + mDBConnection->GetSqliteHandle(&sqh); + PR_LOG(gStorageLog, PR_LOG_DEBUG, ("mozStorageStatement::Execute error: %s", sqlite3_errmsg(sqh))); +#endif + return NS_ERROR_FAILURE; // XXX error code + } + sqlite3_reset (mDBStatement); + + return NS_OK; +} + +/* mozIStorageDataSet executeDataSet (); */ +NS_IMETHODIMP +mozStorageStatement::ExecuteDataSet(mozIStorageDataSet **_retval) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + + int srv; + + + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* boolean executeStep (); */ +NS_IMETHODIMP +mozStorageStatement::ExecuteStep(PRBool *_retval) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + + int srv = sqlite3_step (mDBStatement); + +#ifdef PR_LOGGING + if (srv != SQLITE_ROW && srv != SQLITE_DONE) { + sqlite3 *sqh; + mDBConnection->GetSqliteHandle(&sqh); + PR_LOG(gStorageLog, PR_LOG_DEBUG, ("mozStorageStatement::ExecuteStep error: %s", sqlite3_errmsg(sqh))); + } +#endif + + // SQLITE_ROW and SQLITE_DONE are non-errors + if (srv == SQLITE_ROW) { + // we got a row back + *_retval = PR_TRUE; + return NS_OK; + } else if (srv == SQLITE_DONE) { + // statement is done (no row returned) + *_retval = PR_FALSE; + return NS_OK; + } else if (srv == SQLITE_BUSY) { + // ??? what to do? + return NS_ERROR_FAILURE; + } else if (srv == SQLITE_MISUSE) { + // bad stuff happened + return NS_ERROR_FAILURE; + } else if (srv == SQLITE_ERROR) { + // even worse stuff happened + } else { + // something that shouldn't happen happened + NS_ERROR ("sqlite3_step returned an error code we don't know about!"); + } + + // shouldn't get here + return NS_ERROR_FAILURE; +} + +#if 0 +/* nsISimpleEnumerator executeEnumerator (); */ +NS_IMETHODIMP +mozStorageStatement::ExecuteEnumerator(nsISimpleEnumerator **_retval) +{ + NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized"); + + mozStorageStatementRowEnumerator *mssre = new mozStorageStatementRowEnumerator (mDBStatement); + NS_ADDREF(mssre); + *_retval = mssre; + + return NS_OK; +} +#endif + +/*** + *** mozIStorageValueArray + ***/ + +/* readonly attribute unsigned long length; */ +NS_IMETHODIMP +mozStorageStatement::GetNumColumns(PRUint32 *aLength) +{ + *aLength = mResultColumnCount; + return NS_OK; +} + +/* long getTypeOfIndex (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatement::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + int t = sqlite3_column_type (mDBStatement, aIndex); + switch (t) { + case SQLITE_INTEGER: + *_retval = VALUE_TYPE_INTEGER; + break; + case SQLITE_FLOAT: + *_retval = VALUE_TYPE_FLOAT; + break; + case SQLITE_TEXT: + *_retval = VALUE_TYPE_TEXT; + break; + case SQLITE_BLOB: + *_retval = VALUE_TYPE_BLOB; + break; + case SQLITE_NULL: + *_retval = VALUE_TYPE_NULL; + break; + default: + // ??? + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +/* long getAsInt32 (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatement::GetAsInt32(PRUint32 aIndex, PRInt32 *_retval) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + *_retval = sqlite3_column_int (mDBStatement, aIndex); + + return NS_OK; +} + +/* long long getAsInt64 (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatement::GetAsInt64(PRUint32 aIndex, PRInt64 *_retval) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + *_retval = sqlite3_column_int64 (mDBStatement, aIndex); + + return NS_OK; +} + +/* double getAsDouble (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatement::GetAsDouble(PRUint32 aIndex, double *_retval) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + *_retval = sqlite3_column_double (mDBStatement, aIndex); + + return NS_OK; +} + +/* string getAsCString (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatement::GetAsCString(PRUint32 aIndex, char **_retval) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + int slen = sqlite3_column_bytes (mDBStatement, aIndex); + const unsigned char *cstr = sqlite3_column_text (mDBStatement, aIndex); + char *str = (char *) nsMemory::Clone (cstr, slen); + if (str == NULL) + return NS_ERROR_OUT_OF_MEMORY; + + *_retval = str; + return NS_OK; +} + +/* AUTF8String getAsUTF8String (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatement::GetAsUTF8String(PRUint32 aIndex, nsACString & _retval) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + int slen = sqlite3_column_bytes (mDBStatement, aIndex); + const unsigned char *cstr = sqlite3_column_text (mDBStatement, aIndex); + _retval.Assign ((char *) cstr, slen); + + return NS_OK; +} + +/* AString getAsString (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatement::GetAsString(PRUint32 aIndex, nsAString & _retval) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + int slen = sqlite3_column_bytes16 (mDBStatement, aIndex); + const PRUnichar *wstr = (const PRUnichar *) sqlite3_column_text16 (mDBStatement, aIndex); + _retval.Assign (wstr, slen/2); + + return NS_OK; +} + +/* void getAsBlob (in unsigned long aIndex, [array, size_is (aDataSize)] out octet aData, out unsigned long aDataSize); */ +NS_IMETHODIMP +mozStorageStatement::GetAsBlob(PRUint32 aIndex, PRUint8 **aData, PRUint32 *aDataSize) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + int blobsize = sqlite3_column_bytes (mDBStatement, aIndex); + const void *blob = sqlite3_column_blob (mDBStatement, aIndex); + + void *blobcopy = nsMemory::Clone(blob, blobsize); + if (blobcopy == NULL) + return NS_ERROR_OUT_OF_MEMORY; + + *aData = (PRUint8*) blobcopy; + *aDataSize = blobsize; + + return NS_OK; +} + +/* [notxpcom] long asInt32 (in unsigned long aIndex); */ +NS_IMETHODIMP_(PRInt32) +mozStorageStatement::AsInt32(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + return sqlite3_column_int (mDBStatement, aIndex); +} + +/* [notxpcom] long long asInt64 (in unsigned long aIndex); */ +NS_IMETHODIMP_(PRInt64) +mozStorageStatement::AsInt64(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + return sqlite3_column_int64 (mDBStatement, aIndex); +} + +/* [notxpcom] double asDouble (in unsigned long aIndex); */ +NS_IMETHODIMP_(double) +mozStorageStatement::AsDouble(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + return sqlite3_column_double (mDBStatement, aIndex); +} + +/* [notxpcom] string asSharedCString (in unsigned long aIndex, out unsigned long aLength); */ +NS_IMETHODIMP_(char *) +mozStorageStatement::AsSharedCString(PRUint32 aIndex, PRUint32 *aLength) +{ + if (aLength) { + int slen = sqlite3_column_bytes (mDBStatement, aIndex); + *aLength = slen; + } + + return (char *) sqlite3_column_text (mDBStatement, aIndex); +} + +/* [notxpcom] wstring asSharedWString (in unsigned long aIndex, out unsigned long aLength); */ +NS_IMETHODIMP_(PRUnichar *) +mozStorageStatement::AsSharedWString(PRUint32 aIndex, PRUint32 *aLength) +{ + if (aLength) { + int slen = sqlite3_column_bytes16 (mDBStatement, aIndex); + *aLength = slen; + } + + return (PRUnichar *) sqlite3_column_text16 (mDBStatement, aIndex); +} + +/* [noscript] void asSharedBlob (in unsigned long aIndex, [shared] out voidPtr aData, out unsigned long aDataSize); */ +NS_IMETHODIMP +mozStorageStatement::AsSharedBlob(PRUint32 aIndex, const void * *aData, PRUint32 *aDataSize) +{ + *aDataSize = sqlite3_column_bytes (mDBStatement, aIndex); + *aData = sqlite3_column_blob (mDBStatement, aIndex); + + return NS_OK; +} + +/* boolean getIsNull (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatement::GetIsNull(PRUint32 aIndex, PRBool *_retval) +{ + PRInt32 t; + nsresult rv = GetTypeOfIndex (aIndex, &t); + if (NS_FAILED(rv)) return rv; + + if (t == VALUE_TYPE_NULL) + *_retval = PR_TRUE; + else + *_retval = PR_FALSE; + + return NS_OK; +} + + +/* [notxpcom] boolean isNull (in unsigned long aIndex); */ +PRBool +mozStorageStatement::IsNull(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mResultColumnCount, "aIndex out of range"); + + return (sqlite3_column_type (mDBStatement, aIndex) == SQLITE_NULL); +} diff --git a/mozilla/storage/src/mozStorageStatement.h b/mozilla/storage/src/mozStorageStatement.h new file mode 100644 index 00000000000..9c1a58c9c38 --- /dev/null +++ b/mozilla/storage/src/mozStorageStatement.h @@ -0,0 +1,71 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 _MOZSTORAGESTATEMENT_H_ +#define _MOZSTORAGESTATEMENT_H_ + +#include "nsCOMPtr.h" +#include "nsString.h" + +#include "mozIStorageStatement.h" +#include "mozIStorageConnection.h" + +#include + +class mozStorageStatement : public mozIStorageStatement +{ +public: + mozStorageStatement(); + + // interfaces + NS_DECL_ISUPPORTS + NS_DECL_MOZISTORAGESTATEMENT + NS_DECL_MOZISTORAGEVALUEARRAY + +private: + ~mozStorageStatement(); + +protected: + nsCString mStatementString; + nsCOMPtr mDBConnection; + sqlite3_stmt *mDBStatement; + PRUint32 mParamCount; + PRUint32 mResultColumnCount; +}; + +#endif /* _MOZSTORAGESTATEMENT_H_ */ diff --git a/mozilla/storage/src/mozStorageValueArray.cpp b/mozilla/storage/src/mozStorageValueArray.cpp new file mode 100644 index 00000000000..2ba050b4d36 --- /dev/null +++ b/mozilla/storage/src/mozStorageValueArray.cpp @@ -0,0 +1,547 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 "nsError.h" +#include "nsMemory.h" +#include "nsString.h" + +#include "mozStorageValueArray.h" + +/*** + *** mozStorageStatementRowValueArray + ***/ + +/* Implementation file */ +NS_IMPL_ISUPPORTS1(mozStorageStatementRowValueArray, mozIStorageValueArray) + +mozStorageStatementRowValueArray::mozStorageStatementRowValueArray(sqlite3_stmt *aSqliteStmt) +{ + mSqliteStatement = aSqliteStmt; + mNumColumns = sqlite3_data_count (aSqliteStmt); +} + +mozStorageStatementRowValueArray::~mozStorageStatementRowValueArray() +{ + /* do nothing, we don't own the stmt */ +} + +/* readonly attribute unsigned long length; */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetNumColumns(PRUint32 *aLength) +{ + *aLength = mNumColumns; + return NS_OK; +} + +/* long getTypeOfIndex (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + int t = sqlite3_column_type (mSqliteStatement, aIndex); + switch (t) { + case SQLITE_INTEGER: + *_retval = VALUE_TYPE_INTEGER; + break; + case SQLITE_FLOAT: + *_retval = VALUE_TYPE_FLOAT; + break; + case SQLITE_TEXT: + *_retval = VALUE_TYPE_TEXT; + break; + case SQLITE_BLOB: + *_retval = VALUE_TYPE_BLOB; + break; + case SQLITE_NULL: + *_retval = VALUE_TYPE_NULL; + break; + default: + // ??? + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +/* long getAsInt32 (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetAsInt32(PRUint32 aIndex, PRInt32 *_retval) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + *_retval = sqlite3_column_int (mSqliteStatement, aIndex); + + return NS_OK; +} + +/* long long getAsInt64 (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetAsInt64(PRUint32 aIndex, PRInt64 *_retval) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + *_retval = sqlite3_column_int64 (mSqliteStatement, aIndex); + + return NS_OK; +} + +/* double getAsDouble (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetAsDouble(PRUint32 aIndex, double *_retval) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + *_retval = sqlite3_column_double (mSqliteStatement, aIndex); + + return NS_OK; +} + +/* string getAsCString (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetAsCString(PRUint32 aIndex, char **_retval) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + int slen = sqlite3_column_bytes (mSqliteStatement, aIndex); + const unsigned char *cstr = sqlite3_column_text (mSqliteStatement, aIndex); + char *str = (char *) nsMemory::Clone (cstr, slen); + if (str == NULL) + return NS_ERROR_OUT_OF_MEMORY; + + *_retval = str; + return NS_OK; +} + +/* AUTF8String getAsUTF8String (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetAsUTF8String(PRUint32 aIndex, nsACString & _retval) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + int slen = sqlite3_column_bytes (mSqliteStatement, aIndex); + const unsigned char *cstr = sqlite3_column_text (mSqliteStatement, aIndex); + _retval.Assign ((char *) cstr, slen); + + return NS_OK; +} + +/* AString getAsString (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetAsString(PRUint32 aIndex, nsAString & _retval) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + int slen = sqlite3_column_bytes16 (mSqliteStatement, aIndex); + const PRUnichar *wstr = (const PRUnichar *) sqlite3_column_text16 (mSqliteStatement, aIndex); + _retval.Assign (wstr, slen/2); + + return NS_OK; +} + +/* void getAsBlob (in unsigned long aIndex, [array, size_is (aDataSize)] out octet aData, out unsigned long aDataSize); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetAsBlob(PRUint32 aIndex, PRUint8 **aData, PRUint32 *aDataSize) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + int blobsize = sqlite3_column_bytes (mSqliteStatement, aIndex); + const void *blob = sqlite3_column_blob (mSqliteStatement, aIndex); + + void *blobcopy = nsMemory::Clone(blob, blobsize); + if (blobcopy == NULL) + return NS_ERROR_OUT_OF_MEMORY; + + *aData = (PRUint8*) blobcopy; + *aDataSize = blobsize; + + return NS_OK; +} + +/* [notxpcom] long asInt32 (in unsigned long aIndex); */ +NS_IMETHODIMP_(PRInt32) +mozStorageStatementRowValueArray::AsInt32(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + return sqlite3_column_int (mSqliteStatement, aIndex); +} + +/* [notxpcom] long long asInt64 (in unsigned long aIndex); */ +NS_IMETHODIMP_(PRInt64) +mozStorageStatementRowValueArray::AsInt64(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + return sqlite3_column_int64 (mSqliteStatement, aIndex); +} + +/* [notxpcom] double asDouble (in unsigned long aIndex); */ +NS_IMETHODIMP_(double) +mozStorageStatementRowValueArray::AsDouble(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + return sqlite3_column_double (mSqliteStatement, aIndex); +} + +/* [notxpcom] string asSharedCString (in unsigned long aIndex, out unsigned long aLength); */ +NS_IMETHODIMP_(char *) +mozStorageStatementRowValueArray::AsSharedCString(PRUint32 aIndex, PRUint32 *aLength) +{ + if (aLength) { + int slen = sqlite3_column_bytes (mSqliteStatement, aIndex); + *aLength = slen; + } + + return (char *) sqlite3_column_text (mSqliteStatement, aIndex); +} + +/* [notxpcom] wstring asSharedWString (in unsigned long aIndex, out unsigned long aLength); */ +NS_IMETHODIMP_(PRUnichar *) +mozStorageStatementRowValueArray::AsSharedWString(PRUint32 aIndex, PRUint32 *aLength) +{ + if (aLength) { + int slen = sqlite3_column_bytes16 (mSqliteStatement, aIndex); + *aLength = slen; + } + + return (PRUnichar *) sqlite3_column_text16 (mSqliteStatement, aIndex); +} + +/* [noscript] void asSharedBlob (in unsigned long aIndex, [shared] out voidPtr aData, out unsigned long aDataSize); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::AsSharedBlob(PRUint32 aIndex, const void * *aData, PRUint32 *aDataSize) +{ + *aDataSize = sqlite3_column_bytes (mSqliteStatement, aIndex); + *aData = sqlite3_column_blob (mSqliteStatement, aIndex); + + return NS_OK; +} + +/* boolean getIsNull (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageStatementRowValueArray::GetIsNull(PRUint32 aIndex, PRBool *_retval) +{ + PRInt32 t; + nsresult rv = GetTypeOfIndex (aIndex, &t); + if (NS_FAILED(rv)) return rv; + + if (t == VALUE_TYPE_NULL) + *_retval = PR_TRUE; + else + *_retval = PR_FALSE; + + return NS_OK; +} + + +/* [notxpcom] boolean isNull (in unsigned long aIndex); */ +PRBool +mozStorageStatementRowValueArray::IsNull(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mNumColumns, "aIndex out of range"); + + return (sqlite3_column_type (mSqliteStatement, aIndex) == SQLITE_NULL); +} + +/*** + *** mozStorageArgvValueArray + ***/ + +/* Implementation file */ +NS_IMPL_ISUPPORTS1(mozStorageArgvValueArray, mozIStorageValueArray) + +mozStorageArgvValueArray::mozStorageArgvValueArray(PRInt32 aArgc, sqlite3_value **aArgv) + : mArgc(aArgc), mArgv(aArgv) +{ +} + +mozStorageArgvValueArray::~mozStorageArgvValueArray() +{ + /* do nothing, we don't own the array */ +} + +/* readonly attribute unsigned long length; */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetNumColumns(PRUint32 *aLength) +{ + *aLength = mArgc; + return NS_OK; +} + +/* long getTypeOfIndex (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + int t = sqlite3_value_type (mArgv[aIndex]); + switch (t) { + case SQLITE_INTEGER: + *_retval = VALUE_TYPE_INTEGER; + break; + case SQLITE_FLOAT: + *_retval = VALUE_TYPE_FLOAT; + break; + case SQLITE_TEXT: + *_retval = VALUE_TYPE_TEXT; + break; + case SQLITE_BLOB: + *_retval = VALUE_TYPE_BLOB; + break; + case SQLITE_NULL: + *_retval = VALUE_TYPE_NULL; + break; + default: + // ??? + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +/* long getAsInt32 (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetAsInt32(PRUint32 aIndex, PRInt32 *_retval) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + *_retval = sqlite3_value_int (mArgv[aIndex]); + + return NS_OK; +} + +/* long long getAsInt64 (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetAsInt64(PRUint32 aIndex, PRInt64 *_retval) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + *_retval = sqlite3_value_int64 (mArgv[aIndex]); + + return NS_OK; +} + +/* double getAsDouble (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetAsDouble(PRUint32 aIndex, double *_retval) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + *_retval = sqlite3_value_double (mArgv[aIndex]); + + return NS_OK; +} + +/* string getAsCString (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetAsCString(PRUint32 aIndex, char **_retval) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + int slen = sqlite3_value_bytes (mArgv[aIndex]); + const unsigned char *cstr = sqlite3_value_text (mArgv[aIndex]); + char *str = (char *) nsMemory::Clone (cstr, slen); + if (str == NULL) + return NS_ERROR_OUT_OF_MEMORY; + + *_retval = str; + return NS_OK; +} + +/* AUTF8String getAsUTF8String (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetAsUTF8String(PRUint32 aIndex, nsACString & _retval) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + int slen = sqlite3_value_bytes (mArgv[aIndex]); + const unsigned char *cstr = sqlite3_value_text (mArgv[aIndex]); + _retval.Assign ((char *) cstr, slen); + + return NS_OK; +} + +/* AString getAsString (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetAsString(PRUint32 aIndex, nsAString & _retval) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + int slen = sqlite3_value_bytes16 (mArgv[aIndex]); + const PRUnichar *wstr = (const PRUnichar *) sqlite3_value_text16 (mArgv[aIndex]); + _retval.Assign (wstr, slen); + + return NS_OK; +} + +/* void getAsBlob (in unsigned long aIndex, [array, size_is (aDataSize)] out octet aData, out unsigned long aDataSize); */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetAsBlob(PRUint32 aIndex, PRUint8 **aData, PRUint32 *aDataSize) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + int blobsize = sqlite3_value_bytes (mArgv[aIndex]); + const void *blob = sqlite3_value_blob (mArgv[aIndex]); + + void *blobcopy = nsMemory::Clone(blob, blobsize); + if (blobcopy == NULL) + return NS_ERROR_OUT_OF_MEMORY; + + *aData = (PRUint8*) blobcopy; + *aDataSize = blobsize; + + return NS_OK; +} + +/* [notxpcom] long asInt32 (in unsigned long aIndex); */ +NS_IMETHODIMP_(PRInt32) +mozStorageArgvValueArray::AsInt32(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + return sqlite3_value_int (mArgv[aIndex]); +} + +/* [notxpcom] long long asInt64 (in unsigned long aIndex); */ +NS_IMETHODIMP_(PRInt64) +mozStorageArgvValueArray::AsInt64(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + return sqlite3_value_int64 (mArgv[aIndex]); +} + +/* [notxpcom] double asDouble (in unsigned long aIndex); */ +NS_IMETHODIMP_(double) +mozStorageArgvValueArray::AsDouble(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + return sqlite3_value_double (mArgv[aIndex]); +} + +/* [notxpcom] string asSharedCString (in unsigned long aIndex, out unsigned long aLength); */ +NS_IMETHODIMP_(char *) +mozStorageArgvValueArray::AsSharedCString(PRUint32 aIndex, PRUint32 *aLength) +{ + if (aLength) { + int slen = sqlite3_value_bytes (mArgv[aIndex]); + *aLength = slen; + } + + return (char *) sqlite3_value_text (mArgv[aIndex]); +} + +/* [notxpcom] wstring asSharedWString (in unsigned long aIndex, out unsigned long aLength); */ +NS_IMETHODIMP_(PRUnichar *) +mozStorageArgvValueArray::AsSharedWString(PRUint32 aIndex, PRUint32 *aLength) +{ + if (aLength) { + int slen = sqlite3_value_bytes16 (mArgv[aIndex]); + *aLength = slen; + } + + return (PRUnichar *) sqlite3_value_text16 (mArgv[aIndex]); +} + +/* [noscript] void asSharedBlob (in unsigned long aIndex, [shared] out voidPtr aData, out unsigned long aDataSize); */ +NS_IMETHODIMP +mozStorageArgvValueArray::AsSharedBlob(PRUint32 aIndex, const void * *aData, PRUint32 *aDataSize) +{ + *aDataSize = sqlite3_value_bytes (mArgv[aIndex]); + *aData = sqlite3_value_blob (mArgv[aIndex]); + + return NS_OK; +} + +/* boolean getIsNull (in unsigned long aIndex); */ +NS_IMETHODIMP +mozStorageArgvValueArray::GetIsNull(PRUint32 aIndex, PRBool *_retval) +{ + PRInt32 t; + nsresult rv = GetTypeOfIndex (aIndex, &t); + if (NS_FAILED(rv)) return rv; + + if (t == VALUE_TYPE_NULL) + *_retval = PR_TRUE; + else + *_retval = PR_FALSE; + + return NS_OK; +} + + +/* [notxpcom] boolean isNull (in unsigned long aIndex); */ +PRBool +mozStorageArgvValueArray::IsNull(PRUint32 aIndex) +{ + NS_ASSERTION (aIndex < mArgc, "aIndex out of range"); + + return (sqlite3_value_type (mArgv[aIndex]) == SQLITE_NULL); +} + +/*** + *** mozStorageDataSet + ***/ + +NS_IMPL_ISUPPORTS1(mozStorageDataSet, mozIStorageDataSet) + +mozStorageDataSet::mozStorageDataSet(nsIArray *aRows) + : mRows(aRows) +{ +} + +mozStorageDataSet::~mozStorageDataSet() +{ +} + +/* readonly attribute nsIArray dataRows; */ +NS_IMETHODIMP +mozStorageDataSet::GetDataRows(nsIArray **aDataRows) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +/* nsISimpleEnumerator getRowEnumerator (); */ +NS_IMETHODIMP +mozStorageDataSet::GetRowEnumerator(nsISimpleEnumerator **_retval) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} diff --git a/mozilla/storage/src/mozStorageValueArray.h b/mozilla/storage/src/mozStorageValueArray.h new file mode 100644 index 00000000000..f2a0eb20355 --- /dev/null +++ b/mozilla/storage/src/mozStorageValueArray.h @@ -0,0 +1,97 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 Oracle Corporation code. + * + * The Initial Developer of the Original Code is + * Oracle Corporation + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Vladimir Vukicevic + * + * 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 _MOZSTORAGEVALUEARRAY_H_ +#define _MOZSTORAGEVALUEARRAY_H_ + +#include "nsCOMPtr.h" + +#include "mozIStorageValueArray.h" +#include "mozIStorageDataSet.h" + +#include "nsIArray.h" + +#include + +class nsIArray; + +class mozStorageStatementRowValueArray : public mozIStorageValueArray +{ +public: + mozStorageStatementRowValueArray (sqlite3_stmt *aSqliteStmt); + ~mozStorageStatementRowValueArray(); + + // interfaces + NS_DECL_ISUPPORTS + NS_DECL_MOZISTORAGEVALUEARRAY + +private: + sqlite3_stmt *mSqliteStatement; + PRUint32 mNumColumns; +}; + +class mozStorageArgvValueArray : public mozIStorageValueArray +{ +public: + mozStorageArgvValueArray (PRInt32 aArgc, sqlite3_value **aArgv); + ~mozStorageArgvValueArray(); + + // interfaces + NS_DECL_ISUPPORTS + NS_DECL_MOZISTORAGEVALUEARRAY + +private: + PRUint32 mArgc; + sqlite3_value **mArgv; +}; + +class mozStorageDataSet : public mozIStorageDataSet +{ +public: + mozStorageDataSet (nsIArray *aRows); + ~mozStorageDataSet(); + + // interfaces + NS_DECL_ISUPPORTS + NS_DECL_MOZISTORAGEDATASET + +protected: + nsCOMPtr mRows; +}; + +#endif /* _MOZSTORAGEVALUEARRAY_H_ */ diff --git a/mozilla/storage/test/Makefile.in b/mozilla/storage/test/Makefile.in new file mode 100644 index 00000000000..6876f7af45a --- /dev/null +++ b/mozilla/storage/test/Makefile.in @@ -0,0 +1,59 @@ +# +# ***** 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 Initial Developer of the Original Code is +# Oracle Corporation +# Portions created by the Initial Developer are Copyright (C) 2004 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Vladimir Vukicevic +# +# Alternatively, the contents of this file may be used under the terms of +# either of 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 + +PROGRAM = teststorage1$(BIN_SUFFIX) +CPPSRCS = storage1.cpp +REQUIRES = \ + xpcom \ + string \ + storage \ + storagecomps \ + $(NULL) + +LIBS = \ + $(LIBS_DIR) \ + $(XPCOM_LIBS) \ + $(NSPR_LIBS) \ + $(NULL) + +include $(topsrcdir)/config/rules.mk diff --git a/mozilla/storage/test/storage1.cpp b/mozilla/storage/test/storage1.cpp new file mode 100644 index 00000000000..bfb192917a3 --- /dev/null +++ b/mozilla/storage/test/storage1.cpp @@ -0,0 +1,135 @@ + +#include +#include + +#include "nsIComponentManager.h" +#include "nsISimpleEnumerator.h" +#include "nsIServiceManager.h" +#include "nsCOMPtr.h" +#include "nsString.h" +#include "nsILocalFile.h" + +#include "mozIStorageService.h" +#include "mozIStorageConnection.h" +#include "mozIStorageValue.h" +#include "mozIStorageStatement.h" +#include "mozIStorageFunction.h" + +#include "mozStorageCID.h" + +static NS_DEFINE_CID(kmozStorageServiceCID, MOZ_STORAGE_SERVICE_CID); +static NS_DEFINE_CID(kmozStorageConnectionCID, MOZ_STORAGE_CONNECTION_CID); + +#define TEST_CHECK_ERROR(rv) \ + do { if (NS_FAILED(rv)) { \ + dbConn->GetLastError(&gerr); \ + dbConn->GetLastErrorString(gerrstr); \ + fprintf (stderr, "Error: %d 0x%08x %s\n", gerr, gerr, gerrstr.get()); \ + return 0; \ + } } while (0) + +int gerr; +nsCString gerrstr; + +class TestFunc : public mozIStorageFunction { +public: + TestFunc() { } + NS_DECL_ISUPPORTS + NS_DECL_MOZISTORAGEFUNCTION +}; + +NS_IMPL_ISUPPORTS1(TestFunc, mozIStorageFunction) + +NS_IMETHODIMP +TestFunc::OnFunctionCall (mozIStorageValueArray *sva) +{ + fprintf (stderr, "* function call!\n"); + return NS_OK; +} + +int +main (int argc, char **argv) +{ + nsresult rv; + + NS_InitXPCOM2(nsnull, nsnull, nsnull); + + nsCOMPtr dbSrv; + dbSrv = do_GetService(kmozStorageServiceCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr f; + rv = NS_NewNativeLocalFile (NS_LITERAL_CSTRING("foo.sdb"), PR_FALSE, getter_AddRefs(f)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr dbConn; + rv = dbSrv->OpenDatabase(f, getter_AddRefs(dbConn)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = dbConn->CreateFunction("x_test", -1, new TestFunc()); + NS_ENSURE_SUCCESS(rv, rv); + + rv = dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("SELECT x_test(1)")); + NS_ENSURE_SUCCESS(rv, rv); + + rv = dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DROP TABLE foo")); + // TEST_CHECK_ERROR(rv); + + rv = dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("CREATE TABLE foo (i INTEGER)")); + TEST_CHECK_ERROR(rv); + + nsCOMPtr dbFooInsertStatement; + rv = dbConn->CreateStatement (NS_LITERAL_CSTRING("INSERT INTO foo VALUES ( ?1 )"), getter_AddRefs(dbFooInsertStatement)); + TEST_CHECK_ERROR(rv); + + nsCOMPtr dbFooSelectStatement; + rv = dbConn->CreateStatement (NS_LITERAL_CSTRING("SELECT i FROM foo"), getter_AddRefs(dbFooSelectStatement)); + TEST_CHECK_ERROR(rv); + + rv = dbConn->CreateTrigger("foo_trig", mozIStorageConnection::TRIGGER_EVENT_INSERT, + "foo", "x_test", "1"); + TEST_CHECK_ERROR(rv); + + for (int i = 0; i < 10; i++) { + rv = dbFooInsertStatement->BindInt32Parameter (0, i); + TEST_CHECK_ERROR(rv); + + rv = dbFooInsertStatement->Execute (); + TEST_CHECK_ERROR(rv); + } + + fprintf (stderr, "10 values written to foo...\n"); + + nsCOMPtr dbRow = do_QueryInterface(dbFooSelectStatement); + PRBool hasMore = PR_FALSE; + + while ((dbFooSelectStatement->ExecuteStep(&hasMore) == NS_OK) && hasMore) + { + PRUint32 len; + + dbRow->GetNumColumns (&len); + fprintf (stderr, "Row[length %d]: %d '%s'\n", len, dbRow->AsInt32(0), dbRow->AsSharedCString(0, 0)); + } + + TEST_CHECK_ERROR(rv); + fprintf (stderr, "Done. %d 0x%08x %p\n", rv, rv, dbRow.get()); + +#if 0 + // try an enumerator + fprintf (stderr, "Trying via an enumerator\n"); + + nsCOMPtr dbEnum; + + (void) dbFooSelectStatement->Reset (); + rv = dbFooSelectStatement->ExecuteEnumerator (getter_AddRefs(dbEnum)); + TEST_CHECK_ERROR(rv); + + PRBool b; + while ((dbEnum->HasMoreElements(&b) == NS_OK) && b) { + dbEnum->GetNext (getter_AddRefs (dbRow)); + PRUint32 len; + dbRow->GetLength (&len); + fprintf (stderr, "Row[length %d]: %d '%s'\n", len, dbRow->AsInt32(0), dbRow->AsSharedCString(0, 0)); + } +#endif +}