[M108 Migration][HBBTV] Implement ewk_context_register_jsplugin_mime_types API
[platform/framework/web/chromium-efl.git] / sql / statement.cc
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.
4
5 #include "sql/statement.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
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"
22
23 namespace sql {
24
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,
30                                                         nullptr,
31                                                         false)) {}
32
33 Statement::Statement(scoped_refptr<Database::StatementRef> ref)
34     : ref_(std::move(ref)) {}
35
36 Statement::~Statement() {
37   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
38
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.
42   Reset(true);
43 }
44
45 void Statement::Assign(scoped_refptr<Database::StatementRef> ref) {
46   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
47
48   Reset(true);
49   ref_ = std::move(ref);
50 }
51
52 void Statement::Clear() {
53   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
54
55   Assign(base::MakeRefCounted<Database::StatementRef>(nullptr, nullptr, false));
56   succeeded_ = false;
57 }
58
59 bool Statement::CheckValid() const {
60   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
61
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.";
66   return is_valid();
67 }
68
69 SqliteResultCode Statement::StepInternal() {
70   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
71
72   if (!CheckValid())
73     return SqliteResultCode::kError;
74
75   absl::optional<base::ScopedBlockingCall> scoped_blocking_call;
76   ref_->InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
77
78   auto sqlite_result_code = ToSqliteResultCode(sqlite3_step(ref_->stmt()));
79   return CheckSqliteResultCode(sqlite_result_code);
80 }
81
82 bool Statement::Run() {
83   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
84
85 #if DCHECK_IS_ON()
86   DCHECK(!run_called_) << "Run() must be called exactly once";
87   run_called_ = true;
88   DCHECK(!step_called_) << "Run() must not be mixed with Step()";
89 #endif  // DCHECK_IS_ON()
90   return StepInternal() == SqliteResultCode::kDone;
91 }
92
93 bool Statement::Step() {
94   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
95
96 #if DCHECK_IS_ON()
97   DCHECK(!run_called_) << "Run() must not be mixed with Step()";
98   step_called_ = true;
99 #endif  // DCHECK_IS_ON()
100   return StepInternal() == SqliteResultCode::kRow;
101 }
102
103 void Statement::Reset(bool clear_bound_vars) {
104   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
105
106   absl::optional<base::ScopedBlockingCall> scoped_blocking_call;
107   ref_->InitScopedBlockingCall(FROM_HERE, &scoped_blocking_call);
108   if (is_valid()) {
109     if (clear_bound_vars)
110       sqlite3_clear_bindings(ref_->stmt());
111
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
115     // checked.
116     sqlite3_reset(ref_->stmt());
117   }
118
119   // Potentially release dirty cache pages if an autocommit statement made
120   // changes.
121   if (ref_->database())
122     ref_->database()->ReleaseCacheMemoryIfNeeded(false);
123
124   succeeded_ = false;
125 #if DCHECK_IS_ON()
126   run_called_ = false;
127   step_called_ = false;
128 #endif  // DCHECK_IS_ON()
129 }
130
131 bool Statement::Succeeded() const {
132   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
133
134   return is_valid() && succeeded_;
135 }
136
137 void Statement::BindNull(int param_index) {
138   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
139
140 #if DCHECK_IS_ON()
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()
144
145   if (!is_valid())
146     return;
147
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);
153 }
154
155 void Statement::BindBool(int param_index, bool val) {
156   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
157
158   return BindInt64(param_index, val ? 1 : 0);
159 }
160
161 void Statement::BindInt(int param_index, int val) {
162   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
163
164 #if DCHECK_IS_ON()
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()
168
169   if (!is_valid())
170     return;
171
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);
177 }
178
179 void Statement::BindInt64(int param_index, int64_t val) {
180   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
181
182 #if DCHECK_IS_ON()
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()
186
187   if (!is_valid())
188     return;
189
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);
196 }
197
198 void Statement::BindDouble(int param_index, double val) {
199   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
200
201 #if DCHECK_IS_ON()
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()
205
206   if (!is_valid())
207     return;
208
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);
215 }
216
217 void Statement::BindTime(int param_index, base::Time val) {
218   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
219
220 #if DCHECK_IS_ON()
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()
224
225   if (!is_valid())
226     return;
227
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);
235 }
236
237 void Statement::BindCString(int param_index, const char* val) {
238   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
239
240 #if DCHECK_IS_ON()
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()
244
245   DCHECK(val);
246   if (!is_valid())
247     return;
248
249   DCHECK_GE(param_index, 0);
250   DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
251       << "Invalid parameter index";
252
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.
255   //
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
258   // issue.
259
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);
263 }
264
265 void Statement::BindString(int param_index, base::StringPiece value) {
266   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
267
268 #if DCHECK_IS_ON()
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()
272
273   if (!is_valid())
274     return;
275
276   DCHECK_GE(param_index, 0);
277   DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
278       << "Invalid parameter index";
279
280   // base::StringPiece::data() may return null for empty pieces. In particular,
281   // this may happen when the StringPiece is created from the default
282   // constructor.
283   //
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;
288
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.
291   //
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
294   // issue.
295
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);
299 }
300
301 void Statement::BindString16(int param_index, base::StringPiece16 value) {
302   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
303
304   return BindString(param_index, base::UTF16ToUTF8(value));
305 }
306
307 void Statement::BindBlob(int param_index, base::span<const uint8_t> value) {
308   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
309
310 #if DCHECK_IS_ON()
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()
314
315   if (!is_valid())
316     return;
317
318   DCHECK_GE(param_index, 0);
319   DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
320       << "Invalid parameter index";
321
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.
325   //
326   // However, sqlite3_bind_blob() always interprets a nullptr data argument as a
327   // NULL value, instead of an empty BLOB value.
328   //
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;
334
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.
337   //
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
340   // issue.
341
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);
345 }
346
347 int Statement::ColumnCount() const {
348   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
349
350   if (!is_valid())
351     return 0;
352   return sqlite3_column_count(ref_->stmt());
353 }
354
355 // Verify that our enum matches sqlite's values.
356 static_assert(static_cast<int>(ColumnType::kInteger) == SQLITE_INTEGER,
357               "INTEGER mismatch");
358 static_assert(static_cast<int>(ColumnType::kFloat) == SQLITE_FLOAT,
359               "FLOAT mismatch");
360 static_assert(static_cast<int>(ColumnType::kText) == SQLITE_TEXT,
361               "TEXT mismatch");
362 static_assert(static_cast<int>(ColumnType::kBlob) == SQLITE_BLOB,
363               "BLOB mismatch");
364 static_assert(static_cast<int>(ColumnType::kNull) == SQLITE_NULL,
365               "NULL mismatch");
366
367 ColumnType Statement::GetColumnType(int col) {
368   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
369
370 #if DCHECK_IS_ON()
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()
374
375   return static_cast<enum ColumnType>(sqlite3_column_type(ref_->stmt(), col));
376 }
377
378 bool Statement::ColumnBool(int column_index) {
379   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
380   return static_cast<bool>(ColumnInt64(column_index));
381 }
382
383 int Statement::ColumnInt(int column_index) {
384   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
385
386 #if DCHECK_IS_ON()
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()
390
391   if (!CheckValid())
392     return 0;
393   DCHECK_GE(column_index, 0);
394   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
395       << "Invalid column index";
396
397   return sqlite3_column_int(ref_->stmt(), column_index);
398 }
399
400 int64_t Statement::ColumnInt64(int column_index) {
401   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
402
403 #if DCHECK_IS_ON()
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()
407
408   if (!CheckValid())
409     return 0;
410   DCHECK_GE(column_index, 0);
411   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
412       << "Invalid column index";
413
414   return sqlite3_column_int64(ref_->stmt(), column_index);
415 }
416
417 double Statement::ColumnDouble(int column_index) {
418   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
419
420 #if DCHECK_IS_ON()
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()
424
425   if (!CheckValid())
426     return 0;
427   DCHECK_GE(column_index, 0);
428   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
429       << "Invalid column index";
430
431   return sqlite3_column_double(ref_->stmt(), column_index);
432 }
433
434 base::Time Statement::ColumnTime(int column_index) {
435   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
436
437 #if DCHECK_IS_ON()
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()
441
442   if (!CheckValid())
443     return base::Time();
444   DCHECK_GE(column_index, 0);
445   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
446       << "Invalid column index";
447
448   int64_t int_value = sqlite3_column_int64(ref_->stmt(), column_index);
449   return base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(int_value));
450 }
451
452 std::string Statement::ColumnString(int column_index) {
453   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
454
455 #if DCHECK_IS_ON()
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()
459
460   if (!CheckValid())
461     return std::string();
462   DCHECK_GE(column_index, 0);
463   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
464       << "Invalid column index";
465
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);
469
470   std::string result;
471   if (string_buffer && size > 0)
472     result.assign(string_buffer, size);
473   return result;
474 }
475
476 std::u16string Statement::ColumnString16(int column_index) {
477   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
478
479 #if DCHECK_IS_ON()
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()
483
484   if (!CheckValid())
485     return std::u16string();
486   DCHECK_GE(column_index, 0);
487   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
488       << "Invalid column index";
489
490   std::string string = ColumnString(column_index);
491   return string.empty() ? std::u16string() : base::UTF8ToUTF16(string);
492 }
493
494 base::span<const uint8_t> Statement::ColumnBlob(int column_index) {
495   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
496
497 #if DCHECK_IS_ON()
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()
501
502   if (!CheckValid())
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";
507
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";
512
513   return base::make_span(static_cast<const uint8_t*>(result_buffer),
514                          result_size);
515 }
516
517 bool Statement::ColumnBlobAsString(int column_index, std::string* result) {
518   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
519
520 #if DCHECK_IS_ON()
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()
524
525   if (!CheckValid())
526     return false;
527   DCHECK_GE(column_index, 0);
528   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
529       << "Invalid column index";
530
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);
535   } else {
536     result->clear();
537   }
538   return true;
539 }
540
541 bool Statement::ColumnBlobAsVector(int column_index,
542                                    std::vector<char>* result) {
543   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
544
545 #if DCHECK_IS_ON()
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()
549
550   if (!CheckValid())
551     return false;
552   DCHECK_GE(column_index, 0);
553   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
554       << "Invalid column index";
555
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);
563   } else {
564     result->clear();
565   }
566   return true;
567 }
568
569 bool Statement::ColumnBlobAsVector(int column_index,
570                                    std::vector<uint8_t>* result) {
571   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
572
573   return ColumnBlobAsVector(column_index,
574                             reinterpret_cast<std::vector<char>*>(result));
575 }
576
577 std::string Statement::GetSQLStatement() {
578   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
579
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.
585   //
586   // We use sqlite3_sql() instead of sqlite3_expanded_sql() because:
587   //  - The returned SQL string matches the source code, making it easy to
588   //    search.
589   //  - This works with SQL statements that work with large data, such as BLOBS
590   //    storing images.
591   //  - The returned string is free of bound values, so it does not contain any
592   //    PII that would raise privacy concerns around logging.
593   //
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.
596   //
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());
600 }
601
602 SqliteResultCode Statement::CheckSqliteResultCode(
603     SqliteResultCode sqlite_result_code) {
604   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
605
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);
610   }
611   return sqlite_result_code;
612 }
613
614 }  // namespace sql