1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-19 16:47:14 +02:00

Partial and untested revision of the MySQL module.

This commit is contained in:
Sandu Liviu Catalin
2016-06-28 01:15:31 +03:00
parent fdc73d3ee8
commit 03237f9c15
18 changed files with 1599 additions and 1747 deletions

View File

@ -8,14 +8,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:
// --------------------------------------------------------------------------------------------
@ -29,343 +25,79 @@ 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.
// --------------------------------------------------------------------------------------------
typedef MYSQL_RES ResType; // Database result type.
/* --------------------------------------------------------------------------------------------
* Validate the connection handle and throw an error if invalid.
*/
void Validate() const;
protected:
/* --------------------------------------------------------------------------------------------
* 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.
// ----------------------------------------------------------------------------------------
Uint32 mErrNo; // Last received error string.
String mErrStr; // Last received error message.
// ----------------------------------------------------------------------------------------
Uint16 mPort; // Server port.
String mHost; // Host address.
String mUser; // User name user.
String mPass; // User password.
String mName; // Database name.
String mSocket; // Unix socket.
Ulong mFlags; // Client flags.
// ----------------------------------------------------------------------------------------
String mSSL_Key; // SSL key.
String mSSL_Cert; // SSL certificate.
String mSSL_CA; // SSL certificate authority.
String mSSL_CA_Path; // SSL certificate authority path.
String mSSL_Cipher; // SSL Cipher.
// ----------------------------------------------------------------------------------------
String mCharset; // Default connection character set.
// ----------------------------------------------------------------------------------------
bool mAutoCommit; // Whether autocommit is enabled on this connection.
bool mInTransaction; // Whether the connection is in the middle of a transaction.
/* ----------------------------------------------------------------------------------------
* Base constructor.
*/
Handle(const Account & acc);
/* ----------------------------------------------------------------------------------------
* 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
/* ----------------------------------------------------------------------------------------
* Disconnect the managed connection handle.
*/
void Disconnect();
/* ----------------------------------------------------------------------------------------
* Execute a query on the server.
*/
Uint64 Execute(CSStr query, Ulong size = 0UL);
};
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(const Account & acc)
: m_Hnd(new Handle(acc))
{
/* ... */
}
public:
/* --------------------------------------------------------------------------------------------
* Default constructor (null).
*/
ConnHnd()
: m_Hnd(nullptr)
{
/* ... */
}
// --------------------------------------------------------------------------------------------
Pointer mPtr; // The connection handle resource.
// --------------------------------------------------------------------------------------------
Uint32 mErrNo; // Last received error string.
String mErrStr; // Last received error message.
// --------------------------------------------------------------------------------------------
Uint16 mPort; // Server port.
String mHost; // Host address.
String mUser; // User name user.
String mPass; // User password.
String mName; // Database name.
String mSocket; // Unix socket.
Ulong mFlags; // Client flags.
// --------------------------------------------------------------------------------------------
String mSSL_Key; // SSL key.
String mSSL_Cert; // SSL certificate.
String mSSL_CA; // SSL certificate authority.
String mSSL_CA_Path; // SSL certificate authority path.
String mSSL_Cipher; // SSL Cipher.
// --------------------------------------------------------------------------------------------
String mCharset; // Default connection character set.
// --------------------------------------------------------------------------------------------
bool mAutoCommit; // Whether autocommit is enabled on this connection.
bool mInTransaction; // Whether the connection is in the middle of a transaction.
/* --------------------------------------------------------------------------------------------
* Copy constructor.
* Default 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;
}
ConnHnd();
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~ConnHnd()
{
Drop();
}
~ConnHnd();
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
* Grab the current error in the connection handle.
*/
ConnHnd & operator = (const ConnHnd & 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 connection handle and throw it.
*/
ConnHnd & operator = (ConnHnd && 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.
* Create the connection handle.
*/
ConnHnd & operator = (Uint32 status)
{
if (m_Hnd)
{
m_Hnd->mErrNo = status;
}
return *this;
}
void Create(const Account & acc);
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two connection handles.
* Disconnect the managed connection handle.
*/
bool operator == (const ConnHnd & o) const
{
return (m_Hnd == o.m_Hnd);
}
void Disconnect();
/* --------------------------------------------------------------------------------------------
* Perform an inequality comparison between two connection handles.
* Execute a query on the server.
*/
bool operator != (const ConnHnd & 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 value status.
*/
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;
}
Uint64 Execute(CSStr query, Ulong size = 0UL);
};
} // Namespace:: SqMod