TTK
Loading...
Searching...
No Matches
CinemaQuery.cpp
Go to the documentation of this file.
1#include <CinemaQuery.h>
2#include <iostream>
3
4#if TTK_ENABLE_SQLITE3
5#include <sqlite3.h>
6#endif
7
9 this->setDebugMsgPrefix("CinemaQuery");
10}
12
14 const std::vector<std::string> &sqlTableDefinitions,
15 const std::vector<std::string> &sqlInsertStatements,
16 const std::string &sqlQuery,
17 std::stringstream &resultCSV,
18 int &csvNColumns,
19 int &csvNRows) const {
20
21#if TTK_ENABLE_SQLITE3
22 // print input
23 {
24 std::vector<std::string> sqlLines;
25 {
26 std::stringstream ss(sqlQuery);
27 std::string line;
28 while(std::getline(ss, line))
29 sqlLines.push_back(line);
30 }
32 this->printMsg(sqlLines);
34 }
35
36 // SQLite Variables
37 sqlite3 *db;
38 char *zErrMsg = nullptr;
39 int rc;
40
41 // Create Temporary Database
42 {
43 Timer timer;
44 this->printMsg(
45 "Creating inmemory database", 0, ttk::debug::LineMode::REPLACE);
46
47 // Initialize DB in memory
48 rc = sqlite3_open(":memory:", &db);
49 if(rc != SQLITE_OK) {
50 this->printErr("Creating database: " + std::string{sqlite3_errmsg(db)});
51 return 0;
52 }
53
54 // Create table
55 for(auto &sqlTableDefinition : sqlTableDefinitions) {
56 rc = sqlite3_exec(
57 db, sqlTableDefinition.data(), nullptr, nullptr, &zErrMsg);
58 if(rc != SQLITE_OK) {
59 this->printErr("Create table: " + std::string{zErrMsg});
60
61 sqlite3_free(zErrMsg);
62 sqlite3_close(db);
63
64 return 0;
65 }
66 }
67
68 // Fill table
69 for(auto &sqlInsertStatement : sqlInsertStatements) {
70 rc = sqlite3_exec(
71 db, sqlInsertStatement.data(), nullptr, nullptr, &zErrMsg);
72 if(rc != SQLITE_OK) {
73 this->printErr("Insert values: " + std::string{zErrMsg});
74
75 sqlite3_free(zErrMsg);
76 sqlite3_close(db);
77 return 0;
78 }
79 }
80
81 this->printMsg("Creating inmemory database", 1, timer.getElapsedTime());
82 }
83
84 // Run SQL statement on temporary database
85 {
86 this->printMsg("Querying database", 0, ttk::debug::LineMode::REPLACE);
87 Timer timer;
88
89 sqlite3_stmt *sqlStatement;
90
91 if(sqlite3_prepare_v2(db, sqlQuery.data(), -1, &sqlStatement, nullptr)
92 != SQLITE_OK) {
93 this->printErr("Query: " + std::string{sqlite3_errmsg(db)});
94
95 sqlite3_close(db);
96 return 0;
97 }
98 csvNColumns = sqlite3_column_count(sqlStatement);
99
100 // Get Header
101 {
102 if(csvNColumns < 1) {
103 this->printErr("Query result has no columns.");
104
105 sqlite3_close(db);
106 return 0;
107 }
108
109 resultCSV << sqlite3_column_name(sqlStatement, 0);
110 for(int i = 1; i < csvNColumns; i++)
111 resultCSV << "," << sqlite3_column_name(sqlStatement, i);
112
113 resultCSV << "\n";
114 }
115
116 // Get Content
117 {
118 while((rc = sqlite3_step(sqlStatement)) == SQLITE_ROW) {
119 csvNRows++;
120
121 resultCSV << sqlite3_column_text(sqlStatement, 0);
122 for(int i = 1; i < csvNColumns; i++)
123 resultCSV << "," << sqlite3_column_text(sqlStatement, i);
124 resultCSV << "\n";
125 }
126
127 if(rc != SQLITE_DONE) {
128 this->printErr("Fetching result: " + std::string{sqlite3_errmsg(db)});
129
130 sqlite3_close(db);
131 return 0;
132 } else {
133 this->printMsg("Querying database", 1, timer.getElapsedTime());
134 }
135 }
136
137 sqlite3_finalize(sqlStatement);
138 }
139
140 // Close database
141 {
142 Timer timer;
143 this->printMsg("Closing database", 0, ttk::debug::LineMode::REPLACE);
144
145 // Delete DB
146 rc = sqlite3_close(db);
147
148 // Print status
149 if(rc != SQLITE_OK) {
150 this->printErr("Closing database:" + std::string{sqlite3_errmsg(db)});
151 return 0;
152 } else {
153 this->printMsg("Closing database", 1, timer.getElapsedTime());
154 }
155 }
156
157 return 1;
158
159#else
160 TTK_FORCE_USE(sqlTableDefinitions);
161 TTK_FORCE_USE(sqlInsertStatements);
162 TTK_FORCE_USE(sqlQuery);
163 TTK_FORCE_USE(resultCSV);
164 TTK_FORCE_USE(csvNColumns);
165 TTK_FORCE_USE(csvNRows);
166
167 this->printErr("This filter requires Sqlite3");
168 return 0;
169#endif // TTK_ENABLE_SQLITE3
170}
#define TTK_FORCE_USE(x)
Force the compiler to use the function/method parameter.
Definition: BaseClass.h:57
~CinemaQuery() override
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
Definition: CinemaQuery.cpp:13
void setDebugMsgPrefix(const std::string &prefix)
Definition: Debug.h:364
double getElapsedTime()
Definition: Timer.h:15
printMsg(debug::output::GREEN+"                           "+debug::output::ENDCOLOR+debug::output::GREEN+"▒"+debug::output::ENDCOLOR+debug::output::GREEN+"▒▒▒▒▒▒▒▒▒▒▒▒▒░"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)