commit | author | age
|
87a4a6
|
1 |
<?php |
T |
2 |
/* |
f5b0ca
|
3 |
Copyright (c) 2005, Till Brehm, projektfarm Gmbh |
N |
4 |
All rights reserved. |
87a4a6
|
5 |
|
f5b0ca
|
6 |
Redistribution and use in source and binary forms, with or without modification, |
N |
7 |
are permitted provided that the following conditions are met: |
87a4a6
|
8 |
|
f5b0ca
|
9 |
* Redistributions of source code must retain the above copyright notice, |
N |
10 |
this list of conditions and the following disclaimer. |
|
11 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
12 |
this list of conditions and the following disclaimer in the documentation |
|
13 |
and/or other materials provided with the distribution. |
|
14 |
* Neither the name of ISPConfig nor the names of its contributors |
|
15 |
may be used to endorse or promote products derived from this software without |
|
16 |
specific prior written permission. |
87a4a6
|
17 |
|
f5b0ca
|
18 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
N |
19 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
21 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
22 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
23 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
24 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
25 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
26 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
27 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
28 |
*/ |
87a4a6
|
29 |
|
f5b0ca
|
30 |
class db extends mysqli |
N |
31 |
{ |
7fe908
|
32 |
protected $dbHost = ''; // hostname of the MySQL server |
MC |
33 |
private $dbName = ''; // logical database name on that server |
|
34 |
private $dbUser = ''; // database authorized user |
|
35 |
private $dbPass = ''; // user's password |
|
36 |
private $dbCharset = 'utf8';// Database charset |
|
37 |
private $dbNewLink = false; // Return a new linkID when connect is called again |
|
38 |
private $dbClientFlags = 0; // MySQL Client falgs |
|
39 |
private $linkId = 0; // last result of mysqli_connect() |
|
40 |
private $queryId = 0; // last result of mysqli_query() |
|
41 |
private $record = array(); // last record fetched |
|
42 |
private $autoCommit = 1; // Autocommit Transactions |
|
43 |
private $currentRow; // current row number |
|
44 |
public $errorNumber = 0; // last error number |
|
45 |
public $errorMessage = ''; // last error message |
|
46 |
private $errorLocation = '';// last error location |
|
47 |
public $show_error_messages = false; // false in server, true in interface |
|
48 |
private $isConnected = false; // needed to know if we have a valid mysqli object from the constructor |
87a4a6
|
49 |
|
7fe908
|
50 |
// constructor |
MC |
51 |
public function __construct($host = NULL , $user = NULL, $pass = NULL, $database = NULL) { |
|
52 |
global $app, $conf; |
1d8f7f
|
53 |
|
7fe908
|
54 |
$this->dbHost = $host ? $host : $conf['db_host']; |
MC |
55 |
$this->dbName = $database ? $database : $conf['db_database']; |
|
56 |
$this->dbUser = $user ? $user : $conf['db_user']; |
|
57 |
$this->dbPass = $pass ? $pass : $conf['db_password']; |
|
58 |
$this->dbCharset = $conf['db_charset']; |
|
59 |
$this->dbNewLink = $conf['db_new_link']; |
|
60 |
$this->dbClientFlags = $conf['db_client_flags']; |
1d8f7f
|
61 |
|
7fe908
|
62 |
parent::__construct($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName); |
MC |
63 |
$try = 0; |
|
64 |
//while(!is_null($this->connect_error) && $try < 10) { |
|
65 |
while(mysqli_connect_error() && $try < 10) { |
|
66 |
if($try > 8) sleep(5); |
|
67 |
elseif($try > 0) sleep(1); |
f5b0ca
|
68 |
|
7fe908
|
69 |
$try++; |
MC |
70 |
$this->updateError('DB::__construct'); |
1d8f7f
|
71 |
|
7fe908
|
72 |
parent::__construct($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName); |
MC |
73 |
} |
f5b0ca
|
74 |
|
7fe908
|
75 |
//if(is_null($this->connect_error)) $this->isConnected = true; |
MC |
76 |
if(!mysqli_connect_error()) $this->isConnected = true; |
f5b0ca
|
77 |
|
7fe908
|
78 |
$this->setCharacterEncoding(); |
MC |
79 |
} |
|
80 |
|
|
81 |
public function __destruct() { |
|
82 |
$this->close(); // helps avoid memory leaks, and persitent connections that don't go away. |
|
83 |
} |
|
84 |
|
|
85 |
/* This allows our private variables to be "read" out side of the class */ |
|
86 |
public function __get($var) { |
|
87 |
return isset($this->$var) ? $this->$var : NULL; |
|
88 |
} |
|
89 |
|
|
90 |
// error handler |
|
91 |
public function updateError($location) { |
|
92 |
global $app; |
|
93 |
|
|
94 |
/* |
72695f
|
95 |
if(!is_null($this->connect_error)) { |
f5b0ca
|
96 |
$this->errorNumber = $this->connect_errno; |
N |
97 |
$this->errorMessage = $this->connect_error; |
|
98 |
} else { |
|
99 |
$this->errorNumber = $this->errno; |
|
100 |
$this->errorMessage = $this->error; |
|
101 |
} |
cc6568
|
102 |
*/ |
7fe908
|
103 |
if(mysqli_connect_error()) { |
991019
|
104 |
$this->errorNumber = $this->connect_errno; |
TB |
105 |
$this->errorMessage = $this->connect_error; |
7fe908
|
106 |
} else { |
MC |
107 |
$this->errorNumber = mysqli_errno($this); |
|
108 |
$this->errorMessage = mysqli_error($this); |
|
109 |
} |
f5b0ca
|
110 |
|
7fe908
|
111 |
$this->errorLocation = $location; |
MC |
112 |
if($this->errorNumber) { |
|
113 |
$error_msg = $this->errorLocation .' '. $this->errorMessage; |
|
114 |
// This right here will allow us to use the samefile for server & interface |
|
115 |
if($this->show_error_messages) { |
|
116 |
echo $error_msg; |
|
117 |
} else if(is_object($app) && method_exists($app, 'log')) { |
|
118 |
$app->log($error_msg, LOGLEVEL_WARN); |
|
119 |
} else { |
|
120 |
/* This could be called before $app is ever declared.. In that case we should just spit out to error_log() */ |
|
121 |
error_log($error_msg); |
|
122 |
} |
|
123 |
} |
f5b0ca
|
124 |
} |
7fe908
|
125 |
|
MC |
126 |
private function setCharacterEncoding() { |
|
127 |
if($this->isConnected == false) return false; |
|
128 |
parent::query( 'SET NAMES '.$this->dbCharset); |
|
129 |
parent::query( "SET character_set_results = '".$this->dbCharset."', character_set_client = '".$this->dbCharset."', character_set_connection = '".$this->dbCharset."', character_set_database = '".$this->dbCharset."', character_set_server = '".$this->dbCharset."'"); |
f5b0ca
|
130 |
} |
N |
131 |
|
7fe908
|
132 |
public function query($queryString) { |
132081
|
133 |
global $app; |
MC |
134 |
|
7fe908
|
135 |
if($this->isConnected == false) return false; |
MC |
136 |
$try = 0; |
|
137 |
do { |
|
138 |
$try++; |
|
139 |
$ok = $this->ping(); |
|
140 |
if(!$ok) { |
|
141 |
if(!$this->real_connect($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName)) { |
991019
|
142 |
$this->updateError('DB::query -> reconnect'); |
132081
|
143 |
if($this->errorNumber == '111') { |
MC |
144 |
// server is not available |
|
145 |
if($try > 9) { |
|
146 |
if(isset($app) && isset($app->forceErrorExit)) { |
|
147 |
$app->forceErrorExit('Database connection failure!'); |
|
148 |
} |
|
149 |
// if we reach this, the app object is missing or has no exit method, so we continue as normal |
|
150 |
} |
|
151 |
sleep(30); // additional seconds, please! |
|
152 |
} |
|
153 |
|
7fe908
|
154 |
if($try > 9) { |
MC |
155 |
return false; |
|
156 |
} else { |
|
157 |
sleep(($try > 7 ? 5 : 1)); |
|
158 |
} |
|
159 |
} else { |
|
160 |
$this->setCharacterEncoding(); |
|
161 |
$ok = true; |
|
162 |
} |
|
163 |
} |
|
164 |
} while($ok == false); |
|
165 |
$this->queryId = parent::query($queryString); |
991019
|
166 |
if(!$this->queryId) $this->updateError('DB::query('.$queryString.') -> mysqli_query'); |
7fe908
|
167 |
if(!$this->queryId) { |
MC |
168 |
return false; |
|
169 |
} |
|
170 |
$this->currentRow = 0; |
|
171 |
return $this->queryId; |
|
172 |
} |
f5b0ca
|
173 |
|
7fe908
|
174 |
// returns all records in an array |
MC |
175 |
public function queryAllRecords($queryString) { |
|
176 |
if(!$this->query($queryString)) |
|
177 |
{ |
|
178 |
return false; |
|
179 |
} |
|
180 |
$ret = array(); |
|
181 |
while($line = $this->nextRecord()) |
|
182 |
{ |
|
183 |
$ret[] = $line; |
|
184 |
} |
|
185 |
return $ret; |
|
186 |
} |
43b345
|
187 |
|
7fe908
|
188 |
// returns one record in an array |
MC |
189 |
public function queryOneRecord($queryString) { |
|
190 |
if(!$this->query($queryString) || $this->numRows() == 0) |
|
191 |
{ |
|
192 |
return false; |
|
193 |
} |
|
194 |
return $this->nextRecord(); |
|
195 |
} |
f5b0ca
|
196 |
|
7fe908
|
197 |
// returns the next record in an array |
MC |
198 |
public function nextRecord() { |
|
199 |
$this->record = $this->queryId->fetch_assoc(); |
|
200 |
if(!$this->record || !is_array($this->record)) |
|
201 |
{ |
991019
|
202 |
$this->updateError('DB::nextRecord()-> mysql_fetch_array'); |
7fe908
|
203 |
return false; |
MC |
204 |
} |
|
205 |
$this->currentRow++; |
|
206 |
return $this->record; |
|
207 |
} |
|
208 |
|
|
209 |
// returns number of rows returned by the last select query |
|
210 |
public function numRows() { |
|
211 |
return $this->queryId->num_rows; |
|
212 |
} |
|
213 |
|
|
214 |
public function affectedRows() { |
|
215 |
return $this->queryId->affected_rows; |
|
216 |
} |
|
217 |
|
|
218 |
// returns mySQL insert id |
|
219 |
public function insertID() { |
|
220 |
return $this->insert_id; |
|
221 |
} |
|
222 |
|
|
223 |
|
|
224 |
// Check der variablen |
|
225 |
// Really.. using quote should be phased out in favor of using bind_param's. Though, for legacy code.. |
|
226 |
// here's the equivalent |
|
227 |
public function quote($formfield) { |
|
228 |
return $this->escape_string($formfield); |
|
229 |
} |
|
230 |
|
|
231 |
// Check der variablen |
|
232 |
public function unquote($formfield) { |
|
233 |
return stripslashes($formfield); |
|
234 |
} |
|
235 |
|
|
236 |
public function toLower($record) { |
|
237 |
if(is_array($record)) { |
|
238 |
foreach($record as $key => $val) { |
|
239 |
$key = strtolower($key); |
|
240 |
$out[$key] = $val; |
|
241 |
} |
|
242 |
} |
|
243 |
return $out; |
|
244 |
} |
|
245 |
|
|
246 |
public function diffrec($record_old, $record_new) { |
|
247 |
$diffrec_full = array(); |
|
248 |
$diff_num = 0; |
|
249 |
|
|
250 |
if(is_array($record_old) && count($record_old) > 0) { |
|
251 |
foreach($record_old as $key => $val) { |
|
252 |
// if(!isset($record_new[$key]) || $record_new[$key] != $val) { |
|
253 |
if(@$record_new[$key] != $val) { |
|
254 |
// Record has changed |
|
255 |
$diffrec_full['old'][$key] = $val; |
|
256 |
$diffrec_full['new'][$key] = @$record_new[$key]; |
|
257 |
$diff_num++; |
|
258 |
} else { |
|
259 |
$diffrec_full['old'][$key] = $val; |
|
260 |
$diffrec_full['new'][$key] = $val; |
|
261 |
} |
|
262 |
} |
|
263 |
} elseif(is_array($record_new)) { |
|
264 |
foreach($record_new as $key => $val) { |
|
265 |
if(isset($record_new[$key]) && @$record_old[$key] != $val) { |
|
266 |
// Record has changed |
|
267 |
$diffrec_full['new'][$key] = $val; |
|
268 |
$diffrec_full['old'][$key] = @$record_old[$key]; |
|
269 |
$diff_num++; |
|
270 |
} else { |
|
271 |
$diffrec_full['new'][$key] = $val; |
|
272 |
$diffrec_full['old'][$key] = $val; |
|
273 |
} |
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
return array('diff_num' => $diff_num, 'diff_rec' => $diffrec_full); |
|
278 |
|
|
279 |
} |
|
280 |
|
|
281 |
//** Function to fill the datalog with a full differential record. |
|
282 |
public function datalogSave($db_table, $action, $primary_field, $primary_id, $record_old, $record_new, $force_update = false) { |
|
283 |
global $app, $conf; |
|
284 |
|
|
285 |
// Insert backticks only for incomplete table names. |
|
286 |
if(stristr($db_table, '.')) { |
|
287 |
$escape = ''; |
|
288 |
} else { |
|
289 |
$escape = '`'; |
|
290 |
} |
f5b0ca
|
291 |
|
43b345
|
292 |
if($force_update == true) { |
T |
293 |
//* We force a update even if no record has changed |
7fe908
|
294 |
$diffrec_full = array('new' => $record_new, 'old' => $record_old); |
43b345
|
295 |
$diff_num = count($record_new); |
T |
296 |
} else { |
|
297 |
//* get the difference record between old and new record |
|
298 |
$tmp = $this->diffrec($record_old, $record_new); |
|
299 |
$diffrec_full = $tmp['diff_rec']; |
|
300 |
$diff_num = $tmp['diff_num']; |
|
301 |
unset($tmp); |
|
302 |
} |
f5b0ca
|
303 |
|
7fe908
|
304 |
// Insert the server_id, if the record has a server_id |
MC |
305 |
$server_id = (isset($record_old['server_id']) && $record_old['server_id'] > 0)?$record_old['server_id']:0; |
|
306 |
if(isset($record_new['server_id'])) $server_id = $record_new['server_id']; |
f5b0ca
|
307 |
|
N |
308 |
|
7fe908
|
309 |
if($diff_num > 0) { |
MC |
310 |
$diffstr = $this->quote(serialize($diffrec_full)); |
|
311 |
if(isset($_SESSION)) { |
|
312 |
$username = $this->quote($_SESSION['s']['user']['username']); |
|
313 |
} else { |
|
314 |
$username = 'admin'; |
|
315 |
} |
|
316 |
$dbidx = $primary_field.':'.$primary_id; |
|
317 |
|
|
318 |
if($action == 'INSERT') $action = 'i'; |
|
319 |
if($action == 'UPDATE') $action = 'u'; |
|
320 |
if($action == 'DELETE') $action = 'd'; |
|
321 |
$sql = "INSERT INTO sys_datalog (dbtable,dbidx,server_id,action,tstamp,user,data) VALUES ('".$db_table."','$dbidx','$server_id','$action','".time()."','$username','$diffstr')"; |
|
322 |
$this->query($sql); |
346f60
|
323 |
} |
f5b0ca
|
324 |
|
7fe908
|
325 |
return true; |
346f60
|
326 |
} |
f5b0ca
|
327 |
|
7fe908
|
328 |
//** Inserts a record and saves the changes into the datalog |
MC |
329 |
public function datalogInsert($tablename, $insert_data, $index_field) { |
|
330 |
global $app; |
f5b0ca
|
331 |
|
7fe908
|
332 |
if(is_array($insert_data)) { |
43b345
|
333 |
$key_str = ''; |
T |
334 |
$val_str = ''; |
|
335 |
foreach($insert_data as $key => $val) { |
|
336 |
$key_str .= "`".$key ."`,"; |
|
337 |
$val_str .= "'".$this->quote($val)."',"; |
|
338 |
} |
7fe908
|
339 |
$key_str = substr($key_str, 0, -1); |
MC |
340 |
$val_str = substr($val_str, 0, -1); |
43b345
|
341 |
$insert_data_str = '('.$key_str.') VALUES ('.$val_str.')'; |
T |
342 |
} else { |
|
343 |
$insert_data_str = $insert_data; |
|
344 |
} |
f5b0ca
|
345 |
|
7fe908
|
346 |
$old_rec = array(); |
MC |
347 |
$this->query("INSERT INTO $tablename $insert_data_str"); |
|
348 |
$index_value = $this->insertID(); |
|
349 |
$new_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'"); |
|
350 |
$this->datalogSave($tablename, 'INSERT', $index_field, $index_value, $old_rec, $new_rec); |
f5b0ca
|
351 |
|
7fe908
|
352 |
return $index_value; |
MC |
353 |
} |
f5b0ca
|
354 |
|
7fe908
|
355 |
//** Updates a record and saves the changes into the datalog |
MC |
356 |
public function datalogUpdate($tablename, $update_data, $index_field, $index_value, $force_update = false) { |
43b345
|
357 |
global $app; |
7fe908
|
358 |
|
MC |
359 |
$old_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'"); |
|
360 |
|
|
361 |
if(is_array($update_data)) { |
43b345
|
362 |
$update_data_str = ''; |
T |
363 |
foreach($update_data as $key => $val) { |
|
364 |
$update_data_str .= "`".$key ."` = '".$this->quote($val)."',"; |
|
365 |
} |
7fe908
|
366 |
$update_data_str = substr($update_data_str, 0, -1); |
43b345
|
367 |
} else { |
T |
368 |
$update_data_str = $update_data; |
|
369 |
} |
f5b0ca
|
370 |
|
7fe908
|
371 |
$this->query("UPDATE $tablename SET $update_data_str WHERE $index_field = '$index_value'"); |
MC |
372 |
$new_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'"); |
|
373 |
$this->datalogSave($tablename, 'UPDATE', $index_field, $index_value, $old_rec, $new_rec, $force_update); |
f5b0ca
|
374 |
|
7fe908
|
375 |
return true; |
MC |
376 |
} |
f5b0ca
|
377 |
|
7b47c0
|
378 |
//** Deletes a record and saves the changes into the datalog |
7fe908
|
379 |
public function datalogDelete($tablename, $index_field, $index_value) { |
MC |
380 |
global $app; |
7b47c0
|
381 |
|
7fe908
|
382 |
$old_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'"); |
MC |
383 |
$this->query("DELETE FROM $tablename WHERE $index_field = '$index_value'"); |
|
384 |
$new_rec = array(); |
|
385 |
$this->datalogSave($tablename, 'DELETE', $index_field, $index_value, $old_rec, $new_rec); |
|
386 |
|
|
387 |
return true; |
|
388 |
} |
|
389 |
|
|
390 |
//** Deletes a record and saves the changes into the datalog |
|
391 |
public function datalogError($errormsg) { |
|
392 |
global $app; |
|
393 |
|
|
394 |
if(isset($app->modules->current_datalog_id) && $app->modules->current_datalog_id > 0) $this->query("UPDATE sys_datalog set error = '".$this->quote($errormsg)."' WHERE datalog_id = ".$app->modules->current_datalog_id); |
|
395 |
|
|
396 |
return true; |
|
397 |
} |
f5b0ca
|
398 |
|
N |
399 |
|
7fe908
|
400 |
public function freeResult($query) |
MC |
401 |
{ |
|
402 |
if(is_object($query) && (get_class($query) == "mysqli_result")) { |
|
403 |
$query->free(); |
|
404 |
return true; |
|
405 |
} else { |
|
406 |
return false; |
|
407 |
} |
|
408 |
} |
f5b0ca
|
409 |
|
7fe908
|
410 |
/* TODO: Does anything use this? */ |
MC |
411 |
public function delete() { |
f5b0ca
|
412 |
|
7fe908
|
413 |
} |
f5b0ca
|
414 |
|
7fe908
|
415 |
/* TODO: Does anything use this? */ |
MC |
416 |
public function Transaction($action) { |
|
417 |
//action = begin, commit oder rollback |
f5b0ca
|
418 |
|
7fe908
|
419 |
} |
f5b0ca
|
420 |
|
7fe908
|
421 |
/* |
f5b0ca
|
422 |
$columns = array(action => add | alter | drop |
N |
423 |
name => Spaltenname |
|
424 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
425 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
426 |
typeValue => Wert z.B. bei Varchar |
|
427 |
defaultValue => Default Wert |
|
428 |
notNull => true | false |
|
429 |
autoInc => true | false |
|
430 |
option => unique | primary | index) |
|
431 |
|
|
432 |
|
|
433 |
*/ |
|
434 |
|
7fe908
|
435 |
public function createTable($table_name, $columns) { |
MC |
436 |
$index = ''; |
|
437 |
$sql = "CREATE TABLE $table_name ("; |
|
438 |
foreach($columns as $col){ |
|
439 |
$sql .= $col['name'].' '.$this->mapType($col['type'], $col['typeValue']).' '; |
f5b0ca
|
440 |
|
7fe908
|
441 |
if($col['defaultValue'] != '') $sql .= "DEFAULT '".$col['defaultValue']."' "; |
MC |
442 |
if($col['notNull'] == true) { |
|
443 |
$sql .= 'NOT NULL '; |
|
444 |
} else { |
|
445 |
$sql .= 'NULL '; |
|
446 |
} |
|
447 |
if($col['autoInc'] == true) $sql .= 'auto_increment '; |
|
448 |
$sql.= ','; |
|
449 |
// key Definitionen |
|
450 |
if($col['option'] == 'primary') $index .= 'PRIMARY KEY ('.$col['name'].'),'; |
|
451 |
if($col['option'] == 'index') $index .= 'INDEX ('.$col['name'].'),'; |
|
452 |
if($col['option'] == 'unique') $index .= 'UNIQUE ('.$col['name'].'),'; |
|
453 |
} |
|
454 |
$sql .= $index; |
|
455 |
$sql = substr($sql, 0, -1); |
|
456 |
$sql .= ')'; |
|
457 |
$this->query($sql); |
|
458 |
return true; |
f5b0ca
|
459 |
} |
N |
460 |
|
7fe908
|
461 |
/* |
f5b0ca
|
462 |
$columns = array(action => add | alter | drop |
N |
463 |
name => Spaltenname |
|
464 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
465 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
466 |
typeValue => Wert z.B. bei Varchar |
|
467 |
defaultValue => Default Wert |
|
468 |
notNull => true | false |
|
469 |
autoInc => true | false |
|
470 |
option => unique | primary | index) |
|
471 |
|
|
472 |
|
|
473 |
*/ |
7fe908
|
474 |
public function alterTable($table_name, $columns) { |
MC |
475 |
$index = ''; |
|
476 |
$sql = "ALTER TABLE $table_name "; |
|
477 |
foreach($columns as $col){ |
|
478 |
if($col['action'] == 'add') { |
|
479 |
$sql .= 'ADD '.$col['name'].' '.$this->mapType($col['type'], $col['typeValue']).' '; |
|
480 |
} elseif ($col['action'] == 'alter') { |
|
481 |
$sql .= 'CHANGE '.$col['name'].' '.$col['name_new'].' '.$this->mapType($col['type'], $col['typeValue']).' '; |
|
482 |
} elseif ($col['action'] == 'drop') { |
|
483 |
$sql .= 'DROP '.$col['name'].' '; |
|
484 |
} |
|
485 |
if($col['action'] != 'drop') { |
|
486 |
if($col['defaultValue'] != '') $sql .= "DEFAULT '".$col['defaultValue']."' "; |
|
487 |
if($col['notNull'] == true) { |
|
488 |
$sql .= 'NOT NULL '; |
|
489 |
} else { |
|
490 |
$sql .= 'NULL '; |
|
491 |
} |
|
492 |
if($col['autoInc'] == true) $sql .= 'auto_increment '; |
|
493 |
$sql.= ','; |
|
494 |
// Index definitions |
|
495 |
if($col['option'] == 'primary') $index .= 'PRIMARY KEY ('.$col['name'].'),'; |
|
496 |
if($col['option'] == 'index') $index .= 'INDEX ('.$col['name'].'),'; |
|
497 |
if($col['option'] == 'unique') $index .= 'UNIQUE ('.$col['name'].'),'; |
|
498 |
} |
|
499 |
} |
|
500 |
$sql .= $index; |
|
501 |
$sql = substr($sql, 0, -1); |
|
502 |
|
|
503 |
//die($sql); |
|
504 |
$this->query($sql); |
|
505 |
return true; |
f5b0ca
|
506 |
} |
7fe908
|
507 |
|
MC |
508 |
public function dropTable($table_name) { |
|
509 |
$this->check($table_name); |
|
510 |
$sql = "DROP TABLE '". $table_name."'"; |
|
511 |
return $this->query($sql); |
f5b0ca
|
512 |
} |
N |
513 |
|
7fe908
|
514 |
// gibt Array mit Tabellennamen zur�ck |
MC |
515 |
public function getTables($database_name = '') { |
|
516 |
if($this->isConnected == false) return false; |
|
517 |
if($database_name == '') $database_name = $this->dbName; |
|
518 |
$result = parent::query("SHOW TABLES FROM $database_name"); |
|
519 |
for ($i = 0; $i < $result->num_rows; $i++) { |
|
520 |
$tb_names[$i] = (($result->data_seek( $i) && (($___mysqli_tmp = $result->fetch_row()) !== NULL)) ? array_shift($___mysqli_tmp) : false); |
|
521 |
} |
|
522 |
return $tb_names; |
|
523 |
} |
f5b0ca
|
524 |
|
7fe908
|
525 |
// gibt Feldinformationen zur Tabelle zur�ck |
MC |
526 |
/* |
f5b0ca
|
527 |
$columns = array(action => add | alter | drop |
N |
528 |
name => Spaltenname |
|
529 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
530 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
531 |
typeValue => Wert z.B. bei Varchar |
|
532 |
defaultValue => Default Wert |
|
533 |
notNull => true | false |
|
534 |
autoInc => true | false |
|
535 |
option => unique | primary | index) |
|
536 |
|
|
537 |
|
|
538 |
*/ |
|
539 |
|
7fe908
|
540 |
function tableInfo($table_name) { |
f5b0ca
|
541 |
|
7fe908
|
542 |
global $go_api, $go_info; |
MC |
543 |
// Tabellenfelder einlesen |
f5b0ca
|
544 |
|
7fe908
|
545 |
if($rows = $go_api->db->queryAllRecords('SHOW FIELDS FROM '.$table_name)){ |
MC |
546 |
foreach($rows as $row) { |
|
547 |
$name = $row[0]; |
|
548 |
$default = $row[4]; |
|
549 |
$key = $row[3]; |
|
550 |
$extra = $row[5]; |
|
551 |
$isnull = $row[2]; |
|
552 |
$type = $row[1]; |
f5b0ca
|
553 |
|
N |
554 |
|
7fe908
|
555 |
$column = array(); |
f5b0ca
|
556 |
|
7fe908
|
557 |
$column['name'] = $name; |
MC |
558 |
//$column['type'] = $type; |
|
559 |
$column['defaultValue'] = $default; |
|
560 |
if(stristr($key, 'PRI')) $column['option'] = 'primary'; |
|
561 |
if(stristr($isnull, 'YES')) { |
|
562 |
$column['notNull'] = false; |
|
563 |
} else { |
|
564 |
$column['notNull'] = true; |
|
565 |
} |
|
566 |
if($extra == 'auto_increment') $column['autoInc'] = true; |
f5b0ca
|
567 |
|
N |
568 |
|
7fe908
|
569 |
// Type in Metatype umsetzen |
f5b0ca
|
570 |
|
7fe908
|
571 |
if(stristr($type, 'int(')) $metaType = 'int32'; |
MC |
572 |
if(stristr($type, 'bigint')) $metaType = 'int64'; |
|
573 |
if(stristr($type, 'char')) { |
|
574 |
$metaType = 'char'; |
|
575 |
$tmp_typeValue = explode('(', $type); |
|
576 |
$column['typeValue'] = substr($tmp_typeValue[1], 0, -1); |
|
577 |
} |
|
578 |
if(stristr($type, 'varchar')) { |
|
579 |
$metaType = 'varchar'; |
|
580 |
$tmp_typeValue = explode('(', $type); |
|
581 |
$column['typeValue'] = substr($tmp_typeValue[1], 0, -1); |
|
582 |
} |
|
583 |
if(stristr($type, 'text')) $metaType = 'text'; |
|
584 |
if(stristr($type, 'double')) $metaType = 'double'; |
|
585 |
if(stristr($type, 'blob')) $metaType = 'blob'; |
f5b0ca
|
586 |
|
N |
587 |
|
7fe908
|
588 |
$column['type'] = $metaType; |
f5b0ca
|
589 |
|
7fe908
|
590 |
$columns[] = $column; |
MC |
591 |
} |
|
592 |
return $columns; |
|
593 |
} else { |
|
594 |
return false; |
|
595 |
} |
f5b0ca
|
596 |
|
N |
597 |
|
7fe908
|
598 |
//$this->createTable('tester',$columns); |
f5b0ca
|
599 |
|
7fe908
|
600 |
/* |
f5b0ca
|
601 |
$result = mysql_list_fields($go_info["server"]["db_name"],$table_name); |
N |
602 |
$fields = mysql_num_fields ($result); |
|
603 |
$i = 0; |
|
604 |
$table = mysql_field_table ($result, $i); |
|
605 |
while ($i < $fields) { |
|
606 |
$name = mysql_field_name ($result, $i); |
|
607 |
$type = mysql_field_type ($result, $i); |
|
608 |
$len = mysql_field_len ($result, $i); |
|
609 |
$flags = mysql_field_flags ($result, $i); |
|
610 |
print_r($flags); |
|
611 |
|
|
612 |
$columns = array(name => $name, |
|
613 |
type => "", |
|
614 |
defaultValue => "", |
|
615 |
isnull => 1, |
|
616 |
option => ""); |
|
617 |
$returnvar[] = $columns; |
|
618 |
|
|
619 |
$i++; |
|
620 |
} |
|
621 |
*/ |
|
622 |
|
|
623 |
|
|
624 |
|
7fe908
|
625 |
} |
f5b0ca
|
626 |
|
7fe908
|
627 |
public function mapType($metaType, $typeValue) { |
MC |
628 |
global $go_api; |
|
629 |
$metaType = strtolower($metaType); |
|
630 |
switch ($metaType) { |
|
631 |
case 'int16': |
|
632 |
return 'smallint'; |
|
633 |
break; |
|
634 |
case 'int32': |
|
635 |
return 'int'; |
|
636 |
break; |
|
637 |
case 'int64': |
|
638 |
return 'bigint'; |
|
639 |
break; |
|
640 |
case 'double': |
|
641 |
return 'double'; |
|
642 |
break; |
|
643 |
case 'char': |
|
644 |
return 'char'; |
|
645 |
break; |
|
646 |
case 'varchar': |
|
647 |
if($typeValue < 1) die('Database failure: Lenght required for these data types.'); |
|
648 |
return 'varchar('.$typeValue.')'; |
|
649 |
break; |
|
650 |
case 'text': |
|
651 |
return 'text'; |
|
652 |
break; |
|
653 |
case 'blob': |
|
654 |
return 'blob'; |
|
655 |
break; |
|
656 |
} |
|
657 |
} |
f5b0ca
|
658 |
|
7fe908
|
659 |
} |
f5b0ca
|
660 |
|
7fe908
|
661 |
?> |