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
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
#pike __REAL_VERSION__ 
 
//! The SQL module is a unified interface between pike and all 
//! its supported databases. The parts of this module that is 
//! usuable for all normal uses is the @[Sql] class and the 
//! @[sql_result] class. 
//! 
//! @example 
//! string people_in_group(string group) { 
//!    Sql.Sql db = Sql.Sql("mysql://localhost/testdb"); 
//!    return db->query("SELECT name FROM users WHERE " 
//!                     "group=%s", group)->name * ","; 
//! } 
 
//! @ignore 
// Use getters and Val-> to ensure dynamic resolving in case the 
// values in Val.pmod are replaced. 
program `->Null() {return Val->Null;} 
Val.Null `->NULL() {return Val->null;} 
//! @endignore 
 
//! @class Null 
//!   Class used to implement the SQL NULL value. 
//! 
//! @deprecated Val.Null 
//! 
//! @seealso 
//!   @[Val.Null], @[Val.null] 
 
//! @endclass 
 
//! @decl Val.Null NULL; 
//! 
//!   The SQL NULL value. 
//! 
//! @deprecated Val.null 
//! 
//! @seealso 
//!   @[Val.null] 
 
//! Redact the password (if any) from an Sql-url. 
//! 
//! @param sql_url 
//!   Sql-url possibly containing an unredacted password. 
//! 
//! @returns 
//!   Returns the same Sql-url but with the password (if any) 
//!   replaced by the string @expr{"CENSORED"@}. 
string censor_sql_url(string sql_url) 
{ 
  array(string) a = sql_url/"://"; 
  string prot = a[0]; 
  string host = a[1..] * "://"; 
  a = host/"@"; 
  if (sizeof(a) > 1) { 
    host = a[-1]; 
    a = (a[..<1] * "@")/":"; 
    string user = a[0]; 
    if (sizeof(a) > 1) { 
      sql_url = prot + "://" + user + ":CENSORED@" + host; 
    } 
  } 
  return sql_url; 
} 
 
//! Base class for a connection to an SQL database. 
class Connection { inherit __builtin.Sql.Connection; } 
 
//! Base class for the result from @[Connection.big_query()] et al. 
class Result { inherit __builtin.Sql.Result; } 
 
//! The result from @[Promise]. 
class FutureResult { inherit __builtin.Sql.FutureResult; } 
 
//! The result from @[Connection.promise_query()]. 
class Promise { inherit __builtin.Sql.Promise; } 
 
protected program(Connection) find_dbm(string program_name) 
{ 
  program(Connection) p; 
  // we look in Sql.type and Sql.Provider.type.type for a valid sql class. 
  p = global.Sql[program_name]; 
  if(!p && global.Sql["Provider"] && global.Sql["Provider"][program_name]) 
    p = global.Sql["Provider"][program_name][program_name]; 
  return p; 
} 
 
