blob: 54e87e37bb90892b4ba0221c1ef96ed6fc6e7c27 [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
[email protected]85fc27b02012-02-17 02:15:0959 // Resets the statement to an uninitialized state corrosponding to
60 // 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);
tfarina720d4f32015-05-11 22:31:26109 bool BindInt64(int col, int64_t val);
[email protected]e5ffd0e42009-09-11 21:30:56110 bool BindDouble(int col, double val);
111 bool BindCString(int col, const char* val);
112 bool BindString(int col, const std::string& val);
[email protected]fcf75d42013-12-03 20:11:26113 bool BindString16(int col, const base::string16& value);
[email protected]e5ffd0e42009-09-11 21:30:56114 bool BindBlob(int col, const void* value, int value_len);
115
116 // Retrieving ----------------------------------------------------------------
117
118 // Returns the number of output columns in the result.
119 int ColumnCount() const;
120
[email protected]765b44502009-10-02 05:01:42121 // Returns the type associated with the given column.
122 //
123 // Watch out: the type may be undefined if you've done something to cause a
124 // "type conversion." This means requesting the value of a column of a type
125 // where that type is not the native type. For safety, call ColumnType only
126 // on a column before getting the value out in any way.
Jose Dapena Paze18b4d32019-04-08 20:59:34127 ColumnType GetColumnType(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
Victor Costanbd623112018-07-18 04:17:27139 // vector. Danger! ColumnBlob may return nullptr 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;
vabrb194fc562016-07-13 08:46:37142 bool ColumnBlobAsString(int col, std::string* blob) const;
[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:
Victor Costancfbfa602018-08-01 23:24:46153 friend class Database;
shess58b8df82015-06-03 00:19:32154
[email protected]e5ffd0e42009-09-11 21:30:56155 // This is intended to check for serious errors and report them to the
Victor Costancfbfa602018-08-01 23:24:46156 // Database object. It takes a sqlite error code, and returns the same
[email protected]e5ffd0e42009-09-11 21:30:56157 // 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);
160
[email protected]eff1fa522011-12-12 23:50:59161 // Contraction for checking an error code against SQLITE_OK. Does not set the
162 // succeeded flag.
163 bool CheckOk(int err) const;
164
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;
178
Victor Costan5e785e32019-02-26 20:39:31179 // Helper for Run() and Step(), calls sqlite3_step() and returns the checked
180 // value from it.
181 int StepInternal();
shess58b8df82015-06-03 00:19:32182
[email protected]e5ffd0e42009-09-11 21:30:56183 // The actual sqlite statement. This may be unique to us, or it may be cached
Victor Costancfbfa602018-08-01 23:24:46184 // by the Database, which is why it's ref-counted. This pointer is
Victor Costanbd623112018-07-18 04:17:27185 // guaranteed non-null.
Victor Costancfbfa602018-08-01 23:24:46186 scoped_refptr<Database::StatementRef> ref_;
[email protected]e5ffd0e42009-09-11 21:30:56187
[email protected]097723d2013-10-25 20:09:32188 // Set after Step() or Run() are called, reset by Reset(). Used to
189 // prevent accidental calls to API functions which would not work
190 // correctly after stepping has started.
191 bool stepped_;
192
[email protected]e5ffd0e42009-09-11 21:30:56193 // See Succeeded() for what this holds.
194 bool succeeded_;
195
196 DISALLOW_COPY_AND_ASSIGN(Statement);
197};
198
199} // namespace sql
200
[email protected]f0a54b22011-07-19 18:40:21201#endif // SQL_STATEMENT_H_