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
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
// This is a roxen module. Copyright © 1997-2000, Roxen IS. 
// 
// A module for Roxen, which gives the tags 
// <sqltable>, <sqlquery> and <sqloutput>. 
// 
// Henrik Grubbström 1997-01-12 
 
constant cvs_version="$Id: sqltag.pike,v 1.75 2001/03/07 13:39:05 kuntri Exp $"; 
constant thread_safe=1; 
#include <module.h> 
#include <config.h> 
 
inherit "module"; 
 
//<locale-token project="mod_sqltag">LOCALE</locale-token> 
//<locale-token project="mod_sqltag">SLOCALE</locale-token> 
#define SLOCALE(X,Y)  _STR_LOCALE("mod_sqltag",X,Y) 
#define LOCALE(X,Y) _DEF_LOCALE("mod_sqltag",X,Y) 
// end locale stuff 
 
// Module interface functions 
 
constant module_type=MODULE_TAG|MODULE_PROVIDER; 
LocaleString module_name=LOCALE(1,"SQL tags"); 
LocaleString module_doc = 
LOCALE(2, 
       "The SQL tags module provides the tags <tt>&lt;sqlquery&gt;</tt> and" 
       "<tt>&lt;sqltable&gt;</tt> as well as being a source to the " 
       "<tt>&lt;emit&gt;</tt> tag (<tt>&lt;emit source=\"sql\" ... &gt;</tt>)." 
       "All tags send queries to SQL databases."); 
 
TAGDOCUMENTATION 
#ifdef manual 
constant tagdoc=([ 
"sqltable":#" 
<desc tag='tag'><p><short> 
 Creates an HTML or ASCII table from the results of an SQL query. 
</short></p> 
</desc> 
 
<attr name='ascii'><p> 
 Create an ASCII table rather than a HTML table. Useful for 
 interacting with <xref href='../graphics/diagram.tag' /> and <xref 
 href='../text/tablify.tag' />.</p> 
</attr> 
 
<attr name='host' value='database'><p> 
 Which database to connect to, usually a symbolic name set in the <xref 
 href='../../administrator/installing/databases.xml'><module>SQL 
 Databases</module></xref> module. If omitted the default database will 
 be used.</p> 
</attr> 
 
<attr name='query' value='SQL statement'><p> 
 The actual SQL-statement.</p> 
</attr> 
 
<attr name='parse'><p> 
 If specified, the query will be parsed by the RXML parser. 
 Useful if you wish to dynamically build the query.</p> 
</attr>", 
 
"sqlquery":#" 
<desc tag='tag'><p><short> 
 Executes an SQL query, but doesn't do anything with the 
 result.</short> This is mostly used for SQL queries that change the 
 contents of the database, for example INSERT or UPDATE.</p> 
</desc> 
 
<attr name='host' value='database'><p> 
 Which database to connect to, usually a symbolic name set in the <xref 
 href='../../administrator/installing/databases.xml'><module>SQL 
 Databases</module></xref> module. If omitted the default 
 database will be used.</p> 
</attr> 
 
<attr name='query' value='SQL statement'><p> 
 The actual SQL-statement.</p> 
</attr> 
 
<attr name='parse'><p> 
 If specified, the query will be parsed by the RXML parser. Useful if 
 you wish to dynamically build the query.</p> 
</attr> 
 
<attr name='mysql-insert-id' value='form-variable'><p> 
 Set form-variable to the insert id used by Mysql for 
 auto-incrementing columns. Note: This is only available with Mysql.</p> 
</attr>", 
 
"emit#sql":#"<desc plugin='plugin'><p><short> 
 
 Use this source to connect to and query SQL databases for 
 information.</short> The result will be available in variables named 
 as the SQL columns.</p> 
</desc> 
 
<attr name='host' value='database'><p> 
 Which database to connect to, usually a symbolic name set in the <xref 
 href='../../administrator/installing/databases.xml'><module>SQL 
 Databases</module></xref> module. If omitted the default 
 database will be used.</p> 
</attr> 
 
<attr name='query' value='SQL statement'><p> 
 The actual SQL-statement.</p> 
</attr>" 
]); 
#endif 
 
#if ROXEN_COMPAT <= 1.3 
string compat_default_host; 
#endif 
string default_db; 
 
