blob: ba311635e24103912e4c43b89a54fa3059127372 [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
Victor Costane56cc682018-12-27 01:53:4612#include "base/component_export.h"
tfarina720d4f32015-05-11 22:31:2613#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
Victor Costan3a325b812018-07-23 22:16:1815#include "base/sequence_checker.h"
[email protected]a4bbc1f92013-06-11 07:28:1916#include "base/strings/string16.h"
Jan Wilken Dörrie9720dce2020-07-21 17:14:2317#include "base/strings/string_piece_forward.h"
Victor Costancfbfa602018-08-01 23:24:4618#include "sql/database.h"
[email protected]e5ffd0e42009-09-11 21:30:5619
20namespace sql {
21
[email protected]765b44502009-10-02 05:01:4222// Possible return values from ColumnType in a statement. These should match
23// the values in sqlite3.h.
Victor Costan57aecd232019-04-04 09:09:5724enum class ColumnType {
25 kInteger = 1,
26 kFloat = 2,
27 kText = 3,
28 kBlob = 4,
29 kNull = 5,
[email protected]765b44502009-10-02 05:01:4230};
31
[email protected]e5ffd0e42009-09-11 21:30:5632// Normal usage:
[email protected]3273dce2010-01-27 16:08:0833// sql::Statement s(connection_.GetUniqueStatement(...));
[email protected]e5ffd0e42009-09-11 21:30:5634// s.BindInt(0, a);
35// if (s.Step())
36// return s.ColumnString(0);
[email protected]faa604e2009-09-25 22:38:5937//
[email protected]eff1fa522011-12-12 23:50:5938// If there are errors getting the statement, the statement will be inert; no
39// mutating or database-access methods will work. If you need to check for
40// validity, use:
41// if (!s.is_valid())
42// return false;
43//
[email protected]faa604e2009-09-25 22:38:5944// Step() and Run() just return true to signal success. If you want to handle
45// specific errors such as database corruption, install an error handler in
46// in the connection object using set_error_delegate().
Victor Costane56cc682018-12-27 01:53:4647class COMPONENT_EXPORT(SQL) Statement {
[email protected]e5ffd0e42009-09-11 21:30:5648 public:
49 // Creates an uninitialized statement. The statement will be invalid until
50 // you initialize it via Assign.
51 Statement();
52
Victor Costancfbfa602018-08-01 23:24:4653 explicit Statement(scoped_refptr<Database::StatementRef> ref);
[email protected]e5ffd0e42009-09-11 21:30:5654 ~Statement();
55
56 // Initializes this object with the given statement, which may or may not
57 // be valid. Use is_valid() to check if it's OK.
Victor Costancfbfa602018-08-01 23:24:4658 void Assign(scoped_refptr<Database::StatementRef> ref);
[email protected]e5ffd0e42009-09-11 21:30:5659
Robert Ogden7b0e36302019-10-17 17:12:3060 // Resets the statement to an uninitialized state corresponding to
[email protected]85fc27b02012-02-17 02:15:0961 // the default constructor, releasing the StatementRef.
62 void Clear();
63
[email protected]e5ffd0e42009-09-11 21:30:5664 // Returns true if the statement can be executed. All functions can still
65 // be used if the statement is invalid, but they will return failure or some
66 // default value. This is because the statement can become invalid in the
[email protected]bed29d942011-12-22 19:25:5167 // middle of executing a command if there is a serious error and the database
[email protected]e5ffd0e42009-09-11 21:30:5668 // has to be reset.
69 bool is_valid() const { return ref_->is_valid(); }
70
[email protected]e5ffd0e42009-09-11 21:30:5671 // Running -------------------------------------------------------------------
72
73 // Executes the statement, returning true on success. This is like Step but
74 // for when there is no output, like an INSERT statement.
75 bool Run();
76
77 // Executes the statement, returning true if there is a row of data returned.
78 // You can keep calling Step() until it returns false to iterate through all
79 // the rows in your result set.
80 //
81 // When Step returns false, the result is either that there is no more data
82 // or there is an error. This makes it most convenient for loop usage. If you
83 // need to disambiguate these cases, use Succeeded().
84 //
85 // Typical example:
86 // while (s.Step()) {
87 // ...
88 // }
89 // return s.Succeeded();
90 bool Step();
91
[email protected]389e0a42012-04-25 21:36:4192 // Resets the statement to its initial condition. This includes any current
93 // result row, and also the bound variables if the |clear_bound_vars| is true.
94 void Reset(bool clear_bound_vars);
[email protected]e5ffd0e42009-09-11 21:30:5695
96 // Returns true if the last executed thing in this statement succeeded. If
97 // there was no last executed thing or the statement is invalid, this will
98 // return false.
99 bool Succeeded() const;
100
101 // Binding -------------------------------------------------------------------
102
[email protected]eff1fa522011-12-12 23:50:59103 // These all take a 0-based argument index and return true on success. You
[email protected]e5ffd0e42009-09-11 21:30:56104 // may not always care about the return value (they'll DCHECK if they fail).
105 // The main thing you may want to check is when binding large blobs or
106 // strings there may be out of memory.
107 bool BindNull(int col);
[email protected]765b44502009-10-02 05:01:42108 bool BindBool(int col, bool val);
[email protected]e5ffd0e42009-09-11 21:30:56109 bool BindInt(int col, int val);
Joshua Bellf070ffd2019-12-10 19:44:51110 bool BindInt(int col, int64_t val) = delete; // Call BindInt64() instead.
tfarina720d4f32015-05-11 22:31:26111 bool BindInt64(int col, int64_t val);
[email protected]e5ffd0e42009-09-11 21:30:56112 bool BindDouble(int col, double val);
113 bool BindCString(int col, const char* val);
114 bool BindString(int col, const std::string& val);
Jan Wilken Dörrie9720dce2020-07-21 17:14:23115 bool BindString16(int col, base::StringPiece16 value);
[email protected]e5ffd0e42009-09-11 21:30:56116 bool BindBlob(int col, const void* value, int value_len);
117
118 // Retrieving ----------------------------------------------------------------
119
120 // Returns the number of output columns in the result.
121 int ColumnCount() const;
122
[email protected]765b44502009-10-02 05:01:42123 // Returns the type associated with the given column.
124 //
125 // Watch out: the type may be undefined if you've done something to cause a
126 // "type conversion." This means requesting the value of a column of a type
127 // where that type is not the native type. For safety, call ColumnType only
128 // on a column before getting the value out in any way.
Jose Dapena Paze18b4d32019-04-08 20:59:34129 ColumnType GetColumnType(int col) const;
[email protected]765b44502009-10-02 05:01:42130
[email protected]e5ffd0e42009-09-11 21:30:56131 // These all take a 0-based argument index.
[email protected]765b44502009-10-02 05:01:42132 bool ColumnBool(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56133 int ColumnInt(int col) const;
tfarina720d4f32015-05-11 22:31:26134 int64_t ColumnInt64(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56135 double ColumnDouble(int col) const;
136 std::string ColumnString(int col) const;
[email protected]fcf75d42013-12-03 20:11:26137 base::string16 ColumnString16(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56138
139 // When reading a blob, you can get a raw pointer to the underlying data,
140 // along with the length, or you can just ask us to copy the blob into a
Victor Costanbd623112018-07-18 04:17:27141 // vector. Danger! ColumnBlob may return nullptr if there is no data!
[email protected]1ed78a32009-09-15 20:24:17142 int ColumnByteLength(int col) const;
143 const void* ColumnBlob(int col) const;
vabrb194fc562016-07-13 08:46:37144 bool ColumnBlobAsString(int col, std::string* blob) const;
[email protected]fcf75d42013-12-03 20:11:26145 bool ColumnBlobAsString16(int col, base::string16* val) const;
[email protected]eff1fa522011-12-12 23:50:59146 bool ColumnBlobAsVector(int col, std::vector<char>* val) const;
147 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
[email protected]e5ffd0e42009-09-11 21:30:56148
[email protected]faa604e2009-09-25 22:38:59149 // Diagnostics --------------------------------------------------------------
150
151 // Returns the original text of sql statement. Do not keep a pointer to it.
152 const char* GetSQLStatement();
153
[email protected]e5ffd0e42009-09-11 21:30:56154 private:
Victor Costancfbfa602018-08-01 23:24:46155 friend class Database;
shess58b8df82015-06-03 00:19:32156
[email protected]e5ffd0e42009-09-11 21:30:56157 // This is intended to check for serious errors and report them to the
Victor Costancfbfa602018-08-01 23:24:46158 // Database object. It takes a sqlite error code, and returns the same
[email protected]e5ffd0e42009-09-11 21:30:56159 // code. Currently this function just updates the succeeded flag, but will be
160 // enhanced in the future to do the notification.
161 int CheckError(int err);
162
[email protected]eff1fa522011-12-12 23:50:59163 // Contraction for checking an error code against SQLITE_OK. Does not set the
164 // succeeded flag.
165 bool CheckOk(int err) const;
166
167 // Should be called by all mutating methods to check that the statement is
168 // valid. Returns true if the statement is valid. DCHECKS and returns false
169 // if it is not.
170 // The reason for this is to handle two specific cases in which a Statement
171 // may be invalid. The first case is that the programmer made an SQL error.
172 // Those cases need to be DCHECKed so that we are guaranteed to find them
173 // before release. The second case is that the computer has an error (probably
174 // out of disk space) which is prohibiting the correct operation of the
175 // database. Our testing apparatus should not exhibit this defect, but release
176 // situations may. Therefore, the code is handling disjoint situations in
177 // release and test. In test, we're ensuring correct SQL. In release, we're
178 // ensuring that contracts are honored in error edge cases.
179 bool CheckValid() const;
180
Victor Costan5e785e32019-02-26 20:39:31181 // Helper for Run() and Step(), calls sqlite3_step() and returns the checked
182 // value from it.
183 int StepInternal();
shess58b8df82015-06-03 00:19:32184
[email protected]e5ffd0e42009-09-11 21:30:56185 // The actual sqlite statement. This may be unique to us, or it may be cached
Victor Costancfbfa602018-08-01 23:24:46186 // by the Database, which is why it's ref-counted. This pointer is
Victor Costanbd623112018-07-18 04:17:27187 // guaranteed non-null.
Victor Costancfbfa602018-08-01 23:24:46188 scoped_refptr<Database::StatementRef> ref_;
[email protected]e5ffd0e42009-09-11 21:30:56189
[email protected]097723d2013-10-25 20:09:32190 // Set after Step() or Run() are called, reset by Reset(). Used to
191 // prevent accidental calls to API functions which would not work
192 // correctly after stepping has started.
193 bool stepped_;
194
[email protected]e5ffd0e42009-09-11 21:30:56195 // See Succeeded() for what this holds.
196 bool succeeded_;
197
198 DISALLOW_COPY_AND_ASSIGN(Statement);
199};
200
201} // namespace sql
202
[email protected]f0a54b22011-07-19 18:40:21203#endif // SQL_STATEMENT_H_