Fixed many cpplint warnings

This commit is contained in:
Sébastien Rombauts 2014-03-07 13:12:31 +01:00
parent 64b35bbd4e
commit 6d8b808320
12 changed files with 268 additions and 249 deletions

4
cpplint.py vendored
View File

@ -2032,7 +2032,7 @@ class _NestingState(object):
slots = '' slots = ''
if access_match.group(3): if access_match.group(3):
slots = access_match.group(3) slots = access_match.group(3)
error(filename, linenum, 'whitespace/indent', 3, error(filename, linenum, 'whitespace/indent', 0,
'%s%s: should be indented +1 space inside %s' % ( '%s%s: should be indented +1 space inside %s' % (
access_match.group(2), slots, parent)) access_match.group(2), slots, parent))
@ -3660,7 +3660,7 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
if Match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include): if Match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include):
# Many unit tests use cout, so we exempt them. # Many unit tests use cout, so we exempt them.
if not _IsTestFilename(filename): if not _IsTestFilename(filename):
error(filename, linenum, 'readability/streams', 3, error(filename, linenum, 'readability/streams', 0,
'Streams are highly discouraged.') 'Streams are highly discouraged.')

View File

@ -25,21 +25,22 @@
// if an assert handler is provided by user code, use it instead of assert() // if an assert handler is provided by user code, use it instead of assert()
namespace SQLite namespace SQLite
{ {
// declaration of the assert handler to define in user code // 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); void assertion_failed(const char* apFile, const long apLine, const char* apFunc,
const char* apExpr, const char* apMsg);
#ifdef _MSC_VER #ifdef _MSC_VER
#define __func__ __FUNCTION__ #define __func__ __FUNCTION__
#endif #endif
// call the assert handler provided by user code // call the assert handler provided by user code
#define SQLITECPP_ASSERT(expression,message) \ #define SQLITECPP_ASSERT(expression, message) \
if (!(expression)) SQLite::assertion_failed(__FILE__, __LINE__, __func__, #expression, message) if (!(expression)) SQLite::assertion_failed(__FILE__, __LINE__, __func__, #expression, message)
} } // namespace SQLite
#else #else
// if no assert handler provided by user code, use standard assert() // if no assert handler provided by user code, use standard assert()
// (note: in debug mode, assert() does nothing) // (note: in debug mode, assert() does nothing)
#define SQLITECPP_ASSERT(expression,message) assert(expression && message) #define SQLITECPP_ASSERT(expression, message) assert(expression && message)
#endif #endif

View File

