mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-06-15 22:57:12 +02:00
Partial and untested revision of the MySQL module.
This commit is contained in:
@ -3,22 +3,16 @@
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Handle/Connection.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <Base/Buffer.hpp>
|
||||
#include "Base/Buffer.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Manages a reference counted statement handle.
|
||||
* The structure that holds the data associated with a certain bind point.
|
||||
*/
|
||||
class StmtHnd
|
||||
struct StmtBind
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class Connection;
|
||||
friend class Statement;
|
||||
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
@ -33,409 +27,163 @@ public:
|
||||
typedef const Type& ConstRef; // Constant reference to the managed type.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef unsigned int Counter; // Reference counter type.
|
||||
typedef MYSQL_BIND BindType; // Database bind type.
|
||||
typedef MYSQL_TIME TimeType; // Database time type.
|
||||
typedef my_bool BoolType; // Database boolean type.
|
||||
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
BoolType mIsNull; // Allows the database to specify if the parameter is null.
|
||||
BoolType mError; // Allows the database if errors occured on this parameter.
|
||||
Buffer mData; // Buffer to store non fundamental data for the parameter.
|
||||
BindType * mBind; // The associated database bind point handle.
|
||||
TimeType mTime; // Structure used to store time data from database.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
union
|
||||
{
|
||||
Uint64 mUint64; // Store unsigned integer values for the parameter.
|
||||
Int64 mInt64; // Store signed integer values for the parameter.
|
||||
Int32 mInt32[2]; // Store 32 bit signed integer values for the parameter.
|
||||
Float64 mFloat64; // Store 32 bit floating point values for the parameter.
|
||||
Float32 mFloat32[2]; // Store 64 bit floating point values for the parameter.
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
StmtBind()
|
||||
: mIsNull(0), mError(0), mData(), mBind(nullptr), mTime(), mUint64(0)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor. (disabled)
|
||||
*/
|
||||
StmtBind(const StmtBind & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor. (disabled)
|
||||
*/
|
||||
StmtBind(StmtBind && o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator. (disabled)
|
||||
*/
|
||||
StmtBind & operator = (const StmtBind & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator. (disabled)
|
||||
*/
|
||||
StmtBind & operator = (StmtBind && o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the used buffer.
|
||||
*/
|
||||
CStr GetBuffer()
|
||||
{
|
||||
return mData ? mData.Data() : reinterpret_cast< CStr >(&mUint64);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the buffer length.
|
||||
*/
|
||||
Ulong GetLength() const
|
||||
{
|
||||
return (mBind == nullptr) ? 0 : mBind->buffer_length;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Configure the input of a certain statement parameter.
|
||||
*/
|
||||
void SetInput(enum_field_types type, BindType * bind, CCStr buffer = nullptr, Ulong length = 0);
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* The structure that holds the data associated with a certain statement handle.
|
||||
*/
|
||||
struct StmtHnd
|
||||
{
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef MYSQL_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 MYSQL_BIND BindType; // Database bind type.
|
||||
typedef MYSQL_TIME TimeType; // Database time type.
|
||||
typedef my_bool BoolType; // Database boolean type.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the statement handle and throw an error if invalid.
|
||||
*/
|
||||
void Validate() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the statement handle and parameter index and throw an error if invalid.
|
||||
*/
|
||||
void ValidateIndex(Uint32 idx) const;
|
||||
|
||||
protected:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef std::map< String, int > Indexes; // Container used to identify column indexes.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The structure that holds the data associated with a certain bind point.
|
||||
*/
|
||||
struct Bind
|
||||
{
|
||||
// ----------------------------------------------------------------------------------------
|
||||
BoolType mIsNull; // Allows the database to specify if the parameter is null.
|
||||
BoolType mError; // Allows the database if errors occured on this parameter.
|
||||
Buffer mData; // Buffer to store non fundamental data for the parameter.
|
||||
BindType * mBind; // The associated database bind point handle.
|
||||
TimeType mTime; // Structure used to store time data from database.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
union
|
||||
{
|
||||
Uint64 mUint64; // Store unsigned integer values for the parameter.
|
||||
Int64 mInt64; // Store signed integer values for the parameter.
|
||||
Int32 mInt32[2]; // Store 32 bit signed integer values for the parameter.
|
||||
Float64 mFloat64; // Store 32 bit floating point values for the parameter.
|
||||
Float32 mFloat32[2]; // Store 64 bit floating point values for the parameter.
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
Bind()
|
||||
: mIsNull(0), mError(0), mData(), mBind(nullptr), mTime(), mUint64(0)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Copy constructor. (disabled)
|
||||
*/
|
||||
Bind(const Bind & o) = delete;
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Move constructor. (disabled)
|
||||
*/
|
||||
Bind(Bind && o) = delete;
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Copy assignment operator. (disabled)
|
||||
*/
|
||||
Bind & operator = (const Bind & o) = delete;
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Move assignment operator. (disabled)
|
||||
*/
|
||||
Bind & operator = (Bind && o) = delete;
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Retrieve the used buffer.
|
||||
*/
|
||||
CStr GetBuffer()
|
||||
{
|
||||
return mData ? mData.Data() : reinterpret_cast< CStr >(&mUint64);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Retrieve the buffer length.
|
||||
*/
|
||||
Ulong GetLength() const
|
||||
{
|
||||
return mBind == nullptr ? 0 : mBind->buffer_length;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Configure the input of a certain statement parameter.
|
||||
*/
|
||||
void SetInput(enum_field_types type, BindType * bind, CCStr buffer = nullptr, Ulong length = 0UL);
|
||||
};
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The structure that holds the data associated with a certain statement handle.
|
||||
*/
|
||||
struct Handle
|
||||
{
|
||||
// ----------------------------------------------------------------------------------------
|
||||
Pointer mPtr; // The managed statement handle.
|
||||
Counter mRef; // Reference count to the managed handle.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
Uint32 mErrNo; // Last received error string.
|
||||
String mErrStr; // Last received error message.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
Ulong mParams; // Number of parameters in the statement.
|
||||
Bind * mBinds; // List of parameter binds.
|
||||
BindType * mMyBinds; // List of parameter binds.
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
ConnHnd mConnection; // Reference to the associated connection.
|
||||
String mQuery; // The query string.
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
Handle(const ConnHnd & conn, CSStr query);
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~Handle();
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Grab the current error in the connection handle and throw it.
|
||||
*/
|
||||
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
||||
void ThrowCurrent(CCStr act, CCStr file, Int32 line);
|
||||
#else
|
||||
void ThrowCurrent(CCStr act);
|
||||
#endif // _DEBUG
|
||||
|
||||
/* ----------------------------------------------------------------------------------------
|
||||
* Create the database statement resource.
|
||||
*/
|
||||
void Create(CSStr query);
|
||||
};
|
||||
|
||||
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, CSStr query)
|
||||
: m_Hnd(new Handle(db, query))
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor (null).
|
||||
*/
|
||||
StmtHnd()
|
||||
: m_Hnd(nullptr)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Pointer mPtr; // The managed statement handle.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Uint32 mErrNo; // Last received error string.
|
||||
String mErrStr; // Last received error message.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Ulong mParams; // Number of parameters in the statement.
|
||||
StmtBind * mBinds; // List of parameter binds.
|
||||
BindType * mMyBinds; // List of parameter binds.
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
ConnRef mConnection; // Reference to the associated connection.
|
||||
String mQuery; // The query string.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
* Default 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;
|
||||
}
|
||||
StmtHnd();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~StmtHnd()
|
||||
{
|
||||
Drop();
|
||||
}
|
||||
~StmtHnd();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
* Grab the current error in the associated statement handle.
|
||||
*/
|
||||
StmtHnd & operator = (const StmtHnd & o)
|
||||
{
|
||||
if (m_Hnd != o.m_Hnd)
|
||||
{
|
||||
Drop();
|
||||
m_Hnd = o.m_Hnd;
|
||||
Grab();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
void GrabCurrent();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
* Grab the current error in the associated statement handle and throw it.
|
||||
*/
|
||||
StmtHnd & operator = (StmtHnd && o)
|
||||
{
|
||||
if (m_Hnd != o.m_Hnd)
|
||||
{
|
||||
m_Hnd = o.m_Hnd;
|
||||
o.m_Hnd = nullptr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
||||
void ThrowCurrent(CCStr act, CCStr file, Int32 line);
|
||||
#else
|
||||
void ThrowCurrent(CCStr act);
|
||||
#endif // _DEBUG
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Status assignment operator.
|
||||
* Validate the associated statement handle and parameter index and throw an error if invalid.
|
||||
*/
|
||||
StmtHnd & operator = (Uint32 status)
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
m_Hnd->mErrNo = status;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
||||
void ValidateParam(Uint32 idx, CCStr file, Int32 line) const;
|
||||
#else
|
||||
void ValidateParam(Uint32 idx) const;
|
||||
#endif // _DEBUG
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an equality comparison between two statement handles.
|
||||
* Create the actual statement.
|
||||
*/
|
||||
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 == (Uint32 status) const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return (m_Hnd->mErrNo == status);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Perform an inequality comparison with an integer status value.
|
||||
*/
|
||||
bool operator != (Uint32 status) const
|
||||
{
|
||||
if (m_Hnd)
|
||||
{
|
||||
return (m_Hnd->mErrNo != status);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to boolean for use in boolean operations.
|
||||
*/
|
||||
operator bool () const
|
||||
{
|
||||
return (m_Hnd != nullptr) && (m_Hnd->mPtr != nullptr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator Pointer ()
|
||||
{
|
||||
return (m_Hnd != nullptr) ? m_Hnd->mPtr : nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator Pointer () const
|
||||
{
|
||||
return (m_Hnd != nullptr) ? m_Hnd->mPtr : nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator Reference ()
|
||||
{
|
||||
assert((m_Hnd != nullptr) && (m_Hnd->mPtr != nullptr));
|
||||
return *(m_Hnd->mPtr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit conversion to the managed instance.
|
||||
*/
|
||||
operator ConstRef () const
|
||||
{
|
||||
assert((m_Hnd != nullptr) && (m_Hnd->mPtr != nullptr));
|
||||
return *(m_Hnd->mPtr);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Member operator for dereferencing the managed pointer.
|
||||
*/
|
||||
Handle * operator -> () const
|
||||
{
|
||||
assert(m_Hnd != nullptr);
|
||||
return m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Indirection operator for obtaining a reference of the managed pointer.
|
||||
*/
|
||||
Handle & operator * () const
|
||||
{
|
||||
assert(m_Hnd != nullptr);
|
||||
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 != nullptr) ? m_Hnd->mRef : 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the handle reference but only if valid.
|
||||
*/
|
||||
Handle & GetHnd()
|
||||
{
|
||||
// Validate the managed handle
|
||||
Validate();
|
||||
// Return the requesed information
|
||||
return *m_Hnd;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the handle reference but only if valid.
|
||||
*/
|
||||
const Handle & GetHnd() const
|
||||
{
|
||||
// Validate the managed handle
|
||||
Validate();
|
||||
// Return the requesed information
|
||||
return *m_Hnd;
|
||||
}
|
||||
void Create(const ConnRef & conn, CSStr query);
|
||||
};
|
||||
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _SQMYSQL_HANDLE_STATEMENT_HPP_
|
||||
|
Reference in New Issue
Block a user