From d6beb9a4276f3746a8b018a42391d12dc948fe1f Mon Sep 17 00:00:00 2001 From: Martin Stjernholm <mast@lysator.liu.se> Date: Wed, 19 Sep 2012 09:23:58 +0200 Subject: [PATCH] Moved the sprintf-style formatter to the pmod module. --- lib/modules/Sql.pmod/Sql.pike | 90 ++++-------------------------- lib/modules/Sql.pmod/sql_util.pmod | 77 +++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 80 deletions(-) diff --git a/lib/modules/Sql.pmod/Sql.pike b/lib/modules/Sql.pmod/Sql.pike index f1ecb60d18..682a4eea8d 100644 --- a/lib/modules/Sql.pmod/Sql.pike +++ b/lib/modules/Sql.pmod/Sql.pike @@ -496,81 +496,11 @@ string|object compile_query(string q) return q; } -//! Wrapper to handle zero. -//! -//! @seealso -//! @[zero] -protected class ZeroWrapper -{ - //! @returns - //! Returns the following: - //! @string - //! @value "NULL" - //! If @[fmt] is @expr{'s'@}. - //! @value "ZeroWrapper()" - //! If @[fmt] is @expr{'O'@}. - //! @endstring - //! Otherwise it formats a @expr{0@} (zero). - protected string _sprintf(int fmt, mapping(string:mixed) params) - { - if (fmt == 's') return "NULL"; - if (fmt == 'O') return "ZeroWrapper()"; - return sprintf(sprintf("%%*%c", fmt), params, 0); - } -} - -//! Instance of @[ZeroWrapper] used by @[handle_extraargs()]. -protected ZeroWrapper zero = ZeroWrapper(); - -protected class NullArg -{ - protected string _sprintf (int fmt) - {return fmt == 'O' ? "Sql.Sql.NullArg()" : "NULL";} -} -protected NullArg null_arg = NullArg(); - -//! Handle @[sprintf]-based quoted arguments -//! -//! @param query -//! The query as sent to one of the query functions. -//! -//! @param extraargs -//! The arguments following the query. -//! -//! @returns -//! Returns an array with two elements: -//! @array -//! @elem string 0 -//! The query altered to use bindings-syntax. -//! @elem mapping(string|int:mixed) 1 -//! A bindings mapping. -//! @endarray array(string|mapping(string|int:mixed)) - handle_extraargs(string query, array(mixed) extraargs) { - - array(mixed) args=allocate(sizeof(extraargs)); - mapping(string|int:mixed) b = ([]); - - int a; - foreach(extraargs; int j; mixed s) { - if (stringp(s) || multisetp(s)) { - args[j]=":arg"+(a++); - b[args[j]] = s; - continue; - } - if (intp(s) || floatp(s)) { - args[j] = s || zero; - continue; - } - if (objectp (s) && s->is_val_null) { - args[j] = null_arg; - continue; - } - ERROR("Wrong type to query argument %d: %O\n", j + 1, s); - } - if(!sizeof(b)) b=0; - - return ({sprintf(query,@args), b}); + handle_extraargs(string query, array(mixed) extraargs) +// Compat wrapper. +{ + return .sql_util.handle_extraargs (query, extraargs); } //! Sends an SQL query synchronously to the underlying SQL-server and @@ -632,7 +562,7 @@ array(mapping(string:string)) query(object|string q, if (mappingp(extraargs[0])) bindings=extraargs[0]; else - [q,bindings]=handle_extraargs(q,extraargs); + [q,bindings]=.sql_util.handle_extraargs(q,extraargs); if(bindings) { if(master_sql->query) @@ -681,7 +611,7 @@ array(mapping(string:mixed)) typed_query(object|string q, mixed ... extraargs) if (mappingp(extraargs[0])) bindings=extraargs[0]; else - [q,bindings]=handle_extraargs(q,extraargs); + [q,bindings]=.sql_util.handle_extraargs(q,extraargs); if(bindings) { if(master_sql->typed_query) @@ -722,7 +652,7 @@ int|object big_query(object|string q, mixed ... extraargs) if (mappingp(extraargs[0])) bindings = extraargs[0]; else - [q, bindings] = handle_extraargs(q, extraargs); + [q, bindings] = .sql_util.handle_extraargs(q, extraargs); } object|array(mapping) pre_res; @@ -782,7 +712,7 @@ int|object big_typed_query(object|string q, mixed ... extraargs) if (mappingp(extraargs[0])) bindings = extraargs[0]; else - [q, bindings] = handle_extraargs(q, extraargs); + [q, bindings] = .sql_util.handle_extraargs(q, extraargs); } object|array(mapping) pre_res; @@ -826,7 +756,7 @@ int|object streaming_query(object|string q, mixed ... extraargs) if(mappingp(extraargs[0])) bindings = extraargs[0]; else - [q, bindings] = handle_extraargs(q, extraargs); + [q, bindings] = .sql_util.handle_extraargs(q, extraargs); } object pre_res; @@ -869,7 +799,7 @@ int|object streaming_typed_query(object|string q, mixed ... extraargs) if(mappingp(extraargs[0])) bindings = extraargs[0]; else - [q, bindings] = handle_extraargs(q, extraargs); + [q, bindings] = .sql_util.handle_extraargs(q, extraargs); } object pre_res; diff --git a/lib/modules/Sql.pmod/sql_util.pmod b/lib/modules/Sql.pmod/sql_util.pmod index 836c814cbb..ef1c50fa6a 100644 --- a/lib/modules/Sql.pmod/sql_util.pmod +++ b/lib/modules/Sql.pmod/sql_util.pmod @@ -24,6 +24,83 @@ void fallback() error( "Function not supported in this database." ); } +//! Wrapper to handle zero. +//! +//! @seealso +//! @[zero] +protected class ZeroWrapper +{ + //! @returns + //! Returns the following: + //! @string + //! @value "NULL" + //! If @[fmt] is @expr{'s'@}. + //! @value "ZeroWrapper()" + //! If @[fmt] is @expr{'O'@}. + //! @endstring + //! Otherwise it formats a @expr{0@} (zero). + protected string _sprintf(int fmt, mapping(string:mixed) params) + { + if (fmt == 's') return "NULL"; + if (fmt == 'O') return "ZeroWrapper()"; + return sprintf(sprintf("%%*%c", fmt), params, 0); + } +} + +//! Instance of @[ZeroWrapper] used by @[handle_extraargs()]. +protected ZeroWrapper zero = ZeroWrapper(); + +protected class NullArg +{ + protected string _sprintf (int fmt) + {return fmt == 'O' ? "NullArg()" : "NULL";} +} +protected NullArg null_arg = NullArg(); + +//! Handle @[sprintf]-based quoted arguments +//! +//! @param query +//! The query as sent to one of the query functions. +//! +//! @param extraargs +//! The arguments following the query. +//! +//! @returns +//! Returns an array with two elements: +//! @array +//! @elem string 0 +//! The query altered to use bindings-syntax. +//! @elem mapping(string|int:mixed) 1 +//! A bindings mapping. +//! @endarray +array(string|mapping(string|int:mixed)) + handle_extraargs(string query, array(mixed) extraargs) { + + array(mixed) args=allocate(sizeof(extraargs)); + mapping(string|int:mixed) b = ([]); + + int a; + foreach(extraargs; int j; mixed s) { + if (stringp(s) || multisetp(s)) { + args[j]=":arg"+(a++); + b[args[j]] = s; + continue; + } + if (intp(s) || floatp(s)) { + args[j] = s || zero; + continue; + } + if (objectp (s) && s->is_val_null) { + args[j] = null_arg; + continue; + } + error("Wrong type to query argument %d: %O\n", j + 1, s); + } + if(!sizeof(b)) b=0; + + return ({sprintf(query,@args), b}); +} + //! Build a raw SQL query, given the cooked query and the variable bindings //! It's meant to be used as an emulation engine for those drivers not //! providing such a behaviour directly (i.e. Oracle). -- GitLab