File: sqldb.pike

package info (click to toggle)
roxen 1.2beta2-2
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 16,948 kB
  • ctags: 8,589
  • sloc: ansic: 89,632; asm: 8,431; sh: 2,915; makefile: 1,787; cpp: 377
file content (93 lines) | stat: -rw-r--r-- 2,220 bytes parent folder | download | duplicates (4)
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
/*
 * $Id: sqldb.pike,v 1.1 1997/11/26 22:11:37 grubba Exp $
 *
 * Associates a name with an SQL-database.
 *
 * $Author: grubba $
 */

#include <module.h>

inherit "module";

array register_module()
{
  return (({ 0, "SQL Databases",
	     "Associates names with SQL Database URLs.", ({}), 1 }));
}

void create()
{
  defvar("table", "", "Table", TYPE_TEXT_FIELD,
	 "Associates names with SQL Database URLs.<br>\n"
	 "Format:<br>\n"
	 "<pre>Name1\tSQLURL1\n"
	 "Name2\tSQLURL2</pre><br>\n"
	 "An \"SQL-URL\" is specified on the following format:<br><ul>\n"
	 "<pre>[<i>sqlserver</i>://][[<i>user</i>][:<i>password</i>]@]"
	 "[<i>host</i>[:<i>port</i>]]/<i>database</i></pre><br>\n"
	 "</ul>Valid values for \"sqlserver\" depend on which "
	 "sql-servers your pike has support for, but the following "
	 "might exist: <tt>msql</tt>, <tt>mysql</tt>, <tt>odbc</tt>, "
	 "<tt>oracle</tt>, <tt>postgres</tt>.<br>\n");
}

mapping(string:string) parse_table(string tab)
{
  mapping(string:string) res = ([]);

  tab = replace(tab||"", "\r", "\n");

  foreach(tab/"\n", string line) {
    string line2 = replace(line, "\t", " ");
    array(string) arr = (line2/" ") - ({ "" });
    if ((sizeof(arr) >= 2) && (arr[0][0] != '#')) {
      string name = arr[0];
      string infix = arr[1];
      string suffix = ((line/name)[1..])*name;
      suffix = infix + ((suffix/infix)[1..])*infix;
      res[name] = suffix;
    }
  }
  return(res);
}

void start(int level, object conf)
{
  conf->sql_urls = parse_table(QUERY(table));
}

string status()
{
  mapping sql_urls = parse_table(QUERY(table));

  string res = "";

  if (sizeof(sql_urls)) {
    res += "<table border=0>\n";
    foreach(sort(indices(sql_urls)), string s) {
      object o;

      catch {
	o = Sql.sql(sql_urls[s]);
      };

      if (o) {
	res += sprintf("<tr><td>Connection OK</td>"
		       "<td><tt>%s</tt></td><td><tt>%s</tt></td></tr>\n",
		       s, sql_urls[s]);
      } else {
	res += sprintf("<tr><td><font color=red>Connection Failed</font></td>"
		       "<td><tt>%s</tt></td><td><tt>%s</tt></td></tr>\n",
		       s, sql_urls[s]);
      }
    }
    res += "</table>\n";
  } else {
    res += "No associations defined.<br>\n";
  }
  return(res);
}