commit | author | age
|
b5a2f8
|
1 |
<?php |
436ed8
|
2 |
|
b5a2f8
|
3 |
/* |
436ed8
|
4 |
Copyright (c) 2007, Till Brehm, projektfarm Gmbh |
b5a2f8
|
5 |
All rights reserved. |
T |
6 |
|
|
7 |
Redistribution and use in source and binary forms, with or without modification, |
|
8 |
are permitted provided that the following conditions are met: |
|
9 |
|
|
10 |
* Redistributions of source code must retain the above copyright notice, |
|
11 |
this list of conditions and the following disclaimer. |
|
12 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
13 |
this list of conditions and the following disclaimer in the documentation |
|
14 |
and/or other materials provided with the distribution. |
|
15 |
* Neither the name of ISPConfig nor the names of its contributors |
|
16 |
may be used to endorse or promote products derived from this software without |
|
17 |
specific prior written permission. |
|
18 |
|
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
*/ |
|
30 |
|
436ed8
|
31 |
class db { |
b1a6a5
|
32 |
var $dbHost = ""; // hostname of the MySQL server |
MC |
33 |
var $dbName = ""; // logical database name on that server |
|
34 |
var $dbUser = ""; // database authorized user |
|
35 |
var $dbPass = ""; // user's password |
|
36 |
var $linkId = 0; // last result of mysql_connect() |
|
37 |
var $queryId = 0; // last result of mysql_query() |
|
38 |
var $record = array(); // last record fetched |
|
39 |
var $autoCommit = 1; // Autocommit Transactions |
|
40 |
var $currentRow; // current row number |
|
41 |
var $errorNumber = 0; // last error number |
|
42 |
var $errorMessage = ""; // last error message |
436ed8
|
43 |
var $errorLocation = "";// last error location |
R |
44 |
var $show_error_messages = false; |
|
45 |
var $transID; |
|
46 |
|
|
47 |
// constructor |
f5b0ca
|
48 |
function __construct() |
b5a2f8
|
49 |
{ |
b1a6a5
|
50 |
|
436ed8
|
51 |
global $conf; |
R |
52 |
$this->dbHost = $conf["db_host"]; |
|
53 |
$this->dbName = $conf["db_database"]; |
|
54 |
$this->dbUser = $conf["db_user"]; |
|
55 |
$this->dbPass = $conf["db_password"]; |
|
56 |
$this->connect(); |
b5a2f8
|
57 |
} |
b1a6a5
|
58 |
|
f5b0ca
|
59 |
function __destruct() { |
N |
60 |
$this->closeConn(); |
b1a6a5
|
61 |
} |
b5a2f8
|
62 |
|
436ed8
|
63 |
// error handler |
R |
64 |
function updateError($location) |
|
65 |
{ |
|
66 |
//$this->errorNumber = mysql_errno(); |
|
67 |
$this->errorMessage = ibase_errmsg(); |
|
68 |
$this->errorLocation = $location; |
|
69 |
if($this->errorNumber && $this->show_error_messages) |
|
70 |
{ |
b1a6a5
|
71 |
echo '<br /><b>'.$this->errorLocation.'</b><br />'.$this->errorMessage; |
436ed8
|
72 |
flush(); |
R |
73 |
} |
|
74 |
} |
|
75 |
|
|
76 |
function connect() |
|
77 |
{ |
|
78 |
if($this->linkId == 0) |
|
79 |
{ |
b1a6a5
|
80 |
$this->linkId = ibase_connect( $this->dbHost.":".$this->dbName , $this->dbUser, $this->dbPass, 'ISO8859_1', 0, 3 ); |
436ed8
|
81 |
if(!$this->linkId) |
R |
82 |
{ |
|
83 |
$this->updateError('DB::connect()<br />ibase_pconnect'); |
|
84 |
return false; |
|
85 |
} |
|
86 |
} |
|
87 |
return true; |
|
88 |
} |
|
89 |
|
|
90 |
function query($queryString) |
|
91 |
{ |
|
92 |
if(!$this->connect()) { |
|
93 |
return false; |
|
94 |
} |
b1a6a5
|
95 |
|
436ed8
|
96 |
if($this->autoCommit == 1) { |
R |
97 |
//$transID = ibase_trans(); |
b1a6a5
|
98 |
$this->queryId = @ibase_query($this->linkId, $queryString); |
436ed8
|
99 |
//ibase_commit(); |
R |
100 |
} else { |
b1a6a5
|
101 |
$this->queryId = @ibase_query($this->linkId, $queryString); |
436ed8
|
102 |
} |
b1a6a5
|
103 |
|
MC |
104 |
|
436ed8
|
105 |
$this->updateError('DB::query('.$queryString.')<br />ibase_query'); |
R |
106 |
if(!$this->queryId) { |
|
107 |
return false; |
|
108 |
} |
|
109 |
$this->currentRow = 0; |
|
110 |
return $this->queryId; |
|
111 |
} |
|
112 |
|
|
113 |
// returns all records in an array |
|
114 |
function queryAllRecords($queryString) |
|
115 |
{ |
|
116 |
if(!$this->query($queryString)) |
|
117 |
{ |
|
118 |
return false; |
|
119 |
} |
|
120 |
$ret = array(); |
|
121 |
while($line = $this->nextRecord()) |
|
122 |
{ |
|
123 |
$ret[] = $line; |
|
124 |
} |
|
125 |
//$this->freeResult(); |
|
126 |
ibase_free_result($this->queryId); |
|
127 |
return $ret; |
|
128 |
} |
|
129 |
|
|
130 |
// returns one record in an array |
|
131 |
function queryOneRecord($queryString) |
|
132 |
{ |
|
133 |
if(!$this->query($queryString)) |
|
134 |
{ |
|
135 |
return false; |
|
136 |
} |
|
137 |
$result = $this->nextRecord(); |
|
138 |
ibase_free_result($this->queryId); |
|
139 |
return $result; |
|
140 |
} |
|
141 |
|
|
142 |
// returns the next record in an array |
|
143 |
function nextRecord() |
|
144 |
{ |
b1a6a5
|
145 |
$this->record = ibase_fetch_assoc($this->queryId); |
436ed8
|
146 |
$this->updateError('DB::nextRecord()<br />ibase_fetch_assoc'); |
R |
147 |
if(!$this->record || !is_array($this->record)) |
|
148 |
{ |
|
149 |
return false; |
|
150 |
} |
|
151 |
$this->currentRow++; |
|
152 |
return $this->record; |
|
153 |
} |
|
154 |
|
|
155 |
// returns number of rows returned by the last select query |
|
156 |
function numRows() |
|
157 |
{ |
|
158 |
return false; |
|
159 |
} |
b1a6a5
|
160 |
|
436ed8
|
161 |
// returns mySQL insert id |
R |
162 |
function insertID() |
|
163 |
{ |
|
164 |
return false; |
|
165 |
} |
b1a6a5
|
166 |
|
MC |
167 |
// Check der variablen |
436ed8
|
168 |
// deprecated, now use quote |
b1a6a5
|
169 |
function check($formfield) |
MC |
170 |
{ |
|
171 |
return $this->quote($formfield); |
|
172 |
} |
|
173 |
|
436ed8
|
174 |
// Check der variablen |
b1a6a5
|
175 |
function quote($formfield) |
MC |
176 |
{ |
|
177 |
return str_replace("'", "''", $formfield); |
|
178 |
} |
|
179 |
|
436ed8
|
180 |
// Check der variablen |
b1a6a5
|
181 |
function unquote($formfield) |
MC |
182 |
{ |
|
183 |
return str_replace("''", "'", $formfield); |
|
184 |
} |
|
185 |
|
436ed8
|
186 |
function toLower($record) { |
R |
187 |
if(is_array($record)) { |
|
188 |
foreach($record as $key => $val) { |
|
189 |
$key = strtolower($key); |
|
190 |
$out[$key] = $val; |
|
191 |
} |
|
192 |
} |
b1a6a5
|
193 |
return $out; |
436ed8
|
194 |
} |
b1a6a5
|
195 |
|
MC |
196 |
|
|
197 |
function insert($tablename, $form, $debug = 0) |
|
198 |
{ |
|
199 |
if(is_array($form)){ |
|
200 |
foreach($form as $key => $value) |
|
201 |
{ |
|
202 |
$sql_key .= "$key, "; |
|
203 |
$sql_value .= "'".$this->quote($value)."', "; |
|
204 |
} |
|
205 |
$sql_key = substr($sql_key, 0, strlen($sql_key) - 2); |
|
206 |
$sql_value = substr($sql_value, 0, strlen($sql_value) - 2); |
|
207 |
|
|
208 |
$sql = "INSERT INTO $tablename (" . $sql_key . ") VALUES (" . $sql_value .")"; |
|
209 |
|
|
210 |
if($debug == 1) echo "SQL-Statement: ".$sql."<br><br>"; |
|
211 |
$this->query($sql); |
|
212 |
if($debug == 1) echo "mySQL Error Message: ".$this->errorMessage; |
|
213 |
} |
|
214 |
} |
|
215 |
|
|
216 |
function update($tablename, $form, $bedingung, $debug = 0) |
|
217 |
{ |
|
218 |
|
|
219 |
if(is_array($form)){ |
|
220 |
foreach($form as $key => $value) |
|
221 |
{ |
|
222 |
$insql .= "$key = '".$this->quote($value)."', "; |
|
223 |
} |
|
224 |
$insql = substr($insql, 0, strlen($insql) - 2); |
|
225 |
$sql = "UPDATE $tablename SET " . $insql . " WHERE $bedingung"; |
|
226 |
if($debug == 1) echo "SQL-Statement: ".$sql."<br><br>"; |
|
227 |
$this->query($sql); |
|
228 |
if($debug == 1) echo "mySQL Error Message: ".$this->errorMessage; |
|
229 |
} |
|
230 |
} |
|
231 |
|
|
232 |
function closeConn() { |
|
233 |
ibase_close($this->linkId); |
|
234 |
} |
|
235 |
|
|
236 |
function freeResult() { |
|
237 |
//ibase_free_result(); |
|
238 |
} |
|
239 |
|
|
240 |
function delete() { |
|
241 |
|
|
242 |
} |
|
243 |
|
|
244 |
function trans($action, $transID = null) { |
|
245 |
//action = begin, commit oder rollback |
|
246 |
|
|
247 |
if($action == 'begin') { |
|
248 |
$this->transID = ibase_trans($this->linkId); |
436ed8
|
249 |
return $this->transID; |
b1a6a5
|
250 |
} |
MC |
251 |
|
436ed8
|
252 |
if($action == 'commit' and !empty($this->transID)) { |
b1a6a5
|
253 |
ibase_commit($this->linkId, $this->transID); |
436ed8
|
254 |
} |
b1a6a5
|
255 |
|
436ed8
|
256 |
if($action == 'rollback') { |
b1a6a5
|
257 |
ibase_rollback($this->linkId, $this->transID); |
436ed8
|
258 |
} |
b1a6a5
|
259 |
|
MC |
260 |
} |
|
261 |
|
|
262 |
/* |
436ed8
|
263 |
$columns = array(action => add | alter | drop |
R |
264 |
name => Spaltenname |
|
265 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
266 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
267 |
typeValue => Wert z.B. bei Varchar |
|
268 |
defaultValue => Default Wert |
|
269 |
notNull => true | false |
|
270 |
autoInc => true | false |
|
271 |
option => unique | primary | index) |
b1a6a5
|
272 |
|
MC |
273 |
|
436ed8
|
274 |
*/ |
b1a6a5
|
275 |
|
MC |
276 |
function createTable($table_name, $columns) { |
|
277 |
$index = ""; |
|
278 |
$sql = "CREATE TABLE $table_name ("; |
|
279 |
foreach($columns as $col){ |
|
280 |
$sql .= $col["name"]." ".$this->mapType($col["type"], $col["typeValue"])." "; |
|
281 |
|
|
282 |
if($col["defaultValue"] != "") $sql .= "DEFAULT '".$col["defaultValue"]."' "; |
|
283 |
if($col["notNull"] == true) { |
|
284 |
$sql .= "NOT NULL "; |
|
285 |
} else { |
|
286 |
$sql .= "NULL "; |
|
287 |
} |
|
288 |
if($col["autoInc"] == true) $sql .= "auto_increment "; |
|
289 |
$sql.= ","; |
|
290 |
// key Definitionen |
|
291 |
if($col["option"] == "primary") $index .= "PRIMARY KEY (".$col["name"]."),"; |
|
292 |
if($col["option"] == "index") $index .= "INDEX (".$col["name"]."),"; |
|
293 |
if($col["option"] == "unique") $index .= "UNIQUE (".$col["name"]."),"; |
|
294 |
} |
|
295 |
$sql .= $index; |
|
296 |
$sql = substr($sql, 0, -1); |
|
297 |
$sql .= ")"; |
|
298 |
|
|
299 |
$this->query($sql); |
|
300 |
return true; |
|
301 |
} |
|
302 |
|
|
303 |
/* |
436ed8
|
304 |
$columns = array(action => add | alter | drop |
R |
305 |
name => Spaltenname |
|
306 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
307 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
308 |
typeValue => Wert z.B. bei Varchar |
|
309 |
defaultValue => Default Wert |
|
310 |
notNull => true | false |
|
311 |
autoInc => true | false |
|
312 |
option => unique | primary | index) |
b1a6a5
|
313 |
|
MC |
314 |
|
436ed8
|
315 |
*/ |
b1a6a5
|
316 |
function alterTable($table_name, $columns) { |
MC |
317 |
return false; |
|
318 |
} |
|
319 |
|
|
320 |
function dropTable($table_name) { |
|
321 |
$this->check($table_name); |
|
322 |
$sql = "DROP TABLE '". $table_name."'"; |
|
323 |
return $this->query($sql); |
|
324 |
} |
|
325 |
|
|
326 |
// gibt Array mit Tabellennamen zurück |
|
327 |
function getTables($database_name) { |
|
328 |
return false; |
|
329 |
} |
|
330 |
|
|
331 |
// gibt Feldinformationen zur Tabelle zurück |
|
332 |
/* |
436ed8
|
333 |
$columns = array(action => add | alter | drop |
R |
334 |
name => Spaltenname |
|
335 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
336 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
337 |
typeValue => Wert z.B. bei Varchar |
|
338 |
defaultValue => Default Wert |
|
339 |
notNull => true | false |
|
340 |
autoInc => true | false |
|
341 |
option => unique | primary | index) |
b1a6a5
|
342 |
|
MC |
343 |
|
436ed8
|
344 |
*/ |
b1a6a5
|
345 |
|
MC |
346 |
function tableInfo($table_name) { |
|
347 |
return false; |
|
348 |
} |
|
349 |
|
|
350 |
function mapType($metaType, $typeValue) { |
|
351 |
global $go_api; |
|
352 |
$metaType = strtolower($metaType); |
|
353 |
switch ($metaType) { |
|
354 |
case 'int16': |
|
355 |
return 'smallint'; |
|
356 |
break; |
|
357 |
case 'int32': |
|
358 |
return 'int'; |
|
359 |
break; |
|
360 |
case 'int64': |
|
361 |
return 'bigint'; |
|
362 |
break; |
|
363 |
case 'double': |
|
364 |
return 'double'; |
|
365 |
break; |
|
366 |
case 'char': |
|
367 |
return 'char'; |
|
368 |
break; |
|
369 |
case 'varchar': |
|
370 |
if($typeValue < 1) $go_api->errorMessage("Datenbank Fehler: Für diesen Datentyp ist eine Längenangabe notwendig."); |
|
371 |
return 'varchar('.$typeValue.')'; |
|
372 |
break; |
|
373 |
case 'text': |
|
374 |
return 'text'; |
|
375 |
break; |
|
376 |
case 'blob': |
|
377 |
return 'blob'; |
|
378 |
break; |
|
379 |
} |
|
380 |
} |
|
381 |
|
436ed8
|
382 |
} |
R |
383 |
|
f5b0ca
|
384 |
?> |