11
11
12
12
namespace impeller {
13
13
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
+ }
32
25
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_);
36
31
FML_CHECK (res == SQLITE_OK) << " Unable to finalize the archive." ;
37
32
}
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
+ }
38
49
}
39
50
51
+ ArchiveStatement::~ArchiveStatement () = default ;
52
+
40
53
bool ArchiveStatement::IsValid () const {
41
- return ready_ ;
54
+ return statement_handle_ != nullptr ;
42
55
}
43
56
44
57
bool ArchiveStatement::Reset () {
45
- if (::sqlite3_reset (STATEMENT_HANDLE ) != SQLITE_OK) {
58
+ if (::sqlite3_reset (statement_handle_-> Get () ) != SQLITE_OK) {
46
59
return false ;
47
60
}
48
61
49
- if (::sqlite3_clear_bindings (STATEMENT_HANDLE ) != SQLITE_OK) {
62
+ if (::sqlite3_clear_bindings (statement_handle_-> Get () ) != SQLITE_OK) {
50
63
return false ;
51
64
}
52
65
@@ -68,34 +81,34 @@ static constexpr int ToColumn(size_t index) {
68
81
}
69
82
70
83
size_t ArchiveStatement::GetColumnCount () {
71
- return ::sqlite3_column_count (STATEMENT_HANDLE );
84
+ return ::sqlite3_column_count (statement_handle_-> Get () );
72
85
}
73
86
74
87
/*
75
88
* Bind Variants
76
89
*/
77
90
bool ArchiveStatement::WriteValue (size_t index, const std::string& item) {
78
- return ::sqlite3_bind_text (STATEMENT_HANDLE, //
91
+ return ::sqlite3_bind_text (statement_handle_-> Get (), //
79
92
ToParam (index ), //
80
93
item.data (), //
81
94
static_cast <int >(item.size ()), //
82
95
SQLITE_TRANSIENT) == SQLITE_OK;
83
96
}
84
97
85
98
bool 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 ), //
88
101
item) == SQLITE_OK;
89
102
}
90
103
91
104
bool 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 ), //
94
107
item) == SQLITE_OK;
95
108
}
96
109
97
110
bool ArchiveStatement::WriteValue (size_t index, const Allocation& item) {
98
- return ::sqlite3_bind_blob (STATEMENT_HANDLE, //
111
+ return ::sqlite3_bind_blob (statement_handle_-> Get (), //
99
112
ToParam (index ), //
100
113
item.GetBuffer (), //
101
114
static_cast <int >(item.GetLength ()), //
@@ -106,12 +119,12 @@ bool ArchiveStatement::WriteValue(size_t index, const Allocation& item) {
106
119
* Column Variants
107
120
*/
108
121
bool 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 ));
110
123
return true ;
111
124
}
112
125
113
126
bool 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 ));
115
128
return true ;
116
129
}
117
130
@@ -128,13 +141,13 @@ bool ArchiveStatement::ReadValue(size_t index, std::string& item) {
128
141
* Get the character data
129
142
*/
130
143
auto chars = reinterpret_cast <const char *>(
131
- ::sqlite3_column_text (STATEMENT_HANDLE , ToColumn(index)));
144
+ ::sqlite3_column_text (statement_handle_-> Get () , ToColumn(index)));
132
145
133
146
/*
134
147
* Get the length of the string (in bytes)
135
148
*/
136
149
size_t textByteSize =
137
- ::sqlite3_column_bytes (STATEMENT_HANDLE , ToColumn(index));
150
+ ::sqlite3_column_bytes (statement_handle_-> Get () , ToColumn(index));
138
151
139
152
std::string text (chars, textByteSize);
140
153
item.swap(text);
@@ -147,12 +160,13 @@ bool ArchiveStatement::ReadValue(size_t index, Allocation& item) {
147
160
* Get a blob pointer
148
161
*/
149
162
auto blob = reinterpret_cast <const uint8_t *>(
150
- ::sqlite3_column_blob (STATEMENT_HANDLE , ToColumn(index)));
163
+ ::sqlite3_column_blob (statement_handle_-> Get () , ToColumn(index)));
151
164
152
165
/*
153
166
* Decode the number of bytes in the blob
154
167
*/
155
- size_t byteSize = ::sqlite3_column_bytes (STATEMENT_HANDLE, ToColumn (index ));
168
+ size_t byteSize =
169
+ ::sqlite3_column_bytes (statement_handle_->Get (), ToColumn(index));
156
170
157
171
/*
158
172
* Reszie the host allocation and move the blob contents into it
@@ -166,7 +180,7 @@ bool ArchiveStatement::ReadValue(size_t index, Allocation& item) {
166
180
}
167
181
168
182
ArchiveStatement::Result ArchiveStatement::Execute () {
169
- switch (::sqlite3_step (STATEMENT_HANDLE )) {
183
+ switch (::sqlite3_step (statement_handle_-> Get () )) {
170
184
case SQLITE_DONE:
171
185
return Result::kDone ;
172
186
case SQLITE_ROW:
0 commit comments