From 354323a8752214a861b1488f5c37c532525ef3ba Mon Sep 17 00:00:00 2001 From: Kacperos155 <56676161+Kacperos155@users.noreply.github.com> Date: Tue, 25 Jan 2022 20:56:48 +0100 Subject: [PATCH] Minor refactoring to Statement and Column --- include/SQLiteCpp/Column.h | 2 +- include/SQLiteCpp/Statement.h | 21 ++++++++++----------- src/Column.cpp | 6 +++--- src/Statement.cpp | 10 +++++----- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/include/SQLiteCpp/Column.h b/include/SQLiteCpp/Column.h index 069c6f5..5e341b3 100644 --- a/include/SQLiteCpp/Column.h +++ b/include/SQLiteCpp/Column.h @@ -54,7 +54,7 @@ public: * @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 */ - 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 copy constructor and assignment operator are perfectly suited : diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index 8610fdd..6c68fb4 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -60,7 +60,7 @@ public: * * 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 @@ -70,7 +70,7 @@ public: * * 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()) {} @@ -122,7 +122,7 @@ public: // => if you know what you are doing, use bindNoCopy() instead of bind() 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) @@ -463,7 +463,7 @@ public: * Thus, you should instead extract immediately its data (getInt(), getText()...) * 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) @@ -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. */ - 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 /** @@ -617,7 +617,7 @@ public: } // 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 int getColumnCount() const @@ -646,7 +646,7 @@ public: const char* getErrorMsg() const noexcept; /// Shared pointer to SQLite Prepared Statement Object - typedef std::shared_ptr TStatementPtr; + using TStatementPtr = std::shared_ptr; private: /** @@ -699,11 +699,10 @@ private: */ sqlite3_stmt* getPreparedStatement() const; -private: - /// Map of columns index by name (mutable so getColumnIndex can be const) - typedef std::map TColumnNames; -private: + /// Map of columns index by name (mutable so getColumnIndex can be const) + using TColumnNames = std::map; + std::string mQuery; //!< UTF-8 SQL Query sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object diff --git a/src/Column.cpp b/src/Column.cpp index 4a8cb52..cf25608 100644 --- a/src/Column.cpp +++ b/src/Column.cpp @@ -26,7 +26,7 @@ const int Null = SQLITE_NULL; // 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), 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 const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcept { - const char* pText = reinterpret_cast(sqlite3_column_text(mStmtPtr.get(), mIndex)); + auto pText = reinterpret_cast(sqlite3_column_text(mStmtPtr.get(), mIndex)); return (pText?pText:apDefaultValue); } @@ -88,7 +88,7 @@ std::string Column::getString() const { // 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 - const char *data = static_cast(sqlite3_column_blob(mStmtPtr.get(), mIndex)); + auto data = static_cast(sqlite3_column_blob(mStmtPtr.get(), mIndex)); // 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 diff --git a/src/Statement.cpp b/src/Statement.cpp index 29ea133..135534d 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -20,7 +20,7 @@ namespace SQLite { -Statement::Statement(Database &aDatabase, const char* apQuery) : +Statement::Statement(const Database& aDatabase, const char* apQuery) : mQuery(apQuery), mpSQLite(aDatabase.getHandle()), mpPreparedStatement(prepareStatement()) // prepare the SQL query (needs Database friendship) @@ -49,7 +49,7 @@ void Statement::clearBindings() check(ret); } -int Statement::getIndex(const char * const apName) +int Statement::getIndex(const char * const apName) const { 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 // (use the Column copy-constructor) -Column Statement::getColumn(const int aIndex) +Column Statement::getColumn(const int aIndex) const { checkRow(); 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 // (use the Column copy-constructor) -Column Statement::getColumn(const char* apName) +Column Statement::getColumn(const char* apName) const { checkRow(); 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. -std::string Statement::getExpandedSQL() { +std::string Statement::getExpandedSQL() const { char* expanded = sqlite3_expanded_sql(getPreparedStatement()); std::string expandedString(expanded); sqlite3_free(expanded);