1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00
SqMod/modules/mysql/Connection.hpp
2016-06-03 21:33:21 +03:00

362 lines
12 KiB
C++

#ifndef _SQMYSQL_CONNECTION_HPP_
#define _SQMYSQL_CONNECTION_HPP_
// ------------------------------------------------------------------------------------------------
#include "Handle/Connection.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* ...
*/
class Connection
{
private:
// --------------------------------------------------------------------------------------------
ConnHnd m_Handle; // Handle to the actual database connection.
public:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Connection()
: m_Handle()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
Connection(const Account & acc)
: m_Handle(acc)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Connection(const Connection & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Connection(Connection && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Connection() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Connection & operator = (const Connection & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Connection & operator = (Connection && o) = default;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Connection & o) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Validate the managed connection handle and throw exception if it doesn't exist.
*/
void Validate() const;
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed connection handle.
*/
operator ConnHnd & ()
{
return m_Handle;
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed connection handle.
*/
operator const ConnHnd & () const
{
return m_Handle;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the managed connection handle.
*/
ConnHnd & GetHnd()
{
return m_Handle;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the managed connection handle.
*/
const ConnHnd & GetHnd() const
{
return m_Handle;
}
/* --------------------------------------------------------------------------------------------
* See whether a successful connection was made or not.
*/
bool Connected() const
{
return m_Handle;
}
/* --------------------------------------------------------------------------------------------
* Disconnect from the currently connected database.
*/
void Disconnect()
{
// Validate the managed handle
m_Handle.Validate();
// Perform the requested operation
m_Handle->Disconnect();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the last received error number.
*/
SQInteger GetLastErrNo() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return static_cast< SQInteger >(m_Handle->mErrNo);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the last received error message.
*/
const String & GetLastErrStr() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mErrStr;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection port number.
*/
SQInteger GetPortNum() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return static_cast< SQInteger >(m_Handle->mPort);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection host address.
*/
const String & GetHost() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mHost;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection user name.
*/
const String & GetUser() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mUser;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection password.
*/
const String & GetPass() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mPass;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the selected database name.
*/
const String & GetName() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mName;
}
/* --------------------------------------------------------------------------------------------
* Modify the selected database name.
*/
void SetName(CSStr name);
/* --------------------------------------------------------------------------------------------
* Retrieve the connection socket.
*/
const String & GetSocket() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mSocket;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection flags.
*/
SQInteger GetFlags() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return static_cast< SQInteger >(m_Handle->mFlags);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection SSL key.
*/
const String & GetSSL_Key() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mSSL_Key;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection SSL certificate.
*/
const String & GetSSL_Cert() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mSSL_Cert;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection SSL certificate authority.
*/
const String & GetSSL_CA() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mSSL_CA;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection SSL certificate authority path.
*/
const String & GetSSL_CA_Path() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mSSL_CA_Path;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the connection SSL cipher.
*/
const String & GetSSL_Cipher() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mSSL_Cipher;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the default character set for the managed connection.
*/
const String & GetCharset() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mCharset;
}
/* --------------------------------------------------------------------------------------------
* Modify the default character set for the managed connection.
*/
void SetCharset(CSStr charset);
/* --------------------------------------------------------------------------------------------
* See whether auto-commit is enabled or not.
*/
bool GetAutoCommit() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mAutoCommit;
}
/* --------------------------------------------------------------------------------------------
* Set whether auto-commit should be enabled or not.
*/
void SetAutoCommit(bool toggle);
/* --------------------------------------------------------------------------------------------
* See whether the connection is in the middle of a transaction.
*/
bool GetInTransaction() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return m_Handle->mInTransaction;
}
/* --------------------------------------------------------------------------------------------
* Execute a query on the server.
*/
SQInteger Execute(CSStr query);
/* --------------------------------------------------------------------------------------------
* Execute a query on the server.
*/
SQInteger Insert(CSStr query);
/* --------------------------------------------------------------------------------------------
* Execute a query on the server.
*/
SQInteger Query(CSStr query);
/* --------------------------------------------------------------------------------------------
*
*/
Statement GetStatement(CSStr query);
};
} // Namespace:: SqMod
#endif // _SQMYSQL_CONNECTION_HPP_