mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-07 11:16:11 -04:00
Minor refactoring to Statement and Column
This commit is contained in:
parent
2800b65ac6
commit
354323a875
@ -54,7 +54,7 @@ public:
|
|||||||
* @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object.
|
* @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object.
|
||||||
* @param[in] aIndex Index of the column in the row of result, starting at 0
|
* @param[in] aIndex Index of the column in the row of result, starting at 0
|
||||||
*/
|
*/
|
||||||
explicit Column(Statement::TStatementPtr& aStmtPtr, int aIndex) noexcept;
|
explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) noexcept;
|
||||||
|
|
||||||
// default destructor: the finalization will be done by the destructor of the last shared pointer
|
// default destructor: the finalization will be done by the destructor of the last shared pointer
|
||||||
// default copy constructor and assignment operator are perfectly suited :
|
// default copy constructor and assignment operator are perfectly suited :
|
||||||
|
@ -60,7 +60,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);
|
Statement(const 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
|
||||||
@ -70,7 +70,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) :
|
Statement(const Database& aDatabase, const std::string& aQuery) :
|
||||||
Statement(aDatabase, aQuery.c_str())
|
Statement(aDatabase, aQuery.c_str())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@ -122,7 +122,7 @@ public:
|
|||||||
// => if you know what you are doing, use bindNoCopy() instead of bind()
|
// => if you know what you are doing, use bindNoCopy() instead of bind()
|
||||||
|
|
||||||
SQLITECPP_PURE_FUNC
|
SQLITECPP_PURE_FUNC
|
||||||
int getIndex(const char * const apName);
|
int getIndex(const char * const apName) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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)
|
||||||
@ -463,7 +463,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);
|
Column getColumn(const int aIndex) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return a copy of the column data specified by its column name (less efficient than using an index)
|
* @brief Return a copy of the column data specified by its column name (less efficient than using an index)
|
||||||
@ -494,7 +494,7 @@ public:
|
|||||||
*
|
*
|
||||||
* Throw an exception if the specified name is not one of the aliased name of the columns in the result.
|
* Throw an exception if the specified name is not one of the aliased name of the columns in the result.
|
||||||
*/
|
*/
|
||||||
Column getColumn(const char* apName);
|
Column getColumn(const char* apName) const;
|
||||||
|
|
||||||
#if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1900) // c++14: Visual Studio 2015
|
#if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1900) // c++14: Visual Studio 2015
|
||||||
/**
|
/**
|
||||||
@ -617,7 +617,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return a UTF-8 string containing the SQL text of prepared statement with bound parameters expanded.
|
// Return a UTF-8 string containing the SQL text of prepared statement with bound parameters expanded.
|
||||||
std::string getExpandedSQL();
|
std::string getExpandedSQL() const;
|
||||||
|
|
||||||
/// Return the number of columns in the result set returned by the prepared statement
|
/// Return the number of columns in the result set returned by the prepared statement
|
||||||
int getColumnCount() const
|
int getColumnCount() const
|
||||||
@ -646,7 +646,7 @@ public:
|
|||||||
const char* getErrorMsg() const noexcept;
|
const char* getErrorMsg() const noexcept;
|
||||||
|
|
||||||
/// Shared pointer to SQLite Prepared Statement Object
|
/// Shared pointer to SQLite Prepared Statement Object
|
||||||
typedef std::shared_ptr<sqlite3_stmt> TStatementPtr;
|
using TStatementPtr = std::shared_ptr<sqlite3_stmt>;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
@ -699,11 +699,10 @@ private:
|
|||||||
*/
|
*/
|
||||||
sqlite3_stmt* getPreparedStatement() const;
|
sqlite3_stmt* getPreparedStatement() const;
|
||||||
|
|
||||||
private:
|
|
||||||
/// Map of columns index by name (mutable so getColumnIndex can be const)
|
|
||||||
typedef std::map<std::string, int> TColumnNames;
|
|
||||||
|
|
||||||
private:
|
/// Map of columns index by name (mutable so getColumnIndex can be const)
|
||||||
|
using TColumnNames = std::map<std::string, int>;
|
||||||
|
|
||||||
std::string mQuery; //!< UTF-8 SQL Query
|
std::string mQuery; //!< UTF-8 SQL Query
|
||||||
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
|
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
|
||||||
TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object
|
TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object
|
||||||
|
@ -26,7 +26,7 @@ const int Null = SQLITE_NULL;
|
|||||||
|
|
||||||
|
|
||||||
// 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::TStatementPtr& aStmtPtr, int aIndex) noexcept :
|
Column::Column(const Statement::TStatementPtr& aStmtPtr, int aIndex) noexcept :
|
||||||
mStmtPtr(aStmtPtr),
|
mStmtPtr(aStmtPtr),
|
||||||
mIndex(aIndex)
|
mIndex(aIndex)
|
||||||
{
|
{
|
||||||
@ -73,7 +73,7 @@ double Column::getDouble() const noexcept
|
|||||||
// 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
|
const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcept
|
||||||
{
|
{
|
||||||
const char* pText = reinterpret_cast<const char*>(sqlite3_column_text(mStmtPtr.get(), mIndex));
|
auto pText = reinterpret_cast<const char*>(sqlite3_column_text(mStmtPtr.get(), mIndex));
|
||||||
return (pText?pText:apDefaultValue);
|
return (pText?pText:apDefaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ std::string Column::getString() const
|
|||||||
{
|
{
|
||||||
// Note: using sqlite3_column_blob and not sqlite3_column_text
|
// Note: using sqlite3_column_blob and not sqlite3_column_text
|
||||||
// - no need for sqlite3_column_text to add a \0 on the end, as we're getting the bytes length directly
|
// - no need for sqlite3_column_text to add a \0 on the end, as we're getting the bytes length directly
|
||||||
const char *data = static_cast<const char *>(sqlite3_column_blob(mStmtPtr.get(), mIndex));
|
auto data = static_cast<const char *>(sqlite3_column_blob(mStmtPtr.get(), mIndex));
|
||||||
|
|
||||||
// SQLite docs: "The safest policy is to invoke… sqlite3_column_blob() followed by sqlite3_column_bytes()"
|
// SQLite docs: "The safest policy is to invoke… sqlite3_column_blob() followed by sqlite3_column_bytes()"
|
||||||
// Note: std::string is ok to pass nullptr as first arg, if length is 0
|
// Note: std::string is ok to pass nullptr as first arg, if length is 0
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
namespace SQLite
|
namespace SQLite
|
||||||
{
|
{
|
||||||
|
|
||||||
Statement::Statement(Database &aDatabase, const char* apQuery) :
|
Statement::Statement(const Database& aDatabase, const char* apQuery) :
|
||||||
mQuery(apQuery),
|
mQuery(apQuery),
|
||||||
mpSQLite(aDatabase.getHandle()),
|
mpSQLite(aDatabase.getHandle()),
|
||||||
mpPreparedStatement(prepareStatement()) // prepare the SQL query (needs Database friendship)
|
mpPreparedStatement(prepareStatement()) // prepare the SQL query (needs Database friendship)
|
||||||
@ -49,7 +49,7 @@ void Statement::clearBindings()
|
|||||||
check(ret);
|
check(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Statement::getIndex(const char * const apName)
|
int Statement::getIndex(const char * const apName) const
|
||||||
{
|
{
|
||||||
return sqlite3_bind_parameter_index(getPreparedStatement(), apName);
|
return sqlite3_bind_parameter_index(getPreparedStatement(), apName);
|
||||||
}
|
}
|
||||||
@ -200,7 +200,7 @@ int Statement::tryExecuteStep() noexcept
|
|||||||
|
|
||||||
// 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)
|
Column Statement::getColumn(const int aIndex) const
|
||||||
{
|
{
|
||||||
checkRow();
|
checkRow();
|
||||||
checkIndex(aIndex);
|
checkIndex(aIndex);
|
||||||
@ -211,7 +211,7 @@ Column Statement::getColumn(const int aIndex)
|
|||||||
|
|
||||||
// Return a copy of the column data specified by its column name starting at 0
|
// Return a copy of the column data specified by its column name starting at 0
|
||||||
// (use the Column copy-constructor)
|
// (use the Column copy-constructor)
|
||||||
Column Statement::getColumn(const char* apName)
|
Column Statement::getColumn(const char* apName) const
|
||||||
{
|
{
|
||||||
checkRow();
|
checkRow();
|
||||||
const int index = getColumnIndex(apName);
|
const int index = getColumnIndex(apName);
|
||||||
@ -317,7 +317,7 @@ const char* Statement::getErrorMsg() const noexcept
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return a UTF-8 string containing the SQL text of prepared statement with bound parameters expanded.
|
// Return a UTF-8 string containing the SQL text of prepared statement with bound parameters expanded.
|
||||||
std::string Statement::getExpandedSQL() {
|
std::string Statement::getExpandedSQL() const {
|
||||||
char* expanded = sqlite3_expanded_sql(getPreparedStatement());
|
char* expanded = sqlite3_expanded_sql(getPreparedStatement());
|
||||||
std::string expandedString(expanded);
|
std::string expandedString(expanded);
|
||||||
sqlite3_free(expanded);
|
sqlite3_free(expanded);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user