commit | author | age
|
87a4a6
|
1 |
<?php |
T |
2 |
/* |
a123df
|
3 |
Copyright (c) 2005, Till Brehm, projektfarm Gmbh |
87a4a6
|
4 |
All rights reserved. |
T |
5 |
|
|
6 |
Redistribution and use in source and binary forms, with or without modification, |
|
7 |
are permitted provided that the following conditions are met: |
|
8 |
|
|
9 |
* Redistributions of source code must retain the above copyright notice, |
|
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. |
|
17 |
|
|
18 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
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 |
*/ |
|
29 |
|
a123df
|
30 |
class db |
87a4a6
|
31 |
{ |
458767
|
32 |
var $dbHost = ''; // hostname of the MySQL server |
J |
33 |
var $dbName = ''; // logical database name on that server |
|
34 |
var $dbUser = ''; // database authorized user |
|
35 |
var $dbPass = ''; // user's password |
4b9169
|
36 |
var $dbCharset = 'utf8';// Database charset |
c84a6d
|
37 |
var $dbNewLink = false; // Return a new linkID when connect is called again |
T |
38 |
var $dbClientFlags = 0; // MySQL Client falgs |
a123df
|
39 |
var $linkId = 0; // last result of mysql_connect() |
T |
40 |
var $queryId = 0; // last result of mysql_query() |
|
41 |
var $record = array(); // last record fetched |
e38d14
|
42 |
var $autoCommit = 1; // Autocommit Transactions |
a123df
|
43 |
var $currentRow; // current row number |
T |
44 |
var $errorNumber = 0; // last error number |
458767
|
45 |
var $errorMessage = ''; // last error message |
J |
46 |
var $errorLocation = '';// last error location |
0e381b
|
47 |
var $show_error_messages = true; |
87a4a6
|
48 |
|
a123df
|
49 |
// constructor |
682ed3
|
50 |
public function __construct() { |
a123df
|
51 |
|
T |
52 |
global $conf; |
458767
|
53 |
$this->dbHost = $conf['db_host']; |
J |
54 |
$this->dbName = $conf['db_database']; |
|
55 |
$this->dbUser = $conf['db_user']; |
|
56 |
$this->dbPass = $conf['db_password']; |
d0fc01
|
57 |
$this->dbCharset = $conf['db_charset']; |
c84a6d
|
58 |
$this->dbNewLink = $conf['db_new_link']; |
T |
59 |
$this->dbClientFlags = $conf['db_client_flags']; |
a123df
|
60 |
//$this->connect(); |
436ed8
|
61 |
} |
682ed3
|
62 |
|
T |
63 |
public function __destruct() { |
|
64 |
$this->closeConn(); |
|
65 |
} |
436ed8
|
66 |
|
a123df
|
67 |
// error handler |
T |
68 |
function updateError($location) |
436ed8
|
69 |
{ |
0e381b
|
70 |
global $app; |
eed6b3
|
71 |
$this->errorNumber = @mysql_errno($this->linkId); |
V |
72 |
$this->errorMessage = @mysql_error($this->linkId); |
a123df
|
73 |
$this->errorLocation = $location; |
0e381b
|
74 |
if($this->errorNumber && $this->show_error_messages && method_exists($app,'log')) |
436ed8
|
75 |
{ |
0e381b
|
76 |
// echo('<br /><b>'.$this->errorLocation.'</b><br />'.$this->errorMessage); |
458767
|
77 |
$app->log($this->errorLocation.' '.$this->errorMessage,LOGLEVEL_WARN); |
0e381b
|
78 |
//flush(); |
a123df
|
79 |
} |
T |
80 |
} |
|
81 |
|
|
82 |
function connect() |
|
83 |
{ |
|
84 |
if($this->linkId == 0) |
|
85 |
{ |
c84a6d
|
86 |
$this->linkId = @mysql_connect($this->dbHost, $this->dbUser, $this->dbPass, $this->dbNewLink, $this->dbClientFlags); |
a123df
|
87 |
if(!$this->linkId) |
T |
88 |
{ |
0e381b
|
89 |
$this->updateError('DB::connect()-> mysql_connect'); |
a123df
|
90 |
return false; |
T |
91 |
} |
e65b04
|
92 |
$this->queryId = @mysql_query('SET NAMES '.$this->dbCharset, $this->linkId); |
T |
93 |
$this->queryId = @mysql_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."'", $this->linkId); |
a123df
|
94 |
} |
T |
95 |
return true; |
|
96 |
} |
|
97 |
|
|
98 |
function query($queryString) |
|
99 |
{ |
|
100 |
if(!$this->connect()) |
|
101 |
{ |
436ed8
|
102 |
return false; |
R |
103 |
} |
a123df
|
104 |
if(!mysql_select_db($this->dbName, $this->linkId)) |
T |
105 |
{ |
0e381b
|
106 |
$this->updateError('DB::connect()-> mysql_select_db'); |
a123df
|
107 |
return false; |
T |
108 |
} |
|
109 |
$this->queryId = @mysql_query($queryString, $this->linkId); |
0e381b
|
110 |
$this->updateError('DB::query('.$queryString.') -> mysql_query'); |
a123df
|
111 |
if(!$this->queryId) |
T |
112 |
{ |
|
113 |
return false; |
|
114 |
} |
|
115 |
$this->currentRow = 0; |
|
116 |
return $this->queryId; |
436ed8
|
117 |
} |
R |
118 |
|
a123df
|
119 |
// returns all records in an array |
T |
120 |
function queryAllRecords($queryString) |
436ed8
|
121 |
{ |
a123df
|
122 |
if(!$this->query($queryString)) |
T |
123 |
{ |
|
124 |
return false; |
|
125 |
} |
|
126 |
$ret = array(); |
|
127 |
while($line = $this->nextRecord()) |
|
128 |
{ |
|
129 |
$ret[] = $line; |
|
130 |
} |
|
131 |
return $ret; |
436ed8
|
132 |
} |
R |
133 |
|
a123df
|
134 |
// returns one record in an array |
T |
135 |
function queryOneRecord($queryString) |
436ed8
|
136 |
{ |
a123df
|
137 |
if(!$this->query($queryString) || $this->numRows() == 0) |
T |
138 |
{ |
|
139 |
return false; |
|
140 |
} |
|
141 |
return $this->nextRecord(); |
436ed8
|
142 |
} |
R |
143 |
|
a123df
|
144 |
// returns the next record in an array |
T |
145 |
function nextRecord() |
436ed8
|
146 |
{ |
a123df
|
147 |
$this->record = mysql_fetch_assoc($this->queryId); |
0e381b
|
148 |
$this->updateError('DB::nextRecord()-> mysql_fetch_array'); |
a123df
|
149 |
if(!$this->record || !is_array($this->record)) |
T |
150 |
{ |
|
151 |
return false; |
|
152 |
} |
|
153 |
$this->currentRow++; |
|
154 |
return $this->record; |
436ed8
|
155 |
} |
R |
156 |
|
a123df
|
157 |
// returns number of rows returned by the last select query |
T |
158 |
function numRows() |
436ed8
|
159 |
{ |
a123df
|
160 |
return mysql_num_rows($this->queryId); |
436ed8
|
161 |
} |
R |
162 |
|
a123df
|
163 |
// returns mySQL insert id |
T |
164 |
function insertID() |
|
165 |
{ |
|
166 |
return mysql_insert_id($this->linkId); |
436ed8
|
167 |
} |
a123df
|
168 |
|
T |
169 |
// Check der variablen |
|
170 |
// deprecated, now use quote |
|
171 |
function check($formfield) |
|
172 |
{ |
|
173 |
return $this->quote($formfield); |
|
174 |
} |
|
175 |
|
|
176 |
// Check der variablen |
|
177 |
function quote($formfield) |
|
178 |
{ |
|
179 |
if(!$this->connect()){ |
|
180 |
$this->updateError('WARNING: mysql_connect: Used addslashes instead of mysql_real_escape_string'); |
|
181 |
return addslashes($formfield); |
|
182 |
} |
|
183 |
|
673365
|
184 |
return mysql_real_escape_string($formfield, $this->linkId); |
a123df
|
185 |
} |
T |
186 |
|
|
187 |
// Check der variablen |
|
188 |
function unquote($formfield) |
|
189 |
{ |
|
190 |
return stripslashes($formfield); |
|
191 |
} |
|
192 |
|
|
193 |
function toLower($record) { |
|
194 |
if(is_array($record)) { |
|
195 |
foreach($record as $key => $val) { |
|
196 |
$key = strtolower($key); |
|
197 |
$out[$key] = $val; |
|
198 |
} |
|
199 |
} |
|
200 |
return $out; |
|
201 |
} |
|
202 |
|
33d95b
|
203 |
/* |
T |
204 |
//* These functions are deprecated and will be removed. |
a123df
|
205 |
function insert($tablename,$form,$debug = 0) |
T |
206 |
{ |
|
207 |
if(is_array($form)){ |
|
208 |
foreach($form as $key => $value) |
|
209 |
{ |
|
210 |
$sql_key .= "$key, "; |
|
211 |
$sql_value .= "'".$this->check($value)."', "; |
|
212 |
} |
|
213 |
$sql_key = substr($sql_key,0,strlen($sql_key) - 2); |
|
214 |
$sql_value = substr($sql_value,0,strlen($sql_value) - 2); |
|
215 |
|
|
216 |
$sql = "INSERT INTO $tablename (" . $sql_key . ") VALUES (" . $sql_value .")"; |
|
217 |
|
|
218 |
if($debug == 1) echo "SQL-Statement: ".$sql."<br><br>"; |
|
219 |
$this->query($sql); |
|
220 |
if($debug == 1) echo "mySQL Error Message: ".$this->errorMessage; |
|
221 |
} |
436ed8
|
222 |
} |
a123df
|
223 |
|
T |
224 |
function update($tablename,$form,$bedingung,$debug = 0) |
|
225 |
{ |
|
226 |
|
|
227 |
if(is_array($form)){ |
|
228 |
foreach($form as $key => $value) |
|
229 |
{ |
|
230 |
$insql .= "$key = '".$this->check($value)."', "; |
|
231 |
} |
|
232 |
$insql = substr($insql,0,strlen($insql) - 2); |
|
233 |
$sql = "UPDATE $tablename SET " . $insql . " WHERE $bedingung"; |
|
234 |
if($debug == 1) echo "SQL-Statement: ".$sql."<br><br>"; |
|
235 |
$this->query($sql); |
|
236 |
if($debug == 1) echo "mySQL Error Message: ".$this->errorMessage; |
|
237 |
} |
|
238 |
} |
33d95b
|
239 |
*/ |
8d0c6b
|
240 |
|
T |
241 |
public function diffrec($record_old, $record_new) { |
|
242 |
$diffrec_full = array(); |
|
243 |
$diff_num = 0; |
|
244 |
|
|
245 |
if(is_array($record_old) && count($record_old) > 0) { |
|
246 |
foreach($record_old as $key => $val) { |
|
247 |
// if(!isset($record_new[$key]) || $record_new[$key] != $val) { |
|
248 |
if($record_new[$key] != $val) { |
|
249 |
// Record has changed |
|
250 |
$diffrec_full['old'][$key] = $val; |
|
251 |
$diffrec_full['new'][$key] = $record_new[$key]; |
|
252 |
$diff_num++; |
|
253 |
} else { |
|
254 |
$diffrec_full['old'][$key] = $val; |
|
255 |
$diffrec_full['new'][$key] = $val; |
|
256 |
} |
|
257 |
} |
|
258 |
} elseif(is_array($record_new)) { |
|
259 |
foreach($record_new as $key => $val) { |
|
260 |
if(isset($record_new[$key]) && @$record_old[$key] != $val) { |
|
261 |
// Record has changed |
|
262 |
$diffrec_full['new'][$key] = $val; |
|
263 |
$diffrec_full['old'][$key] = @$record_old[$key]; |
|
264 |
$diff_num++; |
|
265 |
} else { |
|
266 |
$diffrec_full['new'][$key] = $val; |
|
267 |
$diffrec_full['old'][$key] = $val; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
|
|
272 |
return array('diff_num' => $diff_num, 'diff_rec' => $diffrec_full); |
|
273 |
|
|
274 |
} |
|
275 |
|
|
276 |
//** Function to fill the datalog with a full differential record. |
|
277 |
public function datalogSave($db_table, $action, $primary_field, $primary_id, $record_old, $record_new) { |
|
278 |
global $app,$conf; |
|
279 |
|
|
280 |
// Insert backticks only for incomplete table names. |
|
281 |
if(stristr($db_table,'.')) { |
|
282 |
$escape = ''; |
|
283 |
} else { |
|
284 |
$escape = '`'; |
|
285 |
} |
|
286 |
|
|
287 |
$tmp = $this->diffrec($record_old, $record_new); |
|
288 |
$diffrec_full = $tmp['diff_rec']; |
|
289 |
$diff_num = $tmp['diff_num']; |
|
290 |
unset($tmp); |
|
291 |
|
|
292 |
// Insert the server_id, if the record has a server_id |
458767
|
293 |
$server_id = (isset($record_old['server_id']) && $record_old['server_id'] > 0)?$record_old['server_id']:0; |
J |
294 |
if(isset($record_new['server_id'])) $server_id = $record_new['server_id']; |
8d0c6b
|
295 |
|
T |
296 |
|
|
297 |
if($diff_num > 0) { |
|
298 |
//print_r($diff_num); |
|
299 |
//print_r($diffrec_full); |
|
300 |
$diffstr = $app->db->quote(serialize($diffrec_full)); |
458767
|
301 |
$username = $app->db->quote($_SESSION['s']['user']['username']); |
J |
302 |
$dbidx = $primary_field.':'.$primary_id; |
8d0c6b
|
303 |
|
T |
304 |
if($action == 'INSERT') $action = 'i'; |
|
305 |
if($action == 'UPDATE') $action = 'u'; |
|
306 |
if($action == 'DELETE') $action = 'd'; |
|
307 |
$sql = "INSERT INTO sys_datalog (dbtable,dbidx,server_id,action,tstamp,user,data) VALUES ('".$db_table."','$dbidx','$server_id','$action','".time()."','$username','$diffstr')"; |
|
308 |
$app->db->query($sql); |
|
309 |
} |
|
310 |
|
|
311 |
return true; |
|
312 |
} |
|
313 |
|
|
314 |
//** Inserts a record and saves the changes into the datalog |
|
315 |
public function datalogInsert($tablename, $insert_data, $index_field) { |
|
316 |
global $app; |
|
317 |
|
|
318 |
$old_rec = array(); |
|
319 |
$this->query("INSERT INTO $tablename $insert_data"); |
|
320 |
$index_value = $this->insertID(); |
|
321 |
$new_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'"); |
|
322 |
$this->datalogSave($tablename, 'INSERT', $index_field, $index_value, $old_rec, $new_rec); |
|
323 |
|
|
324 |
return $index_value; |
|
325 |
} |
|
326 |
|
|
327 |
//** Updates a record and saves the changes into the datalog |
|
328 |
public function datalogUpdate($tablename, $update_data, $index_field, $index_value) { |
|
329 |
global $app; |
|
330 |
|
|
331 |
$old_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'"); |
|
332 |
$this->query("UPDATE $tablename SET $update_data WHERE $index_field = '$index_value'"); |
|
333 |
$new_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'"); |
|
334 |
$this->datalogSave($tablename, 'UPDATE', $index_field, $index_value, $old_rec, $new_rec); |
|
335 |
|
|
336 |
return true; |
|
337 |
} |
|
338 |
|
|
339 |
//** Deletes a record and saves the changes into the datalog |
|
340 |
public function datalogDelete($tablename, $index_field, $index_value) { |
|
341 |
global $app; |
|
342 |
|
|
343 |
$old_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'"); |
|
344 |
$this->query("DELETE FROM $tablename WHERE $index_field = '$index_value'"); |
|
345 |
$new_rec = array(); |
|
346 |
$this->datalogSave($tablename, 'DELETE', $index_field, $index_value, $old_rec, $new_rec); |
|
347 |
|
|
348 |
return true; |
|
349 |
} |
e38d14
|
350 |
|
J |
351 |
|
a123df
|
352 |
public function closeConn() |
T |
353 |
{ |
|
354 |
if($this->linkId) |
|
355 |
{ |
|
356 |
mysql_close($this->linkId); |
|
357 |
return true; |
|
358 |
} else { return false; } |
|
359 |
} |
|
360 |
|
|
361 |
public function freeResult($query) |
|
362 |
{ |
|
363 |
if(mysql_free_result($query)) |
|
364 |
{ |
|
365 |
return true; |
|
366 |
} else { |
|
367 |
return false; |
|
368 |
} |
|
369 |
} |
|
370 |
|
|
371 |
function delete() { |
|
372 |
|
|
373 |
} |
|
374 |
|
|
375 |
function Transaction($action) { |
|
376 |
//action = begin, commit oder rollback |
|
377 |
|
|
378 |
} |
|
379 |
|
|
380 |
/* |
|
381 |
$columns = array(action => add | alter | drop |
|
382 |
name => Spaltenname |
|
383 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
384 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
385 |
typeValue => Wert z.B. bei Varchar |
|
386 |
defaultValue => Default Wert |
|
387 |
notNull => true | false |
|
388 |
autoInc => true | false |
|
389 |
option => unique | primary | index) |
|
390 |
|
|
391 |
|
|
392 |
*/ |
|
393 |
|
|
394 |
function createTable($table_name,$columns) { |
458767
|
395 |
$index = ''; |
a123df
|
396 |
$sql = "CREATE TABLE $table_name ("; |
T |
397 |
foreach($columns as $col){ |
458767
|
398 |
$sql .= $col['name'].' '.$this->mapType($col['type'],$col['typeValue']).' '; |
a123df
|
399 |
|
458767
|
400 |
if($col['defaultValue'] != '') $sql .= "DEFAULT '".$col['defaultValue']."' "; |
J |
401 |
if($col['notNull'] == true) { |
|
402 |
$sql .= 'NOT NULL '; |
a123df
|
403 |
} else { |
458767
|
404 |
$sql .= 'NULL '; |
a123df
|
405 |
} |
458767
|
406 |
if($col['autoInc'] == true) $sql .= 'auto_increment '; |
J |
407 |
$sql.= ','; |
a123df
|
408 |
// key Definitionen |
e38d14
|
409 |
if($col['option'] == 'primary') $index .= 'PRIMARY KEY ('.$col['name'].'),'; |
J |
410 |
if($col['option'] == 'index') $index .= 'INDEX ('.$col['name'].'),'; |
|
411 |
if($col['option'] == 'unique') $index .= 'UNIQUE ('.$col['name'].'),'; |
a123df
|
412 |
} |
T |
413 |
$sql .= $index; |
|
414 |
$sql = substr($sql,0,-1); |
458767
|
415 |
$sql .= ')'; |
a123df
|
416 |
$this->query($sql); |
T |
417 |
return true; |
e38d14
|
418 |
} |
a123df
|
419 |
|
T |
420 |
/* |
|
421 |
$columns = array(action => add | alter | drop |
|
422 |
name => Spaltenname |
|
423 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
424 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
425 |
typeValue => Wert z.B. bei Varchar |
|
426 |
defaultValue => Default Wert |
|
427 |
notNull => true | false |
|
428 |
autoInc => true | false |
|
429 |
option => unique | primary | index) |
|
430 |
|
|
431 |
|
|
432 |
*/ |
|
433 |
function alterTable($table_name,$columns) { |
458767
|
434 |
$index = ''; |
a123df
|
435 |
$sql = "ALTER TABLE $table_name "; |
T |
436 |
foreach($columns as $col){ |
458767
|
437 |
if($col['action'] == 'add') { |
e38d14
|
438 |
$sql .= 'ADD '.$col['name'].' '.$this->mapType($col['type'],$col['typeValue']).' '; |
458767
|
439 |
} elseif ($col['action'] == 'alter') { |
e38d14
|
440 |
$sql .= 'CHANGE '.$col['name'].' '.$col['name_new'].' '.$this->mapType($col['type'],$col['typeValue']).' '; |
458767
|
441 |
} elseif ($col['action'] == 'drop') { |
e38d14
|
442 |
$sql .= 'DROP '.$col['name'].' '; |
a123df
|
443 |
} |
458767
|
444 |
if($col['action'] != 'drop') { |
J |
445 |
if($col['defaultValue'] != '') $sql .= "DEFAULT '".$col['defaultValue']."' "; |
|
446 |
if($col['notNull'] == true) { |
|
447 |
$sql .= 'NOT NULL '; |
a123df
|
448 |
} else { |
458767
|
449 |
$sql .= 'NULL '; |
a123df
|
450 |
} |
458767
|
451 |
if($col['autoInc'] == true) $sql .= 'auto_increment '; |
J |
452 |
$sql.= ','; |
e38d14
|
453 |
// Index definitions |
J |
454 |
if($col['option'] == 'primary') $index .= 'PRIMARY KEY ('.$col['name'].'),'; |
|
455 |
if($col['option'] == 'index') $index .= 'INDEX ('.$col['name'].'),'; |
|
456 |
if($col['option'] == 'unique') $index .= 'UNIQUE ('.$col['name'].'),'; |
a123df
|
457 |
} |
T |
458 |
} |
|
459 |
$sql .= $index; |
|
460 |
$sql = substr($sql,0,-1); |
|
461 |
|
|
462 |
//die($sql); |
|
463 |
$this->query($sql); |
|
464 |
return true; |
|
465 |
} |
|
466 |
|
|
467 |
function dropTable($table_name) { |
|
468 |
$this->check($table_name); |
|
469 |
$sql = "DROP TABLE '". $table_name."'"; |
|
470 |
return $this->query($sql); |
|
471 |
} |
|
472 |
|
eed6b3
|
473 |
// gibt Array mit Tabellennamen zur�ck |
a123df
|
474 |
function getTables($database_name = '') { |
T |
475 |
|
|
476 |
if($database_name == '') $database_name = $this->dbName; |
|
477 |
$result = mysql_list_tables($database_name); |
|
478 |
for ($i = 0; $i < mysql_num_rows($result); $i++) { |
|
479 |
$tb_names[$i] = mysql_tablename($result, $i); |
|
480 |
} |
|
481 |
return $tb_names; |
|
482 |
} |
|
483 |
|
eed6b3
|
484 |
// gibt Feldinformationen zur Tabelle zur�ck |
a123df
|
485 |
/* |
T |
486 |
$columns = array(action => add | alter | drop |
|
487 |
name => Spaltenname |
|
488 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
489 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
490 |
typeValue => Wert z.B. bei Varchar |
|
491 |
defaultValue => Default Wert |
|
492 |
notNull => true | false |
|
493 |
autoInc => true | false |
|
494 |
option => unique | primary | index) |
|
495 |
|
|
496 |
|
|
497 |
*/ |
|
498 |
|
|
499 |
function tableInfo($table_name) { |
|
500 |
|
|
501 |
global $go_api,$go_info; |
|
502 |
// Tabellenfelder einlesen |
|
503 |
|
e38d14
|
504 |
if($rows = $go_api->db->queryAllRecords('SHOW FIELDS FROM '.$table_name)){ |
a123df
|
505 |
foreach($rows as $row) { |
T |
506 |
$name = $row[0]; |
|
507 |
$default = $row[4]; |
|
508 |
$key = $row[3]; |
|
509 |
$extra = $row[5]; |
|
510 |
$isnull = $row[2]; |
|
511 |
$type = $row[1]; |
|
512 |
|
|
513 |
|
|
514 |
$column = array(); |
|
515 |
|
458767
|
516 |
$column['name'] = $name; |
J |
517 |
//$column['type'] = $type; |
|
518 |
$column['defaultValue'] = $default; |
|
519 |
if(stristr($key,'PRI')) $column['option'] = 'primary'; |
|
520 |
if(stristr($isnull,'YES')) { |
|
521 |
$column['notNull'] = false; |
a123df
|
522 |
} else { |
458767
|
523 |
$column['notNull'] = true; |
a123df
|
524 |
} |
458767
|
525 |
if($extra == 'auto_increment') $column['autoInc'] = true; |
a123df
|
526 |
|
T |
527 |
|
|
528 |
// Type in Metatype umsetzen |
|
529 |
|
458767
|
530 |
if(stristr($type,'int(')) $metaType = 'int32'; |
J |
531 |
if(stristr($type,'bigint')) $metaType = 'int64'; |
|
532 |
if(stristr($type,'char')) { |
a123df
|
533 |
$metaType = 'char'; |
T |
534 |
$tmp_typeValue = explode('(',$type); |
458767
|
535 |
$column['typeValue'] = substr($tmp_typeValue[1],0,-1); |
a123df
|
536 |
} |
458767
|
537 |
if(stristr($type,'varchar')) { |
a123df
|
538 |
$metaType = 'varchar'; |
T |
539 |
$tmp_typeValue = explode('(',$type); |
458767
|
540 |
$column['typeValue'] = substr($tmp_typeValue[1],0,-1); |
a123df
|
541 |
} |
458767
|
542 |
if(stristr($type,'text')) $metaType = 'text'; |
J |
543 |
if(stristr($type,'double')) $metaType = 'double'; |
|
544 |
if(stristr($type,'blob')) $metaType = 'blob'; |
a123df
|
545 |
|
T |
546 |
|
458767
|
547 |
$column['type'] = $metaType; |
a123df
|
548 |
|
T |
549 |
$columns[] = $column; |
|
550 |
} |
|
551 |
return $columns; |
436ed8
|
552 |
} else { |
a123df
|
553 |
return false; |
436ed8
|
554 |
} |
a123df
|
555 |
|
T |
556 |
|
|
557 |
//$this->createTable('tester',$columns); |
|
558 |
|
|
559 |
/* |
|
560 |
$result = mysql_list_fields($go_info["server"]["db_name"],$table_name); |
|
561 |
$fields = mysql_num_fields ($result); |
|
562 |
$i = 0; |
|
563 |
$table = mysql_field_table ($result, $i); |
|
564 |
while ($i < $fields) { |
|
565 |
$name = mysql_field_name ($result, $i); |
|
566 |
$type = mysql_field_type ($result, $i); |
|
567 |
$len = mysql_field_len ($result, $i); |
|
568 |
$flags = mysql_field_flags ($result, $i); |
|
569 |
print_r($flags); |
|
570 |
|
|
571 |
$columns = array(name => $name, |
|
572 |
type => "", |
|
573 |
defaultValue => "", |
|
574 |
isnull => 1, |
|
575 |
option => ""); |
|
576 |
$returnvar[] = $columns; |
|
577 |
|
|
578 |
$i++; |
436ed8
|
579 |
} |
a123df
|
580 |
*/ |
436ed8
|
581 |
|
R |
582 |
|
a123df
|
583 |
|
T |
584 |
} |
|
585 |
|
|
586 |
function mapType($metaType,$typeValue) { |
|
587 |
global $go_api; |
|
588 |
$metaType = strtolower($metaType); |
|
589 |
switch ($metaType) { |
|
590 |
case 'int16': |
|
591 |
return 'smallint'; |
|
592 |
break; |
|
593 |
case 'int32': |
|
594 |
return 'int'; |
|
595 |
break; |
|
596 |
case 'int64': |
|
597 |
return 'bigint'; |
|
598 |
break; |
|
599 |
case 'double': |
|
600 |
return 'double'; |
|
601 |
break; |
|
602 |
case 'char': |
|
603 |
return 'char'; |
|
604 |
break; |
|
605 |
case 'varchar': |
458767
|
606 |
if($typeValue < 1) die('Database failure: Lenght required for these data types.'); |
a123df
|
607 |
return 'varchar('.$typeValue.')'; |
T |
608 |
break; |
|
609 |
case 'text': |
|
610 |
return 'text'; |
|
611 |
break; |
|
612 |
case 'blob': |
|
613 |
return 'blob'; |
|
614 |
break; |
|
615 |
} |
|
616 |
} |
|
617 |
|
|
618 |
} |
436ed8
|
619 |
|
458767
|
620 |
?> |