blob: 22682c8274b10a4e8da21d357a28a914c4fbcb4b [file] [log] [blame]
[email protected]e5ffd0e42009-09-11 21:30:561// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// 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_
7
8#include <string>
9#include <vector>
10
11#include "app/sql/connection.h"
12#include "base/basictypes.h"
13#include "base/ref_counted.h"
14
15namespace sql {
16
[email protected]765b44502009-10-02 05:01:4217// Possible return values from ColumnType in a statement. These should match
18// the values in sqlite3.h.
19enum ColType {
20 COLUMN_TYPE_INTEGER = 1,
21 COLUMN_TYPE_FLOAT = 2,
22 COLUMN_TYPE_TEXT = 3,
23 COLUMN_TYPE_BLOB = 4,
24 COLUMN_TYPE_NULL = 5,
25};
26
[email protected]e5ffd0e42009-09-11 21:30:5627// Normal usage:
[email protected]3273dce2010-01-27 16:08:0828// sql::Statement s(connection_.GetUniqueStatement(...));
[email protected]e5ffd0e42009-09-11 21:30:5629// if (!s) // You should check for errors before using the statement.
30// return false;
31//
32// s.BindInt(0, a);
33// if (s.Step())
34// return s.ColumnString(0);
[email protected]faa604e2009-09-25 22:38:5935//
36// Step() and Run() just return true to signal success. If you want to handle
37// specific errors such as database corruption, install an error handler in
38// in the connection object using set_error_delegate().
[email protected]e5ffd0e42009-09-11 21:30:5639class Statement {
40 public:
41 // Creates an uninitialized statement. The statement will be invalid until
42 // you initialize it via Assign.
43 Statement();
44
[email protected]a5b58f52009-11-17 22:15:4445 explicit Statement(scoped_refptr<Connection::StatementRef> ref);
[email protected]e5ffd0e42009-09-11 21:30:5646 ~Statement();
47
48 // Initializes this object with the given statement, which may or may not
49 // be valid. Use is_valid() to check if it's OK.
50 void Assign(scoped_refptr<Connection::StatementRef> ref);
51
52 // Returns true if the statement can be executed. All functions can still
53 // be used if the statement is invalid, but they will return failure or some
54 // default value. This is because the statement can become invalid in the
55 // middle of executing a command if there is a serioud error and the database
56 // has to be reset.
57 bool is_valid() const { return ref_->is_valid(); }
58
59 // These operators allow conveniently checking if the statement is valid
60 // or not. See the pattern above for an example.
61 operator bool() const { return is_valid(); }
62 bool operator!() const { return !is_valid(); }
63
64 // Running -------------------------------------------------------------------
65
66 // Executes the statement, returning true on success. This is like Step but
67 // for when there is no output, like an INSERT statement.
68 bool Run();
69
70 // Executes the statement, returning true if there is a row of data returned.
71 // You can keep calling Step() until it returns false to iterate through all
72 // the rows in your result set.
73 //
74 // When Step returns false, the result is either that there is no more data
75 // or there is an error. This makes it most convenient for loop usage. If you
76 // need to disambiguate these cases, use Succeeded().
77 //
78 // Typical example:
79 // while (s.Step()) {
80 // ...
81 // }
82 // return s.Succeeded();
83 bool Step();
84
85 // Resets the statement to its initial condition. This includes clearing all
86 // the bound variables and any current result row.
87 void Reset();
88
89 // Returns true if the last executed thing in this statement succeeded. If
90 // there was no last executed thing or the statement is invalid, this will
91 // return false.
92 bool Succeeded() const;
93
94 // Binding -------------------------------------------------------------------
95
96 // These all take a 0-based argument index and return true on failure. You
97 // may not always care about the return value (they'll DCHECK if they fail).
98 // The main thing you may want to check is when binding large blobs or
99 // strings there may be out of memory.
100 bool BindNull(int col);
[email protected]765b44502009-10-02 05:01:42101 bool BindBool(int col, bool val);
[email protected]e5ffd0e42009-09-11 21:30:56102 bool BindInt(int col, int val);
103 bool BindInt64(int col, int64 val);
104 bool BindDouble(int col, double val);
105 bool BindCString(int col, const char* val);
106 bool BindString(int col, const std::string& val);
107 bool BindBlob(int col, const void* value, int value_len);
108
109 // Retrieving ----------------------------------------------------------------
110
111 // Returns the number of output columns in the result.
112 int ColumnCount() const;
113
[email protected]765b44502009-10-02 05:01:42114 // Returns the type associated with the given column.
115 //
116 // Watch out: the type may be undefined if you've done something to cause a
117 // "type conversion." This means requesting the value of a column of a type
118 // where that type is not the native type. For safety, call ColumnType only
119 // on a column before getting the value out in any way.
120 ColType ColumnType(int col) const;
121
[email protected]e5ffd0e42009-09-11 21:30:56122 // These all take a 0-based argument index.
[email protected]765b44502009-10-02 05:01:42123 bool ColumnBool(int col) const;
[email protected]e5ffd0e42009-09-11 21:30:56124 int ColumnInt(int col) const;
125 int64 ColumnInt64(int col) const;
126 double ColumnDouble(int col) const;
127 std::string ColumnString(int col) const;
128
129 // When reading a blob, you can get a raw pointer to the underlying data,
130 // along with the length, or you can just ask us to copy the blob into a
131 // vector. Danger! ColumnBlob may return NULL if there is no data!
[email protected]1ed78a32009-09-15 20:24:17132 int ColumnByteLength(int col) const;
133 const void* ColumnBlob(int col) const;
134 void ColumnBlobAsVector(int col, std::vector<char>* val) const;
135 void ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
[email protected]e5ffd0e42009-09-11 21:30:56136
[email protected]faa604e2009-09-25 22:38:59137 // Diagnostics --------------------------------------------------------------
138
139 // Returns the original text of sql statement. Do not keep a pointer to it.
140 const char* GetSQLStatement();
141
[email protected]e5ffd0e42009-09-11 21:30:56142 private:
143 // This is intended to check for serious errors and report them to the
144 // connection object. It takes a sqlite error code, and returns the same
145 // code. Currently this function just updates the succeeded flag, but will be
146 // enhanced in the future to do the notification.
147 int CheckError(int err);
148
149 // The actual sqlite statement. This may be unique to us, or it may be cached
150 // by the connection, which is why it's refcounted. This pointer is
151 // guaranteed non-NULL.
152 scoped_refptr<Connection::StatementRef> ref_;
153
154 // See Succeeded() for what this holds.
155 bool succeeded_;
156
157 DISALLOW_COPY_AND_ASSIGN(Statement);
158};
159
160} // namespace sql
161
162#endif // APP_SQL_STATEMENT_H_