From 4288cb511e5a7a86cb256653bd62eba6c211aa40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Mon, 2 Apr 2012 06:57:49 +0200 Subject: [PATCH] Moving all methods getColumnXxx() to an intermediate Column object --- src/SQLiteC++/Column.cpp | 55 ++++++++++++++++++++++++++++++ src/SQLiteC++/Column.h | 67 +++++++++++++++++++++++++++++++++++++ src/SQLiteC++/Database.h | 2 +- src/SQLiteC++/Statement.cpp | 52 ++-------------------------- src/SQLiteC++/Statement.h | 25 +++++--------- src/example1/main.cpp | 4 +-- 6 files changed, 137 insertions(+), 68 deletions(-) create mode 100644 src/SQLiteC++/Column.cpp create mode 100644 src/SQLiteC++/Column.h diff --git a/src/SQLiteC++/Column.cpp b/src/SQLiteC++/Column.cpp new file mode 100644 index 0000000..005fe95 --- /dev/null +++ b/src/SQLiteC++/Column.cpp @@ -0,0 +1,55 @@ +/** + * @file Column.cpp + * @brief A SQLite Column is a result field in a certain row. + * + * Copyright (c) 2012 Sebastien Rombauts (sebastien dot rombauts at gmail dot com) + * + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt + * or copy at http://opensource.org/licenses/MIT) + */ +#include "Column.h" + +#include "Database.h" +#include + +namespace SQLite +{ + +// Encapsulation of a Column in a Row of the result. +Column::Column(sqlite3* apSQLite, sqlite3_stmt* apStmt, int aIndex) throw() : // nothrow + mpSQLite(apSQLite), + mpStmt(apStmt), + mIndex(aIndex) +{ +} + +Column::~Column(void) throw() // nothrow +{ +} + +// Return the integer value of the column specified by its index starting at 0 +int Column::getInt(void) const // throw(SQLite::Exception) +{ + return sqlite3_column_int(mpStmt, mIndex); +} + +// Return the 64bits integer value of the column specified by its index starting at 0 +sqlite3_int64 Column::getInt64(void) const // throw(SQLite::Exception) +{ + return sqlite3_column_int64(mpStmt, mIndex); +} + +// Return the double value of the column specified by its index starting at 0 +double Column::getDouble(void) const // throw(SQLite::Exception) +{ + return sqlite3_column_double(mpStmt, mIndex); +} + +// Return the text value (NULL terminated string) of the column specified by its index starting at 0 +const char * Column::getText(void) const // throw(SQLite::Exception) +{ + return (const char*)sqlite3_column_text(mpStmt, mIndex); +} + + +}; // namespace SQLite diff --git a/src/SQLiteC++/Column.h b/src/SQLiteC++/Column.h new file mode 100644 index 0000000..89dfc05 --- /dev/null +++ b/src/SQLiteC++/Column.h @@ -0,0 +1,67 @@ +/** + * @file Column.h + * @brief A SQLite Column is a result field in a certain row. + * + * Copyright (c) 2012 Sebastien Rombauts (sebastien dot rombauts at gmail dot com) + * + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt + * or copy at http://opensource.org/licenses/MIT) + */ +#pragma once + +#include +#include "Exception.h" + +namespace SQLite +{ + +// Forward declaration +class Database; + +/** + * @brief Encapsulation of a Column in a Row of the result. + * + * A Column is a particular field of SQLite data in the current row of result of the Statement. + * + * @warning A Column object must not be copied or memorized as it is only valid for a short time, + * only while the row from the Statement remains valid, that is only until next executeStep + */ +class Column +{ +public: + /** + * @brief Compile and register the SQL query for the provided SQLite Database Connection + */ + explicit Column(sqlite3* apSQLite, sqlite3_stmt* apStmt, int aIndex) throw(); // nothrow + virtual ~Column(void) throw(); // nothrow + + /** + * @brief Return the integer value of the column + */ + int getInt (void) const; // throw(SQLite::Exception); + /** + * @brief Return the 64bits integer value of the column + */ + sqlite3_int64 getInt64 (void) const; // throw(SQLite::Exception); + /** + * @brief Return the double (64bits float) value of the column + */ + double getDouble(void) const; // throw(SQLite::Exception); + /** + * @brief Return the text value (NULL terminated string) of the column + */ + const char* getText (void) const; // throw(SQLite::Exception); + +private: + // Column is copyable, but copy should not be used elsewhere than in return form getColumn + Column(void); + Column& operator=(const Column&); + +private: + sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle + sqlite3_stmt* mpStmt; //!< Pointeur to SQLite Statement Object + int mIndex; //!< Index of the column in the row of result +}; + + +}; // namespace SQLite diff --git a/src/SQLiteC++/Database.h b/src/SQLiteC++/Database.h index a1a106f..5e48c9e 100644 --- a/src/SQLiteC++/Database.h +++ b/src/SQLiteC++/Database.h @@ -74,7 +74,7 @@ public: } private: - // Database must be non copyable + // Database must not be copyable Database(void); Database(const Database&); Database& operator=(const Database&); diff --git a/src/SQLiteC++/Statement.cpp b/src/SQLiteC++/Statement.cpp index 4a192c5..c869937 100644 --- a/src/SQLiteC++/Statement.cpp +++ b/src/SQLiteC++/Statement.cpp @@ -117,8 +117,8 @@ bool Statement::executeStep(void) // throw(SQLite::Exception) return mbOk; } -// Return the integer value of the column specified by its index starting at 0 -int Statement::getColumnInt(const int aIndex) const // throw(SQLite::Exception) +// Return a copy of the column data specified by its index starting at 0 +Column Statement::getColumn(const int aIndex) const // throw(SQLite::Exception) { if (false == mbOk) { @@ -129,53 +129,7 @@ int Statement::getColumnInt(const int aIndex) const // throw(SQLite::Exception) throw SQLite::Exception("Column index out of range"); } - return sqlite3_column_int(mpStmt, aIndex); -} - - -// Return the 64bits integer value of the column specified by its index starting at 0 -sqlite3_int64 Statement::getColumnInt64(const int aIndex) const // throw(SQLite::Exception) -{ - if (false == mbOk) - { - throw SQLite::Exception("No row to get a column from"); - } - else if ((aIndex < 0) || (aIndex >= mColumnCount)) - { - throw SQLite::Exception("Column index out of range"); - } - - return sqlite3_column_int64(mpStmt, aIndex); -} - -// Return the double value of the column specified by its index starting at 0 -double Statement::getColumnDouble(const int aIndex) const // throw(SQLite::Exception) -{ - if (false == mbOk) - { - throw SQLite::Exception("No row to get a column from"); - } - else if ((aIndex < 0) || (aIndex >= mColumnCount)) - { - throw SQLite::Exception("Column index out of range"); - } - - return sqlite3_column_double(mpStmt, aIndex); -} - -// Return the text value (NULL terminated string) of the column specified by its index starting at 0 -const char * Statement::getColumnText(const int aIndex) const // throw(SQLite::Exception) -{ - if (false == mbOk) - { - throw SQLite::Exception("No row to get a column from"); - } - else if ((aIndex < 0) || (aIndex >= mColumnCount)) - { - throw SQLite::Exception("Column index out of range"); - } - - return (const char*)sqlite3_column_text(mpStmt, aIndex); + return Column(mDatabase.mpSQLite, mpStmt, aIndex); } // Test if the column is NULL diff --git a/src/SQLiteC++/Statement.h b/src/SQLiteC++/Statement.h index b7bb3e6..dd62e75 100644 --- a/src/SQLiteC++/Statement.h +++ b/src/SQLiteC++/Statement.h @@ -11,6 +11,7 @@ #include #include "Exception.h" +#include "Column.h" namespace SQLite { @@ -81,26 +82,18 @@ public: //////////////////////////////////////////////////////////////////////////// /** - * @brief Return the integer value of the column specified by its index starting at 0 (aIndex >= 0) + * @brief Return a copie of the column data specified by its index starting at 0 (aIndex >= 0) + * + * @warning The resulting Column object must not be copied or memorized as it is only valid for a short time, + * only while the row from the Statement remains valid, that is only until next executeStep */ - int getColumnInt (const int aIndex) const; // throw(SQLite::Exception); - /** - * @brief Return the 64bits integer value of the column specified by its index starting at 0 (aIndex >= 0) - */ - sqlite3_int64 getColumnInt64 (const int aIndex) const; // throw(SQLite::Exception); - /** - * @brief Return the double (64bits float) value of the column specified by its index starting at 0 (aIndex >= 0) - */ - double getColumnDouble(const int aIndex) const; // throw(SQLite::Exception); - /** - * @brief Return the text value (NULL terminated string) of the column specified by its index starting at 0 (aIndex >= 0) - */ - const char* getColumnText (const int aIndex) const; // throw(SQLite::Exception); + Column getColumn (const int aIndex) const; // throw(SQLite::Exception); + /** * @brief Test if the column is NULL */ - bool isColumnNull (const int aIndex) const; // throw(SQLite::Exception); + bool isColumnNull (const int aIndex) const; // throw(SQLite::Exception); //////////////////////////////////////////////////////////////////////////// @@ -137,7 +130,7 @@ public: } private: - // Database must be non copyable + // Statement must not be copyable Statement(void); Statement(const Statement&); Statement& operator=(const Statement&); diff --git a/src/example1/main.cpp b/src/example1/main.cpp index e95387d..c4d9f72 100644 --- a/src/example1/main.cpp +++ b/src/example1/main.cpp @@ -11,13 +11,13 @@ int main (void) // Compile a SQL query, containing one parameter (index 1) SQLite::Statement query(db, "SELECT * FROM test WHERE size>?"); - std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " collumns in the result)\n"; + std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " columns in the result)\n"; // Bind an integer value "6" to the first parameter of the SQL query query.bind(1, 6); while (query.executeStep()) { - std::cout << "row : (" << query.getColumnInt(0) << ", " << query.getColumnText(1) << ", " << query.getColumnInt(2) << ")\n"; + std::cout << "row : (" << query.getColumn(0).getInt() << ", " << query.getColumn(1).getText() << ", " << query.getColumn(2).getInt() << ")\n"; } } catch (std::exception& e)