array|object do_sql_query(mapping args, RequestID id, void|int big_query) 
{ 
  string host = compat_default_host; 
  if (args->host) 
  { 
    host=args->host; 
    args->host="SECRET"; 
  } 
 
  if (!args->query) 
    RXML.parse_error("No query.\n"); 
 
  if (args->parse) 
    args->query = Roxen.parse_rxml(args->query, id); 
 
  Sql.Sql con; 
  array(mapping(string:mixed))|object result; 
  mixed error; 
   
#if ROXEN_COMPAT <= 1.3 
  if( args->host || (!args->db && (query("db") == " none")) ) 
  { 
    function sql_connect = id->conf->sql_connect; 
    error = catch(con = sql_connect(host)); 
 
    if (error) 
      RXML.run_error(LOCALE(3,"Couldn't connect to SQL server")+ 
                     ": "+error[0]+"\n"); 
 
    // Got a connection now. Any errors below this point ought to be 
    // syntax errors and should be reported with parse_error. 
  } 
  else 
#endif 
  { 
    error = catch(con = DBManager.get( args->db || default_db, 
                                       my_configuration() )); 
    if (error) 
      RXML.run_error(LOCALE(3,"Couldn't connect to SQL server")+ 
                     ": "+error[0]+"\n"); 
  } 
  if (error = catch(result = (big_query?con->big_query(args->query): 
                              con->query(args->query)))) 
  { 
    error = con->error(); 
    if (error) error = ": " + error; 
    error = sprintf("Query failed%s\n", error||"."); 
    RXML.parse_error(error); 
  } 
 
  args["dbobj"]=con; 
  if(result && args->rowinfo) { 
    int rows; 
    if(arrayp(result)) rows=sizeof(result); 
    if(objectp(result)) rows=result->num_rows(); 
    RXML.user_set_var(args->rowinfo, rows); 
  } 
 
  return result; 
} 
 
 
// -------------------------------- Tag handlers ------------------------------------ 
 
#if ROXEN_COMPAT <= 1.3 
class TagSQLOutput { 
  inherit RXML.Tag; 
  constant name = "sqloutput"; 
  RXML.Type content_type = RXML.t_same; 
  array(RXML.Type) result_types = ({ RXML.t_any(RXML.PXml) }); 
 
  class Frame { 
    inherit RXML.Frame; 
    inherit "roxenlib"; 
 
    array do_return(RequestID id) { 
      NOCACHE(); 
 
      array res=do_sql_query(args, id); 
 
      if (res && sizeof(res)) { 
        result = do_output_tag(args, res, content, id); 
        id->misc->defines[" _ok"] = 1; // The effect of <true>, since res isn't parsed. 
 
        return 0; 
      } 
 
      if (args["do-once"]) { 
        result = do_output_tag( args, ({([])}), content, id ); 
        id->misc->defines[" _ok"] = 1; 
        return 0; 
      } 
 
      id->misc->defines[" _ok"] = 0; 
      return 0; 
    } 
  } 
} 
#endif 
 
#ifdef SQL_EMIT_FOR_DATABASES_WITHOUT_NULL_ENTRIES 
class TagSqlplugin { 
  inherit RXML.Tag; 
  constant name = "emit"; 
  constant plugin_name = "sql"; 
 
  array get_dataset(mapping m, RequestID id) { 
    return do_sql_query(m, id); 
  } 
} 
#else 
class TagSqlplugin { 
  inherit RXML.Tag; 
  constant name = "emit"; 
  constant plugin_name = "sql"; 
 
  array get_dataset(mapping m, RequestID id) { 
    array(mapping(string:string|int)) res=do_sql_query(m, id); 
 
    foreach(res, mapping(string:string|int) row) 
      foreach(indices(row), string col) 
        if(!row[col]) row[col]=RXML.Void; 
 
    return res; 
  } 
} 
#endif 
 
class TagSQLQuery { 
  inherit RXML.Tag; 
  constant name = "sqlquery"; 
  constant flags = RXML.FLAG_EMPTY_ELEMENT; 
 
  class Frame { 
    inherit RXML.Frame; 
 
    array do_return(RequestID id) { 
      NOCACHE(); 
 
      array res=do_sql_query(args, id); 
 
      if(args["mysql-insert-id"]) 
        if(args->dbobj && args->dbobj->master_sql) 
          RXML.user_set_var(args["mysql-insert-id"], args->dbobj->master_sql->insert_id()); 
        else 
          RXML.parse_error("No insert_id present.\n"); 
 
      id->misc->defines[" _ok"] = 1; 
      return 0; 
    } 
  } 
} 
 
