From 1dac281ba3bca7f464fd5fd364fc4fd7a362ee6a Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Mon, 29 Aug 2016 15:34:20 +0300 Subject: [PATCH] Improve the string escape functions from the SQLite module by allowing formatted string input. --- modules/sqlite/Common.cpp | 73 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/modules/sqlite/Common.cpp b/modules/sqlite/Common.cpp index 0cdef247..2ed4a708 100644 --- a/modules/sqlite/Common.cpp +++ b/modules/sqlite/Common.cpp @@ -114,6 +114,27 @@ CSStr EscapeString(CSStr str) return GetTempBuff(); } +// ------------------------------------------------------------------------------------------------ +static SQInteger SqEscapeString(HSQUIRRELVM vm) +{ + // Was the string specified? + if (sq_gettop(vm) <= 1) + { + return sq_throwerror(vm, "Missing un-escaped string"); + } + // Attempt to generate the string value + StackStrF val(vm, 2); + // Have we failed to retrieve the string? + if (SQ_FAILED(val.mRes)) + { + return val.mRes; // Propagate the error! + } + // Attempt to escape the obtained string and push it on the stack + sq_pushstring(vm, EscapeString(val.mPtr), -1); + // Specify that we have a return value + return 1; +} + // ------------------------------------------------------------------------------------------------ CCStr EscapeStringEx(SQChar spec, CCStr str) { @@ -139,6 +160,48 @@ CCStr EscapeStringEx(SQChar spec, CCStr str) return GetTempBuff(); } +// ------------------------------------------------------------------------------------------------ +static SQInteger SqEscapeStringEx(HSQUIRRELVM vm) +{ + const Int32 top = sq_gettop(vm); + // Was the escape specifier specified? + if (top <= 1) + { + return sq_throwerror(vm, "Missing escape specifier"); + } + // Was the string specified? + else if (top <= 2) + { + return sq_throwerror(vm, "Missing un-escaped string"); + } + SQInteger spec = 'q'; + // Attempt to extract the escape specifier + const SQRESULT res = sq_getinteger(vm, 2, &spec); + // Have we failed to retrieve the specifier? + if (SQ_FAILED(res)) + { + return res; // Propagate the error! + } + // Attempt to generate the string value + StackStrF val(vm, 3); + // Have we failed to retrieve the string? + if (SQ_FAILED(val.mRes)) + { + return val.mRes; // Propagate the error! + } + // Attempt to escape the obtained string and push it on the stack + try + { + sq_pushstring(vm, EscapeStringEx(static_cast< SQChar >(spec), val.mPtr), -1); + } + catch (const Sqrat::Exception & e) + { + return sq_throwerror(vm, e.what()); + } + // Specify that we have a return value + return 1; +} + // ------------------------------------------------------------------------------------------------ CCStr ArrayToQueryColumns(Array & arr) { @@ -230,12 +293,12 @@ void Register_Common(Table & sqlns) .Func(_SC("SetSoftHeapLimit"), &SetSoftHeapLimit) .Func(_SC("ReleaseMemory"), &ReleaseMemory) .Func(_SC("MemoryUsage"), &GetMemoryUsage) - .Func(_SC("EscapeString"), &EscapeString) - .Func(_SC("EscapeStringEx"), &EscapeStringEx) - .Func(_SC("Escape"), &EscapeString) - .Func(_SC("EscapeEx"), &EscapeStringEx) .Func(_SC("ArrayToQueryColumns"), &ArrayToQueryColumns) - .Func(_SC("TableToQueryColumns"), &TableToQueryColumns); + .Func(_SC("TableToQueryColumns"), &TableToQueryColumns) + .SquirrelFunc(_SC("EscapeString"), &SqEscapeString) + .SquirrelFunc(_SC("EscapeStringEx"), &SqEscapeStringEx) + .SquirrelFunc(_SC("Escape"), &SqEscapeString) + .SquirrelFunc(_SC("EscapeEx"), &SqEscapeStringEx); } } // Namespace:: SqMod