1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sql/statement.h"
10 #include "base/containers/span.h"
11 #include "base/dcheck_is_on.h"
12 #include "base/logging.h"
13 #include "base/numerics/safe_conversions.h"
14 #include "base/sequence_checker.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h"
19 #include "sql/sqlite_result_code.h"
20 #include "sql/sqlite_result_code_values.h"
21 #include "third_party/sqlite/sqlite3.h"
25 // This empty constructor initializes our reference with an empty one so that
26 // we don't have to null-check the ref_ to see if the statement is valid: we
27 // only have to check the ref's validity bit.
28 Statement::Statement()
29 : ref_(base::MakeRefCounted<Database::StatementRef>(nullptr,
33 Statement::Statement(scoped_refptr<Database::StatementRef> ref)
34 : ref_(std::move(ref)) {}
36 Statement::~Statement() {
37 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
39 // Free the resources associated with this statement. We assume there's only
40 // one statement active for a given sqlite3_stmt at any time, so this won't
41 // mess with anything.
45 void Statement::Assign(scoped_refptr<Database::StatementRef> ref) {
46 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
49 ref_ = std::move(ref);
52 void Statement::Clear() {
53 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
55 Assign(base::MakeRefCounted<Database::StatementRef>(nullptr, nullptr, false));
59 bool Statement::CheckValid() const {
60 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
62 // Allow operations to fail silently if a statement was invalidated
63 // because the database was closed by an error handler.
64 DLOG_IF(FATAL, !ref_->was_valid())
65 << "Cannot call mutating statements on an invalid statement.";
69 SqliteResultCode Statement::StepInternal() {
70 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
73 return SqliteResultCode::kError;
75 absl::optional<base::ScopedBlockingCall> scoped_blocking_call;
76 ref_->InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
78 auto sqlite_result_code = ToSqliteResultCode(sqlite3_step(ref_->stmt()));
79 return CheckSqliteResultCode(sqlite_result_code);
82 bool Statement::Run() {
83 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
86 DCHECK(!run_called_) << "Run() must be called exactly once";
88 DCHECK(!step_called_) << "Run() must not be mixed with Step()";
89 #endif // DCHECK_IS_ON()
90 return StepInternal() == SqliteResultCode::kDone;
93 bool Statement::Step() {
94 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
97 DCHECK(!run_called_) << "Run() must not be mixed with Step()";
99 #endif // DCHECK_IS_ON()
100 return StepInternal() == SqliteResultCode::kRow;
103 void Statement::Reset(bool clear_bound_vars) {
104 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
106 absl::optional<base::ScopedBlockingCall> scoped_blocking_call;
107 ref_->InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
109 if (clear_bound_vars)
110 sqlite3_clear_bindings(ref_->stmt());
112 // StepInternal() cannot track success because statements may be reset
113 // before reaching SQLITE_DONE. Don't call CheckError() because
114 // sqlite3_reset() returns the last step error, which StepInternal() already
116 sqlite3_reset(ref_->stmt());
119 // Potentially release dirty cache pages if an autocommit statement made
121 if (ref_->database())
122 ref_->database()->ReleaseCacheMemoryIfNeeded(false);
127 step_called_ = false;
128 #endif // DCHECK_IS_ON()
131 bool Statement::Succeeded() const {
132 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
134 return is_valid() && succeeded_;
137 void Statement::BindNull(int param_index) {
138 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
141 DCHECK(!run_called_) << __func__ << " must not be called after Run()";
142 DCHECK(!step_called_) << __func__ << " must not be called after Step()";
143 #endif // DCHECK_IS_ON()
148 DCHECK_GE(param_index, 0);
149 DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
150 << "Invalid parameter index";
151 int sqlite_result_code = sqlite3_bind_null(ref_->stmt(), param_index + 1);
152 DCHECK_EQ(sqlite_result_code, SQLITE_OK);
155 void Statement::BindBool(int param_index, bool val) {
156 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
158 return BindInt64(param_index, val ? 1 : 0);
161 void Statement::BindInt(int param_index, int val) {
162 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
165 DCHECK(!run_called_) << __func__ << " must not be called after Run()";
166 DCHECK(!step_called_) << __func__ << " must not be called after Step()";
167 #endif // DCHECK_IS_ON()
172 DCHECK_GE(param_index, 0);
173 DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
174 << "Invalid parameter index";
175 int sqlite_result_code = sqlite3_bind_int(ref_->stmt(), param_index + 1, val);
176 DCHECK_EQ(sqlite_result_code, SQLITE_OK);
179 void Statement::BindInt64(int param_index, int64_t val) {
180 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
183 DCHECK(!run_called_) << __func__ << " must not be called after Run()";
184 DCHECK(!step_called_) << __func__ << " must not be called after Step()";
185 #endif // DCHECK_IS_ON()
190 DCHECK_GE(param_index, 0);
191 DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
192 << "Invalid parameter index";
193 int sqlite_result_code =
194 sqlite3_bind_int64(ref_->stmt(), param_index + 1, val);
195 DCHECK_EQ(sqlite_result_code, SQLITE_OK);
198 void Statement::BindDouble(int param_index, double val) {
199 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
202 DCHECK(!run_called_) << __func__ << " must not be called after Run()";
203 DCHECK(!step_called_) << __func__ << " must not be called after Step()";
204 #endif // DCHECK_IS_ON()
209 DCHECK_GE(param_index, 0);
210 DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
211 << "Invalid parameter index";
212 int sqlite_result_code =
213 sqlite3_bind_double(ref_->stmt(), param_index + 1, val);
214 DCHECK_EQ(sqlite_result_code, SQLITE_OK);
217 void Statement::BindTime(int param_index, base::Time val) {
218 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
221 DCHECK(!run_called_) << __func__ << " must not be called after Run()";
222 DCHECK(!step_called_) << __func__ << " must not be called after Step()";
223 #endif // DCHECK_IS_ON()
228 DCHECK_GE(param_index, 0);
229 DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
230 << "Invalid parameter index";
231 int64_t int_value = val.ToDeltaSinceWindowsEpoch().InMicroseconds();
232 int sqlite_result_code =
233 sqlite3_bind_int64(ref_->stmt(), param_index + 1, int_value);
234 DCHECK_EQ(sqlite_result_code, SQLITE_OK);
237 void Statement::BindCString(int param_index, const char* val) {
238 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
241 DCHECK(!run_called_) << __func__ << " must not be called after Run()";
242 DCHECK(!step_called_) << __func__ << " must not be called after Step()";
243 #endif // DCHECK_IS_ON()
249 DCHECK_GE(param_index, 0);
250 DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
251 << "Invalid parameter index";
253 // If the string length is more than SQLITE_MAX_LENGTH (or the per-database
254 // SQLITE_LIMIT_LENGTH limit), sqlite3_bind_text() fails with SQLITE_TOOBIG.
256 // We're not currently handling this error. SQLITE_MAX_LENGTH is set to the
257 // default (1 billion bytes) in Chrome's SQLite build, so this is an unlilely
260 int sqlite_result_code = sqlite3_bind_text(ref_->stmt(), param_index + 1, val,
261 -1, SQLITE_TRANSIENT);
262 DCHECK_EQ(sqlite_result_code, SQLITE_OK);
265 void Statement::BindString(int param_index, base::StringPiece value) {
266 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
269 DCHECK(!run_called_) << __func__ << " must not be called after Run()";
270 DCHECK(!step_called_) << __func__ << " must not be called after Step()";
271 #endif // DCHECK_IS_ON()
276 DCHECK_GE(param_index, 0);
277 DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
278 << "Invalid parameter index";
280 // base::StringPiece::data() may return null for empty pieces. In particular,
281 // this may happen when the StringPiece is created from the default
284 // However, sqlite3_bind_text() always interprets a nullptr data argument as a
285 // NULL value, instead of an empty BLOB value.
286 static constexpr char kEmptyPlaceholder[] = {0x00};
287 const char* data = (value.size() > 0) ? value.data() : kEmptyPlaceholder;
289 // If the string length is more than SQLITE_MAX_LENGTH (or the per-database
290 // SQLITE_LIMIT_LENGTH limit), sqlite3_bind_text() fails with SQLITE_TOOBIG.
292 // We're not currently handling this error. SQLITE_MAX_LENGTH is set to the
293 // default (1 billion bytes) in Chrome's SQLite build, so this is an unlilely
296 int sqlite_result_code = sqlite3_bind_text(
297 ref_->stmt(), param_index + 1, data, value.size(), SQLITE_TRANSIENT);
298 DCHECK_EQ(sqlite_result_code, SQLITE_OK);
301 void Statement::BindString16(int param_index, base::StringPiece16 value) {
302 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
304 return BindString(param_index, base::UTF16ToUTF8(value));
307 void Statement::BindBlob(int param_index, base::span<const uint8_t> value) {
308 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
311 DCHECK(!run_called_) << __func__ << " must not be called after Run()";
312 DCHECK(!step_called_) << __func__ << " must not be called after Step()";
313 #endif // DCHECK_IS_ON()
318 DCHECK_GE(param_index, 0);
319 DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
320 << "Invalid parameter index";
322 // span::data() may return null for empty spans. In particular, this may
323 // happen when the span is created out of a std::vector, because
324 // std::vector::data() may (or may not) return null for empty vectors.
326 // However, sqlite3_bind_blob() always interprets a nullptr data argument as a
327 // NULL value, instead of an empty BLOB value.
329 // While the difference between NULL and an empty BLOB may not matter in some
330 // cases, it may also cause subtle bugs in other cases. So, we cannot pass
331 // span.data() directly to sqlite3_bind_blob().
332 static constexpr uint8_t kEmptyPlaceholder[] = {0x00};
333 const uint8_t* data = (value.size() > 0) ? value.data() : kEmptyPlaceholder;
335 // If the string length is more than SQLITE_MAX_LENGTH (or the per-database
336 // SQLITE_LIMIT_LENGTH limit), sqlite3_bind_text() fails with SQLITE_TOOBIG.
338 // We're not currently handling this error. SQLITE_MAX_LENGTH is set to the
339 // default (1 billion bytes) in Chrome's SQLite build, so this is an unlilely
342 int sqlite_result_code = sqlite3_bind_blob(
343 ref_->stmt(), param_index + 1, data, value.size(), SQLITE_TRANSIENT);
344 DCHECK_EQ(sqlite_result_code, SQLITE_OK);
347 int Statement::ColumnCount() const {
348 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
352 return sqlite3_column_count(ref_->stmt());
355 // Verify that our enum matches sqlite's values.
356 static_assert(static_cast<int>(ColumnType::kInteger) == SQLITE_INTEGER,
358 static_assert(static_cast<int>(ColumnType::kFloat) == SQLITE_FLOAT,
360 static_assert(static_cast<int>(ColumnType::kText) == SQLITE_TEXT,
362 static_assert(static_cast<int>(ColumnType::kBlob) == SQLITE_BLOB,
364 static_assert(static_cast<int>(ColumnType::kNull) == SQLITE_NULL,
367 ColumnType Statement::GetColumnType(int col) {
368 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
371 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
372 DCHECK(step_called_) << __func__ << " can only be used after Step()";
373 #endif // DCHECK_IS_ON()
375 return static_cast<enum ColumnType>(sqlite3_column_type(ref_->stmt(), col));
378 bool Statement::ColumnBool(int column_index) {
379 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
380 return static_cast<bool>(ColumnInt64(column_index));
383 int Statement::ColumnInt(int column_index) {
384 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
387 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
388 DCHECK(step_called_) << __func__ << " can only be used after Step()";
389 #endif // DCHECK_IS_ON()
393 DCHECK_GE(column_index, 0);
394 DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
395 << "Invalid column index";
397 return sqlite3_column_int(ref_->stmt(), column_index);
400 int64_t Statement::ColumnInt64(int column_index) {
401 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
404 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
405 DCHECK(step_called_) << __func__ << " can only be used after Step()";
406 #endif // DCHECK_IS_ON()
410 DCHECK_GE(column_index, 0);
411 DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
412 << "Invalid column index";
414 return sqlite3_column_int64(ref_->stmt(), column_index);
417 double Statement::ColumnDouble(int column_index) {
418 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
421 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
422 DCHECK(step_called_) << __func__ << " can only be used after Step()";
423 #endif // DCHECK_IS_ON()
427 DCHECK_GE(column_index, 0);
428 DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
429 << "Invalid column index";
431 return sqlite3_column_double(ref_->stmt(), column_index);
434 base::Time Statement::ColumnTime(int column_index) {
435 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
438 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
439 DCHECK(step_called_) << __func__ << " can only be used after Step()";
440 #endif // DCHECK_IS_ON()
444 DCHECK_GE(column_index, 0);
445 DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
446 << "Invalid column index";
448 int64_t int_value = sqlite3_column_int64(ref_->stmt(), column_index);
449 return base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(int_value));
452 std::string Statement::ColumnString(int column_index) {
453 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
456 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
457 DCHECK(step_called_) << __func__ << " can only be used after Step()";
458 #endif // DCHECK_IS_ON()
461 return std::string();
462 DCHECK_GE(column_index, 0);
463 DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
464 << "Invalid column index";
466 const char* string_buffer = reinterpret_cast<const char*>(
467 sqlite3_column_text(ref_->stmt(), column_index));
468 int size = sqlite3_column_bytes(ref_->stmt(), column_index);
471 if (string_buffer && size > 0)
472 result.assign(string_buffer, size);
476 std::u16string Statement::ColumnString16(int column_index) {
477 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
480 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
481 DCHECK(step_called_) << __func__ << " can only be used after Step()";
482 #endif // DCHECK_IS_ON()
485 return std::u16string();
486 DCHECK_GE(column_index, 0);
487 DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
488 << "Invalid column index";
490 std::string string = ColumnString(column_index);
491 return string.empty() ? std::u16string() : base::UTF8ToUTF16(string);
494 base::span<const uint8_t> Statement::ColumnBlob(int column_index) {
495 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
498 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
499 DCHECK(step_called_) << __func__ << " can only be used after Step()";
500 #endif // DCHECK_IS_ON()
503 return base::span<const uint8_t>();
504 DCHECK_GE(column_index, 0);
505 DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
506 << "Invalid column index";
508 int result_size = sqlite3_column_bytes(ref_->stmt(), column_index);
509 const void* result_buffer = sqlite3_column_blob(ref_->stmt(), column_index);
510 DCHECK(result_size == 0 || result_buffer != nullptr)
511 << "sqlite3_column_blob() returned a null buffer for a non-empty BLOB";
513 return base::make_span(static_cast<const uint8_t*>(result_buffer),
517 bool Statement::ColumnBlobAsString(int column_index, std::string* result) {
518 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
521 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
522 DCHECK(step_called_) << __func__ << " can only be used after Step()";
523 #endif // DCHECK_IS_ON()
527 DCHECK_GE(column_index, 0);
528 DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
529 << "Invalid column index";
531 const void* result_buffer = sqlite3_column_blob(ref_->stmt(), column_index);
532 int size = sqlite3_column_bytes(ref_->stmt(), column_index);
533 if (result_buffer && size > 0) {
534 result->assign(reinterpret_cast<const char*>(result_buffer), size);
541 bool Statement::ColumnBlobAsVector(int column_index,
542 std::vector<char>* result) {
543 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
546 DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
547 DCHECK(step_called_) << __func__ << " can only be used after Step()";
548 #endif // DCHECK_IS_ON()
552 DCHECK_GE(column_index, 0);
553 DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
554 << "Invalid column index";
556 const void* result_buffer = sqlite3_column_blob(ref_->stmt(), column_index);
557 int size = sqlite3_column_bytes(ref_->stmt(), column_index);
558 if (result_buffer && size > 0) {
559 // Unlike std::string, std::vector does not have an assign() overload that
560 // takes a buffer and a size.
561 result->assign(static_cast<const char*>(result_buffer),
562 static_cast<const char*>(result_buffer) + size);
569 bool Statement::ColumnBlobAsVector(int column_index,
570 std::vector<uint8_t>* result) {
571 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
573 return ColumnBlobAsVector(column_index,
574 reinterpret_cast<std::vector<char>*>(result));
577 std::string Statement::GetSQLStatement() {
578 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
580 // SQLite promises to keep the returned buffer alive until the statement is
581 // finalized. We immediately copy the buffer contents into a std::string so we
582 // don't need to worry about its lifetime. The performance overhead is
583 // acceptable because this method should only be invoked for logging details
584 // about SQLite errors.
586 // We use sqlite3_sql() instead of sqlite3_expanded_sql() because:
587 // - The returned SQL string matches the source code, making it easy to
589 // - This works with SQL statements that work with large data, such as BLOBS
591 // - The returned string is free of bound values, so it does not contain any
592 // PII that would raise privacy concerns around logging.
594 // Do not change this to use sqlite3_expanded_sql(). If that need ever arises
595 // in the future, make a new function instead listing the above caveats.
597 // See https://www.sqlite.org/c3ref/expanded_sql.html for more details on the
598 // difference between sqlite3_sql() and sqlite3_expanded_sql().
599 return sqlite3_sql(ref_->stmt());
602 SqliteResultCode Statement::CheckSqliteResultCode(
603 SqliteResultCode sqlite_result_code) {
604 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
606 succeeded_ = IsSqliteSuccessCode(sqlite_result_code);
607 if (!succeeded_ && ref_.get() && ref_->database()) {
608 auto sqlite_error_code = ToSqliteErrorCode(sqlite_result_code);
609 ref_->database()->OnSqliteError(sqlite_error_code, this, nullptr);
611 return sqlite_result_code;