1 |
<? |
2 |
|
3 |
class Database { |
4 |
|
5 |
var $properties; |
6 |
var $dbh; |
7 |
var $ref_dssession; |
8 |
|
9 |
var $bool_show_errors; |
10 |
var $bool_stop_on_errors; |
11 |
var $bool_ImplicitUpdate; |
12 |
|
13 |
var $currentQuery; |
14 |
|
15 |
function Database($args) { |
16 |
if (!isset($args['connection'])) { |
17 |
$args['connection'] = 'none'; |
18 |
} |
19 |
$this->properties = $args['connection']; |
20 |
|
21 |
$this->bool_show_errors = 0; |
22 |
isset($args['show_errors']) && ($this->bool_show_errors = $args['show_errors']); |
23 |
|
24 |
$this->bool_stop_on_errors = 0; |
25 |
isset($args['stop_on_errors']) && ($this->bool_stop_on_errors = $args['stop_on_errors']); |
26 |
|
27 |
$this->bool_ImplicitUpdate = 0; |
28 |
isset($args['dynamic_updates']) && ($this->bool_ImplicitUpdate = $args['dynamic_updates']); |
29 |
|
30 |
$this->connect(); |
31 |
} |
32 |
|
33 |
function connect() { |
34 |
switch ($this->properties['type']) { |
35 |
|
36 |
case 'mysql': |
37 |
($this->dbh = @mysql_pconnect($this->properties['host'], $this->properties['user'], $this->properties['pass'])) |
38 |
|| die("Could not connect to rdbms-daemon (type=\"{$this->properties['type']}\", host=\"{$this->properties['host']}\")"); |
39 |
if (!mysql_select_db($this->properties['name'], $this->dbh)) { |
40 |
if (!$this->createDatabase()) { |
41 |
die("Could not create rdbms-database (name=\"{$this->properties['name']}\")"); |
42 |
} |
43 |
if (!mysql_select_db($this->properties['name'], $this->dbh)) { |
44 |
die("Could not open rdbms-database. (name=\"{$this->properties['name']}\") We already tried to create it, but: no way!"); |
45 |
} |
46 |
} |
47 |
break; |
48 |
|
49 |
case 'interbase': |
50 |
if (!function_exists('ibase_pconnect')) { die("There seems to be no interbase support on this server, since \"function_exists('ibase_pconnect')\" returned \"false\"."); } |
51 |
$ibase_dsn = $this->properties['host'] . ':' . $this->properties['name']; |
52 |
($this->dbh = @ibase_pconnect($ibase_dsn, $this->properties['user'], $this->properties['pass'])) |
53 |
|| die("Could not connect to rdbms-daemon (type=\"{$this->properties['type']}\", dsn=\"$ibase_dsn\")"); |
54 |
//$this->_handleError(); |
55 |
break; |
56 |
} |
57 |
} |
58 |
|
59 |
function &query($sql) { |
60 |
|
61 |
$this->currentQuery = $sql; |
62 |
|
63 |
slog("Database: &query($sql)"); |
64 |
switch ($this->properties['type']) { |
65 |
|
66 |
case 'mysql': |
67 |
if ($tmp_rs =& mysql_query($sql, $this->dbh)) { |
68 |
return $tmp_rs; |
69 |
} else { |
70 |
$this->_handleError(); |
71 |
} |
72 |
break; |
73 |
|
74 |
case 'interbase': |
75 |
if ($tmp_rs = @ibase_query($this->dbh, $sql)) { |
76 |
return $tmp_rs; |
77 |
} else { |
78 |
$this->_handleError(); |
79 |
} |
80 |
break; |
81 |
|
82 |
} |
83 |
} |
84 |
|
85 |
function fetchRow($resultset) { |
86 |
switch ($this->properties['type']) { |
87 |
case 'mysql': |
88 |
if ( (isset($resultset)) && ($tmp_row = mysql_fetch_assoc($resultset)) ) { |
89 |
return $tmp_row; |
90 |
} |
91 |
break; |
92 |
} |
93 |
} |
94 |
|
95 |
function getRowCount($resultset) { |
96 |
switch ($this->properties['type']) { |
97 |
case 'mysql': |
98 |
if ( (isset($resultset)) && ($tmp_count = mysql_num_rows($resultset)) ) { |
99 |
return $tmp_count; |
100 |
} |
101 |
break; |
102 |
} |
103 |
} |
104 |
|
105 |
function getLastInsertId() { |
106 |
switch ($this->properties['type']) { |
107 |
case 'mysql': |
108 |
if ( $tmp_id = mysql_insert_id($this->dbh) ) { |
109 |
return $tmp_id; |
110 |
} |
111 |
break; |
112 |
} |
113 |
} |
114 |
|
115 |
|
116 |
function retrieveMetaInfo(&$resultset) { |
117 |
|
118 |
$arr_meta = array(); |
119 |
|
120 |
switch ($this->properties['type']) { |
121 |
|
122 |
case 'mysql': |
123 |
if ( !isset($resultset)) { return; } |
124 |
|
125 |
// - - - - - - - - - - - - - - - |
126 |
// table-metadata |
127 |
//$arr_meta['table']['name'] = |
128 |
|
129 |
// - - - - - - - - - - - - - - - |
130 |
// field-metadata |
131 |
if ($fields = @mysql_num_fields($resultset)) { |
132 |
$arr_meta['table']['name'] = mysql_field_table($resultset, 0); |
133 |
$i = 0; |
134 |
while ($i < $fields) { |
135 |
$fieldname = mysql_field_name($resultset, $i); |
136 |
$arr_meta['fields'][$fieldname] = array( |
137 |
'type' => mysql_field_type($resultset, $i), |
138 |
'table' => mysql_field_table($resultset, $i), |
139 |
'name' => mysql_field_name($resultset, $i), |
140 |
'len' => mysql_field_len($resultset, $i), |
141 |
'flags' => mysql_field_flags($resultset, $i), |
142 |
); |
143 |
$i++; |
144 |
} |
145 |
} |
146 |
|
147 |
break; |
148 |
} |
149 |
return $arr_meta; |
150 |
|
151 |
} |
152 |
|
153 |
|
154 |
|
155 |
function getError() { |
156 |
switch ($this->properties['type']) { |
157 |
case 'mysql': |
158 |
return mysql_error($this->dbh); |
159 |
break; |
160 |
case 'interbase': |
161 |
return ibase_errmsg($this->dbh); |
162 |
break; |
163 |
} |
164 |
} |
165 |
|
166 |
|
167 |
function _handleError() { |
168 |
|
169 |
$dbErrorMessage = $this->getError(); |
170 |
$errortype = ''; |
171 |
|
172 |
switch ($this->properties['type']) { |
173 |
|
174 |
case 'mysql': |
175 |
// create tables (mysql) |
176 |
if ( preg_match("/Table '(.+)' doesn't exist/", $dbErrorMessage, $regs) ) { |
177 |
$errortype = "table_missing"; |
178 |
if ($regs[1]) { |
179 |
if ($this->bool_ImplicitUpdate) { |
180 |
return $this->_createTable($regs[1]); |
181 |
} |
182 |
} |
183 |
} |
184 |
break; |
185 |
|
186 |
case 'interbase': |
187 |
/* |
188 |
// create database (interbase) |
189 |
if ( preg_match('/I\/O error for file "(.+)" Error while trying to open file unknown Win32 error 2 /', $dbErrorMessage, $regs) ) { |
190 |
if ($regs[1]) { |
191 |
$this->createDatabase(); |
192 |
} |
193 |
} |
194 |
*/ |
195 |
// create table (interbase) |
196 |
if (preg_match('/Table unknown (.+).+$/', $dbErrorMessage, $regs)) { |
197 |
if ($tablename = $regs[1]) { |
198 |
if ($this->bool_ImplicitUpdate) { |
199 |
$this->_createTable($tablename); |
200 |
} |
201 |
} |
202 |
} |
203 |
break; |
204 |
|
205 |
} |
206 |
|
207 |
|
208 |
if ($this->bool_show_errors) { |
209 |
?> |
210 |
<br> |
211 |
<div style="width:550px;padding:5px;background:#dddddd;border:2px groove;"> |
212 |
unresolved database-error:<br> |
213 |
<b><i><?= $dbErrorMessage ?></i></b><hr> |
214 |
the query causing this error was:<br> |
215 |
<b><i><?= $this->currentQuery ?></i></b> |
216 |
</div> |
217 |
<? |
218 |
|
219 |
if ($errortype == 'table_missing') { |
220 |
?> |
221 |
<div style="width:550px;padding:10px;background:#aabbcc;border:2px groove;"> |
222 |
<b>Nähere Erläuterung / Vorschlag:</b> |
223 |
<ul> |
224 |
<li>Eine Objekt-/Datenoperation führte zum Zugriff auf eine nicht vorhandene Tabelle im RDBMS, der Prozess wurde angehalten.</li> |
225 |
<li>Bitte <a class="acontentlight" href="#">benachrichtigen</a> Sie Ihren Administrator oder unsere Entwicklungsabteilung.</li> |
226 |
<li> |
227 |
Die Tabelle "users" konnte nicht gefunden werden, Sie haben die Möglichkeit, |
228 |
diese Tabelle jetzt zu <a class="acontentlight" href="#">erstellen</a>, |
229 |
falls vom Administrator bereits Meta-Informationen vorgegeben wurden. Falls nicht, können Sie von hier aus - die entsprechenden |
230 |
Berechtigungen vorausgesetzt - in den <a class="acontentlight" href="#">Objekteditor</a> verzweigen, um die nötigen Meta-Informationen selbst zu definieren. |
231 |
</li> |
232 |
</ul> |
233 |
</div> |
234 |
<? |
235 |
} |
236 |
|
237 |
} |
238 |
|
239 |
if ($this->bool_stop_on_errors) { |
240 |
exit; |
241 |
} |
242 |
|
243 |
} |
244 |
|
245 |
function createDatabase() { |
246 |
switch ($this->properties['type']) { |
247 |
case 'mysql': |
248 |
return mysql_create_db($this->properties['name'], $this->dbh); |
249 |
break; |
250 |
case 'interbase': |
251 |
//return ibase_query($this->dbh, "CREATE DATABASE {$this->properties['name']};"); |
252 |
print "database \"{$this->properties['name']}\" does not exist, "; |
253 |
//$csql = "CREATE DATABASE {$this->properties['name']};"; |
254 |
//print "please create it by issuing a \"$csql\"."; |
255 |
print "please create it manually"; |
256 |
exit; |
257 |
break; |
258 |
} |
259 |
} |
260 |
|
261 |
function _createTable($tablename) { |
262 |
|
263 |
$tablename = strtolower($tablename); |
264 |
|
265 |
print "_createTable($tablename)<br>"; |
266 |
|
267 |
print "metadata:<br>"; |
268 |
if ($columnMetaData = $this->ref_dssession->PDL->rdbms_getColumnMetaData($tablename)) { |
269 |
while (list($column, $attribs) = each($columnMetaData)) { |
270 |
print $column . "<br>"; |
271 |
} |
272 |
} |
273 |
|
274 |
// HACK: |
275 |
// create a new PDLEngine-Object |
276 |
global $site; |
277 |
$pdl =& new PDLEngine( array('path_pdl' => $site->getAttribute('path_base') . 'pdl/' ) ); |
278 |
$pdl->loadModel('users'); |
279 |
$mo = $pdl->getModelObject("users.User"); |
280 |
|
281 |
return; |
282 |
|
283 |
$sql = "CREATE TABLE $tablename;"; |
284 |
print $sql . "<br>"; |
285 |
if ($this->query($sql)) { |
286 |
return 1; |
287 |
} |
288 |
|
289 |
/* |
290 |
switch ($this->properties['type']) { |
291 |
case 'mysql': |
292 |
break; |
293 |
case 'interbase': |
294 |
break; |
295 |
} |
296 |
*/ |
297 |
|
298 |
} |
299 |
|
300 |
|
301 |
function updateTable() { |
302 |
|
303 |
} |
304 |
|
305 |
} |
306 |
|
307 |
?> |