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
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is the mozStorage statement cache.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : * Shawn Wilsher <me@shawnwilsher.com>
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef mozilla_storage_StatementCache_h
41 : #define mozilla_storage_StatementCache_h
42 :
43 : #include "mozIStorageConnection.h"
44 : #include "mozIStorageStatement.h"
45 : #include "mozIStorageAsyncStatement.h"
46 :
47 : #include "nsAutoPtr.h"
48 : #include "nsHashKeys.h"
49 : #include "nsInterfaceHashtable.h"
50 :
51 : namespace mozilla {
52 : namespace storage {
53 :
54 : /**
55 : * Class used to cache statements (mozIStorageStatement or
56 : * mozIStorageAsyncStatement).
57 : */
58 : template<typename StatementType>
59 960 : class StatementCache {
60 : public:
61 : /**
62 : * Constructor for the cache.
63 : *
64 : * @note a connection can have more than one cache.
65 : *
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.
70 : */
71 960 : StatementCache(nsCOMPtr<mozIStorageConnection>& aConnection)
72 960 : : mConnection(aConnection)
73 : {
74 960 : if (!mCachedStatements.Init()) {
75 0 : NS_ERROR("Out of memory!?");
76 : }
77 960 : }
78 :
79 : /**
80 : * Obtains a cached statement. If this statement is not yet created, it will
81 : * be created and stored for later use.
82 : *
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.
87 : */
88 : inline
89 : already_AddRefed<StatementType>
90 58912 : GetCachedStatement(const nsACString& aQuery)
91 : {
92 117824 : nsCOMPtr<StatementType> stmt;
93 58912 : if (!mCachedStatements.Get(aQuery, getter_AddRefs(stmt))) {
94 4299 : stmt = CreateStatement(aQuery);
95 4299 : NS_ENSURE_TRUE(stmt, nsnull);
96 :
97 4299 : if (!mCachedStatements.Put(aQuery, stmt)) {
98 0 : NS_ERROR("Out of memory!?");
99 : }
100 : }
101 58912 : return stmt.forget();
102 : }
103 :
104 : template<int N>
105 : NS_ALWAYS_INLINE already_AddRefed<StatementType>
106 : GetCachedStatement(const char (&aQuery)[N])
107 : {
108 422 : nsDependentCString query(aQuery, N - 1);
109 211 : return GetCachedStatement(query);
110 : }
111 :
112 : /**
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.
115 : */
116 : inline
117 : void
118 873 : FinalizeStatements()
119 : {
120 873 : (void)mCachedStatements.Enumerate(FinalizeCachedStatements, NULL);
121 :
122 : // Clear the cache at this time too!
123 873 : (void)mCachedStatements.Clear();
124 873 : }
125 :
126 : private:
127 : inline
128 : already_AddRefed<StatementType>
129 : CreateStatement(const nsACString& aQuery);
130 : static
131 : PLDHashOperator
132 4295 : FinalizeCachedStatements(const nsACString& aKey,
133 : nsCOMPtr<StatementType>& aStatement,
134 : void*)
135 : {
136 4295 : (void)aStatement->Finalize();
137 4295 : return PL_DHASH_NEXT;
138 : }
139 :
140 : nsInterfaceHashtable<nsCStringHashKey, StatementType> mCachedStatements;
141 : nsCOMPtr<mozIStorageConnection>& mConnection;
142 : };
143 :
144 : template< >
145 : inline
146 : already_AddRefed<mozIStorageStatement>
147 3457 : StatementCache<mozIStorageStatement>::CreateStatement(const nsACString& aQuery)
148 : {
149 3457 : NS_ENSURE_TRUE(mConnection, nsnull);
150 :
151 6914 : nsCOMPtr<mozIStorageStatement> stmt;
152 3457 : nsresult rv = mConnection->CreateStatement(aQuery, getter_AddRefs(stmt));
153 3457 : if (NS_FAILED(rv)) {
154 0 : nsCString error;
155 0 : error.AppendLiteral("The statement '");
156 0 : error.Append(aQuery);
157 0 : error.AppendLiteral("' failed to compile with the error message '");
158 0 : nsCString msg;
159 0 : (void)mConnection->GetLastErrorString(msg);
160 0 : error.Append(msg);
161 0 : error.AppendLiteral("'.");
162 0 : NS_ERROR(error.get());
163 : }
164 3457 : NS_ENSURE_SUCCESS(rv, nsnull);
165 :
166 3457 : return stmt.forget();
167 : }
168 :
169 : template< >
170 : inline
171 : already_AddRefed<mozIStorageAsyncStatement>
172 842 : StatementCache<mozIStorageAsyncStatement>::CreateStatement(const nsACString& aQuery)
173 : {
174 842 : NS_ENSURE_TRUE(mConnection, nsnull);
175 :
176 1684 : nsCOMPtr<mozIStorageAsyncStatement> stmt;
177 842 : nsresult rv = mConnection->CreateAsyncStatement(aQuery, getter_AddRefs(stmt));
178 842 : NS_ENSURE_SUCCESS(rv, nsnull);
179 :
180 842 : return stmt.forget();
181 : }
182 :
183 : } // namespace storage
184 : } // namespace mozilla
185 :
186 : #endif // mozilla_storage_StatementCache_h
|