1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-31 09:57:14 +01:00

Implement the query execution functions on MySQL connection.

Minor adjustments in comments.
This commit is contained in:
Sandu Liviu Catalin 2016-06-03 22:17:52 +03:00
parent 2f16d63e2a
commit 525148ccd1
7 changed files with 133 additions and 16 deletions

View File

@ -8,7 +8,7 @@
namespace SqMod { namespace SqMod {
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Allows management of the connection handle. * Allows management and interaction with a connection handle.
*/ */
class Connection class Connection
{ {

View File

@ -1,6 +1,11 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "Connection.hpp" #include "Connection.hpp"
#include "Statement.hpp" #include "Statement.hpp"
#include "ResultSet.hpp"
#include "Transaction.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstring>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
@ -43,6 +48,24 @@ CSStr Connection::ToString() const
return _SC(""); return _SC("");
} }
// ------------------------------------------------------------------------------------------------
SQInteger Connection::GetErrNo() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return static_cast< SQInteger >(mysql_errno(m_Handle));
}
// ------------------------------------------------------------------------------------------------
CSStr Connection::GetErrStr() const
{
// Validate the managed handle
m_Handle.Validate();
// Return the requested information
return mysql_error(m_Handle);
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Connection::SetName(CSStr name) void Connection::SetName(CSStr name)
{ {
@ -88,24 +111,50 @@ void Connection::SetAutoCommit(bool toggle)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
SQInteger Connection::Execute(CSStr query) Object Connection::Execute(CSStr query)
{ {
// Validate the managed handle // Validate the managed handle
m_Handle.Validate(); m_Handle.Validate();
// Perform the requested operation // Perform the requested operation
return m_Handle->Execute(query); return MakeULongObj(m_Handle->Execute(query));
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
SQInteger Connection::Insert(CSStr /*query*/) Object Connection::Insert(CSStr query)
{ {
return 0; // Validate the managed handle
m_Handle.Validate();
// Make sure the specified query is valid
if (!query || *query == '\0')
{
STHROWF("Invalid or empty MySQL query");
}
// Attempt to execute the specified query
else if (mysql_real_query(m_Handle, query, std::strlen(query)) != 0)
{
THROW_CURRENT(m_Handle, "Unable to execute query");
}
// Return the identifier of the inserted row
return MakeULongObj(mysql_insert_id(m_Handle));
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
SQInteger Connection::Query(CSStr /*query*/) ResultSet Connection::Query(CSStr query)
{ {
return 0; // Validate the managed handle
m_Handle.Validate();
// Make sure the specified query is valid
if (!query || *query == '\0')
{
STHROWF("Invalid or empty MySQL query");
}
// Attempt to execute the specified query
else if (mysql_real_query(m_Handle, query, std::strlen(query)) != 0)
{
THROW_CURRENT(m_Handle, "Unable to execute query");
}
// Return the identifier of the inserted row
return ResultSet(ResHnd(m_Handle));
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -117,4 +166,10 @@ Statement Connection::GetStatement(CSStr query)
return Statement(m_Handle, query); return Statement(m_Handle, query);
} }
// ------------------------------------------------------------------------------------------------
Transaction Connection::GetTransaction()
{
return Transaction();
}
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -8,7 +8,7 @@
namespace SqMod { namespace SqMod {
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* ... * Allows management and interaction with a connection handle.
*/ */
class Connection class Connection
{ {
@ -50,7 +50,10 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Destructor. * Destructor.
*/ */
~Connection() = default; ~Connection()
{
// Let the handle deal with closing the connection if necessary
}
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Copy assignment operator. * Copy assignment operator.
@ -133,6 +136,16 @@ public:
m_Handle->Disconnect(); m_Handle->Disconnect();
} }
/* --------------------------------------------------------------------------------------------
* Retrieve the current error number.
*/
SQInteger GetErrNo() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the current error message.
*/
CSStr GetErrStr() const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve the last received error number. * Retrieve the last received error number.
*/ */
@ -338,22 +351,27 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Execute a query on the server. * Execute a query on the server.
*/ */
SQInteger Execute(CSStr query); Object Execute(CSStr query);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Execute a query on the server. * Execute a query on the server.
*/ */
SQInteger Insert(CSStr query); Object Insert(CSStr query);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Execute a query on the server. * Execute a query on the server.
*/ */
SQInteger Query(CSStr query); ResultSet Query(CSStr query);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* * Create a new statement on the managed connection.
*/ */
Statement GetStatement(CSStr query); Statement GetStatement(CSStr query);
/* --------------------------------------------------------------------------------------------
* Create a new transaction on the managed connection.
*/
Transaction GetTransaction();
}; };
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -219,6 +219,8 @@ void RegisterAPI(HSQUIRRELVM vm)
.Func(_SC("_tostring"), &Connection::ToString) .Func(_SC("_tostring"), &Connection::ToString)
// Properties // Properties
.Prop(_SC("Connected"), &Connection::Connected) .Prop(_SC("Connected"), &Connection::Connected)
.Prop(_SC("ErrNo"), &Connection::GetErrNo)
.Prop(_SC("ErrStr"), &Connection::GetErrStr)
.Prop(_SC("LastErrNo"), &Connection::GetLastErrNo) .Prop(_SC("LastErrNo"), &Connection::GetLastErrNo)
.Prop(_SC("LastErrStr"), &Connection::GetLastErrStr) .Prop(_SC("LastErrStr"), &Connection::GetLastErrStr)
.Prop(_SC("Port"), &Connection::GetPortNum) .Prop(_SC("Port"), &Connection::GetPortNum)
@ -242,6 +244,8 @@ void RegisterAPI(HSQUIRRELVM vm)
.Func(_SC("Execute"), &Connection::Execute) .Func(_SC("Execute"), &Connection::Execute)
.Func(_SC("Insert"), &Connection::Insert) .Func(_SC("Insert"), &Connection::Insert)
.Func(_SC("Query"), &Connection::Query) .Func(_SC("Query"), &Connection::Query)
.Func(_SC("Statement"), &Connection::GetStatement)
.Func(_SC("Transaction"), &Connection::GetTransaction)
); );
} }

View File

@ -8,7 +8,7 @@
namespace SqMod { namespace SqMod {
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Allows management and interaction with a result set. * Allows management and interaction with a result set handle.
*/ */
class ResultSet class ResultSet
{ {

View File

@ -8,7 +8,7 @@
namespace SqMod { namespace SqMod {
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* ... * Allows management and interaction with a statement handle.
*/ */
class Statement class Statement
{ {

View File

@ -8,9 +8,49 @@
namespace SqMod { namespace SqMod {
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* * ...
*/ */
class Transaction
{
public:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Transaction()
{
}
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
Transaction(const Transaction & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor. (disabled)
*/
Transaction(Transaction && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Transaction()
{
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
Transaction & operator = (const Transaction & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
Transaction & operator = (Transaction && o) = default;
};
} // Namespace:: SqMod } // Namespace:: SqMod