OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SQL_CONNECTION_H_ | 5 #ifndef SQL_CONNECTION_H_ |
6 #define SQL_CONNECTION_H_ | 6 #define SQL_CONNECTION_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 | 197 |
198 // Returns the current transaction nesting, which will be 0 if there are | 198 // Returns the current transaction nesting, which will be 0 if there are |
199 // no open transactions. | 199 // no open transactions. |
200 int transaction_nesting() const { return transaction_nesting_; } | 200 int transaction_nesting() const { return transaction_nesting_; } |
201 | 201 |
202 // Statements ---------------------------------------------------------------- | 202 // Statements ---------------------------------------------------------------- |
203 | 203 |
204 // Executes the given SQL string, returning true on success. This is | 204 // Executes the given SQL string, returning true on success. This is |
205 // normally used for simple, 1-off statements that don't take any bound | 205 // normally used for simple, 1-off statements that don't take any bound |
206 // parameters and don't return any data (e.g. CREATE TABLE). | 206 // parameters and don't return any data (e.g. CREATE TABLE). |
| 207 // This will DCHECK if the |sql| contains errors. |
207 bool Execute(const char* sql); | 208 bool Execute(const char* sql); |
208 | 209 |
| 210 // Like Execute(), but returns the error code given by SQLite. |
| 211 int ExecuteAndReturnErrorCode(const char* sql); |
| 212 |
209 // Returns true if we have a statement with the given identifier already | 213 // Returns true if we have a statement with the given identifier already |
210 // cached. This is normally not necessary to call, but can be useful if the | 214 // cached. This is normally not necessary to call, but can be useful if the |
211 // caller has to dynamically build up SQL to avoid doing so if it's already | 215 // caller has to dynamically build up SQL to avoid doing so if it's already |
212 // cached. | 216 // cached. |
213 bool HasCachedStatement(const StatementID& id) const; | 217 bool HasCachedStatement(const StatementID& id) const; |
214 | 218 |
215 // Returns a statement for the given SQL using the statement cache. It can | 219 // Returns a statement for the given SQL using the statement cache. It can |
216 // take a nontrivial amount of work to parse and compile a statement, so | 220 // take a nontrivial amount of work to parse and compile a statement, so |
217 // keeping commonly-used ones around for future use is important for | 221 // keeping commonly-used ones around for future use is important for |
218 // performance. | 222 // performance. |
219 // | 223 // |
220 // The SQL may have an error, so the caller must check validity of the | 224 // If the |sql| has an error, an invalid, inert StatementRef is returned (and |
221 // statement before using it. | 225 // the code will crash in debug). The caller must deal with this eventuality, |
| 226 // either by checking validity of the |sql| before calling, by correctly |
| 227 // handling the return of an inert statement, or both. |
222 // | 228 // |
223 // The StatementID and the SQL must always correspond to one-another. The | 229 // The StatementID and the SQL must always correspond to one-another. The |
224 // ID is the lookup into the cache, so crazy things will happen if you use | 230 // ID is the lookup into the cache, so crazy things will happen if you use |
225 // different SQL with the same ID. | 231 // different SQL with the same ID. |
226 // | 232 // |
227 // You will normally use the SQL_FROM_HERE macro to generate a statement | 233 // You will normally use the SQL_FROM_HERE macro to generate a statement |
228 // ID associated with the current line of code. This gives uniqueness without | 234 // ID associated with the current line of code. This gives uniqueness without |
229 // you having to manage unique names. See StatementID above for more. | 235 // you having to manage unique names. See StatementID above for more. |
230 // | 236 // |
231 // Example: | 237 // Example: |
232 // sql::Statement stmt(connection_.GetCachedStatement( | 238 // sql::Statement stmt(connection_.GetCachedStatement( |
233 // SQL_FROM_HERE, "SELECT * FROM foo")); | 239 // SQL_FROM_HERE, "SELECT * FROM foo")); |
234 // if (!stmt) | 240 // if (!stmt) |
235 // return false; // Error creating statement. | 241 // return false; // Error creating statement. |
236 scoped_refptr<StatementRef> GetCachedStatement(const StatementID& id, | 242 scoped_refptr<StatementRef> GetCachedStatement(const StatementID& id, |
237 const char* sql); | 243 const char* sql); |
238 | 244 |
| 245 // Used to check a |sql| statement for syntactic validity. If the statement is |
| 246 // valid SQL, returns true. |
| 247 bool IsSQLValid(const char* sql); |
| 248 |
239 // Returns a non-cached statement for the given SQL. Use this for SQL that | 249 // Returns a non-cached statement for the given SQL. Use this for SQL that |
240 // is only executed once or only rarely (there is overhead associated with | 250 // is only executed once or only rarely (there is overhead associated with |
241 // keeping a statement cached). | 251 // keeping a statement cached). |
242 // | 252 // |
243 // See GetCachedStatement above for examples and error information. | 253 // See GetCachedStatement above for examples and error information. |
244 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql); | 254 scoped_refptr<StatementRef> GetUniqueStatement(const char* sql); |
245 | 255 |
246 // Info querying ------------------------------------------------------------- | 256 // Info querying ------------------------------------------------------------- |
247 | 257 |
248 // Returns true if the given table exists. | 258 // Returns true if the given table exists. |
(...skipping 18 matching lines...) Expand all Loading... |
267 | 277 |
268 // Returns the errno associated with GetErrorCode(). See | 278 // Returns the errno associated with GetErrorCode(). See |
269 // SQLITE_LAST_ERRNO in SQLite documentation. | 279 // SQLITE_LAST_ERRNO in SQLite documentation. |
270 int GetLastErrno() const; | 280 int GetLastErrno() const; |
271 | 281 |
272 // Returns a pointer to a statically allocated string associated with the | 282 // Returns a pointer to a statically allocated string associated with the |
273 // last sqlite operation. | 283 // last sqlite operation. |
274 const char* GetErrorMessage() const; | 284 const char* GetErrorMessage() const; |
275 | 285 |
276 private: | 286 private: |
277 // Statement access StatementRef which we don't want to expose to erverybody | 287 // Statement accesses StatementRef which we don't want to expose to everybody |
278 // (they should go through Statement). | 288 // (they should go through Statement). |
279 friend class Statement; | 289 friend class Statement; |
280 | 290 |
281 // Internal initialize function used by both Init and InitInMemory. The file | 291 // Internal initialize function used by both Init and InitInMemory. The file |
282 // name is always 8 bits since we want to use the 8-bit version of | 292 // name is always 8 bits since we want to use the 8-bit version of |
283 // sqlite3_open. The string can also be sqlite's special ":memory:" string. | 293 // sqlite3_open. The string can also be sqlite's special ":memory:" string. |
284 bool OpenInternal(const std::string& file_name); | 294 bool OpenInternal(const std::string& file_name); |
285 | 295 |
286 // A StatementRef is a refcounted wrapper around a sqlite statement pointer. | 296 // A StatementRef is a refcounted wrapper around a sqlite statement pointer. |
287 // Refcounting allows us to give these statements out to sql::Statement | 297 // Refcounting allows us to give these statements out to sql::Statement |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 // This object handles errors resulting from all forms of executing sqlite | 389 // This object handles errors resulting from all forms of executing sqlite |
380 // commands or statements. It can be null which means default handling. | 390 // commands or statements. It can be null which means default handling. |
381 scoped_refptr<ErrorDelegate> error_delegate_; | 391 scoped_refptr<ErrorDelegate> error_delegate_; |
382 | 392 |
383 DISALLOW_COPY_AND_ASSIGN(Connection); | 393 DISALLOW_COPY_AND_ASSIGN(Connection); |
384 }; | 394 }; |
385 | 395 |
386 } // namespace sql | 396 } // namespace sql |
387 | 397 |
388 #endif // SQL_CONNECTION_H_ | 398 #endif // SQL_CONNECTION_H_ |
OLD | NEW |