blob: e192e60725192b2f44b174c48c26911898b73877 [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
8#include <string>
9#include <vector>
10
[email protected]e5ffd0e42009-09-11 21:30:5611#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
[email protected]a4bbc1f92013-06-11 07:28:1913#include "base/strings/string16.h"
[email protected]f0a54b22011-07-19 18:40:2114#include "sql/connection.h"
[email protected]d4526962011-11-10 21:40:2815#include "sql/sql_export.h"
[email protected]e5ffd0e42009-09-11 21:30:5616
17namespace sql {
18
[email protected]765b44502009-10-02 05:01:4219// Possible return values from ColumnType in a statement. These should match
20// the values in sqlite3.h.
21enum ColType {
22 COLUMN_TYPE_INTEGER = 1,
23 COLUMN_TYPE_FLOAT = 2,
24 COLUMN_TYPE_TEXT = 3,
25 COLUMN_TYPE_BLOB = 4,
26 COLUMN_TYPE_NULL = 5,
27};
28
[email protected]e5ffd0e42009-09-11 21:30:5629// Normal usage:
[email protected]3273dce2010-01-27 16:08:0830// sql::Statement s(connection_.GetUniqueStatement(...));
[email protected]e5ffd0e42009-09-11 21:30:5631// s.BindInt(0, a);
32// if (s.Step())
33// return s.ColumnString(0);
[email protected]faa604e2009-09-25 22:38:5934//
[email protected]eff1fa522011-12-12 23:50:5935// If there are errors getting the statement, the statement will be inert; no
36// mutating or database-access methods will work. If you need to check for
37// validity, use:
38// if (!s.is_valid())
39// return false;
40//
[email protected]faa604e2009-09-25 22:38:5941// Step() and Run() just return true to signal success. If you want to handle
42// specific errors such as database corruption, install an error handler in
43// in the connection object using set_error_delegate().
[email protected]d4526962011-11-10 21:40:2844class SQL_EXPORT Statement {
[email protected]e5ffd0e42009-09-11 21:30:5645 public:
46 // Creates an uninitialized statement. The statement will be invalid until
47 // you initialize it via Assign.
48 Statement();
49
[email protected]a5b58f52009-11-17 22:15:4450 explicit Statement(scoped_refptr<Connection::StatementRef> ref);
[email protected]e5ffd0e42009-09-11 21:30:5651 ~Statement();
52
53 // Initializes this object with the given statement, which may or may not
54 // be valid. Use is_valid() to check if it's OK.
55 void Assign(scoped_refptr<Connection::StatementRef> ref);
56
[email protected]85fc27b02012-02-17 02:15:0957 // Resets the statement to an uninitialized state corrosponding to
58 // the default constructor, releasing the StatementRef.
59 void Clear();
60
[email protected]e5ffd0e42009-09-11 21:30:5661 // Returns true if the statement can be executed. All functions can still
62 // be used if the statement is invalid, but they will return failure or some
63 // default value. This is because the statement can become invalid in the
[email protected]bed29d942011-12-22 19:25:5164 // middle of executing a command if there is a serious error and the database
[email protected]e5ffd0e42009-09-11 21:30:5665 // has to be reset.
66 bool is_valid() const { return ref_->is_valid(); }
67
[email protected]e5ffd0e42009-09-11 21:30:5668 // Running -------------------------------------------------------------------
69
70 // Executes the statement, returning true on success. This is like Step but
71 // for when there is no output, like an INSERT statement.
72 bool Run();
73
74 // Executes the statement, returning true if there is a row of data returned.
75 // You can keep calling Step() until it returns false to iterate through all
76 // the rows in your result set.
77 //
78 // When Step returns false, the result is either that there is no more data
79 // or there is an error. This makes it most convenient for loop usage. If you
80 // need to disambiguate these cases, use Succeeded().
81 //
82 // Typical example:
83 // while (s.Step()) {
84 // ...
85 // }
86 // return s.Succeeded();
87 bool Step();
88
[email protected]389e0a42012-04-25 21:36:4189 // Resets the statement to its initial condition. This includes any current
90 // result row, and also the bound variables if the |clear_bound_vars| is true.
91 void Reset(bool clear_bound_vars);
[email protected]e5ffd0e42009-09-11 21:30:5692
93 // Returns true if the last executed thing in this statement succeeded. If
94 // there was no last executed thing or the statement is invalid, this will
95 // return false.
96 bool Succeeded() const;
97
98 // Binding -------------------------------------------------------------------
99
[email protected]eff1fa522011-12-12 23:50:59100 // These all take a 0-based argument index and return true on success. You
[email protected]e5ffd0e42009-09-11 21:30:56101 // may not always care about the return value (they'll DCHECK if they fail).
102 // The main thing you may want to check is when binding large blobs or
103 // strings there may be out of memory.
104 bool BindNull(int col);
[email protected]765b44502009-10-02 05:01:42105 bool BindBool(int col, bool val);
[email protected]e5ffd0e42009-09-11 21:30:56106 bool BindInt(int col, int val);
107 bool BindInt64(int col, int64 val);
108 bool BindDouble(int col, double val);
109 bool BindCString(int col, const char* val);
110 bool BindString(int col, const std::string& val);
[email protected]fcf75d42013-12-03 20:11:26111 bool BindString16(int col, const base::string16& value);
[email protected]e5ffd0e42009-09-11 21:30:56112 bool BindBlob(int col, const void* value, int value_len);
113
114 // Retrieving ----------------------------------------------------------------
115
116 // Returns the number of output columns in the result.
117 int ColumnCount() const;
118
[email protected]765b44502009-10-02 05:01:42119 // Returns the type associated with the given column.
120 //
121 // Watch out: the type may be undefined if you've done something to cause a
122 // "type conversion." This means requesting the value of a column of a type
123 // where that type is not the native type. For safety, call ColumnType only
124 // on a column before getting the value out in any way.
125 ColType ColumnType(int col) const;
[email protected]98b6f8b12012-02-10 13:31:59126 ColType DeclaredColumnType(int col) const;
[email protected]765b44502009-10-02 05:01:42127
[email protected]e5ffd0e42009-09-11 21:30:56128 // These all take a 0-based argument index.
[email protected]765b44502009-10-02 05:01:42129 bool ColumnBool(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56130 int ColumnInt(int col) const;
131 int64 ColumnInt64(int col) const;
132 double ColumnDouble(int col) const;
133 std::string ColumnString(int col) const;
[email protected]fcf75d42013-12-03 20:11:26134 base::string16 ColumnString16(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56135
136 // When reading a blob, you can get a raw pointer to the underlying data,
137 // along with the length, or you can just ask us to copy the blob into a
138 // vector. Danger! ColumnBlob may return NULL if there is no data!
[email protected]1ed78a32009-09-15 20:24:17139 int ColumnByteLength(int col) const;
140 const void* ColumnBlob(int col) const;
[email protected]5eea1162010-05-11 17:14:16141 bool ColumnBlobAsString(int col, std::string* blob);
[email protected]fcf75d42013-12-03 20:11:26142 bool ColumnBlobAsString16(int col, base::string16* val) const;
[email protected]eff1fa522011-12-12 23:50:59143 bool ColumnBlobAsVector(int col, std::vector<char>* val) const;
144 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
[email protected]e5ffd0e42009-09-11 21:30:56145
[email protected]faa604e2009-09-25 22:38:59146 // Diagnostics --------------------------------------------------------------
147
148 // Returns the original text of sql statement. Do not keep a pointer to it.
149 const char* GetSQLStatement();
150
[email protected]e5ffd0e42009-09-11 21:30:56151 private:
152 // This is intended to check for serious errors and report them to the
153 // connection object. It takes a sqlite error code, and returns the same
154 // code. Currently this function just updates the succeeded flag, but will be
155 // enhanced in the future to do the notification.
156 int CheckError(int err);
157
[email protected]eff1fa522011-12-12 23:50:59158 // Contraction for checking an error code against SQLITE_OK. Does not set the
159 // succeeded flag.
160 bool CheckOk(int err) const;
161
162 // Should be called by all mutating methods to check that the statement is
163 // valid. Returns true if the statement is valid. DCHECKS and returns false
164 // if it is not.
165 // The reason for this is to handle two specific cases in which a Statement
166 // may be invalid. The first case is that the programmer made an SQL error.
167 // Those cases need to be DCHECKed so that we are guaranteed to find them
168 // before release. The second case is that the computer has an error (probably
169 // out of disk space) which is prohibiting the correct operation of the
170 // database. Our testing apparatus should not exhibit this defect, but release
171 // situations may. Therefore, the code is handling disjoint situations in
172 // release and test. In test, we're ensuring correct SQL. In release, we're
173 // ensuring that contracts are honored in error edge cases.
174 bool CheckValid() const;
175
[email protected]e5ffd0e42009-09-11 21:30:56176 // The actual sqlite statement. This may be unique to us, or it may be cached
177 // by the connection, which is why it's refcounted. This pointer is
178 // guaranteed non-NULL.
179 scoped_refptr<Connection::StatementRef> ref_;
180
[email protected]097723d2013-10-25 20:09:32181 // Set after Step() or Run() are called, reset by Reset(). Used to
182 // prevent accidental calls to API functions which would not work
183 // correctly after stepping has started.
184 bool stepped_;
185
[email protected]e5ffd0e42009-09-11 21:30:56186 // See Succeeded() for what this holds.
187 bool succeeded_;
188
189 DISALLOW_COPY_AND_ASSIGN(Statement);
190};
191
192} // namespace sql
193
[email protected]f0a54b22011-07-19 18:40:21194#endif // SQL_STATEMENT_H_