14 const std::vector<std::string> &sqlTableDefinitions,
15 const std::vector<std::string> &sqlInsertStatements,
16 const std::string &sqlQuery,
17 std::stringstream &resultCSV,
19 int &csvNRows)
const {
21#ifdef TTK_ENABLE_SQLITE3
24 std::vector<std::string> sqlLines;
26 std::stringstream ss(sqlQuery);
28 while(std::getline(ss, line))
29 sqlLines.push_back(line);
38 char *zErrMsg =
nullptr;
48 rc = sqlite3_open(
":memory:", &db);
50 this->printErr(
"Creating database: " + std::string{sqlite3_errmsg(db)});
55 for(
auto &sqlTableDefinition : sqlTableDefinitions) {
57 db, sqlTableDefinition.data(),
nullptr,
nullptr, &zErrMsg);
59 this->printErr(
"Create table: " + std::string{zErrMsg});
61 sqlite3_free(zErrMsg);
69 for(
auto &sqlInsertStatement : sqlInsertStatements) {
71 db, sqlInsertStatement.data(),
nullptr,
nullptr, &zErrMsg);
73 this->printErr(
"Insert values: " + std::string{zErrMsg});
75 sqlite3_free(zErrMsg);
89 sqlite3_stmt *sqlStatement;
91 if(sqlite3_prepare_v2(db, sqlQuery.data(), -1, &sqlStatement,
nullptr)
93 this->printErr(
"Query: " + std::string{sqlite3_errmsg(db)});
98 csvNColumns = sqlite3_column_count(sqlStatement);
102 if(csvNColumns < 1) {
103 this->printErr(
"Query result has no columns.");
109 resultCSV << sqlite3_column_name(sqlStatement, 0);
110 for(
int i = 1; i < csvNColumns; i++)
111 resultCSV <<
"," << sqlite3_column_name(sqlStatement, i);
118 while((rc = sqlite3_step(sqlStatement)) == SQLITE_ROW) {
121 resultCSV << sqlite3_column_text(sqlStatement, 0);
122 for(
int i = 1; i < csvNColumns; i++)
123 resultCSV <<
"," << sqlite3_column_text(sqlStatement, i);
127 if(rc != SQLITE_DONE) {
128 this->printErr(
"Fetching result: " + std::string{sqlite3_errmsg(db)});
137 sqlite3_finalize(sqlStatement);
146 rc = sqlite3_close(db);
149 if(rc != SQLITE_OK) {
150 this->printErr(
"Closing database:" + std::string{sqlite3_errmsg(db)});
167 this->printErr(
"This filter requires Sqlite3");
int execute(const std::vector< std::string > &sqlTableDefinitions, const std::vector< std::string > &sqlInsertStatements, const std::string &sqlQuery, std::stringstream &resultCSV, int &csvNColumns, int &csvNRows) const
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)