1111
1212namespace impeller {
1313
14- #define STATEMENT_HANDLE reinterpret_cast <::sqlite3_stmt*>(statement_handle_)
15-
16- ArchiveStatement::ArchiveStatement (void * db, const std::string& statememt) {
17- ::sqlite3_stmt* statementHandle = nullptr ;
18- auto res = ::sqlite3_prepare_v2 (reinterpret_cast <sqlite3*>(db), //
19- statememt.c_str (), //
20- static_cast <int >(statememt.size ()), //
21- &statementHandle, //
22- nullptr );
23- statement_handle_ = statementHandle;
24- ready_ = res == SQLITE_OK && statement_handle_ != nullptr ;
25- }
26-
27- ArchiveStatement::ArchiveStatement (ArchiveStatement&& other)
28- : statement_handle_(other.statement_handle_), ready_(other.ready_) {
29- other.statement_handle_ = nullptr ;
30- other.ready_ = false ;
31- }
14+ struct ArchiveStatement ::Handle {
15+ Handle (void * db, const std::string& statememt) {
16+ ::sqlite3_stmt* handle = nullptr ;
17+ if (::sqlite3_prepare_v2 (reinterpret_cast <sqlite3*>(db), //
18+ statememt.c_str (), //
19+ static_cast <int >(statememt.size ()), //
20+ &handle, //
21+ nullptr ) == SQLITE_OK) {
22+ handle_ = handle;
23+ }
24+ }
3225
33- ArchiveStatement::~ArchiveStatement () {
34- if (statement_handle_ != nullptr ) {
35- auto res = ::sqlite3_finalize (STATEMENT_HANDLE);
26+ ~Handle () {
27+ if (handle_ == nullptr ) {
28+ return ;
29+ }
30+ auto res = ::sqlite3_finalize (handle_);
3631 FML_CHECK (res == SQLITE_OK) << " Unable to finalize the archive." ;
3732 }
33+
34+ bool IsValid () { return handle_ != nullptr ; }
35+
36+ ::sqlite3_stmt* Get () const { return handle_; }
37+
38+ private:
39+ ::sqlite3_stmt* handle_;
40+
41+ FML_DISALLOW_COPY_AND_ASSIGN (Handle);
42+ };
43+
44+ ArchiveStatement::ArchiveStatement (void * db, const std::string& statememt)
45+ : statement_handle_(std::make_unique<Handle>(db, statememt)) {
46+ if (!statement_handle_->IsValid ()) {
47+ statement_handle_.reset ();
48+ }
3849}
3950
51+ ArchiveStatement::~ArchiveStatement () = default ;
52+
4053bool ArchiveStatement::IsValid () const {
41- return ready_ ;
54+ return statement_handle_ != nullptr ;
4255}
4356
4457bool ArchiveStatement::Reset () {
45- if (::sqlite3_reset (STATEMENT_HANDLE ) != SQLITE_OK) {
58+ if (::sqlite3_reset (statement_handle_-> Get () ) != SQLITE_OK) {
4659 return false ;
4760 }
4861
49- if (::sqlite3_clear_bindings (STATEMENT_HANDLE ) != SQLITE_OK) {
62+ if (::sqlite3_clear_bindings (statement_handle_-> Get () ) != SQLITE_OK) {
5063 return false ;
5164 }
5265
@@ -68,34 +81,34 @@ static constexpr int ToColumn(size_t index) {
6881}
6982
7083size_t ArchiveStatement::GetColumnCount () {
71- return ::sqlite3_column_count (STATEMENT_HANDLE );
84+ return ::sqlite3_column_count (statement_handle_-> Get () );
7285}
7386
7487/*
7588 * Bind Variants
7689 */
7790bool ArchiveStatement::WriteValue (size_t index, const std::string& item) {
78- return ::sqlite3_bind_text (STATEMENT_HANDLE, //
91+ return ::sqlite3_bind_text (statement_handle_-> Get (), //
7992 ToParam (index), //
8093 item.data (), //
8194 static_cast <int >(item.size ()), //
8295 SQLITE_TRANSIENT) == SQLITE_OK;
8396}
8497
8598bool ArchiveStatement::BindIntegral (size_t index, int64_t item) {
86- return ::sqlite3_bind_int64 (STATEMENT_HANDLE , //
87- ToParam (index), //
99+ return ::sqlite3_bind_int64 (statement_handle_-> Get () , //
100+ ToParam (index), //
88101 item) == SQLITE_OK;
89102}
90103
91104bool ArchiveStatement::WriteValue (size_t index, double item) {
92- return ::sqlite3_bind_double (STATEMENT_HANDLE , //
93- ToParam (index), //
105+ return ::sqlite3_bind_double (statement_handle_-> Get () , //
106+ ToParam (index), //
94107 item) == SQLITE_OK;
95108}
96109
97110bool ArchiveStatement::WriteValue (size_t index, const Allocation& item) {
98- return ::sqlite3_bind_blob (STATEMENT_HANDLE, //
111+ return ::sqlite3_bind_blob (statement_handle_-> Get (), //
99112 ToParam (index), //
100113 item.GetBuffer (), //
101114 static_cast <int >(item.GetLength ()), //
@@ -106,12 +119,12 @@ bool ArchiveStatement::WriteValue(size_t index, const Allocation& item) {
106119 * Column Variants
107120 */
108121bool ArchiveStatement::ColumnIntegral (size_t index, int64_t & item) {
109- item = ::sqlite3_column_int64 (STATEMENT_HANDLE , ToColumn (index));
122+ item = ::sqlite3_column_int64 (statement_handle_-> Get () , ToColumn (index));
110123 return true ;
111124}
112125
113126bool ArchiveStatement::ReadValue (size_t index, double & item) {
114- item = ::sqlite3_column_double (STATEMENT_HANDLE , ToColumn (index));
127+ item = ::sqlite3_column_double (statement_handle_-> Get () , ToColumn (index));
115128 return true ;
116129}
117130
@@ -128,13 +141,13 @@ bool ArchiveStatement::ReadValue(size_t index, std::string& item) {
128141 * Get the character data
129142 */
130143 auto chars = reinterpret_cast <const char *>(
131- ::sqlite3_column_text (STATEMENT_HANDLE , ToColumn(index)));
144+ ::sqlite3_column_text (statement_handle_-> Get () , ToColumn(index)));
132145
133146 /*
134147 * Get the length of the string (in bytes)
135148 */
136149 size_t textByteSize =
137- ::sqlite3_column_bytes (STATEMENT_HANDLE , ToColumn(index));
150+ ::sqlite3_column_bytes (statement_handle_-> Get () , ToColumn(index));
138151
139152 std::string text (chars, textByteSize);
140153 item.swap(text);
@@ -147,12 +160,13 @@ bool ArchiveStatement::ReadValue(size_t index, Allocation& item) {
147160 * Get a blob pointer
148161 */
149162 auto blob = reinterpret_cast <const uint8_t *>(
150- ::sqlite3_column_blob (STATEMENT_HANDLE , ToColumn(index)));
163+ ::sqlite3_column_blob (statement_handle_-> Get () , ToColumn(index)));
151164
152165 /*
153166 * Decode the number of bytes in the blob
154167 */
155- size_t byteSize = ::sqlite3_column_bytes (STATEMENT_HANDLE, ToColumn (index));
168+ size_t byteSize =
169+ ::sqlite3_column_bytes (statement_handle_->Get (), ToColumn(index));
156170
157171 /*
158172 * Reszie the host allocation and move the blob contents into it
@@ -166,7 +180,7 @@ bool ArchiveStatement::ReadValue(size_t index, Allocation& item) {
166180}
167181
168182ArchiveStatement::Result ArchiveStatement::Execute () {
169- switch (::sqlite3_step (STATEMENT_HANDLE )) {
183+ switch (::sqlite3_step (statement_handle_-> Get () )) {
170184 case SQLITE_DONE:
171185 return Result::kDone ;
172186 case SQLITE_ROW:
0 commit comments