@ -15,6 +15,8 @@
#include <SQLiteCpp/Statement.h> #include <SQLiteCpp/Statement.h>
#include <SQLiteCpp/Exception.h> #include <SQLiteCpp/Exception.h>
#include <string>
namespace SQLite namespace SQLite
{ {
@ -50,14 +52,14 @@ public:
Column(Statement::Ptr& aStmtPtr, int aIndex) noexcept; // nothrow Column(Statement::Ptr& aStmtPtr, int aIndex) noexcept; // nothrow
/// @brief Simple destructor /// @brief Simple destructor
virtual ~Column(void) noexcept; // nothrow virtual ~Column(void) noexcept; // nothrow
// default copy constructor and assignment operator are perfectly suited : // default copy constructor and assignment operator are perfectly suited :
// they copy the Statement::Ptr which in turn increments the reference counter. // they copy the Statement::Ptr which in turn increments the reference counter.
/** /**
* @brief Return a pointer to the named assigned to a result column (potentially aliased) * @brief Return a pointer to the named assigned to a result column (potentially aliased)
*/ */
const char* getName (void) const noexcept; // nothrow const char* getName(void) const noexcept; // nothrow
#ifdef SQLITE_ENABLE_COLUMN_METADATA #ifdef SQLITE_ENABLE_COLUMN_METADATA
/** /**
@ -67,13 +69,13 @@ public:
* - when building the SQLite library itself (which is the case for the Debian libsqlite3 binary for instance), * - when building the SQLite library itself (which is the case for the Debian libsqlite3 binary for instance),
* - and also when compiling this wrapper. * - and also when compiling this wrapper.
*/ */
const char* getOriginName (void) const noexcept; // nothrow const char* getOriginName(void) const noexcept; // nothrow
#endif #endif
/// @brief Return the integer value of the column. /// @brief Return the integer value of the column.
int getInt (void) const noexcept; // nothrow int getInt(void) const noexcept; // nothrow
/// @brief Return the 64bits integer value of the column. /// @brief Return the 64bits integer value of the column.
sqlite3_int64 getInt64 (void) const noexcept; // nothrow sqlite3_int64 getInt64(void) const noexcept; // nothrow
/// @brief Return the double (64bits float) value of the column. /// @brief Return the double (64bits float) value of the column.
double getDouble(void) const noexcept; // nothrow double getDouble(void) const noexcept; // nothrow
/** /**
@ -82,15 +84,15 @@ public:
* @warning The value pointed at is only valid while the statement is valid (ie. not finalized), * @warning The value pointed at is only valid while the statement is valid (ie. not finalized),
* thus you must copy it before using it beyond its scope (to a std::string for instance). * thus you must copy it before using it beyond its scope (to a std::string for instance).
*/ */
const char* getText (const char* apDefaultValue = "") const noexcept; // nothrow const char* getText(const char* apDefaultValue = "") const noexcept; // nothrow
/** /**
* @brief Return a pointer to the binary blob value of the column. * @brief Return a pointer to the binary blob value of the column.
* *
* @warning The value pointed at is only valid while the statement is valid (ie. not finalized), * @warning The value pointed at is only valid while the statement is valid (ie. not finalized),
* thus you must copy it before using it beyond its scope (to a std::string for instance). * thus you must copy it before using it beyond its scope (to a std::string for instance).
*/ */
const void* getBlob (void) const noexcept; // nothrow const void* getBlob(void) const noexcept; // nothrow
/** /**
* @brief Return the type of the value of the column * @brief Return the type of the value of the column
* *
@ -100,7 +102,7 @@ public:
* the value returned by sqlite3_column_type() is undefined. * the value returned by sqlite3_column_type() is undefined.
*/ */
int getType(void) const noexcept; // nothrow int getType(void) const noexcept; // nothrow
/// @brief Test if the column is an integer type value (meaningful only before any conversion) /// @brief Test if the column is an integer type value (meaningful only before any conversion)
inline bool isInteger(void) const noexcept // nothrow inline bool isInteger(void) const noexcept // nothrow
{ {
@ -179,7 +181,7 @@ public:
} }
#ifdef __GNUC__ #ifdef __GNUC__
// NOTE : the following is required by GCC to cast a Column result in a std::string // NOTE : the following is required by GCC to cast a Column result in a std::string
// (error: conversion from SQLite::Column to non-scalar type std::string {aka std::basic_string<char>} requested) // (error: conversion from SQLite::Column to non-scalar type std::string {aka std::basic_string<char>})
// but is not working under Microsoft Visual Studio 2010 and 2012 // but is not working under Microsoft Visual Studio 2010 and 2012
// (error C2440: 'initializing' : cannot convert from 'SQLite::Column' to 'std::basic_string<_Elem,_Traits,_Ax>' // (error C2440: 'initializing' : cannot convert from 'SQLite::Column' to 'std::basic_string<_Elem,_Traits,_Ax>'
// [...] constructor overload resolution was ambiguous) // [...] constructor overload resolution was ambiguous)

View File

@ -14,6 +14,8 @@
#include <SQLiteCpp/Column.h> #include <SQLiteCpp/Column.h>
#include <string>
namespace SQLite namespace SQLite
{ {
@ -54,8 +56,10 @@ public:
* @param[in] apFilename UTF-8 path/uri to the database file ("filename" sqlite3 parameter) * @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... * @param[in] aFlags SQLITE_OPEN_READONLY/SQLITE_OPEN_READWRITE/SQLITE_OPEN_CREATE...
* @param[in] apVfs UTF-8 name of custom VFS to use, or nullptr for sqlite3 default * @param[in] apVfs UTF-8 name of custom VFS to use, or nullptr for sqlite3 default
*
* @throw SQLite::Exception in case of error
*/ */
Database(const char* apFilename, const int aFlags = SQLITE_OPEN_READONLY, const char * apVfs = NULL); // throw(SQLite::Exception); Database(const char* apFilename, const int aFlags = SQLITE_OPEN_READONLY, const char * apVfs = NULL);
/** /**
* @brief Open the provided database UTF-8 filename. * @brief Open the provided database UTF-8 filename.
@ -70,14 +74,18 @@ public:
* @param[in] aFilename UTF-8 path/uri to the database file ("filename" sqlite3 parameter) * @param[in] aFilename UTF-8 path/uri to the database file ("filename" sqlite3 parameter)
* @param[in] aFlags SQLITE_OPEN_READONLY/SQLITE_OPEN_READWRITE/SQLITE_OPEN_CREATE... * @param[in] aFlags SQLITE_OPEN_READONLY/SQLITE_OPEN_READWRITE/SQLITE_OPEN_CREATE...
* @param[in] aVfs UTF-8 name of custom VFS to use, or empty string for sqlite3 default * @param[in] aVfs UTF-8 name of custom VFS to use, or empty string for sqlite3 default
*
* @throw SQLite::Exception in case of error
*/ */
Database(const std::string& aFilename, const int aFlags = SQLITE_OPEN_READONLY, const std::string& aVfs = ""); // throw(SQLite::Exception); Database(const std::string& aFilename, const int aFlags = SQLITE_OPEN_READONLY, const std::string& aVfs = "");
/** /**
* @brief Close the SQLite database connection. * @brief Close the SQLite database connection.
* *
* All SQLite statements must have been finalized before, * All SQLite statements must have been finalized before,
* so all Statement objects must have been unregistered. * so all Statement objects must have been unregistered.
*
* @warning assert in case of error
*/ */
virtual ~Database(void) noexcept; // nothrow virtual ~Database(void) noexcept; // nothrow
@ -98,7 +106,7 @@ public:
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
int exec(const char* apQueries); // throw(SQLite::Exception); int exec(const char* apQueries);
/** /**
* @brief Shortcut to execute one or multiple statements without results. * @brief Shortcut to execute one or multiple statements without results.
@ -117,7 +125,7 @@ public:
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
inline int exec(const std::string& aQueries) // throw(SQLite::Exception); inline int exec(const std::string& aQueries)
{ {
return exec(aQueries.c_str()); return exec(aQueries.c_str());
} }
@ -141,7 +149,7 @@ public:
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
Column execAndGet(const char* apQuery); // throw(SQLite::Exception); Column execAndGet(const char* apQuery);
/** /**
* @brief Shortcut to execute a one step query and fetch the first column of the result. * @brief Shortcut to execute a one step query and fetch the first column of the result.
@ -162,7 +170,7 @@ public:
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
inline Column execAndGet(const std::string& aQuery) // throw(SQLite::Exception); inline Column execAndGet(const std::string& aQuery)
{ {
return execAndGet(aQuery.c_str()); return execAndGet(aQuery.c_str());
} }
@ -178,7 +186,7 @@ public:
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
bool tableExists(const char* apTableName); // throw(SQLite::Exception); bool tableExists(const char* apTableName);
/** /**
* @brief Shortcut to test if a table exists. * @brief Shortcut to test if a table exists.
@ -191,7 +199,7 @@ public:
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
inline bool tableExists(const std::string& aTableName) // throw(SQLite::Exception); inline bool tableExists(const std::string& aTableName)
{ {
return tableExists(aTableName.c_str()); return tableExists(aTableName.c_str());
} }
@ -201,7 +209,7 @@ public:
* *
* @param[in] aTimeoutMs Amount of milliseconds to wait before returning SQLITE_BUSY * @param[in] aTimeoutMs Amount of milliseconds to wait before returning SQLITE_BUSY
*/ */
inline int setBusyTimeout(int aTimeoutMs) // noexcept; nothrow inline int setBusyTimeout(int aTimeoutMs) noexcept // nothrow
{ {
return sqlite3_busy_timeout(mpSQLite, aTimeoutMs); return sqlite3_busy_timeout(mpSQLite, aTimeoutMs);
} }
@ -211,7 +219,7 @@ public:
* *
* @return Rowid of the most recent successful INSERT into the database, or 0 if there was none. * @return Rowid of the most recent successful INSERT into the database, or 0 if there was none.
*/ */
inline sqlite3_int64 getLastInsertRowid(void) const // noexcept; nothrow inline sqlite3_int64 getLastInsertRowid(void) const noexcept // nothrow
{ {
return sqlite3_last_insert_rowid(mpSQLite); return sqlite3_last_insert_rowid(mpSQLite);
} }
@ -219,7 +227,7 @@ public:
/** /**
* @brief Return the filename used to open the database * @brief Return the filename used to open the database
*/ */
inline const std::string& getFilename(void) const inline const std::string& getFilename(void) const noexcept // nothrow
{ {
return mFilename; return mFilename;
} }
@ -227,11 +235,11 @@ public:
/** /**
* @brief Return UTF-8 encoded English language explanation of the most recent error. * @brief Return UTF-8 encoded English language explanation of the most recent error.
*/ */
inline const char* errmsg(void) const inline const char* errmsg(void) const noexcept // nothrow
{ {
return sqlite3_errmsg(mpSQLite); return sqlite3_errmsg(mpSQLite);
} }
/** /**
* @brief Create or redefine a SQL function or aggregate in the sqlite database. * @brief Create or redefine a SQL function or aggregate in the sqlite database.
* *
@ -243,7 +251,7 @@ public:
* @param[in] apFuncName Name of the SQL function to be created or redefined * @param[in] apFuncName Name of the SQL function to be created or redefined
* @param[in] aNbArg Number of arguments in the function * @param[in] aNbArg Number of arguments in the function
* @param[in] abDeterministic Optimize for deterministic functions (most are). A random number generator is not. * @param[in] abDeterministic Optimize for deterministic functions (most are). A random number generator is not.
* @param[in] apApp Arbitrary pointer ot user data, accessible with sqlite3_user_data(). * @param[in] apApp Arbitrary pointer of user data, accessible with sqlite3_user_data().
* @param[in] apFunc Pointer to a C-function to implement a scalar SQL function (apStep & apFinal NULL) * @param[in] apFunc Pointer to a C-function to implement a scalar SQL function (apStep & apFinal NULL)
* @param[in] apStep Pointer to a C-function to implement an aggregate SQL function (apFunc NULL) * @param[in] apStep Pointer to a C-function to implement an aggregate SQL function (apFunc NULL)
* @param[in] apFinal Pointer to a C-function to implement an aggregate SQL function (apFunc NULL) * @param[in] apFinal Pointer to a C-function to implement an aggregate SQL function (apFunc NULL)
@ -257,7 +265,7 @@ public:
void* apApp, void* apApp,
void (*apFunc)(sqlite3_context *, int, sqlite3_value **), void (*apFunc)(sqlite3_context *, int, sqlite3_value **),
void (*apStep)(sqlite3_context *, int, sqlite3_value **), void (*apStep)(sqlite3_context *, int, sqlite3_value **),
void (*apFinal)(sqlite3_context *), void (*apFinal)(sqlite3_context *), // NOLINT(readability/casting)
void (*apDestroy)(void *)); void (*apDestroy)(void *));
/** /**
@ -271,7 +279,7 @@ public:
* @param[in] aFuncName Name of the SQL function to be created or redefined * @param[in] aFuncName Name of the SQL function to be created or redefined
* @param[in] aNbArg Number of arguments in the function * @param[in] aNbArg Number of arguments in the function
* @param[in] abDeterministic Optimize for deterministic functions (most are). A random number generator is not. * @param[in] abDeterministic Optimize for deterministic functions (most are). A random number generator is not.
* @param[in] apApp Arbitrary pointer ot user data, accessible with sqlite3_user_data(). * @param[in] apApp Arbitrary pointer of user data, accessible with sqlite3_user_data().
* @param[in] apFunc Pointer to a C-function to implement a scalar SQL function (apStep & apFinal NULL) * @param[in] apFunc Pointer to a C-function to implement a scalar SQL function (apStep & apFinal NULL)
* @param[in] apStep Pointer to a C-function to implement an aggregate SQL function (apFunc NULL) * @param[in] apStep Pointer to a C-function to implement an aggregate SQL function (apFunc NULL)
* @param[in] apFinal Pointer to a C-function to implement an aggregate SQL function (apFunc NULL) * @param[in] apFinal Pointer to a C-function to implement an aggregate SQL function (apFunc NULL)
@ -285,7 +293,7 @@ public:
void* apApp, void* apApp,
void (*apFunc)(sqlite3_context *, int, sqlite3_value **), void (*apFunc)(sqlite3_context *, int, sqlite3_value **),
void (*apStep)(sqlite3_context *, int, sqlite3_value **), void (*apStep)(sqlite3_context *, int, sqlite3_value **),
void (*apFinal)(sqlite3_context *), void (*apFinal)(sqlite3_context *), // NOLINT(readability/casting)
void (*apDestroy)(void *)) void (*apDestroy)(void *))
{ {
return createFunction(aFuncName.c_str(), aNbArg, abDeterministic, return createFunction(aFuncName.c_str(), aNbArg, abDeterministic,
@ -301,7 +309,7 @@ private:
/** /**
* @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message * @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
*/ */
void check(const int aRet) const; // throw(SQLite::Exception); void check(const int aRet) const;
private: private:
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle

View File

@ -11,6 +11,7 @@
#pragma once #pragma once
#include <stdexcept> #include <stdexcept>
#include <string>
namespace SQLite namespace SQLite
@ -28,7 +29,7 @@ public:
* *
* @param[in] aErrorMessage The string message describing the SQLite error * @param[in] aErrorMessage The string message describing the SQLite error
*/ */
Exception(const std::string& aErrorMessage) : explicit Exception(const std::string& aErrorMessage) :
std::runtime_error(aErrorMessage) std::runtime_error(aErrorMessage)
{ {
} }
@ -40,7 +41,7 @@ public:
/// Compatibility with non-clang compilers. /// Compatibility with non-clang compilers.
#ifndef __has_feature #ifndef __has_feature
#define __has_feature(x) 0 #define __has_feature(x) 0
#endif #endif
// Detect whether the compiler supports C++11 noexcept exception specifications. // Detect whether the compiler supports C++11 noexcept exception specifications.

View File

@ -1,42 +1,42 @@
/** /**
* @file SQLiteCpp.h * @file SQLiteCpp.h
* @ingroup SQLiteCpp * @ingroup SQLiteCpp
* @brief SQLiteC++ is a smart and simple C++ SQLite3 wrapper. This file is only "easy include" for other files. * @brief SQLiteC++ is a smart and simple C++ SQLite3 wrapper. This file is only "easy include" for other files.
* *
* Include this main header file in your project to gain access to all functionality provided by the wrapper. * Include this main header file in your project to gain access to all functionality provided by the wrapper.
* *
* Copyright (c) 2012-2014 Sebastien Rombauts (sebastien.rombauts@gmail.com) * Copyright (c) 2012-2014 Sebastien Rombauts (sebastien.rombauts@gmail.com)
* *
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT) * or copy at http://opensource.org/licenses/MIT)
*/ */
/** /**
* @defgroup SQLiteCpp SQLiteC++ * @defgroup SQLiteCpp SQLiteC++
* @brief SQLiteC++ is a smart and simple C++ SQLite3 wrapper. This file is only "easy include" for other files. * @brief SQLiteC++ is a smart and simple C++ SQLite3 wrapper. This file is only "easy include" for other files.
*/ */
#pragma once #pragma once
// Include useful headers of SQLiteC++ // Include useful headers of SQLiteC++
#include <SQLiteCpp/Assertion.h> #include <SQLiteCpp/Assertion.h>
#include <SQLiteCpp/Exception.h> #include <SQLiteCpp/Exception.h>
#include <SQLiteCpp/Database.h> #include <SQLiteCpp/Database.h>
#include <SQLiteCpp/Statement.h> #include <SQLiteCpp/Statement.h>
#include <SQLiteCpp/Column.h> #include <SQLiteCpp/Column.h>
#include <SQLiteCpp/Transaction.h> #include <SQLiteCpp/Transaction.h>
/** /**
* @brief Version numbers for SQLiteC++ are provided in the same way as sqlite3.h * @brief Version numbers for SQLiteC++ are provided in the same way as sqlite3.h
* *
* The [SQLITECPP_VERSION] C preprocessor macro in the SQLiteC++.h header * The [SQLITECPP_VERSION] C preprocessor macro in the SQLiteC++.h header
* evaluates to a string literal that is the SQLite version in the * evaluates to a string literal that is the SQLite version in the
* format "X.Y.Z" where X is the major version number * format "X.Y.Z" where X is the major version number
* and Y is the minor version number and Z is the release number. * and Y is the minor version number and Z is the release number.
* *
* The [SQLITECPP_VERSION_NUMBER] C preprocessor macro resolves to an integer * The [SQLITECPP_VERSION_NUMBER] C preprocessor macro resolves to an integer
* with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same * with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
* numbers used in [SQLITECPP_VERSION]. * numbers used in [SQLITECPP_VERSION].
*/ */
#define SQLITECPP_VERSION "0.9.9" #define SQLITECPP_VERSION "0.9.9"
#define SQLITECPP_VERSION_NUMBER 0009009 #define SQLITECPP_VERSION_NUMBER 0009009

View File

@ -54,7 +54,7 @@ public:
* *
* Exception is thrown in case of error, then the Statement object is NOT constructed. * Exception is thrown in case of error, then the Statement object is NOT constructed.
*/ */
Statement(Database& aDatabase, const char* apQuery); // throw(SQLite::Exception); Statement(Database& aDatabase, const char* apQuery);
/** /**
* @brief Compile and register the SQL query for the provided SQLite Database Connection * @brief Compile and register the SQL query for the provided SQLite Database Connection
@ -64,7 +64,7 @@ public:
* *
* Exception is thrown in case of error, then the Statement object is NOT constructed. * Exception is thrown in case of error, then the Statement object is NOT constructed.
*/ */
Statement(Database& aDatabase, const std::string& aQuery); // throw(SQLite::Exception); Statement(Database& aDatabase, const std::string& aQuery);
/** /**
* @brief Finalize and unregister the SQL query from the SQLite Database Connection. * @brief Finalize and unregister the SQL query from the SQLite Database Connection.
@ -74,7 +74,7 @@ public:
/** /**
* @brief Reset the statement to make it ready for a new execution. * @brief Reset the statement to make it ready for a new execution.
*/ */
void reset(void); // throw(SQLite::Exception); void reset(void);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Bind a value to a parameter of the SQL statement, // Bind a value to a parameter of the SQL statement,
@ -86,7 +86,7 @@ public:
// //
// Note that for text and blob values, the SQLITE_TRANSIENT flag is used, // Note that for text and blob values, the SQLITE_TRANSIENT flag is used,
// which tell the sqlite library to make its own copy of the data before the bind() call returns. // which tell the sqlite library to make its own copy of the data before the bind() call returns.
// This choice is done to prevent any common misuses, like passing a pointer to a // This choice is done to prevent any common misuses, like passing a pointer to a
// dynamic allocated and temporary variable (a std::string for instance). // dynamic allocated and temporary variable (a std::string for instance).
// This is under-optimized for static data (a static text define in code) // This is under-optimized for static data (a static text define in code)
// as well as for dynamic allocated buffer which could be transfer to sqlite // as well as for dynamic allocated buffer which could be transfer to sqlite
@ -95,92 +95,92 @@ public:
/** /**
* @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
void bind(const int aIndex, const int& aValue) ; // throw(SQLite::Exception); void bind(const int aIndex, const int& aValue);
/** /**
* @brief Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
void bind(const int aIndex, const sqlite3_int64& aValue) ; // throw(SQLite::Exception); void bind(const int aIndex, const sqlite3_int64& aValue);
/** /**
* @brief Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
void bind(const int aIndex, const double& aValue) ; // throw(SQLite::Exception); void bind(const int aIndex, const double& aValue);
/** /**
* @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const int aIndex, const std::string& aValue) ; // throw(SQLite::Exception); void bind(const int aIndex, const std::string& aValue);
/** /**
* @brief Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const int aIndex, const char* apValue) ; // throw(SQLite::Exception); void bind(const int aIndex, const char* apValue);
/** /**
* @brief Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const int aIndex, const void* apValue, const int aSize) ; // throw(SQLite::Exception); void bind(const int aIndex, const void* apValue, const int aSize);
/** /**
* @brief Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
void bind(const int aIndex); // throw(SQLite::Exception); void bind(const int aIndex);
/** /**
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
void bind(const char* apName, const int& aValue) ; // throw(SQLite::Exception); void bind(const char* apName, const int& aValue);
/** /**
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
void bind(const char* apName, const sqlite3_int64& aValue) ; // throw(SQLite::Exception); void bind(const char* apName, const sqlite3_int64& aValue);
/** /**
* @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
void bind(const char* apName, const double& aValue) ; // throw(SQLite::Exception); void bind(const char* apName, const double& aValue);
/** /**
* @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const char* apName, const std::string& aValue) ; // throw(SQLite::Exception); void bind(const char* apName, const std::string& aValue);
/** /**
* @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a text value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const char* apName, const char* apValue) ; // throw(SQLite::Exception); void bind(const char* apName, const char* apValue);
/** /**
* @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a binary blob value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
void bind(const char* apName, const void* apValue, const int aSize) ; // throw(SQLite::Exception); void bind(const char* apName, const void* apValue, const int aSize);
/** /**
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
void bind(const char* apName); // throw(SQLite::Exception); // bind NULL value void bind(const char* apName); // bind NULL value
/** /**
* @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
inline void bind(const std::string& aName, const int& aValue) // throw(SQLite::Exception); inline void bind(const std::string& aName, const int& aValue)
{ {
bind(aName.c_str(), aValue); bind(aName.c_str(), aValue);
} }
/** /**
* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
inline void bind(const std::string& aName, const sqlite3_int64& aValue) // throw(SQLite::Exception); inline void bind(const std::string& aName, const sqlite3_int64& aValue)
{ {
bind(aName.c_str(), aValue); bind(aName.c_str(), aValue);
} }
/** /**
* @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
inline void bind(const std::string& aName, const double& aValue) // throw(SQLite::Exception); inline void bind(const std::string& aName, const double& aValue)
{ {
bind(aName.c_str(), aValue); bind(aName.c_str(), aValue);
} }
@ -189,7 +189,7 @@ public:
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
inline void bind(const std::string& aName, const std::string& aValue) // throw(SQLite::Exception); inline void bind(const std::string& aName, const std::string& aValue)
{ {
bind(aName.c_str(), aValue); bind(aName.c_str(), aValue);
} }
@ -198,7 +198,7 @@ public:
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
inline void bind(const std::string& aName, const char* apValue) // throw(SQLite::Exception); inline void bind(const std::string& aName, const char* apValue)
{ {
bind(aName.c_str(), apValue); bind(aName.c_str(), apValue);
} }
@ -207,14 +207,14 @@ public:
* *
* @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use * @note This uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
*/ */
inline void bind(const std::string& aName, const void* apValue, const int aSize) // throw(SQLite::Exception); inline void bind(const std::string& aName, const void* apValue, const int aSize)
{ {
bind(aName.c_str(), apValue, aSize); bind(aName.c_str(), apValue, aSize);
} }
/** /**
* @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1) * @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (aIndex >= 1)
*/ */
inline void bind(const std::string& aName) // throw(SQLite::Exception); // bind NULL value inline void bind(const std::string& aName) // bind NULL value
{ {
bind(aName.c_str()); bind(aName.c_str());
} }
@ -238,7 +238,7 @@ public:
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
bool executeStep(void); // throw(SQLite::Exception); bool executeStep(void);
/** /**
* @brief Execute a one-step query with no expected result. * @brief Execute a one-step query with no expected result.
@ -259,7 +259,7 @@ public:
* *
* @throw SQLite::Exception in case of error, or if row of results are returned ! * @throw SQLite::Exception in case of error, or if row of results are returned !
*/ */
int exec(void); // throw(SQLite::Exception); int exec(void);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
@ -288,7 +288,7 @@ public:
* Thus, you should instead extract immediately its data (getInt(), getText()...) * Thus, you should instead extract immediately its data (getInt(), getText()...)
* and use or copy this data for any later usage. * and use or copy this data for any later usage.
*/ */
Column getColumn(const int aIndex); // throw(SQLite::Exception); Column getColumn(const int aIndex);
/** /**
* @brief Test if the column value is NULL * @brief Test if the column value is NULL
@ -297,7 +297,7 @@ public:
* *
* @return true if the column value is NULL * @return true if the column value is NULL
*/ */
bool isColumnNull(const int aIndex) const; // throw(SQLite::Exception); bool isColumnNull(const int aIndex) const;
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
@ -365,7 +365,8 @@ public:
private: private:
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object sqlite3_stmt* mpStmt; //!< Pointer to SQLite Statement Object
unsigned int* mpRefCount; //!< Pointer to the heap allocated reference counter of the sqlite3_stmt (to share it with Column objects) unsigned int* mpRefCount; //!< Pointer to the heap allocated reference counter of the sqlite3_stmt
//!< (to share it with Column objects)
}; };
private: private:
@ -379,7 +380,7 @@ private:
* *
* @param[in] SQLite return code to test against the SQLITE_OK expected value * @param[in] SQLite return code to test against the SQLITE_OK expected value
*/ */
void check(const int aRet) const; // throw(SQLite::Exception); void check(const int aRet) const;
private: private:
std::string mQuery; //!< UTF-8 SQL Query std::string mQuery; //!< UTF-8 SQL Query

View File

@ -50,7 +50,7 @@ public:
* *
* Exception is thrown in case of error, then the Transaction is NOT initiated. * Exception is thrown in case of error, then the Transaction is NOT initiated.
*/ */
explicit Transaction(Database& aDatabase); // throw(SQLite::Exception); explicit Transaction(Database& aDatabase);
/** /**
* @brief Safely rollback the transaction if it has not been committed. * @brief Safely rollback the transaction if it has not been committed.
@ -60,7 +60,7 @@ public:
/** /**
* @brief Commit the transaction. * @brief Commit the transaction.
*/ */
void commit(void); // throw(SQLite::Exception); void commit(void);
private: private:
// Transaction must be non-copyable // Transaction must be non-copyable

View File

@ -1,98 +1,98 @@
/** /**
* @file Column.cpp * @file Column.cpp
* @ingroup SQLiteCpp * @ingroup SQLiteCpp
* @brief Encapsulation of a Column in a row of the result pointed by the prepared SQLite::Statement. * @brief Encapsulation of a Column in a row of the result pointed by the prepared SQLite::Statement.
* *
* Copyright (c) 2012-2014 Sebastien Rombauts (sebastien.rombauts@gmail.com) * Copyright (c) 2012-2014 Sebastien Rombauts (sebastien.rombauts@gmail.com)
* *
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT) * or copy at http://opensource.org/licenses/MIT)
*/ */
#include <SQLiteCpp/Column.h> #include <SQLiteCpp/Column.h>
#include <iostream> #include <iostream>
namespace SQLite namespace SQLite
{ {
// Encapsulation of a Column in a row of the result pointed by the prepared Statement. // Encapsulation of a Column in a row of the result pointed by the prepared Statement.
Column::Column(Statement::Ptr& aStmtPtr, int aIndex) noexcept : // nothrow Column::Column(Statement::Ptr& aStmtPtr, int aIndex) noexcept : // nothrow
mStmtPtr (aStmtPtr), mStmtPtr(aStmtPtr),
mIndex (aIndex) mIndex(aIndex)
{ {
} }
// Finalize and unregister the SQL query from the SQLite Database Connection. // Finalize and unregister the SQL query from the SQLite Database Connection.
Column::~Column(void) noexcept // nothrow Column::~Column(void) noexcept // nothrow
{ {
// the finalization will be done by the destructor of the last shared pointer // the finalization will be done by the destructor of the last shared pointer
} }
// Return the named assigned to this result column (potentially aliased) // Return the named assigned to this result column (potentially aliased)
const char * Column::getName(void) const noexcept // nothrow const char * Column::getName(void) const noexcept // nothrow
{ {
return sqlite3_column_name(mStmtPtr, mIndex); return sqlite3_column_name(mStmtPtr, mIndex);
} }
#ifdef SQLITE_ENABLE_COLUMN_METADATA #ifdef SQLITE_ENABLE_COLUMN_METADATA
// Return the name of the table column that is the origin of this result column // Return the name of the table column that is the origin of this result column
const char * Column::getOriginName(void) const noexcept // nothrow const char * Column::getOriginName(void) const noexcept // nothrow
{ {
return sqlite3_column_origin_name(mStmtPtr, mIndex); return sqlite3_column_origin_name(mStmtPtr, mIndex);
} }
#endif #endif
// Return the integer value of the column specified by its index starting at 0 // Return the integer value of the column specified by its index starting at 0
int Column::getInt(void) const noexcept // nothrow int Column::getInt(void) const noexcept // nothrow
{ {
return sqlite3_column_int(mStmtPtr, mIndex); return sqlite3_column_int(mStmtPtr, mIndex);
} }
// Return the 64bits integer value of the column specified by its index starting at 0 // Return the 64bits integer value of the column specified by its index starting at 0
sqlite3_int64 Column::getInt64(void) const noexcept // nothrow sqlite3_int64 Column::getInt64(void) const noexcept // nothrow
{ {
return sqlite3_column_int64(mStmtPtr, mIndex); return sqlite3_column_int64(mStmtPtr, mIndex);
} }
// Return the double value of the column specified by its index starting at 0 // Return the double value of the column specified by its index starting at 0
double Column::getDouble(void) const noexcept // nothrow double Column::getDouble(void) const noexcept // nothrow
{ {
return sqlite3_column_double(mStmtPtr, mIndex); return sqlite3_column_double(mStmtPtr, mIndex);
} }
// Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0 // Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0
const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcept // nothrow const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcept // nothrow
{ {
const char* pText = (const char*)sqlite3_column_text(mStmtPtr, mIndex); const char* pText = (const char*)sqlite3_column_text(mStmtPtr, mIndex);
return (pText?pText:apDefaultValue); return (pText?pText:apDefaultValue);
} }
// Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0 // Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0
const void* Column::getBlob(void) const noexcept // nothrow const void* Column::getBlob(void) const noexcept // nothrow
{ {
return sqlite3_column_blob(mStmtPtr, mIndex); return sqlite3_column_blob(mStmtPtr, mIndex);
} }
// Return the type of the value of the column // Return the type of the value of the column
int Column::getType(void) const noexcept // nothrow int Column::getType(void) const noexcept // nothrow
{ {
return sqlite3_column_type(mStmtPtr, mIndex); return sqlite3_column_type(mStmtPtr, mIndex);
} }
// Return the number of bytes used by the text value of the column // Return the number of bytes used by the text value of the column
int Column::getBytes(void) const noexcept // nothrow int Column::getBytes(void) const noexcept // nothrow
{ {
return sqlite3_column_bytes(mStmtPtr, mIndex); return sqlite3_column_bytes(mStmtPtr, mIndex);
} }
// Standard std::ostream inserter // Standard std::ostream inserter
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn) std::ostream& operator<<(std::ostream& aStream, const Column& aColumn)
{ {
aStream << aColumn.getText(); aStream << aColumn.getText();
return aStream; return aStream;
} }
} // namespace SQLite } // namespace SQLite

View File

@ -14,16 +14,19 @@
#include <SQLiteCpp/Assertion.h> #include <SQLiteCpp/Assertion.h>
#include <SQLiteCpp/Exception.h> #include <SQLiteCpp/Exception.h>
#include <string>
#ifndef SQLITE_DETERMINISTIC #ifndef SQLITE_DETERMINISTIC
#define SQLITE_DETERMINISTIC 0x800 #define SQLITE_DETERMINISTIC 0x800
#endif //SQLITE_DETERMINISTIC #endif // SQLITE_DETERMINISTIC
namespace SQLite namespace SQLite
{ {
// Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags. // Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags.
Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const char* apVfs /*= NULL*/) : // throw(SQLite::Exception) Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const char* apVfs /*= NULL*/) :
mpSQLite(NULL), mpSQLite(NULL),
mFilename(apFilename) mFilename(apFilename)
{ {
@ -37,7 +40,7 @@ Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READ
} }
// Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags. // Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags.
Database::Database(const std::string& aFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const std::string& aVfs /*= ""*/) : // throw(SQLite::Exception) Database::Database(const std::string& aFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const std::string& aVfs) :
mpSQLite(NULL), mpSQLite(NULL),
mFilename(aFilename) mFilename(aFilename)
{ {
@ -55,11 +58,11 @@ Database::~Database(void) noexcept // nothrow
{ {
int ret = sqlite3_close(mpSQLite); int ret = sqlite3_close(mpSQLite);
// Never throw an exception in a destructor // Never throw an exception in a destructor
SQLITECPP_ASSERT (SQLITE_OK == ret, sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER 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...). // Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT...).
int Database::exec(const char* apQueries) // throw(SQLite::Exception); int Database::exec(const char* apQueries)
{ {
int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL); int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL);
check(ret); check(ret);
@ -74,7 +77,7 @@ int Database::exec(const char* apQueries) // throw(SQLite::Exception);
// (when the underlying temporary Statement and Column objects are destroyed) // (when the underlying temporary Statement and Column objects are destroyed)
// this is an issue only for pointer type result (ie. char* and blob) // this is an issue only for pointer type result (ie. char* and blob)
// (use the Column copy-constructor) // (use the Column copy-constructor)
Column Database::execAndGet(const char* apQuery) // throw(SQLite::Exception) Column Database::execAndGet(const char* apQuery)
{ {
Statement query(*this, apQuery); Statement query(*this, apQuery);
(void)query.executeStep(); // Can return false if no result, which will throw next line in getColumn() (void)query.executeStep(); // Can return false if no result, which will throw next line in getColumn()
@ -82,7 +85,7 @@ Column Database::execAndGet(const char* apQuery) // throw(SQLite::Exception)
} }
// Shortcut to test if a table exists. // Shortcut to test if a table exists.
bool Database::tableExists(const char* apTableName) // throw(SQLite::Exception) bool Database::tableExists(const char* apTableName)
{ {
Statement query(*this, "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?"); Statement query(*this, "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?");
query.bind(1, apTableName); query.bind(1, apTableName);
@ -92,14 +95,14 @@ bool Database::tableExists(const char* apTableName) // throw(SQLite::Exception)
} }
// Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message // 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) void Database::check(const int aRet) const
{ {
if (SQLITE_OK != aRet) if (SQLITE_OK != aRet)
{ {
throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); throw SQLite::Exception(sqlite3_errmsg(mpSQLite));
} }
} }
// Attach a custom function to your sqlite database. Assumes UTF8 text representation. // Attach a custom function to your sqlite database. Assumes UTF8 text representation.
// Parameter details can be found here: http://www.sqlite.org/c3ref/create_function.html // Parameter details can be found here: http://www.sqlite.org/c3ref/create_function.html
void Database::createFunction(const char* apFuncName, void Database::createFunction(const char* apFuncName,
@ -108,7 +111,7 @@ void Database::createFunction(const char* apFuncName,
void* apApp, void* apApp,
void (*apFunc)(sqlite3_context *, int, sqlite3_value **), void (*apFunc)(sqlite3_context *, int, sqlite3_value **),
void (*apStep)(sqlite3_context *, int, sqlite3_value **), void (*apStep)(sqlite3_context *, int, sqlite3_value **),
void (*apFinal)(sqlite3_context *), void (*apFinal)(sqlite3_context *), // NOLINT(readability/casting)
void (*apDestroy)(void *)) void (*apDestroy)(void *))
{ {
int TextRep = SQLITE_UTF8; int TextRep = SQLITE_UTF8;
@ -118,7 +121,7 @@ void Database::createFunction(const char* apFuncName,
} }
int ret = sqlite3_create_function_v2(mpSQLite, apFuncName, aNbArg, TextRep, int ret = sqlite3_create_function_v2(mpSQLite, apFuncName, aNbArg, TextRep,
apApp, apFunc, apStep, apFinal, apDestroy); apApp, apFunc, apStep, apFinal, apDestroy);
check(ret); check(ret);
} }

View File

@ -15,12 +15,14 @@
#include <SQLiteCpp/Assertion.h> #include <SQLiteCpp/Assertion.h>
#include <SQLiteCpp/Exception.h> #include <SQLiteCpp/Exception.h>
#include <string>
namespace SQLite namespace SQLite
{ {
// Compile and register the SQL query for the provided SQLite Database Connection // 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) :
mQuery(apQuery), mQuery(apQuery),
mStmtPtr(aDatabase.mpSQLite, mQuery), // prepare the SQL query, and ref count (needs Database friendship) mStmtPtr(aDatabase.mpSQLite, mQuery), // prepare the SQL query, and ref count (needs Database friendship)
mColumnCount(0), mColumnCount(0),
@ -31,7 +33,7 @@ Statement::Statement(Database &aDatabase, const char* apQuery) : // throw(SQLite
} }
// Compile and register the SQL query for the provided SQLite Database Connection // Compile and register the SQL query for the provided SQLite Database Connection
Statement::Statement(Database &aDatabase, const std::string& aQuery) : // throw(SQLite::Exception) Statement::Statement(Database &aDatabase, const std::string& aQuery) :
mQuery(aQuery), mQuery(aQuery),
mStmtPtr(aDatabase.mpSQLite, mQuery), // prepare the SQL query, and ref count (needs Database friendship) mStmtPtr(aDatabase.mpSQLite, mQuery), // prepare the SQL query, and ref count (needs Database friendship)
mColumnCount(0), mColumnCount(0),
@ -48,7 +50,7 @@ Statement::~Statement(void) noexcept // nothrow
} }
// Reset the statement to make it ready for a new execution // Reset the statement to make it ready for a new execution
void Statement::reset(void) // throw(SQLite::Exception) void Statement::reset(void)
{ {
mbOk = false; mbOk = false;
mbDone = false; mbDone = false;
@ -57,49 +59,49 @@ void Statement::reset(void) // throw(SQLite::Exception)
} }
// Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // 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) void Statement::bind(const int aIndex, const int& aValue)
{ {
int ret = sqlite3_bind_int(mStmtPtr, aIndex, aValue); int ret = sqlite3_bind_int(mStmtPtr, aIndex, aValue);
check(ret); check(ret);
} }
// Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // 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) void Statement::bind(const int aIndex, const sqlite3_int64& aValue)
{ {
int ret = sqlite3_bind_int64(mStmtPtr, aIndex, aValue); int ret = sqlite3_bind_int64(mStmtPtr, aIndex, aValue);
check(ret); check(ret);
} }
// Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // 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) void Statement::bind(const int aIndex, const double& aValue)
{ {
int ret = sqlite3_bind_double(mStmtPtr, aIndex, aValue); int ret = sqlite3_bind_double(mStmtPtr, aIndex, aValue);
check(ret); check(ret);
} }
// Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // 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) void Statement::bind(const int aIndex, const std::string& aValue)
{ {
int ret = sqlite3_bind_text(mStmtPtr, aIndex, aValue.c_str(), static_cast<int>(aValue.size()), SQLITE_TRANSIENT); int ret = sqlite3_bind_text(mStmtPtr, aIndex, aValue.c_str(), static_cast<int>(aValue.size()), SQLITE_TRANSIENT);
check(ret); check(ret);
} }
// Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // 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) void Statement::bind(const int aIndex, const char* apValue)
{ {
int ret = sqlite3_bind_text(mStmtPtr, aIndex, apValue, -1, SQLITE_TRANSIENT); int ret = sqlite3_bind_text(mStmtPtr, aIndex, apValue, -1, SQLITE_TRANSIENT);
check(ret); check(ret);
} }
// Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a binary blob value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const int aIndex, const void* apValue, const int aSize) // throw(SQLite::Exception) void Statement::bind(const int aIndex, const void* apValue, const int aSize)
{ {
int ret = sqlite3_bind_blob(mStmtPtr, aIndex, apValue, aSize, SQLITE_TRANSIENT); int ret = sqlite3_bind_blob(mStmtPtr, aIndex, apValue, aSize, SQLITE_TRANSIENT);
check(ret); check(ret);
} }
// Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // 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) void Statement::bind(const int aIndex)
{ {
int ret = sqlite3_bind_null(mStmtPtr, aIndex); int ret = sqlite3_bind_null(mStmtPtr, aIndex);
check(ret); check(ret);
@ -107,7 +109,7 @@ void Statement::bind(const int aIndex) // throw(SQLite::Exception)
// Bind an int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind an int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const char* apName, const int& aValue) // throw(SQLite::Exception) void Statement::bind(const char* apName, const int& aValue)
{ {
int index = sqlite3_bind_parameter_index(mStmtPtr, apName); int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
int ret = sqlite3_bind_int(mStmtPtr, index, aValue); int ret = sqlite3_bind_int(mStmtPtr, index, aValue);
@ -115,7 +117,7 @@ void Statement::bind(const char* apName, const int& aValue) // throw(SQLite::Exc
} }
// Bind a 64bits int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a 64bits int value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const char* apName, const sqlite3_int64& aValue) // throw(SQLite::Exception) void Statement::bind(const char* apName, const sqlite3_int64& aValue)
{ {
int index = sqlite3_bind_parameter_index(mStmtPtr, apName); int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
int ret = sqlite3_bind_int64(mStmtPtr, index, aValue); int ret = sqlite3_bind_int64(mStmtPtr, index, aValue);
@ -123,7 +125,7 @@ void Statement::bind(const char* apName, const sqlite3_int64& aValue) // throw(S
} }
// Bind a double (64bits float) value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a double (64bits float) value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const char* apName, const double& aValue) // throw(SQLite::Exception) void Statement::bind(const char* apName, const double& aValue)
{ {
int index = sqlite3_bind_parameter_index(mStmtPtr, apName); int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
int ret = sqlite3_bind_double(mStmtPtr, index, aValue); int ret = sqlite3_bind_double(mStmtPtr, index, aValue);
@ -131,7 +133,7 @@ void Statement::bind(const char* apName, const double& aValue) // throw(SQLite::
} }
// Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a string value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const char* apName, const std::string& aValue) // throw(SQLite::Exception) void Statement::bind(const char* apName, const std::string& aValue)
{ {
int index = sqlite3_bind_parameter_index(mStmtPtr, apName); int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(), static_cast<int>(aValue.size()), SQLITE_TRANSIENT); int ret = sqlite3_bind_text(mStmtPtr, index, aValue.c_str(), static_cast<int>(aValue.size()), SQLITE_TRANSIENT);
@ -139,7 +141,7 @@ void Statement::bind(const char* apName, const std::string& aValue) // throw(SQL
} }
// Bind a text value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a text value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const char* apName, const char* apValue) // throw(SQLite::Exception) void Statement::bind(const char* apName, const char* apValue)
{ {
int index = sqlite3_bind_parameter_index(mStmtPtr, apName); int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_TRANSIENT); int ret = sqlite3_bind_text(mStmtPtr, index, apValue, -1, SQLITE_TRANSIENT);
@ -147,7 +149,7 @@ void Statement::bind(const char* apName, const char* apValue) // throw(SQLite::E
} }
// Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a binary blob value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const char* apName, const void* apValue, const int aSize) // throw(SQLite::Exception) void Statement::bind(const char* apName, const void* apValue, const int aSize)
{ {
int index = sqlite3_bind_parameter_index(mStmtPtr, apName); int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_TRANSIENT); int ret = sqlite3_bind_blob(mStmtPtr, index, apValue, aSize, SQLITE_TRANSIENT);
@ -155,7 +157,7 @@ void Statement::bind(const char* apName, const void* apValue, const int aSize) /
} }
// Bind a NULL value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement // Bind a NULL value to a parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement
void Statement::bind(const char* apName) // throw(SQLite::Exception) void Statement::bind(const char* apName)
{ {
int index = sqlite3_bind_parameter_index(mStmtPtr, apName); int index = sqlite3_bind_parameter_index(mStmtPtr, apName);
int ret = sqlite3_bind_null(mStmtPtr, index); int ret = sqlite3_bind_null(mStmtPtr, index);
@ -164,7 +166,7 @@ void Statement::bind(const char* apName) // throw(SQLite::Exception)
// Execute a step of the query to fetch one row of results // Execute a step of the query to fetch one row of results
bool Statement::executeStep(void) // throw(SQLite::Exception) bool Statement::executeStep(void)
{ {
if (false == mbDone) if (false == mbDone)
{ {
@ -194,7 +196,7 @@ bool Statement::executeStep(void) // throw(SQLite::Exception)
} }
// Execute a one-step query with no expected result // Execute a one-step query with no expected result
int Statement::exec(void) // throw(SQLite::Exception) int Statement::exec(void)
{ {
if (false == mbDone) if (false == mbDone)
{ {
@ -228,7 +230,7 @@ int Statement::exec(void) // throw(SQLite::Exception)
// Return a copy of the column data specified by its index starting at 0 // Return a copy of the column data specified by its index starting at 0
// (use the Column copy-constructor) // (use the Column copy-constructor)
Column Statement::getColumn(const int aIndex) // throw(SQLite::Exception) Column Statement::getColumn(const int aIndex)
{ {
if (false == mbOk) if (false == mbOk)
{ {
@ -244,7 +246,7 @@ Column Statement::getColumn(const int aIndex) // throw(SQLite::Exception)
} }
// Test if the column is NULL // Test if the column is NULL
bool Statement::isColumnNull(const int aIndex) const // throw(SQLite::Exception) bool Statement::isColumnNull(const int aIndex) const
{ {
if (false == mbOk) if (false == mbOk)
{ {
@ -259,7 +261,7 @@ bool Statement::isColumnNull(const int aIndex) const // throw(SQLite::Exception)
} }
// Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message // Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
void Statement::check(const int aRet) const // throw(SQLite::Exception) void Statement::check(const int aRet) const
{ {
if (SQLITE_OK != aRet) if (SQLITE_OK != aRet)
{ {
@ -291,7 +293,7 @@ Statement::Ptr::Ptr(sqlite3* apSQLite, std::string& aQuery) :
// Initialize the reference counter of the sqlite3_stmt : // Initialize the reference counter of the sqlite3_stmt :
// used to share the mStmtPtr between Statement and Column objects; // used to share the mStmtPtr between Statement and Column objects;
// This is needed to enable Column objects to live longer than the Statement objet it refers to. // This is needed to enable Column objects to live longer than the Statement objet it refers to.
mpRefCount = new unsigned int(1); mpRefCount = new unsigned int(1); // NOLINT(readability/casting)
} }
/** /**
@ -328,7 +330,7 @@ Statement::Ptr::~Ptr(void) noexcept // nothrow
// as no Statement not Column objet use it anymore // as no Statement not Column objet use it anymore
int ret = sqlite3_finalize(mpStmt); int ret = sqlite3_finalize(mpStmt);
// Never throw an exception in a destructor // Never throw an exception in a destructor
SQLITECPP_ASSERT (SQLITE_OK == ret, sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER SQLITECPP_ASSERT(SQLITE_OK == ret, sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER
// and delete the reference counter // and delete the reference counter
delete mpRefCount; delete mpRefCount;
@ -338,4 +340,5 @@ Statement::Ptr::~Ptr(void) noexcept // nothrow
// else, the finalization will be done later, by the last object // else, the finalization will be done later, by the last object
} }
} // namespace SQLite } // namespace SQLite

View File

@ -19,7 +19,7 @@ namespace SQLite
// Begins the SQLite transaction // Begins the SQLite transaction
Transaction::Transaction(Database& aDatabase) : // throw(SQLite::Exception) Transaction::Transaction(Database& aDatabase) :
mDatabase(aDatabase), mDatabase(aDatabase),
mbCommited(false) mbCommited(false)
{ {
@ -44,7 +44,7 @@ Transaction::~Transaction(void) noexcept // nothrow
} }
// Commit the transaction. // Commit the transaction.
void Transaction::commit(void) // throw(SQLite::Exception) void Transaction::commit(void)
{ {
if (false == mbCommited) if (false == mbCommited)
{ {