Moving all methods getColumnXxx() to an intermediate Column object

This commit is contained in:
Sébastien Rombauts 2012-04-02 06:57:49 +02:00
parent 39db797186
commit 4288cb511e
6 changed files with 137 additions and 68 deletions

55
src/SQLiteC++/Column.cpp Normal file
View File

@ -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 <iostream>
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

67
src/SQLiteC++/Column.h Normal file
View File

@ -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 <sqlite3.h>
#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

View File

@ -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&);

View File

@ -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

View File

@ -11,6 +11,7 @@
#include <sqlite3.h>
#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&);

View File

@ -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)