Improve SQLite logging.

This commit is contained in:
levlam 2022-10-05 18:24:19 +03:00
parent 59acbf6d15
commit c6c8fdcb10
4 changed files with 11 additions and 8 deletions

View File

@ -125,14 +125,17 @@ Status SqliteDb::exec(CSlice cmd) {
VLOG(sqlite) << "Start exec " << tag("query", cmd) << tag("database", raw_->db());
}
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());
}
if (rc != SQLITE_OK) {
CHECK(msg != nullptr);
if (enable_logging_) {
VLOG(sqlite) << "Finish exec with error " << msg;
}
return Status::Error(PSLICE() << tag("query", cmd) << " to database \"" << raw_->path() << "\" failed: " << msg);
}
CHECK(msg == nullptr);
if (enable_logging_) {
VLOG(sqlite) << "Finish exec";
}
return Status::OK();
}

View File

@ -32,7 +32,7 @@ class SqliteDb {
// dangerous
SqliteDb clone() const {
return SqliteDb(raw_);
return SqliteDb(raw_, enable_logging_);
}
bool empty() const {
@ -77,7 +77,8 @@ class SqliteDb {
optional<int32> get_cipher_version() const;
private:
explicit SqliteDb(std::shared_ptr<detail::RawSqliteDb> raw) : raw_(std::move(raw)) {
SqliteDb(std::shared_ptr<detail::RawSqliteDb> raw, bool enable_logging)
: raw_(std::move(raw)), enable_logging_(enable_logging) {
}
std::shared_ptr<detail::RawSqliteDb> raw_;
bool enable_logging_ = false;

View File

@ -75,7 +75,7 @@ string SqliteKeyValue::get(Slice key) {
get_stmt_.bind_blob(1, key).ensure();
get_stmt_.step().ensure();
if (!get_stmt_.has_row()) {
return "";
return string();
}
auto data = get_stmt_.view_blob(0).str();
get_stmt_.step().ignore();

View File

@ -181,8 +181,7 @@ Status SqliteStatement::step() {
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 " << tag("query", tdsqlite3_sql(stmt_.get())) << tag("statement", stmt_.get())
<< tag("database", db_.get());
VLOG(sqlite) << "Finish step with response " << (rc == SQLITE_ROW ? "ROW" : (rc == SQLITE_DONE ? "DONE" : "ERROR"));
if (rc == SQLITE_ROW) {
state_ = State::GotRow;
return Status::OK();