[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 5 | #ifndef SQL_STATEMENT_H_ |
| 6 | #define SQL_STATEMENT_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 12 | #include "base/basictypes.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 5eea116 | 2010-05-11 17:14:16 | [diff] [blame] | 14 | #include "base/string16.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 15 | #include "sql/connection.h" |
[email protected] | d452696 | 2011-11-10 21:40:28 | [diff] [blame] | 16 | #include "sql/sql_export.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 17 | |
| 18 | namespace sql { |
| 19 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 20 | // Possible return values from ColumnType in a statement. These should match |
| 21 | // the values in sqlite3.h. |
| 22 | enum ColType { |
| 23 | COLUMN_TYPE_INTEGER = 1, |
| 24 | COLUMN_TYPE_FLOAT = 2, |
| 25 | COLUMN_TYPE_TEXT = 3, |
| 26 | COLUMN_TYPE_BLOB = 4, |
| 27 | COLUMN_TYPE_NULL = 5, |
| 28 | }; |
| 29 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 30 | // Normal usage: |
[email protected] | 3273dce | 2010-01-27 16:08:08 | [diff] [blame] | 31 | // sql::Statement s(connection_.GetUniqueStatement(...)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 32 | // s.BindInt(0, a); |
| 33 | // if (s.Step()) |
| 34 | // return s.ColumnString(0); |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 35 | // |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 36 | // If there are errors getting the statement, the statement will be inert; no |
| 37 | // mutating or database-access methods will work. If you need to check for |
| 38 | // validity, use: |
| 39 | // if (!s.is_valid()) |
| 40 | // return false; |
| 41 | // |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 42 | // Step() and Run() just return true to signal success. If you want to handle |
| 43 | // specific errors such as database corruption, install an error handler in |
| 44 | // in the connection object using set_error_delegate(). |
[email protected] | d452696 | 2011-11-10 21:40:28 | [diff] [blame] | 45 | class SQL_EXPORT Statement { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 46 | public: |
| 47 | // Creates an uninitialized statement. The statement will be invalid until |
| 48 | // you initialize it via Assign. |
| 49 | Statement(); |
| 50 | |
[email protected] | a5b58f5 | 2009-11-17 22:15:44 | [diff] [blame] | 51 | explicit Statement(scoped_refptr<Connection::StatementRef> ref); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 52 | ~Statement(); |
| 53 | |
| 54 | // Initializes this object with the given statement, which may or may not |
| 55 | // be valid. Use is_valid() to check if it's OK. |
| 56 | void Assign(scoped_refptr<Connection::StatementRef> ref); |
| 57 | |
| 58 | // Returns true if the statement can be executed. All functions can still |
| 59 | // be used if the statement is invalid, but they will return failure or some |
| 60 | // default value. This is because the statement can become invalid in the |
[email protected] | bed29d94 | 2011-12-22 19:25:51 | [diff] [blame] | 61 | // middle of executing a command if there is a serious error and the database |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 62 | // has to be reset. |
| 63 | bool is_valid() const { return ref_->is_valid(); } |
| 64 | |
| 65 | // These operators allow conveniently checking if the statement is valid |
| 66 | // or not. See the pattern above for an example. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 67 | // TODO(shess,gbillock): Remove these once clients are converted. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 68 | operator bool() const { return is_valid(); } |
| 69 | bool operator!() const { return !is_valid(); } |
| 70 | |
| 71 | // Running ------------------------------------------------------------------- |
| 72 | |
| 73 | // Executes the statement, returning true on success. This is like Step but |
| 74 | // for when there is no output, like an INSERT statement. |
| 75 | bool Run(); |
| 76 | |
| 77 | // Executes the statement, returning true if there is a row of data returned. |
| 78 | // You can keep calling Step() until it returns false to iterate through all |
| 79 | // the rows in your result set. |
| 80 | // |
| 81 | // When Step returns false, the result is either that there is no more data |
| 82 | // or there is an error. This makes it most convenient for loop usage. If you |
| 83 | // need to disambiguate these cases, use Succeeded(). |
| 84 | // |
| 85 | // Typical example: |
| 86 | // while (s.Step()) { |
| 87 | // ... |
| 88 | // } |
| 89 | // return s.Succeeded(); |
| 90 | bool Step(); |
| 91 | |
| 92 | // Resets the statement to its initial condition. This includes clearing all |
| 93 | // the bound variables and any current result row. |
| 94 | void Reset(); |
| 95 | |
| 96 | // Returns true if the last executed thing in this statement succeeded. If |
| 97 | // there was no last executed thing or the statement is invalid, this will |
| 98 | // return false. |
| 99 | bool Succeeded() const; |
| 100 | |
| 101 | // Binding ------------------------------------------------------------------- |
| 102 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 103 | // These all take a 0-based argument index and return true on success. You |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 104 | // may not always care about the return value (they'll DCHECK if they fail). |
| 105 | // The main thing you may want to check is when binding large blobs or |
| 106 | // strings there may be out of memory. |
| 107 | bool BindNull(int col); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 108 | bool BindBool(int col, bool val); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 109 | bool BindInt(int col, int val); |
| 110 | bool BindInt64(int col, int64 val); |
| 111 | bool BindDouble(int col, double val); |
| 112 | bool BindCString(int col, const char* val); |
| 113 | bool BindString(int col, const std::string& val); |
[email protected] | 5eea116 | 2010-05-11 17:14:16 | [diff] [blame] | 114 | bool BindString16(int col, const string16& value); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 115 | bool BindBlob(int col, const void* value, int value_len); |
| 116 | |
| 117 | // Retrieving ---------------------------------------------------------------- |
| 118 | |
| 119 | // Returns the number of output columns in the result. |
| 120 | int ColumnCount() const; |
| 121 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 122 | // Returns the type associated with the given column. |
| 123 | // |
| 124 | // Watch out: the type may be undefined if you've done something to cause a |
| 125 | // "type conversion." This means requesting the value of a column of a type |
| 126 | // where that type is not the native type. For safety, call ColumnType only |
| 127 | // on a column before getting the value out in any way. |
| 128 | ColType ColumnType(int col) const; |
[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame^] | 129 | ColType DeclaredColumnType(int col) const; |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 130 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 131 | // These all take a 0-based argument index. |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 132 | bool ColumnBool(int col) const; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 133 | int ColumnInt(int col) const; |
| 134 | int64 ColumnInt64(int col) const; |
| 135 | double ColumnDouble(int col) const; |
| 136 | std::string ColumnString(int col) const; |
[email protected] | 5eea116 | 2010-05-11 17:14:16 | [diff] [blame] | 137 | string16 ColumnString16(int col) const; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 138 | |
| 139 | // When reading a blob, you can get a raw pointer to the underlying data, |
| 140 | // along with the length, or you can just ask us to copy the blob into a |
| 141 | // vector. Danger! ColumnBlob may return NULL if there is no data! |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 142 | int ColumnByteLength(int col) const; |
| 143 | const void* ColumnBlob(int col) const; |
[email protected] | 5eea116 | 2010-05-11 17:14:16 | [diff] [blame] | 144 | bool ColumnBlobAsString(int col, std::string* blob); |
[email protected] | 98b6f8b1 | 2012-02-10 13:31:59 | [diff] [blame^] | 145 | bool ColumnBlobAsString16(int col, string16* val) const; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 146 | bool ColumnBlobAsVector(int col, std::vector<char>* val) const; |
| 147 | bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 148 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 149 | // Diagnostics -------------------------------------------------------------- |
| 150 | |
| 151 | // Returns the original text of sql statement. Do not keep a pointer to it. |
| 152 | const char* GetSQLStatement(); |
| 153 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 154 | private: |
| 155 | // This is intended to check for serious errors and report them to the |
| 156 | // connection object. It takes a sqlite error code, and returns the same |
| 157 | // code. Currently this function just updates the succeeded flag, but will be |
| 158 | // enhanced in the future to do the notification. |
| 159 | int CheckError(int err); |
| 160 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 161 | // Contraction for checking an error code against SQLITE_OK. Does not set the |
| 162 | // succeeded flag. |
| 163 | bool CheckOk(int err) const; |
| 164 | |
| 165 | // Should be called by all mutating methods to check that the statement is |
| 166 | // valid. Returns true if the statement is valid. DCHECKS and returns false |
| 167 | // if it is not. |
| 168 | // The reason for this is to handle two specific cases in which a Statement |
| 169 | // may be invalid. The first case is that the programmer made an SQL error. |
| 170 | // Those cases need to be DCHECKed so that we are guaranteed to find them |
| 171 | // before release. The second case is that the computer has an error (probably |
| 172 | // out of disk space) which is prohibiting the correct operation of the |
| 173 | // database. Our testing apparatus should not exhibit this defect, but release |
| 174 | // situations may. Therefore, the code is handling disjoint situations in |
| 175 | // release and test. In test, we're ensuring correct SQL. In release, we're |
| 176 | // ensuring that contracts are honored in error edge cases. |
| 177 | bool CheckValid() const; |
| 178 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 179 | // The actual sqlite statement. This may be unique to us, or it may be cached |
| 180 | // by the connection, which is why it's refcounted. This pointer is |
| 181 | // guaranteed non-NULL. |
| 182 | scoped_refptr<Connection::StatementRef> ref_; |
| 183 | |
| 184 | // See Succeeded() for what this holds. |
| 185 | bool succeeded_; |
| 186 | |
| 187 | DISALLOW_COPY_AND_ASSIGN(Statement); |
| 188 | }; |
| 189 | |
| 190 | } // namespace sql |
| 191 | |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 192 | #endif // SQL_STATEMENT_H_ |