· 8 years ago · Nov 09, 2017, 02:46 AM
1<?php
2/**
3 * This file is a part of MyWebSQL package
4 * A simple and easy to debug mysqli wrapper class
5 */
6
7if (defined("CLASS_DB_MYSQLI_INCLUDED"))
8 return true;
9
10define("CLASS_DB_MYSQLI_INCLUDED", "1");
11
12define("NOT_NULL_FLAG", 1); /* Field can't be NULL */
13define("PRI_KEY_FLAG", 2); /* Field is part of a primary key */
14define("UNIQUE_KEY_FLAG", 4); /* Field is part of a unique key */
15define("MULTIPLE_KEY_FLAG", 8); /* Field is part of a key */
16define("BLOB_FLAG", 16); /* Field is a blob */
17define("UNSIGNED_FLAG", 32); /* Field is unsigned */
18define("ZEROFILL_FLAG", 64); /* Field is zerofill */
19define("BINARY_FLAG", 128); /* Field is binary */
20define("ENUM_FLAG", 256); /* field is an enum */
21define("AUTO_INCREMENT_FLAG", 512); /* field is a autoincrement field */
22define("TIMESTAMP_FLAG", 1024); /* Field is a timestamp */
23define("SET_FLAG", 2048); /* Field is a set */
24
25class DB_Mysqli {
26 var $ip, $user, $password, $db;
27 var $conn;
28 var $result; // array
29 var $errMsg;
30 var $escapeData;
31 var $lastQuery;
32 var $queryTime;
33
34 // these help identify field definitions more clearly
35 var $field_types = array(
36 7 => 'datetime',
37 10 => 'date',
38 11 => 'time',
39 12 => 'datetime',
40 13 => 'year',
41 16 => 'bit',
42 246 => 'numeric'
43 );
44
45 function DB_Mysqli() {
46 $this->conn = null;
47 $this->errMsg = null;
48 $this->escapeData = true;
49 $this->result = array();
50 }
51
52 function name() {
53 return 'mysql';
54 }
55
56 function hasServer() {
57 return true;
58 }
59
60 function hasObject($type) {
61 switch($type) {
62 case 'table':
63 case 'view':
64 case 'procedure':
65 case 'function':
66 case 'trigger':
67 return true;
68 break;
69 case 'event':
70 if ( ((float)Session::get('db', 'version_full')) >= 5.1 )
71 return true;
72 break;
73 }
74 return false;
75 }
76
77 function getObjectTypes() {
78 $types = array(
79 'tables', 'views', 'procedures', 'functions', 'triggers'
80 );
81
82 if ($this->hasObject('event'))
83 $types[] = 'events';
84
85 return $types;
86 }
87
88 function getObjectList( $details = false ) {
89 $data = array(
90 'tables' => $this->getTables( $details ),
91 'views' => $this->getViews(),
92 'procedures' => $this->getProcedures(),
93 'functions' => $this->getFunctions(),
94 'triggers' => $this->getTriggers(),
95 );
96
97 if ($this->hasObject('event'))
98 $data['events'] = $this->getEvents();
99
100 return $data;
101 }
102
103 function getBackQuotes() {
104 return '`';
105 }
106
107 function getQuotes() {
108 return '"';
109 }
110
111 function getStandardDbList() {
112 return array( 'information_schema', 'performance_schema', 'mysql', 'test' );
113 }
114
115 function setAuthOptions($options) {
116 }
117
118 function connect($ip, $user, $password, $db="") {
119 if (!function_exists('mysqli_connect')) {
120 return $this->error(str_replace('{{NAME}}', 'MySQLi', __('{{NAME}} client library is not installed')));
121 }
122
123 $this->conn = @mysqli_connect($ip, $user, $password);
124 if (!$this->conn)
125 return $this->error(__('Database connection failed to the server'));
126
127 if ($db && !@mysqli_select_db($this->conn, $db))
128 return $this->error(mysqli_error($this->conn));
129
130 $this->ip = $ip;
131 $this->user = $user;
132 $this->password = $password;
133 $this->db = $db;
134
135 $this->selectVersion();
136 $this->query("SET CHARACTER SET 'utf8'");
137 $this->query("SET collation_connection = 'utf8_general_ci'");
138
139 return true;
140 }
141
142 function disconnect() {
143 @mysqli_close($this->conn);
144 $this->conn = false;
145 return true;
146 }
147
148 function getCurrentUser() {
149 if ($this->query('select user()')) {
150 $row = $this->fetchRow();
151 return $row[0];
152 }
153 return '';
154 }
155
156 function selectDb($db) {
157 $this->db = $db;
158 mysqli_select_db($this->conn, $this->db);
159 }
160
161 function createDatabase( $name ) {
162 $sql = "create database `".$this->escape($name)."`";
163 return $this->query($sql);
164 }
165
166 function query($sql, $stack=0) { // call with query($sql, 1) to store multiple results
167 if (!$this->conn) {
168 log_message("DB: Connection has been closed");
169 return false;
170 }
171
172 if (v($this->result[$stack]))
173 @mysqli_free_result($this->result[$stack]);
174
175 $this->result[$stack] = "";
176
177 $this->lastQuery = $sql;
178 $this->queryTime = $this->getMicroTime();
179 $this->result[$stack] = @mysqli_query($this->conn, $sql);
180 $this->queryTime = $this->getMicroTime() - $this->queryTime;
181
182 if ($this->result[$stack] === FALSE) {
183 $this->errMsg = mysqli_error($this->conn);
184 log_message("DB: $sql ::: ".@mysqli_error($this->conn));
185 return false;
186 }
187
188 return true;
189 }
190
191 function getWarnings() {
192 $ret = array();
193 $res = mysqli_query($this->conn, "SHOW WARNINGS");
194 if ($res !== FALSE) {
195 while($row = mysqli_fetch_array($res))
196 $ret[$row['Code']] = $row['Message'];
197 }
198 return $ret;
199 }
200
201 function getQueryTime($time=false) { // returns formatted given value or internal query time
202 return sprintf("%.2f", ($time ? $time : $this->queryTime) * 1000) . " ms";
203 }
204
205 function hasAffectedRows() {
206 return ($this->getAffectedRows() > 0);
207 }
208
209 function insert($table, $values) {
210 if (!is_array($values))
211 return false;
212
213 $sql = "insert into $table (";
214
215 foreach($values as $field=>$value)
216 $sql .= " $field,";
217
218 $sql = substr($sql, 0, strlen($sql) - 1);
219
220 $sql .= ") values (";
221
222 foreach($values as $field=>$value) {
223 if ($this->escapeData)
224 $sql .= "'" . $this->escape($value) . "',";
225 else
226 $sql .= "'$value',";
227 }
228
229 $sql = substr($sql, 0, strlen($sql) - 1);
230
231 $sql .= ")";
232
233 $this->query($sql);
234 }
235
236 function update($table, $values, $condition="") {
237 if (!is_array($values))
238 return false;
239
240 $sql = "update $table set ";
241
242 foreach($values as $field=>$value) {
243 if ($this->escapeData)
244 $sql .= "$field = '" . $this->escape($field) . "',";
245 else
246 $sql .= "$field = '$value',";
247 }
248
249 $sql = substr($sql, 0, strlen($sql) - 1);
250
251 if ($condition != "")
252 $sql .= "$condition";
253
254 $this->query($sql);
255 }
256
257 function getInsertID() {
258 return mysqli_insert_id($this->conn);
259 }
260
261 function getResult($stack=0) {
262 return $this->result[$stack];
263 }
264
265 function hasResult($stack=0) {
266 return (is_object($this->result[$stack])); // !== FALSE && $this->result[$stack] !== TRUE);
267 }
268
269 function fetchRow($stack=0, $type="") {
270 if($type == "")
271 $type = MYSQLI_BOTH;
272 else if ($type == "num")
273 $type = MYSQLI_NUM;
274 else if ($type == "assoc")
275 $type = MYSQLI_ASSOC;
276
277 if (!$this->result[$stack]) {
278 log_message("DB: called fetchRow[$stack] but result is false");
279 return;
280 }
281 return @mysqli_fetch_array($this->result[$stack], $type);
282 }
283
284 function fetchSpecificRow($num, $type="", $stack=0) {
285 if($type == "")
286 $type = MYSQL_BOTH;
287 else if ($type == "num")
288 $type = MYSQLI_NUM;
289 else if ($type == "assoc")
290 $type = MYSQLI_ASSOC;
291
292 if (!$this->result[$stack]) {
293 log_message("DB: called fetchSpecificRow[$stack] but result is false");
294 return;
295 }
296
297 mysqli_data_seek($this->result[$stack], $num);
298 return @mysqli_fetch_array($this->result[$stack], $type);
299 }
300
301 function numRows($stack=0) {
302 return mysqli_num_rows($this->result[$stack]);
303 }
304
305 function error($str) {
306 log_message("DB: " . $str);
307 $this->errMsg = $str;
308 return false;
309 }
310
311 function getError() {
312 return $this->errMsg;
313 }
314
315 function escape($str) {
316 return mysqli_escape_string($this->conn, $str);
317 }
318
319 function quote($str) {
320 if(strpos($str, '.') === false)
321 return '`' . $str . '`';
322 return '`' . str_replace('.', '`.`', $str) . '`';
323 }
324
325 function setEscape($escape=true) {
326 $this->escapeData = $escape;
327 }
328
329 function getAffectedRows() {
330 return mysqli_affected_rows($this->conn);
331 }
332
333 /**************************************/
334 function getDatabases() {
335 $res = mysqli_query($this->conn, "show databases");
336 $ret = array();
337 while($row = mysqli_fetch_array($res))
338 $ret[] = $row[0];
339 return $ret;
340 }
341
342 function getTables( $details = false ) {
343 if (!$this->db)
344 return array();
345 $res = mysqli_query($this->conn, "show table status from `$this->db` where engine is NOT null");
346 $ret = array();
347 while($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
348 $ret[] = $details ? array(
349 $row['Name'], // table name
350 $row['Rows'], // number of records,
351 $row['Data_length'] + $row['Index_length'], // size of the table
352 (empty($row['Update_time']) ? $row['Create_time'] : $row['Update_time'] ), // last update timestamp
353 ) : $row['Name'];
354 }
355 return $ret;
356 }
357
358 function getViews() {
359 if (!$this->db)
360 return array();
361 $res = mysqli_query($this->conn, "show table status from `$this->db` where engine is null");
362 if (!$res)
363 return array();
364 $ret = array();
365 while($row = mysqli_fetch_array($res))
366 $ret[] = $row[0];
367 return $ret;
368 }
369
370 function getProcedures() {
371 if (!$this->db)
372 return array();
373 $res = mysqli_query($this->conn, "show procedure status where db = '$this->db'");
374 if (!$res)
375 return array();
376 $ret = array();
377 while($row = mysqli_fetch_array($res))
378 $ret[] = $row[1];
379 return $ret;
380 }
381
382 function getFunctions() {
383 if (!$this->db)
384 return array();
385 $res = mysqli_query($this->conn, "show function status where db = '$this->db'");
386 if (!$res)
387 return array();
388 $ret = array();
389 while($row = mysqli_fetch_array($res))
390 $ret[] = $row[1];
391 return $ret;
392 }
393
394 function getTriggers() {
395 if (!$this->db)
396 return array();
397 $res = mysqli_query($this->conn, "select `TRIGGER_NAME` from `INFORMATION_SCHEMA`.`TRIGGERS` where `TRIGGER_SCHEMA` = '$this->db'");
398 if (!$res)
399 return array();
400 $ret = array();
401 while($row = mysqli_fetch_array($res))
402 $ret[] = $row[0];
403 return $ret;
404 }
405
406 function getEvents() {
407 if (!$this->db)
408 return array();
409 $res = mysqli_query($this->conn, "select `EVENT_NAME` from `INFORMATION_SCHEMA`.`EVENTS` where `EVENT_SCHEMA` = '$this->db'");
410 if (!$res)
411 return array();
412 $ret = array();
413 while($row = mysqli_fetch_array($res))
414 $ret[] = $row[0];
415 return $ret;
416 }
417
418 /**************************************/
419 function getFieldInfo($stack=0) {
420 $fields = array();
421 $i = 0;
422 while ($i < mysqli_num_fields($this->result[$stack])) {
423 $meta = mysqli_fetch_field_direct($this->result[$stack], $i);
424 if ($meta) {
425 $f = new StdClass;
426 $f->name = $meta->name;
427 $f->table = $meta->table;
428 $f->not_null = ($meta->flags & NOT_NULL_FLAG) ? 1 : 0;
429 $f->blob = ($meta->flags & BLOB_FLAG) ? 1 : 0;
430 $f->pkey = ($meta->flags & PRI_KEY_FLAG) ? 1 : 0;
431 $f->ukey = ($meta->flags & UNIQUE_KEY_FLAG) ? 1 : 0;
432 $f->mkey = ($meta->flags & MULTIPLE_KEY_FLAG) ? 1 : 0;
433 $f->zerofill = ($meta->flags & ZEROFILL_FLAG) ? 1 : 0;
434 $f->unsigned = ($meta->flags & UNSIGNED_FLAG) ? 1 : 0;
435 $f->autoinc = ($meta->flags & AUTO_INCREMENT_FLAG) ? 1 : 0;
436 $f->numeric = $meta->type < 10 ? 1 : 0;
437 if (isset($this->field_types[$meta->type]))
438 $f->type = $this->field_types[$meta->type];
439 else if ($meta->flags & ENUM_FLAG)
440 $f->type = 'enum';
441 else if ($meta->flags & SET_FLAG)
442 $f->type = 'set';
443 else if ($meta->flags & TIMESTAMP_FLAG)
444 $f->type = "timestamp";
445 else if ($meta->flags & BINARY_FLAG)
446 $f->type = 'binary';
447 else if ($meta->type < 10)
448 $f->type = 'numeric';
449 else
450 $f->type = 'text';
451
452 if ($f->type == 'enum' || $f->type == 'set')
453 $f->list = $this->getFieldValues($f->table, $f->name);
454 else if ($f->type == 'bit')
455 $f->list = array('0', '1');
456
457 $fields[] = $f;
458 }
459 $i++;
460 }
461 return $fields;
462 }
463
464 function getMicroTime() {
465 list($usec, $sec) = explode(" ",microtime());
466 return ((float)$usec + (float)$sec);
467 }
468
469 function selectVersion() {
470 $res = mysqli_query($this->conn, "SHOW VARIABLES LIKE 'version%'");
471 while($row = mysqli_fetch_array($res)) {
472 if ($row[0] == 'version') {
473 Session::set('db', 'version', intval($row[1]));
474 Session::set('db', 'version_full', $row[1]);
475 } else if ($row[0] == 'version_comment') {
476 Session::set('db', 'version_comment', $row[1]);
477 }
478 }
479 }
480
481 function getCreateCommand($type, $name) {
482 $cmd = '';
483 $type = $this->escape($type);
484 $name = $this->escape($name);
485
486 if ($type == "trigger")
487 $sql = "show triggers where `trigger` = '$name'";
488 else
489 $sql = "show create $type `$name`";
490
491 if (!$this->query($sql) || $this->numRows() == 0)
492 return '';
493
494 $row = $this->fetchRow();
495
496 if ($type == "trigger")
497 $cmd = "create trigger `$row[0]`\r\n$row[4] $row[1] on `$row[2]`\r\nfor each row\r\n$row[3]";
498 else {
499 switch($type) {
500 case 'table':
501 case 'view':
502 $cmd = $row[1];
503 break;
504 case 'procedure':
505 case 'function':
506 $cmd = $row[2];
507 break;
508 case 'event':
509 $cmd = $row[3];
510 break;
511 }
512 }
513 return $cmd;
514 }
515
516 function getDropCommand( $table ) {
517 return "drop table if exists " . $this->quote( $table );
518 }
519
520 function getTruncateCommand( $table ) {
521 return 'truncate table ' . $this->quote( $table );
522 }
523
524 function getFieldValues($table, $name) {
525 $sql = 'show full fields from `'.$table.'` where `Field` = \''.$this->escape($name).'\'';
526 $res = mysqli_query($this->conn, $sql);
527 if (mysqli_num_rows($res) == 0)
528 return ( (object) array('list' => array()) );
529 $row = mysqli_fetch_array($res);
530 $type = $row['Type'];
531 preg_match('/enum\((.*)\)$/', $type, $matches);
532 if (!isset($matches[1]))
533 preg_match('/set\((.*)\)$/', $type, $matches);
534 if (isset($matches[1])) {
535 if (phpCheck(5.3)) {
536 $regex = "/\('(.*)'\)/";
537 preg_match_all($regex, $row['Type'], $list);
538 return array_map('replace_single_quotes', explode("','", $list[1][0]));
539 } else {
540 $list = explode(',', $matches[1]);
541 foreach($list as $k => $v)
542 $list[$k] = str_replace("\\'", "'", trim($v, " '"));
543 return $list;
544 }
545 }
546 return ( (object) array('list' => array()) );
547 }
548
549 function getEngines() {
550 $sql = 'show engines';
551 $res = mysqli_query($this->conn, $sql);
552 if (mysqli_num_rows($res) == 0)
553 return ( array() );
554
555 $arr = array();
556 while($row = mysqli_fetch_array($res))
557 if ($row['Support'] != 'NO')
558 $arr[] = $row['Engine'];
559 return $arr;
560 }
561
562 function getCharsets() {
563 $sql = 'show character set';
564 $res = mysqli_query($this->conn, $sql);
565 if (mysqli_num_rows($res) == 0)
566 return ( array() );
567
568 $arr = array();
569 while($row = mysqli_fetch_array($res))
570 $arr[] = $row['Charset'];
571
572 asort($arr);
573 return $arr;
574 }
575
576 function getCollations() {
577 $sql = 'show collation';
578 $res = mysqli_query($this->conn, $sql);
579 if (mysqli_num_rows($res) == 0)
580 return ( array() );
581
582 $arr = array();
583 while($row = mysqli_fetch_array($res))
584 $arr[] = $row['Collation'];
585
586 asort($arr);
587 return $arr;
588 }
589
590 function getTableFields($table) {
591 $sql = "show full fields from ".$this->quote($table);
592 if (!$this->query($sql, "_temp"))
593 return array();
594
595 $fields = array();
596 while($row = $this->fetchRow("_temp")) {
597 $f = new StdClass;
598 $f->type = $row['Type'];
599 $f->name = $row['Field'];
600 $fields[] = $f;
601 }
602
603 return $fields;
604 }
605
606 function getTableProperties($table) {
607 $sql = "show table status where `Name` like '".$this->escape($table)."'";
608 if (!$this->query($sql, "_tmp_query"))
609 return FALSE;
610 return $this->fetchRow("_tmp_query");
611 }
612
613 function queryTableStatus() {
614 $sql = "show table status where Engine is not null";
615 return $this->query($sql);
616 }
617
618 function getTableDescription( $table ) {
619 $sql = "describe " . $this->quote( $table );
620 return $this->query($sql);
621 }
622
623 function flush($option = '', $skiplog=false) {
624 $options = array('HOSTS', 'PRIVILEGES', 'TABLES', 'STATUS', 'DES_KEY_FILE', 'QUERY CACHE', 'USER_RESOURCES', 'TABLES WITH READ LOCK');
625 if ($option == '') {
626 foreach($options as $option) {
627 $sql = "flush " . ( $skiplog ? "NO_WRITE_TO_BINLOG " : "") . $this->escape($option);
628 $this->query($sql, '_temp_flush');
629 }
630 $this->query('UNLOCK TABLES', '_temp_flush');
631 } else {
632 $sql = "flush " . ( $skiplog ? "NO_WRITE_TO_BINLOG " : "") . $this->escape($option);
633 $this->query($sql, '_temp_flush');
634 if ($option == 'TABLES WITH READ LOCK')
635 $this->query('UNLOCK TABLES', '_temp_flush');
636 }
637
638 return true;
639 }
640
641 function getLastQuery() {
642 return $this->lastQuery;
643 }
644
645
646 function getInsertStatement($tbl) {
647 $sql = "show full fields from `$tbl`";
648 if (!$this->query($sql, '_insert'))
649 return false;
650
651 $str = "INSERT INTO `".$tbl."` (";
652 $num = $this->numRows('_insert');
653 $row = $this->fetchRow('_insert');
654 $str .= "`" . $row[0] . "`";
655
656 if ($row["Extra"] == "auto_increment")
657 $str2 = " VALUES (NULL";
658 else
659 $str2 = " VALUES (\"\"";
660
661 for($i=1; $i<$num; $i++) {
662 $row = $this->fetchRow('_insert');
663 $str .= ",`" . $row[0] . "`";
664 if ($row["Extra"] == "auto_increment")
665 $str2 .= ",NULL";
666 //else if (strpos($row["Type"], "int") !== false)
667 // $str2 .= ", "; // for numeric fields
668 else
669 $str2 .= ",\"\"";
670 }
671
672 $str .= ")";
673 $str2 .= ")";
674
675 return $str.$str2;
676 }
677
678 function getUpdateStatement($tbl) {
679 $sql = "show full fields from `".$this->escape($tbl)."`";
680 if (!$this->query($sql, '_update'))
681 return false;
682
683 $pKey = ''; // if a primary key is available, this helps avoid multikey attributes in where clause
684 $str2 = "";
685 $str = "UPDATE `".$tbl."` SET ";
686 $num = $this->numRows('_update');
687 $row = $this->fetchRow('_update');
688
689 $str .= "`" . $row[0] . "`=\"\"";
690 if ($row["Key"] != "")
691 $str2 .= "`$row[0]`=\"\"";
692 if ($row["Key"] == 'PRI')
693 $pKey = $row[0];
694
695 for($i=1; $i<$num; $i++) {
696 $row = $this->fetchRow('_update');
697 $str .= ",`" . $row[0] . "`=\"\"";
698 if ($row["Key"] != "") {
699 if ($row["Key"] == 'PRI')
700 $pKey = $row[0];
701 if ($str2 != "")
702 $str2 .= " AND ";
703 $str2 .= "`$row[0]`=\"\"";
704 }
705 }
706
707 // if we found a primary key, then use it only for where clause and discard other keys
708 if ($pKey != '')
709 $str2 = "`$pKey`=\"\"";
710 if ($str2 != "")
711 $str2 = " WHERE " . $str2;
712
713 return $str . $str2;
714 }
715
716 function truncateTable($tbl) {
717 return $this->query('truncate table '.$this->quote($tbl));
718 }
719
720 function renameObject($name, $type, $new_name) {
721 $result = false;
722 if($type == 'table') {
723 $query = 'rename '.$this->escape($type).' `'.$this->escape($name).'` to `'.$this->escape($new_name).'`';
724 $result = $this->query($query);
725 }
726 else {
727 $command = $this->getCreateCommand($type, $name);
728 $search = '/(create.*'.$type. ' )('.$name.'|\`'.$name.'\`)/i';
729 $replace = '${1} `'.$new_name.'`';
730 $query = preg_replace($search, $replace, $command, 1);
731 if ($this->query($query)) {
732 $query = 'drop '.$this->escape($type).' `'.$this->escape($name).'`';
733 $result = $this->query($query);
734 }
735 }
736
737 return $result;
738 }
739
740 function dropObject($name, $type) {
741 $result = false;
742 $query = 'drop '.$this->escape($type).' `'.$this->escape($name).'`';
743 $result = $this->query($query);
744 return $result;
745 }
746
747 function copyObject($name, $type, $new_name) {
748 $result = false;
749 if($type == 'table') {
750 $query = 'create '.$this->escape($type).' `' . $this->escape($new_name) . '` like `' . $this->escape($name) . '`';
751 $result = $this->query($query);
752 if ($result) {
753 $query = 'insert into `' . $this->escape($new_name) . '` select * from `' . $this->escape($name) . '`';
754 $result = $this->query($query);
755 }
756 }
757 else {
758 $command = $this->getCreateCommand($type, $name);
759 $search = '/(create.*'.$type. ' )('.$name.'|\`'.$name.'\`)/i';
760 $replace = '${1} `'.$new_name.'`';
761 $query = preg_replace($search, $replace, $command, 1);
762 $result = $this->query($query);
763 }
764 return $result;
765 }
766
767 function getAutoIncField($table) {
768 $sql = "show full fields from `".$this->escape($table)."`";
769 if (!$this->query($sql, "_temp"))
770 return false;
771
772 $i = 0;
773 while($row = $this->fetchRow("_temp")) {
774 if (strpos($row["Extra"], "auto_increment") !== false) {
775 return $i;
776 }
777 $i++;
778 }
779
780 return -1;
781 }
782
783 function queryVariables() {
784 return $this->query("SHOW VARIABLES");
785 }
786
787 function getLimit($count, $offset = 0) {
788 return " limit $offset, $count";
789 }
790
791 function addExportHeader( $name, $obj = 'db', $type='insert' ) {
792 $str = '';
793 switch($type) {
794 case 'insert':
795 if ( $obj == 'db' ) {
796 $str = "/* Database export results for db ".$name." */\n";
797 $str .= "\n/* Preserve session variables */\nSET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS;\nSET FOREIGN_KEY_CHECKS=0;\n\n/* Export data */\n";
798 } else if ( $obj == 'table' ) {
799 $str = "/* Table data export for table ".$name." */\n";
800 $str .= "\n/* Preserve session variables */\nSET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS;\nSET FOREIGN_KEY_CHECKS=0;\n\n/* Export data */\n";
801 } else if ( $obj == 'query' ) {
802 $str = "/* Export results for query data */\n";
803 $str .= "/* Query: \n".$name."\n*/\n";
804 $str .= "\n/* Preserve session variables */\nSET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS;\nSET FOREIGN_KEY_CHECKS=0;\n\n/* Export data */\n";
805 }
806 break;
807 }
808 return $str;
809 }
810
811 function addExportFooter( $type='insert' ) {
812 if ($type == 'insert')
813 return "\n/* Restore session variables to original values */\nSET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;\n";
814 return '';
815 }
816
817 function set_constraint( $constraint, $value ) {
818 switch ($constraint) {
819 case 'fkey':
820 $this->query('SET FOREIGN_KEY_CHECKS=' . ($value ? '1' : '0') );
821 break;
822 }
823 }
824}
825?>