From 6458a7f8791cbd2ead80fa0c27178fa1c09eb85d Mon Sep 17 00:00:00 2001 From: Francesco Chemolli <li@kinkie.it> Date: Sat, 29 Apr 2000 02:11:09 +0200 Subject: [PATCH] Added support for (emulated) parametric queries. Rev: lib/modules/Sql.pmod/msql.pike:1.6 Rev: lib/modules/Sql.pmod/mysql.pike:1.8 Rev: lib/modules/Sql.pmod/odbc.pike:1.4 Rev: lib/modules/Sql.pmod/postgres.pike:1.8 Rev: lib/modules/Sql.pmod/rsql.pike:1.2 Rev: lib/modules/Sql.pmod/sql_util.pmod:1.3 Rev: lib/modules/Sql.pmod/sybase.pike:1.2 --- lib/modules/Sql.pmod/msql.pike | 8 ++++++++ lib/modules/Sql.pmod/mysql.pike | 11 +++++++++-- lib/modules/Sql.pmod/odbc.pike | 10 +++++++++- lib/modules/Sql.pmod/postgres.pike | 10 +++++++++- lib/modules/Sql.pmod/rsql.pike | 5 ++++- lib/modules/Sql.pmod/sql_util.pmod | 27 +++++++++++++++++++++++++-- lib/modules/Sql.pmod/sybase.pike | 8 +++++++- 7 files changed, 71 insertions(+), 8 deletions(-) diff --git a/lib/modules/Sql.pmod/msql.pike b/lib/modules/Sql.pmod/msql.pike index 86c3fa5575..03dfd4fad5 100644 --- a/lib/modules/Sql.pmod/msql.pike +++ b/lib/modules/Sql.pmod/msql.pike @@ -12,6 +12,14 @@ mapping(string:mapping(string:mixed)) list_fields(string table, string|void wild return mkmapping(a,Array.map(a,lambda(string s, mapping m) {return m[s];},result)); } + +array(mapping(string:mixed)) query(string q, + mapping(string|int:mixed)|void bindings) { + if (!bindings) + return ::query(q); + return ::query(.sql_util.emulate_bindings(q,bindings)); +} + #else /* !constant(Msql.msql) */ #error "mSQL support not available.\n" #endif /* constant(Msql.msql) */ diff --git a/lib/modules/Sql.pmod/mysql.pike b/lib/modules/Sql.pmod/mysql.pike index 68cb19cdf6..b0a885402b 100644 --- a/lib/modules/Sql.pmod/mysql.pike +++ b/lib/modules/Sql.pmod/mysql.pike @@ -1,12 +1,12 @@ /* - * $Id: mysql.pike,v 1.7 1999/06/14 23:08:37 grubba Exp $ + * $Id: mysql.pike,v 1.8 2000/04/29 00:10:21 kinkie Exp $ * * Glue for the Mysql-module */ //. //. File: mysql.pike -//. RCSID: $Id: mysql.pike,v 1.7 1999/06/14 23:08:37 grubba Exp $ +//. RCSID: $Id: mysql.pike,v 1.8 2000/04/29 00:10:21 kinkie Exp $ //. Author: Henrik Grubbstr�m (grubba@idonex.se) //. //. Synopsis: Implements the glue to the Mysql-module. @@ -128,6 +128,13 @@ int decode_datetime (string timestr) } } +int|object big_query(string q, mapping(string|int:mixed)|void bindings) +{ + if (!bindings) + return ::big_query(q); + return ::big_query(.sql_util.emulate_bindings(q,bindings)); +} + #else /* !constant(Mysql.mysql) */ #error "Mysql support not available.\n" #endif /* constant(Mysql.mysql) */ diff --git a/lib/modules/Sql.pmod/odbc.pike b/lib/modules/Sql.pmod/odbc.pike index 90666d905a..21608605a2 100644 --- a/lib/modules/Sql.pmod/odbc.pike +++ b/lib/modules/Sql.pmod/odbc.pike @@ -1,11 +1,19 @@ /* - * $Id: odbc.pike,v 1.3 1999/06/14 23:08:38 grubba Exp $ + * $Id: odbc.pike,v 1.4 2000/04/29 00:10:32 kinkie Exp $ * * Glue for the ODBC-module */ #if constant(Odbc.odbc) inherit Odbc.odbc; + +int|object big_query(object|string q, mapping(string|int:mixed)|void bindings) +{ + if (!bindings) + return ::big_query(q); + return ::big_query(.sql_util.emulate_bindings(q,bindings)); +} + #else /* !constant(Odbc.odbc) */ #error "ODBC support not available.\n" #endif /* constant(Odbc.odbc) */ diff --git a/lib/modules/Sql.pmod/postgres.pike b/lib/modules/Sql.pmod/postgres.pike index a99f8376d4..eb24ee535f 100644 --- a/lib/modules/Sql.pmod/postgres.pike +++ b/lib/modules/Sql.pmod/postgres.pike @@ -2,7 +2,7 @@ * This is part of the Postgres module for Pike. * (C) 1997 Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> * - * $Id: postgres.pike,v 1.7 1999/06/22 20:10:20 grubba Exp $ + * $Id: postgres.pike,v 1.8 2000/04/29 00:10:38 kinkie Exp $ * */ @@ -124,6 +124,14 @@ mapping(string:array(mixed)) list_fields (string table, void|string wild) { } return ret; } + +int|object big_query(object|string q, mapping(string|int:mixed)|void bindings) +{ + if (!bindings) + return ::big_query(q); + return ::big_query(.sql_util.emulate_bindings(q,bindings)); +} + #else /* !constant(Postgres.postgres) */ #error "Postgres support not available.\n" #endif /* constant(Postgres.postgres) */ diff --git a/lib/modules/Sql.pmod/rsql.pike b/lib/modules/Sql.pmod/rsql.pike index 799c5308f9..72a9a8ec42 100644 --- a/lib/modules/Sql.pmod/rsql.pike +++ b/lib/modules/Sql.pmod/rsql.pike @@ -154,8 +154,11 @@ string quote(string s) return do_request('q'); } -object big_query(string q) +int|object big_query(object|string q, mapping(string|int:mixed)|void bindings) { + if(bindings) + q=.sql_util.emulate_bindings(q,bindings); + mixed qid = do_request('Q', q); return qid && class { diff --git a/lib/modules/Sql.pmod/sql_util.pmod b/lib/modules/Sql.pmod/sql_util.pmod index 48b607b0c1..826a6a5fdc 100644 --- a/lib/modules/Sql.pmod/sql_util.pmod +++ b/lib/modules/Sql.pmod/sql_util.pmod @@ -1,5 +1,5 @@ /* - * $Id: sql_util.pmod,v 1.2 1999/07/01 20:25:38 grubba Exp $ + * $Id: sql_util.pmod,v 1.3 2000/04/29 00:10:59 kinkie Exp $ * * Some SQL utility functions. * They are kept here to avoid circular references. @@ -9,7 +9,7 @@ //. //. File: sql_util.pmod -//. RCSID: $Id: sql_util.pmod,v 1.2 1999/07/01 20:25:38 grubba Exp $ +//. RCSID: $Id: sql_util.pmod,v 1.3 2000/04/29 00:10:59 kinkie Exp $ //. Author: Henrik Grubbstr�m (grubba@idonex.se) //. //. Synopsis: Some SQL utility functions @@ -33,3 +33,26 @@ void fallback() { throw(({ "Function not supported in this database.", backtrace() })); } + +//. - 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). +//. The raw query can contain some variables (identified by prefixing +//. a colon to a name or a number(i.e. :var, :2). They will be +//. replaced by the corresponding value in the mapping. +//. > query +//. The query +//. > bindings +//. Optional mapping containing the variable bindings. Make sure that +//. no confusion is possible in the query. If necessary, change the +//. variables' names +string emulate_bindings(string query, mapping(string|int:mixed)|void bindings) +{ + array(string)k, v; + if (!bindings) + return query; + v=Array.map(values(bindings), + lambda(mixed m) {return (stringp(m)?m:(string)m);}); + k=Array.map(indices(bindings),lambda(string s){return ":"+s;}); + return replace(query,k,v); +} diff --git a/lib/modules/Sql.pmod/sybase.pike b/lib/modules/Sql.pmod/sybase.pike index 10f3403368..d3897fe59d 100644 --- a/lib/modules/Sql.pmod/sybase.pike +++ b/lib/modules/Sql.pmod/sybase.pike @@ -3,7 +3,7 @@ * By Francesco Chemolli <kinkie@roxen.com> 10/12/1999 * (C) Roxen IS * - * $Id: sybase.pike,v 1.1 2000/03/26 20:49:06 kinkie Exp $ + * $Id: sybase.pike,v 1.2 2000/04/29 00:11:09 kinkie Exp $ * */ @@ -97,6 +97,12 @@ void create(void|string host, void|string db, void|string user, } } +int|object big_query(string q, mapping(string|int:mixed)|void bindings) { + if (!bindings) + return ::big_query(q); + return ::big_query(.sql_util.emulate_bindings(q,bindings)); +} + #else #error "Sybase driver not available.\n" #endif -- GitLab