2016-05-22 21:34:27 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-03 20:33:21 +02:00
|
|
|
#include "Connection.hpp"
|
2016-06-28 00:15:31 +02:00
|
|
|
#include "Account.hpp"
|
2016-06-03 20:33:21 +02:00
|
|
|
#include "Statement.hpp"
|
2016-06-03 21:17:52 +02:00
|
|
|
#include "ResultSet.hpp"
|
|
|
|
#include "Transaction.hpp"
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <cstring>
|
2016-05-22 21:34:27 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-03 20:33:21 +02:00
|
|
|
SQInteger Connection::Typename(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
static const SQChar name[] = _SC("SqMySQLConnection");
|
|
|
|
sq_pushstring(vm, name, sizeof(name));
|
|
|
|
return 1;
|
|
|
|
}
|
2016-05-22 21:34:27 +02:00
|
|
|
|
2016-06-03 20:33:21 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-28 00:15:31 +02:00
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
void Connection::Validate(CCStr file, Int32 line) const
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
if (!m_Handle)
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
SqThrowF("Invalid MySQL connection reference =>[%s:%d]", file, line);
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-28 00:15:31 +02:00
|
|
|
#else
|
|
|
|
void Connection::Validate() const
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
if (!m_Handle)
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
SqThrowF("Invalid MySQL connection reference");
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
2016-06-03 21:17:52 +02:00
|
|
|
}
|
2016-06-28 00:15:31 +02:00
|
|
|
#endif // _DEBUG
|
2016-06-03 21:17:52 +02:00
|
|
|
|
2016-06-03 20:33:21 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-28 00:15:31 +02:00
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
void Connection::ValidateCreated(CCStr file, Int32 line) const
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
if (!m_Handle)
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
SqThrowF("Invalid MySQL connection reference =>[%s:%d]", file, line);
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
2016-06-28 00:15:31 +02:00
|
|
|
else if (m_Handle->mPtr == nullptr)
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
SqThrowF("Invalid MySQL connection =>[%s:%d]", file, line);
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-28 00:15:31 +02:00
|
|
|
#else
|
|
|
|
void Connection::ValidateCreated() const
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
if (!m_Handle)
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
SqThrowF("Invalid MySQL connection reference");
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
2016-06-28 00:15:31 +02:00
|
|
|
else if (m_Handle->mPtr == nullptr)
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
SqThrowF("Invalid MySQL connection");
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-28 00:15:31 +02:00
|
|
|
#endif // _DEBUG
|
2016-06-03 20:33:21 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-28 00:15:31 +02:00
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
const ConnRef & Connection::GetValid(CCStr file, Int32 line) const
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
Validate(file, line);
|
|
|
|
return m_Handle;
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
2016-06-28 00:15:31 +02:00
|
|
|
#else
|
|
|
|
const ConnRef & Connection::GetValid() const
|
|
|
|
{
|
|
|
|
Validate();
|
|
|
|
return m_Handle;
|
|
|
|
}
|
|
|
|
#endif // _DEBUG
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
const ConnRef & Connection::GetCreated(CCStr file, Int32 line) const
|
|
|
|
{
|
|
|
|
ValidateCreated(file, line);
|
|
|
|
return m_Handle;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
const ConnRef & Connection::GetCreated() const
|
|
|
|
{
|
|
|
|
ValidateCreated();
|
|
|
|
return m_Handle;
|
|
|
|
}
|
|
|
|
#endif // _DEBUG
|
2016-06-03 20:33:21 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-03 21:17:52 +02:00
|
|
|
Object Connection::Insert(CSStr query)
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-03 21:17:52 +02:00
|
|
|
// Make sure the specified query is valid
|
|
|
|
if (!query || *query == '\0')
|
|
|
|
{
|
|
|
|
STHROWF("Invalid or empty MySQL query");
|
|
|
|
}
|
|
|
|
// Attempt to execute the specified query
|
2016-06-28 00:15:31 +02:00
|
|
|
else if (mysql_real_query(SQMOD_GET_CREATED(*this)->mPtr, query, std::strlen(query)) != 0)
|
2016-06-03 21:17:52 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
SQMOD_THROW_CURRENT(*m_Handle, "Unable to execute MySQL query");
|
2016-06-03 21:17:52 +02:00
|
|
|
}
|
|
|
|
// Return the identifier of the inserted row
|
2016-06-28 00:15:31 +02:00
|
|
|
return MakeULongObj(mysql_insert_id(m_Handle->mPtr));
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-03 21:17:52 +02:00
|
|
|
ResultSet Connection::Query(CSStr query)
|
2016-06-03 20:33:21 +02:00
|
|
|
{
|
2016-06-03 21:17:52 +02:00
|
|
|
// Make sure the specified query is valid
|
|
|
|
if (!query || *query == '\0')
|
|
|
|
{
|
|
|
|
STHROWF("Invalid or empty MySQL query");
|
|
|
|
}
|
|
|
|
// Attempt to execute the specified query
|
2016-06-28 00:15:31 +02:00
|
|
|
else if (mysql_real_query(SQMOD_GET_CREATED(*this)->mPtr, query, std::strlen(query)) != 0)
|
2016-06-03 21:17:52 +02:00
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
SQMOD_THROW_CURRENT(*m_Handle, "Unable to execute MySQL query");
|
2016-06-03 21:17:52 +02:00
|
|
|
}
|
|
|
|
// Return the identifier of the inserted row
|
2016-06-28 00:15:31 +02:00
|
|
|
return ResultSet(m_Handle);
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Statement Connection::GetStatement(CSStr query)
|
|
|
|
{
|
2016-06-28 00:15:31 +02:00
|
|
|
return Statement(SQMOD_GET_CREATED(*this), query);
|
2016-06-03 20:33:21 +02:00
|
|
|
}
|
2016-05-22 21:34:27 +02:00
|
|
|
|
2016-06-03 21:17:52 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Transaction Connection::GetTransaction()
|
|
|
|
{
|
|
|
|
return Transaction();
|
|
|
|
}
|
|
|
|
|
2016-07-27 21:52:59 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Connection::ExecuteF(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
const Int32 top = sq_gettop(vm);
|
|
|
|
// Was the query value specified?
|
|
|
|
if (top <= 1)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Missing query value");
|
|
|
|
}
|
|
|
|
// The connection instance
|
|
|
|
Connection * conn = nullptr;
|
|
|
|
// Attempt to extract the argument values
|
|
|
|
try
|
|
|
|
{
|
|
|
|
conn = Var< Connection * >(vm, 1).value;
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// Propagate the error
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Do we have a valid connection instance?
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Invalid MySQL connection instance");
|
|
|
|
}
|
|
|
|
// Validate the connection info
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SQMOD_VALIDATE_CREATED(*conn);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// Propagate the error
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Attempt to retrieve the value from the stack as a string
|
|
|
|
StackStrF val(vm, 2);
|
|
|
|
// Have we failed to retrieve the string?
|
|
|
|
if (SQ_FAILED(val.mRes))
|
|
|
|
{
|
|
|
|
return val.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
// Make sure the query string is valid
|
|
|
|
else if (!(val.mPtr) || *(val.mPtr) == '\0')
|
|
|
|
{
|
|
|
|
STHROWF("Invalid or empty MySQL query");
|
|
|
|
}
|
|
|
|
// Attempt to execute the specified query
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const SQRESULT res = SqMod_PushULongObject(vm, conn->m_Handle->Execute(val.mPtr, val.mLen));
|
|
|
|
// Validate the result of pushing the number of database changes on the stack
|
|
|
|
if (SQ_FAILED(res))
|
|
|
|
{
|
|
|
|
return res; // Propagate the error!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// Propagate the error
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// This function returned a value
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Connection::InsertF(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
const Int32 top = sq_gettop(vm);
|
|
|
|
// Was the query value specified?
|
|
|
|
if (top <= 1)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Missing query value");
|
|
|
|
}
|
|
|
|
// The connection instance
|
|
|
|
Connection * conn = nullptr;
|
|
|
|
// Attempt to extract the argument values
|
|
|
|
try
|
|
|
|
{
|
|
|
|
conn = Var< Connection * >(vm, 1).value;
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// Propagate the error
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Do we have a valid connection instance?
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Invalid MySQL connection instance");
|
|
|
|
}
|
|
|
|
// Validate the connection info
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SQMOD_VALIDATE_CREATED(*conn);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// Propagate the error
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Attempt to retrieve the value from the stack as a string
|
|
|
|
StackStrF val(vm, 2);
|
|
|
|
// Have we failed to retrieve the string?
|
|
|
|
if (SQ_FAILED(val.mRes))
|
|
|
|
{
|
|
|
|
return val.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
// Make sure the query string is valid
|
|
|
|
else if (!(val.mPtr) || *(val.mPtr) == '\0')
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Invalid or empty MySQL query");
|
|
|
|
}
|
|
|
|
// Attempt to execute the specified query
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (mysql_real_query(conn->m_Handle->mPtr, val.mPtr, val.mLen) != 0)
|
|
|
|
{
|
|
|
|
SQMOD_THROW_CURRENT(*(conn->m_Handle), "Unable to execute MySQL query");
|
|
|
|
}
|
|
|
|
// Return the identifier of the inserted row
|
|
|
|
const SQRESULT res = SqMod_PushULongObject(vm, mysql_insert_id(conn->m_Handle->mPtr));
|
|
|
|
// Validate the result of pushing the identifier of the inserted row
|
|
|
|
if (SQ_FAILED(res))
|
|
|
|
{
|
|
|
|
return res; // Propagate the error!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// Propagate the error
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// This function returned a value
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Connection::QueryF(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
const Int32 top = sq_gettop(vm);
|
|
|
|
// Was the query value specified?
|
|
|
|
if (top <= 1)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Missing query value");
|
|
|
|
}
|
|
|
|
// The connection instance
|
|
|
|
Connection * conn = nullptr;
|
|
|
|
// Attempt to extract the argument values
|
|
|
|
try
|
|
|
|
{
|
|
|
|
conn = Var< Connection * >(vm, 1).value;
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// Propagate the error
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Do we have a valid connection instance?
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Invalid MySQL connection instance");
|
|
|
|
}
|
|
|
|
// Validate the connection info
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SQMOD_VALIDATE_CREATED(*conn);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// Propagate the error
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// Attempt to retrieve the value from the stack as a string
|
|
|
|
StackStrF val(vm, 2);
|
|
|
|
// Have we failed to retrieve the string?
|
|
|
|
if (SQ_FAILED(val.mRes))
|
|
|
|
{
|
|
|
|
return val.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
// Make sure the query string is valid
|
|
|
|
else if (!(val.mPtr) || *(val.mPtr) == '\0')
|
|
|
|
{
|
|
|
|
return sq_throwerror(vm, "Invalid or empty MySQL query");
|
|
|
|
}
|
|
|
|
// Attempt to execute the specified query
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (mysql_real_query(conn->m_Handle->mPtr, val.mPtr, val.mLen) != 0)
|
|
|
|
{
|
|
|
|
SQMOD_THROW_CURRENT(*(conn->m_Handle), "Unable to execute MySQL query");
|
|
|
|
}
|
|
|
|
// Return a new instance with the obtained result set
|
|
|
|
Var< ResultSet * >::push(vm, new ResultSet(conn->m_Handle));
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
// Propagate the error
|
|
|
|
return sq_throwerror(vm, e.what());
|
|
|
|
}
|
|
|
|
// This function returned a value
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-28 00:15:31 +02:00
|
|
|
// ================================================================================================
|
|
|
|
void Register_Connection(Table & sqlns)
|
|
|
|
{
|
|
|
|
sqlns.Bind(_SC("Connection")
|
|
|
|
, Class< Connection >(sqlns.GetVM(), _SC("SqMySQLConnection"))
|
|
|
|
// Constructors
|
|
|
|
.Ctor()
|
|
|
|
.Ctor< const Account & >()
|
|
|
|
// Core Meta-methods
|
|
|
|
.Func(_SC("_cmp"), &Connection::Cmp)
|
|
|
|
.SquirrelFunc(_SC("_typename"), &Connection::Typename)
|
|
|
|
.Func(_SC("_tostring"), &Connection::ToString)
|
|
|
|
// Properties
|
|
|
|
.Prop(_SC("IsValid"), &Connection::IsValid)
|
|
|
|
.Prop(_SC("Connected"), &Connection::IsConnected)
|
|
|
|
.Prop(_SC("References"), &Connection::GetRefCount)
|
|
|
|
.Prop(_SC("ErrNo"), &Connection::GetErrNo)
|
|
|
|
.Prop(_SC("ErrStr"), &Connection::GetErrStr)
|
|
|
|
.Prop(_SC("LastErrNo"), &Connection::GetLastErrNo)
|
|
|
|
.Prop(_SC("LastErrStr"), &Connection::GetLastErrStr)
|
|
|
|
.Prop(_SC("Port"), &Connection::GetPortNum)
|
|
|
|
.Prop(_SC("Host"), &Connection::GetHost)
|
|
|
|
.Prop(_SC("User"), &Connection::GetUser)
|
|
|
|
.Prop(_SC("Pass"), &Connection::GetPass)
|
|
|
|
.Prop(_SC("Name"), &Connection::GetName, &Connection::SetName)
|
|
|
|
.Prop(_SC("Socket"), &Connection::GetSocket)
|
|
|
|
.Prop(_SC("Flags"), &Connection::GetFlags)
|
|
|
|
.Prop(_SC("SSL_Key"), &Connection::GetSSL_Key)
|
|
|
|
.Prop(_SC("SSL_Cert"), &Connection::GetSSL_Cert)
|
|
|
|
.Prop(_SC("SSL_CA"), &Connection::GetSSL_CA)
|
|
|
|
.Prop(_SC("SSL_CA_Path"), &Connection::GetSSL_CA_Path)
|
|
|
|
.Prop(_SC("SSL_Cipher"), &Connection::GetSSL_Cipher)
|
|
|
|
.Prop(_SC("Charset"), &Connection::GetCharset, &Connection::SetCharset)
|
|
|
|
.Prop(_SC("AutoCommit"), &Connection::GetAutoCommit, &Connection::SetAutoCommit)
|
|
|
|
.Prop(_SC("InTransaction"), &Connection::GetInTransaction)
|
|
|
|
// Member Methods
|
|
|
|
.Func(_SC("Disconnect"), &Connection::Disconnect)
|
|
|
|
.Func(_SC("SelectDb"), &Connection::SetName)
|
|
|
|
.Func(_SC("Execute"), &Connection::Execute)
|
|
|
|
.Func(_SC("Insert"), &Connection::Insert)
|
|
|
|
.Func(_SC("Query"), &Connection::Query)
|
|
|
|
.Func(_SC("Statement"), &Connection::GetStatement)
|
|
|
|
.Func(_SC("Transaction"), &Connection::GetTransaction)
|
2016-07-27 21:52:59 +02:00
|
|
|
// Squirrel Methods
|
|
|
|
.SquirrelFunc(_SC("ExecuteF"), &Connection::ExecuteF)
|
|
|
|
.SquirrelFunc(_SC("InsertF"), &Connection::InsertF)
|
|
|
|
.SquirrelFunc(_SC("QueryF"), &Connection::QueryF)
|
2016-06-28 00:15:31 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-22 21:34:27 +02:00
|
|
|
} // Namespace:: SqMod
|