Move SQLite to namespace tdsqlite.
This commit is contained in:
parent
29460cacc8
commit
110ef61861
@ -19,6 +19,8 @@ set(SQLITE_SOURCE
|
||||
sqlite/sqlite3session.h
|
||||
)
|
||||
|
||||
# all SQLite functions are moved to namespace tdsqlite3 by `sed -Ebi 's/sqlite3([^.]|$)/td&/g' *`
|
||||
|
||||
add_library(tdsqlite STATIC ${SQLITE_SOURCE})
|
||||
target_include_directories(tdsqlite PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
|
||||
target_include_directories(tdsqlite SYSTEM PRIVATE ${OPENSSL_INCLUDE_DIR})
|
||||
|
50388
sqlite/sqlite/sqlite3.c
vendored
50388
sqlite/sqlite/sqlite3.c
vendored
File diff suppressed because it is too large
Load Diff
4806
sqlite/sqlite/sqlite3.h
vendored
4806
sqlite/sqlite/sqlite3.h
vendored
File diff suppressed because it is too large
Load Diff
966
sqlite/sqlite/sqlite3ext.h
vendored
966
sqlite/sqlite/sqlite3ext.h
vendored
File diff suppressed because it is too large
Load Diff
472
sqlite/sqlite/sqlite3session.h
vendored
472
sqlite/sqlite/sqlite3session.h
vendored
File diff suppressed because it is too large
Load Diff
@ -84,16 +84,16 @@ Status SqliteDb::init(CSlice path, bool allow_creation) {
|
||||
TRY_STATUS(destroy(path));
|
||||
}
|
||||
|
||||
sqlite3 *db;
|
||||
CHECK(sqlite3_threadsafe() != 0);
|
||||
tdsqlite3 *db;
|
||||
CHECK(tdsqlite3_threadsafe() != 0);
|
||||
int rc =
|
||||
sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | (allow_creation ? SQLITE_OPEN_CREATE : 0), nullptr);
|
||||
tdsqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | (allow_creation ? SQLITE_OPEN_CREATE : 0), nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
auto res = detail::RawSqliteDb::last_error(db, path);
|
||||
sqlite3_close(db);
|
||||
tdsqlite3_close(db);
|
||||
return res;
|
||||
}
|
||||
sqlite3_busy_timeout(db, 1000 * 5 /* 5 seconds */);
|
||||
tdsqlite3_busy_timeout(db, 1000 * 5 /* 5 seconds */);
|
||||
raw_ = std::make_shared<detail::RawSqliteDb>(db, path.str());
|
||||
return Status::OK();
|
||||
}
|
||||
@ -108,14 +108,14 @@ static int trace_v2_callback(unsigned code, void *ctx, void *p_raw, void *x_raw)
|
||||
if (x[0] == '-' && x[1] == '-') {
|
||||
trace_callback(ctx, x);
|
||||
} else {
|
||||
trace_callback(ctx, sqlite3_expanded_sql(static_cast<sqlite3_stmt *>(p_raw)));
|
||||
trace_callback(ctx, tdsqlite3_expanded_sql(static_cast<tdsqlite3_stmt *>(p_raw)));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SqliteDb::trace(bool flag) {
|
||||
sqlite3_trace_v2(raw_->db(), SQLITE_TRACE_STMT, flag ? trace_v2_callback : nullptr, nullptr);
|
||||
tdsqlite3_trace_v2(raw_->db(), SQLITE_TRACE_STMT, flag ? trace_v2_callback : nullptr, nullptr);
|
||||
}
|
||||
|
||||
Status SqliteDb::exec(CSlice cmd) {
|
||||
@ -124,7 +124,7 @@ Status SqliteDb::exec(CSlice cmd) {
|
||||
if (enable_logging_) {
|
||||
VLOG(sqlite) << "Start exec " << tag("query", cmd) << tag("database", raw_->db());
|
||||
}
|
||||
auto rc = sqlite3_exec(raw_->db(), cmd.c_str(), nullptr, nullptr, &msg);
|
||||
auto rc = tdsqlite3_exec(raw_->db(), cmd.c_str(), nullptr, nullptr, &msg);
|
||||
if (enable_logging_) {
|
||||
VLOG(sqlite) << "Finish exec " << tag("query", cmd) << tag("database", raw_->db());
|
||||
}
|
||||
@ -307,8 +307,9 @@ Status SqliteDb::destroy(Slice path) {
|
||||
}
|
||||
|
||||
Result<SqliteStatement> SqliteDb::get_statement(CSlice statement) {
|
||||
sqlite3_stmt *stmt = nullptr;
|
||||
auto rc = sqlite3_prepare_v2(get_native(), statement.c_str(), static_cast<int>(statement.size()) + 1, &stmt, nullptr);
|
||||
tdsqlite3_stmt *stmt = nullptr;
|
||||
auto rc =
|
||||
tdsqlite3_prepare_v2(get_native(), statement.c_str(), static_cast<int>(statement.size()) + 1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
return Status::Error(PSLICE() << "Failed to prepare SQLite " << tag("statement", statement) << raw_->last_error());
|
||||
}
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
struct sqlite3;
|
||||
struct tdsqlite3;
|
||||
|
||||
namespace td {
|
||||
|
||||
@ -63,7 +63,7 @@ class SqliteDb {
|
||||
static Result<SqliteDb> change_key(CSlice path, bool allow_creation, const DbKey &new_db_key,
|
||||
const DbKey &old_db_key);
|
||||
|
||||
sqlite3 *get_native() const {
|
||||
tdsqlite3 *get_native() const {
|
||||
return raw_->db();
|
||||
}
|
||||
|
||||
|
@ -18,39 +18,39 @@ namespace td {
|
||||
int VERBOSITY_NAME(sqlite) = VERBOSITY_NAME(DEBUG) + 10;
|
||||
|
||||
namespace {
|
||||
int printExplainQueryPlan(StringBuilder &sb, sqlite3_stmt *pStmt) {
|
||||
const char *zSql = sqlite3_sql(pStmt);
|
||||
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 = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
|
||||
char *zExplain = tdsqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
|
||||
if (zExplain == nullptr) {
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
|
||||
sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
|
||||
int rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, nullptr);
|
||||
sqlite3_free(zExplain);
|
||||
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 == sqlite3_step(pExplain)) {
|
||||
int iSelectid = sqlite3_column_int(pExplain, 0);
|
||||
int iOrder = sqlite3_column_int(pExplain, 1);
|
||||
int iFrom = sqlite3_column_int(pExplain, 2);
|
||||
const char *zDetail = reinterpret_cast<const char *>(sqlite3_column_text(pExplain, 3));
|
||||
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 sqlite3_finalize(pExplain);
|
||||
return tdsqlite3_finalize(pExplain);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
SqliteStatement::SqliteStatement(sqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db)
|
||||
SqliteStatement::SqliteStatement(tdsqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db)
|
||||
: stmt_(stmt), db_(std::move(db)) {
|
||||
CHECK(stmt != nullptr);
|
||||
}
|
||||
@ -72,14 +72,14 @@ Result<string> SqliteStatement::explain() {
|
||||
return sb.as_cslice().str();
|
||||
}
|
||||
Status SqliteStatement::bind_blob(int id, Slice blob) {
|
||||
auto rc = sqlite3_bind_blob(stmt_.get(), id, blob.data(), static_cast<int>(blob.size()), nullptr);
|
||||
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 = sqlite3_bind_text(stmt_.get(), id, str.data(), static_cast<int>(str.size()), nullptr);
|
||||
auto rc = tdsqlite3_bind_text(stmt_.get(), id, str.data(), static_cast<int>(str.size()), nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
return last_error();
|
||||
}
|
||||
@ -87,21 +87,21 @@ Status SqliteStatement::bind_string(int id, Slice str) {
|
||||
}
|
||||
|
||||
Status SqliteStatement::bind_int32(int id, int32 value) {
|
||||
auto rc = sqlite3_bind_int(stmt_.get(), id, 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 = sqlite3_bind_int64(stmt_.get(), id, 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 = sqlite3_bind_null(stmt_.get(), id);
|
||||
auto rc = tdsqlite3_bind_null(stmt_.get(), id);
|
||||
if (rc != SQLITE_OK) {
|
||||
return last_error();
|
||||
}
|
||||
@ -127,8 +127,8 @@ StringBuilder &operator<<(StringBuilder &sb, SqliteStatement::Datatype type) {
|
||||
}
|
||||
Slice SqliteStatement::view_blob(int id) {
|
||||
LOG_IF(ERROR, view_datatype(id) != Datatype::Blob) << view_datatype(id);
|
||||
auto *data = sqlite3_column_blob(stmt_.get(), id);
|
||||
auto size = sqlite3_column_bytes(stmt_.get(), id);
|
||||
auto *data = tdsqlite3_column_blob(stmt_.get(), id);
|
||||
auto size = tdsqlite3_column_bytes(stmt_.get(), id);
|
||||
if (data == nullptr) {
|
||||
return Slice();
|
||||
}
|
||||
@ -136,8 +136,8 @@ Slice SqliteStatement::view_blob(int id) {
|
||||
}
|
||||
Slice SqliteStatement::view_string(int id) {
|
||||
LOG_IF(ERROR, view_datatype(id) != Datatype::Text) << view_datatype(id);
|
||||
auto *data = sqlite3_column_text(stmt_.get(), id);
|
||||
auto size = sqlite3_column_bytes(stmt_.get(), id);
|
||||
auto *data = tdsqlite3_column_text(stmt_.get(), id);
|
||||
auto size = tdsqlite3_column_bytes(stmt_.get(), id);
|
||||
if (data == nullptr) {
|
||||
return Slice();
|
||||
}
|
||||
@ -145,14 +145,14 @@ Slice SqliteStatement::view_string(int id) {
|
||||
}
|
||||
int32 SqliteStatement::view_int32(int id) {
|
||||
LOG_IF(ERROR, view_datatype(id) != Datatype::Integer) << view_datatype(id);
|
||||
return sqlite3_column_int(stmt_.get(), 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 sqlite3_column_int64(stmt_.get(), id);
|
||||
return tdsqlite3_column_int64(stmt_.get(), id);
|
||||
}
|
||||
SqliteStatement::Datatype SqliteStatement::view_datatype(int id) {
|
||||
auto type = sqlite3_column_type(stmt_.get(), id);
|
||||
auto type = tdsqlite3_column_type(stmt_.get(), id);
|
||||
switch (type) {
|
||||
case SQLITE_INTEGER:
|
||||
return Datatype::Integer;
|
||||
@ -170,7 +170,7 @@ SqliteStatement::Datatype SqliteStatement::view_datatype(int id) {
|
||||
}
|
||||
|
||||
void SqliteStatement::reset() {
|
||||
sqlite3_reset(stmt_.get());
|
||||
tdsqlite3_reset(stmt_.get());
|
||||
state_ = State::Start;
|
||||
}
|
||||
|
||||
@ -178,10 +178,10 @@ Status SqliteStatement::step() {
|
||||
if (state_ == State::Finish) {
|
||||
return Status::Error("One has to reset statement");
|
||||
}
|
||||
VLOG(sqlite) << "Start step " << tag("query", sqlite3_sql(stmt_.get())) << tag("statement", stmt_.get())
|
||||
VLOG(sqlite) << "Start step " << tag("query", tdsqlite3_sql(stmt_.get())) << tag("statement", stmt_.get())
|
||||
<< tag("database", db_.get());
|
||||
auto rc = sqlite3_step(stmt_.get());
|
||||
VLOG(sqlite) << "Finish step " << tag("query", sqlite3_sql(stmt_.get())) << tag("statement", stmt_.get())
|
||||
auto rc = tdsqlite3_step(stmt_.get());
|
||||
VLOG(sqlite) << "Finish step " << tag("query", tdsqlite3_sql(stmt_.get())) << tag("statement", stmt_.get())
|
||||
<< tag("database", db_.get());
|
||||
if (rc == SQLITE_ROW) {
|
||||
state_ = State::GotRow;
|
||||
@ -195,8 +195,8 @@ Status SqliteStatement::step() {
|
||||
return last_error();
|
||||
}
|
||||
|
||||
void SqliteStatement::StmtDeleter::operator()(sqlite3_stmt *stmt) {
|
||||
sqlite3_finalize(stmt);
|
||||
void SqliteStatement::StmtDeleter::operator()(tdsqlite3_stmt *stmt) {
|
||||
tdsqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
Status SqliteStatement::last_error() {
|
||||
|
@ -16,8 +16,8 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
struct sqlite3;
|
||||
struct sqlite3_stmt;
|
||||
struct tdsqlite3;
|
||||
struct tdsqlite3_stmt;
|
||||
|
||||
namespace td {
|
||||
|
||||
@ -69,17 +69,17 @@ class SqliteStatement {
|
||||
|
||||
private:
|
||||
friend class SqliteDb;
|
||||
SqliteStatement(sqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db);
|
||||
SqliteStatement(tdsqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db);
|
||||
|
||||
class StmtDeleter {
|
||||
public:
|
||||
void operator()(sqlite3_stmt *stmt);
|
||||
void operator()(tdsqlite3_stmt *stmt);
|
||||
};
|
||||
|
||||
enum class State { Start, GotRow, Finish };
|
||||
State state_ = State::Start;
|
||||
|
||||
std::unique_ptr<sqlite3_stmt, StmtDeleter> stmt_;
|
||||
std::unique_ptr<tdsqlite3_stmt, StmtDeleter> stmt_;
|
||||
std::shared_ptr<detail::RawSqliteDb> db_;
|
||||
|
||||
Status last_error();
|
||||
|
@ -21,8 +21,8 @@ namespace detail {
|
||||
|
||||
static std::atomic<bool> was_database_destroyed{false};
|
||||
|
||||
Status RawSqliteDb::last_error(sqlite3 *db, CSlice path) {
|
||||
return Status::Error(PSLICE() << Slice(sqlite3_errmsg(db)) << " for database \"" << path << '"');
|
||||
Status RawSqliteDb::last_error(tdsqlite3 *db, CSlice path) {
|
||||
return Status::Error(PSLICE() << Slice(tdsqlite3_errmsg(db)) << " for database \"" << path << '"');
|
||||
}
|
||||
|
||||
Status RawSqliteDb::destroy(Slice path) {
|
||||
@ -38,7 +38,7 @@ Status RawSqliteDb::destroy(Slice path) {
|
||||
|
||||
Status RawSqliteDb::last_error() {
|
||||
//If database was corrupted, try to delete it.
|
||||
auto code = sqlite3_errcode(db_);
|
||||
auto code = tdsqlite3_errcode(db_);
|
||||
if (code == SQLITE_CORRUPT) {
|
||||
was_database_destroyed.store(true, std::memory_order_relaxed);
|
||||
destroy(path_).ignore();
|
||||
@ -52,7 +52,7 @@ bool RawSqliteDb::was_any_database_destroyed() {
|
||||
}
|
||||
|
||||
RawSqliteDb::~RawSqliteDb() {
|
||||
auto rc = sqlite3_close(db_);
|
||||
auto rc = tdsqlite3_close(db_);
|
||||
LOG_IF(FATAL, rc != SQLITE_OK) << last_error(db_, path());
|
||||
}
|
||||
|
||||
|
@ -11,14 +11,14 @@
|
||||
#include "td/utils/SliceBuilder.h"
|
||||
#include "td/utils/Status.h"
|
||||
|
||||
struct sqlite3;
|
||||
struct tdsqlite3;
|
||||
|
||||
namespace td {
|
||||
namespace detail {
|
||||
|
||||
class RawSqliteDb {
|
||||
public:
|
||||
RawSqliteDb(sqlite3 *db, std::string path) : db_(db), path_(std::move(path)) {
|
||||
RawSqliteDb(tdsqlite3 *db, std::string path) : db_(db), path_(std::move(path)) {
|
||||
}
|
||||
RawSqliteDb(const RawSqliteDb &) = delete;
|
||||
RawSqliteDb(RawSqliteDb &&) = delete;
|
||||
@ -35,7 +35,7 @@ class RawSqliteDb {
|
||||
}
|
||||
static Status destroy(Slice path) TD_WARN_UNUSED_RESULT;
|
||||
|
||||
sqlite3 *db() {
|
||||
tdsqlite3 *db() {
|
||||
return db_;
|
||||
}
|
||||
CSlice path() const {
|
||||
@ -43,7 +43,7 @@ class RawSqliteDb {
|
||||
}
|
||||
|
||||
Status last_error();
|
||||
static Status last_error(sqlite3 *db, CSlice path);
|
||||
static Status last_error(tdsqlite3 *db, CSlice path);
|
||||
|
||||
static bool was_any_database_destroyed();
|
||||
|
||||
@ -68,7 +68,7 @@ class RawSqliteDb {
|
||||
}
|
||||
|
||||
private:
|
||||
sqlite3 *db_;
|
||||
tdsqlite3 *db_;
|
||||
std::string path_;
|
||||
size_t begin_cnt_{0};
|
||||
optional<int32> cipher_version_;
|
||||
|
Loading…
Reference in New Issue
Block a user