1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
/* 
 * $Id: sql_util.pmod,v 1.11 2003/04/22 18:02:53 nilsson Exp $ 
 * 
 * Some SQL utility functions. 
 * They are kept here to avoid circular references. 
 * 
 * Henrik Grubbström 1999-07-01 
 */ 
 
#pike __REAL_VERSION__ 
 
//! Some SQL utility functions 
 
//! Quote a string so that it can safely be put in a query. 
//! 
//! @param s 
//!   String to quote. 
string quote(string s) 
{ 
  return(replace(s, "\'", "\'\'")); 
} 
 
//! Throw an error in case an unimplemented function is called. 
void fallback() 
{ 
  error( "Function not supported in this database." ); 
} 
 
//! 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" or  ":2"). They will be 
//! replaced by the corresponding value in the mapping. 
//! 
//! @param query 
//!   The query. 
//! 
//! @param 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, 
                        void|object driver) 
{ 
  array(string)k, v; 
  if (!bindings) 
    return query; 
  function my_quote=(driver&&driver->quote?driver->quote:quote); 
  v=Array.map(values(bindings), 
              lambda(mixed m) { 
                if(multisetp(m)) m = indices(m)[0]; 
                return (stringp(m)? "'"+my_quote(m)+"'" : (string)m); 
              }); 
  // Throws if mapping key is empty string. 
  k=Array.map(indices(bindings),lambda(string s){ 
                                  return ( (stringp(s)&&s[0]==':') ? 
                                           s : ":"+s); 
                                }); 
  return replace(query,k,v); 
}