9 #include "flutter/fml/logging.h"
10 #include "third_party/sqlite/sqlite3.h"
15 Handle(
void* db,
const std::string& statememt) {
19 ::sqlite3_stmt* handle =
nullptr;
20 if (::sqlite3_prepare_v2(
reinterpret_cast<sqlite3*
>(db),
22 static_cast<int>(statememt.size()),
24 nullptr) == SQLITE_OK) {
30 if (handle_ ==
nullptr) {
33 auto res = ::sqlite3_finalize(handle_);
34 FML_CHECK(res == SQLITE_OK) <<
"Unable to finalize the archive.";
37 bool IsValid()
const {
return handle_ !=
nullptr; }
39 ::sqlite3_stmt*
Get()
const {
return handle_; }
42 ::sqlite3_stmt* handle_ =
nullptr;
49 ArchiveStatement::ArchiveStatement(
void* db,
const std::string& statememt)
50 : statement_handle_(
std::make_unique<Handle>(db, statememt)) {
51 if (!statement_handle_->IsValid()) {
52 statement_handle_.reset();
59 return statement_handle_ !=
nullptr;
66 if (::sqlite3_reset(statement_handle_->Get()) != SQLITE_OK) {
70 if (::sqlite3_clear_bindings(statement_handle_->Get()) != SQLITE_OK) {
77 static constexpr
int ToParam(
size_t index) {
81 return static_cast<int>(index + 1);
88 return static_cast<int>(index);
95 return ::sqlite3_column_count(statement_handle_->Get());
105 return ::sqlite3_bind_text(statement_handle_->Get(),
108 static_cast<int>(item.size()),
109 SQLITE_TRANSIENT) == SQLITE_OK;
112 bool ArchiveStatement::BindIntegral(
size_t index, int64_t item) {
116 return ::sqlite3_bind_int64(statement_handle_->Get(),
125 return ::sqlite3_bind_double(statement_handle_->Get(),
134 return ::sqlite3_bind_blob(statement_handle_->Get(),
138 SQLITE_TRANSIENT) == SQLITE_OK;
144 bool ArchiveStatement::ColumnIntegral(
size_t index, int64_t& item) {
148 item = ::sqlite3_column_int64(statement_handle_->Get(),
ToColumn(index));
156 item = ::sqlite3_column_double(statement_handle_->Get(),
ToColumn(index));
175 auto chars =
reinterpret_cast<const char*
>(
176 ::sqlite3_column_text(statement_handle_->Get(),
ToColumn(index)));
181 size_t textByteSize =
182 ::sqlite3_column_bytes(statement_handle_->Get(),
ToColumn(index));
184 std::string text(chars, textByteSize);
197 auto blob =
reinterpret_cast<const uint8_t*
>(
198 ::sqlite3_column_blob(statement_handle_->Get(),
ToColumn(index)));
204 ::sqlite3_column_bytes(statement_handle_->Get(),
ToColumn(index));
209 if (!item.
Truncate(byteSize,
false )) {
213 memmove(item.
GetBuffer(), blob, byteSize);
221 switch (::sqlite3_step(statement_handle_->Get())) {