mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2026-08-18 05:37:11 +02:00
Partial untested implementation of the MySQL module.
This commit is contained in:
+118
-35
@@ -1,49 +1,126 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Account.hpp"
|
||||
#include "Connection.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <cstring>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Account::Account(CSStr host, CSStr user, CSStr pass, CSStr schema, Uint16 port, CSStr socket)
|
||||
: m_Host(host ? host : _SC(""))
|
||||
, m_User(user ? user : _SC(""))
|
||||
, m_Pass(pass ? pass : _SC(""))
|
||||
, m_Port(port)
|
||||
, m_Schema(schema ? schema : _SC(""))
|
||||
, m_Socket(socket ? socket : _SC(""))
|
||||
const String Account::s_String = String(_SC(""));
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger Account::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
static const SQChar name[] = _SC("SqMySQLAccount");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Account::Account(CSStr host, CSStr user, CSStr pass, CSStr name, SQInteger port, CSStr socket)
|
||||
: m_Port(0)
|
||||
, m_Host()
|
||||
, m_User()
|
||||
, m_Pass()
|
||||
, m_Name()
|
||||
, m_Socket()
|
||||
, m_Flags(CLIENT_MULTI_STATEMENTS)
|
||||
, m_SSL_Key()
|
||||
, m_SSL_Cert()
|
||||
, m_SSL_CA()
|
||||
, m_SSL_CA_Path()
|
||||
, m_SSL_Cipher()
|
||||
, m_Autocommit()
|
||||
, m_Options()
|
||||
, m_AutoCommit(false)
|
||||
{
|
||||
// Attempt to extract the port number from the host
|
||||
String::size_type pos = m_Host.find(':');
|
||||
// Was it specified in the host
|
||||
if (pos != String::npos)
|
||||
// Validate the specified port number
|
||||
if (port >= 0xFFFF)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Attempt to extract the numeric value
|
||||
ULong num = std::stol(m_Host.substr(pos + 1));
|
||||
// Is the port number withing range?
|
||||
if (num > 0xFFFF)
|
||||
{
|
||||
SHROWF("Port number out of range: %lu");
|
||||
}
|
||||
// Assign the extracted port number
|
||||
m_Port = static_cast< Uint16 >(num);
|
||||
// Remove everything after the port number
|
||||
m_Host = m_Host.substr(0, pos);
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
SHROWF("Cannot extract the port number [%s]", e.what());
|
||||
}
|
||||
STHROWF("Port number out of range: " _PRINT_INT_FMT, port);
|
||||
}
|
||||
// Assign the specified port
|
||||
else
|
||||
{
|
||||
m_Port = ConvTo< Uint16 >::From(port);
|
||||
}
|
||||
// Assign the remaining values
|
||||
SetHost(host);
|
||||
SetUser(user);
|
||||
SetPass(pass);
|
||||
SetName(name);
|
||||
SetSocket(socket);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 Account::Cmp(const Account & o) const
|
||||
{
|
||||
if (m_User == o.m_User && m_Pass == o.m_Pass)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (m_User > o.m_User && m_Pass > o.m_Pass)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr Account::ToString() const
|
||||
{
|
||||
return ToStringF("%s:%s@%s:%u", m_User.c_str(), m_Pass.c_str(), m_Host.c_str(), m_Port);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Account::SetHost(CSStr addr)
|
||||
{
|
||||
// Clear the current host address
|
||||
m_Host.assign(_SC(""));
|
||||
// Validate the given address
|
||||
if (!addr || *addr == '\0')
|
||||
{
|
||||
return; // Nothing to set.
|
||||
}
|
||||
// See if there'sa colon in the address
|
||||
CSStr pos = std::strchr(addr, ':');
|
||||
// Should we look for a port number?
|
||||
if (pos != nullptr)
|
||||
{
|
||||
// Assign the address portion
|
||||
m_Host.assign(addr, pos - addr);
|
||||
// Attempt to extract the numeric value
|
||||
const Ulong num = std::strtoul(++pos, nullptr, 10);
|
||||
// Is the port number withing range?
|
||||
if (num > 0xFFFF)
|
||||
{
|
||||
STHROWF("Port number out of range: %lu", num);
|
||||
}
|
||||
// Assign the extracted port number
|
||||
m_Port = ConvTo< Uint16 >::From(num);
|
||||
}
|
||||
// Assign the address as is
|
||||
else
|
||||
{
|
||||
m_Host.assign(addr);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Account::SetPortNum(SQInteger port)
|
||||
{
|
||||
// Validate the specified port number
|
||||
if (port >= 0xFFFF)
|
||||
{
|
||||
STHROWF("Port number out of range: " _PRINT_INT_FMT, port);
|
||||
}
|
||||
// Assign the specified port number
|
||||
m_Port = ConvTo< Uint16 >::From(port);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@@ -68,7 +145,7 @@ void Account::SetSSL(CSStr key, CSStr cert, CSStr ca, CSStr ca_path, CSStr ciphe
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Table Account::GetOptions() const
|
||||
Table Account::GetOptionsTable() const
|
||||
{
|
||||
// Allocate an empty table
|
||||
Table tbl(DefaultVM::Get());
|
||||
@@ -87,7 +164,7 @@ const String & Account::GetOption(CSStr name) const
|
||||
// Make sure the specified name is valid
|
||||
if (!name || *name == '\0')
|
||||
{
|
||||
SHROWF("Invalid or empty option name");
|
||||
STHROWF("Invalid or empty option name");
|
||||
}
|
||||
// Attempt to find the requested option
|
||||
const Options::const_iterator itr = m_Options.find(name);
|
||||
@@ -101,22 +178,28 @@ void Account::SetOption(CSStr name, CSStr value)
|
||||
// Make sure the specified name is valid
|
||||
if (!name || *name == '\0')
|
||||
{
|
||||
SHROWF("Invalid or empty option name");
|
||||
STHROWF("Invalid or empty option name");
|
||||
}
|
||||
// Assign the specified value
|
||||
m_Options[name] = value ? value : _SC("");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Account::RemoveOption(CSStr name) const
|
||||
void Account::RemoveOption(CSStr name)
|
||||
{
|
||||
// Make sure the specified name is valid
|
||||
if (!name || *name == '\0')
|
||||
{
|
||||
SHROWF("Invalid or empty option name");
|
||||
STHROWF("Invalid or empty option name");
|
||||
}
|
||||
// Erase the specified value
|
||||
m_Options.erase(name);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Connection Account::Connect() const
|
||||
{
|
||||
return Connection(*this);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
Reference in New Issue
Block a user