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
3 // found in the LICENSE file.
5 #ifndef SQL_STATEMENT_H_
6 #define SQL_STATEMENT_H_
12 #include "base/component_export.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/sequence_checker.h"
16 #include "base/strings/string16.h"
17 #include "sql/database.h"
21 // Possible return values from ColumnType in a statement. These should match
22 // the values in sqlite3.h.
23 enum class ColumnType {
32 // sql::Statement s(connection_.GetUniqueStatement(...));
35 // return s.ColumnString(0);
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
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().
46 class COMPONENT_EXPORT(SQL) Statement {
48 // Creates an uninitialized statement. The statement will be invalid until
49 // you initialize it via Assign.
52 explicit Statement(scoped_refptr<Database::StatementRef> ref);
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.
57 void Assign(scoped_refptr<Database::StatementRef> ref);
59 // Resets the statement to an uninitialized state corresponding to
60 // the default constructor, releasing the StatementRef.
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
66 // middle of executing a command if there is a serious error and the database
68 bool is_valid() const { return ref_->is_valid(); }
70 // Running -------------------------------------------------------------------
72 // Executes the statement, returning true on success. This is like Step but
73 // for when there is no output, like an INSERT statement.
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.
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().
88 // return s.Succeeded();
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);
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
98 bool Succeeded() const;
100 // Binding -------------------------------------------------------------------
102 // These all take a 0-based argument index and return true on success. You
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);
107 bool BindBool(int col, bool val);
108 bool BindInt(int col, int val);
109 bool BindInt(int col, int64_t val) = delete; // Call BindInt64() instead.
110 bool BindInt64(int col, int64_t 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);
114 bool BindString16(int col, const base::string16& value);
115 bool BindBlob(int col, const void* value, int value_len);
117 // Retrieving ----------------------------------------------------------------
119 // Returns the number of output columns in the result.
120 int ColumnCount() const;
122 // Returns the type associated with the given column.
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 ColumnType GetColumnType(int col) const;
130 // These all take a 0-based argument index.
131 bool ColumnBool(int col) const;
132 int ColumnInt(int col) const;
133 int64_t ColumnInt64(int col) const;
134 double ColumnDouble(int col) const;
135 std::string ColumnString(int col) const;
136 base::string16 ColumnString16(int col) const;
138 // When reading a blob, you can get a raw pointer to the underlying data,
139 // along with the length, or you can just ask us to copy the blob into a
140 // vector. Danger! ColumnBlob may return nullptr if there is no data!
141 int ColumnByteLength(int col) const;
142 const void* ColumnBlob(int col) const;
143 bool ColumnBlobAsString(int col, std::string* blob) const;
144 bool ColumnBlobAsString16(int col, base::string16* val) const;
145 bool ColumnBlobAsVector(int col, std::vector<char>* val) const;
146 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
148 // Diagnostics --------------------------------------------------------------
150 // Returns the original text of sql statement. Do not keep a pointer to it.
151 const char* GetSQLStatement();
154 friend class Database;
156 // This is intended to check for serious errors and report them to the
157 // Database object. It takes a sqlite error code, and returns the same
158 // code. Currently this function just updates the succeeded flag, but will be
159 // enhanced in the future to do the notification.
160 int CheckError(int err);
162 // Contraction for checking an error code against SQLITE_OK. Does not set the
164 bool CheckOk(int err) const;
166 // Should be called by all mutating methods to check that the statement is
167 // valid. Returns true if the statement is valid. DCHECKS and returns false
169 // The reason for this is to handle two specific cases in which a Statement
170 // may be invalid. The first case is that the programmer made an SQL error.
171 // Those cases need to be DCHECKed so that we are guaranteed to find them
172 // before release. The second case is that the computer has an error (probably
173 // out of disk space) which is prohibiting the correct operation of the
174 // database. Our testing apparatus should not exhibit this defect, but release
175 // situations may. Therefore, the code is handling disjoint situations in
176 // release and test. In test, we're ensuring correct SQL. In release, we're
177 // ensuring that contracts are honored in error edge cases.
178 bool CheckValid() const;
180 // Helper for Run() and Step(), calls sqlite3_step() and returns the checked
184 // The actual sqlite statement. This may be unique to us, or it may be cached
185 // by the Database, which is why it's ref-counted. This pointer is
186 // guaranteed non-null.
187 scoped_refptr<Database::StatementRef> ref_;
189 // Set after Step() or Run() are called, reset by Reset(). Used to
190 // prevent accidental calls to API functions which would not work
191 // correctly after stepping has started.
194 // See Succeeded() for what this holds.
197 DISALLOW_COPY_AND_ASSIGN(Statement);
202 #endif // SQL_STATEMENT_H_