diff --git a/modules/mg/Connection.hpp b/modules/mg/Connection.hpp index a1ead137..f7a4dffa 100644 --- a/modules/mg/Connection.hpp +++ b/modules/mg/Connection.hpp @@ -8,7 +8,7 @@ namespace SqMod { /* ------------------------------------------------------------------------------------------------ - * Allows management of the connection handle. + * Allows management and interaction with a connection handle. */ class Connection { diff --git a/modules/mysql/Connection.cpp b/modules/mysql/Connection.cpp index 5cc9c687..2cbbf4fa 100644 --- a/modules/mysql/Connection.cpp +++ b/modules/mysql/Connection.cpp @@ -1,6 +1,11 @@ // ------------------------------------------------------------------------------------------------ #include "Connection.hpp" #include "Statement.hpp" +#include "ResultSet.hpp" +#include "Transaction.hpp" + +// ------------------------------------------------------------------------------------------------ +#include // ------------------------------------------------------------------------------------------------ namespace SqMod { @@ -43,6 +48,24 @@ CSStr Connection::ToString() const 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) { @@ -88,24 +111,50 @@ void Connection::SetAutoCommit(bool toggle) } // ------------------------------------------------------------------------------------------------ -SQInteger Connection::Execute(CSStr query) +Object Connection::Execute(CSStr query) { // Validate the managed handle m_Handle.Validate(); // 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); } +// ------------------------------------------------------------------------------------------------ +Transaction Connection::GetTransaction() +{ + return Transaction(); +} + } // Namespace:: SqMod diff --git a/modules/mysql/Connection.hpp b/modules/mysql/Connection.hpp index 5f99d554..b461f9b4 100644 --- a/modules/mysql/Connection.hpp +++ b/modules/mysql/Connection.hpp @@ -8,7 +8,7 @@ namespace SqMod { /* ------------------------------------------------------------------------------------------------ - * ... + * Allows management and interaction with a connection handle. */ class Connection { @@ -50,7 +50,10 @@ public: /* -------------------------------------------------------------------------------------------- * Destructor. */ - ~Connection() = default; + ~Connection() + { + // Let the handle deal with closing the connection if necessary + } /* -------------------------------------------------------------------------------------------- * Copy assignment operator. @@ -133,6 +136,16 @@ public: 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. */ @@ -338,22 +351,27 @@ public: /* -------------------------------------------------------------------------------------------- * Execute a query on the server. */ - SQInteger Execute(CSStr query); + Object Execute(CSStr query); /* -------------------------------------------------------------------------------------------- * Execute a query on the server. */ - SQInteger Insert(CSStr query); + Object Insert(CSStr query); /* -------------------------------------------------------------------------------------------- * 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); + + /* -------------------------------------------------------------------------------------------- + * Create a new transaction on the managed connection. + */ + Transaction GetTransaction(); }; } // Namespace:: SqMod diff --git a/modules/mysql/Module.cpp b/modules/mysql/Module.cpp index 356c1eea..2fefcd98 100644 --- a/modules/mysql/Module.cpp +++ b/modules/mysql/Module.cpp @@ -219,6 +219,8 @@ void RegisterAPI(HSQUIRRELVM vm) .Func(_SC("_tostring"), &Connection::ToString) // Properties .Prop(_SC("Connected"), &Connection::Connected) + .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) @@ -242,6 +244,8 @@ void RegisterAPI(HSQUIRRELVM vm) .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) ); } diff --git a/modules/mysql/ResultSet.hpp b/modules/mysql/ResultSet.hpp index 3d697216..a3458840 100644 --- a/modules/mysql/ResultSet.hpp +++ b/modules/mysql/ResultSet.hpp @@ -8,7 +8,7 @@ namespace SqMod { /* ------------------------------------------------------------------------------------------------ - * Allows management and interaction with a result set. + * Allows management and interaction with a result set handle. */ class ResultSet { diff --git a/modules/mysql/Statement.hpp b/modules/mysql/Statement.hpp index 4096265e..ddad3e10 100644 --- a/modules/mysql/Statement.hpp +++ b/modules/mysql/Statement.hpp @@ -8,7 +8,7 @@ namespace SqMod { /* ------------------------------------------------------------------------------------------------ - * ... + * Allows management and interaction with a statement handle. */ class Statement { diff --git a/modules/mysql/Transaction.hpp b/modules/mysql/Transaction.hpp index 75c553f5..c970c636 100644 --- a/modules/mysql/Transaction.hpp +++ b/modules/mysql/Transaction.hpp @@ -8,9 +8,49 @@ 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