From ecd22dc11240f5fac5cdafecaefeab8fce47a31e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Fri, 1 Nov 2013 21:10:44 +0100 Subject: [PATCH] Fix issue #7 : SQLITECPP_ENABLE_ASSERT_HANDLER - SQLITECPP_ASSERT() can call a user defined SQLite::assertion_failed() handler. --- CMakeLists.txt | 5 +- README.md | 19 + examples/example1/main.cpp | 714 +++++++++++++++++++------------------ src/Assert.h | 56 +++ src/CMakeLists.txt | 1 + src/Database.cpp | 4 +- src/Exception.h | 17 - src/SQLiteC++.h | 1 + src/Statement.cpp | 4 +- src/Transaction.cpp | 6 +- 10 files changed, 452 insertions(+), 375 deletions(-) create mode 100644 src/Assert.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dd8a038..8f2a972 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,10 @@ option (SQLITE_ENABLE_COLUMN_METADATA "Enable Column::getName(). Require support from sqlite3 library." OFF) if (SQLITE_ENABLE_COLUMN_METADATA) add_definitions(-DSQLITE_ENABLE_COLUMN_METADATA) -endif() +endif() + +add_definitions(-DSQLITECPP_ENABLE_ASSERT_HANDLER) + if (MSVC) # build the SQLite3 C library for Windows (for ease of use) diff --git a/README.md b/README.md index d184dd4..833efe6 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,25 @@ catch (std::exception& e) } ``` +### How to handle in assertion in SQLiteC++: +Exceptions shall not be used in destructors, so SQLiteC++ use SQLITECPP_ASSERT() to check for errors in destructors. +If you don't want assert() to be called, you have to enable and define an assert handler as shown below. + +```C++ +#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER +namespace SQLite +{ +/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt) +void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg) +{ + // Print a message to the standard error output stream, and abort the program. + std::cerr << apFile << ":" << apLine << ":" << " error: assertion (" << apExpr << ") failed in '" << apFunc << "' (" << apMsg << ")\n"; + std::abort(); +} +} +#endif +``` + ## How to contribute ### GitHub website The most efficient way to help and contribute to this wrapper project is to diff --git a/examples/example1/main.cpp b/examples/example1/main.cpp index 1298a81..a83baab 100644 --- a/examples/example1/main.cpp +++ b/examples/example1/main.cpp @@ -1,350 +1,364 @@ -/** - * @file main.cpp - * @brief A few short examples in a row. - * - * Demonstrate how-to use the SQLite++ wrapper - * - * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com) - * - * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt - * or copy at http://opensource.org/licenses/MIT) - */ - -#include -#include -#include - -#include "../../src/SQLiteC++.h" - - -static const char* filename_example_db3 = "examples/example1/example.db3"; -static const char* filename_logo_png = "examples/example1/logo.png"; - - -/// Object Oriented Basic example -class Example -{ -public: - // Constructor - Example(void) : - mDb(filename_example_db3), // Open a database file in readonly mode - mQuery(mDb, "SELECT * FROM test WHERE weight > :min_weight")// Compile a SQL query, containing one parameter (index 1) - { - } - virtual ~Example(void) - { - } - - /// List the rows where the "weight" column is greater than the provided aParamValue - void ListGreaterThan (const int aParamValue) - { - std::cout << "ListGreaterThan (" << aParamValue << ")\n"; - - // Bind the integer value provided to the first parameter of the SQL query - mQuery.bind(":min_weight", aParamValue); // same as mQuery.bind(1, aParamValue); - - // Loop to execute the query step by step, to get one a row of results at a time - while (mQuery.executeStep()) - { - std::cout << "row (" << mQuery.getColumn(0) << ", \"" << mQuery.getColumn(1) << "\", " << mQuery.getColumn(2) << ")\n"; - } - - // Reset the query to be able to use it again later - mQuery.reset(); - } - -private: - SQLite::Database mDb; ///< Database connection - SQLite::Statement mQuery; ///< Database prepared SQL query -}; - - -int main (void) -{ - // Basic example (1/6) : - try - { - // Open a database file in readonly mode - SQLite::Database db(filename_example_db3); // SQLITE_OPEN_READONLY - std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; - - // Test if the 'test' table exists - bool bExists = db.tableExists("test"); - std::cout << "SQLite table 'test' exists=" << bExists << "\n"; - - // Get a single value result with an easy to use shortcut - std::string value = db.execAndGet("SELECT value FROM test WHERE id=2"); - std::cout << "execAndGet=" << value.c_str() << std::endl; - - // Compile a SQL query, containing one parameter (index 1) - SQLite::Statement query(db, "SELECT * FROM test WHERE weight > ?"); - std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " columns in the result)\n"; - // Bind the integer value 2 to the first parameter of the SQL query - query.bind(1, 2); - std::cout << "binded with integer value '2' :\n"; - - // Loop to execute the query step by step, to get one a row of results at a time - while (query.executeStep()) - { - // Demonstrate how to get some typed column value (and the equivalent explicit call) - int id = query.getColumn(0); // = query.getColumn(0).getInt() - //const char* pvalue = query.getColumn(1); // = query.getColumn(1).getText() - std::string value2 = query.getColumn(1); // = query.getColumn(1).getText() - int bytes = query.getColumn(1).getBytes(); - double weight = query.getColumn(2); // = query.getColumn(2).getInt() - -#ifdef SQLITE_ENABLE_COLUMN_METADATA - static bool bFirst = true; - if (bFirst) - { - // Show how to get the name of a column. - // Requires the SQLITE_ENABLE_COLUMN_METADATA preprocessor macro to be also defined at compile times of the SQLite library. - std::string name0 = query.getColumn(0).getName(); - std::string name1 = query.getColumn(1).getName(); - std::string name2 = query.getColumn(2).getName(); - std::cout << "table 'test' [\"" << name0.c_str() << "\", \"" << name1.c_str() << "\", \"" << name2.c_str() << "\"]\n"; - bFirst = false; - } -#endif - std::cout << "row (" << id << ", \"" << value2.c_str() << "\" " << bytes << " bytes, " << weight << ")\n"; - - } - - // Reset the query to use it again - query.reset(); - std::cout << "SQLite statement '" << query.getQuery().c_str() << "' reseted (" << query.getColumnCount () << " columns in the result)\n"; - // Bind the string value "6" to the first parameter of the SQL query - query.bind(1, "6"); - std::cout << "binded with string value \"6\" :\n"; - - while (query.executeStep()) - { - // Demonstrate that inserting column value in a std:ostream is natural - std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\", " << query.getColumn(2) << ")\n"; - } - } - catch (std::exception& e) - { - std::cout << "SQLite exception: " << e.what() << std::endl; - return EXIT_FAILURE; // unexpected error : exit the example program - } - - //////////////////////////////////////////////////////////////////////////// - // Object Oriented Basic example (2/6) : - try - { - // Open the database and compile the query - Example example; - - // Demonstrate the way to use the same query with different parameter values - example.ListGreaterThan(8); - example.ListGreaterThan(6); - example.ListGreaterThan(2); - } - catch (std::exception& e) - { - std::cout << "SQLite exception: " << e.what() << std::endl; - return EXIT_FAILURE; // unexpected error : exit the example program - } - - // The execAndGet wrapper example (3/6) : - try - { - // Open a database file in readonly mode - SQLite::Database db(filename_example_db3); // SQLITE_OPEN_READONLY - 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.c_str() << std::endl; - } - catch (std::exception& e) - { - std::cout << "SQLite exception: " << e.what() << std::endl; - return EXIT_FAILURE; // unexpected error : exit the example program - } - - //////////////////////////////////////////////////////////////////////////// - // Simple batch queries example (4/6) : - try - { - // Open a database file in create/write mode - SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); - std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; - - db.exec("DROP TABLE IF EXISTS test"); - - db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); - - // first row - int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); - std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl; - - // second row - nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")"); - std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl; - - // update the second row - nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'"); - std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl; - - // Check the results : expect two row of result - SQLite::Statement query(db, "SELECT * FROM test"); - std::cout << "SELECT * FROM test :\n"; - while (query.executeStep()) - { - std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\")\n"; - } - - db.exec("DROP TABLE test"); - } - catch (std::exception& e) - { - std::cout << "SQLite exception: " << e.what() << std::endl; - return EXIT_FAILURE; // unexpected error : exit the example program - } - remove("test.db3"); - - //////////////////////////////////////////////////////////////////////////// - // RAII transaction example (5/6) : - try - { - // Open a database file in create/write mode - SQLite::Database db("transaction.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); - std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; - - db.exec("DROP TABLE IF EXISTS test"); - - // Exemple of a successful transaction : - try - { - // Begin transaction - SQLite::Transaction transaction(db); - - db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); - - int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); - std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl; - - // Commit transaction - transaction.commit(); - } - catch (std::exception& e) - { - std::cout << "SQLite exception: " << e.what() << std::endl; - return EXIT_FAILURE; // unexpected error : exit the example program - } - - // Exemple of a rollbacked transaction : - try - { - // Begin transaction - SQLite::Transaction transaction(db); - - int nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")"); - std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl; - - nb = db.exec("INSERT INTO test ObviousError"); - std::cout << "INSERT INTO test \"error\", returned " << nb << std::endl; - - return EXIT_FAILURE; // unexpected success : exit the example program - - // Commit transaction - transaction.commit(); - } - catch (std::exception& e) - { - std::cout << "SQLite exception: " << e.what() << std::endl; - // expected error, see above - } - - // Check the results (expect only one row of result, as the second one has been rollbacked by the error) - SQLite::Statement query(db, "SELECT * FROM test"); - std::cout << "SELECT * FROM test :\n"; - while (query.executeStep()) - { - std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\")\n"; - } - } - catch (std::exception& e) - { - std::cout << "SQLite exception: " << e.what() << std::endl; - return EXIT_FAILURE; // unexpected error : exit the example program - } - remove("transaction.db3"); - - //////////////////////////////////////////////////////////////////////////// - // Binary blob and in-memory database example (6/6) : - try - { - // Open a database file in create/write mode - SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); - std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; - - db.exec("DROP TABLE IF EXISTS test"); - db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value BLOB)"); - - FILE* fp = fopen(filename_logo_png, "rb"); - if (NULL != fp) - { - char buffer[16*1024]; - void* blob = &buffer; - int size = static_cast(fread(blob, 1, 16*1024, fp)); - buffer[size] = '\0'; - fclose (fp); - std::cout << "blob size=" << size << " :\n"; - - // Insert query - SQLite::Statement query(db, "INSERT INTO test VALUES (NULL, ?)"); - // Bind the blob value to the first parameter of the SQL query - query.bind(1, blob, size); - std::cout << "blob binded successfully\n"; - - // Execute the one-step query to insert the blob - int nb = query.exec (); - std::cout << "INSERT INTO test VALUES (NULL, ?)\", returned " << nb << std::endl; - } - else - { - std::cout << "file " << filename_logo_png << " not found !\n"; - return EXIT_FAILURE; // unexpected error : exit the example program - } - - fp = fopen("out.png", "wb"); - if (NULL != fp) - { - const void* blob = NULL; - size_t size; - - SQLite::Statement query(db, "SELECT * FROM test"); - std::cout << "SELECT * FROM test :\n"; - if (query.executeStep()) - { - SQLite::Column colBlob = query.getColumn(1); - blob = colBlob.getBlob (); - size = colBlob.getBytes (); - std::cout << "row (" << query.getColumn(0) << ", size=" << size << ")\n"; - size_t sizew = fwrite(blob, 1, size, fp); - SQLITE_CPP_ASSERT(sizew == size); - fclose (fp); - } - } - else - { - std::cout << "file out.png not created !\n"; - return EXIT_FAILURE; // unexpected error : exit the example program - } - } - catch (std::exception& e) - { - std::cout << "SQLite exception: " << e.what() << std::endl; - return EXIT_FAILURE; // unexpected error : exit the example program - } - remove("out.png"); - - std::cout << "everything ok, quitting\n"; - - return EXIT_SUCCESS; -} +/** + * @file main.cpp + * @brief A few short examples in a row. + * + * Demonstrate how-to use the SQLite++ wrapper + * + * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com) + * + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt + * or copy at http://opensource.org/licenses/MIT) + */ + +#include +#include +#include + + +#include "../../src/SQLiteC++.h" + +#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER +namespace SQLite +{ +/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt) +void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg) +{ + // Print a message to the standard error output stream, and abort the program. + std::cerr << apFile << ":" << apLine << ":" << " error: assertion (" << apExpr << ") failed in " << apFunc << ": '" << apMsg << "'\n"; + std::abort(); +} +} +#endif + +/// Example Database +static const char* filename_example_db3 = "examples/example1/example.db3"; +/// Image +static const char* filename_logo_png = "examples/example1/logo.png"; + + +/// Object Oriented Basic example +class Example +{ +public: + // Constructor + Example(void) : + mDb(filename_example_db3), // Open a database file in readonly mode + mQuery(mDb, "SELECT * FROM test WHERE weight > :min_weight")// Compile a SQL query, containing one parameter (index 1) + { + } + virtual ~Example(void) + { + } + + /// List the rows where the "weight" column is greater than the provided aParamValue + void ListGreaterThan (const int aParamValue) + { + std::cout << "ListGreaterThan (" << aParamValue << ")\n"; + + // Bind the integer value provided to the first parameter of the SQL query + mQuery.bind(":min_weight", aParamValue); // same as mQuery.bind(1, aParamValue); + + // Loop to execute the query step by step, to get one a row of results at a time + while (mQuery.executeStep()) + { + std::cout << "row (" << mQuery.getColumn(0) << ", \"" << mQuery.getColumn(1) << "\", " << mQuery.getColumn(2) << ")\n"; + } + + // Reset the query to be able to use it again later + mQuery.reset(); + } + +private: + SQLite::Database mDb; ///< Database connection + SQLite::Statement mQuery; ///< Database prepared SQL query +}; + + +int main (void) +{ + // Basic example (1/6) : + try + { + // Open a database file in readonly mode + SQLite::Database db(filename_example_db3); // SQLITE_OPEN_READONLY + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; + + // Test if the 'test' table exists + bool bExists = db.tableExists("test"); + std::cout << "SQLite table 'test' exists=" << bExists << "\n"; + + // Get a single value result with an easy to use shortcut + std::string value = db.execAndGet("SELECT value FROM test WHERE id=2"); + std::cout << "execAndGet=" << value.c_str() << std::endl; + + // Compile a SQL query, containing one parameter (index 1) + SQLite::Statement query(db, "SELECT * FROM test WHERE weight > ?"); + std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " columns in the result)\n"; + // Bind the integer value 2 to the first parameter of the SQL query + query.bind(1, 2); + std::cout << "binded with integer value '2' :\n"; + + // Loop to execute the query step by step, to get one a row of results at a time + while (query.executeStep()) + { + // Demonstrate how to get some typed column value (and the equivalent explicit call) + int id = query.getColumn(0); // = query.getColumn(0).getInt() + //const char* pvalue = query.getColumn(1); // = query.getColumn(1).getText() + std::string value2 = query.getColumn(1); // = query.getColumn(1).getText() + int bytes = query.getColumn(1).getBytes(); + double weight = query.getColumn(2); // = query.getColumn(2).getInt() + +#ifdef SQLITE_ENABLE_COLUMN_METADATA + static bool bFirst = true; + if (bFirst) + { + // Show how to get the name of a column. + // Requires the SQLITE_ENABLE_COLUMN_METADATA preprocessor macro to be also defined at compile times of the SQLite library. + std::string name0 = query.getColumn(0).getName(); + std::string name1 = query.getColumn(1).getName(); + std::string name2 = query.getColumn(2).getName(); + std::cout << "table 'test' [\"" << name0.c_str() << "\", \"" << name1.c_str() << "\", \"" << name2.c_str() << "\"]\n"; + bFirst = false; + } +#endif + std::cout << "row (" << id << ", \"" << value2.c_str() << "\" " << bytes << " bytes, " << weight << ")\n"; + } + + // Reset the query to use it again + query.reset(); + std::cout << "SQLite statement '" << query.getQuery().c_str() << "' reseted (" << query.getColumnCount () << " columns in the result)\n"; + // Bind the string value "6" to the first parameter of the SQL query + query.bind(1, "6"); + std::cout << "binded with string value \"6\" :\n"; + + while (query.executeStep()) + { + // Demonstrate that inserting column value in a std:ostream is natural + std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\", " << query.getColumn(2) << ")\n"; + } + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } + + //////////////////////////////////////////////////////////////////////////// + // Object Oriented Basic example (2/6) : + try + { + // Open the database and compile the query + Example example; + + // Demonstrate the way to use the same query with different parameter values + example.ListGreaterThan(8); + example.ListGreaterThan(6); + example.ListGreaterThan(2); + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } + + // The execAndGet wrapper example (3/6) : + try + { + // Open a database file in readonly mode + SQLite::Database db(filename_example_db3); // SQLITE_OPEN_READONLY + 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.c_str() << std::endl; + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } + + //////////////////////////////////////////////////////////////////////////// + // Simple batch queries example (4/6) : + try + { + // Open a database file in create/write mode + SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; + + db.exec("DROP TABLE IF EXISTS test"); + + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); + + // first row + int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); + std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl; + + // second row + nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")"); + std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl; + + // update the second row + nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'"); + std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl; + + // Check the results : expect two row of result + SQLite::Statement query(db, "SELECT * FROM test"); + std::cout << "SELECT * FROM test :\n"; + while (query.executeStep()) + { + std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\")\n"; + } + + db.exec("DROP TABLE test"); + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } + remove("test.db3"); + + //////////////////////////////////////////////////////////////////////////// + // RAII transaction example (5/6) : + try + { + // Open a database file in create/write mode + SQLite::Database db("transaction.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; + + db.exec("DROP TABLE IF EXISTS test"); + + // Exemple of a successful transaction : + try + { + // Begin transaction + SQLite::Transaction transaction(db); + + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); + + int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); + std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl; + + // Commit transaction + transaction.commit(); + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } + + // Exemple of a rollbacked transaction : + try + { + // Begin transaction + SQLite::Transaction transaction(db); + + int nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")"); + std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl; + + nb = db.exec("INSERT INTO test ObviousError"); + std::cout << "INSERT INTO test \"error\", returned " << nb << std::endl; + + return EXIT_FAILURE; // unexpected success : exit the example program + + // Commit transaction + transaction.commit(); + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + // expected error, see above + } + + // Check the results (expect only one row of result, as the second one has been rollbacked by the error) + SQLite::Statement query(db, "SELECT * FROM test"); + std::cout << "SELECT * FROM test :\n"; + while (query.executeStep()) + { + std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\")\n"; + } + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } + remove("transaction.db3"); + + //////////////////////////////////////////////////////////////////////////// + // Binary blob and in-memory database example (6/6) : + try + { + // Open a database file in create/write mode + SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; + + db.exec("DROP TABLE IF EXISTS test"); + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value BLOB)"); + + FILE* fp = fopen(filename_logo_png, "rb"); + if (NULL != fp) + { + char buffer[16*1024]; + void* blob = &buffer; + int size = static_cast(fread(blob, 1, 16*1024, fp)); + buffer[size] = '\0'; + fclose (fp); + std::cout << "blob size=" << size << " :\n"; + + // Insert query + SQLite::Statement query(db, "INSERT INTO test VALUES (NULL, ?)"); + // Bind the blob value to the first parameter of the SQL query + query.bind(1, blob, size); + std::cout << "blob binded successfully\n"; + + // Execute the one-step query to insert the blob + int nb = query.exec (); + std::cout << "INSERT INTO test VALUES (NULL, ?)\", returned " << nb << std::endl; + } + else + { + std::cout << "file " << filename_logo_png << " not found !\n"; + return EXIT_FAILURE; // unexpected error : exit the example program + } + + fp = fopen("out.png", "wb"); + if (NULL != fp) + { + const void* blob = NULL; + size_t size; + + SQLite::Statement query(db, "SELECT * FROM test"); + std::cout << "SELECT * FROM test :\n"; + if (query.executeStep()) + { + SQLite::Column colBlob = query.getColumn(1); + blob = colBlob.getBlob (); + size = colBlob.getBytes (); + std::cout << "row (" << query.getColumn(0) << ", size=" << size << ")\n"; + size_t sizew = fwrite(blob, 1, size, fp); + SQLITECPP_ASSERT(sizew == size, "fwrite failed"); // See SQLITECPP_ENABLE_ASSERT_HANDLER + fclose (fp); + } + } + else + { + std::cout << "file out.png not created !\n"; + return EXIT_FAILURE; // unexpected error : exit the example program + } + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } + remove("out.png"); + + std::cout << "everything ok, quitting\n"; + + return EXIT_SUCCESS; +} diff --git a/src/Assert.h b/src/Assert.h new file mode 100644 index 0000000..984902d --- /dev/null +++ b/src/Assert.h @@ -0,0 +1,56 @@ +/** + * @file Assert.h + * @ingroup SQLiteCpp + * @brief Definition of the SQLITECPP_ASSERT() macro. + * + * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com) + * + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt + * or copy at http://opensource.org/licenses/MIT) + */ +#pragma once + +#include + + +/** + * @def SQLITECPP_ASSERT SQLITECPP_ASSERT() is used in destructors, where exceptions shall not be thrown + * + * Define SQLITECPP_ENABLE_ASSERT_HANDLER at the project level + * and define a SQLite::assertion_failed() assertion handler + * to tell SQLiteC++ to use it instead of assert() when an assertion fail. +*/ +#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER + +// if an assert handler is provided by user code, use it instead of assert() +namespace SQLite +{ + // declaration of the assert handler to define in user code + void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg); + +#ifdef _MSC_VER + #define __func__ __FUNCTION__ +#endif +// call the assert handler provided by user code +#define SQLITECPP_ASSERT(expression,message) \ + if (!(expression)) SQLite::assertion_failed(__FILE__, __LINE__, __func__, #expression, message) +} + +#else + +// if no assert handler provided by user code, use standard assert() + +#ifndef NDEBUG + // in debug mode, assert() : + #define SQLITECPP_ASSERT(expression,message) assert(expression) +#else + // in release mode, nothing : + #define SQLITECPP_ASSERT(expression,message) (expression) +#endif + +#endif + + +#ifdef _WIN32 +#pragma warning(disable:4290) // Disable warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow) +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2fa8483..7e88c42 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,7 @@ # add sources of the wrapper as a "SQLiteCpp" static library add_library (SQLiteCpp SQLiteC++.h + Assert.h Column.cpp Column.h Database.cpp diff --git a/src/Database.cpp b/src/Database.cpp index e8985a3..ec18057 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -11,6 +11,7 @@ #include "Database.h" #include "Statement.h" +#include "Assert.h" #include "Exception.h" @@ -37,8 +38,7 @@ Database::~Database(void) throw() // nothrow { int ret = sqlite3_close(mpSQLite); // Never throw an exception in a destructor - //std::cout << sqlite3_errmsg(mpSQLite) << std::endl; - SQLITE_CPP_ASSERT (SQLITE_OK == ret); + SQLITECPP_ASSERT (SQLITE_OK == ret, sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER } // Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT...). diff --git a/src/Exception.h b/src/Exception.h index af866bb..8448113 100644 --- a/src/Exception.h +++ b/src/Exception.h @@ -11,23 +11,6 @@ #pragma once #include -#include - - -// assert() is used in destructors, where exceptions are not allowed -// here you can chose if you want to use them or not -#ifndef NDEBUG - // in debug mode : - #define SQLITE_CPP_ASSERT(expression) assert(expression) -#else - // in release mode : - #define SQLITE_CPP_ASSERT(expression) (expression) -#endif - - -#ifdef _WIN32 -#pragma warning(disable:4290) // Disable warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow) -#endif namespace SQLite diff --git a/src/SQLiteC++.h b/src/SQLiteC++.h index 899c234..8aa3763 100644 --- a/src/SQLiteC++.h +++ b/src/SQLiteC++.h @@ -18,6 +18,7 @@ // Include useful headers of SQLiteC++ +#include "Assert.h" #include "Exception.h" #include "Database.h" #include "Statement.h" diff --git a/src/Statement.cpp b/src/Statement.cpp index a0f7fab..72c9d7c 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -12,6 +12,7 @@ #include "Database.h" #include "Column.h" +#include "Assert.h" #include "Exception.h" @@ -316,8 +317,7 @@ Statement::Ptr::~Ptr(void) throw() // nothrow // as no Statement not Column objet use it anymore int ret = sqlite3_finalize(mpStmt); // Never throw an exception in a destructor - //std::cout << sqlite3_errmsg(mpSQLite) << std::endl; - SQLITE_CPP_ASSERT (SQLITE_OK == ret); + SQLITECPP_ASSERT (SQLITE_OK == ret, sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER // and delete the reference counter delete mpRefCount; diff --git a/src/Transaction.cpp b/src/Transaction.cpp index a1bdb3f..827fe83 100644 --- a/src/Transaction.cpp +++ b/src/Transaction.cpp @@ -11,6 +11,7 @@ #include "Transaction.h" #include "Database.h" +#include "Assert.h" #include "Exception.h" @@ -35,11 +36,10 @@ Transaction::~Transaction(void) throw() // nothrow { mDatabase.exec("ROLLBACK"); } - catch (SQLite::Exception& /*e*/) + catch (SQLite::Exception& e) { // Never throw an exception in a destructor - //std::cout << e.what() << std::endl; - SQLITE_CPP_ASSERT(false); + SQLITECPP_ASSERT(false, e.what()); // See SQLITECPP_ENABLE_ASSERT_HANDLER } } }