1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Oracle Corporation code.
18 * The Initial Developer of the Original Code is
20 * Portions created by the Initial Developer are Copyright (C) 2004
21 * the Initial Developer. All Rights Reserved.
24 * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 * Lev Serebryakov <lev@serebryakov.spb.ru>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef mozilla_storage_Connection_h
42 #define mozilla_storage_Connection_h
44 #include "nsAutoPtr.h"
46 #include "mozilla/Mutex.h"
47 #include "nsIInterfaceRequestor.h"
50 #include "nsDataHashtable.h"
51 #include "mozIStorageProgressHandler.h"
52 #include "SQLiteMutex.h"
53 #include "mozIStorageConnection.h"
54 #include "mozStorageService.h"
56 #include "nsIMutableArray.h"
68 class Connection
: public mozIStorageConnection
69 , public nsIInterfaceRequestor
73 NS_DECL_MOZISTORAGECONNECTION
74 NS_DECL_NSIINTERFACEREQUESTOR
77 * Structure used to describe user functions on the database connection.
85 nsCOMPtr
<nsISupports
> function
;
92 * Pointer to the storage service. Held onto for the lifetime of the
95 * The flags to pass to sqlite3_open_v2.
97 Connection(Service
*aService
, int aFlags
);
100 * Creates the connection to the database.
102 * @param aDatabaseFile
103 * The nsIFile of the location of the database to open, or create if it
104 * does not exist. Passing in nsnull here creates an in-memory
107 * The VFS that SQLite will use when opening this database. NULL means
110 nsresult
initialize(nsIFile
*aDatabaseFile
,
111 const char* aVFSName
= NULL
);
113 // fetch the native handle
114 sqlite3
*GetNativeConnection() { return mDBConn
; }
117 * Lazily creates and returns a background execution thread. In the future,
118 * the thread may be re-claimed if left idle, so you should call this
119 * method just before you dispatch and not save the reference.
121 * @returns an event target suitable for asynchronous statement execution.
123 nsIEventTarget
*getAsyncExecutionTarget();
126 * Mutex used by asynchronous statements to protect state. The mutex is
127 * declared on the connection object because there is no contention between
128 * asynchronous statements (they are serialized on mAsyncExecutionThread). It
129 * also protects mPendingStatements.
131 Mutex sharedAsyncExecutionMutex
;
134 * Wraps the mutex that SQLite gives us from sqlite3_db_mutex. This is public
135 * because we already expose the sqlite3* native connection and proper
136 * operation of the deadlock detector requires everyone to use the same single
137 * SQLiteMutex instance for correctness.
139 SQLiteMutex sharedDBMutex
;
142 * References the thread this database was opened on. This MUST be thread it is
145 const nsCOMPtr
<nsIThread
> threadOpenedOn
;
148 * Closes the SQLite database, and warns about any non-finalized statements.
150 nsresult
internalClose();
153 * Obtains the filename of the connection. Useful for logging.
155 nsCString
getFilename();
161 * Sets the database into a closed state so no further actions can be
164 * @note mDBConn is set to NULL in this method.
166 nsresult
setClosedState();
169 * Describes a certain primitive type in the database.
171 * Possible Values Are:
172 * INDEX - To check for the existence of an index
173 * TABLE - To check for the existence of a table
175 enum DatabaseElementType
{
181 * Determines if the specified primitive exists.
183 * @param aElementType
184 * The type of element to check the existence of
185 * @param aElementName
186 * The name of the element to check for
187 * @returns true if element exists, false otherwise
189 nsresult
databaseElementExists(enum DatabaseElementType aElementType
,
190 const nsACString
& aElementName
,
193 bool findFunctionByInstance(nsISupports
*aInstance
);
195 static int sProgressHelper(void *aArg
);
196 // Generic progress handler
197 // Dispatch call to registered progress handler,
198 // if there is one. Do nothing in other cases.
199 int progressHandler();
202 nsCOMPtr
<nsIFile
> mDatabaseFile
;
205 * Lazily created thread for asynchronous statement execution. Consumers
206 * should use getAsyncExecutionTarget rather than directly accessing this
209 nsCOMPtr
<nsIThread
> mAsyncExecutionThread
;
211 * Set to true by Close() prior to actually shutting down the thread. This
212 * lets getAsyncExecutionTarget() know not to hand out any more thread
213 * references (or to create the thread in the first place). This variable
214 * should be accessed while holding the mAsyncExecutionMutex.
216 bool mAsyncExecutionThreadShuttingDown
;
219 * Tracks if we have a transaction in progress or not. Access protected by
222 PRBool mTransactionInProgress
;
225 * Stores the mapping of a given function by name to its instance. Access is
226 * protected by mDBMutex.
228 nsDataHashtable
<nsCStringHashKey
, FunctionInfo
> mFunctions
;
231 * Stores the registered progress handler for the database connection. Access
232 * is protected by mDBMutex.
234 nsCOMPtr
<mozIStorageProgressHandler
> mProgressHandler
;
237 * Stores the flags we passed to sqlite3_open_v2.
241 // This is here for two reasons: 1) It's used to make sure that the
242 // connections do not outlive the service. 2) Our custom collating functions
243 // call its localeCompareStrings() method.
244 nsRefPtr
<Service
> mStorageService
;
247 } // namespace storage
248 } // namespace mozilla
250 #endif // mozilla_storage_Connection_h