aura: Remove more code related to layerless windows.
[chromium-blink-merge.git] / sql / statement.h
blob83a5a2dfa83aeabc30460886bcd5d146a2067fe2
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_
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/strings/string16.h"
15 #include "sql/connection.h"
16 #include "sql/sql_export.h"
18 namespace sql {
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,
30 // Normal usage:
31 // sql::Statement s(connection_.GetUniqueStatement(...));
32 // s.BindInt(0, a);
33 // if (s.Step())
34 // return s.ColumnString(0);
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;
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().
45 class SQL_EXPORT Statement {
46 public:
47 // Creates an uninitialized statement. The statement will be invalid until
48 // you initialize it via Assign.
49 Statement();
51 explicit Statement(scoped_refptr<Connection::StatementRef> ref);
52 ~Statement();
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);
58 // Resets the statement to an uninitialized state corrosponding to
59 // the default constructor, releasing the StatementRef.
60 void Clear();
62 // Returns true if the statement can be executed. All functions can still
63 // be used if the statement is invalid, but they will return failure or some
64 // default value. This is because the statement can become invalid in the
65 // middle of executing a command if there is a serious error and the database
66 // has to be reset.
67 bool is_valid() const { return ref_->is_valid(); }
69 // Running -------------------------------------------------------------------
71 // Executes the statement, returning true on success. This is like Step but
72 // for when there is no output, like an INSERT statement.
73 bool Run();
75 // Executes the statement, returning true if there is a row of data returned.
76 // You can keep calling Step() until it returns false to iterate through all
77 // the rows in your result set.
79 // When Step returns false, the result is either that there is no more data
80 // or there is an error. This makes it most convenient for loop usage. If you
81 // need to disambiguate these cases, use Succeeded().
83 // Typical example:
84 // while (s.Step()) {
85 // ...
86 // }
87 // return s.Succeeded();
88 bool Step();
90 // Resets the statement to its initial condition. This includes any current
91 // result row, and also the bound variables if the |clear_bound_vars| is true.
92 void Reset(bool clear_bound_vars);
94 // Returns true if the last executed thing in this statement succeeded. If
95 // there was no last executed thing or the statement is invalid, this will
96 // return false.
97 bool Succeeded() const;
99 // Binding -------------------------------------------------------------------
101 // These all take a 0-based argument index and return true on success. You
102 // may not always care about the return value (they'll DCHECK if they fail).
103 // The main thing you may want to check is when binding large blobs or
104 // strings there may be out of memory.
105 bool BindNull(int col);
106 bool BindBool(int col, bool val);
107 bool BindInt(int col, int val);
108 bool BindInt64(int col, int64_t val);
109 bool BindDouble(int col, double val);
110 bool BindCString(int col, const char* val);
111 bool BindString(int col, const std::string& val);
112 bool BindString16(int col, const base::string16& value);
113 bool BindBlob(int col, const void* value, int value_len);
115 // Retrieving ----------------------------------------------------------------
117 // Returns the number of output columns in the result.
118 int ColumnCount() const;
120 // Returns the type associated with the given column.
122 // Watch out: the type may be undefined if you've done something to cause a
123 // "type conversion." This means requesting the value of a column of a type
124 // where that type is not the native type. For safety, call ColumnType only
125 // on a column before getting the value out in any way.
126 ColType ColumnType(int col) const;
127 ColType DeclaredColumnType(int col) const;
129 // These all take a 0-based argument index.
130 bool ColumnBool(int col) const;
131 int ColumnInt(int col) const;
132 int64_t ColumnInt64(int col) const;
133 double ColumnDouble(int col) const;
134 std::string ColumnString(int col) const;
135 base::string16 ColumnString16(int col) const;
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
139 // vector. Danger! ColumnBlob may return NULL if there is no data!
140 int ColumnByteLength(int col) const;
141 const void* ColumnBlob(int col) const;
142 bool ColumnBlobAsString(int col, std::string* blob);
143 bool ColumnBlobAsString16(int col, base::string16* val) const;
144 bool ColumnBlobAsVector(int col, std::vector<char>* val) const;
145 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
147 // Diagnostics --------------------------------------------------------------
149 // Returns the original text of sql statement. Do not keep a pointer to it.
150 const char* GetSQLStatement();
152 private:
153 friend class Connection;
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);
161 // Contraction for checking an error code against SQLITE_OK. Does not set the
162 // succeeded flag.
163 bool CheckOk(int err) const;
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;
179 // Helper for Run() and Step(), calls sqlite3_step() and then generates
180 // sql::Connection histograms based on the results. Timing and change count
181 // are only recorded if |timer_flag| is true. The checked value from
182 // sqlite3_step() is returned.
183 int StepInternal(bool timer_flag);
185 // sql::Connection uses cached statments for transactions, but tracks their
186 // runtime independently.
187 bool RunWithoutTimers();
189 // The actual sqlite statement. This may be unique to us, or it may be cached
190 // by the connection, which is why it's refcounted. This pointer is
191 // guaranteed non-NULL.
192 scoped_refptr<Connection::StatementRef> ref_;
194 // Set after Step() or Run() are called, reset by Reset(). Used to
195 // prevent accidental calls to API functions which would not work
196 // correctly after stepping has started.
197 bool stepped_;
199 // See Succeeded() for what this holds.
200 bool succeeded_;
202 DISALLOW_COPY_AND_ASSIGN(Statement);
205 } // namespace sql
207 #endif // SQL_STATEMENT_H_