postgres.pike
Go to the documentation of this file.
1 inherit "handle";
2 
3 #include <macros.h>
4 
5 string describe() { return "PostgresHandle"; }
6 
7 array list_tables() {
8  string a, b;
9  array result = oHandle->list_tables() || ({ });
10  array tables = ({ });
11  foreach( result, string tname ) {
12  sscanf(tname, "%s_key%s", a, b);
13  if ( search(tname, "sql_") != 0 &&
14  search(tname, "_pkey") == -1 &&
15  search(tname, "_key") == -1 &&
16  search(tname, "_i_") == -1 )
17  tables += ({ tname });
18  }
19  return tables;
20 }
21 
22 string quote_index(mixed index) {
23  if ( !stringp(index) )
24  return index;
25  return oHandle->quote(string_to_utf8(lower_case(utf8_to_string(index))));
26 }
27 
28 string create_insert_statement(array sStatements)
29 {
30  string s = "";
31  foreach(sStatements, string statement) {
32  s+= "insert into ob_data values " + statement + ";";
33  }
34  return s;
35 }
36 
37 void check_tables() { }
38 
39 void create_table(string name, array types)
40 {
41  string query = "create table " + name + " (";
42  for ( int i = 0; i < sizeof(types) - 1; i++ ) {
43  mapping type = types[i];
44  query += type->name + " " + type->type + ",";
45  }
46  query += types[sizeof(types)-1]->name + " " + types[sizeof(types)-1]->type;
47 }
48 
49 mapping check_updates(object dbupdates, function update_classtableobject)
50 {
51  return ([ ]);
52 }
53 
54 string escape_blob(string data)
55 {
56  return replace(data, ({ "\0", "\\", "'" }),
57  ({ "\\\\000", "\\\\134", "\\\\047" }));
58 }
59 
60 string unescape_blob(string data)
61 {
62  return replace(data, ({ "\\\\000", "\\\\134", "\\\\047" }),
63  ({ "\0", "\\", "'" }));
64 }
65 
66 string get_database() { return "postgres"; }
67 
68 void create_tables()
69 {
70  MESSAGE("creating table \"doc_data\" ");
71  oHandle->big_query("create table doc_data ("+
72  " rec_data bytea, "+
73  " doc_id int, "+
74  " rec_order int, "+
75  " primary key (doc_id, rec_order)"+
76  ") ");
77  MESSAGE("creating table \"ob_class\" ");
78  oHandle->big_query("create table ob_class ("+
79  " ob_id int primary key, "+
80  " ob_class text, "+
81  " obkeywords text, "+
82  " obname text, "+
83  " obdescription text, "+
84  " obmimetype text, " +
85  " obversionof text "+
86  ")");
87 
88  oHandle->query("create index _i_obdescription on ob_class (obdescription)");
89  oHandle->query("create index _i_obkeywords on ob_class (obkeywords)");
90  oHandle->query("create index _i_obname on ob_class (obname)");
91  oHandle->query("create index _i_obmimetype on ob_class (obmimetype)");
92  oHandle->query("create index _i_obversionof on ob_class (obversionof)");
93 
94  MESSAGE("creating table \"ob_data\" ");
95  oHandle->big_query("create table ob_data ("+
96  " ob_id int, "+
97  " ob_ident text,"+
98  " ob_attr text, "+
99  " ob_data text,"+
100  " unique(ob_id, ob_ident, ob_attr)"+
101  ")");
102 
103  oHandle->query("create index _i_obdata on ob_data (ob_ident, ob_attr, ob_data)");
104 
105  MESSAGE("creating table \"variables\" ");
106  oHandle->big_query("create table variables ("+
107  " var text primary key, "+
108  " value int"+
109  ")");
110 }
111