| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "app/sql/statement.h" | 5 #include "app/sql/statement.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" |
| 8 #include "third_party/sqlite/preprocessed/sqlite3.h" | 9 #include "third_party/sqlite/preprocessed/sqlite3.h" |
| 9 | 10 |
| 10 namespace sql { | 11 namespace sql { |
| 11 | 12 |
| 12 // This empty constructor initializes our reference with an empty one so that | 13 // This empty constructor initializes our reference with an empty one so that |
| 13 // we don't have to NULL-check the ref_ to see if the statement is valid: we | 14 // we don't have to NULL-check the ref_ to see if the statement is valid: we |
| 14 // only have to check the ref's validity bit. | 15 // only have to check the ref's validity bit. |
| 15 Statement::Statement() | 16 Statement::Statement() |
| 16 : ref_(new Connection::StatementRef), | 17 : ref_(new Connection::StatementRef), |
| 17 succeeded_(false) { | 18 succeeded_(false) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 111 |
| 111 bool Statement::BindString(int col, const std::string& val) { | 112 bool Statement::BindString(int col, const std::string& val) { |
| 112 if (is_valid()) { | 113 if (is_valid()) { |
| 113 int err = CheckError(sqlite3_bind_text(ref_->stmt(), col + 1, val.data(), | 114 int err = CheckError(sqlite3_bind_text(ref_->stmt(), col + 1, val.data(), |
| 114 val.size(), SQLITE_TRANSIENT)); | 115 val.size(), SQLITE_TRANSIENT)); |
| 115 return err == SQLITE_OK; | 116 return err == SQLITE_OK; |
| 116 } | 117 } |
| 117 return false; | 118 return false; |
| 118 } | 119 } |
| 119 | 120 |
| 121 bool Statement::BindString16(int col, const string16& value) { |
| 122 return BindString(col, UTF16ToUTF8(value)); |
| 123 } |
| 124 |
| 120 bool Statement::BindBlob(int col, const void* val, int val_len) { | 125 bool Statement::BindBlob(int col, const void* val, int val_len) { |
| 121 if (is_valid()) { | 126 if (is_valid()) { |
| 122 int err = CheckError(sqlite3_bind_blob(ref_->stmt(), col + 1, | 127 int err = CheckError(sqlite3_bind_blob(ref_->stmt(), col + 1, |
| 123 val, val_len, SQLITE_TRANSIENT)); | 128 val, val_len, SQLITE_TRANSIENT)); |
| 124 return err == SQLITE_OK; | 129 return err == SQLITE_OK; |
| 125 } | 130 } |
| 126 return false; | 131 return false; |
| 127 } | 132 } |
| 128 | 133 |
| 129 int Statement::ColumnCount() const { | 134 int Statement::ColumnCount() const { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 const char* str = reinterpret_cast<const char*>( | 186 const char* str = reinterpret_cast<const char*>( |
| 182 sqlite3_column_text(ref_->stmt(), col)); | 187 sqlite3_column_text(ref_->stmt(), col)); |
| 183 int len = sqlite3_column_bytes(ref_->stmt(), col); | 188 int len = sqlite3_column_bytes(ref_->stmt(), col); |
| 184 | 189 |
| 185 std::string result; | 190 std::string result; |
| 186 if (str && len > 0) | 191 if (str && len > 0) |
| 187 result.assign(str, len); | 192 result.assign(str, len); |
| 188 return result; | 193 return result; |
| 189 } | 194 } |
| 190 | 195 |
| 196 string16 Statement::ColumnString16(int col) const { |
| 197 if (!is_valid()) { |
| 198 NOTREACHED(); |
| 199 return string16(); |
| 200 } |
| 201 std::string s = ColumnString(col); |
| 202 return !s.empty() ? UTF8ToUTF16(s) : string16(); |
| 203 } |
| 204 |
| 191 int Statement::ColumnByteLength(int col) const { | 205 int Statement::ColumnByteLength(int col) const { |
| 192 if (!is_valid()) { | 206 if (!is_valid()) { |
| 193 NOTREACHED(); | 207 NOTREACHED(); |
| 194 return 0; | 208 return 0; |
| 195 } | 209 } |
| 196 return sqlite3_column_bytes(ref_->stmt(), col); | 210 return sqlite3_column_bytes(ref_->stmt(), col); |
| 197 } | 211 } |
| 198 | 212 |
| 199 const void* Statement::ColumnBlob(int col) const { | 213 const void* Statement::ColumnBlob(int col) const { |
| 200 if (!is_valid()) { | 214 if (!is_valid()) { |
| 201 NOTREACHED(); | 215 NOTREACHED(); |
| 202 return NULL; | 216 return NULL; |
| 203 } | 217 } |
| 204 | 218 |
| 205 return sqlite3_column_blob(ref_->stmt(), col); | 219 return sqlite3_column_blob(ref_->stmt(), col); |
| 206 } | 220 } |
| 207 | 221 |
| 222 bool Statement::ColumnBlobAsString(int col, std::string* blob) { |
| 223 if (!is_valid()) { |
| 224 NOTREACHED(); |
| 225 return false; |
| 226 } |
| 227 const void* p = ColumnBlob(col); |
| 228 size_t len = ColumnByteLength(col); |
| 229 blob->resize(len); |
| 230 if (blob->size() != len) { |
| 231 return false; |
| 232 } |
| 233 blob->assign(reinterpret_cast<const char*>(p), len); |
| 234 return true; |
| 235 } |
| 236 |
| 208 void Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const { | 237 void Statement::ColumnBlobAsVector(int col, std::vector<char>* val) const { |
| 209 val->clear(); | 238 val->clear(); |
| 210 if (!is_valid()) { | 239 if (!is_valid()) { |
| 211 NOTREACHED(); | 240 NOTREACHED(); |
| 212 return; | 241 return; |
| 213 } | 242 } |
| 214 | 243 |
| 215 const void* data = sqlite3_column_blob(ref_->stmt(), col); | 244 const void* data = sqlite3_column_blob(ref_->stmt(), col); |
| 216 int len = sqlite3_column_bytes(ref_->stmt(), col); | 245 int len = sqlite3_column_bytes(ref_->stmt(), col); |
| 217 if (data && len > 0) { | 246 if (data && len > 0) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 232 | 261 |
| 233 int Statement::CheckError(int err) { | 262 int Statement::CheckError(int err) { |
| 234 // Please don't add DCHECKs here, OnSqliteError() already has them. | 263 // Please don't add DCHECKs here, OnSqliteError() already has them. |
| 235 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); | 264 succeeded_ = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); |
| 236 if (!succeeded_ && is_valid()) | 265 if (!succeeded_ && is_valid()) |
| 237 return ref_->connection()->OnSqliteError(err, this); | 266 return ref_->connection()->OnSqliteError(err, this); |
| 238 return err; | 267 return err; |
| 239 } | 268 } |
| 240 | 269 |
| 241 } // namespace sql | 270 } // namespace sql |
| OLD | NEW |