From d5a78360117b763592ea2ed4192ada85d9ce0505 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Sat, 28 Dec 2019 09:18:33 +0000 Subject: [PATCH 1/2] Overload execute_statement method in sqlite3db --- include/sqlite3db.h | 8 ++++++-- lib/sqlite3db.cpp | 18 ++++++++++++++++++ test/tap/tests/Makefile | 14 +++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/sqlite3db.h b/include/sqlite3db.h index d6d569913..0750289e5 100644 --- a/include/sqlite3db.h +++ b/include/sqlite3db.h @@ -1,7 +1,10 @@ #ifndef __CLASS_SQLITE3DB_H #define __CLASS_SQLITE3DB_H -#include "proxysql.h" -#include "cpp.h" +#include "sqlite3.h" +#undef swap +#undef min +#undef max +#include #define PROXYSQL_SQLITE3DB_PTHREAD_MUTEX class SQLite3_row { @@ -70,6 +73,7 @@ class SQLite3DB { bool execute(const char *); bool execute_statement(const char *, char **, int *, int *, SQLite3_result **); + SQLite3_result* execute_statement(const char *, char **_error=NULL, int *_cols=NULL, int *_affected_rows=NULL); bool execute_statement_raw(const char *, char **, int *, int *, sqlite3_stmt **); int return_one_int(const char *); int check_table_structure(char *table_name, char *table_def); diff --git a/lib/sqlite3db.cpp b/lib/sqlite3db.cpp index 0534aacc6..8c6458f09 100644 --- a/lib/sqlite3db.cpp +++ b/lib/sqlite3db.cpp @@ -189,6 +189,24 @@ int SQLite3DB::prepare_v2(const char *str, sqlite3_stmt **statement) { return rc; } +SQLite3_result* SQLite3DB::execute_statement(const char *str, char **_error, int *_cols, int *_affected_rows) { + SQLite3_result* resultset; + + char *myerror; + char **error = (_error == NULL ? &myerror : _error); + + int mycols; + int *cols = (_cols == NULL ? &mycols : _cols); + + int my_affected_rows; + int *affected_rows = (_affected_rows == NULL ? &my_affected_rows : _affected_rows); + + if (execute_statement(str, error, cols, affected_rows, &resultset)) + return resultset; + + return NULL; +} + bool SQLite3DB::execute_statement(const char *str, char **error, int *cols, int *affected_rows, SQLite3_result **resultset) { int rc; sqlite3_stmt *statement=NULL; diff --git a/test/tap/tests/Makefile b/test/tap/tests/Makefile index f1017c027..f6ba80ac6 100644 --- a/test/tap/tests/Makefile +++ b/test/tap/tests/Makefile @@ -7,6 +7,11 @@ MARIADB_LDIR=$(MARIADB_PATH)/libmariadb INCLUDEDIR=../tap LIBDIR=../tap +PROXYSQL_PATH=../../.. +PROXYSQL_IDIR=$(PROXYSQL_PATH)/include + +SQLITE3_DIR=$(DEPS_PATH)/sqlite3/sqlite3 + STATIC_LIBS=$(MARIADB_LDIR)/libmariadbclient.a .PHONY: all @@ -14,13 +19,16 @@ all: tests .PHONY: clean clean: - rm -f basic-t set_character_set-t charset_unsigned_int-t select_config_file-t || true + rm -f basic-t set_character_set-t charset_unsigned_int-t select_config_file-t sqlite3-t || true OPT=-O2 -SRC=basic-t.cpp set_character_set-t.cpp charset_unsigned_int-t.cpp select_config_file-t.cpp +SRC=basic-t.cpp set_character_set-t.cpp charset_unsigned_int-t.cpp select_config_file-t.cpp sqlite3-t.cpp + +tests: basic-t set_character_set-t charset_unsigned_int-t select_config_file-t sqlite3-t -tests: basic-t set_character_set-t charset_unsigned_int-t select_config_file-t +sqlite3-t: $(LIBDIR)/libtap.a sqlite3-t.cpp + g++ sqlite3-t.cpp -I$(INCLUDEDIR) -I$(PROXYSQL_IDIR) -I$(MARIADB_IDIR) -I$(SQLITE3_DIR) -L$(LIBDIR) $(OPT) ../../../lib/libproxysql.a -lpthread -ldl -std=c++11 -ltap -o sqlite3-t basic-t: $(LIBDIR)/libtap.a g++ basic-t.cpp -I$(INCLUDEDIR) -L$(LIBDIR) $(OPT) -std=c++11 -ltap -o basic-t From a9d46ce747016e9c8e5f851ceacfa8661b73a2e1 Mon Sep 17 00:00:00 2001 From: Valentin Rakush Date: Tue, 31 Dec 2019 08:58:47 +0000 Subject: [PATCH 2/2] Tess for overload method execute_statement. Usage example --- test/tap/tests/sqlite3-t.cpp | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/tap/tests/sqlite3-t.cpp diff --git a/test/tap/tests/sqlite3-t.cpp b/test/tap/tests/sqlite3-t.cpp new file mode 100644 index 000000000..a6589b0d6 --- /dev/null +++ b/test/tap/tests/sqlite3-t.cpp @@ -0,0 +1,39 @@ +#include +#include "tap.h" +#include +#include +#include +#include "sqlite3db.h" + +int main() { + plan(8); + + SQLite3DB *db; // in memory + db = new SQLite3DB(); + db->open((char *)"file:mem_db?mode=memory&cache=shared", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX); + bool rc=db->check_and_build_table((char*)"test", (char*)"CREATE TABLE TEST (hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 3306)"); + ok(rc, "TEST table is created"); + rc=db->execute("INSERT INTO TEST (hostname, port) VALUES ('localhost', 6033)"); + ok(rc, "Row is inserted"); + + // Execute statement and get a result set in std::unique_ptr. will be automatically destroyed later.yy + std::unique_ptr result = std::unique_ptr(db->execute_statement("SELECT * FROM TEST")); + ok(nullptr != result, "Query result is not empty"); + + // Execute statement and gets number of columns and error message (error message should be empty) + int cols=0; + char *error=NULL; + std::unique_ptr result_2 = std::unique_ptr(db->execute_statement("SELECT * FROM TEST", &error, &cols)); + ok(nullptr != result_2, "Query result is not empty"); + ok(cols == 2, "Query returns correct number of columns"); + ok(!error, "No error message expected"); + + // Execute statement and gets number of columns and error message (error message should be empty) + error=NULL; + std::unique_ptr result_3 = std::unique_ptr(db->execute_statement("SELECT * FROM TEST1", &error)); + ok(nullptr == result_3, "Query result is empty"); + ok(error != NULL, "There is an error message expected"); + + return exit_status(); +} +