mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Column is again an independant class
This commit is contained in:
parent
9c2544ef16
commit
d516ea72c5
73
src/SQLiteC++/Column.cpp
Normal file
73
src/SQLiteC++/Column.cpp
Normal file
@ -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 <iostream>
|
||||||
|
|
||||||
|
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
|
86
src/SQLiteC++/Column.h
Normal file
86
src/SQLiteC++/Column.h
Normal file
@ -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 <sqlite3.h>
|
||||||
|
#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
|
@ -10,6 +10,7 @@
|
|||||||
#include "Database.h"
|
#include "Database.h"
|
||||||
|
|
||||||
#include "Statement.h"
|
#include "Statement.h"
|
||||||
|
#include "Column.h"
|
||||||
|
|
||||||
namespace SQLite
|
namespace SQLite
|
||||||
{
|
{
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
// Include useful headers of SQLiteC++
|
// Include useful headers of SQLiteC++
|
||||||
#include "Database.h"
|
#include "Database.h"
|
||||||
#include "Statement.h"
|
#include "Statement.h"
|
||||||
|
#include "Column.h"
|
||||||
#include "Transaction.h"
|
#include "Transaction.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "Statement.h"
|
#include "Statement.h"
|
||||||
|
|
||||||
#include "Database.h"
|
#include "Database.h"
|
||||||
|
#include "Column.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace SQLite
|
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
|
// 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)
|
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
|
}; // namespace SQLite
|
||||||
|
@ -17,6 +17,7 @@ namespace SQLite
|
|||||||
|
|
||||||
// Forward declaration
|
// Forward declaration
|
||||||
class Database;
|
class Database;
|
||||||
|
class Column;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief RAII encapsulation of a prepared SQLite Statement.
|
* @brief RAII encapsulation of a prepared SQLite Statement.
|
||||||
@ -30,10 +31,6 @@ class Database;
|
|||||||
*/
|
*/
|
||||||
class Statement
|
class Statement
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
// Forward declaration of the inner class "Column"
|
|
||||||
class Column;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @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
|
||||||
@ -166,74 +163,6 @@ public:
|
|||||||
return mbDone;
|
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:
|
private:
|
||||||
// Statement must not be copyable
|
// Statement must not be copyable
|
||||||
Statement(void);
|
Statement(void);
|
||||||
@ -255,9 +184,4 @@ private:
|
|||||||
bool mbDone; //!< True when the last executeStep() had no more row to fetch
|
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
|
}; // namespace SQLite
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include "../SQLiteC++/Database.h"
|
#include "../SQLiteC++/Database.h"
|
||||||
#include "../SQLiteC++/Statement.h"
|
#include "../SQLiteC++/Statement.h"
|
||||||
|
#include "../SQLiteC++/Column.h"
|
||||||
#include "../SQLiteC++/Transaction.h"
|
#include "../SQLiteC++/Transaction.h"
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user