1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/db/SqliteStatement.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/StackAllocator.h"
#include "td/utils/StringBuilder.h"
#include "sqlite/sqlite3.h"
namespace td {
int VERBOSITY_NAME(sqlite) = VERBOSITY_NAME(DEBUG) + 10;
namespace {
int printExplainQueryPlan(StringBuilder &sb, tdsqlite3_stmt *pStmt) {
const char *zSql = tdsqlite3_sql(pStmt);
if (zSql == nullptr) {
return SQLITE_ERROR;
}
sb << "Explain query " << zSql;
char *zExplain = tdsqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
if (zExplain == nullptr) {
return SQLITE_NOMEM;
}
tdsqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
int rc = tdsqlite3_prepare_v2(tdsqlite3_db_handle(pStmt), zExplain, -1, &pExplain, nullptr);
tdsqlite3_free(zExplain);
if (rc != SQLITE_OK) {
return rc;
}
while (SQLITE_ROW == tdsqlite3_step(pExplain)) {
int iSelectid = tdsqlite3_column_int(pExplain, 0);
int iOrder = tdsqlite3_column_int(pExplain, 1);
int iFrom = tdsqlite3_column_int(pExplain, 2);
const char *zDetail = reinterpret_cast<const char *>(tdsqlite3_column_text(pExplain, 3));
sb << '\n' << iSelectid << ' ' << iOrder << ' ' << iFrom << ' ' << zDetail;
}
return tdsqlite3_finalize(pExplain);
}
} // namespace
SqliteStatement::SqliteStatement(tdsqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db)
: stmt_(stmt), db_(std::move(db)) {
CHECK(stmt != nullptr);
}
SqliteStatement::~SqliteStatement() = default;
Result<string> SqliteStatement::explain() {
if (empty()) {
return Status::Error("No statement");
}
auto tmp = StackAllocator::alloc(10000);
StringBuilder sb(tmp.as_slice());
auto code = printExplainQueryPlan(sb, stmt_.get());
if (code != SQLITE_OK) {
return last_error();
}
if (sb.is_error()) {
return Status::Error("StringBuilder buffer overflow");
}
return sb.as_cslice().str();
}
Status SqliteStatement::bind_blob(int id, Slice blob) {
auto rc = tdsqlite3_bind_blob(stmt_.get(), id, blob.data(), static_cast<int>(blob.size()), nullptr);
if (rc != SQLITE_OK) {
return last_error();
}
return Status::OK();
}
Status SqliteStatement::bind_string(int id, Slice str) {
auto rc = tdsqlite3_bind_text(stmt_.get(), id, str.data(), static_cast<int>(str.size()), nullptr);
if (rc != SQLITE_OK) {
return last_error();
}
return Status::OK();
}
Status SqliteStatement::bind_int32(int id, int32 value) {
auto rc = tdsqlite3_bind_int(stmt_.get(), id, value);
if (rc != SQLITE_OK) {
return last_error();
}
return Status::OK();
}
Status SqliteStatement::bind_int64(int id, int64 value) {
auto rc = tdsqlite3_bind_int64(stmt_.get(), id, value);
if (rc != SQLITE_OK) {
return last_error();
}
return Status::OK();
}
Status SqliteStatement::bind_null(int id) {
auto rc = tdsqlite3_bind_null(stmt_.get(), id);
if (rc != SQLITE_OK) {
return last_error();
}
return Status::OK();
}
StringBuilder &operator<<(StringBuilder &sb, SqliteStatement::Datatype type) {
using Datatype = SqliteStatement::Datatype;
switch (type) {
case Datatype::Integer:
return sb << "Integer";
case Datatype::Float:
return sb << "Float";
case Datatype::Blob:
return sb << "Blob";
case Datatype::Null:
return sb << "Null";
case Datatype::Text:
return sb << "Text";
}
UNREACHABLE();
return sb;
}
Slice SqliteStatement::view_blob(int id) {
LOG_IF(ERROR, view_datatype(id) != Datatype::Blob) << view_datatype(id);
auto *data = tdsqlite3_column_blob(stmt_.get(), id);
auto size = tdsqlite3_column_bytes(stmt_.get(), id);
if (data == nullptr) {
return Slice();
}
return Slice(static_cast<const char *>(data), size);
}
Slice SqliteStatement::view_string(int id) {
LOG_IF(ERROR, view_datatype(id) != Datatype::Text) << view_datatype(id);
auto *data = tdsqlite3_column_text(stmt_.get(), id);
auto size = tdsqlite3_column_bytes(stmt_.get(), id);
if (data == nullptr) {
return Slice();
}
return Slice(data, size);
}
int32 SqliteStatement::view_int32(int id) {
LOG_IF(ERROR, view_datatype(id) != Datatype::Integer) << view_datatype(id);
return tdsqlite3_column_int(stmt_.get(), id);
}
int64 SqliteStatement::view_int64(int id) {
LOG_IF(ERROR, view_datatype(id) != Datatype::Integer) << view_datatype(id);
return tdsqlite3_column_int64(stmt_.get(), id);
}
SqliteStatement::Datatype SqliteStatement::view_datatype(int id) {
auto type = tdsqlite3_column_type(stmt_.get(), id);
switch (type) {
case SQLITE_INTEGER:
return Datatype::Integer;
case SQLITE_FLOAT:
return Datatype::Float;
case SQLITE_BLOB:
return Datatype::Blob;
case SQLITE_NULL:
return Datatype::Null;
case SQLITE3_TEXT:
return Datatype::Text;
default:
UNREACHABLE();
}
}
void SqliteStatement::reset() {
tdsqlite3_reset(stmt_.get());
state_ = State::Start;
}
Status SqliteStatement::step() {
if (state_ == State::Finish) {
return Status::Error("One has to reset statement");
}
VLOG(sqlite) << "Start step " << tag("query", tdsqlite3_sql(stmt_.get())) << tag("statement", stmt_.get())
<< tag("database", db_.get());
auto rc = tdsqlite3_step(stmt_.get());
VLOG(sqlite) << "Finish step with response " << (rc == SQLITE_ROW ? "ROW" : (rc == SQLITE_DONE ? "DONE" : "ERROR"));
if (rc == SQLITE_ROW) {
state_ = State::HaveRow;
return Status::OK();
}
state_ = State::Finish;
if (rc == SQLITE_DONE) {
return Status::OK();
}
return last_error();
}
void SqliteStatement::StmtDeleter::operator()(tdsqlite3_stmt *stmt) {
tdsqlite3_finalize(stmt);
}
Status SqliteStatement::last_error() {
return db_->last_error();
}
} // namespace td
|