handle._pike
Go to the documentation of this file.
1 /*0*/
2 #include <macros.h>
3 #include <database.h>
4 class handle {
5 public:
6 
7 Sql.Sql oHandle;
8 private string db_connect;
9 
10 void keep() {
11  Sql.sql_result res =
12  oHandle->big_query("select ob_class from ob_class "+
13  "where ob_id = 13");
14  res->fetch_row();
15 }
16 
17 void create(string connect) {
18  db_connect = connect;
19  oHandle = Sql.Sql(db_connect);
20 }
21 
22 int|object big_query(object|string q, mixed ... extraargs) {
23  Sql.sql_result res;
24 
25  string query = q;
26  if ( arrayp(extraargs) && sizeof(extraargs)>0)
27  query = sprintf(q, @extraargs);
28 
29  mixed err = catch { res=oHandle->big_query(query); };
30  if (err)
31  {
32  FATAL("Database Error ("+(string)oHandle->error()+")\n"+
33  master()->describe_backtrace(err));
34  throw(err);
35  }
36  return res;
37 }
38 
39 array list_tables() {
40  return oHandle->list_tables() || ({ });
41 }
42 
43 array(mapping(string:mixed)) query(object|string q, mixed ... extraargs) {
44  array(mapping(string:mixed)) res;
45  string query = q;
46  if ( arrayp(extraargs) && sizeof(extraargs)>0)
47  query = sprintf(q, @extraargs);
48 
49  mixed err = catch ( res=oHandle->query(query) );
50  if (err) {
51  FATAL(" SQL Problem: SQL=" + query + "\n"+
52  " Database Error("+(string)oHandle->error()+")");
53  destruct(oHandle);
54  oHandle = Sql.Sql(db_connect);
55  res = oHandle->query(q, @extraargs);
56  }
57  return res;
58 }
59 
60 mapping get_indices(string tname) {
61  mixed result = ([ ]);
62  mixed res = oHandle->query("show index from ob_data");
63  werror("get_indices() = %O\n", res);
64  return result;
65 }
66 
67 
68 void check_tables() { }
69 
70 void create_tables() { }
71 
72 string create_insert_statement(array sStatements)
73 {
74  string s = sStatements * ",";
75  return "insert into ob_data values " + s;
76 }
77 
78 void create_table(string name, array types)
79 {
80  string query = "create table " + name + " (";
81  for ( int i = 0; i < sizeof(types) - 1; i++ ) {
82  mapping type = types[i];
83  query += type->name + " " + type->type + ",";
84  }
85  query += types[sizeof(types)-1]->name + " " + types[sizeof(types)-1]->type;
86 
87  array unique = ({ });
88  foreach( types, mapping type ) {
89  if ( type->unique )
90  unique += ({ type->name });
91  }
92  if ( sizeof(unique) > 0 ) {
93  query += ", UNIQUE(" + (unique*",")+")";
94  }
95  query += ")";
96 }
97 
98 string escape_blob(string data) {
99  return oHandle->quote(data);
100 }
101 
102 string unescape_blob(string data) {
103  return data;
104 }
105 
106 string quote_index(string index) {
107  if ( !stringp(index) )
108  return index;
109  return oHandle->quote(index);
110 }
111 
112 int enable_decorations() { return 0; }
113 int get_object_class() { return 0; }
114 object get_object() { return _Database; }
115 
116 string get_database() { return "mysql"; }
117 
118 mapping check_updates(object dbupdates, function func) { return ([ ]); }
119 
120 function `->(string fname) {
121  switch(fname) {
122  case "query": return query;
123  case "big_query" : return big_query;
124  case "keep": return keep;
125  case "list_tables": return list_tables;
126  case "create_tables": return create_tables;
127  case "check_tables": return check_tables;
128  case "create_insert_statement": return create_insert_statement;
129  case "create_table": return create_table;
130  case "check_updates": return check_updates;
131  case "unescape_blob": return unescape_blob;
132  case "escape_blob": return escape_blob;
133  case "get_database": return get_database;
134  case "quote_index": return quote_index;
135  case "get_object_class": return get_object_class;
136  case "get_object": return get_object;
137  case "enable_decorations": return enable_decorations;
138  case "this": return this;
139  case "describe": return describe;
140  default :
141  if ( !objectp(oHandle) ) {
142  return 0;
143  }
144  return oHandle[fname];
145  }
146 }
147 
148 string describe() { return "SqlHandle()"; }
149 
150 
151 
152 };