From 5dd91c7b80443c1f0e747088159c4d8b78d1856d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 24 Dec 2015 04:13:24 +0000 Subject: SQLite files prefixed with sqlite- --- libsqlitepp/command.cpp | 105 --------------------------- libsqlitepp/command.h | 41 ----------- libsqlitepp/connection.cpp | 134 ----------------------------------- libsqlitepp/connection.h | 41 ----------- libsqlitepp/error.cpp | 29 -------- libsqlitepp/error.h | 24 ------- libsqlitepp/mock.cpp | 39 ---------- libsqlitepp/mock.h | 29 -------- libsqlitepp/modifycommand.cpp | 31 -------- libsqlitepp/modifycommand.h | 25 ------- libsqlitepp/selectcommand.cpp | 83 ---------------------- libsqlitepp/selectcommand.h | 21 ------ libsqlitepp/sqlite-command.cpp | 105 +++++++++++++++++++++++++++ libsqlitepp/sqlite-command.h | 41 +++++++++++ libsqlitepp/sqlite-connection.cpp | 134 +++++++++++++++++++++++++++++++++++ libsqlitepp/sqlite-connection.h | 41 +++++++++++ libsqlitepp/sqlite-error.cpp | 29 ++++++++ libsqlitepp/sqlite-error.h | 24 +++++++ libsqlitepp/sqlite-mock.cpp | 39 ++++++++++ libsqlitepp/sqlite-mock.h | 29 ++++++++ libsqlitepp/sqlite-modifycommand.cpp | 31 ++++++++ libsqlitepp/sqlite-modifycommand.h | 25 +++++++ libsqlitepp/sqlite-selectcommand.cpp | 83 ++++++++++++++++++++++ libsqlitepp/sqlite-selectcommand.h | 21 ++++++ libsqlitepp/unittests/testsqlite.cpp | 4 +- 25 files changed, 604 insertions(+), 604 deletions(-) delete mode 100644 libsqlitepp/command.cpp delete mode 100644 libsqlitepp/command.h delete mode 100644 libsqlitepp/connection.cpp delete mode 100644 libsqlitepp/connection.h delete mode 100644 libsqlitepp/error.cpp delete mode 100644 libsqlitepp/error.h delete mode 100644 libsqlitepp/mock.cpp delete mode 100644 libsqlitepp/mock.h delete mode 100644 libsqlitepp/modifycommand.cpp delete mode 100644 libsqlitepp/modifycommand.h delete mode 100644 libsqlitepp/selectcommand.cpp delete mode 100644 libsqlitepp/selectcommand.h create mode 100644 libsqlitepp/sqlite-command.cpp create mode 100644 libsqlitepp/sqlite-command.h create mode 100644 libsqlitepp/sqlite-connection.cpp create mode 100644 libsqlitepp/sqlite-connection.h create mode 100644 libsqlitepp/sqlite-error.cpp create mode 100644 libsqlitepp/sqlite-error.h create mode 100644 libsqlitepp/sqlite-mock.cpp create mode 100644 libsqlitepp/sqlite-mock.h create mode 100644 libsqlitepp/sqlite-modifycommand.cpp create mode 100644 libsqlitepp/sqlite-modifycommand.h create mode 100644 libsqlitepp/sqlite-selectcommand.cpp create mode 100644 libsqlitepp/sqlite-selectcommand.h diff --git a/libsqlitepp/command.cpp b/libsqlitepp/command.cpp deleted file mode 100644 index 4e24426..0000000 --- a/libsqlitepp/command.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "command.h" -#include "connection.h" -#include -#include - -SQLite::Command::Command(const Connection * conn, const std::string & sql) : - DB::Command(sql), - c(conn) -{ - if (sqlite3_prepare_v2(conn->db, sql.c_str(), sql.length(), &stmt, NULL) != SQLITE_OK) { - throw Error(sqlite3_errmsg(conn->db)); - } -} - -SQLite::Command::~Command() -{ - sqlite3_finalize(stmt); -} - -void -SQLite::Command::bindParamI(unsigned int n, int v) -{ - if (sqlite3_bind_int(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} -void -SQLite::Command::bindParamI(unsigned int n, long int v) -{ - if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} -void -SQLite::Command::bindParamI(unsigned int n, long long int v) -{ - if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} -void -SQLite::Command::bindParamI(unsigned int n, unsigned int v) -{ - if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} -void -SQLite::Command::bindParamI(unsigned int n, long unsigned int v) -{ - if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} -void -SQLite::Command::bindParamI(unsigned int n, long long unsigned int v) -{ - if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} -void -SQLite::Command::bindParamF(unsigned int n, double v) -{ - if (sqlite3_bind_double(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} -void -SQLite::Command::bindParamF(unsigned int n, float v) -{ - if (sqlite3_bind_double(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} -void -SQLite::Command::bindParamS(unsigned int n, const Glib::ustring & s) -{ - if (sqlite3_bind_text(stmt, n + 1, s.c_str(), s.length(), SQLITE_STATIC) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} -void -SQLite::Command::bindParamB(unsigned int, bool) -{ - throw Error("Not supported"); -} -void -SQLite::Command::bindParamT(unsigned int, const boost::posix_time::time_duration &) -{ - throw Error("Not supported"); -} -void -SQLite::Command::bindParamT(unsigned int, const boost::posix_time::ptime &) -{ - throw Error("Not supported"); -} -void -SQLite::Command::bindNull(unsigned int n) -{ - if (sqlite3_bind_null(stmt, n + 1) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); - } -} - diff --git a/libsqlitepp/command.h b/libsqlitepp/command.h deleted file mode 100644 index c66d71d..0000000 --- a/libsqlitepp/command.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef SQLITE_COMMAND_H -#define SQLITE_COMMAND_H - -#include -#include - -namespace SQLite { - class Connection; - class Command : public virtual DB::Command { - public: - Command(const Connection *, const std::string & sql); - virtual ~Command() = 0; - - void bindParamI(unsigned int, int) override; - void bindParamI(unsigned int, long int) override; - void bindParamI(unsigned int, long long int) override; - void bindParamI(unsigned int, unsigned int) override; - void bindParamI(unsigned int, long unsigned int) override; - void bindParamI(unsigned int, long long unsigned int) override; - - void bindParamB(unsigned int, bool) override; - - void bindParamF(unsigned int, double) override; - void bindParamF(unsigned int, float) override; - - void bindParamS(unsigned int, const Glib::ustring&) override; - - void bindParamT(unsigned int, const boost::posix_time::time_duration &) override; - void bindParamT(unsigned int, const boost::posix_time::ptime &) override; - - void bindNull(unsigned int) override; - - protected: - const Connection * c; - sqlite3_stmt * stmt; - }; -} - -#endif - - diff --git a/libsqlitepp/connection.cpp b/libsqlitepp/connection.cpp deleted file mode 100644 index 3331024..0000000 --- a/libsqlitepp/connection.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#include "connection.h" -#include "error.h" -#include "selectcommand.h" -#include "modifycommand.h" - -SQLite::Connection::Connection(const std::string & str) : - txDepth(0), - rolledback(false) -{ - if (sqlite3_open(str.c_str(), &db) != SQLITE_OK) { - if (db) { - std::string err(sqlite3_errmsg(db)); - sqlite3_close(db); - throw Error(err.c_str()); - } - throw Error("Unknown error opening database"); - } -} - -SQLite::Connection::~Connection() -{ - sqlite3_close(db); -} - -void -SQLite::Connection::finish() const -{ - if (txDepth != 0) { - rollbackTx(); - throw Error("Transaction still open"); - } -} - -int -SQLite::Connection::beginTx() const -{ - if (txDepth == 0) { - if (sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { - throw Error(sqlite3_errmsg(db)); - } - rolledback = false; - } - return ++txDepth; -} - -int -SQLite::Connection::commitTx() const -{ - if (rolledback) { - return rollbackTx(); - } - if (--txDepth == 0) { - if (sqlite3_exec(db, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { - throw Error(sqlite3_errmsg(db)); - } - } - return txDepth; -} - -int -SQLite::Connection::rollbackTx() const -{ - if (--txDepth == 0) { - if (sqlite3_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { - throw Error(sqlite3_errmsg(db)); - } - } - else { - rolledback = true; - } - return txDepth; -} - -bool -SQLite::Connection::inTx() const -{ - return txDepth; -} - -DB::BulkDeleteStyle -SQLite::Connection::bulkDeleteStyle() const -{ - return DB::BulkDeleteUsingUsingAlias; -} - -DB::BulkUpdateStyle -SQLite::Connection::bulkUpdateStyle() const -{ - return DB::BulkUpdateUsingJoin; -} - -void -SQLite::Connection::ping() const -{ - // Can this fail? -} - - -DB::SelectCommand * -SQLite::Connection::newSelectCommand(const std::string & sql) const -{ - return new SelectCommand(this, sql); -} - -DB::ModifyCommand * -SQLite::Connection::newModifyCommand(const std::string & sql) const -{ - return new ModifyCommand(this, sql); -} - -void -SQLite::Connection::beginBulkUpload(const char *, const char *) const -{ - throw Error("Not implemented"); -} - -void -SQLite::Connection::endBulkUpload(const char *) const -{ - throw Error("Not implemented"); -} - -size_t -SQLite::Connection::bulkUploadData(const char *, size_t) const -{ - throw Error("Not implemented"); -} - -int64_t -SQLite::Connection::insertId() const -{ - return sqlite3_last_insert_rowid(db); -} - diff --git a/libsqlitepp/connection.h b/libsqlitepp/connection.h deleted file mode 100644 index f5e2ec4..0000000 --- a/libsqlitepp/connection.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef SQLITE_CONNECTION_H -#define SQLITE_CONNECTION_H - -#include -#include "error.h" -#include - -namespace SQLite { - class Connection : public DB::Connection { - public: - Connection(const std::string & info); - ~Connection(); - - void finish() const override; - int beginTx() const override; - int commitTx() const override; - int rollbackTx() const override; - bool inTx() const override; - void ping() const override; - DB::BulkDeleteStyle bulkDeleteStyle() const override; - DB::BulkUpdateStyle bulkUpdateStyle() const override; - - DB::SelectCommand * newSelectCommand(const std::string & sql) const override; - DB::ModifyCommand * newModifyCommand(const std::string & sql) const override; - - void beginBulkUpload(const char *, const char *) const override; - void endBulkUpload(const char *) const override; - size_t bulkUploadData(const char *, size_t) const override; - - int64_t insertId() const override; - - sqlite3 * db; - - private: - mutable unsigned int txDepth; - mutable bool rolledback; - }; -} - -#endif - diff --git a/libsqlitepp/error.cpp b/libsqlitepp/error.cpp deleted file mode 100644 index 9bdf80b..0000000 --- a/libsqlitepp/error.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "error.h" -#include - -SQLite::Error::Error() : - msg(NULL) -{ -} - -SQLite::Error::Error(const SQLite::Error & e) : - msg(e.msg ? strdup(e.msg) : NULL) -{ -} - -SQLite::Error::Error(const char * e) : - msg(e ? strdup(e) : NULL) -{ -} - -SQLite::Error::~Error() throw() -{ - free(msg); -} - -const char * -SQLite::Error::what() const throw() -{ - return msg ? msg : "No message"; -} - diff --git a/libsqlitepp/error.h b/libsqlitepp/error.h deleted file mode 100644 index 3f8d32e..0000000 --- a/libsqlitepp/error.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef SQLITE_ERROR_H -#define SQLITE_ERROR_H - -#include - -namespace SQLite { - class Error : public DB::Error { - public: - Error(); - Error(const Error &); - Error(const char *); - ~Error() throw(); - - const char * what() const throw(); - - private: - char * msg; - }; - class ConnectionError : public Error, public virtual DB::ConnectionError { - }; -} - -#endif - diff --git a/libsqlitepp/mock.cpp b/libsqlitepp/mock.cpp deleted file mode 100644 index 4748116..0000000 --- a/libsqlitepp/mock.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "mock.h" -#include "connection.h" -#include -#include - -namespace SQLite { - -Mock::Mock(const std::string & name, const std::vector & ss) : - MockDatabase(name), - testDbPath(boost::filesystem::path("/tmp") / "sqliteut" / stringbf("%d", getpid()) / stringbf("%d", ++DB::MockDatabase::mocked)) -{ - CreateNewDatabase(); - PlaySchemaScripts(ss); -} - -DB::Connection * -Mock::openConnection() const -{ - return new Connection(testDbPath.string()); -} - -Mock::~Mock() -{ - DropDatabase(); -} - -void Mock::DropDatabase() const -{ - boost::filesystem::remove(testDbPath); -} - -void Mock::CreateNewDatabase() const -{ - boost::filesystem::create_directories(testDbPath.parent_path()); - delete openConnection(); -} - -} - diff --git a/libsqlitepp/mock.h b/libsqlitepp/mock.h deleted file mode 100644 index 8bd0899..0000000 --- a/libsqlitepp/mock.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef MOCKSQLITEDATASOURCE_H -#define MOCKSQLITEDATASOURCE_H - -#include -#include -#include - -namespace SQLite { - -class DLL_PUBLIC Mock : public DB::MockDatabase { - public: - Mock(const std::string & name, const std::vector & ss); - ~Mock(); - - protected: - void DropDatabase() const override; - void CreateNewDatabase() const override; - - DB::Connection * openConnection() const override; - - private: - const boost::filesystem::path testDbPath; -}; - -} - -#endif - - diff --git a/libsqlitepp/modifycommand.cpp b/libsqlitepp/modifycommand.cpp deleted file mode 100644 index 1b44cf4..0000000 --- a/libsqlitepp/modifycommand.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "modifycommand.h" -#include "error.h" -#include -#include "connection.h" - -SQLite::ModifyCommand::ModifyCommand(const Connection * conn, const std::string & sql) : - DB::Command(sql), - DB::ModifyCommand(sql), - SQLite::Command(conn, sql) -{ -} - -SQLite::ModifyCommand::~ModifyCommand() -{ -} - -unsigned int -SQLite::ModifyCommand::execute(bool anc) -{ - if (sqlite3_step(stmt) != SQLITE_DONE) { - sqlite3_reset(stmt); - throw Error(sqlite3_errmsg(c->db)); - } - unsigned int rows = sqlite3_changes(c->db); - sqlite3_reset(stmt); - if (rows == 0 && !anc) { - throw Error("No rows affected"); - } - return rows; -} - diff --git a/libsqlitepp/modifycommand.h b/libsqlitepp/modifycommand.h deleted file mode 100644 index 7f052ca..0000000 --- a/libsqlitepp/modifycommand.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef SQLITE_MODIFYCOMMAND_H -#define SQLITE_MODIFYCOMMAND_H - -#include -#include "command.h" - -namespace SQLite { - class Connection; - class ModifyCommand : public DB::ModifyCommand, public Command { - public: - ModifyCommand(const Connection *, const std::string & sql); - virtual ~ModifyCommand(); - - unsigned int execute(bool); - - private: - void prepare() const; - mutable bool prepared; - }; -} - -#endif - - - diff --git a/libsqlitepp/selectcommand.cpp b/libsqlitepp/selectcommand.cpp deleted file mode 100644 index a79b031..0000000 --- a/libsqlitepp/selectcommand.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "selectcommand.h" -#include "connection.h" -#include "error.h" -#include -#include -#include - -namespace SQLite { - class Column : public DB::Column { - public: - Column(const Glib::ustring & n, unsigned int i, sqlite3_stmt * s) : - DB::Column(n, i), - stmt(s) - { - } - - bool isNull() const { - return (SQLITE_NULL == sqlite3_column_type(stmt, colNo)); - } - - void apply(DB::HandleField & h) const { - switch (sqlite3_column_type(stmt, colNo)) { - case SQLITE_INTEGER: - h.integer(sqlite3_column_int64(stmt, colNo)); - return; - case SQLITE_FLOAT: - h.floatingpoint(sqlite3_column_double(stmt, colNo)); - return; - case SQLITE_TEXT: - { - auto t = sqlite3_column_text(stmt, colNo); - auto l = sqlite3_column_bytes(stmt, colNo); - h.string(reinterpret_cast(t), l); - return; - } - case SQLITE_NULL: - h.null(); - return; - case SQLITE_BLOB: - throw std::runtime_error("Blobs not supported"); - } - } - - void rebind(DB::Command*, unsigned int) const { - throw std::runtime_error("Not implemented"); - } - - private: - sqlite3_stmt * const stmt; - }; -} - -SQLite::SelectCommand::SelectCommand(const Connection * conn, const std::string & sql) : - DB::Command(sql), - DB::SelectCommand(sql), - SQLite::Command(conn, sql) -{ -} - -void -SQLite::SelectCommand::execute() -{ - // No explicit execute required -} - -bool -SQLite::SelectCommand::fetch() -{ - switch (sqlite3_step(stmt)) { - case SQLITE_ROW: - if (columns->empty()) { - for (int c = sqlite3_data_count(stmt) - 1; c >= 0; c -= 1) { - insertColumn(DB::ColumnPtr(new Column(sqlite3_column_name(stmt, c), c, stmt))); - } - } - return true; - case SQLITE_DONE: - return false; - default: - throw Error(sqlite3_errmsg(c->db)); - } -} - diff --git a/libsqlitepp/selectcommand.h b/libsqlitepp/selectcommand.h deleted file mode 100644 index c3ad01d..0000000 --- a/libsqlitepp/selectcommand.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef SQLITE_SELECTCOMMAND_H -#define SQLITE_SELECTCOMMAND_H - -#include -#include "command.h" - -namespace SQLite { - class Connection; - class ColumnBase; - class SelectCommand : public DB::SelectCommand, public Command { - public: - SelectCommand(const Connection *, const std::string & sql); - - bool fetch(); - void execute(); - }; -} - -#endif - - diff --git a/libsqlitepp/sqlite-command.cpp b/libsqlitepp/sqlite-command.cpp new file mode 100644 index 0000000..48563fa --- /dev/null +++ b/libsqlitepp/sqlite-command.cpp @@ -0,0 +1,105 @@ +#include "sqlite-command.h" +#include "sqlite-connection.h" +#include +#include + +SQLite::Command::Command(const Connection * conn, const std::string & sql) : + DB::Command(sql), + c(conn) +{ + if (sqlite3_prepare_v2(conn->db, sql.c_str(), sql.length(), &stmt, NULL) != SQLITE_OK) { + throw Error(sqlite3_errmsg(conn->db)); + } +} + +SQLite::Command::~Command() +{ + sqlite3_finalize(stmt); +} + +void +SQLite::Command::bindParamI(unsigned int n, int v) +{ + if (sqlite3_bind_int(stmt, n + 1, v) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} +void +SQLite::Command::bindParamI(unsigned int n, long int v) +{ + if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} +void +SQLite::Command::bindParamI(unsigned int n, long long int v) +{ + if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} +void +SQLite::Command::bindParamI(unsigned int n, unsigned int v) +{ + if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} +void +SQLite::Command::bindParamI(unsigned int n, long unsigned int v) +{ + if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} +void +SQLite::Command::bindParamI(unsigned int n, long long unsigned int v) +{ + if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} +void +SQLite::Command::bindParamF(unsigned int n, double v) +{ + if (sqlite3_bind_double(stmt, n + 1, v) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} +void +SQLite::Command::bindParamF(unsigned int n, float v) +{ + if (sqlite3_bind_double(stmt, n + 1, v) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} +void +SQLite::Command::bindParamS(unsigned int n, const Glib::ustring & s) +{ + if (sqlite3_bind_text(stmt, n + 1, s.c_str(), s.length(), SQLITE_STATIC) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} +void +SQLite::Command::bindParamB(unsigned int, bool) +{ + throw Error("Not supported"); +} +void +SQLite::Command::bindParamT(unsigned int, const boost::posix_time::time_duration &) +{ + throw Error("Not supported"); +} +void +SQLite::Command::bindParamT(unsigned int, const boost::posix_time::ptime &) +{ + throw Error("Not supported"); +} +void +SQLite::Command::bindNull(unsigned int n) +{ + if (sqlite3_bind_null(stmt, n + 1) != SQLITE_OK) { + throw Error(sqlite3_errmsg(c->db)); + } +} + diff --git a/libsqlitepp/sqlite-command.h b/libsqlitepp/sqlite-command.h new file mode 100644 index 0000000..c66d71d --- /dev/null +++ b/libsqlitepp/sqlite-command.h @@ -0,0 +1,41 @@ +#ifndef SQLITE_COMMAND_H +#define SQLITE_COMMAND_H + +#include +#include + +namespace SQLite { + class Connection; + class Command : public virtual DB::Command { + public: + Command(const Connection *, const std::string & sql); + virtual ~Command() = 0; + + void bindParamI(unsigned int, int) override; + void bindParamI(unsigned int, long int) override; + void bindParamI(unsigned int, long long int) override; + void bindParamI(unsigned int, unsigned int) override; + void bindParamI(unsigned int, long unsigned int) override; + void bindParamI(unsigned int, long long unsigned int) override; + + void bindParamB(unsigned int, bool) override; + + void bindParamF(unsigned int, double) override; + void bindParamF(unsigned int, float) override; + + void bindParamS(unsigned int, const Glib::ustring&) override; + + void bindParamT(unsigned int, const boost::posix_time::time_duration &) override; + void bindParamT(unsigned int, const boost::posix_time::ptime &) override; + + void bindNull(unsigned int) override; + + protected: + const Connection * c; + sqlite3_stmt * stmt; + }; +} + +#endif + + diff --git a/libsqlitepp/sqlite-connection.cpp b/libsqlitepp/sqlite-connection.cpp new file mode 100644 index 0000000..5eead63 --- /dev/null +++ b/libsqlitepp/sqlite-connection.cpp @@ -0,0 +1,134 @@ +#include "sqlite-connection.h" +#include "sqlite-error.h" +#include "sqlite-selectcommand.h" +#include "sqlite-modifycommand.h" + +SQLite::Connection::Connection(const std::string & str) : + txDepth(0), + rolledback(false) +{ + if (sqlite3_open(str.c_str(), &db) != SQLITE_OK) { + if (db) { + std::string err(sqlite3_errmsg(db)); + sqlite3_close(db); + throw Error(err.c_str()); + } + throw Error("Unknown error opening database"); + } +} + +SQLite::Connection::~Connection() +{ + sqlite3_close(db); +} + +void +SQLite::Connection::finish() const +{ + if (txDepth != 0) { + rollbackTx(); + throw Error("Transaction still open"); + } +} + +int +SQLite::Connection::beginTx() const +{ + if (txDepth == 0) { + if (sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { + throw Error(sqlite3_errmsg(db)); + } + rolledback = false; + } + return ++txDepth; +} + +int +SQLite::Connection::commitTx() const +{ + if (rolledback) { + return rollbackTx(); + } + if (--txDepth == 0) { + if (sqlite3_exec(db, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { + throw Error(sqlite3_errmsg(db)); + } + } + return txDepth; +} + +int +SQLite::Connection::rollbackTx() const +{ + if (--txDepth == 0) { + if (sqlite3_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { + throw Error(sqlite3_errmsg(db)); + } + } + else { + rolledback = true; + } + return txDepth; +} + +bool +SQLite::Connection::inTx() const +{ + return txDepth; +} + +DB::BulkDeleteStyle +SQLite::Connection::bulkDeleteStyle() const +{ + return DB::BulkDeleteUsingUsingAlias; +} + +DB::BulkUpdateStyle +SQLite::Connection::bulkUpdateStyle() const +{ + return DB::BulkUpdateUsingJoin; +} + +void +SQLite::Connection::ping() const +{ + // Can this fail? +} + + +DB::SelectCommand * +SQLite::Connection::newSelectCommand(const std::string & sql) const +{ + return new SelectCommand(this, sql); +} + +DB::ModifyCommand * +SQLite::Connection::newModifyCommand(const std::string & sql) const +{ + return new ModifyCommand(this, sql); +} + +void +SQLite::Connection::beginBulkUpload(const char *, const char *) const +{ + throw Error("Not implemented"); +} + +void +SQLite::Connection::endBulkUpload(const char *) const +{ + throw Error("Not implemented"); +} + +size_t +SQLite::Connection::bulkUploadData(const char *, size_t) const +{ + throw Error("Not implemented"); +} + +int64_t +SQLite::Connection::insertId() const +{ + return sqlite3_last_insert_rowid(db); +} + diff --git a/libsqlitepp/sqlite-connection.h b/libsqlitepp/sqlite-connection.h new file mode 100644 index 0000000..9398ee8 --- /dev/null +++ b/libsqlitepp/sqlite-connection.h @@ -0,0 +1,41 @@ +#ifndef SQLITE_CONNECTION_H +#define SQLITE_CONNECTION_H + +#include +#include "sqlite-error.h" +#include + +namespace SQLite { + class Connection : public DB::Connection { + public: + Connection(const std::string & info); + ~Connection(); + + void finish() const override; + int beginTx() const override; + int commitTx() const override; + int rollbackTx() const override; + bool inTx() const override; + void ping() const override; + DB::BulkDeleteStyle bulkDeleteStyle() const override; + DB::BulkUpdateStyle bulkUpdateStyle() const override; + + DB::SelectCommand * newSelectCommand(const std::string & sql) const override; + DB::ModifyCommand * newModifyCommand(const std::string & sql) const override; + + void beginBulkUpload(const char *, const char *) const override; + void endBulkUpload(const char *) const override; + size_t bulkUploadData(const char *, size_t) const override; + + int64_t insertId() const override; + + sqlite3 * db; + + private: + mutable unsigned int txDepth; + mutable bool rolledback; + }; +} + +#endif + diff --git a/libsqlitepp/sqlite-error.cpp b/libsqlitepp/sqlite-error.cpp new file mode 100644 index 0000000..a0758ef --- /dev/null +++ b/libsqlitepp/sqlite-error.cpp @@ -0,0 +1,29 @@ +#include "sqlite-error.h" +#include + +SQLite::Error::Error() : + msg(NULL) +{ +} + +SQLite::Error::Error(const SQLite::Error & e) : + msg(e.msg ? strdup(e.msg) : NULL) +{ +} + +SQLite::Error::Error(const char * e) : + msg(e ? strdup(e) : NULL) +{ +} + +SQLite::Error::~Error() throw() +{ + free(msg); +} + +const char * +SQLite::Error::what() const throw() +{ + return msg ? msg : "No message"; +} + diff --git a/libsqlitepp/sqlite-error.h b/libsqlitepp/sqlite-error.h new file mode 100644 index 0000000..3f8d32e --- /dev/null +++ b/libsqlitepp/sqlite-error.h @@ -0,0 +1,24 @@ +#ifndef SQLITE_ERROR_H +#define SQLITE_ERROR_H + +#include + +namespace SQLite { + class Error : public DB::Error { + public: + Error(); + Error(const Error &); + Error(const char *); + ~Error() throw(); + + const char * what() const throw(); + + private: + char * msg; + }; + class ConnectionError : public Error, public virtual DB::ConnectionError { + }; +} + +#endif + diff --git a/libsqlitepp/sqlite-mock.cpp b/libsqlitepp/sqlite-mock.cpp new file mode 100644 index 0000000..2f0958d --- /dev/null +++ b/libsqlitepp/sqlite-mock.cpp @@ -0,0 +1,39 @@ +#include "sqlite-mock.h" +#include "sqlite-connection.h" +#include +#include + +namespace SQLite { + +Mock::Mock(const std::string & name, const std::vector & ss) : + MockDatabase(name), + testDbPath(boost::filesystem::path("/tmp") / "sqliteut" / stringbf("%d", getpid()) / stringbf("%d", ++DB::MockDatabase::mocked)) +{ + CreateNewDatabase(); + PlaySchemaScripts(ss); +} + +DB::Connection * +Mock::openConnection() const +{ + return new Connection(testDbPath.string()); +} + +Mock::~Mock() +{ + DropDatabase(); +} + +void Mock::DropDatabase() const +{ + boost::filesystem::remove(testDbPath); +} + +void Mock::CreateNewDatabase() const +{ + boost::filesystem::create_directories(testDbPath.parent_path()); + delete openConnection(); +} + +} + diff --git a/libsqlitepp/sqlite-mock.h b/libsqlitepp/sqlite-mock.h new file mode 100644 index 0000000..8bd0899 --- /dev/null +++ b/libsqlitepp/sqlite-mock.h @@ -0,0 +1,29 @@ +#ifndef MOCKSQLITEDATASOURCE_H +#define MOCKSQLITEDATASOURCE_H + +#include +#include +#include + +namespace SQLite { + +class DLL_PUBLIC Mock : public DB::MockDatabase { + public: + Mock(const std::string & name, const std::vector & ss); + ~Mock(); + + protected: + void DropDatabase() const override; + void CreateNewDatabase() const override; + + DB::Connection * openConnection() const override; + + private: + const boost::filesystem::path testDbPath; +}; + +} + +#endif + + diff --git a/libsqlitepp/sqlite-modifycommand.cpp b/libsqlitepp/sqlite-modifycommand.cpp new file mode 100644 index 0000000..cb5d761 --- /dev/null +++ b/libsqlitepp/sqlite-modifycommand.cpp @@ -0,0 +1,31 @@ +#include "sqlite-modifycommand.h" +#include "sqlite-error.h" +#include +#include "sqlite-connection.h" + +SQLite::ModifyCommand::ModifyCommand(const Connection * conn, const std::string & sql) : + DB::Command(sql), + DB::ModifyCommand(sql), + SQLite::Command(conn, sql) +{ +} + +SQLite::ModifyCommand::~ModifyCommand() +{ +} + +unsigned int +SQLite::ModifyCommand::execute(bool anc) +{ + if (sqlite3_step(stmt) != SQLITE_DONE) { + sqlite3_reset(stmt); + throw Error(sqlite3_errmsg(c->db)); + } + unsigned int rows = sqlite3_changes(c->db); + sqlite3_reset(stmt); + if (rows == 0 && !anc) { + throw Error("No rows affected"); + } + return rows; +} + diff --git a/libsqlitepp/sqlite-modifycommand.h b/libsqlitepp/sqlite-modifycommand.h new file mode 100644 index 0000000..657639b --- /dev/null +++ b/libsqlitepp/sqlite-modifycommand.h @@ -0,0 +1,25 @@ +#ifndef SQLITE_MODIFYCOMMAND_H +#define SQLITE_MODIFYCOMMAND_H + +#include +#include "sqlite-command.h" + +namespace SQLite { + class Connection; + class ModifyCommand : public DB::ModifyCommand, public Command { + public: + ModifyCommand(const Connection *, const std::string & sql); + virtual ~ModifyCommand(); + + unsigned int execute(bool); + + private: + void prepare() const; + mutable bool prepared; + }; +} + +#endif + + + diff --git a/libsqlitepp/sqlite-selectcommand.cpp b/libsqlitepp/sqlite-selectcommand.cpp new file mode 100644 index 0000000..30d1561 --- /dev/null +++ b/libsqlitepp/sqlite-selectcommand.cpp @@ -0,0 +1,83 @@ +#include "sqlite-selectcommand.h" +#include "sqlite-connection.h" +#include "sqlite-error.h" +#include +#include +#include + +namespace SQLite { + class Column : public DB::Column { + public: + Column(const Glib::ustring & n, unsigned int i, sqlite3_stmt * s) : + DB::Column(n, i), + stmt(s) + { + } + + bool isNull() const { + return (SQLITE_NULL == sqlite3_column_type(stmt, colNo)); + } + + void apply(DB::HandleField & h) const { + switch (sqlite3_column_type(stmt, colNo)) { + case SQLITE_INTEGER: + h.integer(sqlite3_column_int64(stmt, colNo)); + return; + case SQLITE_FLOAT: + h.floatingpoint(sqlite3_column_double(stmt, colNo)); + return; + case SQLITE_TEXT: + { + auto t = sqlite3_column_text(stmt, colNo); + auto l = sqlite3_column_bytes(stmt, colNo); + h.string(reinterpret_cast(t), l); + return; + } + case SQLITE_NULL: + h.null(); + return; + case SQLITE_BLOB: + throw std::runtime_error("Blobs not supported"); + } + } + + void rebind(DB::Command*, unsigned int) const { + throw std::runtime_error("Not implemented"); + } + + private: + sqlite3_stmt * const stmt; + }; +} + +SQLite::SelectCommand::SelectCommand(const Connection * conn, const std::string & sql) : + DB::Command(sql), + DB::SelectCommand(sql), + SQLite::Command(conn, sql) +{ +} + +void +SQLite::SelectCommand::execute() +{ + // No explicit execute required +} + +bool +SQLite::SelectCommand::fetch() +{ + switch (sqlite3_step(stmt)) { + case SQLITE_ROW: + if (columns->empty()) { + for (int c = sqlite3_data_count(stmt) - 1; c >= 0; c -= 1) { + insertColumn(DB::ColumnPtr(new Column(sqlite3_column_name(stmt, c), c, stmt))); + } + } + return true; + case SQLITE_DONE: + return false; + default: + throw Error(sqlite3_errmsg(c->db)); + } +} + diff --git a/libsqlitepp/sqlite-selectcommand.h b/libsqlitepp/sqlite-selectcommand.h new file mode 100644 index 0000000..c795018 --- /dev/null +++ b/libsqlitepp/sqlite-selectcommand.h @@ -0,0 +1,21 @@ +#ifndef SQLITE_SELECTCOMMAND_H +#define SQLITE_SELECTCOMMAND_H + +#include +#include "sqlite-command.h" + +namespace SQLite { + class Connection; + class ColumnBase; + class SelectCommand : public DB::SelectCommand, public Command { + public: + SelectCommand(const Connection *, const std::string & sql); + + bool fetch(); + void execute(); + }; +} + +#endif + + diff --git a/libsqlitepp/unittests/testsqlite.cpp b/libsqlitepp/unittests/testsqlite.cpp index 62764bd..9cd8de9 100644 --- a/libsqlitepp/unittests/testsqlite.cpp +++ b/libsqlitepp/unittests/testsqlite.cpp @@ -5,8 +5,8 @@ #include #include #include -#include -#include "testCore.h" +#include +#include #include class StandardMockDatabase : public SQLite::Mock { -- cgit v1.2.3