mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-06-19 16:47:14 +02:00
Untested revised implementation of the SQLite module.
This commit is contained in:
@ -11,14 +11,10 @@
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Manages a reference counted database connection handle.
|
||||
* The structure that holds the data associated with a certain connection.
|
||||
*/
|
||||
class ConnHnd
|
||||
struct ConnHnd
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class Connection;
|
||||
friend class Statement;
|
||||
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
@ -32,334 +28,76 @@ public:
|
||||
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)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Pointer mPtr; // The connection handle resource.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
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.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
* Default constructor.
|
||||
*/
|
||||
ConnHnd(const ConnHnd & o)
|
||||
: m_Hnd(o.m_Hnd)
|
||||
{
|
||||
Grab();
|
||||
}
|
||||
ConnHnd();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
* Copy constructor. (disabled)
|
||||
*/
|
||||
ConnHnd(ConnHnd && o)
|
||||
: m_Hnd(o.m_Hnd)
|
||||
{
|
||||
o.m_Hnd = nullptr;
|
||||
}
|
||||
ConnHnd(const ConnHnd & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor. (disabled)
|
||||
*/
|
||||
ConnHnd(ConnHnd && o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~ConnHnd()
|
||||
{
|
||||
Drop();
|
||||
}
|
||||
~ConnHnd();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
* Copy assignment operator. (disabled)
|
||||
*/
|
||||
ConnHnd & operator = (const ConnHnd & o)
|
||||
{
|
||||
if (m_Hnd != o.m_Hnd)
|
||||
{
|
||||
Drop();
|
||||
m_Hnd = o.m_Hnd;
|
||||
Grab();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
ConnHnd & operator = (const ConnHnd & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
* Move assignment operator. (disabled)
|
||||
*/
|
||||
ConnHnd & operator = (ConnHnd && o)
|
||||
{
|
||||
if (m_Hnd != o.m_Hnd)
|
||||
{
|
||||
m_Hnd = o.m_Hnd;
|
||||
o.m_Hnd = nullptr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
ConnHnd & operator = (ConnHnd && o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Status assignment operator.
|
||||
* Create the database connection resource.
|
||||
*/
|
||||
ConnHnd & operator = (Int32 status)
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
m_Hnd->mStatus = status;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
void Create(CSStr name, Int32 flags, CSStr vfs);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an equality comparison between two connection handles.
|
||||
* Execute a specific amount of queries from the queue.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
Int32 Flush(Uint32 num, Object & env, Function & func);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* 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 sqlite3_errstr(sqlite3_errcode(mPtr));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -367,12 +105,7 @@ public:
|
||||
*/
|
||||
CCStr ErrMsg() const
|
||||
{
|
||||
// SQLite does it's null pointer validations internally
|
||||
if (m_Hnd)
|
||||
{
|
||||
return sqlite3_errmsg(m_Hnd->mPtr);
|
||||
}
|
||||
return _SC("");
|
||||
return sqlite3_errmsg(mPtr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -380,12 +113,7 @@ public:
|
||||
*/
|
||||
Int32 ErrNo() const
|
||||
{
|
||||
// SQLite does it's null pointer validations internally
|
||||
if (m_Hnd)
|
||||
{
|
||||
return sqlite3_errcode(m_Hnd->mPtr);
|
||||
}
|
||||
return SQLITE_NOMEM;
|
||||
return sqlite3_errcode(mPtr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -393,12 +121,7 @@ public:
|
||||
*/
|
||||
Int32 ExErrNo() const
|
||||
{
|
||||
// SQLite does it's null pointer validations internally
|
||||
if (m_Hnd)
|
||||
{
|
||||
return sqlite3_extended_errcode(m_Hnd->mPtr);
|
||||
}
|
||||
return SQLITE_NOMEM;
|
||||
return sqlite3_extended_errcode(mPtr);
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user