[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 1 | // 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 | |
| 15 | namespace sql { |
| 16 | |
| 17 | // Normal usage: |
| 18 | // sql::Statement s = connection_.GetUniqueStatement(...); |
| 19 | // if (!s) // You should check for errors before using the statement. |
| 20 | // return false; |
| 21 | // |
| 22 | // s.BindInt(0, a); |
| 23 | // if (s.Step()) |
| 24 | // return s.ColumnString(0); |
| 25 | class Statement { |
| 26 | public: |
| 27 | // Creates an uninitialized statement. The statement will be invalid until |
| 28 | // you initialize it via Assign. |
| 29 | Statement(); |
| 30 | |
| 31 | Statement(scoped_refptr<Connection::StatementRef> ref); |
| 32 | ~Statement(); |
| 33 | |
| 34 | // Initializes this object with the given statement, which may or may not |
| 35 | // be valid. Use is_valid() to check if it's OK. |
| 36 | void Assign(scoped_refptr<Connection::StatementRef> ref); |
| 37 | |
| 38 | // Returns true if the statement can be executed. All functions can still |
| 39 | // be used if the statement is invalid, but they will return failure or some |
| 40 | // default value. This is because the statement can become invalid in the |
| 41 | // middle of executing a command if there is a serioud error and the database |
| 42 | // has to be reset. |
| 43 | bool is_valid() const { return ref_->is_valid(); } |
| 44 | |
| 45 | // These operators allow conveniently checking if the statement is valid |
| 46 | // or not. See the pattern above for an example. |
| 47 | operator bool() const { return is_valid(); } |
| 48 | bool operator!() const { return !is_valid(); } |
| 49 | |
| 50 | // Running ------------------------------------------------------------------- |
| 51 | |
| 52 | // Executes the statement, returning true on success. This is like Step but |
| 53 | // for when there is no output, like an INSERT statement. |
| 54 | bool Run(); |
| 55 | |
| 56 | // Executes the statement, returning true if there is a row of data returned. |
| 57 | // You can keep calling Step() until it returns false to iterate through all |
| 58 | // the rows in your result set. |
| 59 | // |
| 60 | // When Step returns false, the result is either that there is no more data |
| 61 | // or there is an error. This makes it most convenient for loop usage. If you |
| 62 | // need to disambiguate these cases, use Succeeded(). |
| 63 | // |
| 64 | // Typical example: |
| 65 | // while (s.Step()) { |
| 66 | // ... |
| 67 | // } |
| 68 | // return s.Succeeded(); |
| 69 | bool Step(); |
| 70 | |
| 71 | // Resets the statement to its initial condition. This includes clearing all |
| 72 | // the bound variables and any current result row. |
| 73 | void Reset(); |
| 74 | |
| 75 | // Returns true if the last executed thing in this statement succeeded. If |
| 76 | // there was no last executed thing or the statement is invalid, this will |
| 77 | // return false. |
| 78 | bool Succeeded() const; |
| 79 | |
| 80 | // Binding ------------------------------------------------------------------- |
| 81 | |
| 82 | // These all take a 0-based argument index and return true on failure. You |
| 83 | // may not always care about the return value (they'll DCHECK if they fail). |
| 84 | // The main thing you may want to check is when binding large blobs or |
| 85 | // strings there may be out of memory. |
| 86 | bool BindNull(int col); |
| 87 | bool BindInt(int col, int val); |
| 88 | bool BindInt64(int col, int64 val); |
| 89 | bool BindDouble(int col, double val); |
| 90 | bool BindCString(int col, const char* val); |
| 91 | bool BindString(int col, const std::string& val); |
| 92 | bool BindBlob(int col, const void* value, int value_len); |
| 93 | |
| 94 | // Retrieving ---------------------------------------------------------------- |
| 95 | |
| 96 | // Returns the number of output columns in the result. |
| 97 | int ColumnCount() const; |
| 98 | |
| 99 | // These all take a 0-based argument index. |
| 100 | int ColumnInt(int col) const; |
| 101 | int64 ColumnInt64(int col) const; |
| 102 | double ColumnDouble(int col) const; |
| 103 | std::string ColumnString(int col) const; |
| 104 | |
| 105 | // When reading a blob, you can get a raw pointer to the underlying data, |
| 106 | // along with the length, or you can just ask us to copy the blob into a |
| 107 | // vector. Danger! ColumnBlob may return NULL if there is no data! |
| 108 | int ColumnByteLength(int col); |
| 109 | const void* ColumnBlob(int col); |
| 110 | void ColumnBlobAsVector(int col, std::vector<char>* val); |
| 111 | |
| 112 | private: |
| 113 | // This is intended to check for serious errors and report them to the |
| 114 | // connection object. It takes a sqlite error code, and returns the same |
| 115 | // code. Currently this function just updates the succeeded flag, but will be |
| 116 | // enhanced in the future to do the notification. |
| 117 | int CheckError(int err); |
| 118 | |
| 119 | // The actual sqlite statement. This may be unique to us, or it may be cached |
| 120 | // by the connection, which is why it's refcounted. This pointer is |
| 121 | // guaranteed non-NULL. |
| 122 | scoped_refptr<Connection::StatementRef> ref_; |
| 123 | |
| 124 | // See Succeeded() for what this holds. |
| 125 | bool succeeded_; |
| 126 | |
| 127 | DISALLOW_COPY_AND_ASSIGN(Statement); |
| 128 | }; |
| 129 | |
| 130 | } // namespace sql |
| 131 | |
| 132 | #endif // APP_SQL_STATEMENT_H_ |