class TagSQLTable { 
  inherit RXML.Tag; 
  constant name = "sqltable"; 
  constant flags = RXML.FLAG_EMPTY_ELEMENT; 
 
  class Frame { 
    inherit RXML.Frame; 
 
    array do_return(RequestID id) { 
      NOCACHE(); 
 
      object res=do_sql_query(args, id, 1); 
 
      int ascii=!!args->ascii; 
      string ret=""; 
 
      if (res) { 
        string nullvalue=args->nullvalue||""; 
 
        if (!ascii) { 
          ret="<tr>"; 
          foreach(map(res->fetch_fields(), lambda (mapping m) { 
                                             return m->name; 
                                           } ), string name) 
            ret += "<th>"+name+"</th>"; 
          ret += "</tr>\n"; 
        } 
 
        array row; 
        while(arrayp(row=res->fetch_row())) { 
          if (ascii) 
            ret += ((array(string))row) * "\t" + "\n"; 
          else { 
            ret += "<tr>"; 
            foreach(row, mixed value) 
              ret += "<td>"+(value==""?nullvalue:value)+"</td>"; 
            ret += "</tr>\n"; 
          } 
        } 
 
        if (!ascii) 
          ret=Roxen.make_container("table", 
                                   args-(["host":"","database":"","user":"", 
                                          "password":"","query":"", 
                                          "nullvalue":"", "dbobj":""]), ret); 
 
        id->misc->defines[" _ok"] = 1; 
        result=ret; 
        return 0; 
      } 
 
      id->misc->defines[" _ok"] = 0; 
      return 0; 
    } 
  } 
} 
 
 
// ------------------------ Setting the defaults ------------------------- 
 
class DatabaseVar 
{ 
  inherit Variable.StringChoice; 
  array get_choice_list( ) 
  { 
    return ({ " none" }) 
           + sort(DBManager.list( my_configuration() )); 
  } 
} 
 
void create() 
{ 
#if ROXEN_COMPAT <= 1.3 
  defvar("hostname", "mysql://localhost/", 
         LOCALE(4,"Default database"), 
         TYPE_STRING | VAR_INVISIBLE, 
         LOCALE(5,"The default database that will be used if no <i>host</i> " 
         "attribute is given to the tags. " 
         "The value is a database URL in this format:\n" 
         "<p><blockquote><pre>" 
         "<i>driver</i><b>://</b>" 
         "[<i>username</i>[<b>:</b><i>password</i>]<b>@</b>]" 
         "<i>host</i>[<b>:</b><i>port</i>]" 
         "[<b>/</b><i>database</i>]\n" 
         "</pre></blockquote>\n" 
         "<p>If the <i>SQL databases</i> module is loaded, it's also " 
         "possible to use an alias registered there. That's the " 
         "recommended way, since this (usually sensitive) data is " 
         "collected in one place then.")); 
#endif 
  defvar( "db", 
          DatabaseVar( " none",({}),0, 
                       LOCALE(4,"Default database"), 
                       LOCALE(8,"If this is defined, it's the " 
                              "database this server will use as the " 
                              "default database") ) ); 
} 
 
 
// --------------------- More interface functions -------------------------- 
 
void start(int level, Configuration _conf) 
{ 
#if ROXEN_COMPAT <= 1.3 
  compat_default_host = query("hostname"); 
#endif 
  default_db          = query("db"); 
} 
 
string status() 
{ 
  if( query("db") != " none" ) 
  { 
    if(mixed err = catch { 
      object o = DBManager.get(query("db"),my_configuration()); 
      if(!o) 
        error("The database specified as default database does not exist"); 
      return sprintf(LOCALE(6,"The default database is connected to %s " 
                            "server on %s.")+ 
                     "<br />\n", 
                     Roxen.html_encode_string (o->server_info()), 
                     Roxen.html_encode_string (o->host_info())); 
    }) 
    { 
      return 
        "<font color=\"red\">"+ 
        LOCALE(7,"The default database is not connected")+ 
        ":</font><br />\n" + 
        replace( Roxen.html_encode_string( describe_error(err) ), 
                 "\n", "<br />\n") + 
        "<br />\n"; 
    } 
  } 
  return ""; 
}