Skip to content

Commit f574741

Browse files
committed
Implementation of sqlite debug print show queries
1 parent d5d6265 commit f574741

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ cmake-build-debug
55
sqlite/build
66
tpch.db
77
.clang-format
8-
*.tbl
8+
*.tbl
9+
duckdb_unittest_tempdir/

src/include/sqlite_db.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class SQLiteDB {
4949
bool ColumnExists(const string &table_name, const string &column_name);
5050
vector<IndexInfo> GetIndexInfo(const string &table_name);
5151

52+
static void DebugSetPrintQueries(bool print);
53+
5254
bool IsOpen();
5355
void Close();
5456
};

src/sqlite_db.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace duckdb {
1111

12+
static bool debug_sqlite_print_queries = false;
13+
1214
SQLiteDB::SQLiteDB() : db(nullptr) {
1315
}
1416

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

6567
bool SQLiteDB::TryPrepare(const string &query, SQLiteStatement &stmt) {
6668
stmt.db = db;
69+
if (debug_sqlite_print_queries) {
70+
Printer::Print(query + "\n");
71+
}
6772
auto rc = sqlite3_prepare_v2(db, query.c_str(), -1, &stmt.stmt, nullptr);
6873
if (rc != SQLITE_OK) {
6974
return false;
@@ -81,6 +86,9 @@ SQLiteStatement SQLiteDB::Prepare(const string &query) {
8186
}
8287

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

314+
void SQLiteDB::DebugSetPrintQueries(bool print) {
315+
debug_sqlite_print_queries = print;
316+
}
317+
306318
} // namespace duckdb

src/sqlite_extension.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#endif
44
#include "duckdb.hpp"
55

6+
#include "sqlite_db.hpp"
67
#include "sqlite_scanner.hpp"
78
#include "sqlite_storage.hpp"
89
#include "sqlite_scanner_extension.hpp"
@@ -15,6 +16,10 @@ using namespace duckdb;
1516

1617
extern "C" {
1718

19+
static void SetSqliteDebugQueryPrint(ClientContext &context, SetScope scope, Value &parameter) {
20+
SQLiteDB::DebugSetPrintQueries(BooleanValue::Get(parameter));
21+
}
22+
1823
static void LoadInternal(DatabaseInstance &db) {
1924
SqliteScanFunction sqlite_fun;
2025
ExtensionUtil::RegisterFunction(db, sqlite_fun);
@@ -25,6 +30,9 @@ static void LoadInternal(DatabaseInstance &db) {
2530
auto &config = DBConfig::GetConfig(db);
2631
config.AddExtensionOption("sqlite_all_varchar", "Load all SQLite columns as VARCHAR columns", LogicalType::BOOLEAN);
2732

33+
config.AddExtensionOption("sqlite_debug_show_queries", "DEBUG SETTING: print all queries sent to Postgres to stdout",
34+
LogicalType::BOOLEAN, Value::BOOLEAN(false), SetSqliteDebugQueryPrint);
35+
2836
config.storage_extensions["sqlite_scanner"] = make_uniq<SQLiteStorageExtension>();
2937
}
3038

0 commit comments

Comments
 (0)