From fb1b17bfffa536d400d82a0124ffe8586b4cdd0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Mon, 16 Apr 2012 07:19:54 +0200 Subject: [PATCH] Added an easy wrapper Column Database::execAndGet("query"), version number increased to 0.3.0 --- .cproject | 12 ++++++++++-- .gitignore | 1 + TODO.txt | 5 ++--- src/SQLiteC++/Database.cpp | 13 ++++++++++++- src/SQLiteC++/Database.h | 24 +++++++++++++++++++----- src/SQLiteC++/SQLiteC++.h | 4 ++-- src/example1/main.cpp | 32 ++++++++++++++++++++++++++++---- 7 files changed, 74 insertions(+), 17 deletions(-) diff --git a/.cproject b/.cproject index 97b6293..128c991 100644 --- a/.cproject +++ b/.cproject @@ -467,7 +467,11 @@ - + + + + + @@ -929,7 +933,11 @@ - + + + + + diff --git a/.gitignore b/.gitignore index 6b876a9..34e183d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ Release *.suo *.user *~ +core diff --git a/TODO.txt b/TODO.txt index f0165eb..16ea28d 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,9 +1,8 @@ Add a comparison of others C++ wrappers (code style, C++ design, in code documentation, tests, online documentation, examples, license, UTF-16) -Missing features in v0.2.0: -- Statement::execStepAndGetScalar() easy wrapper -- Database::execScalar() easy wrapper like CppSqlite +Missing features in v0.3.0: - Database::tableExists +- Blob - getColumnByName ? std::map getRow() ? Advanced missing features: diff --git a/src/SQLiteC++/Database.cpp b/src/SQLiteC++/Database.cpp index 29bd72e..382e618 100644 --- a/src/SQLiteC++/Database.cpp +++ b/src/SQLiteC++/Database.cpp @@ -10,7 +10,6 @@ #include "Database.h" #include "Statement.h" -#include "Column.h" namespace SQLite { @@ -50,6 +49,18 @@ int Database::exec(const char* apQueries) // throw(SQLite::Exception); return sqlite3_changes(mpSQLite); } +// Shortcut to execute a one step query and fetch the first column of the result. +// WARNING: Be very careful with this dangerous method: you have to +// make a COPY OF THE result, else it will be destroy before the next line +// (when the underlying temporary Statement and Column objects are destroyed) +Column Database::execAndGet(const char* apQuery) // throw(SQLite::Exception) +{ + Statement query(*this, apQuery); + query.executeStep(); + return query.getColumn(0); +} + + // Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message void Database::check(const int aRet) const // throw(SQLite::Exception) { diff --git a/src/SQLiteC++/Database.h b/src/SQLiteC++/Database.h index 3e34a19..4d0a432 100644 --- a/src/SQLiteC++/Database.h +++ b/src/SQLiteC++/Database.h @@ -11,14 +11,11 @@ #include #include "Exception.h" +#include "Column.h" namespace SQLite { -// Forward declarations -class Statement; -class Exception; - /** * @brief RAII management of a SQLite Database Connection. * @@ -68,6 +65,23 @@ public: */ int exec(const char* apQueries); // throw(SQLite::Exception); + /** + * @brief Shortcut to execute a one step query and fetch the first column of the result. + * + * This is a shortcut to execute a simple statement with a single result. + * This should be used only for non reusable queries (else you should use a Statement with bind()). + * This should be used only for queries with expected results (else an exception is fired). + * + * @warning WARNING: Be very careful with this dangerous method: you have to + * make a COPY OF THE result, else it will be destroy before the next line + * (when the underlying temporary Statement and Column objects are destroyed) + * + * @see also Statement class for handling queries with multiple results + * + * @param[in] apQuery a UTF-8 encoded SQL query + */ + Column execAndGet(const char* apQuery); // throw(SQLite::Exception); + /** * @brief Set a busy handler that sleeps for a specified amount of time when a table is locked. * @@ -80,7 +94,7 @@ public: /** * @brief Get the rowid of the most recent successful INSERT into the database from the current connection. - * + * * @return Rowid of the most recent successful INSERT into the database, or 0 if there was none. */ inline sqlite3_int64 getLastInsertRowid(void) const // throw(); nothrow diff --git a/src/SQLiteC++/SQLiteC++.h b/src/SQLiteC++/SQLiteC++.h index 3576a40..86eacfb 100644 --- a/src/SQLiteC++/SQLiteC++.h +++ b/src/SQLiteC++/SQLiteC++.h @@ -32,5 +32,5 @@ * with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same * numbers used in [SQLITECPP_VERSION]. */ -#define SQLITECPP_VERSION "0.2.0" -#define SQLITECPP_VERSION_NUMBER 0002000 +#define SQLITECPP_VERSION "0.3.0" +#define SQLITECPP_VERSION_NUMBER 0003000 diff --git a/src/example1/main.cpp b/src/example1/main.cpp index 133ec23..01d8449 100644 --- a/src/example1/main.cpp +++ b/src/example1/main.cpp @@ -58,13 +58,19 @@ private: int main (void) { - // Basic example (1/4) : + // Basic example (1/5) : try { // Open a database file SQLite::Database db("example.db3"); std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; + // TODO SRombauts: + //SQLite::Statement::Column col = db.execAndGet("SELECT value FROM test WHERE id=2"); + //const char* pvalue = col; + std::string value = db.execAndGet("SELECT value FROM test WHERE id=2"); + std::cout << "execAndGet=" << value << std::endl; + // Compile a SQL query, containing one parameter (index 1) SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?"); std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " columns in the result)\n"; @@ -101,7 +107,7 @@ int main (void) } //////////////////////////////////////////////////////////////////////////// - // Object Oriented Basic example (2/4) : + // Object Oriented Basic example (2/5) : try { // Open the database and compile the query @@ -117,8 +123,26 @@ int main (void) std::cout << "SQLite exception: " << e.what() << std::endl; } + // The execAndGet wrapper example (3/5) : + try + { + // Open a database file + SQLite::Database db("example.db3"); + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; + + // WARNING: Be very careful with this dangerous method: you have to + // make a COPY OF THE result, else it will be destroy before the next line + // (when the underlying temporary Statement and Column objects are destroyed) + std::string value = db.execAndGet("SELECT value FROM test WHERE id=2"); + std::cout << "execAndGet=" << value << std::endl; + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + } + //////////////////////////////////////////////////////////////////////////// - // Simple batch queries example (3/4) : + // Simple batch queries example (4/5) : try { // Open a database file @@ -141,7 +165,7 @@ int main (void) remove("test.db3"); //////////////////////////////////////////////////////////////////////////// - // RAII transaction example (4/4) : + // RAII transaction example (5/5) : try { // Open a database file