[M120 Migration][VD] Fix url crash in RequestCertificateConfirm
[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::BindTimeDelta(int param_index, base::TimeDelta delta) {
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   if (!is_valid()) {
246     return;
247   }
248
249   DCHECK_GE(param_index, 0);
250   DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
251       << "Invalid parameter index";
252   int64_t int_value = delta.InMicroseconds();
253   int sqlite_result_code =
254       sqlite3_bind_int64(ref_->stmt(), param_index + 1, int_value);
255   DCHECK_EQ(sqlite_result_code, SQLITE_OK);
256 }
257
258 void Statement::BindCString(int param_index, const char* val) {
259   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
260
261 #if DCHECK_IS_ON()
262   DCHECK(!run_called_) << __func__ << " must not be called after Run()";
263   DCHECK(!step_called_) << __func__ << " must not be called after Step()";
264 #endif  // DCHECK_IS_ON()
265
266   DCHECK(val);
267   if (!is_valid())
268     return;
269
270   DCHECK_GE(param_index, 0);
271   DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
272       << "Invalid parameter index";
273
274   // If the string length is more than SQLITE_MAX_LENGTH (or the per-database
275   // SQLITE_LIMIT_LENGTH limit), sqlite3_bind_text() fails with SQLITE_TOOBIG.
276   //
277   // We're not currently handling this error. SQLITE_MAX_LENGTH is set to the
278   // default (1 billion bytes) in Chrome's SQLite build, so this is an unlilely
279   // issue.
280
281   int sqlite_result_code = sqlite3_bind_text(ref_->stmt(), param_index + 1, val,
282                                              -1, SQLITE_TRANSIENT);
283   DCHECK_EQ(sqlite_result_code, SQLITE_OK);
284 }
285
286 void Statement::BindString(int param_index, base::StringPiece value) {
287   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
288
289 #if DCHECK_IS_ON()
290   DCHECK(!run_called_) << __func__ << " must not be called after Run()";
291   DCHECK(!step_called_) << __func__ << " must not be called after Step()";
292 #endif  // DCHECK_IS_ON()
293
294   if (!is_valid())
295     return;
296
297   DCHECK_GE(param_index, 0);
298   DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
299       << "Invalid parameter index";
300
301   // base::StringPiece::data() may return null for empty pieces. In particular,
302   // this may happen when the StringPiece is created from the default
303   // constructor.
304   //
305   // However, sqlite3_bind_text() always interprets a nullptr data argument as a
306   // NULL value, instead of an empty BLOB value.
307   static constexpr char kEmptyPlaceholder[] = {0x00};
308   const char* data = (value.size() > 0) ? value.data() : kEmptyPlaceholder;
309
310   // If the string length is more than SQLITE_MAX_LENGTH (or the per-database
311   // SQLITE_LIMIT_LENGTH limit), sqlite3_bind_text() fails with SQLITE_TOOBIG.
312   //
313   // We're not currently handling this error. SQLITE_MAX_LENGTH is set to the
314   // default (1 billion bytes) in Chrome's SQLite build, so this is an unlilely
315   // issue.
316
317   int sqlite_result_code = sqlite3_bind_text(
318       ref_->stmt(), param_index + 1, data, value.size(), SQLITE_TRANSIENT);
319   DCHECK_EQ(sqlite_result_code, SQLITE_OK);
320 }
321
322 void Statement::BindString16(int param_index, base::StringPiece16 value) {
323   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
324
325   return BindString(param_index, base::UTF16ToUTF8(value));
326 }
327
328 void Statement::BindBlob(int param_index, base::span<const uint8_t> value) {
329   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
330
331 #if DCHECK_IS_ON()
332   DCHECK(!run_called_) << __func__ << " must not be called after Run()";
333   DCHECK(!step_called_) << __func__ << " must not be called after Step()";
334 #endif  // DCHECK_IS_ON()
335
336   if (!is_valid())
337     return;
338
339   DCHECK_GE(param_index, 0);
340   DCHECK_LT(param_index, sqlite3_bind_parameter_count(ref_->stmt()))
341       << "Invalid parameter index";
342
343   // span::data() may return null for empty spans. In particular, this may
344   // happen when the span is created out of a std::vector, because
345   // std::vector::data() may (or may not) return null for empty vectors.
346   //
347   // However, sqlite3_bind_blob() always interprets a nullptr data argument as a
348   // NULL value, instead of an empty BLOB value.
349   //
350   // While the difference between NULL and an empty BLOB may not matter in some
351   // cases, it may also cause subtle bugs in other cases. So, we cannot pass
352   // span.data() directly to sqlite3_bind_blob().
353   static constexpr uint8_t kEmptyPlaceholder[] = {0x00};
354   const uint8_t* data = (value.size() > 0) ? value.data() : kEmptyPlaceholder;
355
356   // If the string length is more than SQLITE_MAX_LENGTH (or the per-database
357   // SQLITE_LIMIT_LENGTH limit), sqlite3_bind_text() fails with SQLITE_TOOBIG.
358   //
359   // We're not currently handling this error. SQLITE_MAX_LENGTH is set to the
360   // default (1 billion bytes) in Chrome's SQLite build, so this is an unlilely
361   // issue.
362
363   int sqlite_result_code = sqlite3_bind_blob(
364       ref_->stmt(), param_index + 1, data, value.size(), SQLITE_TRANSIENT);
365   DCHECK_EQ(sqlite_result_code, SQLITE_OK);
366 }
367
368 int Statement::ColumnCount() const {
369   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
370
371   if (!is_valid())
372     return 0;
373   return sqlite3_column_count(ref_->stmt());
374 }
375
376 // Verify that our enum matches sqlite's values.
377 static_assert(static_cast<int>(ColumnType::kInteger) == SQLITE_INTEGER,
378               "INTEGER mismatch");
379 static_assert(static_cast<int>(ColumnType::kFloat) == SQLITE_FLOAT,
380               "FLOAT mismatch");
381 static_assert(static_cast<int>(ColumnType::kText) == SQLITE_TEXT,
382               "TEXT mismatch");
383 static_assert(static_cast<int>(ColumnType::kBlob) == SQLITE_BLOB,
384               "BLOB mismatch");
385 static_assert(static_cast<int>(ColumnType::kNull) == SQLITE_NULL,
386               "NULL mismatch");
387
388 ColumnType Statement::GetColumnType(int col) {
389   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
390
391 #if DCHECK_IS_ON()
392   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
393   DCHECK(step_called_) << __func__ << " can only be used after Step()";
394 #endif  // DCHECK_IS_ON()
395
396   return static_cast<enum ColumnType>(sqlite3_column_type(ref_->stmt(), col));
397 }
398
399 bool Statement::ColumnBool(int column_index) {
400   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
401   return static_cast<bool>(ColumnInt64(column_index));
402 }
403
404 int Statement::ColumnInt(int column_index) {
405   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
406
407 #if DCHECK_IS_ON()
408   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
409   DCHECK(step_called_) << __func__ << " can only be used after Step()";
410 #endif  // DCHECK_IS_ON()
411
412   if (!CheckValid())
413     return 0;
414   DCHECK_GE(column_index, 0);
415   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
416       << "Invalid column index";
417
418   return sqlite3_column_int(ref_->stmt(), column_index);
419 }
420
421 int64_t Statement::ColumnInt64(int column_index) {
422   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
423
424 #if DCHECK_IS_ON()
425   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
426   DCHECK(step_called_) << __func__ << " can only be used after Step()";
427 #endif  // DCHECK_IS_ON()
428
429   if (!CheckValid())
430     return 0;
431   DCHECK_GE(column_index, 0);
432   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
433       << "Invalid column index";
434
435   return sqlite3_column_int64(ref_->stmt(), column_index);
436 }
437
438 double Statement::ColumnDouble(int column_index) {
439   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
440
441 #if DCHECK_IS_ON()
442   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
443   DCHECK(step_called_) << __func__ << " can only be used after Step()";
444 #endif  // DCHECK_IS_ON()
445
446   if (!CheckValid())
447     return 0;
448   DCHECK_GE(column_index, 0);
449   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
450       << "Invalid column index";
451
452   return sqlite3_column_double(ref_->stmt(), column_index);
453 }
454
455 base::Time Statement::ColumnTime(int column_index) {
456   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
457
458 #if DCHECK_IS_ON()
459   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
460   DCHECK(step_called_) << __func__ << " can only be used after Step()";
461 #endif  // DCHECK_IS_ON()
462
463   if (!CheckValid())
464     return base::Time();
465   DCHECK_GE(column_index, 0);
466   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
467       << "Invalid column index";
468
469   int64_t int_value = sqlite3_column_int64(ref_->stmt(), column_index);
470   return base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(int_value));
471 }
472
473 base::TimeDelta Statement::ColumnTimeDelta(int column_index) {
474   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
475
476 #if DCHECK_IS_ON()
477   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
478   DCHECK(step_called_) << __func__ << " can only be used after Step()";
479 #endif  // DCHECK_IS_ON()
480
481   if (!CheckValid()) {
482     return base::TimeDelta();
483   }
484   DCHECK_GE(column_index, 0);
485   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
486       << "Invalid column index";
487
488   int64_t int_value = sqlite3_column_int64(ref_->stmt(), column_index);
489   return base::Microseconds(int_value);
490 }
491
492 std::string Statement::ColumnString(int column_index) {
493   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
494
495 #if DCHECK_IS_ON()
496   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
497   DCHECK(step_called_) << __func__ << " can only be used after Step()";
498 #endif  // DCHECK_IS_ON()
499
500   if (!CheckValid())
501     return std::string();
502   DCHECK_GE(column_index, 0);
503   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
504       << "Invalid column index";
505
506   const char* string_buffer = reinterpret_cast<const char*>(
507       sqlite3_column_text(ref_->stmt(), column_index));
508   int size = sqlite3_column_bytes(ref_->stmt(), column_index);
509
510   std::string result;
511   if (string_buffer && size > 0)
512     result.assign(string_buffer, size);
513   return result;
514 }
515
516 std::u16string Statement::ColumnString16(int column_index) {
517   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
518
519 #if DCHECK_IS_ON()
520   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
521   DCHECK(step_called_) << __func__ << " can only be used after Step()";
522 #endif  // DCHECK_IS_ON()
523
524   if (!CheckValid())
525     return std::u16string();
526   DCHECK_GE(column_index, 0);
527   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
528       << "Invalid column index";
529
530   std::string string = ColumnString(column_index);
531   return string.empty() ? std::u16string() : base::UTF8ToUTF16(string);
532 }
533
534 base::span<const uint8_t> Statement::ColumnBlob(int column_index) {
535   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
536
537 #if DCHECK_IS_ON()
538   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
539   DCHECK(step_called_) << __func__ << " can only be used after Step()";
540 #endif  // DCHECK_IS_ON()
541
542   if (!CheckValid())
543     return base::span<const uint8_t>();
544   DCHECK_GE(column_index, 0);
545   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
546       << "Invalid column index";
547
548   int result_size = sqlite3_column_bytes(ref_->stmt(), column_index);
549   const void* result_buffer = sqlite3_column_blob(ref_->stmt(), column_index);
550   DCHECK(result_size == 0 || result_buffer != nullptr)
551       << "sqlite3_column_blob() returned a null buffer for a non-empty BLOB";
552
553   return base::make_span(static_cast<const uint8_t*>(result_buffer),
554                          base::checked_cast<size_t>(result_size));
555 }
556
557 bool Statement::ColumnBlobAsString(int column_index, std::string* result) {
558   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
559
560 #if DCHECK_IS_ON()
561   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
562   DCHECK(step_called_) << __func__ << " can only be used after Step()";
563 #endif  // DCHECK_IS_ON()
564
565   if (!CheckValid())
566     return false;
567   DCHECK_GE(column_index, 0);
568   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
569       << "Invalid column index";
570
571   const void* result_buffer = sqlite3_column_blob(ref_->stmt(), column_index);
572   int size = sqlite3_column_bytes(ref_->stmt(), column_index);
573   if (result_buffer && size > 0) {
574     result->assign(reinterpret_cast<const char*>(result_buffer), size);
575   } else {
576     result->clear();
577   }
578   return true;
579 }
580
581 bool Statement::ColumnBlobAsString16(int column_index, std::u16string* result) {
582   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
583   DCHECK(result);
584
585 #if DCHECK_IS_ON()
586   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
587   DCHECK(step_called_) << __func__ << " can only be used after Step()";
588 #endif  // DCHECK_IS_ON()
589
590   if (!CheckValid()) {
591     return false;
592   }
593   DCHECK_GE(column_index, 0);
594   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
595       << "Invalid column index";
596
597   const void* result_buffer = sqlite3_column_blob(ref_->stmt(), column_index);
598   int size = sqlite3_column_bytes(ref_->stmt(), column_index);
599   if (result_buffer && size > 0) {
600     result->assign(reinterpret_cast<const char16_t*>(result_buffer), size / 2);
601   } else {
602     result->clear();
603   }
604   return true;
605 }
606
607 bool Statement::ColumnBlobAsVector(int column_index,
608                                    std::vector<char>* result) {
609   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
610
611 #if DCHECK_IS_ON()
612   DCHECK(!run_called_) << __func__ << " can be used after Step(), not Run()";
613   DCHECK(step_called_) << __func__ << " can only be used after Step()";
614 #endif  // DCHECK_IS_ON()
615
616   if (!CheckValid())
617     return false;
618   DCHECK_GE(column_index, 0);
619   DCHECK_LT(column_index, sqlite3_data_count(ref_->stmt()))
620       << "Invalid column index";
621
622   const void* result_buffer = sqlite3_column_blob(ref_->stmt(), column_index);
623   int size = sqlite3_column_bytes(ref_->stmt(), column_index);
624   if (result_buffer && size > 0) {
625     // Unlike std::string, std::vector does not have an assign() overload that
626     // takes a buffer and a size.
627     result->assign(static_cast<const char*>(result_buffer),
628                    static_cast<const char*>(result_buffer) + size);
629   } else {
630     result->clear();
631   }
632   return true;
633 }
634
635 bool Statement::ColumnBlobAsVector(int column_index,
636                                    std::vector<uint8_t>* result) {
637   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
638
639   return ColumnBlobAsVector(column_index,
640                             reinterpret_cast<std::vector<char>*>(result));
641 }
642
643 std::string Statement::GetSQLStatement() {
644   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
645
646   // SQLite promises to keep the returned buffer alive until the statement is
647   // finalized. We immediately copy the buffer contents into a std::string so we
648   // don't need to worry about its lifetime. The performance overhead is
649   // acceptable because this method should only be invoked for logging details
650   // about SQLite errors.
651   //
652   // We use sqlite3_sql() instead of sqlite3_expanded_sql() because:
653   //  - The returned SQL string matches the source code, making it easy to
654   //    search.
655   //  - This works with SQL statements that work with large data, such as BLOBS
656   //    storing images.
657   //  - The returned string is free of bound values, so it does not contain any
658   //    PII that would raise privacy concerns around logging.
659   //
660   // Do not change this to use sqlite3_expanded_sql(). If that need ever arises
661   // in the future, make a new function instead listing the above caveats.
662   //
663   // See https://www.sqlite.org/c3ref/expanded_sql.html for more details on the
664   // difference between sqlite3_sql() and sqlite3_expanded_sql().
665   return sqlite3_sql(ref_->stmt());
666 }
667
668 SqliteResultCode Statement::CheckSqliteResultCode(
669     SqliteResultCode sqlite_result_code) {
670   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
671
672   succeeded_ = IsSqliteSuccessCode(sqlite_result_code);
673   if (!succeeded_ && ref_.get() && ref_->database()) {
674     auto sqlite_error_code = ToSqliteErrorCode(sqlite_result_code);
675     ref_->database()->OnSqliteError(sqlite_error_code, this, nullptr);
676   }
677   return sqlite_result_code;
678 }
679
680 }  // namespace sql