[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] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 7 | |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 8 | #include <stdint.h> |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Victor Costan | e56cc68 | 2018-12-27 01:53:46 | [diff] [blame] | 12 | #include "base/component_export.h" |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
Victor Costan | 3a325b81 | 2018-07-23 22:16:18 | [diff] [blame] | 15 | #include "base/sequence_checker.h" |
[email protected] | a4bbc1f9 | 2013-06-11 07:28:19 | [diff] [blame] | 16 | #include "base/strings/string16.h" |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 17 | #include "sql/database.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 18 | |
| 19 | namespace sql { |
| 20 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 21 | // Possible return values from ColumnType in a statement. These should match |
| 22 | // the values in sqlite3.h. |
Victor Costan | 57aecd23 | 2019-04-04 09:09:57 | [diff] [blame] | 23 | enum class ColumnType { |
| 24 | kInteger = 1, |
| 25 | kFloat = 2, |
| 26 | kText = 3, |
| 27 | kBlob = 4, |
| 28 | kNull = 5, |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 29 | }; |
| 30 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 31 | // Normal usage: |
[email protected] | 3273dce | 2010-01-27 16:08:08 | [diff] [blame] | 32 | // sql::Statement s(connection_.GetUniqueStatement(...)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 33 | // s.BindInt(0, a); |
| 34 | // if (s.Step()) |
| 35 | // return s.ColumnString(0); |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 36 | // |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 37 | // If there are errors getting the statement, the statement will be inert; no |
| 38 | // mutating or database-access methods will work. If you need to check for |
| 39 | // validity, use: |
| 40 | // if (!s.is_valid()) |
| 41 | // return false; |
| 42 | // |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 43 | // Step() and Run() just return true to signal success. If you want to handle |
| 44 | // specific errors such as database corruption, install an error handler in |
| 45 | // in the connection object using set_error_delegate(). |
Victor Costan | e56cc68 | 2018-12-27 01:53:46 | [diff] [blame] | 46 | class COMPONENT_EXPORT(SQL) Statement { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 47 | public: |
| 48 | // Creates an uninitialized statement. The statement will be invalid until |
| 49 | // you initialize it via Assign. |
| 50 | Statement(); |
| 51 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 52 | explicit Statement(scoped_refptr<Database::StatementRef> ref); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 53 | ~Statement(); |
| 54 | |
| 55 | // Initializes this object with the given statement, which may or may not |
| 56 | // be valid. Use is_valid() to check if it's OK. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 57 | void Assign(scoped_refptr<Database::StatementRef> ref); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 58 | |
[email protected] | 85fc27b0 | 2012-02-17 02:15:09 | [diff] [blame] | 59 | // Resets the statement to an uninitialized state corrosponding to |
| 60 | // the default constructor, releasing the StatementRef. |
| 61 | void Clear(); |
| 62 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 63 | // Returns true if the statement can be executed. All functions can still |
| 64 | // be used if the statement is invalid, but they will return failure or some |
| 65 | // default value. This is because the statement can become invalid in the |
[email protected] | bed29d94 | 2011-12-22 19:25:51 | [diff] [blame] | 66 | // 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] | 67 | // has to be reset. |
| 68 | bool is_valid() const { return ref_->is_valid(); } |
| 69 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 70 | // Running ------------------------------------------------------------------- |
| 71 | |
| 72 | // Executes the statement, returning true on success. This is like Step but |
| 73 | // for when there is no output, like an INSERT statement. |
| 74 | bool Run(); |
| 75 | |
| 76 | // Executes the statement, returning true if there is a row of data returned. |
| 77 | // You can keep calling Step() until it returns false to iterate through all |
| 78 | // the rows in your result set. |
| 79 | // |
| 80 | // When Step returns false, the result is either that there is no more data |
| 81 | // or there is an error. This makes it most convenient for loop usage. If you |
| 82 | // need to disambiguate these cases, use Succeeded(). |
| 83 | // |
| 84 | // Typical example: |
| 85 | // while (s.Step()) { |
| 86 | // ... |
| 87 | // } |
| 88 | // return s.Succeeded(); |
| 89 | bool Step(); |
| 90 | |
[email protected] | 389e0a4 | 2012-04-25 21:36:41 | [diff] [blame] | 91 | // Resets the statement to its initial condition. This includes any current |
| 92 | // result row, and also the bound variables if the |clear_bound_vars| is true. |
| 93 | void Reset(bool clear_bound_vars); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 94 | |
| 95 | // Returns true if the last executed thing in this statement succeeded. If |
| 96 | // there was no last executed thing or the statement is invalid, this will |
| 97 | // return false. |
| 98 | bool Succeeded() const; |
| 99 | |
| 100 | // Binding ------------------------------------------------------------------- |
| 101 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 102 | // 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] | 103 | // may not always care about the return value (they'll DCHECK if they fail). |
| 104 | // The main thing you may want to check is when binding large blobs or |
| 105 | // strings there may be out of memory. |
| 106 | bool BindNull(int col); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 107 | bool BindBool(int col, bool val); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 108 | bool BindInt(int col, int val); |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 109 | bool BindInt64(int col, int64_t val); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 110 | bool BindDouble(int col, double val); |
| 111 | bool BindCString(int col, const char* val); |
| 112 | bool BindString(int col, const std::string& val); |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 113 | bool BindString16(int col, const base::string16& value); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 114 | bool BindBlob(int col, const void* value, int value_len); |
| 115 | |
| 116 | // Retrieving ---------------------------------------------------------------- |
| 117 | |
| 118 | // Returns the number of output columns in the result. |
| 119 | int ColumnCount() const; |
| 120 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 121 | // Returns the type associated with the given column. |
| 122 | // |
| 123 | // Watch out: the type may be undefined if you've done something to cause a |
| 124 | // "type conversion." This means requesting the value of a column of a type |
| 125 | // where that type is not the native type. For safety, call ColumnType only |
| 126 | // on a column before getting the value out in any way. |
Jose Dapena Paz | e18b4d3 | 2019-04-08 20:59:34 | [diff] [blame] | 127 | ColumnType GetColumnType(int col) const; |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 128 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 129 | // These all take a 0-based argument index. |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 130 | bool ColumnBool(int col) const; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 131 | int ColumnInt(int col) const; |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 132 | int64_t ColumnInt64(int col) const; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 133 | double ColumnDouble(int col) const; |
| 134 | std::string ColumnString(int col) const; |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 135 | base::string16 ColumnString16(int col) const; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 136 | |
| 137 | // When reading a blob, you can get a raw pointer to the underlying data, |
| 138 | // along with the length, or you can just ask us to copy the blob into a |
Victor Costan | bd62311 | 2018-07-18 04:17:27 | [diff] [blame] | 139 | // vector. Danger! ColumnBlob may return nullptr if there is no data! |
[email protected] | 1ed78a3 | 2009-09-15 20:24:17 | [diff] [blame] | 140 | int ColumnByteLength(int col) const; |
| 141 | const void* ColumnBlob(int col) const; |
vabr | b194fc56 | 2016-07-13 08:46:37 | [diff] [blame] | 142 | bool ColumnBlobAsString(int col, std::string* blob) const; |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 143 | bool ColumnBlobAsString16(int col, base::string16* val) const; |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 144 | bool ColumnBlobAsVector(int col, std::vector<char>* val) const; |
| 145 | bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 146 | |
[email protected] | faa604e | 2009-09-25 22:38:59 | [diff] [blame] | 147 | // Diagnostics -------------------------------------------------------------- |
| 148 | |
| 149 | // Returns the original text of sql statement. Do not keep a pointer to it. |
| 150 | const char* GetSQLStatement(); |
| 151 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 152 | private: |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 153 | friend class Database; |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 154 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 155 | // This is intended to check for serious errors and report them to the |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 156 | // Database object. It takes a sqlite error code, and returns the same |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 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 | |
Victor Costan | 5e785e3 | 2019-02-26 20:39:31 | [diff] [blame] | 179 | // Helper for Run() and Step(), calls sqlite3_step() and returns the checked |
| 180 | // value from it. |
| 181 | int StepInternal(); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 182 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 183 | // The actual sqlite statement. This may be unique to us, or it may be cached |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 184 | // by the Database, which is why it's ref-counted. This pointer is |
Victor Costan | bd62311 | 2018-07-18 04:17:27 | [diff] [blame] | 185 | // guaranteed non-null. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 186 | scoped_refptr<Database::StatementRef> ref_; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 187 | |
[email protected] | 097723d | 2013-10-25 20:09:32 | [diff] [blame] | 188 | // Set after Step() or Run() are called, reset by Reset(). Used to |
| 189 | // prevent accidental calls to API functions which would not work |
| 190 | // correctly after stepping has started. |
| 191 | bool stepped_; |
| 192 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 193 | // See Succeeded() for what this holds. |
| 194 | bool succeeded_; |
| 195 | |
| 196 | DISALLOW_COPY_AND_ASSIGN(Statement); |
| 197 | }; |
| 198 | |
| 199 | } // namespace sql |
| 200 | |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 201 | #endif // SQL_STATEMENT_H_ |