Move PMA_DBI_get_column_values and PMA_DBI_get_table_indexes to server_synchronize...
[phpmyadmin.git] / libraries / database_interface.lib.php
blobe6d4094c505595701702734ba719a0313a1fd66b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Common Option Constants For DBI Functions
6 * @package phpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 // PMA_DBI_try_query()
16 define('PMA_DBI_QUERY_STORE', 1); // Force STORE_RESULT method, ignored by classic MySQL.
17 define('PMA_DBI_QUERY_UNBUFFERED', 2); // Do not read whole query
18 // PMA_DBI_get_variable()
19 define('PMA_DBI_GETVAR_SESSION', 1);
20 define('PMA_DBI_GETVAR_GLOBAL', 2);
22 /**
23 * Checks one of the mysql extensions
25 * @param string $extension mysql extension to check
27 function PMA_DBI_checkMysqlExtension($extension = 'mysql') {
28 if (! function_exists($extension . '_connect')) {
29 return false;
32 return true;
35 /**
36 * check for requested extension
38 if (! PMA_DBI_checkMysqlExtension($GLOBALS['cfg']['Server']['extension'])) {
40 // if it fails try alternative extension ...
41 // and display an error ...
43 /**
44 * @todo add different messages for alternative extension
45 * and complete fail (no alternative extension too)
47 PMA_warnMissingExtension($GLOBALS['cfg']['Server']['extension'], false, PMA_showDocu('faqmysql'));
49 if ($GLOBALS['cfg']['Server']['extension'] === 'mysql') {
50 $alternativ_extension = 'mysqli';
51 } else {
52 $alternativ_extension = 'mysql';
55 if (! PMA_DBI_checkMysqlExtension($alternativ_extension)) {
56 // if alternative fails too ...
57 PMA_warnMissingExtension($GLOBALS['cfg']['Server']['extension'], true, PMA_showDocu('faqmysql'));
60 $GLOBALS['cfg']['Server']['extension'] = $alternativ_extension;
61 unset($alternativ_extension);
64 /**
65 * Including The DBI Plugin
67 require_once './libraries/dbi/' . $GLOBALS['cfg']['Server']['extension'] . '.dbi.lib.php';
69 /**
70 * Common Functions
72 function PMA_DBI_query($query, $link = null, $options = 0, $cache_affected_rows = true) {
73 $res = PMA_DBI_try_query($query, $link, $options, $cache_affected_rows)
74 or PMA_mysqlDie(PMA_DBI_getError($link), $query);
75 return $res;
78 /**
79 * runs a query and returns the result
81 * @param string $query query to run
82 * @param resource $link mysql link resource
83 * @param integer $options
84 * @return mixed
86 function PMA_DBI_try_query($query, $link = null, $options = 0, $cache_affected_rows = true)
88 if (empty($link)) {
89 if (isset($GLOBALS['userlink'])) {
90 $link = $GLOBALS['userlink'];
91 } else {
92 return false;
96 if ($GLOBALS['cfg']['DBG']['sql']) {
97 $time = microtime(true);
100 $r = PMA_DBI_real_query($query, $link, $options);
102 if ($cache_affected_rows) {
103 $GLOBALS['cached_affected_rows'] = PMA_DBI_affected_rows($link, $get_from_cache = false);
106 if ($GLOBALS['cfg']['DBG']['sql']) {
107 $time = microtime(true) - $time;
109 $hash = md5($query);
111 if (isset($_SESSION['debug']['queries'][$hash])) {
112 $_SESSION['debug']['queries'][$hash]['count']++;
113 } else {
114 $_SESSION['debug']['queries'][$hash] = array();
115 $_SESSION['debug']['queries'][$hash]['count'] = 1;
116 $_SESSION['debug']['queries'][$hash]['query'] = $query;
117 $_SESSION['debug']['queries'][$hash]['time'] = $time;
120 $trace = array();
121 foreach (debug_backtrace() as $trace_step) {
122 $trace[] = PMA_Error::relPath($trace_step['file']) . '#'
123 . $trace_step['line'] . ': '
124 . (isset($trace_step['class']) ? $trace_step['class'] : '')
125 //. (isset($trace_step['object']) ? get_class($trace_step['object']) : '')
126 . (isset($trace_step['type']) ? $trace_step['type'] : '')
127 . (isset($trace_step['function']) ? $trace_step['function'] : '')
128 . '('
129 . (isset($trace_step['params']) ? implode(', ', $trace_step['params']) : '')
130 . ')'
133 $_SESSION['debug']['queries'][$hash]['trace'][] = $trace;
135 if ($r != false && PMA_Tracker::isActive() == true ) {
136 PMA_Tracker::handleQuery($query);
139 return $r;
143 * converts charset of a mysql message, usually coming from mysql_error(),
144 * into PMA charset, usally UTF-8
145 * uses language to charset mapping from mysql/share/errmsg.txt
146 * and charset names to ISO charset from information_schema.CHARACTER_SETS
148 * @param string $message
149 * @return string $message
151 function PMA_DBI_convert_message($message) {
152 // latin always last!
153 $encodings = array(
154 'japanese' => 'EUC-JP', //'ujis',
155 'japanese-sjis' => 'Shift-JIS', //'sjis',
156 'korean' => 'EUC-KR', //'euckr',
157 'russian' => 'KOI8-R', //'koi8r',
158 'ukrainian' => 'KOI8-U', //'koi8u',
159 'greek' => 'ISO-8859-7', //'greek',
160 'serbian' => 'CP1250', //'cp1250',
161 'estonian' => 'ISO-8859-13', //'latin7',
162 'slovak' => 'ISO-8859-2', //'latin2',
163 'czech' => 'ISO-8859-2', //'latin2',
164 'hungarian' => 'ISO-8859-2', //'latin2',
165 'polish' => 'ISO-8859-2', //'latin2',
166 'romanian' => 'ISO-8859-2', //'latin2',
167 'spanish' => 'CP1252', //'latin1',
168 'swedish' => 'CP1252', //'latin1',
169 'italian' => 'CP1252', //'latin1',
170 'norwegian-ny' => 'CP1252', //'latin1',
171 'norwegian' => 'CP1252', //'latin1',
172 'portuguese' => 'CP1252', //'latin1',
173 'danish' => 'CP1252', //'latin1',
174 'dutch' => 'CP1252', //'latin1',
175 'english' => 'CP1252', //'latin1',
176 'french' => 'CP1252', //'latin1',
177 'german' => 'CP1252', //'latin1',
180 if ($server_language = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'language\';', 0, 1)) {
181 $found = array();
182 if (preg_match('&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $server_language, $found)) {
183 $server_language = $found[1];
187 if (! empty($server_language) && isset($encodings[$server_language])) {
188 if (function_exists('iconv')) {
189 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
190 require_once './libraries/iconv_wrapper.lib.php';
191 $message = PMA_aix_iconv_wrapper($encodings[$server_language],
192 'utf-8' . $GLOBALS['cfg']['IconvExtraParams'], $message);
193 } else {
194 $message = iconv($encodings[$server_language],
195 'utf-8' . $GLOBALS['cfg']['IconvExtraParams'], $message);
197 } elseif (function_exists('recode_string')) {
198 $message = recode_string($encodings[$server_language] . '..' . 'utf-8',
199 $message);
200 } elseif (function_exists('libiconv')) {
201 $message = libiconv($encodings[$server_language], 'utf-8', $message);
202 } elseif (function_exists('mb_convert_encoding')) {
203 // do not try unsupported charsets
204 if (! in_array($server_language, array('ukrainian', 'greek', 'serbian'))) {
205 $message = mb_convert_encoding($message, 'utf-8',
206 $encodings[$server_language]);
209 } else {
211 * @todo lang not found, try all, what TODO ?
215 return $message;
219 * returns array with table names for given db
221 * @param string $database name of database
222 * @param mixed $link mysql link resource|object
223 * @return array tables names
225 function PMA_DBI_get_tables($database, $link = null)
227 return PMA_DBI_fetch_result('SHOW TABLES FROM ' . PMA_backquote($database) . ';',
228 null, 0, $link, PMA_DBI_QUERY_STORE);
232 * usort comparison callback
234 * @param string $a first argument to sort
235 * @param string $b second argument to sort
237 * @return integer a value representing whether $a should be before $b in the
238 * sorted array or not
240 * @global string the column the array shall be sorted by
241 * @global string the sorting order ('ASC' or 'DESC')
243 * @access private
245 function PMA_usort_comparison_callback($a, $b)
247 if ($GLOBALS['cfg']['NaturalOrder']) {
248 $sorter = 'strnatcasecmp';
249 } else {
250 $sorter = 'strcasecmp';
252 /* No sorting when key is not present */
253 if (! isset($a[$GLOBALS['callback_sort_by']]) || ! isset($b[$GLOBALS['callback_sort_by']])) {
254 return 0;
256 // produces f.e.:
257 // return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
258 return ($GLOBALS['callback_sort_order'] == 'ASC' ? 1 : -1) * $sorter($a[$GLOBALS['callback_sort_by']], $b[$GLOBALS['callback_sort_by']]);
259 } // end of the 'PMA_usort_comparison_callback()' function
262 * returns array of all tables in given db or dbs
263 * this function expects unquoted names:
264 * RIGHT: my_database
265 * WRONG: `my_database`
266 * WRONG: my\_database
267 * if $tbl_is_group is true, $table is used as filter for table names
268 * if $tbl_is_group is 'comment, $table is used as filter for table comments
270 * <code>
271 * PMA_DBI_get_tables_full('my_database');
272 * PMA_DBI_get_tables_full('my_database', 'my_table'));
273 * PMA_DBI_get_tables_full('my_database', 'my_tables_', true));
274 * PMA_DBI_get_tables_full('my_database', 'my_tables_', 'comment'));
275 * </code>
277 * @todo move into PMA_Table
278 * @param string $database database
279 * @param string|false $table table
280 * @param boolean|string $tbl_is_group $table is a table group
281 * @param resource $link mysql link
282 * @param integer $limit_offset zero-based offset for the count
283 * @param boolean|integer $limit_count number of tables to return
284 * @param string $sort_by table attribute to sort by
285 * @param string $sort_order direction to sort (ASC or DESC)
286 * @return array list of tables in given db(s)
288 function PMA_DBI_get_tables_full($database, $table = false, $tbl_is_group = false, $link = null,
289 $limit_offset = 0, $limit_count = false, $sort_by = 'Name', $sort_order = 'ASC')
291 if (true === $limit_count) {
292 $limit_count = $GLOBALS['cfg']['MaxTableList'];
294 // prepare and check parameters
295 if (! is_array($database)) {
296 $databases = array($database);
297 } else {
298 $databases = $database;
301 $tables = array();
303 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
304 // get table information from information_schema
305 if ($table) {
306 if (true === $tbl_is_group) {
307 $sql_where_table = 'AND `TABLE_NAME` LIKE \''
308 . PMA_escape_mysql_wildcards(PMA_sqlAddSlashes($table)) . '%\'';
309 } elseif ('comment' === $tbl_is_group) {
310 $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
311 . PMA_escape_mysql_wildcards(PMA_sqlAddSlashes($table)) . '%\'';
312 } else {
313 $sql_where_table = 'AND `TABLE_NAME` = \'' . PMA_sqlAddSlashes($table) . '\'';
315 } else {
316 $sql_where_table = '';
319 // for PMA bc:
320 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
322 // on non-Windows servers,
323 // added BINARY in the WHERE clause to force a case sensitive
324 // comparison (if we are looking for the db Aa we don't want
325 // to find the db aa)
326 $this_databases = array_map('PMA_sqlAddSlashes', $databases);
328 $sql = '
329 SELECT *,
330 `TABLE_SCHEMA` AS `Db`,
331 `TABLE_NAME` AS `Name`,
332 `TABLE_TYPE` AS `TABLE_TYPE`,
333 `ENGINE` AS `Engine`,
334 `ENGINE` AS `Type`,
335 `VERSION` AS `Version`,
336 `ROW_FORMAT` AS `Row_format`,
337 `TABLE_ROWS` AS `Rows`,
338 `AVG_ROW_LENGTH` AS `Avg_row_length`,
339 `DATA_LENGTH` AS `Data_length`,
340 `MAX_DATA_LENGTH` AS `Max_data_length`,
341 `INDEX_LENGTH` AS `Index_length`,
342 `DATA_FREE` AS `Data_free`,
343 `AUTO_INCREMENT` AS `Auto_increment`,
344 `CREATE_TIME` AS `Create_time`,
345 `UPDATE_TIME` AS `Update_time`,
346 `CHECK_TIME` AS `Check_time`,
347 `TABLE_COLLATION` AS `Collation`,
348 `CHECKSUM` AS `Checksum`,
349 `CREATE_OPTIONS` AS `Create_options`,
350 `TABLE_COMMENT` AS `Comment`
351 FROM `information_schema`.`TABLES`
352 WHERE ' . (PMA_IS_WINDOWS ? '' : 'BINARY') . ' `TABLE_SCHEMA` IN (\'' . implode("', '", $this_databases) . '\')
353 ' . $sql_where_table;
355 // Sort the tables
356 $sql .= " ORDER BY $sort_by $sort_order";
358 if ($limit_count) {
359 $sql .= ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
362 $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
363 null, $link);
364 unset($sql_where_table, $sql);
365 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
366 // here, the array's first key is by schema name
367 foreach($tables as $one_database_name => $one_database_tables) {
368 uksort($one_database_tables, 'strnatcasecmp');
370 if ($sort_order == 'DESC') {
371 $one_database_tables = array_reverse($one_database_tables);
373 $tables[$one_database_name] = $one_database_tables;
376 } // end (get information from table schema)
378 // If permissions are wrong on even one database directory,
379 // information_schema does not return any table info for any database
380 // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002
381 if (empty($tables)) {
382 foreach ($databases as $each_database) {
383 if ($table || (true === $tbl_is_group)) {
384 $sql = 'SHOW TABLE STATUS FROM '
385 . PMA_backquote($each_database)
386 .' LIKE \'' . PMA_escape_mysql_wildcards(PMA_sqlAddSlashes($table, true)) . '%\'';
387 } else {
388 $sql = 'SHOW TABLE STATUS FROM '
389 . PMA_backquote($each_database);
392 $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
394 // Sort naturally if the config allows it and we're sorting
395 // the Name column.
396 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
397 uksort($each_tables, 'strnatcasecmp');
399 if ($sort_order == 'DESC') {
400 $each_tables = array_reverse($each_tables);
402 } else {
403 // Prepare to sort by creating array of the selected sort
404 // value to pass to array_multisort
406 // Size = Data_length + Index_length
407 if ($sort_by == 'Data_length') {
408 foreach ($each_tables as $table_name => $table_data) {
409 ${$sort_by}[$table_name] = strtolower($table_data['Data_length'] + $table_data['Index_length']);
411 } else {
412 foreach ($each_tables as $table_name => $table_data) {
413 ${$sort_by}[$table_name] = strtolower($table_data[$sort_by]);
417 if ($sort_order == 'DESC') {
418 array_multisort($$sort_by, SORT_DESC, $each_tables);
419 } else {
420 array_multisort($$sort_by, SORT_ASC, $each_tables);
423 // cleanup the temporary sort array
424 unset($$sort_by);
427 if ($limit_count) {
428 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
431 foreach ($each_tables as $table_name => $each_table) {
432 if ('comment' === $tbl_is_group
433 && 0 === strpos($each_table['Comment'], $table))
435 // remove table from list
436 unset($each_tables[$table_name]);
437 continue;
440 if (! isset($each_tables[$table_name]['Type'])
441 && isset($each_tables[$table_name]['Engine'])) {
442 // pma BC, same parts of PMA still uses 'Type'
443 $each_tables[$table_name]['Type']
444 =& $each_tables[$table_name]['Engine'];
445 } elseif (! isset($each_tables[$table_name]['Engine'])
446 && isset($each_tables[$table_name]['Type'])) {
447 // old MySQL reports Type, newer MySQL reports Engine
448 $each_tables[$table_name]['Engine']
449 =& $each_tables[$table_name]['Type'];
452 // MySQL forward compatibility
453 // so pma could use this array as if every server is of version >5.0
454 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
455 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
456 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
457 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
458 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
459 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
460 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
461 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
462 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
463 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
464 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
465 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
466 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
467 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
468 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
469 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
470 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
471 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
472 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
474 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW'
475 && $each_tables[$table_name]['Engine'] == NULL) {
476 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
477 } else {
479 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
481 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
485 $tables[$each_database] = $each_tables;
489 // cache table data
490 // so PMA_Table does not require to issue SHOW TABLE STATUS again
491 // Note: I don't see why we would need array_merge_recursive() here,
492 // as it creates double entries for the same table (for example a double
493 // entry for Comment when changing the storage engine in Operations)
494 // Note 2: Instead of array_merge(), simply use the + operator because
495 // array_merge() renumbers numeric keys starting with 0, therefore
496 // we would lose a db name thats consists only of numbers
497 foreach($tables as $one_database => $its_tables) {
498 if (isset(PMA_Table::$cache[$one_database])) {
499 PMA_Table::$cache[$one_database] = PMA_Table::$cache[$one_database] + $tables[$one_database];
500 } else {
501 PMA_Table::$cache[$one_database] = $tables[$one_database];
504 unset($one_database, $its_tables);
506 if (! is_array($database)) {
507 if (isset($tables[$database])) {
508 return $tables[$database];
509 } elseif (isset($tables[strtolower($database)])) {
510 // on windows with lower_case_table_names = 1
511 // MySQL returns
512 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
513 // but information_schema.TABLES gives `test`
514 // bug #1436171
515 // http://sf.net/support/tracker.php?aid=1436171
516 return $tables[strtolower($database)];
517 } else {
518 return $tables;
520 } else {
521 return $tables;
526 * returns array with databases containing extended infos about them
528 * @todo move into PMA_List_Database?
529 * @param string $database database
530 * @param boolean $force_stats retrieve stats also for MySQL < 5
531 * @param resource $link mysql link
532 * @param string $sort_by column to order by
533 * @param string $sort_order ASC or DESC
534 * @param integer $limit_offset starting offset for LIMIT
535 * @param bool|int $limit_count row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
536 * @return array $databases
538 function PMA_DBI_get_databases_full($database = null, $force_stats = false,
539 $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
540 $limit_offset = 0, $limit_count = false)
542 $sort_order = strtoupper($sort_order);
544 if (true === $limit_count) {
545 $limit_count = $GLOBALS['cfg']['MaxDbList'];
548 // initialize to avoid errors when there are no databases
549 $databases = array();
551 $apply_limit_and_order_manual = true;
553 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
555 * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
556 * cause MySQL does not support natural ordering, we have to do it afterward
558 $limit = '';
559 if (!$GLOBALS['cfg']['NaturalOrder']) {
560 if ($limit_count) {
561 $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
564 $apply_limit_and_order_manual = false;
567 // get table information from information_schema
568 if ($database) {
569 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
570 . PMA_sqlAddSlashes($database) . '\'';
571 } else {
572 $sql_where_schema = '';
575 // for PMA bc:
576 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
577 $sql = '
578 SELECT `information_schema`.`SCHEMATA`.*';
579 if ($force_stats) {
580 $sql .= ',
581 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
582 AS `SCHEMA_TABLES`,
583 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
584 AS `SCHEMA_TABLE_ROWS`,
585 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
586 AS `SCHEMA_DATA_LENGTH`,
587 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
588 AS `SCHEMA_MAX_DATA_LENGTH`,
589 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
590 AS `SCHEMA_INDEX_LENGTH`,
591 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
592 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
593 AS `SCHEMA_LENGTH`,
594 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
595 AS `SCHEMA_DATA_FREE`';
597 $sql .= '
598 FROM `information_schema`.`SCHEMATA`';
599 if ($force_stats) {
600 $sql .= '
601 LEFT JOIN `information_schema`.`TABLES`
602 ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
603 = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
605 $sql .= '
606 ' . $sql_where_schema . '
607 GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
608 ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
609 . $limit;
610 $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
612 $mysql_error = PMA_DBI_getError($link);
613 if (! count($databases) && $GLOBALS['errno']) {
614 PMA_mysqlDie($mysql_error, $sql);
617 // display only databases also in official database list
618 // f.e. to apply hide_db and only_db
619 $drops = array_diff(array_keys($databases), (array) $GLOBALS['pma']->databases);
620 if (count($drops)) {
621 foreach ($drops as $drop) {
622 unset($databases[$drop]);
624 unset($drop);
626 unset($sql_where_schema, $sql, $drops);
627 } else {
628 foreach ($GLOBALS['pma']->databases as $database_name) {
629 // MySQL forward compatibility
630 // so pma could use this array as if every server is of version >5.0
631 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
633 if ($force_stats) {
634 require_once './libraries/mysql_charsets.lib.php';
636 $databases[$database_name]['DEFAULT_COLLATION_NAME']
637 = PMA_getDbCollation($database_name);
639 // get additional info about tables
640 $databases[$database_name]['SCHEMA_TABLES'] = 0;
641 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
642 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
643 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
644 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
645 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
646 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
648 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($database_name) . ';');
649 while ($row = PMA_DBI_fetch_assoc($res)) {
650 $databases[$database_name]['SCHEMA_TABLES']++;
651 $databases[$database_name]['SCHEMA_TABLE_ROWS']
652 += $row['Rows'];
653 $databases[$database_name]['SCHEMA_DATA_LENGTH']
654 += $row['Data_length'];
655 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
656 += $row['Max_data_length'];
657 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
658 += $row['Index_length'];
660 // for InnoDB, this does not contain the number of
661 // overhead bytes but the total free space
662 if ('InnoDB' != $row['Engine']) {
663 $databases[$database_name]['SCHEMA_DATA_FREE']
664 += $row['Data_free'];
666 $databases[$database_name]['SCHEMA_LENGTH']
667 += $row['Data_length'] + $row['Index_length'];
669 PMA_DBI_free_result($res);
670 unset($res);
677 * apply limit and order manually now
678 * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
680 if ($apply_limit_and_order_manual) {
681 $GLOBALS['callback_sort_order'] = $sort_order;
682 $GLOBALS['callback_sort_by'] = $sort_by;
683 usort($databases, 'PMA_usort_comparison_callback');
684 unset($GLOBALS['callback_sort_order'], $GLOBALS['callback_sort_by']);
687 * now apply limit
689 if ($limit_count) {
690 $databases = array_slice($databases, $limit_offset, $limit_count);
694 return $databases;
698 * returns detailed array with all columns for given table in database,
699 * or all tables/databases
701 * @param string $database name of database
702 * @param string $table name of table to retrieve columns from
703 * @param string $column name of specific column
704 * @param mixed $link mysql link resource
705 * @return array
707 function PMA_DBI_get_columns_full($database = null, $table = null,
708 $column = null, $link = null)
710 $columns = array();
712 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
713 $sql_wheres = array();
714 $array_keys = array();
716 // get columns information from information_schema
717 if (null !== $database) {
718 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . PMA_sqlAddSlashes($database) . '\' ';
719 } else {
720 $array_keys[] = 'TABLE_SCHEMA';
722 if (null !== $table) {
723 $sql_wheres[] = '`TABLE_NAME` = \'' . PMA_sqlAddSlashes($table) . '\' ';
724 } else {
725 $array_keys[] = 'TABLE_NAME';
727 if (null !== $column) {
728 $sql_wheres[] = '`COLUMN_NAME` = \'' . PMA_sqlAddSlashes($column) . '\' ';
729 } else {
730 $array_keys[] = 'COLUMN_NAME';
733 // for PMA bc:
734 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
735 $sql = '
736 SELECT *,
737 `COLUMN_NAME` AS `Field`,
738 `COLUMN_TYPE` AS `Type`,
739 `COLLATION_NAME` AS `Collation`,
740 `IS_NULLABLE` AS `Null`,
741 `COLUMN_KEY` AS `Key`,
742 `COLUMN_DEFAULT` AS `Default`,
743 `EXTRA` AS `Extra`,
744 `PRIVILEGES` AS `Privileges`,
745 `COLUMN_COMMENT` AS `Comment`
746 FROM `information_schema`.`COLUMNS`';
747 if (count($sql_wheres)) {
748 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
751 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
752 unset($sql_wheres, $sql);
753 } else {
754 if (null === $database) {
755 foreach ($GLOBALS['pma']->databases as $database) {
756 $columns[$database] = PMA_DBI_get_columns_full($database, null,
757 null, $link);
759 return $columns;
760 } elseif (null === $table) {
761 $tables = PMA_DBI_get_tables($database);
762 foreach ($tables as $table) {
763 $columns[$table] = PMA_DBI_get_columns_full(
764 $database, $table, null, $link);
766 return $columns;
769 $sql = 'SHOW FULL COLUMNS FROM '
770 . PMA_backquote($database) . '.' . PMA_backquote($table);
771 if (null !== $column) {
772 $sql .= " LIKE '" . $column . "'";
775 $columns = PMA_DBI_fetch_result($sql, 'Field', null, $link);
777 $ordinal_position = 1;
778 foreach ($columns as $column_name => $each_column) {
780 // MySQL forward compatibility
781 // so pma could use this array as if every server is of version >5.0
782 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
783 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
784 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
785 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
786 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
787 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
788 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
789 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
790 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
792 $columns[$column_name]['TABLE_CATALOG'] = null;
793 $columns[$column_name]['TABLE_SCHEMA'] = $database;
794 $columns[$column_name]['TABLE_NAME'] = $table;
795 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
796 $columns[$column_name]['DATA_TYPE'] =
797 substr($columns[$column_name]['COLUMN_TYPE'], 0,
798 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
800 * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
802 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
804 * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
806 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
807 $columns[$column_name]['NUMERIC_PRECISION'] = null;
808 $columns[$column_name]['NUMERIC_SCALE'] = null;
809 $columns[$column_name]['CHARACTER_SET_NAME'] =
810 substr($columns[$column_name]['COLLATION_NAME'], 0,
811 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
813 $ordinal_position++;
816 if (null !== $column) {
817 reset($columns);
818 $columns = current($columns);
822 return $columns;
826 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
828 * @param string $database name of database
829 * @param string $table name of table to retrieve columns from
830 * @param boolean $full whether to return full info or only column names
831 * @param mixed $link mysql link resource
832 * @return array column names
834 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
836 $fields = PMA_DBI_fetch_result(
837 'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
838 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
839 'Field', null, $link);
840 if (! is_array($fields) || count($fields) < 1) {
841 return false;
843 return $fields;
847 * returns value of given mysql server variable
849 * @param string $var mysql server variable name
850 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
851 * @param mixed $link mysql link resource|object
852 * @return mixed value for mysql server variable
856 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
858 if ($link === null) {
859 if (isset($GLOBALS['userlink'])) {
860 $link = $GLOBALS['userlink'];
861 } else {
862 return false;
866 switch ($type) {
867 case PMA_DBI_GETVAR_SESSION:
868 $modifier = ' SESSION';
869 break;
870 case PMA_DBI_GETVAR_GLOBAL:
871 $modifier = ' GLOBAL';
872 break;
873 default:
874 $modifier = '';
876 return PMA_DBI_fetch_value(
877 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
881 * Function called just after a connection to the MySQL database server has been established
882 * It sets the connection collation, and determins the version of MySQL which is running.
884 * @param mixed $link mysql link resource|object
885 * @param boolean $is_controluser
887 function PMA_DBI_postConnect($link, $is_controluser = false)
889 if (! defined('PMA_MYSQL_INT_VERSION')) {
890 if (PMA_cacheExists('PMA_MYSQL_INT_VERSION', true)) {
891 define('PMA_MYSQL_INT_VERSION', PMA_cacheGet('PMA_MYSQL_INT_VERSION', true));
892 define('PMA_MYSQL_MAJOR_VERSION', PMA_cacheGet('PMA_MYSQL_MAJOR_VERSION', true));
893 define('PMA_MYSQL_STR_VERSION', PMA_cacheGet('PMA_MYSQL_STR_VERSION', true));
894 } else {
895 $mysql_version = PMA_DBI_fetch_value(
896 'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE);
897 if ($mysql_version) {
898 $match = explode('.', $mysql_version);
899 define('PMA_MYSQL_MAJOR_VERSION', (int)$match[0]);
900 define('PMA_MYSQL_INT_VERSION',
901 (int) sprintf('%d%02d%02d', $match[0], $match[1],
902 intval($match[2])));
903 define('PMA_MYSQL_STR_VERSION', $mysql_version);
904 unset($mysql_version, $match);
905 } else {
906 define('PMA_MYSQL_INT_VERSION', 50015);
907 define('PMA_MYSQL_MAJOR_VERSION', 5);
908 define('PMA_MYSQL_STR_VERSION', '5.00.15');
910 PMA_cacheSet('PMA_MYSQL_INT_VERSION', PMA_MYSQL_INT_VERSION, true);
911 PMA_cacheSet('PMA_MYSQL_MAJOR_VERSION', PMA_MYSQL_MAJOR_VERSION, true);
912 PMA_cacheSet('PMA_MYSQL_STR_VERSION', PMA_MYSQL_STR_VERSION, true);
914 // detect Drizzle by version number (year.month.day.?patch-version)
915 define('PMA_DRIZZLE', PMA_MYSQL_MAJOR_VERSION >= 2009);
918 // Skip charsets for Drizzle
919 if (!PMA_DRIZZLE) {
920 if (! empty($GLOBALS['collation_connection'])) {
921 PMA_DBI_query("SET CHARACTER SET 'utf8';", $link, PMA_DBI_QUERY_STORE);
922 PMA_DBI_query("SET collation_connection = '" . PMA_sqlAddSlashes($GLOBALS['collation_connection']) . "';", $link, PMA_DBI_QUERY_STORE);
923 } else {
924 PMA_DBI_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci';", $link, PMA_DBI_QUERY_STORE);
930 * returns a single value from the given result or query,
931 * if the query or the result has more than one row or field
932 * the first field of the first row is returned
934 * <code>
935 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
936 * $user_name = PMA_DBI_fetch_value($sql);
937 * // produces
938 * // $user_name = 'John Doe'
939 * </code>
941 * @param string|mysql_result $result query or mysql result
942 * @param integer $row_number row to fetch the value from,
943 * starting at 0, with 0 beeing default
944 * @param integer|string $field field to fetch the value from,
945 * starting at 0, with 0 beeing default
946 * @param resource $link mysql link
947 * @param mixed $options
948 * @return mixed value of first field in first row from result
949 * or false if not found
951 function PMA_DBI_fetch_value($result, $row_number = 0, $field = 0, $link = null, $options = 0) {
952 $value = false;
954 if (is_string($result)) {
955 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
958 // return false if result is empty or false
959 // or requested row is larger than rows in result
960 if (PMA_DBI_num_rows($result) < ($row_number + 1)) {
961 return $value;
964 // if $field is an integer use non associative mysql fetch function
965 if (is_int($field)) {
966 $fetch_function = 'PMA_DBI_fetch_row';
967 } else {
968 $fetch_function = 'PMA_DBI_fetch_assoc';
971 // get requested row
972 for ($i = 0; $i <= $row_number; $i++) {
973 $row = $fetch_function($result);
975 PMA_DBI_free_result($result);
977 // return requested field
978 if (isset($row[$field])) {
979 $value = $row[$field];
981 unset($row);
983 return $value;
987 * returns only the first row from the result
989 * <code>
990 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
991 * $user = PMA_DBI_fetch_single_row($sql);
992 * // produces
993 * // $user = array('id' => 123, 'name' => 'John Doe')
994 * </code>
996 * @param string|mysql_result $result query or mysql result
997 * @param string $type NUM|ASSOC|BOTH
998 * returned array should either numeric
999 * associativ or booth
1000 * @param resource $link mysql link
1001 * @param mixed $options
1002 * @return array|boolean first row from result
1003 * or false if result is empty
1005 function PMA_DBI_fetch_single_row($result, $type = 'ASSOC', $link = null, $options = 0) {
1006 if (is_string($result)) {
1007 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1010 // return null if result is empty or false
1011 if (! PMA_DBI_num_rows($result)) {
1012 return false;
1015 switch ($type) {
1016 case 'NUM' :
1017 $fetch_function = 'PMA_DBI_fetch_row';
1018 break;
1019 case 'ASSOC' :
1020 $fetch_function = 'PMA_DBI_fetch_assoc';
1021 break;
1022 case 'BOTH' :
1023 default :
1024 $fetch_function = 'PMA_DBI_fetch_array';
1025 break;
1028 $row = $fetch_function($result);
1029 PMA_DBI_free_result($result);
1030 return $row;
1034 * returns all rows in the resultset in one array
1036 * <code>
1037 * $sql = 'SELECT * FROM `user`';
1038 * $users = PMA_DBI_fetch_result($sql);
1039 * // produces
1040 * // $users[] = array('id' => 123, 'name' => 'John Doe')
1042 * $sql = 'SELECT `id`, `name` FROM `user`';
1043 * $users = PMA_DBI_fetch_result($sql, 'id');
1044 * // produces
1045 * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
1047 * $sql = 'SELECT `id`, `name` FROM `user`';
1048 * $users = PMA_DBI_fetch_result($sql, 0);
1049 * // produces
1050 * // $users['123'] = array(0 => 123, 1 => 'John Doe')
1052 * $sql = 'SELECT `id`, `name` FROM `user`';
1053 * $users = PMA_DBI_fetch_result($sql, 'id', 'name');
1054 * // or
1055 * $users = PMA_DBI_fetch_result($sql, 0, 1);
1056 * // produces
1057 * // $users['123'] = 'John Doe'
1059 * $sql = 'SELECT `name` FROM `user`';
1060 * $users = PMA_DBI_fetch_result($sql);
1061 * // produces
1062 * // $users[] = 'John Doe'
1064 * $sql = 'SELECT `group`, `name` FROM `user`'
1065 * $users = PMA_DBI_fetch_result($sql, array('group', null), 'name');
1066 * // produces
1067 * // $users['admin'][] = 'John Doe'
1069 * $sql = 'SELECT `group`, `name` FROM `user`'
1070 * $users = PMA_DBI_fetch_result($sql, array('group', 'name'), 'id');
1071 * // produces
1072 * // $users['admin']['John Doe'] = '123'
1073 * </code>
1075 * @param string|mysql_result $result query or mysql result
1076 * @param string|integer $key field-name or offset
1077 * used as key for array
1078 * @param string|integer $value value-name or offset
1079 * used as value for array
1080 * @param resource $link mysql link
1081 * @param mixed $options
1082 * @return array resultrows or values indexed by $key
1084 function PMA_DBI_fetch_result($result, $key = null, $value = null,
1085 $link = null, $options = 0)
1087 $resultrows = array();
1089 if (is_string($result)) {
1090 $result = PMA_DBI_try_query($result, $link, $options);
1093 // return empty array if result is empty or false
1094 if (! $result) {
1095 return $resultrows;
1098 $fetch_function = 'PMA_DBI_fetch_assoc';
1100 // no nested array if only one field is in result
1101 if (null === $key && 1 === PMA_DBI_num_fields($result)) {
1102 $value = 0;
1103 $fetch_function = 'PMA_DBI_fetch_row';
1106 // if $key is an integer use non associative mysql fetch function
1107 if (is_int($key)) {
1108 $fetch_function = 'PMA_DBI_fetch_row';
1111 if (null === $key && null === $value) {
1112 while ($row = $fetch_function($result)) {
1113 $resultrows[] = $row;
1115 } elseif (null === $key) {
1116 while ($row = $fetch_function($result)) {
1117 $resultrows[] = $row[$value];
1119 } elseif (null === $value) {
1120 if (is_array($key)) {
1121 while ($row = $fetch_function($result)) {
1122 $result_target =& $resultrows;
1123 foreach ($key as $key_index) {
1124 if (null === $key_index) {
1125 $result_target =& $result_target[];
1126 continue;
1129 if (! isset($result_target[$row[$key_index]])) {
1130 $result_target[$row[$key_index]] = array();
1132 $result_target =& $result_target[$row[$key_index]];
1134 $result_target = $row;
1136 } else {
1137 while ($row = $fetch_function($result)) {
1138 $resultrows[$row[$key]] = $row;
1141 } else {
1142 if (is_array($key)) {
1143 while ($row = $fetch_function($result)) {
1144 $result_target =& $resultrows;
1145 foreach ($key as $key_index) {
1146 if (null === $key_index) {
1147 $result_target =& $result_target[];
1148 continue;
1151 if (! isset($result_target[$row[$key_index]])) {
1152 $result_target[$row[$key_index]] = array();
1154 $result_target =& $result_target[$row[$key_index]];
1156 $result_target = $row[$value];
1158 } else {
1159 while ($row = $fetch_function($result)) {
1160 $resultrows[$row[$key]] = $row[$value];
1165 PMA_DBI_free_result($result);
1166 return $resultrows;
1170 * return default table engine for given database
1172 * @return string default table engine
1174 function PMA_DBI_get_default_engine()
1176 return PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'storage_engine\';', 0, 1);
1180 * Get supported SQL compatibility modes
1182 * @return array supported SQL compatibility modes
1184 function PMA_DBI_getCompatibilities()
1186 $compats = array('NONE');
1187 $compats[] = 'ANSI';
1188 $compats[] = 'DB2';
1189 $compats[] = 'MAXDB';
1190 $compats[] = 'MYSQL323';
1191 $compats[] = 'MYSQL40';
1192 $compats[] = 'MSSQL';
1193 $compats[] = 'ORACLE';
1194 // removed; in MySQL 5.0.33, this produces exports that
1195 // can't be read by POSTGRESQL (see our bug #1596328)
1196 //$compats[] = 'POSTGRESQL';
1197 $compats[] = 'TRADITIONAL';
1199 return $compats;
1203 * returns warnings for last query
1205 * @param resource $link mysql link resource
1206 * @return array warnings
1208 function PMA_DBI_get_warnings($link = null)
1210 if (empty($link)) {
1211 if (isset($GLOBALS['userlink'])) {
1212 $link = $GLOBALS['userlink'];
1213 } else {
1214 return array();
1218 return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1222 * returns true (int > 0) if current user is superuser
1223 * otherwise 0
1225 * @return integer $is_superuser
1227 function PMA_isSuperuser()
1229 if (PMA_cacheExists('is_superuser', true)) {
1230 return PMA_cacheGet('is_superuser', true);
1233 // with mysql extension, when connection failed we don't have
1234 // a $userlink
1235 if (isset($GLOBALS['userlink'])) {
1236 $r = (bool) PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user', $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
1237 PMA_cacheSet('is_superuser', $r, true);
1238 } else {
1239 PMA_cacheSet('is_superuser', false, true);
1242 return PMA_cacheGet('is_superuser', true);
1246 * returns an array of PROCEDURE or FUNCTION names for a db
1248 * @param string $db db name
1249 * @param string $which PROCEDURE | FUNCTION
1250 * @param resource $link mysql link
1252 * @return array the procedure names or function names
1254 function PMA_DBI_get_procedures_or_functions($db, $which, $link = null)
1256 $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1257 $result = array();
1258 foreach ($shows as $one_show) {
1259 if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1260 $result[] = $one_show['Name'];
1263 return($result);
1267 * returns the definition of a specific PROCEDURE, FUNCTION or EVENT
1269 * @param string $db db name
1270 * @param string $which PROCEDURE | FUNCTION | EVENT
1271 * @param string $name the procedure|function|event name
1272 * @param resource $link mysql link
1274 * @return string the definition
1276 function PMA_DBI_get_definition($db, $which, $name, $link = null)
1278 $returned_field = array(
1279 'PROCEDURE' => 'Create Procedure',
1280 'FUNCTION' => 'Create Function',
1281 'EVENT' => 'Create Event'
1283 $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($name);
1284 return(PMA_DBI_fetch_value($query, 0, $returned_field[$which]));
1288 * returns details about the TRIGGERs for a specific table or database
1290 * @param string $db db name
1291 * @param string $table table name
1292 * @param string $delimiter the delimiter to use (may be empty)
1294 * @return array information about triggers (may be empty)
1296 function PMA_DBI_get_triggers($db, $table = '', $delimiter = '//')
1298 $result = array();
1299 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1300 // Note: in http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
1301 // their example uses WHERE TRIGGER_SCHEMA='dbname' so let's use this
1302 // instead of WHERE EVENT_OBJECT_SCHEMA='dbname'
1303 $query = "SELECT TRIGGER_SCHEMA, TRIGGER_NAME, EVENT_MANIPULATION, EVENT_OBJECT_TABLE, ACTION_TIMING, ACTION_STATEMENT, EVENT_OBJECT_SCHEMA, EVENT_OBJECT_TABLE FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA= '" . PMA_sqlAddSlashes($db,true) . "';";
1304 if (! empty($table)) {
1305 $query .= " AND EVENT_OBJECT_TABLE = '" . PMA_sqlAddSlashes($table, true) . "';";
1307 } else {
1308 $query = "SHOW TRIGGERS FROM " . PMA_backquote(PMA_sqlAddSlashes($db,true));
1309 if (! empty($table)) {
1310 $query .= " LIKE '" . PMA_sqlAddSlashes($table, true) . "';";
1314 if ($triggers = PMA_DBI_fetch_result($query)) {
1315 foreach ($triggers as $trigger) {
1316 if ($GLOBALS['cfg']['Server']['DisableIS']) {
1317 $trigger['TRIGGER_NAME'] = $trigger['Trigger'];
1318 $trigger['ACTION_TIMING'] = $trigger['Timing'];
1319 $trigger['EVENT_MANIPULATION'] = $trigger['Event'];
1320 $trigger['EVENT_OBJECT_TABLE'] = $trigger['Table'];
1321 $trigger['ACTION_STATEMENT'] = $trigger['Statement'];
1323 $one_result = array();
1324 $one_result['name'] = $trigger['TRIGGER_NAME'];
1325 $one_result['table'] = $trigger['EVENT_OBJECT_TABLE'];
1326 $one_result['action_timing'] = $trigger['ACTION_TIMING'];
1327 $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
1329 // do not prepend the schema name; this way, importing the
1330 // definition into another schema will work
1331 $one_result['full_trigger_name'] = PMA_backquote($trigger['TRIGGER_NAME']);
1332 $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
1333 $one_result['create'] = 'CREATE TRIGGER ' . $one_result['full_trigger_name'] . ' ' . $trigger['ACTION_TIMING']. ' ' . $trigger['EVENT_MANIPULATION'] . ' ON ' . PMA_backquote($trigger['EVENT_OBJECT_TABLE']) . "\n" . ' FOR EACH ROW ' . $trigger['ACTION_STATEMENT'] . "\n" . $delimiter . "\n";
1335 $result[] = $one_result;
1338 return($result);
1342 * Returns true if $db.$view_name is a view, false if not
1344 * @param string $db database name
1345 * @param string $view_name view/table name
1347 * @return bool true if $db.$view_name is a view, false if not
1349 function PMA_isView($db, $view_name)
1351 $result = PMA_DBI_fetch_result(
1352 "SELECT TABLE_NAME
1353 FROM information_schema.VIEWS
1354 WHERE TABLE_SCHEMA = '" . PMA_sqlAddSlashes($db) . "'
1355 AND TABLE_NAME = '" . PMA_sqlAddSlashes($view_name) . "'");
1357 if ($result) {
1358 return true;
1359 } else {
1360 return false;