8785481999-07-01Henrik Grubbström (Grubba) /*
a20af62000-09-26Fredrik Hübinette (Hubbe)  * $Id: sql_util.pmod,v 1.5 2000/09/26 19:00:12 hubbe Exp $
8785481999-07-01Henrik Grubbström (Grubba)  * * Some SQL utility functions. * They are kept here to avoid circular references. * * Henrik Grubbström 1999-07-01 */
a20af62000-09-26Fredrik Hübinette (Hubbe) #pike __VERSION__
8785481999-07-01Henrik Grubbström (Grubba) //. //. File: sql_util.pmod
a20af62000-09-26Fredrik Hübinette (Hubbe) //. RCSID: $Id: sql_util.pmod,v 1.5 2000/09/26 19:00:12 hubbe Exp $
8785481999-07-01Henrik Grubbström (Grubba) //. Author: Henrik Grubbström (grubba@idonex.se) //. //. Synopsis: Some SQL utility functions //. //. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //. //. These functions are kept here mainly to avoid circular references. //. //. - quote //. Quote a string so that it can safely be put in a query. //. > s - String to quote. string quote(string s) { return(replace(s, "\'", "\'\'")); } //. - fallback //. Throw an error in case an unimplemented function is called.
485cb11999-07-01Henrik Grubbström (Grubba) void fallback()
8785481999-07-01Henrik Grubbström (Grubba) {
485cb11999-07-01Henrik Grubbström (Grubba)  throw(({ "Function not supported in this database.", backtrace() }));
8785481999-07-01Henrik Grubbström (Grubba) }
6458a72000-04-29Francesco Chemolli  //. - 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
4903d52000-04-29Francesco Chemolli string emulate_bindings(string query, mapping(string|int:mixed)|void bindings, void|object driver)
6458a72000-04-29Francesco Chemolli { array(string)k, v;
4903d52000-04-29Francesco Chemolli  function my_quote=(driver&&driver->quote?driver->quote:quote);
6458a72000-04-29Francesco Chemolli  if (!bindings) return query; v=Array.map(values(bindings),
4903d52000-04-29Francesco Chemolli  lambda(mixed m) {return my_quote(stringp(m)?m:(string)m);});
6458a72000-04-29Francesco Chemolli  k=Array.map(indices(bindings),lambda(string s){return ":"+s;}); return replace(query,k,v); }