blob: b411e809b6ca9cee8f6792cb7135db81f2d6fc92 [file] [log] [blame]
[email protected]98b6f8b12012-02-10 13:31:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f0a54b22011-07-19 18:40:215#ifndef SQL_STATEMENT_H_
6#define SQL_STATEMENT_H_
[email protected]e5ffd0e42009-09-11 21:30:567
tfarina720d4f32015-05-11 22:31:268#include <stdint.h>
[email protected]e5ffd0e42009-09-11 21:30:569#include <string>
10#include <vector>
11
tfarina720d4f32015-05-11 22:31:2612#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]a4bbc1f92013-06-11 07:28:1914#include "base/strings/string16.h"
[email protected]f0a54b22011-07-19 18:40:2115#include "sql/connection.h"
[email protected]d4526962011-11-10 21:40:2816#include "sql/sql_export.h"
[email protected]e5ffd0e42009-09-11 21:30:5617
18namespace sql {
19
[email protected]765b44502009-10-02 05:01:4220// Possible return values from ColumnType in a statement. These should match
21// the values in sqlite3.h.
22enum 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]e5ffd0e42009-09-11 21:30:5630// Normal usage:
[email protected]3273dce2010-01-27 16:08:0831// sql::Statement s(connection_.GetUniqueStatement(...));
[email protected]e5ffd0e42009-09-11 21:30:5632// s.BindInt(0, a);
33// if (s.Step())
34// return s.ColumnString(0);
[email protected]faa604e2009-09-25 22:38:5935//
[email protected]eff1fa522011-12-12 23:50:5936// 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]faa604e2009-09-25 22:38:5942// 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]d4526962011-11-10 21:40:2845class SQL_EXPORT Statement {
[email protected]e5ffd0e42009-09-11 21:30:5646 public:
47 // Creates an uninitialized statement. The statement will be invalid until
48 // you initialize it via Assign.
49 Statement();
50
[email protected]a5b58f52009-11-17 22:15:4451 explicit Statement(scoped_refptr<Connection::StatementRef> ref);
[email protected]e5ffd0e42009-09-11 21:30:5652 ~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
[email protected]85fc27b02012-02-17 02:15:0958 // Resets the statement to an uninitialized state corrosponding to
59 // the default constructor, releasing the StatementRef.
60 void Clear();
61
[email protected]e5ffd0e42009-09-11 21:30:5662 // 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
[email protected]bed29d942011-12-22 19:25:5165 // middle of executing a command if there is a serious error and the database
[email protected]e5ffd0e42009-09-11 21:30:5666 // has to be reset.
67 bool is_valid() const { return ref_->is_valid(); }
68
[email protected]e5ffd0e42009-09-11 21:30:5669 // Running -------------------------------------------------------------------
70
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();
74
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.
78 //
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().
82 //
83 // Typical example:
84 // while (s.Step()) {
85 // ...
86 // }
87 // return s.Succeeded();
88 bool Step();
89
[email protected]389e0a42012-04-25 21:36:4190 // 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);
[email protected]e5ffd0e42009-09-11 21:30:5693
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;
98
99 // Binding -------------------------------------------------------------------
100
[email protected]eff1fa522011-12-12 23:50:59101 // These all take a 0-based argument index and return true on success. You
[email protected]e5ffd0e42009-09-11 21:30:56102 // 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);
[email protected]765b44502009-10-02 05:01:42106 bool BindBool(int col, bool val);
[email protected]e5ffd0e42009-09-11 21:30:56107 bool BindInt(int col, int val);
tfarina720d4f32015-05-11 22:31:26108 bool BindInt64(int col, int64_t val);
[email protected]e5ffd0e42009-09-11 21:30:56109 bool BindDouble(int col, double val);
110 bool BindCString(int col, const char* val);
111 bool BindString(int col, const std::string& val);
[email protected]fcf75d42013-12-03 20:11:26112 bool BindString16(int col, const base::string16& value);
[email protected]e5ffd0e42009-09-11 21:30:56113 bool BindBlob(int col, const void* value, int value_len);
114
115 // Retrieving ----------------------------------------------------------------
116
117 // Returns the number of output columns in the result.
118 int ColumnCount() const;
119
[email protected]765b44502009-10-02 05:01:42120 // Returns the type associated with the given column.
121 //
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;
[email protected]98b6f8b12012-02-10 13:31:59127 ColType DeclaredColumnType(int col) const;
[email protected]765b44502009-10-02 05:01:42128
[email protected]e5ffd0e42009-09-11 21:30:56129 // These all take a 0-based argument index.
[email protected]765b44502009-10-02 05:01:42130 bool ColumnBool(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56131 int ColumnInt(int col) const;
tfarina720d4f32015-05-11 22:31:26132 int64_t ColumnInt64(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56133 double ColumnDouble(int col) const;
134 std::string ColumnString(int col) const;
[email protected]fcf75d42013-12-03 20:11:26135 base::string16 ColumnString16(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56136
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!
[email protected]1ed78a32009-09-15 20:24:17140 int ColumnByteLength(int col) const;
141 const void* ColumnBlob(int col) const;
[email protected]5eea1162010-05-11 17:14:16142 bool ColumnBlobAsString(int col, std::string* blob);
[email protected]fcf75d42013-12-03 20:11:26143 bool ColumnBlobAsString16(int col, base::string16* val) const;
[email protected]eff1fa522011-12-12 23:50:59144 bool ColumnBlobAsVector(int col, std::vector<char>* val) const;
145 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
[email protected]e5ffd0e42009-09-11 21:30:56146
[email protected]faa604e2009-09-25 22:38:59147 // Diagnostics --------------------------------------------------------------
148
149 // Returns the original text of sql statement. Do not keep a pointer to it.
150 const char* GetSQLStatement();
151
[email protected]e5ffd0e42009-09-11 21:30:56152 private:
153 // This is intended to check for serious errors and report them to the
154 // connection object. It takes a sqlite error code, and returns the same
155 // code. Currently this function just updates the succeeded flag, but will be
156 // enhanced in the future to do the notification.
157 int CheckError(int err);
158
[email protected]eff1fa522011-12-12 23:50:59159 // Contraction for checking an error code against SQLITE_OK. Does not set the
160 // succeeded flag.
161 bool CheckOk(int err) const;
162
163 // Should be called by all mutating methods to check that the statement is
164 // valid. Returns true if the statement is valid. DCHECKS and returns false
165 // if it is not.
166 // The reason for this is to handle two specific cases in which a Statement
167 // may be invalid. The first case is that the programmer made an SQL error.
168 // Those cases need to be DCHECKed so that we are guaranteed to find them
169 // before release. The second case is that the computer has an error (probably
170 // out of disk space) which is prohibiting the correct operation of the
171 // database. Our testing apparatus should not exhibit this defect, but release
172 // situations may. Therefore, the code is handling disjoint situations in
173 // release and test. In test, we're ensuring correct SQL. In release, we're
174 // ensuring that contracts are honored in error edge cases.
175 bool CheckValid() const;
176
[email protected]e5ffd0e42009-09-11 21:30:56177 // The actual sqlite statement. This may be unique to us, or it may be cached
178 // by the connection, which is why it's refcounted. This pointer is
179 // guaranteed non-NULL.
180 scoped_refptr<Connection::StatementRef> ref_;
181
[email protected]097723d2013-10-25 20:09:32182 // Set after Step() or Run() are called, reset by Reset(). Used to
183 // prevent accidental calls to API functions which would not work
184 // correctly after stepping has started.
185 bool stepped_;
186
[email protected]e5ffd0e42009-09-11 21:30:56187 // See Succeeded() for what this holds.
188 bool succeeded_;
189
190 DISALLOW_COPY_AND_ASSIGN(Statement);
191};
192
193} // namespace sql
194
[email protected]f0a54b22011-07-19 18:40:21195#endif // SQL_STATEMENT_H_