mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-08-05 15:41:47 +02:00
Update the SQLite module to work with the modified API.
Separate the SQLite handles into their own source files.
This commit is contained in:
170
modules/sqlite/Handle/Connection.cpp
Normal file
170
modules/sqlite/Handle/Connection.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Handle/Connection.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void ConnHnd::Validate() const
|
||||
{
|
||||
// Is the handle valid?
|
||||
if ((m_Hnd == nullptr) || (m_Hnd->mPtr == nullptr))
|
||||
{
|
||||
STHROWF("Invalid SQLite connection reference");
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
ConnHnd::Handle::~Handle()
|
||||
{
|
||||
// Is there anything to close?
|
||||
if (!mPtr)
|
||||
{
|
||||
return; // Nothing to close
|
||||
}
|
||||
// Are we dealing with a memory leak? Technically shouldn't reach this situation!
|
||||
else if (mRef != 0)
|
||||
{
|
||||
// Should we deal with undefined behavior instead? How bad is one connection left open?
|
||||
_SqMod->LogErr("SQLite connection is still referenced (%s)", mName.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
// NOTE: Should we call sqlite3_interrupt(...) before closing?
|
||||
Object env;
|
||||
Function func;
|
||||
// Flush remaining queries in the queue and ignore the result
|
||||
Flush(mQueue.size(), env, func);
|
||||
// Attempt to close the database
|
||||
if ((sqlite3_close(mPtr)) != SQLITE_OK)
|
||||
{
|
||||
_SqMod->LogErr("Unable to close SQLite connection [%s]", sqlite3_errmsg(mPtr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void ConnHnd::Handle::Create(CSStr name, Int32 flags, CSStr vfs)
|
||||
{
|
||||
// Make sure a previous connection doesn't exist
|
||||
if (mPtr)
|
||||
{
|
||||
STHROWF("Unable to connect to database. Database already connected");
|
||||
}
|
||||
// Make sure the name is valid
|
||||
else if (!name || *name == '\0')
|
||||
{
|
||||
STHROWF("Unable to connect to database. The name is invalid");
|
||||
}
|
||||
// Attempt to create the database connection
|
||||
else if ((mStatus = sqlite3_open_v2(name, &mPtr, flags, vfs)) != SQLITE_OK)
|
||||
{
|
||||
// Grab the error message before destroying the handle
|
||||
String msg(sqlite3_errmsg(mPtr) ? sqlite3_errmsg(mPtr) : _SC("Unknown reason"));
|
||||
// Must be destroyed regardless of result
|
||||
sqlite3_close(mPtr);
|
||||
// Explicitly make sure it's null
|
||||
mPtr = nullptr;
|
||||
// Now its safe to throw the error
|
||||
STHROWF("Unable to connect to database [%s]", msg.c_str());
|
||||
}
|
||||
// Let's save the specified information
|
||||
mName.assign(name);
|
||||
mFlags = flags;
|
||||
mVFS.assign(vfs ? vfs : _SC(""));
|
||||
// Optional check if database is initially stored in memory
|
||||
mMemory = (mName.compare(_SC(":memory:")) == 0);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 ConnHnd::Handle::Flush(Uint32 num, Object & env, Function & func)
|
||||
{
|
||||
// Do we even have a valid connection?
|
||||
if (!mPtr)
|
||||
{
|
||||
return -1; // No connection!
|
||||
}
|
||||
// Is there anything to flush?
|
||||
else if (!num || mQueue.empty())
|
||||
{
|
||||
return 0; // Nothing to process!
|
||||
}
|
||||
// Can we even flush that many?
|
||||
else if (num > mQueue.size())
|
||||
{
|
||||
num = mQueue.size();
|
||||
}
|
||||
// Generate the function that should be called upon error
|
||||
Function callback = Function(env.GetVM(), env.GetObject(), func.GetFunc());
|
||||
// Obtain iterators to the range of queries that should be flushed
|
||||
QueryList::iterator itr = mQueue.begin();
|
||||
QueryList::iterator end = mQueue.begin() + num;
|
||||
// Attempt to begin the flush transaction
|
||||
if ((mStatus = sqlite3_exec(mPtr, "BEGIN", nullptr, nullptr, nullptr)) != SQLITE_OK)
|
||||
{
|
||||
STHROWF("Unable to begin flush transaction [%s]", sqlite3_errmsg(mPtr));
|
||||
}
|
||||
// Process all queries within range of selection
|
||||
for (; itr != end; ++itr)
|
||||
{
|
||||
// Should we manually terminate this query?
|
||||
/*
|
||||
if (*(*itr).rbegin() != ';')
|
||||
{
|
||||
itr->push_back(';');
|
||||
}
|
||||
*/
|
||||
// Attempt to execute the currently processed query string
|
||||
if ((mStatus = sqlite3_exec(mPtr, itr->c_str(), nullptr, nullptr, nullptr)) == SQLITE_OK)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// Do we have to execute any callback to resolve our issue?
|
||||
else if (!callback.IsNull())
|
||||
{
|
||||
try
|
||||
{
|
||||
// Ask the callback whether the query processing should end here
|
||||
SharedPtr< bool > ret = callback.Evaluate< bool, Int32, const String & >(mStatus, *itr);
|
||||
// Should we break here?
|
||||
if (!!ret && (*ret == false))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (const Sqrat::Exception & e)
|
||||
{
|
||||
_SqMod->LogErr("Squirrel error caught in flush handler [%s]", e.Message().c_str());
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
_SqMod->LogErr("Program error caught in flush handler [%s]", e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_SqMod->LogErr("Unknown error caught in flush handler");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Erase all queries till end or till the point of failure (if any occurred)
|
||||
mQueue.erase(mQueue.begin(), itr);
|
||||
// Attempt to commit changes requested during transaction
|
||||
if ((mStatus = sqlite3_exec(mPtr, "COMMIT", nullptr, nullptr, nullptr)) == SQLITE_OK)
|
||||
{
|
||||
return sqlite3_changes(mPtr);
|
||||
}
|
||||
// Attempt to roll back erroneous changes
|
||||
else if ((mStatus = sqlite3_exec(mPtr, "ROLLBACK", nullptr, nullptr, nullptr)) != SQLITE_OK)
|
||||
{
|
||||
STHROWF("Unable to rollback flush transaction [%s]", sqlite3_errmsg(mPtr));
|
||||
}
|
||||
// The transaction failed somehow but we managed to rollback
|
||||
else
|
||||
{
|
||||
STHROWF("Unable to commit flush transaction [%s]", sqlite3_errmsg(mPtr));
|
||||
}
|
||||
// Operation failed
|
||||
return -1;
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
407
modules/sqlite/Handle/Connection.hpp
Normal file
407
modules/sqlite/Handle/Connection.hpp
Normal file
@@ -0,0 +1,407 @@
|
||||
#ifndef _SQSQLITE_HANDLE_CONNECTION_HPP_
|
||||
#define _SQSQLITE_HANDLE_CONNECTION_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Common.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <vector>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Manages a reference counted database connection handle.
|
||||
*/
|
||||
class ConnHnd
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class Connection;
|
||||
friend class Statement;
|
||||
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef sqlite3 Type; // The managed type.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef Type* Pointer; // Pointer to the managed type.
|
||||
typedef const Type* ConstPtr; // Constant pointer to the managed type.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef Type& Reference; // Reference to the managed type.
|
||||
typedef const Type& ConstRef; // Constant reference to the managed type.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef unsigned int Counter; // Reference counter type.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the connection handle and throw an error if invalid.
|
||||
*/
|
||||
void Validate() const;
|
||||
|
||||
protected:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef std::vector< String > QueryList; // Container used to queue queries.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The structure that holds the data associated with a certain connection.
|
||||
*/
|
||||
struct Handle
|
||||
{
|
||||
// ----------------------------------------------------------------------------------------
|
||||
Pointer mPtr; // The connection handle resource.
|
||||
Counter mRef; // Reference count to the managed handle.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
Int32 mStatus; // The last status code of this connection handle.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
QueryList mQueue; // A queue of queries to be executed in groups.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
Int32 mFlags; // The flags used to create the database connection handle.
|
||||
String mName; // The specified name to be used as the database file.
|
||||
String mVFS; // The specified virtual file system.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
bool mMemory; // Whether the database exists in memory and not disk.
|
||||
bool mTrace; // Whether tracing was activated on the database.
|
||||
bool mProfile; // Whether profiling was activated on the database.
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
Handle(Counter counter)
|
||||
: mPtr(nullptr)
|
||||
, mRef(counter)
|
||||
, mStatus(SQLITE_OK)
|
||||
, mQueue()
|
||||
, mFlags(0)
|
||||
, mName()
|
||||
, mVFS()
|
||||
, mMemory(false)
|
||||
, mTrace(false)
|
||||
, mProfile(false)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~Handle();
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Create the database connection resource.
|
||||
*/
|
||||
void Create(CSStr name, Int32 flags, CSStr vfs);
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Execute a specific amount of queries from the queue.
|
||||
*/
|
||||
Int32 Flush(Uint32 num, Object & env, Function & func);
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Handle * m_Hnd; // The managed handle instance.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Grab a strong reference to a connection handle.
|
||||
*/
|
||||
void Grab()
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
++(m_Hnd->mRef);
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Drop a strong reference to a connection handle.
|
||||
*/
|
||||
void Drop()
|
||||
{
|
||||
if (m_Hnd && --(m_Hnd->mRef) == 0)
|
||||
{
|
||||
delete m_Hnd; // Let the destructor take care of cleaning up (if necessary)
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
ConnHnd(CSStr name)
|
||||
: m_Hnd(name ? new Handle(1) : nullptr)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor (null).
|
||||
*/
|
||||
ConnHnd()
|
||||
: m_Hnd(nullptr)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
ConnHnd(const ConnHnd & o)
|
||||
: m_Hnd(o.m_Hnd)
|
||||
{
|
||||
Grab();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
ConnHnd(ConnHnd && o)
|
||||
: m_Hnd(o.m_Hnd)
|
||||
{
|
||||
o.m_Hnd = nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~ConnHnd()
|
||||
{
|
||||
Drop();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
ConnHnd & operator = (const ConnHnd & o)
|
||||
{
|
||||
if (m_Hnd != o.m_Hnd)
|
||||
{
|
||||
Drop();
|
||||
m_Hnd = o.m_Hnd;
|
||||
Grab();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
ConnHnd & operator = (ConnHnd && o)
|
||||
{
|
||||
if (m_Hnd != o.m_Hnd)
|
||||
{
|
||||
m_Hnd = o.m_Hnd;
|
||||
o.m_Hnd = nullptr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Status assignment operator.
|
||||
*/
|
||||
ConnHnd & operator = (Int32 status)
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
m_Hnd->mStatus = status;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an equality comparison between two connection handles.
|
||||
*/
|
||||
bool operator == (const ConnHnd & o) const
|
||||
{
|
||||
return (m_Hnd == o.m_Hnd);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an inequality comparison between two connection handles.
|
||||
*/
|
||||
bool operator != (const ConnHnd & o) const
|
||||
{
|
||||
return (m_Hnd != o.m_Hnd);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an equality comparison with an integer value status.
|
||||
*/
|
||||
bool operator == (Int32 status) const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return (m_Hnd->mStatus == status);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an inequality comparison with an integer value status.
|
||||
*/
|
||||
bool operator != (Int32 status) const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return (m_Hnd->mStatus != status);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to boolean for use in boolean operations.
|
||||
*/
|
||||
operator bool () const
|
||||
{
|
||||
return m_Hnd && m_Hnd->mPtr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator Pointer ()
|
||||
{
|
||||
return m_Hnd ? m_Hnd->mPtr : nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator Pointer () const
|
||||
{
|
||||
return m_Hnd ? m_Hnd->mPtr : nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator Reference ()
|
||||
{
|
||||
assert(m_Hnd && m_Hnd->mPtr);
|
||||
return *(m_Hnd->mPtr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator ConstRef () const
|
||||
{
|
||||
assert(m_Hnd && m_Hnd->mPtr);
|
||||
return *(m_Hnd->mPtr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Member operator for dereferencing the managed pointer.
|
||||
*/
|
||||
Handle * operator -> () const
|
||||
{
|
||||
assert(m_Hnd);
|
||||
return m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Indirection operator for obtaining a reference of the managed pointer.
|
||||
*/
|
||||
Handle & operator * () const
|
||||
{
|
||||
assert(m_Hnd);
|
||||
return *m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the raw handle structure pointer.
|
||||
*/
|
||||
Handle * HndPtr()
|
||||
{
|
||||
return m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the raw handle structure pointer.
|
||||
*/
|
||||
Handle * HndPtr() const
|
||||
{
|
||||
return m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the number of active references to the managed instance.
|
||||
*/
|
||||
Counter Count() const
|
||||
{
|
||||
return m_Hnd ? m_Hnd->mRef : 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the last known status code.
|
||||
*/
|
||||
Int32 Status() const
|
||||
{
|
||||
return m_Hnd ? m_Hnd->mStatus : SQLITE_NOMEM;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the message of the last received error code.
|
||||
*/
|
||||
CCStr ErrStr() const
|
||||
{
|
||||
// SQLite does it's null pointer validations internally
|
||||
if (m_Hnd)
|
||||
{
|
||||
return sqlite3_errstr(sqlite3_errcode(m_Hnd->mPtr));
|
||||
}
|
||||
return _SC("");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Return the last error message associated with this database connection.
|
||||
*/
|
||||
CCStr ErrMsg() const
|
||||
{
|
||||
// SQLite does it's null pointer validations internally
|
||||
if (m_Hnd)
|
||||
{
|
||||
return sqlite3_errmsg(m_Hnd->mPtr);
|
||||
}
|
||||
return _SC("");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Return the numeric result code for the most recent failed API call (if any).
|
||||
*/
|
||||
Int32 ErrNo() const
|
||||
{
|
||||
// SQLite does it's null pointer validations internally
|
||||
if (m_Hnd)
|
||||
{
|
||||
return sqlite3_errcode(m_Hnd->mPtr);
|
||||
}
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Return the extended numeric result code for the most recent failed API call (if any).
|
||||
*/
|
||||
Int32 ExErrNo() const
|
||||
{
|
||||
// SQLite does it's null pointer validations internally
|
||||
if (m_Hnd)
|
||||
{
|
||||
return sqlite3_extended_errcode(m_Hnd->mPtr);
|
||||
}
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _SQSQLITE_HANDLE_CONNECTION_HPP_
|
117
modules/sqlite/Handle/Statement.cpp
Normal file
117
modules/sqlite/Handle/Statement.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Handle/Statement.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void StmtHnd::Validate() const
|
||||
{
|
||||
// Is the handle valid?
|
||||
if ((m_Hnd == nullptr) || (m_Hnd->mPtr == nullptr))
|
||||
{
|
||||
STHROWF("Invalid SQLite statement reference");
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
StmtHnd::Handle::~Handle()
|
||||
{
|
||||
// Is there anything to finalize?
|
||||
if (!mPtr)
|
||||
{
|
||||
return; // Nothing to finalize
|
||||
}
|
||||
// Are we dealing with a memory leak? Technically shouldn't reach this situation!
|
||||
else if (mRef != 0)
|
||||
{
|
||||
// Should we deal with undefined behavior instead? How bad is one statement left alive?
|
||||
_SqMod->LogErr("SQLite statement is still referenced (%s)", mQuery.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Attempt to finalize the statement
|
||||
if ((sqlite3_finalize(mPtr)) != SQLITE_OK)
|
||||
{
|
||||
_SqMod->LogErr("Unable to finalize SQLite statement [%s]", mConn.ErrMsg());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void StmtHnd::Handle::Create(CSStr query)
|
||||
{
|
||||
// Make sure a previous statement doesn't exist
|
||||
if (mPtr)
|
||||
{
|
||||
STHROWF("Unable to prepare statement. Statement already prepared");
|
||||
}
|
||||
// Is the specified database connection is valid?
|
||||
else if (!mConn)
|
||||
{
|
||||
STHROWF("Unable to prepare statement. Invalid connection handle");
|
||||
}
|
||||
// Save the query string and therefore multiple strlen(...) calls
|
||||
mQuery.assign(query ? query : _SC(""));
|
||||
// Is the specified query string we just saved, valid?
|
||||
if (mQuery.empty())
|
||||
{
|
||||
STHROWF("Unable to prepare statement. Invalid query string");
|
||||
}
|
||||
// Attempt to prepare a statement with the specified query string
|
||||
else if ((mStatus = sqlite3_prepare_v2(mConn, mQuery.c_str(), (Int32)mQuery.size(),
|
||||
&mPtr, nullptr)) != SQLITE_OK)
|
||||
{
|
||||
// Clear the query string since it failed
|
||||
mQuery.clear();
|
||||
// Explicitly make sure the handle is null
|
||||
mPtr = nullptr;
|
||||
// Now it's safe to throw the error
|
||||
STHROWF("Unable to prepare statement [%s]", mConn.ErrMsg());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Obtain the number of available columns
|
||||
mColumns = sqlite3_column_count(mPtr);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 StmtHnd::Handle::GetColumnIndex(CSStr name)
|
||||
{
|
||||
// Validate the handle
|
||||
if (!mPtr)
|
||||
{
|
||||
STHROWF("Invalid SQLite statement");
|
||||
}
|
||||
// Are the names cached?
|
||||
else if (mIndexes.empty())
|
||||
{
|
||||
for (Int32 i = 0; i < mColumns; ++i)
|
||||
{
|
||||
// Get the column name at the current index
|
||||
CSStr name = (CSStr)sqlite3_column_name(mPtr, i);
|
||||
// Validate the name
|
||||
if (!name)
|
||||
{
|
||||
STHROWF("Unable to retrieve column name for index (%d)", i);
|
||||
}
|
||||
// Save it to guarantee the same lifetime as this instance
|
||||
else
|
||||
{
|
||||
mIndexes[name] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Attempt to find the specified column
|
||||
const Indexes::iterator itr = mIndexes.find(name);
|
||||
// Was there a column with the specified name?
|
||||
if (itr != mIndexes.end())
|
||||
{
|
||||
return itr->second;
|
||||
}
|
||||
// No such column exists (expecting the invoker to validate the result)
|
||||
return -1;
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
412
modules/sqlite/Handle/Statement.hpp
Normal file
412
modules/sqlite/Handle/Statement.hpp
Normal file
@@ -0,0 +1,412 @@
|
||||
#ifndef _SQSQLITE_HANDLE_STATEMENT_HPP_
|
||||
#define _SQSQLITE_HANDLE_STATEMENT_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Handle/Connection.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <map>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Manages a reference counted database statement handle.
|
||||
*/
|
||||
class StmtHnd
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class Connection;
|
||||
friend class Statement;
|
||||
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef sqlite3_stmt Type; // The managed type.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef Type* Pointer; // Pointer to the managed type.
|
||||
typedef const Type* ConstPtr; // Constant pointer to the managed type.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef Type& Reference; // Reference to the managed type.
|
||||
typedef const Type& ConstRef; // Constant reference to the managed type.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef unsigned int Counter; // Reference counter type.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the statement handle and throw an error if invalid.
|
||||
*/
|
||||
void Validate() const;
|
||||
|
||||
protected:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef std::map< String, int > Indexes; // Container used to identify column indexes.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The structure that holds the data associated with a certain statement.
|
||||
*/
|
||||
struct Handle
|
||||
{
|
||||
// ----------------------------------------------------------------------------------------
|
||||
Pointer mPtr; // The statement handle resource.
|
||||
Counter mRef; // Reference count to the managed handle.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
Int32 mStatus; // The last status code of this connection handle.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
ConnHnd mConn; // The handle to the associated database connection.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
String mQuery; // The query string used to create this statement.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
Int32 mColumns; // The amount of columns available in this statement.
|
||||
Indexes mIndexes; // An associative container with column names and their index.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
bool mGood; // True when a row has been fetched with step.
|
||||
bool mDone; // True when the last step had no more rows to fetch.
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
Handle(const ConnHnd & conn, Counter counter)
|
||||
: mPtr(nullptr)
|
||||
, mRef(counter)
|
||||
, mStatus(SQLITE_OK)
|
||||
, mConn(conn)
|
||||
, mQuery()
|
||||
, mColumns(0)
|
||||
, mIndexes()
|
||||
, mGood(false)
|
||||
, mDone(false)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~Handle();
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Create the database statement resource.
|
||||
*/
|
||||
void Create(CSStr query);
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Check whether a specific index is in range.
|
||||
*/
|
||||
bool CheckIndex(Int32 idx) const
|
||||
{
|
||||
return (idx >= 0) && (idx <= mColumns);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Retrieve the column index associated with the specified name.
|
||||
*/
|
||||
Int32 GetColumnIndex(CSStr name);
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Handle * m_Hnd; // The managed handle instance.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Grab a strong reference to a statement handle.
|
||||
*/
|
||||
void Grab()
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
++(m_Hnd->mRef);
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Drop a strong reference to a statement handle.
|
||||
*/
|
||||
void Drop()
|
||||
{
|
||||
if (m_Hnd && --(m_Hnd->mRef) == 0)
|
||||
{
|
||||
delete m_Hnd; // Let the destructor take care of cleaning up (if necessary)
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
StmtHnd(const ConnHnd & db)
|
||||
: m_Hnd(new Handle(db, 1))
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor (null).
|
||||
*/
|
||||
StmtHnd()
|
||||
: m_Hnd(nullptr)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
StmtHnd(const StmtHnd & o)
|
||||
: m_Hnd(o.m_Hnd)
|
||||
|
||||
{
|
||||
Grab();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
StmtHnd(StmtHnd && o)
|
||||
: m_Hnd(o.m_Hnd)
|
||||
{
|
||||
o.m_Hnd = nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~StmtHnd()
|
||||
{
|
||||
Drop();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
StmtHnd & operator = (const StmtHnd & o)
|
||||
{
|
||||
if (m_Hnd != o.m_Hnd)
|
||||
{
|
||||
Drop();
|
||||
m_Hnd = o.m_Hnd;
|
||||
Grab();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
StmtHnd & operator = (StmtHnd && o)
|
||||
{
|
||||
if (m_Hnd != o.m_Hnd)
|
||||
{
|
||||
m_Hnd = o.m_Hnd;
|
||||
o.m_Hnd = nullptr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Status assignment operator.
|
||||
*/
|
||||
StmtHnd & operator = (Int32 status)
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
m_Hnd->mStatus = status;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an equality comparison between two statement handles.
|
||||
*/
|
||||
bool operator == (const StmtHnd & o) const
|
||||
{
|
||||
return (m_Hnd == o.m_Hnd);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an inequality comparison between two statement handles.
|
||||
*/
|
||||
bool operator != (const StmtHnd & o) const
|
||||
{
|
||||
return (m_Hnd != o.m_Hnd);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an equality comparison with an integer value status.
|
||||
*/
|
||||
bool operator == (Int32 status) const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return (m_Hnd->mStatus == status);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an inequality comparison with an integer status value.
|
||||
*/
|
||||
bool operator != (Int32 status) const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return (m_Hnd->mStatus != status);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to boolean for use in boolean operations.
|
||||
*/
|
||||
operator bool () const
|
||||
{
|
||||
return m_Hnd && m_Hnd->mPtr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator Pointer ()
|
||||
{
|
||||
return m_Hnd ? m_Hnd->mPtr : nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator Pointer () const
|
||||
{
|
||||
return m_Hnd ? m_Hnd->mPtr : nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator Reference ()
|
||||
{
|
||||
assert(m_Hnd && m_Hnd->mPtr);
|
||||
return *(m_Hnd->mPtr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator ConstRef () const
|
||||
{
|
||||
assert(m_Hnd && m_Hnd->mPtr);
|
||||
return *(m_Hnd->mPtr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Member operator for dereferencing the managed pointer.
|
||||
*/
|
||||
Handle * operator -> () const
|
||||
{
|
||||
assert(m_Hnd);
|
||||
return m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Indirection operator for obtaining a reference of the managed pointer.
|
||||
*/
|
||||
Handle & operator * () const
|
||||
{
|
||||
assert(m_Hnd);
|
||||
return *m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the raw handle structure pointer.
|
||||
*/
|
||||
Handle * HndPtr()
|
||||
{
|
||||
return m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the raw handle structure pointer.
|
||||
*/
|
||||
Handle * HndPtr() const
|
||||
{
|
||||
return m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the number of active references to the managed instance.
|
||||
*/
|
||||
Counter Count() const
|
||||
{
|
||||
return m_Hnd ? m_Hnd->mRef : 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the last known status code.
|
||||
*/
|
||||
Int32 Status() const
|
||||
{
|
||||
return m_Hnd ? m_Hnd->mStatus : SQLITE_NOMEM;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the message of the last received error code.
|
||||
*/
|
||||
CCStr ErrStr() const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return m_Hnd->mConn.ErrStr();
|
||||
}
|
||||
return _SC("");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Return the last error message associated with this database connection.
|
||||
*/
|
||||
CCStr ErrMsg() const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return m_Hnd->mConn.ErrMsg();
|
||||
}
|
||||
return _SC("");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Return the numeric result code for the most recent failed API call (if any).
|
||||
*/
|
||||
Int32 ErrNo() const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return m_Hnd->mConn.ErrNo();
|
||||
}
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Return the extended numeric result code for the most recent failed API call (if any).
|
||||
*/
|
||||
Int32 ExErrNo() const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return m_Hnd->mConn.ExErrNo();
|
||||
}
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _SQSQLITE_HANDLE_STATEMENT_HPP_
|
Reference in New Issue
Block a user