Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option sqlite_debug_show_queries to print queries sent to SQLite #123

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ cmake-build-debug
sqlite/build
tpch.db
.clang-format
*.tbl
*.tbl
duckdb_unittest_tempdir/
2 changes: 2 additions & 0 deletions src/include/sqlite_db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class SQLiteDB {
bool ColumnExists(const string &table_name, const string &column_name);
vector<IndexInfo> GetIndexInfo(const string &table_name);

static void DebugSetPrintQueries(bool print);

bool IsOpen();
void Close();
};
Expand Down
12 changes: 12 additions & 0 deletions src/sqlite_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace duckdb {

static bool debug_sqlite_print_queries = false;

SQLiteDB::SQLiteDB() : db(nullptr) {
}

Expand Down Expand Up @@ -64,6 +66,9 @@ SQLiteDB SQLiteDB::Open(const string &path, const SQLiteOpenOptions &options, bo

bool SQLiteDB::TryPrepare(const string &query, SQLiteStatement &stmt) {
stmt.db = db;
if (debug_sqlite_print_queries) {
Printer::Print(query + "\n");
}
auto rc = sqlite3_prepare_v2(db, query.c_str(), -1, &stmt.stmt, nullptr);
if (rc != SQLITE_OK) {
return false;
Expand All @@ -81,6 +86,9 @@ SQLiteStatement SQLiteDB::Prepare(const string &query) {
}

void SQLiteDB::Execute(const string &query) {
if (debug_sqlite_print_queries) {
Printer::Print(query + "\n");
}
auto rc = sqlite3_exec(db, query.c_str(), nullptr, nullptr, nullptr);
if (rc != SQLITE_OK) {
string error = "Failed to execute query \"" + query + "\": " + string(sqlite3_errmsg(db));
Expand Down Expand Up @@ -303,4 +311,8 @@ idx_t SQLiteDB::RunPragma(string pragma_name) {
throw InternalException("No result returned from pragma " + pragma_name);
}

void SQLiteDB::DebugSetPrintQueries(bool print) {
debug_sqlite_print_queries = print;
}

} // namespace duckdb
8 changes: 8 additions & 0 deletions src/sqlite_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#endif
#include "duckdb.hpp"

#include "sqlite_db.hpp"
#include "sqlite_scanner.hpp"
#include "sqlite_storage.hpp"
#include "sqlite_scanner_extension.hpp"
Expand All @@ -15,6 +16,10 @@ using namespace duckdb;

extern "C" {

static void SetSqliteDebugQueryPrint(ClientContext &context, SetScope scope, Value &parameter) {
SQLiteDB::DebugSetPrintQueries(BooleanValue::Get(parameter));
}

static void LoadInternal(DatabaseInstance &db) {
SqliteScanFunction sqlite_fun;
ExtensionUtil::RegisterFunction(db, sqlite_fun);
Expand All @@ -25,6 +30,9 @@ static void LoadInternal(DatabaseInstance &db) {
auto &config = DBConfig::GetConfig(db);
config.AddExtensionOption("sqlite_all_varchar", "Load all SQLite columns as VARCHAR columns", LogicalType::BOOLEAN);

config.AddExtensionOption("sqlite_debug_show_queries", "DEBUG SETTING: print all queries sent to SQLite to stdout",
LogicalType::BOOLEAN, Value::BOOLEAN(false), SetSqliteDebugQueryPrint);

config.storage_extensions["sqlite_scanner"] = make_uniq<SQLiteStorageExtension>();
}

Expand Down
40 changes: 40 additions & 0 deletions test/sql/storage/attach_debug_print_unharmful.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# name: test/sql/storage/attach_debug_print_unharmful.test
# description: set sqlite_debug_show_queries setting to true, make sure a sample query still runs properly
# group: [sqlite_storage]

require sqlite_scanner

statement ok
CALL sqlite_attach('data/db/sakila.db');

query IIII
SELECT * FROM actor LIMIT 10;
----
1 PENELOPE GUINESS 2021-03-06 15:51:59
2 NICK WAHLBERG 2021-03-06 15:51:59
3 ED CHASE 2021-03-06 15:51:59
4 JENNIFER DAVIS 2021-03-06 15:51:59
5 JOHNNY LOLLOBRIGIDA 2021-03-06 15:51:59
6 BETTE NICHOLSON 2021-03-06 15:51:59
7 GRACE MOSTEL 2021-03-06 15:51:59
8 MATTHEW JOHANSSON 2021-03-06 15:51:59
9 JOE SWANK 2021-03-06 15:51:59
10 CHRISTIAN GABLE 2021-03-06 15:51:59


statement ok
SET sqlite_debug_show_queries=true;

query IIII
SELECT * FROM actor LIMIT 10;
----
1 PENELOPE GUINESS 2021-03-06 15:51:59
2 NICK WAHLBERG 2021-03-06 15:51:59
3 ED CHASE 2021-03-06 15:51:59
4 JENNIFER DAVIS 2021-03-06 15:51:59
5 JOHNNY LOLLOBRIGIDA 2021-03-06 15:51:59
6 BETTE NICHOLSON 2021-03-06 15:51:59
7 GRACE MOSTEL 2021-03-06 15:51:59
8 MATTHEW JOHANSSON 2021-03-06 15:51:59
9 JOE SWANK 2021-03-06 15:51:59
10 CHRISTIAN GABLE 2021-03-06 15:51:59