| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 APP_SQL_STATEMENT_H_ | 5 #ifndef APP_SQL_STATEMENT_H_ |
| 6 #define APP_SQL_STATEMENT_H_ | 6 #define APP_SQL_STATEMENT_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "app/sql/connection.h" | 11 #include "app/sql/connection.h" |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
| 14 | 14 |
| 15 namespace sql { | 15 namespace sql { |
| 16 | 16 |
| 17 // Normal usage: | 17 // Normal usage: |
| 18 // sql::Statement s = connection_.GetUniqueStatement(...); | 18 // sql::Statement s = connection_.GetUniqueStatement(...); |
| 19 // if (!s) // You should check for errors before using the statement. | 19 // if (!s) // You should check for errors before using the statement. |
| 20 // return false; | 20 // return false; |
| 21 // | 21 // |
| 22 // s.BindInt(0, a); | 22 // s.BindInt(0, a); |
| 23 // if (s.Step()) | 23 // if (s.Step()) |
| 24 // return s.ColumnString(0); | 24 // return s.ColumnString(0); |
| 25 // |
| 26 // Step() and Run() just return true to signal success. If you want to handle |
| 27 // specific errors such as database corruption, install an error handler in |
| 28 // in the connection object using set_error_delegate(). |
| 25 class Statement { | 29 class Statement { |
| 26 public: | 30 public: |
| 27 // Creates an uninitialized statement. The statement will be invalid until | 31 // Creates an uninitialized statement. The statement will be invalid until |
| 28 // you initialize it via Assign. | 32 // you initialize it via Assign. |
| 29 Statement(); | 33 Statement(); |
| 30 | 34 |
| 31 Statement(scoped_refptr<Connection::StatementRef> ref); | 35 Statement(scoped_refptr<Connection::StatementRef> ref); |
| 32 ~Statement(); | 36 ~Statement(); |
| 33 | 37 |
| 34 // Initializes this object with the given statement, which may or may not | 38 // Initializes this object with the given statement, which may or may not |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 std::string ColumnString(int col) const; | 107 std::string ColumnString(int col) const; |
| 104 | 108 |
| 105 // When reading a blob, you can get a raw pointer to the underlying data, | 109 // When reading a blob, you can get a raw pointer to the underlying data, |
| 106 // along with the length, or you can just ask us to copy the blob into a | 110 // along with the length, or you can just ask us to copy the blob into a |
| 107 // vector. Danger! ColumnBlob may return NULL if there is no data! | 111 // vector. Danger! ColumnBlob may return NULL if there is no data! |
| 108 int ColumnByteLength(int col) const; | 112 int ColumnByteLength(int col) const; |
| 109 const void* ColumnBlob(int col) const; | 113 const void* ColumnBlob(int col) const; |
| 110 void ColumnBlobAsVector(int col, std::vector<char>* val) const; | 114 void ColumnBlobAsVector(int col, std::vector<char>* val) const; |
| 111 void ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const; | 115 void ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const; |
| 112 | 116 |
| 117 // Diagnostics -------------------------------------------------------------- |
| 118 |
| 119 // Returns the original text of sql statement. Do not keep a pointer to it. |
| 120 const char* GetSQLStatement(); |
| 121 |
| 113 private: | 122 private: |
| 114 // This is intended to check for serious errors and report them to the | 123 // This is intended to check for serious errors and report them to the |
| 115 // connection object. It takes a sqlite error code, and returns the same | 124 // connection object. It takes a sqlite error code, and returns the same |
| 116 // code. Currently this function just updates the succeeded flag, but will be | 125 // code. Currently this function just updates the succeeded flag, but will be |
| 117 // enhanced in the future to do the notification. | 126 // enhanced in the future to do the notification. |
| 118 int CheckError(int err); | 127 int CheckError(int err); |
| 119 | 128 |
| 120 // The actual sqlite statement. This may be unique to us, or it may be cached | 129 // The actual sqlite statement. This may be unique to us, or it may be cached |
| 121 // by the connection, which is why it's refcounted. This pointer is | 130 // by the connection, which is why it's refcounted. This pointer is |
| 122 // guaranteed non-NULL. | 131 // guaranteed non-NULL. |
| 123 scoped_refptr<Connection::StatementRef> ref_; | 132 scoped_refptr<Connection::StatementRef> ref_; |
| 124 | 133 |
| 125 // See Succeeded() for what this holds. | 134 // See Succeeded() for what this holds. |
| 126 bool succeeded_; | 135 bool succeeded_; |
| 127 | 136 |
| 128 DISALLOW_COPY_AND_ASSIGN(Statement); | 137 DISALLOW_COPY_AND_ASSIGN(Statement); |
| 129 }; | 138 }; |
| 130 | 139 |
| 131 } // namespace sql | 140 } // namespace sql |
| 132 | 141 |
| 133 #endif // APP_SQL_STATEMENT_H_ | 142 #endif // APP_SQL_STATEMENT_H_ |
| OLD | NEW |