Roxen.git / server / modules / tags / sqltag.pike

version» Context lines:

Roxen.git/server/modules/tags/sqltag.pike:1:   /* -  * $Id: sqltag.pike,v 1.8 1997/09/23 11:05:05 grubba Exp $ +  * $Id: sqltag.pike,v 1.9 1997/09/28 17:25:16 grubba Exp $    *    * A module for Roxen Challenger, which gives the tags    * <SQLQUERY> and <SQLOUTPUT>.    *    * Henrik Grubbström 1997-01-12    */    - constant cvs_version="$Id: sqltag.pike,v 1.8 1997/09/23 11:05:05 grubba Exp $"; + constant cvs_version="$Id: sqltag.pike,v 1.9 1997/09/28 17:25:16 grubba Exp $";   constant thread_safe=1;   #include <module.h>      inherit "module";   inherit "roxenlib";      import Array;   import Sql;      /*    * Module interface functions    */      array register_module()   {    return( ({ MODULE_PARSER,    "SQL-module",    "This module gives the three tags &lt;SQLQUERY&gt;, " -  "&lt;SQLOUTPUT&gt; and &lt;SQLTABLE&gt;.<br>\n" +  "&lt;SQLOUTPUT&gt;, &lt;SQLELSE&gt; and &lt;SQLTABLE&gt;.<br>\n"    "Usage:<ul>\n"    "<table border=0>\n"    "<tr><td valign=top><b>&lt;sqloutput&gt;</b></td>"    "<td>Executes an SQL-query, and "    "replaces #-quoted fieldnames with the results. # is "    "quoted as ##. The content inbetween &lt;sqloutput&gt; and "    "&lt;/sqloutput&gt; is repeated once for every row in the "    "result.</td></tr>\n"    "<tr><td valign=top><b>&lt;sqlquery&gt;</b></td>\n"    "<td>Executes an SQL-query, but "
Roxen.git/server/modules/tags/sqltag.pike:101:    user = args->user;    }    if (args->password) {    password = args->password;    }    host = lower_case(host);    host = (host == "localhost")?"":host;       if (error = catch(con = sql(host, database, user, password))) {    contents = "<h1>Couldn't connect to SQL-server</h1><br>\n" + -  ((master()->describe_backtrace(error)/"\n")*"<br>\n") + -  contents; +  ((master()->describe_backtrace(error)/"\n")*"<br>\n");    } else if (error = catch(result = con->query(args->query))) {    contents = "<h1>Query \"" + args->query + "\" failed: " +    con->error() + "</h1>\n" + -  ((master()->describe_backtrace(error)/"\n")*"<br>\n") + -  contents; +  ((master()->describe_backtrace(error)/"\n")*"<br>\n");    } else if (result) {    string nullvalue="";    array(string) content_array = contents/"#";    array(string) res_array=allocate(sizeof(content_array)*sizeof(result));    int j = 0;       if (args->nullvalue) {    nullvalue = (string)args->nullvalue;    }   
Roxen.git/server/modules/tags/sqltag.pike:138:    res_array[j] = "#";    } else {    res_array[j] = "<!-- Missing field " + content_array[i] + " -->";    }    } else {    res_array[j] = content_array[i];    }    }    }    contents = res_array * ""; +  request_id->misc->sqlelse = 0; +  } else { +  request_id->misc->sqlelse = 1;    }    } else {    contents = "<!-- No query! -->" + contents;    }    return(contents);   }      string sqlquery_tag(string tag_name, mapping args,    object request_id, mapping defines)   {    if (args->query) {    string host = query("hostname");    string database = query("database");    string user = query("user");    string password = query("password");    object(sql) con;    mixed error; -  +  array(mapping(string:mixed)) res;       if (args->host) {    host = args->host;    user = "";    password = "";    }    if (args->database) {    database = args->database;    user = "";    password = "";
Roxen.git/server/modules/tags/sqltag.pike:178:    }    if (args->password) {    password = args->password;    }    host = lower_case(host);    host = (host == "localhost")?"":host;       if (error = catch(con = sql(host, database, user, password))) {    return("<h1>Couldn't connect to SQL-server</h1><br>\n" +    ((master()->describe_backtrace(error)/"\n")*"<br>\n")); -  } else if (error = catch(con->query(args->query))) { +  } else if (error = catch(res = con->query(args->query))) {    return("<h1>Query \"" + args->query + "\" failed: " +    con->error() + "</h1>\n" +    ((master()->describe_backtrace(error)/"\n")*"<br>\n"));    } -  +  request_id->misc->sqlelse = !res;    } else {    return("<!-- No query! -->");    }    return("");   }      string sqltable_tag(string tag_name, mapping args,    object request_id, mapping defines)   {    if (args->query) {
Roxen.git/server/modules/tags/sqltag.pike:256:    res += " "+attr;    }    }    res += "><tr>";    foreach(map(result->fetch_fields(), lambda (mapping m) {    return(m->name);    } ), string name) {    res += "<th>"+name+"</th>";    }    res += "</tr>\n"; +     while (row = result->fetch_row()) {    res += "<tr>";    foreach(row, mixed value) {    value = (string)value;    res += "<td>"+(value==""?nullvalue:value)+"</td>";    }    res += "</tr>\n";    }    res += "</table>";    -  +  request_id->misc->sqlelse = 0;    return(res);    } else { -  +  request_id->misc->sqlelse = 1;    return("<!-- No result from query -->");    }    } else {    return("<!-- No query! -->");    }   }    -  + string sqlelse_tag(string tag_name, mapping args, string contents, +  object request_id, mapping defines) + { +  if (request_id->misc->sqlelse) { +  return(contents); +  } +  return(""); + } +    string dumpid_tag(string tag_name, mapping args,    object request_id, mapping defines)   {    return(sprintf("<pre>ID:%O\n</pre>\n",    mkmapping(indices(request_id), values(request_id))));   }      /*    * Hook in the tags    */      mapping query_tag_callers()   {    return( ([ "sql":sqlquery_tag, "sqlquery":sqlquery_tag,    "sqltable":sqltable_tag, "dumpid":dumpid_tag ]) );   }      mapping query_container_callers()   { -  return( ([ "sqloutput":sqloutput_tag ]) ); +  return( ([ "sqloutput":sqloutput_tag, "sqlelse":sqlelse_tag ]) );   }      /*    * Setting the defaults    */      void create()   {    defvar("hostname", "localhost", "Default SQL-database host",    TYPE_STRING, "Specifies the default host to use for SQL-queries.\n"