1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Implemented format based query execution for SQLite database connection.

This commit is contained in:
Sandu Liviu Catalin 2015-11-09 08:05:36 +02:00
parent 00da988cc2
commit dfc5701799
3 changed files with 143 additions and 0 deletions

View File

@ -1,6 +1,10 @@
#include "Library/SQLite/Connection.hpp"
#include "Library/SQLite/Statement.hpp"
// ------------------------------------------------------------------------------------------------
#include <sqstdstring.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
namespace SQLite {
@ -827,5 +831,131 @@ SQInt32 Connection::GetInfo(int operation, bool highwater, bool reset) const
return SQMOD_UNKNOWN;
}
// ------------------------------------------------------------------------------------------------
SQInteger Connection::ExecF(HSQUIRRELVM vm)
{
const SQInteger top = sq_gettop(vm);
// Are there any arguments on the stack?
if (top <= 1)
{
LogErr("Attempting to <execute database query> without specifying a value");
// Push a null value on the stack
sq_pushnull(vm);
}
// Attempt to retrieve the connection instance
Var< Connection & > inst(vm, 1);
// Validate the connection instance
if (!inst.value)
{
LogErr("Attempting to <execute database query> using an invalid connection: null");
// Push a null value on the stack
sq_pushnull(vm);
}
// Is there a single string or at least something that can convert to a string on the stack?
else if (top == 2 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
// Variable where the resulted string will be retrieved
const SQChar * str = 0;
// Attempt to retrieve the specified message from the stack
if (SQ_FAILED(sq_getstring(vm, -1, &str)))
{
LogErr("Unable to <fetch database query> because : the string cannot be retrieved from the stack");
// Push a null value on the stack
sq_pushnull(vm);
}
else
{
// Attempt to execute the specified query string and push the result on the stack
sq_pushinteger(vm, inst.value.Exec(str));
}
// Pop any pushed values pushed to the stack
sq_settop(vm, top);
}
else if (top > 2)
{
// Variables containing the resulted string
SQChar * str = NULL;
SQInteger len = 0;
// Attempt to call the format function with the passed arguments
if (SQ_FAILED(sqstd_format(vm, 2, &len, &str)))
{
LogErr("Unable to <generate database query> because : %s", Error::Message(vm).c_str());
// Push a null value on the stack
sq_pushnull(vm);
}
else
{
// Attempt to execute the resulted query string and push the result on the stack
sq_pushinteger(vm, inst.value.Exec(str));
}
}
else
{
LogErr("Unable to <extract database query> from the specified value");
// Push a null value on the stack
sq_pushnull(vm);
}
// At this point everything went correctly (probably)
return 1;
}
// ------------------------------------------------------------------------------------------------
SQInteger Connection::QueueF(HSQUIRRELVM vm)
{
const SQInteger top = sq_gettop(vm);
// Are there any arguments on the stack?
if (top <= 1)
{
LogErr("Attempting to <queue database query> without specifying a value");
}
// Attempt to retrieve the connection instance
Var< Connection & > inst(vm, 1);
// Validate the connection instance
if (!inst.value)
{
LogErr("Attempting to <queue database query> using an invalid connection: null");
}
// Is there a single string or at least something that can convert to a string on the stack?
else if (top == 2 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
// Variable where the resulted string will be retrieved
const SQChar * str = 0;
// Attempt to retrieve the specified message from the stack
if (SQ_FAILED(sq_getstring(vm, -1, &str)))
{
LogErr("Unable to <fetch database query> because : the string cannot be retrieved from the stack");
}
else
{
// Attempt to queue the specified query string
inst.value.Queue(str);
}
// Pop any pushed values pushed to the stack
sq_settop(vm, top);
}
else if (top > 2)
{
// Variables containing the resulted string
SQChar * str = NULL;
SQInteger len = 0;
// Attempt to call the format function with the passed arguments
if (SQ_FAILED(sqstd_format(vm, 2, &len, &str)))
{
LogErr("Unable to <generate database query> because : %s", Error::Message(vm).c_str());
}
else
{
// Attempt to queue the specified query string
inst.value.Queue(str);
}
}
else
{
LogErr("Unable to <extract database query> from the specified value");
}
// At this point everything went correctly (probably)
return 0;
}
} // Namespace:: SQLite
} // Namespace:: SqMod

View File

@ -381,6 +381,16 @@ public:
*/
Connection CopyToDatabase(const SQChar * path);
/* --------------------------------------------------------------------------------------------
* Attempt to execute the specified query.
*/
static SQInteger ExecF(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Attempt to queue the specified query.
*/
static SQInteger QueueF(HSQUIRRELVM vm);
protected:
/* --------------------------------------------------------------------------------------------

View File

@ -229,6 +229,9 @@ bool Register_SQLite(HSQUIRRELVM vm)
.Func(_SC("release_memory"), &Connection::ReleaseMemory)
.Func(_SC("copy_to_memory"), &Connection::CopyToMemory)
.Func(_SC("copy_to_database"), &Connection::CopyToDatabase)
/* Raw Functions */
.SquirrelFunc(_SC("execf"), &Connection::ExecF)
.SquirrelFunc(_SC("queuef"), &Connection::QueueF)
);
// Output debugging information
LogDbg("Registration of <SQLite Connection> type was successful");