diff --git a/example.db3 b/example.db3 index d5b967a..eef46c0 100644 Binary files a/example.db3 and b/example.db3 differ diff --git a/src/SQLiteC++/Database.cpp b/src/SQLiteC++/Database.cpp index 983780c..143701a 100644 --- a/src/SQLiteC++/Database.cpp +++ b/src/SQLiteC++/Database.cpp @@ -16,9 +16,9 @@ namespace SQLite { // Open the provided database UTF-8 filename. - Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/) throw (SQLite::Exception) : +Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/) throw(SQLite::Exception) : mpSQLite(NULL), - mFilename (apFilename) + mFilename(apFilename) { int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, NULL); if (SQLITE_OK != ret) @@ -30,7 +30,7 @@ namespace SQLite } // Close the SQLite database connection. -Database::~Database(void) throw () // nothrow +Database::~Database(void) throw() // nothrow { // check for undestroyed statements std::vector::iterator iStatement; @@ -50,19 +50,19 @@ Database::~Database(void) throw () // nothrow } // Register a Statement object (a SQLite query) -void Database::registerStatement (Statement& aStatement) throw (SQLite::Exception) +void Database::registerStatement(Statement& aStatement) throw(SQLite::Exception) { - mStatementList.push_back (&aStatement); + mStatementList.push_back(&aStatement); } // Unregister a Statement object -void Database::unregisterStatement (Statement& aStatement) throw (SQLite::Exception) +void Database::unregisterStatement(Statement& aStatement) throw(SQLite::Exception) { std::vector::iterator iStatement; - iStatement = std::find (mStatementList.begin(), mStatementList.end(), &aStatement); + iStatement = std::find(mStatementList.begin(), mStatementList.end(), &aStatement); if (mStatementList.end() != iStatement) { - mStatementList.erase (iStatement); + mStatementList.erase(iStatement); } } diff --git a/src/SQLiteC++/Database.h b/src/SQLiteC++/Database.h index 77df398..b930246 100644 --- a/src/SQLiteC++/Database.h +++ b/src/SQLiteC++/Database.h @@ -45,7 +45,7 @@ public: * @param[in] apFilename UTF-8 path/uri to the database file ("filename" sqlite3 parameter) * @param[in] aFlags SQLITE_OPEN_READONLY/SQLITE_OPEN_READWRITE/SQLITE_OPEN_CREATE... */ - explicit Database(const char* apFilename, const int aFlags = SQLITE_OPEN_READONLY) throw (SQLite::Exception); + explicit Database(const char* apFilename, const int aFlags = SQLITE_OPEN_READONLY) throw(SQLite::Exception); /** * @brief Close the SQLite database connection. @@ -53,17 +53,17 @@ public: * All SQLite statements must have been finalized before, * so all Statement objects must have been unregistered. */ - virtual ~Database(void) throw (); // nothrow + virtual ~Database(void) throw(); // nothrow /** * @brief Register a Statement object (a SQLite query) */ - void registerStatement (Statement& aStatement) throw (SQLite::Exception); + void registerStatement(Statement& aStatement) throw(SQLite::Exception); /** * @brief Unregister a Statement object */ - void unregisterStatement (Statement& aStatement) throw (SQLite::Exception); + void unregisterStatement(Statement& aStatement) throw(SQLite::Exception); /** * @brief Filename used to open the database diff --git a/src/SQLiteC++/Statement.cpp b/src/SQLiteC++/Statement.cpp index 968751c..9ae6411 100644 --- a/src/SQLiteC++/Statement.cpp +++ b/src/SQLiteC++/Statement.cpp @@ -16,7 +16,7 @@ namespace SQLite { // Compile and register the SQL query for the provided SQLite Database Connection -Statement::Statement(Database &aDatabase, const char* apQuery) throw (SQLite::Exception) : +Statement::Statement(Database &aDatabase, const char* apQuery) throw(SQLite::Exception) : mpStmt(NULL), mDatabase(aDatabase), mQuery(apQuery), @@ -31,7 +31,7 @@ Statement::Statement(Database &aDatabase, const char* apQuery) throw (SQLite::Ex } // Finalize and unregister the SQL query from the SQLite Database Connection. -Statement::~Statement(void) throw () // nothrow +Statement::~Statement(void) throw() // nothrow { int ret = sqlite3_finalize(mpStmt); if (SQLITE_OK != ret) @@ -43,7 +43,7 @@ Statement::~Statement(void) throw () // nothrow } // Reset the statement to make it ready for a new execution -void Statement::reset (void) throw (SQLite::Exception) +void Statement::reset(void) throw(SQLite::Exception) { mbDone = false; int ret = sqlite3_reset(mpStmt); @@ -53,8 +53,69 @@ void Statement::reset (void) throw (SQLite::Exception) } } +// Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement +void Statement::bind(const int aIndex, const int& aValue) throw(SQLite::Exception) +{ + int ret = sqlite3_bind_int(mpStmt, aIndex, aValue); + if (SQLITE_OK != ret) + { + throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); + } +} + +// Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement +void Statement::bind(const int aIndex, const sqlite3_int64& aValue) throw(SQLite::Exception) + +{ + int ret = sqlite3_bind_int64(mpStmt, aIndex, aValue); + if (SQLITE_OK != ret) + { + throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); + } +} + +// Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement +void Statement::bind(const int aIndex, const double& aValue) throw(SQLite::Exception) +{ + int ret = sqlite3_bind_double(mpStmt, aIndex, aValue); + if (SQLITE_OK != ret) + { + throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); + } +} + +// Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement +void Statement::bind(const int aIndex, const std::string& aValue) throw(SQLite::Exception) +{ + int ret = sqlite3_bind_text(mpStmt, aIndex, aValue.c_str(), aValue.size(), SQLITE_TRANSIENT); + if (SQLITE_OK != ret) + { + throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); + } +} + +// Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement +void Statement::bind(const int aIndex, const char* apValue) throw(SQLite::Exception) +{ + int ret = sqlite3_bind_text(mpStmt, aIndex, apValue, -1, SQLITE_TRANSIENT); + if (SQLITE_OK != ret) + { + throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); + } +} + +// Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement +void Statement::bind(const int aIndex) throw(SQLite::Exception) +{ + int ret = sqlite3_bind_null(mpStmt, aIndex); + if (SQLITE_OK != ret) + { + throw SQLite::Exception(sqlite3_errmsg(mDatabase.mpSQLite)); + } +} + // Execute a step of the query to fetch one row of results -bool Statement::executeStep (void) throw (SQLite::Exception) +bool Statement::executeStep(void) throw(SQLite::Exception) { bool bOk = false; @@ -67,7 +128,7 @@ bool Statement::executeStep (void) throw (SQLite::Exception) } else if (SQLITE_DONE == ret) { - bOk = true; + bOk = false; mbDone = true; } else diff --git a/src/SQLiteC++/Statement.h b/src/SQLiteC++/Statement.h index cce74f0..ca0437d 100644 --- a/src/SQLiteC++/Statement.h +++ b/src/SQLiteC++/Statement.h @@ -32,24 +32,47 @@ public: * * Exception is thrown in case of error, then the Statement object is NOT constructed. */ - explicit Statement(Database &aDatabase, const char* apQuery) throw (SQLite::Exception); + explicit Statement(Database &aDatabase, const char* apQuery) throw(SQLite::Exception); /** * @brief Finalize and unregister the SQL query from the SQLite Database Connection. */ - virtual ~Statement(void) throw (); // nothrow + virtual ~Statement(void) throw(); // nothrow /** * @brief Reset the statement to make it ready for a new execution. */ - void reset (void) throw (SQLite::Exception); + void reset(void) throw(SQLite::Exception); - // TODO bind + /** + * @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement + */ + void bind(const int aIndex, const int& aValue) throw(SQLite::Exception); + /** + * @brief Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement + */ + void bind(const int aIndex, const sqlite3_int64& aValue) throw(SQLite::Exception); + /** + * @brief Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement + */ + void bind(const int aIndex, const double& aValue) throw(SQLite::Exception); + /** + * @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement + */ + void bind(const int aIndex, const std::string& aValue) throw(SQLite::Exception); + /** + * @brief Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement + */ + void bind(const int aIndex, const char* apValue) throw(SQLite::Exception); + /** + * @brief Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement + */ + void bind(const int aIndex) throw(SQLite::Exception); // bind NULL value /** * @brief Execute a step of the query to fetch one row of results. */ - bool executeStep (void) throw (SQLite::Exception); + bool executeStep(void) throw(SQLite::Exception); /** * @brief UTF-8 SQL Query. diff --git a/src/example1/main.cpp b/src/example1/main.cpp index c7b6bb7..d7be5ba 100644 --- a/src/example1/main.cpp +++ b/src/example1/main.cpp @@ -3,25 +3,26 @@ int main (void) { - std::cout << "Hello SQLite.hpp\n"; try { + // Open a database file SQLite::Database db("example.db3"); - std::cout << db.getFilename().c_str() << " onpened\n"; + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; - SQLite::Statement stmt(db, "SELECT * FROM test"); - std::cout << "statement created\n"; + // Compile a SQL query, containing one parameter (index 1) + SQLite::Statement query(db, "SELECT * FROM test WHERE size>?"); + // Bind an integer value "6" to the first parameter of the SQL query + query.bind(1, 6); - while (stmt.executeStep()) + while (query.executeStep()) { std::cout << "executeStep\n"; } } catch (std::exception& e) { - std::cout << "SQLite exception: " << e.what() << std::endl; + std::cout << "SQLite exception: " << e.what() << std::endl; } - std::cout << "Bye SQLite.hpp\n"; return 0; }