blob: 6bf394978e4fa37984ab7dfa68cac0b5fa401889 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 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
5#ifndef APP_SQL_STATEMENT_H_
6#define APP_SQL_STATEMENT_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]e5ffd0e42009-09-11 21:30:568
9#include <string>
10#include <vector>
11
12#include "app/sql/connection.h"
13#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]5eea1162010-05-11 17:14:1615#include "base/string16.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// if (!s) // You should check for errors before using the statement.
32// return false;
33//
34// s.BindInt(0, a);
35// if (s.Step())
36// return s.ColumnString(0);
[email protected]faa604e2009-09-25 22:38:5937//
38// Step() and Run() just return true to signal success. If you want to handle
39// specific errors such as database corruption, install an error handler in
40// in the connection object using set_error_delegate().
[email protected]e5ffd0e42009-09-11 21:30:5641class Statement {
42 public:
43 // Creates an uninitialized statement. The statement will be invalid until
44 // you initialize it via Assign.
45 Statement();
46
[email protected]a5b58f52009-11-17 22:15:4447 explicit Statement(scoped_refptr<Connection::StatementRef> ref);
[email protected]e5ffd0e42009-09-11 21:30:5648 ~Statement();
49
50 // Initializes this object with the given statement, which may or may not
51 // be valid. Use is_valid() to check if it's OK.
52 void Assign(scoped_refptr<Connection::StatementRef> ref);
53
54 // Returns true if the statement can be executed. All functions can still
55 // be used if the statement is invalid, but they will return failure or some
56 // default value. This is because the statement can become invalid in the
57 // middle of executing a command if there is a serioud error and the database
58 // has to be reset.
59 bool is_valid() const { return ref_->is_valid(); }
60
61 // These operators allow conveniently checking if the statement is valid
62 // or not. See the pattern above for an example.
63 operator bool() const { return is_valid(); }
64 bool operator!() const { return !is_valid(); }
65
66 // Running -------------------------------------------------------------------
67
68 // Executes the statement, returning true on success. This is like Step but
69 // for when there is no output, like an INSERT statement.
70 bool Run();
71
72 // Executes the statement, returning true if there is a row of data returned.
73 // You can keep calling Step() until it returns false to iterate through all
74 // the rows in your result set.
75 //
76 // When Step returns false, the result is either that there is no more data
77 // or there is an error. This makes it most convenient for loop usage. If you
78 // need to disambiguate these cases, use Succeeded().
79 //
80 // Typical example:
81 // while (s.Step()) {
82 // ...
83 // }
84 // return s.Succeeded();
85 bool Step();
86
87 // Resets the statement to its initial condition. This includes clearing all
88 // the bound variables and any current result row.
89 void Reset();
90
91 // Returns true if the last executed thing in this statement succeeded. If
92 // there was no last executed thing or the statement is invalid, this will
93 // return false.
94 bool Succeeded() const;
95
96 // Binding -------------------------------------------------------------------
97
98 // These all take a 0-based argument index and return true on failure. You
99 // may not always care about the return value (they'll DCHECK if they fail).
100 // The main thing you may want to check is when binding large blobs or
101 // strings there may be out of memory.
102 bool BindNull(int col);
[email protected]765b44502009-10-02 05:01:42103 bool BindBool(int col, bool val);
[email protected]e5ffd0e42009-09-11 21:30:56104 bool BindInt(int col, int val);
105 bool BindInt64(int col, int64 val);
106 bool BindDouble(int col, double val);
107 bool BindCString(int col, const char* val);
108 bool BindString(int col, const std::string& val);
[email protected]5eea1162010-05-11 17:14:16109 bool BindString16(int col, const string16& value);
[email protected]e5ffd0e42009-09-11 21:30:56110 bool BindBlob(int col, const void* value, int value_len);
111
112 // Retrieving ----------------------------------------------------------------
113
114 // Returns the number of output columns in the result.
115 int ColumnCount() const;
116
[email protected]765b44502009-10-02 05:01:42117 // Returns the type associated with the given column.
118 //
119 // Watch out: the type may be undefined if you've done something to cause a
120 // "type conversion." This means requesting the value of a column of a type
121 // where that type is not the native type. For safety, call ColumnType only
122 // on a column before getting the value out in any way.
123 ColType ColumnType(int col) const;
124
[email protected]e5ffd0e42009-09-11 21:30:56125 // These all take a 0-based argument index.
[email protected]765b44502009-10-02 05:01:42126 bool ColumnBool(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56127 int ColumnInt(int col) const;
128 int64 ColumnInt64(int col) const;
129 double ColumnDouble(int col) const;
130 std::string ColumnString(int col) const;
[email protected]5eea1162010-05-11 17:14:16131 string16 ColumnString16(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56132
133 // When reading a blob, you can get a raw pointer to the underlying data,
134 // along with the length, or you can just ask us to copy the blob into a
135 // vector. Danger! ColumnBlob may return NULL if there is no data!
[email protected]1ed78a32009-09-15 20:24:17136 int ColumnByteLength(int col) const;
137 const void* ColumnBlob(int col) const;
[email protected]5eea1162010-05-11 17:14:16138 bool ColumnBlobAsString(int col, std::string* blob);
[email protected]1ed78a32009-09-15 20:24:17139 void ColumnBlobAsVector(int col, std::vector<char>* val) const;
140 void ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
[email protected]e5ffd0e42009-09-11 21:30:56141
[email protected]faa604e2009-09-25 22:38:59142 // Diagnostics --------------------------------------------------------------
143
144 // Returns the original text of sql statement. Do not keep a pointer to it.
145 const char* GetSQLStatement();
146
[email protected]e5ffd0e42009-09-11 21:30:56147 private:
148 // This is intended to check for serious errors and report them to the
149 // connection object. It takes a sqlite error code, and returns the same
150 // code. Currently this function just updates the succeeded flag, but will be
151 // enhanced in the future to do the notification.
152 int CheckError(int err);
153
154 // The actual sqlite statement. This may be unique to us, or it may be cached
155 // by the connection, which is why it's refcounted. This pointer is
156 // guaranteed non-NULL.
157 scoped_refptr<Connection::StatementRef> ref_;
158
159 // See Succeeded() for what this holds.
160 bool succeeded_;
161
162 DISALLOW_COPY_AND_ASSIGN(Statement);
163};
164
165} // namespace sql
166
167#endif // APP_SQL_STATEMENT_H_