blob: 6983e2132aed547a6724ff366a25ace795bc124c [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"
Victor Costancfbfa602018-08-01 23:24:4617#include "sql/database.h"
[email protected]e5ffd0e42009-09-11 21:30:5618
19namespace sql {
20
[email protected]765b44502009-10-02 05:01:4221// Possible return values from ColumnType in a statement. These should match
22// the values in sqlite3.h.
Victor Costan57aecd232019-04-04 09:09:5723enum class ColumnType {
24 kInteger = 1,
25 kFloat = 2,
26 kText = 3,
27 kBlob = 4,
28 kNull = 5,
[email protected]765b44502009-10-02 05:01:4229};
30
[email protected]e5ffd0e42009-09-11 21:30:5631// Normal usage:
[email protected]3273dce2010-01-27 16:08:0832// sql::Statement s(connection_.GetUniqueStatement(...));
[email protected]e5ffd0e42009-09-11 21:30:5633// s.BindInt(0, a);
34// if (s.Step())
35// return s.ColumnString(0);
[email protected]faa604e2009-09-25 22:38:5936//
[email protected]eff1fa522011-12-12 23:50:5937// 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
39// validity, use:
40// if (!s.is_valid())
41// return false;
42//
[email protected]faa604e2009-09-25 22:38:5943// 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().
Victor Costane56cc682018-12-27 01:53:4646class COMPONENT_EXPORT(SQL) Statement {
[email protected]e5ffd0e42009-09-11 21:30:5647 public:
48 // Creates an uninitialized statement. The statement will be invalid until
49 // you initialize it via Assign.
50 Statement();
51
Victor Costancfbfa602018-08-01 23:24:4652 explicit Statement(scoped_refptr<Database::StatementRef> ref);
[email protected]e5ffd0e42009-09-11 21:30:5653 ~Statement();
54
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.
Victor Costancfbfa602018-08-01 23:24:4657 void Assign(scoped_refptr<Database::StatementRef> ref);
[email protected]e5ffd0e42009-09-11 21:30:5658
Robert Ogden7b0e36302019-10-17 17:12:3059 // Resets the statement to an uninitialized state corresponding to
[email protected]85fc27b02012-02-17 02:15:0960 // the default constructor, releasing the StatementRef.
61 void Clear();
62
[email protected]e5ffd0e42009-09-11 21:30:5663 // 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
[email protected]bed29d942011-12-22 19:25:5166 // middle of executing a command if there is a serious error and the database
[email protected]e5ffd0e42009-09-11 21:30:5667 // has to be reset.
68 bool is_valid() const { return ref_->is_valid(); }
69
[email protected]e5ffd0e42009-09-11 21:30:5670 // Running -------------------------------------------------------------------
71
72 // Executes the statement, returning true on success. This is like Step but
73 // for when there is no output, like an INSERT statement.
74 bool Run();
75
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.
79 //
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().
83 //
84 // Typical example:
85 // while (s.Step()) {
86 // ...
87 // }
88 // return s.Succeeded();
89 bool Step();
90
[email protected]389e0a42012-04-25 21:36:4191 // 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);
[email protected]e5ffd0e42009-09-11 21:30:5694
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
97 // return false.
98 bool Succeeded() const;
99
100 // Binding -------------------------------------------------------------------
101
[email protected]eff1fa522011-12-12 23:50:59102 // These all take a 0-based argument index and return true on success. You
[email protected]e5ffd0e42009-09-11 21:30:56103 // 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);
[email protected]765b44502009-10-02 05:01:42107 bool BindBool(int col, bool val);
[email protected]e5ffd0e42009-09-11 21:30:56108 bool BindInt(int col, int val);
Joshua Bellf070ffd2019-12-10 19:44:51109 bool BindInt(int col, int64_t val) = delete; // Call BindInt64() instead.
tfarina720d4f32015-05-11 22:31:26110 bool BindInt64(int col, int64_t val);
[email protected]e5ffd0e42009-09-11 21:30:56111 bool BindDouble(int col, double val);
112 bool BindCString(int col, const char* val);
113 bool BindString(int col, const std::string& val);
[email protected]fcf75d42013-12-03 20:11:26114 bool BindString16(int col, const base::string16& value);
[email protected]e5ffd0e42009-09-11 21:30:56115 bool BindBlob(int col, const void* value, int value_len);
116
117 // Retrieving ----------------------------------------------------------------
118
119 // Returns the number of output columns in the result.
120 int ColumnCount() const;
121
[email protected]765b44502009-10-02 05:01:42122 // Returns the type associated with the given column.
123 //
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.
Jose Dapena Paze18b4d32019-04-08 20:59:34128 ColumnType GetColumnType(int col) const;
[email protected]765b44502009-10-02 05:01:42129
[email protected]e5ffd0e42009-09-11 21:30:56130 // These all take a 0-based argument index.
[email protected]765b44502009-10-02 05:01:42131 bool ColumnBool(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56132 int ColumnInt(int col) const;
tfarina720d4f32015-05-11 22:31:26133 int64_t ColumnInt64(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56134 double ColumnDouble(int col) const;
135 std::string ColumnString(int col) const;
[email protected]fcf75d42013-12-03 20:11:26136 base::string16 ColumnString16(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56137
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
Victor Costanbd623112018-07-18 04:17:27140 // vector. Danger! ColumnBlob may return nullptr if there is no data!
[email protected]1ed78a32009-09-15 20:24:17141 int ColumnByteLength(int col) const;
142 const void* ColumnBlob(int col) const;
vabrb194fc562016-07-13 08:46:37143 bool ColumnBlobAsString(int col, std::string* blob) const;
[email protected]fcf75d42013-12-03 20:11:26144 bool ColumnBlobAsString16(int col, base::string16* val) const;
[email protected]eff1fa522011-12-12 23:50:59145 bool ColumnBlobAsVector(int col, std::vector<char>* val) const;
146 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
[email protected]e5ffd0e42009-09-11 21:30:56147
[email protected]faa604e2009-09-25 22:38:59148 // Diagnostics --------------------------------------------------------------
149
150 // Returns the original text of sql statement. Do not keep a pointer to it.
151 const char* GetSQLStatement();
152
[email protected]e5ffd0e42009-09-11 21:30:56153 private:
Victor Costancfbfa602018-08-01 23:24:46154 friend class Database;
shess58b8df82015-06-03 00:19:32155
[email protected]e5ffd0e42009-09-11 21:30:56156 // This is intended to check for serious errors and report them to the
Victor Costancfbfa602018-08-01 23:24:46157 // Database object. It takes a sqlite error code, and returns the same
[email protected]e5ffd0e42009-09-11 21:30:56158 // 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);
161
[email protected]eff1fa522011-12-12 23:50:59162 // Contraction for checking an error code against SQLITE_OK. Does not set the
163 // succeeded flag.
164 bool CheckOk(int err) const;
165
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
168 // if it is not.
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;
179
Victor Costan5e785e32019-02-26 20:39:31180 // Helper for Run() and Step(), calls sqlite3_step() and returns the checked
181 // value from it.
182 int StepInternal();
shess58b8df82015-06-03 00:19:32183
[email protected]e5ffd0e42009-09-11 21:30:56184 // The actual sqlite statement. This may be unique to us, or it may be cached
Victor Costancfbfa602018-08-01 23:24:46185 // by the Database, which is why it's ref-counted. This pointer is
Victor Costanbd623112018-07-18 04:17:27186 // guaranteed non-null.
Victor Costancfbfa602018-08-01 23:24:46187 scoped_refptr<Database::StatementRef> ref_;
[email protected]e5ffd0e42009-09-11 21:30:56188
[email protected]097723d2013-10-25 20:09:32189 // 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.
192 bool stepped_;
193
[email protected]e5ffd0e42009-09-11 21:30:56194 // See Succeeded() for what this holds.
195 bool succeeded_;
196
197 DISALLOW_COPY_AND_ASSIGN(Statement);
198};
199
200} // namespace sql
201
[email protected]f0a54b22011-07-19 18:40:21202#endif // SQL_STATEMENT_H_