From d516ea72c5aea049087162acfc90d07b78c391e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Mon, 16 Apr 2012 07:11:23 +0200 Subject: [PATCH] Column is again an independant class --- src/SQLiteC++/Column.cpp | 73 +++++++++++++++++++++++++++++++ src/SQLiteC++/Column.h | 86 +++++++++++++++++++++++++++++++++++++ src/SQLiteC++/Database.cpp | 1 + src/SQLiteC++/SQLiteC++.h | 1 + src/SQLiteC++/Statement.cpp | 63 +-------------------------- src/SQLiteC++/Statement.h | 78 +-------------------------------- src/example1/main.cpp | 1 + 7 files changed, 165 insertions(+), 138 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..e672392 --- /dev/null +++ b/src/SQLiteC++/Column.cpp @@ -0,0 +1,73 @@ +/** + * @file Column.cpp + * @brief Encapsulation of a Column in a Row of the result. + * + * Copyright (c) 2012 Sebastien Rombauts (sebastien.rombauts@gmail.com) + * + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt + * or copy at http://opensource.org/licenses/MIT) + */ +#include "Column.h" + +#include + +namespace SQLite +{ + +// Encapsulation of a Column in a Row of the result. +Column::Column(sqlite3* apSQLite, sqlite3_stmt* apStmt, unsigned int* apStmtRefCount, int aIndex) throw() : // nothrow + mpSQLite(apSQLite), + mpStmt(apStmt), + mpStmtRefCount(apStmtRefCount), + mIndex(aIndex) +{ + (*mpStmtRefCount)++; +} + +Column::~Column(void) throw() // nothrow +{ + (*mpStmtRefCount)--; + if (0 == *mpStmtRefCount) + { + int ret = sqlite3_finalize(mpStmt); + if (SQLITE_OK != ret) + { + throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); + } + mpStmt = NULL; + } +} + +// Return the integer value of the column specified by its index starting at 0 +int Column::getInt(void) const throw() // nothrow +{ + 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() // nothrow +{ + 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() // nothrow +{ + return sqlite3_column_double(mpStmt, mIndex); +} + +// Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0 +const char* Column::getText(void) const throw() // nothrow +{ + return (const char*)sqlite3_column_text(mpStmt, mIndex); +} + + +// Standard std::ostream inserter +std::ostream& operator<<(std::ostream &stream, const Column& column) +{ + stream << column.getText(); + return stream; +} + +}; // namespace SQLite diff --git a/src/SQLiteC++/Column.h b/src/SQLiteC++/Column.h new file mode 100644 index 0000000..fa87ab5 --- /dev/null +++ b/src/SQLiteC++/Column.h @@ -0,0 +1,86 @@ +/** + * @file Column.h + * @brief Encapsulation of a Column in a Row of the result. + * + * Copyright (c) 2012 Sebastien Rombauts (sebastien.rombauts@gmail.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 +{ + + +/** + * @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. + */ +class Column +{ +public: + /** + * @brief Compile and register the SQL query for the provided SQLite Database Connection + */ + explicit Column(sqlite3* apSQLite, sqlite3_stmt* apStmt, unsigned int* apStmtRefCount, int aIndex) throw(); // nothrow + /// Basic destructor + virtual ~Column(void) throw(); // nothrow + + /// Return the integer value of the column. + int getInt (void) const throw(); + /// Return the 64bits integer value of the column. + sqlite3_int64 getInt64 (void) const throw(); + /// Return the double (64bits float) value of the column. + double getDouble(void) const throw(); + /// Return a pointer to the text value (NULL terminated string) of the column. + /// 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). + const char* getText (void) const throw(); + + /// Inline cast operator to int + inline operator const int() const + { + return getInt(); + } + /// Inline cast operator to 64bits integer + inline operator const sqlite3_int64() const + { + return getInt64(); + } + /// Inline cast operator to double + inline operator const double() const + { + return getDouble(); + } + /// Inline cast operator to char* + inline operator const char*() const + { + return getText(); + } + /// Inline cast operator to std::string + inline operator const std::string() const + { + return getText(); + } + +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; //!< Pointer to SQLite Statement Object + unsigned int* mpStmtRefCount; //!< Pointer to the reference counter of the (shared) Statement Object + int mIndex; //!< Index of the column in the row of result +}; + +/// Standard std::ostream inserter +std::ostream& operator<<(std::ostream &stream, const Column& column); + +}; // namespace SQLite diff --git a/src/SQLiteC++/Database.cpp b/src/SQLiteC++/Database.cpp index d4ce6b2..29bd72e 100644 --- a/src/SQLiteC++/Database.cpp +++ b/src/SQLiteC++/Database.cpp @@ -10,6 +10,7 @@ #include "Database.h" #include "Statement.h" +#include "Column.h" namespace SQLite { diff --git a/src/SQLiteC++/SQLiteC++.h b/src/SQLiteC++/SQLiteC++.h index c76d45f..3576a40 100644 --- a/src/SQLiteC++/SQLiteC++.h +++ b/src/SQLiteC++/SQLiteC++.h @@ -16,6 +16,7 @@ // Include useful headers of SQLiteC++ #include "Database.h" #include "Statement.h" +#include "Column.h" #include "Transaction.h" diff --git a/src/SQLiteC++/Statement.cpp b/src/SQLiteC++/Statement.cpp index 4f18eec..533b8f4 100644 --- a/src/SQLiteC++/Statement.cpp +++ b/src/SQLiteC++/Statement.cpp @@ -10,6 +10,7 @@ #include "Statement.h" #include "Database.h" +#include "Column.h" #include namespace SQLite @@ -177,7 +178,7 @@ bool Statement::executeStep(void) // throw(SQLite::Exception) } // Return a copy of the column data specified by its index starting at 0 -Statement::Column Statement::getColumn(const int aIndex) const // throw(SQLite::Exception) +Column Statement::getColumn(const int aIndex) const // throw(SQLite::Exception) { if (false == mbOk) { @@ -216,64 +217,4 @@ void Statement::check(const int aRet) const // throw(SQLite::Exception) } } - -//////////////////////////////////////////////////////////////////////////////// -// Implementation of the inner class Statement::Column -// - -// Encapsulation of a Column in a Row of the result. -Statement::Column::Column(sqlite3* apSQLite, sqlite3_stmt* apStmt, unsigned int* apStmtRefCount, int aIndex) throw() : // nothrow - mpSQLite(apSQLite), - mpStmt(apStmt), - mpStmtRefCount(apStmtRefCount), - mIndex(aIndex) -{ - (*mpStmtRefCount)++; -} - -Statement::Column::~Column(void) throw() // nothrow -{ - (*mpStmtRefCount)--; - if (0 == *mpStmtRefCount) - { - int ret = sqlite3_finalize(mpStmt); - if (SQLITE_OK != ret) - { - throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); - } - mpStmt = NULL; - } -} - -// Return the integer value of the column specified by its index starting at 0 -int Statement::Column::getInt(void) const throw() // nothrow -{ - return sqlite3_column_int(mpStmt, mIndex); -} - -// Return the 64bits integer value of the column specified by its index starting at 0 -sqlite3_int64 Statement::Column::getInt64(void) const throw() // nothrow -{ - return sqlite3_column_int64(mpStmt, mIndex); -} - -// Return the double value of the column specified by its index starting at 0 -double Statement::Column::getDouble(void) const throw() // nothrow -{ - return sqlite3_column_double(mpStmt, mIndex); -} - -// Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0 -const char* Statement::Column::getText(void) const throw() // nothrow -{ - return (const char*)sqlite3_column_text(mpStmt, mIndex); -} - -// Standard std::ostream inserter -std::ostream& operator<<(std::ostream &stream, const Statement::Column& column) -{ - stream << column.getText(); - return stream; -} - }; // namespace SQLite diff --git a/src/SQLiteC++/Statement.h b/src/SQLiteC++/Statement.h index f5db1a2..757c2f4 100644 --- a/src/SQLiteC++/Statement.h +++ b/src/SQLiteC++/Statement.h @@ -17,6 +17,7 @@ namespace SQLite // Forward declaration class Database; +class Column; /** * @brief RAII encapsulation of a prepared SQLite Statement. @@ -30,10 +31,6 @@ class Database; */ class Statement { -public: - // Forward declaration of the inner class "Column" - class Column; - public: /** * @brief Compile and register the SQL query for the provided SQLite Database Connection @@ -166,74 +163,6 @@ public: return mbDone; } -public: - /** - * @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, unsigned int* apStmtRefCount, int aIndex) throw(); // nothrow - /// Basic destructor - virtual ~Column(void) throw(); // nothrow - - /// Return the integer value of the column. - int getInt (void) const throw(); - /// Return the 64bits integer value of the column. - sqlite3_int64 getInt64 (void) const throw(); - /// Return the double (64bits float) value of the column. - double getDouble(void) const throw(); - /// Return a pointer to the text value (NULL terminated string) of the column. - /// 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). - const char* getText (void) const throw(); - - /// Inline cast operator to int - inline operator const int() const - { - return getInt(); - } - /// Inline cast operator to 64bits integer - inline operator const sqlite3_int64() const - { - return getInt64(); - } - /// Inline cast operator to double - inline operator const double() const - { - return getDouble(); - } - /// Inline cast operator to char* - inline operator const char*() const - { - return getText(); - } - /// Inline cast operator to std::string - inline operator const std::string() const - { - return getText(); - } - - 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; //!< Pointer to SQLite Statement Object - unsigned int* mpStmtRefCount; //!< Pointer to the reference counter of the (shared) Statement Object - int mIndex; //!< Index of the column in the row of result - }; - private: // Statement must not be copyable Statement(void); @@ -255,9 +184,4 @@ private: bool mbDone; //!< True when the last executeStep() had no more row to fetch }; - -/// Standard std::ostream inserter -std::ostream& operator<<(std::ostream &stream, const Statement::Column& column); - - }; // namespace SQLite diff --git a/src/example1/main.cpp b/src/example1/main.cpp index c771b4f..133ec23 100644 --- a/src/example1/main.cpp +++ b/src/example1/main.cpp @@ -15,6 +15,7 @@ #include "../SQLiteC++/Database.h" #include "../SQLiteC++/Statement.h" +#include "../SQLiteC++/Column.h" #include "../SQLiteC++/Transaction.h"