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

Direct SQLite string escape.

This commit is contained in:
Sandu Liviu Catalin 2021-03-21 19:58:39 +02:00
parent 954b1f5c0d
commit 0b62694491
2 changed files with 53 additions and 0 deletions

View File

@ -7,6 +7,8 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#ifdef SQMOD_POCO_HAS_SQLITE #ifdef SQMOD_POCO_HAS_SQLITE
#include <Poco/Data/SQLite/Connector.h> #include <Poco/Data/SQLite/Connector.h>
// Used for string escape functionality
#include <sqlite3.h>
#endif #endif
#ifdef SQMOD_POCO_HAS_MYSQL #ifdef SQMOD_POCO_HAS_MYSQL
#include <Poco/Data/MySQL/Connector.h> #include <Poco/Data/MySQL/Connector.h>
@ -47,6 +49,51 @@ void InitializePocoDataConnectors()
#endif #endif
} }
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_POCO_HAS_SQLITE
// ------------------------------------------------------------------------------------------------
static LightObj SQLiteEscapeString(StackStrF & str)
{
// Is there even a string to escape?
if (str.mLen <= 0)
{
return LightObj("", 0); // Default to empty string
}
// Allocate a memory buffer
Buffer b(static_cast< Buffer::SzType >(str.mLen * 2));
// Attempt to escape the specified string
sqlite3_snprintf(b.Capacity(), b.Get< char >(), "%q", str.mPtr);
// Return the resulted string
return LightObj(b.Get< SQChar >(), -1);
}
// ------------------------------------------------------------------------------------------------
static LightObj SQLiteEscapeStringEx(SQChar spec, StackStrF & str)
{
// Utility that allows changing the format specifier temporarily
SQChar fs[3]{'%', 'q', '\0'};
// Validate the specified format specifier
if ((spec != 'q') && (spec != 'Q') && (spec != 'w') && (spec != 's'))
{
STHROWF("Unknown format specifier: '%c'", spec);
}
// Is there even a string to escape?
else if (!str.mLen)
{
return LightObj("", 0); // Default to empty string
}
// Apply the format specifier
fs[1] = spec;
// Allocate a memory buffer
Buffer b(static_cast< Buffer::SzType >(str.mLen * 2));
// Attempt to escape the specified string
sqlite3_snprintf(b.Capacity(), b.Get< char >(), fs, str.mPtr);
// Return the resulted string
return LightObj(b.Get< SQChar >(), -1);
}
#endif
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqDataSession::SetProperty(const LightObj & value, StackStrF & name) void SqDataSession::SetProperty(const LightObj & value, StackStrF & name)
{ {
@ -684,6 +731,11 @@ void Register_POCO_Data(HSQUIRRELVM vm, Table &)
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
ns.Func(_SC("Process"), ProcessPocoData); ns.Func(_SC("Process"), ProcessPocoData);
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
#ifdef SQMOD_POCO_HAS_SQLITE
ns.Func(_SC("SQLiteEscapeString"), SQLiteEscapeString);
ns.Func(_SC("SQLiteEscapeStringEx"), SQLiteEscapeStringEx);
#endif
// --------------------------------------------------------------------------------------------
Register_POCO_Data_Binding< SQInteger, SqIntegerBinding >(vm, ns, _SC("IntBind")); Register_POCO_Data_Binding< SQInteger, SqIntegerBinding >(vm, ns, _SC("IntBind"));
Register_POCO_Data_Binding< String, SqStringBinding >(vm, ns, _SC("StrBind")); Register_POCO_Data_Binding< String, SqStringBinding >(vm, ns, _SC("StrBind"));
Register_POCO_Data_Binding< SQFloat, SqFloatBinding >(vm, ns, _SC("FloatBind")); Register_POCO_Data_Binding< SQFloat, SqFloatBinding >(vm, ns, _SC("FloatBind"));

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/Utils/Vector.hpp" #include "Library/Utils/Vector.hpp"