CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / storage / public / StatementCache.h
blob1adac7013e199d174ed9dc8930980f42d78e5c0c
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
14 * License.
16 * The Original Code is the mozStorage statement cache.
18 * The Initial Developer of the Original Code is
19 * the Mozilla Foundation.
20 * Portions created by the Initial Developer are Copyright (C) 2010
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Shawn Wilsher <me@shawnwilsher.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef mozilla_storage_StatementCache_h
41 #define mozilla_storage_StatementCache_h
43 #include "mozIStorageConnection.h"
44 #include "mozIStorageStatement.h"
45 #include "mozIStorageAsyncStatement.h"
47 #include "nsAutoPtr.h"
48 #include "nsHashKeys.h"
49 #include "nsInterfaceHashtable.h"
51 namespace mozilla {
52 namespace storage {
54 /**
55 * Class used to cache statements (mozIStorageStatement or
56 * mozIStorageAsyncStatement).
58 template<typename StatementType>
59 class StatementCache {
60 public:
61 /**
62 * Constructor for the cache.
64 * @note a connection can have more than one cache.
66 * @param aConnection
67 * A reference to the nsCOMPtr for the connection this cache is to be
68 * used for. This nsCOMPtr must at least live as long as this class,
69 * otherwise crashes will happen.
71 StatementCache(nsCOMPtr<mozIStorageConnection>& aConnection)
72 : mConnection(aConnection)
74 if (!mCachedStatements.Init()) {
75 NS_ERROR("Out of memory!?");
79 /**
80 * Obtains a cached statement. If this statement is not yet created, it will
81 * be created and stored for later use.
83 * @param aQuery
84 * The SQL string (either a const char [] or nsACString) to get a
85 * cached query for.
86 * @return the cached statement, or null upon error.
88 inline
89 already_AddRefed<StatementType>
90 GetCachedStatement(const nsACString& aQuery)
92 nsCOMPtr<StatementType> stmt;
93 if (!mCachedStatements.Get(aQuery, getter_AddRefs(stmt))) {
94 stmt = CreateStatement(aQuery);
95 NS_ENSURE_TRUE(stmt, nsnull);
97 if (!mCachedStatements.Put(aQuery, stmt)) {
98 NS_ERROR("Out of memory!?");
101 return stmt.forget();
104 template<int N>
105 NS_ALWAYS_INLINE already_AddRefed<StatementType>
106 GetCachedStatement(const char (&aQuery)[N])
108 nsDependentCString query(aQuery, N - 1);
109 return GetCachedStatement(query);
113 * Finalizes all cached statements so the database can be safely closed. The
114 * behavior of this cache is unspecified after this method is called.
116 inline
117 void
118 FinalizeStatements()
120 (void)mCachedStatements.Enumerate(FinalizeCachedStatements, NULL);
122 // Clear the cache at this time too!
123 (void)mCachedStatements.Clear();
126 private:
127 inline
128 already_AddRefed<StatementType>
129 CreateStatement(const nsACString& aQuery);
130 static
131 PLDHashOperator
132 FinalizeCachedStatements(const nsACString& aKey,
133 nsCOMPtr<StatementType>& aStatement,
134 void*)
136 (void)aStatement->Finalize();
137 return PL_DHASH_NEXT;
140 nsInterfaceHashtable<nsCStringHashKey, StatementType> mCachedStatements;
141 nsCOMPtr<mozIStorageConnection>& mConnection;
144 template< >
145 inline
146 already_AddRefed<mozIStorageStatement>
147 StatementCache<mozIStorageStatement>::CreateStatement(const nsACString& aQuery)
149 NS_ENSURE_TRUE(mConnection, nsnull);
151 nsCOMPtr<mozIStorageStatement> stmt;
152 nsresult rv = mConnection->CreateStatement(aQuery, getter_AddRefs(stmt));
153 if (NS_FAILED(rv)) {
154 nsCString error;
155 error.AppendLiteral("The statement '");
156 error.Append(aQuery);
157 error.AppendLiteral("' failed to compile with the error message '");
158 nsCString msg;
159 (void)mConnection->GetLastErrorString(msg);
160 error.Append(msg);
161 error.AppendLiteral("'.");
162 NS_ERROR(error.get());
164 NS_ENSURE_SUCCESS(rv, nsnull);
166 return stmt.forget();
169 template< >
170 inline
171 already_AddRefed<mozIStorageAsyncStatement>
172 StatementCache<mozIStorageAsyncStatement>::CreateStatement(const nsACString& aQuery)
174 NS_ENSURE_TRUE(mConnection, nsnull);
176 nsCOMPtr<mozIStorageAsyncStatement> stmt;
177 nsresult rv = mConnection->CreateAsyncStatement(aQuery, getter_AddRefs(stmt));
178 NS_ENSURE_SUCCESS(rv, nsnull);
180 return stmt.forget();
183 } // namespace storage
184 } // namespace mozilla
186 #endif // mozilla_storage_StatementCache_h