//! @decl Connection Sql(string host) 
//! @decl Connection Sql(string host, string db) 
//! @decl Connection Sql(string host, mapping(string:int|string) options) 
//! @decl Connection Sql(string host, string db, string user) 
//! @decl Connection Sql(string host, string db, string user, string password) 
//! @decl Connection Sql(string host, string db, string user, @ 
//!                      string password, mapping(string:int|string) options) 
//! 
//! Create a new generic SQL connection. 
//! 
//! @param host 
//!   @mixed 
//!     @type string 
//!       Connect to the server specified. The string should be on the 
//!       format: 
//!       @tt{dbtype://[user[:password]@@]hostname[:port][/database]@} 
//!       Use the @i{dbtype@} protocol to connect to the database 
//!       server on the specified host. If the hostname is @expr{""@} 
//!       then the port can be a file name to access through a 
//!       UNIX-domain socket or similar, e g 
//!       @expr{"mysql://root@@:/tmp/mysql.sock/"@}. 
//! 
//!       There is a special dbtype @expr{"mysqls"@} which works like 
//!       @expr{"mysql"@} but sets the @tt{CLIENT_SSL@} option and 
//!       loads the @tt{/etc/my.cnf@} config file to find the SSL 
//!       parameters. The same function can be achieved using the 
//!       @expr{"mysql"@} dbtype. 
//!     @type int(0..0) 
//!       Access through a UNIX-domain socket or similar. 
//!   @endmixed 
//! 
//! @param db 
//!   Select this database. 
//! 
//! @param user 
//!   User name to access the database as. 
//! 
//! @param password 
//!   Password to access the database. 
//! 
//! @param options 
//!   Optional mapping of options. 
//!   See the SQL-database documentation for the supported options. 
//!   (eg @[Mysql.mysql()->create()]). 
//! 
//! @note 
//!   In versions of Pike prior to 7.2 it was possible to leave out the 
//!   dbtype, but that has been deprecated, since it never worked well. 
//! 
//! @note 
//!  Exactly which databases are supported by pike depends on the 
//!  installed set of client libraries when pike was compiled. 
//! 
//! The possible ones are 
//!   @dl 
//!     @item mysql 
//!       libmysql based mysql connection 
//!     @item mysqls 
//!       libmysql based mysql connection, using SSL 
//!     @item dsn 
//!       @[ODBC] based connection 
//!     @item msql 
//!       @[Msql] 
//!     @item odbc 
//!       @[ODBC] based connection 
//!     @item oracle 
//!       @[Oracle] using oracle libraries 
//!     @item pgsql 
//!       PostgreSQL direct network access. 
//!       This module is independent of any external libraries. 
//!     @item postgres 
//!       PostgreSQL libray access. Uses the @[Postgres] module. 
//!     @item rsql 
//!       Remote SQL api, requires a rsql server running on another host. 
//!       This is an API that uses sockets to communicate with a remote pike 
//!       running pike -x rsqld on another host. 
//!     @item sqlite 
//!       In-process SQLite database, uses the @[SQLite] module 
//!     @item sybase 
//!       Uses the @[sybase] module to access sybase 
//!     @item tds 
//!       Sybase and Microsoft SQL direct network access using the TDS protocol. 
//!       This module is independent of any external libraries. 
//!   @enddl 
//! 
//! @note 
//!   Support for @[options] was added in Pike 7.3. 
//! 
//! @note 
//!   Use of an object @[host] was deprecated in Pike 8.1. 
//! 
//! @note 
//!   Prior to Pike 8.1 this was a wrapper class. 
//! 
//! @seealso 
//!   @[8.0::Sql.Sql], @[Connection] 
Connection Sql(string host, 
               void|string|mapping(string:int|string) db, 
               void|string user, void|string _password, 
               void|mapping(string:int|string) options) 
{ 
  // Note: No need to censor host here, since it is rewritten below if 
  //       it contains an SQL-URL. 
  string password = _password; 
  _password = "CENSORED"; 
 
  Connection con; 
 
  if (mappingp(db)) { 
    options = db; 
    db = 0; 
  } 
  if (db == "") { 
    db = 0; 
  } 
  if (user == "") { 
    user = 0; 
  } 
  if (password == "") { 
    password = 0; 
  } 
 
  string program_name; 
 
  if (host && (host != replace(host, ([ ":":"", "/":"", "@":"" ]) ))) { 
 
    // The hostname is on the format: 
    // 
    // dbtype://[user[:password]@]hostname[:port][/database] 
 
    array(string) arr = host/"://"; 
    if ((sizeof(arr) > 1) && (arr[0] != "")) { 
      if (sizeof(arr[0]/".pike") > 1) { 
        program_name = (arr[0]/".pike")[0]; 
      } else { 
        program_name = arr[0]; 
      } 
      host = arr[1..] * "://"; 
    } 
    arr = host/"@"; 
    if (sizeof(arr) > 1) { 
      // User and/or password specified 
      host = arr[-1]; 
      arr = (arr[..<1]*"@")/":"; 
      if (!user && sizeof(arr[0])) { 
        user = arr[0]; 
      } 
      if (!password && (sizeof(arr) > 1)) { 
        password = arr[1..]*":"; 
        if (password == "") { 
          password = 0; 
        } 
      } 
    } 
    arr = host/"/"; 
    if (sizeof(arr) > 1) { 
      host = arr[..<1]*"/"; 
      if (!db) { 
        db = arr[-1]; 
      } 
    } 
  } 
 
  if (host == "") { 
    host = 0; 
  } 
 
  if (!program_name) { 
    error("No protocol specified.\n"); 
  } 
  // Don't call ourselves... 
  if ((sizeof(program_name / "_result") != 1) || 
      ((< "Sql", "sql", "sql_util", "module" >)[program_name]) ) { 
    error("Unsupported protocol %O.\n", program_name); 
  } 
 
  program p; 
 
  if (!(p = find_dbm(program_name))) { 
    error("Failed to index module Sql.%s or Sql.Provider.%s.\n", 
          program_name, program_name); 
  } 
 
  if (options) { 
    con = p(host||"", db||"", user||"", password||"", options); 
  } else if (password) { 
    con = p(host||"", db||"", user||"", password); 
  } else if (user) { 
    con = p(host||"", db||"", user); 
  } else if (db) { 
    con = p(host||"", db); 
  } else if (host) { 
    con = p(host); 
  } else { 
    con = p(); 
  } 
  if (!con->query && !con->big_query) { 
    con = UNDEFINED; 
    error("Failed to index module Sql.%s or Sql.Provider.%s.\n", 
          program_name, program_name); 
  } 
 
  return con; 
} 
 
#pragma no_deprecation_warnings 
 
//! @decl Connection Sql(__deprecated__(Connection) con) 
//! @decl Connection Sql(__deprecated__(Connection) con, string db) 
//! 
//! Create a new generic SQL connection (DEPRECATED). 
//! 
//! @param con 
//!   Use this connection to access the SQL-database. 
//! 
//! @param db 
//!   Select this database. 
//! 
//! @note 
//!   In Pike 8.1 and later this function is essentially a noop; 
//!   if you actually need it, you may want to use @[8.0::Sql.Sql]. 
//! 
//! @returns 
//!   Returns @[con]. 
//! 
//! @seealso 
//!   @[8.0::Sql.Sql], @[Connection] 
variant Connection Sql(__deprecated__(Connection) con, void|string db) 
{ 
  if (db && db != "") { 
    con->select_db(db); 
  } 
 
  return con; 
} 
 
#pragma deprecation_warnings 
 
 
//! @class mysql_result 
//! @deprecated Result 
 
//! @endclass 
 
__deprecated__(program(Result)) mysql_result = 
  (__deprecated__(program(Result)))Result; 
 
//! @class mysqls_result 
//! @deprecated Result 
 
//! @endclass 
 
__deprecated__(program(Result)) mysqls_result = 
  (__deprecated__(program(Result)))Result;