OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/threading/thread_restrictions.h" |
16 #include "base/time.h" | 17 #include "base/time.h" |
17 #include "sql/sql_export.h" | 18 #include "sql/sql_export.h" |
18 | 19 |
19 class FilePath; | 20 class FilePath; |
20 struct sqlite3; | 21 struct sqlite3; |
21 struct sqlite3_stmt; | 22 struct sqlite3_stmt; |
22 | 23 |
23 namespace sql { | 24 namespace sql { |
24 | 25 |
25 class Statement; | 26 class Statement; |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 private: | 315 private: |
315 // Statement accesses StatementRef which we don't want to expose to everybody | 316 // Statement accesses StatementRef which we don't want to expose to everybody |
316 // (they should go through Statement). | 317 // (they should go through Statement). |
317 friend class Statement; | 318 friend class Statement; |
318 | 319 |
319 // Internal initialize function used by both Init and InitInMemory. The file | 320 // Internal initialize function used by both Init and InitInMemory. The file |
320 // name is always 8 bits since we want to use the 8-bit version of | 321 // name is always 8 bits since we want to use the 8-bit version of |
321 // sqlite3_open. The string can also be sqlite's special ":memory:" string. | 322 // sqlite3_open. The string can also be sqlite's special ":memory:" string. |
322 bool OpenInternal(const std::string& file_name); | 323 bool OpenInternal(const std::string& file_name); |
323 | 324 |
| 325 // Check whether the current thread is allowed to make IO calls, but only |
| 326 // if database wasn't open in memory. Function is inlined to be a no-op in |
| 327 // official build. |
| 328 void AssertIOAllowed() { |
| 329 if (!in_memory_) |
| 330 base::ThreadRestrictions::AssertIOAllowed(); |
| 331 } |
| 332 |
324 // Internal helper for DoesTableExist and DoesIndexExist. | 333 // Internal helper for DoesTableExist and DoesIndexExist. |
325 bool DoesTableOrIndexExist(const char* name, const char* type) const; | 334 bool DoesTableOrIndexExist(const char* name, const char* type) const; |
326 | 335 |
327 // A StatementRef is a refcounted wrapper around a sqlite statement pointer. | 336 // A StatementRef is a refcounted wrapper around a sqlite statement pointer. |
328 // Refcounting allows us to give these statements out to sql::Statement | 337 // Refcounting allows us to give these statements out to sql::Statement |
329 // objects while also optionally maintaining a cache of compiled statements | 338 // objects while also optionally maintaining a cache of compiled statements |
330 // by just keeping a refptr to these objects. | 339 // by just keeping a refptr to these objects. |
331 // | 340 // |
332 // A statement ref can be valid, in which case it can be used, or invalid to | 341 // A statement ref can be valid, in which case it can be used, or invalid to |
333 // indicate that the statement hasn't been created yet, has an error, or has | 342 // indicate that the statement hasn't been created yet, has an error, or has |
(...skipping 15 matching lines...) Expand all Loading... |
349 Connection* connection() const { return connection_; } | 358 Connection* connection() const { return connection_; } |
350 | 359 |
351 // Returns the sqlite statement if any. If the statement is not active, | 360 // Returns the sqlite statement if any. If the statement is not active, |
352 // this will return NULL. | 361 // this will return NULL. |
353 sqlite3_stmt* stmt() const { return stmt_; } | 362 sqlite3_stmt* stmt() const { return stmt_; } |
354 | 363 |
355 // Destroys the compiled statement and marks it NULL. The statement will | 364 // Destroys the compiled statement and marks it NULL. The statement will |
356 // no longer be active. | 365 // no longer be active. |
357 void Close(); | 366 void Close(); |
358 | 367 |
| 368 // Check whether the current thread is allowed to make IO calls, but only |
| 369 // if database wasn't open in memory. |
| 370 void AssertIOAllowed() { if (connection_) connection_->AssertIOAllowed(); } |
| 371 |
359 private: | 372 private: |
360 friend class base::RefCounted<StatementRef>; | 373 friend class base::RefCounted<StatementRef>; |
361 | 374 |
362 ~StatementRef(); | 375 ~StatementRef(); |
363 | 376 |
364 Connection* connection_; | 377 Connection* connection_; |
365 sqlite3_stmt* stmt_; | 378 sqlite3_stmt* stmt_; |
366 | 379 |
367 DISALLOW_COPY_AND_ASSIGN(StatementRef); | 380 DISALLOW_COPY_AND_ASSIGN(StatementRef); |
368 }; | 381 }; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 StatementRefSet open_statements_; | 424 StatementRefSet open_statements_; |
412 | 425 |
413 // Number of currently-nested transactions. | 426 // Number of currently-nested transactions. |
414 int transaction_nesting_; | 427 int transaction_nesting_; |
415 | 428 |
416 // True if any of the currently nested transactions have been rolled back. | 429 // True if any of the currently nested transactions have been rolled back. |
417 // When we get to the outermost transaction, this will determine if we do | 430 // When we get to the outermost transaction, this will determine if we do |
418 // a rollback instead of a commit. | 431 // a rollback instead of a commit. |
419 bool needs_rollback_; | 432 bool needs_rollback_; |
420 | 433 |
| 434 // True if database is open with OpenInMemory(), False if database is open |
| 435 // with Open(). |
| 436 bool in_memory_; |
| 437 |
421 // This object handles errors resulting from all forms of executing sqlite | 438 // This object handles errors resulting from all forms of executing sqlite |
422 // commands or statements. It can be null which means default handling. | 439 // commands or statements. It can be null which means default handling. |
423 scoped_refptr<ErrorDelegate> error_delegate_; | 440 scoped_refptr<ErrorDelegate> error_delegate_; |
424 | 441 |
425 DISALLOW_COPY_AND_ASSIGN(Connection); | 442 DISALLOW_COPY_AND_ASSIGN(Connection); |
426 }; | 443 }; |
427 | 444 |
428 } // namespace sql | 445 } // namespace sql |
429 | 446 |
430 #endif // SQL_CONNECTION_H_ | 447 #endif // SQL_CONNECTION_H_ |
OLD | NEW |