bug 2941037 - Database structure not sorted by table correctly
[phpmyadmin/madhuracj.git] / libraries / database_interface.lib.php
blob0dab59013c6ae09fe2a34a2d2daf2c1a9c88f670
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Common Option Constants For DBI Functions
6 * @version $Id$
7 * @package phpMyAdmin
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 // PMA_DBI_try_query()
17 define('PMA_DBI_QUERY_STORE', 1); // Force STORE_RESULT method, ignored by classic MySQL.
18 define('PMA_DBI_QUERY_UNBUFFERED', 2); // Do not read whole query
19 // PMA_DBI_get_variable()
20 define('PMA_DBI_GETVAR_SESSION', 1);
21 define('PMA_DBI_GETVAR_GLOBAL', 2);
23 /**
24 * Checks one of the mysql extensions
26 * @param string $extension mysql extension to check
28 function PMA_DBI_checkMysqlExtension($extension = 'mysql') {
29 if (! function_exists($extension . '_connect')) {
30 return false;
33 return true;
36 /**
37 * check for requested extension
39 if (! PMA_DBI_checkMysqlExtension($GLOBALS['cfg']['Server']['extension'])) {
41 // if it fails try alternative extension ...
42 // and display an error ...
44 /**
45 * @todo add different messages for alternative extension
46 * and complete fail (no alternative extension too)
48 $error =
49 sprintf(PMA_sanitize($GLOBALS['strCantLoad']),
50 $GLOBALS['cfg']['Server']['extension'])
51 .' - <a href="./Documentation.html#faqmysql" target="documentation">'
52 .$GLOBALS['strDocu'] . '</a>';
53 trigger_error($error, E_USER_ERROR);
55 if ($GLOBALS['cfg']['Server']['extension'] === 'mysql') {
56 $alternativ_extension = 'mysqli';
57 } else {
58 $alternativ_extension = 'mysql';
61 if (! PMA_DBI_checkMysqlExtension($alternativ_extension)) {
62 // if alternative fails too ...
63 PMA_fatalError(
64 sprintf($GLOBALS['strCantLoad'],
65 $GLOBALS['cfg']['Server']['extension'])
66 . ' - [a@./Documentation.html#faqmysql@documentation]'
67 . $GLOBALS['strDocu'] . '[/a]');
70 $GLOBALS['cfg']['Server']['extension'] = $alternativ_extension;
71 unset($alternativ_extension);
74 /**
75 * Including The DBI Plugin
77 require_once './libraries/dbi/' . $GLOBALS['cfg']['Server']['extension'] . '.dbi.lib.php';
79 /**
80 * Common Functions
82 function PMA_DBI_query($query, $link = null, $options = 0) {
83 $res = PMA_DBI_try_query($query, $link, $options)
84 or PMA_mysqlDie(PMA_DBI_getError($link), $query);
85 return $res;
88 /**
89 * converts charset of a mysql message, usually coming from mysql_error(),
90 * into PMA charset, usally UTF-8
91 * uses language to charset mapping from mysql/share/errmsg.txt
92 * and charset names to ISO charset from information_schema.CHARACTER_SETS
94 * @uses $GLOBALS['cfg']['IconvExtraParams']
95 * @uses $GLOBALS['charset'] as target charset
96 * @uses PMA_DBI_fetch_value() to get server_language
97 * @uses preg_match() to filter server_language
98 * @uses in_array()
99 * @uses function_exists() to check for a convert function
100 * @uses iconv() to convert message
101 * @uses libiconv() to convert message
102 * @uses recode_string() to convert message
103 * @uses mb_convert_encoding() to convert message
104 * @param string $message
105 * @return string $message
107 function PMA_DBI_convert_message($message) {
108 // latin always last!
109 $encodings = array(
110 'japanese' => 'EUC-JP', //'ujis',
111 'japanese-sjis' => 'Shift-JIS', //'sjis',
112 'korean' => 'EUC-KR', //'euckr',
113 'russian' => 'KOI8-R', //'koi8r',
114 'ukrainian' => 'KOI8-U', //'koi8u',
115 'greek' => 'ISO-8859-7', //'greek',
116 'serbian' => 'CP1250', //'cp1250',
117 'estonian' => 'ISO-8859-13', //'latin7',
118 'slovak' => 'ISO-8859-2', //'latin2',
119 'czech' => 'ISO-8859-2', //'latin2',
120 'hungarian' => 'ISO-8859-2', //'latin2',
121 'polish' => 'ISO-8859-2', //'latin2',
122 'romanian' => 'ISO-8859-2', //'latin2',
123 'spanish' => 'CP1252', //'latin1',
124 'swedish' => 'CP1252', //'latin1',
125 'italian' => 'CP1252', //'latin1',
126 'norwegian-ny' => 'CP1252', //'latin1',
127 'norwegian' => 'CP1252', //'latin1',
128 'portuguese' => 'CP1252', //'latin1',
129 'danish' => 'CP1252', //'latin1',
130 'dutch' => 'CP1252', //'latin1',
131 'english' => 'CP1252', //'latin1',
132 'french' => 'CP1252', //'latin1',
133 'german' => 'CP1252', //'latin1',
136 if ($server_language = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'language\';', 0, 1)) {
137 $found = array();
138 if (preg_match('&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $server_language, $found)) {
139 $server_language = $found[1];
143 if (! empty($server_language) && isset($encodings[$server_language])) {
144 if (function_exists('iconv')) {
145 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
146 require_once './libraries/iconv_wrapper.lib.php';
147 $message = PMA_aix_iconv_wrapper($encodings[$server_language],
148 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
149 } else {
150 $message = iconv($encodings[$server_language],
151 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
153 } elseif (function_exists('recode_string')) {
154 $message = recode_string($encodings[$server_language] . '..' . $GLOBALS['charset'],
155 $message);
156 } elseif (function_exists('libiconv')) {
157 $message = libiconv($encodings[$server_language], $GLOBALS['charset'], $message);
158 } elseif (function_exists('mb_convert_encoding')) {
159 // do not try unsupported charsets
160 if (! in_array($server_language, array('ukrainian', 'greek', 'serbian'))) {
161 $message = mb_convert_encoding($message, $GLOBALS['charset'],
162 $encodings[$server_language]);
165 } else {
167 * @todo lang not found, try all, what TODO ?
171 return $message;
175 * returns array with table names for given db
177 * @param string $database name of database
178 * @param mixed $link mysql link resource|object
179 * @return array tables names
181 function PMA_DBI_get_tables($database, $link = null)
183 return PMA_DBI_fetch_result('SHOW TABLES FROM ' . PMA_backquote($database) . ';',
184 null, 0, $link, PMA_DBI_QUERY_STORE);
188 * usort comparison callback
190 * @param string $a first argument to sort
191 * @param string $b second argument to sort
193 * @return integer a value representing whether $a should be before $b in the
194 * sorted array or not
196 * @global string the column the array shall be sorted by
197 * @global string the sorting order ('ASC' or 'DESC')
199 * @access private
201 function PMA_usort_comparison_callback($a, $b)
203 if ($GLOBALS['cfg']['NaturalOrder']) {
204 $sorter = 'strnatcasecmp';
205 } else {
206 $sorter = 'strcasecmp';
208 // produces f.e.:
209 // return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
210 return ($GLOBALS['callback_sort_order'] == 'ASC' ? 1 : -1) * $sorter($a[$GLOBALS['callback_sort_by']], $b[$GLOBALS['callback_sort_by']]);
211 } // end of the 'PMA_usort_comparison_callback()' function
214 * returns array of all tables in given db or dbs
215 * this function expects unquoted names:
216 * RIGHT: my_database
217 * WRONG: `my_database`
218 * WRONG: my\_database
219 * if $tbl_is_group is true, $table is used as filter for table names
220 * if $tbl_is_group is 'comment, $table is used as filter for table comments
222 * <code>
223 * PMA_DBI_get_tables_full('my_database');
224 * PMA_DBI_get_tables_full('my_database', 'my_table'));
225 * PMA_DBI_get_tables_full('my_database', 'my_tables_', true));
226 * PMA_DBI_get_tables_full('my_database', 'my_tables_', 'comment'));
227 * </code>
229 * @todo move into PMA_Table
230 * @uses PMA_DBI_fetch_result()
231 * @uses PMA_escape_mysql_wildcards()
232 * @uses PMA_backquote()
233 * @uses is_array()
234 * @uses addslashes()
235 * @uses strpos()
236 * @uses strtoupper()
237 * @param string $databases database
238 * @param string $table table
239 * @param boolean|string $tbl_is_group $table is a table group
240 * @param resource $link mysql link
241 * @param integer $limit_offset zero-based offset for the count
242 * @param boolean|integer $limit_count number of tables to return
243 * @param string $sort_by table attribute to sort by
244 * @param string $sort_order direction to sort (ASC or DESC)
245 * @return array list of tables in given db(s)
247 function PMA_DBI_get_tables_full($database, $table = false, $tbl_is_group = false, $link = null,
248 $limit_offset = 0, $limit_count = false, $sort_by = 'Name', $sort_order = 'ASC')
250 require_once './libraries/Table.class.php';
252 if (true === $limit_count) {
253 $limit_count = $GLOBALS['cfg']['MaxTableList'];
255 // prepare and check parameters
256 if (! is_array($database)) {
257 $databases = array($database);
258 } else {
259 $databases = $database;
262 $tables = array();
264 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
265 // get table information from information_schema
266 if ($table) {
267 if (true === $tbl_is_group) {
268 $sql_where_table = 'AND `TABLE_NAME` LIKE \''
269 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
270 } elseif ('comment' === $tbl_is_group) {
271 $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
272 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
273 } else {
274 $sql_where_table = 'AND `TABLE_NAME` = \'' . addslashes($table) . '\'';
276 } else {
277 $sql_where_table = '';
280 // for PMA bc:
281 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
283 // on non-Windows servers,
284 // added BINARY in the WHERE clause to force a case sensitive
285 // comparison (if we are looking for the db Aa we don't want
286 // to find the db aa)
287 $this_databases = array_map('PMA_sqlAddslashes', $databases);
289 $sql = '
290 SELECT *,
291 `TABLE_SCHEMA` AS `Db`,
292 `TABLE_NAME` AS `Name`,
293 `ENGINE` AS `Engine`,
294 `ENGINE` AS `Type`,
295 `VERSION` AS `Version`,
296 `ROW_FORMAT` AS `Row_format`,
297 `TABLE_ROWS` AS `Rows`,
298 `AVG_ROW_LENGTH` AS `Avg_row_length`,
299 `DATA_LENGTH` AS `Data_length`,
300 `MAX_DATA_LENGTH` AS `Max_data_length`,
301 `INDEX_LENGTH` AS `Index_length`,
302 `DATA_FREE` AS `Data_free`,
303 `AUTO_INCREMENT` AS `Auto_increment`,
304 `CREATE_TIME` AS `Create_time`,
305 `UPDATE_TIME` AS `Update_time`,
306 `CHECK_TIME` AS `Check_time`,
307 `TABLE_COLLATION` AS `Collation`,
308 `CHECKSUM` AS `Checksum`,
309 `CREATE_OPTIONS` AS `Create_options`,
310 `TABLE_COMMENT` AS `Comment`
311 FROM `information_schema`.`TABLES`
312 WHERE ' . (PMA_IS_WINDOWS ? '' : 'BINARY') . ' `TABLE_SCHEMA` IN (\'' . implode("', '", $this_databases) . '\')
313 ' . $sql_where_table;
315 // Sort the tables
316 $sql .= " ORDER BY $sort_by $sort_order";
318 if ($limit_count) {
319 $sql .= ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
322 $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
323 null, $link);
324 unset($sql_where_table, $sql);
325 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
326 // here, the array's first key is by schema name
327 foreach($tables as $one_database_name => $one_database_tables) {
328 uksort($one_database_tables, 'strnatcasecmp');
330 if ($sort_order == 'DESC') {
331 $one_database_tables = array_reverse($one_database_tables);
333 $tables[$one_database_name] = $one_database_tables;
336 } // end (get information from table schema)
338 // If permissions are wrong on even one database directory,
339 // information_schema does not return any table info for any database
340 // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002
341 if (empty($tables)) {
342 foreach ($databases as $each_database) {
343 if ($table || (true === $tbl_is_group)) {
344 $sql = 'SHOW TABLE STATUS FROM '
345 . PMA_backquote($each_database)
346 .' LIKE \'' . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
347 } else {
348 $sql = 'SHOW TABLE STATUS FROM '
349 . PMA_backquote($each_database);
352 $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
354 // Sort naturally if the config allows it and we're sorting
355 // the Name column.
356 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
357 uksort($each_tables, 'strnatcasecmp');
359 if ($sort_order == 'DESC') {
360 $each_tables = array_reverse($each_tables);
362 } else {
363 // Prepare to sort by creating array of the selected sort
364 // value to pass to array_multisort
365 foreach ($each_tables as $table_name => $table_data) {
366 ${$sort_by}[$table_name] = strtolower($table_data[$sort_by]);
369 if ($sort_order == 'DESC') {
370 array_multisort($$sort_by, SORT_DESC, $each_tables);
371 } else {
372 array_multisort($$sort_by, SORT_ASC, $each_tables);
375 // cleanup the temporary sort array
376 unset($$sort_by);
379 if ($limit_count) {
380 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
383 foreach ($each_tables as $table_name => $each_table) {
384 if ('comment' === $tbl_is_group
385 && 0 === strpos($each_table['Comment'], $table))
387 // remove table from list
388 unset($each_tables[$table_name]);
389 continue;
392 if (! isset($each_tables[$table_name]['Type'])
393 && isset($each_tables[$table_name]['Engine'])) {
394 // pma BC, same parts of PMA still uses 'Type'
395 $each_tables[$table_name]['Type']
396 =& $each_tables[$table_name]['Engine'];
397 } elseif (! isset($each_tables[$table_name]['Engine'])
398 && isset($each_tables[$table_name]['Type'])) {
399 // old MySQL reports Type, newer MySQL reports Engine
400 $each_tables[$table_name]['Engine']
401 =& $each_tables[$table_name]['Type'];
404 // MySQL forward compatibility
405 // so pma could use this array as if every server is of version >5.0
406 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
407 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
408 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
409 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
410 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
411 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
412 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
413 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
414 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
415 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
416 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
417 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
418 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
419 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
420 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
421 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
422 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
423 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
424 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
426 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW'
427 && $each_tables[$table_name]['Engine'] == NULL) {
428 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
429 } else {
431 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
433 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
437 $tables[$each_database] = $each_tables;
441 // cache table data
442 // so PMA_Table does not require to issue SHOW TABLE STATUS again
443 // Note: I don't see why we would need array_merge_recursive() here,
444 // as it creates double entries for the same table (for example a double
445 // entry for Comment when changing the storage engine in Operations)
446 // Note 2: Instead of array_merge(), simply use the + operator because
447 // array_merge() renumbers numeric keys starting with 0, therefore
448 // we would lose a db name thats consists only of numbers
449 PMA_Table::$cache = PMA_Table::$cache + $tables;
451 if (! is_array($database)) {
452 if (isset($tables[$database])) {
453 return $tables[$database];
454 } elseif (isset($tables[strtolower($database)])) {
455 // on windows with lower_case_table_names = 1
456 // MySQL returns
457 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
458 // but information_schema.TABLES gives `test`
459 // bug #1436171
460 // http://sf.net/support/tracker.php?aid=1436171
461 return $tables[strtolower($database)];
462 } else {
463 return $tables;
465 } else {
466 return $tables;
471 * returns array with databases containing extended infos about them
473 * @todo move into PMA_List_Database?
474 * @param string $databases database
475 * @param boolean $force_stats retrieve stats also for MySQL < 5
476 * @param resource $link mysql link
477 * @param string $sort_by column to order by
478 * @param string $sort_order ASC or DESC
479 * @param integer $limit_offset starting offset for LIMIT
480 * @param bool|int $limit_count row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
481 * @return array $databases
483 function PMA_DBI_get_databases_full($database = null, $force_stats = false,
484 $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
485 $limit_offset = 0, $limit_count = false)
487 $sort_order = strtoupper($sort_order);
489 if (true === $limit_count) {
490 $limit_count = $GLOBALS['cfg']['MaxDbList'];
493 // initialize to avoid errors when there are no databases
494 $databases = array();
496 $apply_limit_and_order_manual = true;
498 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
500 * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
501 * cause MySQL does not support natural ordering, we have to do it afterward
503 if ($GLOBALS['cfg']['NaturalOrder']) {
504 $limit = '';
505 } else {
506 if ($limit_count) {
507 $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
510 $apply_limit_and_order_manual = false;
513 // get table information from information_schema
514 if ($database) {
515 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
516 . addslashes($database) . '\'';
517 } else {
518 $sql_where_schema = '';
521 // for PMA bc:
522 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
523 $sql = '
524 SELECT `information_schema`.`SCHEMATA`.*';
525 if ($force_stats) {
526 $sql .= ',
527 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
528 AS `SCHEMA_TABLES`,
529 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
530 AS `SCHEMA_TABLE_ROWS`,
531 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
532 AS `SCHEMA_DATA_LENGTH`,
533 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
534 AS `SCHEMA_MAX_DATA_LENGTH`,
535 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
536 AS `SCHEMA_INDEX_LENGTH`,
537 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
538 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
539 AS `SCHEMA_LENGTH`,
540 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
541 AS `SCHEMA_DATA_FREE`';
543 $sql .= '
544 FROM `information_schema`.`SCHEMATA`';
545 if ($force_stats) {
546 $sql .= '
547 LEFT JOIN `information_schema`.`TABLES`
548 ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
549 = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
551 $sql .= '
552 ' . $sql_where_schema . '
553 GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
554 ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
555 . $limit;
556 $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
558 $mysql_error = PMA_DBI_getError($link);
559 if (! count($databases) && $GLOBALS['errno']) {
560 PMA_mysqlDie($mysql_error, $sql);
563 // display only databases also in official database list
564 // f.e. to apply hide_db and only_db
565 $drops = array_diff(array_keys($databases), (array) $GLOBALS['pma']->databases);
566 if (count($drops)) {
567 foreach ($drops as $drop) {
568 unset($databases[$drop]);
570 unset($drop);
572 unset($sql_where_schema, $sql, $drops);
573 } else {
574 foreach ($GLOBALS['pma']->databases as $database_name) {
575 // MySQL forward compatibility
576 // so pma could use this array as if every server is of version >5.0
577 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
579 if ($force_stats) {
580 require_once './libraries/mysql_charsets.lib.php';
582 $databases[$database_name]['DEFAULT_COLLATION_NAME']
583 = PMA_getDbCollation($database_name);
585 // get additional info about tables
586 $databases[$database_name]['SCHEMA_TABLES'] = 0;
587 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
588 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
589 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
590 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
591 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
592 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
594 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($database_name) . ';');
595 while ($row = PMA_DBI_fetch_assoc($res)) {
596 $databases[$database_name]['SCHEMA_TABLES']++;
597 $databases[$database_name]['SCHEMA_TABLE_ROWS']
598 += $row['Rows'];
599 $databases[$database_name]['SCHEMA_DATA_LENGTH']
600 += $row['Data_length'];
601 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
602 += $row['Max_data_length'];
603 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
604 += $row['Index_length'];
606 // for InnoDB, this does not contain the number of
607 // overhead bytes but the total free space
608 if ('InnoDB' != $row['Engine']) {
609 $databases[$database_name]['SCHEMA_DATA_FREE']
610 += $row['Data_free'];
612 $databases[$database_name]['SCHEMA_LENGTH']
613 += $row['Data_length'] + $row['Index_length'];
615 PMA_DBI_free_result($res);
616 unset($res);
623 * apply limit and order manually now
624 * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
626 if ($apply_limit_and_order_manual) {
627 $GLOBALS['callback_sort_order'] = $sort_order;
628 $GLOBALS['callback_sort_by'] = $sort_by;
629 usort($databases, 'PMA_usort_comparison_callback');
630 unset($GLOBALS['callback_sort_order'], $GLOBALS['callback_sort_by']);
633 * now apply limit
635 if ($limit_count) {
636 $databases = array_slice($databases, $limit_offset, $limit_count);
640 return $databases;
644 * returns detailed array with all columns for given table in database,
645 * or all tables/databases
647 * @param string $database name of database
648 * @param string $table name of table to retrieve columns from
649 * @param string $column name of specific column
650 * @param mixed $link mysql link resource
652 function PMA_DBI_get_columns_full($database = null, $table = null,
653 $column = null, $link = null)
655 $columns = array();
657 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
658 $sql_wheres = array();
659 $array_keys = array();
661 // get columns information from information_schema
662 if (null !== $database) {
663 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . addslashes($database) . '\' ';
664 } else {
665 $array_keys[] = 'TABLE_SCHEMA';
667 if (null !== $table) {
668 $sql_wheres[] = '`TABLE_NAME` = \'' . addslashes($table) . '\' ';
669 } else {
670 $array_keys[] = 'TABLE_NAME';
672 if (null !== $column) {
673 $sql_wheres[] = '`COLUMN_NAME` = \'' . addslashes($column) . '\' ';
674 } else {
675 $array_keys[] = 'COLUMN_NAME';
678 // for PMA bc:
679 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
680 $sql = '
681 SELECT *,
682 `COLUMN_NAME` AS `Field`,
683 `COLUMN_TYPE` AS `Type`,
684 `COLLATION_NAME` AS `Collation`,
685 `IS_NULLABLE` AS `Null`,
686 `COLUMN_KEY` AS `Key`,
687 `COLUMN_DEFAULT` AS `Default`,
688 `EXTRA` AS `Extra`,
689 `PRIVILEGES` AS `Privileges`,
690 `COLUMN_COMMENT` AS `Comment`
691 FROM `information_schema`.`COLUMNS`';
692 if (count($sql_wheres)) {
693 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
696 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
697 unset($sql_wheres, $sql);
698 } else {
699 if (null === $database) {
700 foreach ($GLOBALS['pma']->databases as $database) {
701 $columns[$database] = PMA_DBI_get_columns_full($database, null,
702 null, $link);
704 return $columns;
705 } elseif (null === $table) {
706 $tables = PMA_DBI_get_tables($database);
707 foreach ($tables as $table) {
708 $columns[$table] = PMA_DBI_get_columns_full(
709 $database, $table, null, $link);
711 return $columns;
714 $sql = 'SHOW FULL COLUMNS FROM '
715 . PMA_backquote($database) . '.' . PMA_backquote($table);
716 if (null !== $column) {
717 $sql .= " LIKE '" . $column . "'";
720 $columns = PMA_DBI_fetch_result($sql, 'Field', null, $link);
722 $ordinal_position = 1;
723 foreach ($columns as $column_name => $each_column) {
725 // MySQL forward compatibility
726 // so pma could use this array as if every server is of version >5.0
727 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
728 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
729 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
730 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
731 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
732 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
733 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
734 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
735 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
737 $columns[$column_name]['TABLE_CATALOG'] = null;
738 $columns[$column_name]['TABLE_SCHEMA'] = $database;
739 $columns[$column_name]['TABLE_NAME'] = $table;
740 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
741 $columns[$column_name]['DATA_TYPE'] =
742 substr($columns[$column_name]['COLUMN_TYPE'], 0,
743 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
745 * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
747 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
749 * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
751 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
752 $columns[$column_name]['NUMERIC_PRECISION'] = null;
753 $columns[$column_name]['NUMERIC_SCALE'] = null;
754 $columns[$column_name]['CHARACTER_SET_NAME'] =
755 substr($columns[$column_name]['COLLATION_NAME'], 0,
756 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
758 $ordinal_position++;
761 if (null !== $column) {
762 reset($columns);
763 $columns = current($columns);
767 return $columns;
771 * @todo should only return columns names, for more info use PMA_DBI_get_columns_full()
773 * @deprecated by PMA_DBI_get_columns() or PMA_DBI_get_columns_full()
774 * @param string $database name of database
775 * @param string $table name of table to retrieve columns from
776 * @param mixed $link mysql link resource
777 * @return array column info
779 function PMA_DBI_get_fields($database, $table, $link = null)
781 // here we use a try_query because when coming from
782 // tbl_create + tbl_properties.inc.php, the table does not exist
783 $fields = PMA_DBI_fetch_result(
784 'SHOW FULL COLUMNS
785 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
786 null, null, $link);
787 if (! is_array($fields) || count($fields) < 1) {
788 return false;
790 return $fields;
794 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
796 * @param string $database name of database
797 * @param string $table name of table to retrieve columns from
798 * @param boolean $full whether to return full info or only column names
799 * @param mixed $link mysql link resource
800 * @return array column names
802 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
804 $fields = PMA_DBI_fetch_result(
805 'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
806 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
807 'Field', ($full ? null : 'Field'), $link);
808 if (! is_array($fields) || count($fields) < 1) {
809 return false;
811 return $fields;
815 * array PMA_DBI_get_column_values (string $database, string $table, string $column , mysql db link $link = null)
817 * @param string $database name of database
818 * @param string $table name of table to retrieve columns from
819 * @param string $column name of the column to retrieve data from
820 * @param mixed $link mysql link resource
821 * @return array $field_values
824 function PMA_DBI_get_column_values($database, $table, $column, $link = null)
826 $query = 'SELECT ';
827 for($i=0; $i< sizeof($column); $i++)
829 $query.= PMA_backquote($column[$i]);
830 if($i < (sizeof($column)-1))
832 $query.= ', ';
835 $query.= ' FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table);
836 $field_values = PMA_DBI_fetch_result($query, null, null, $link);
838 if (! is_array($field_values) || count($field_values) < 1) {
839 return false;
841 return $field_values;
844 * array PMA_DBI_get_table_data (string $database, string $table, mysql db link $link = null)
846 * @param string $database name of database
847 * @param string $table name of table to retrieve columns from
848 * @param mixed $link mysql link resource
849 * @return array $result
852 function PMA_DBI_get_table_data($database, $table, $link = null)
855 $result = PMA_DBI_fetch_result(
856 'SELECT * FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
857 null,null, $link);
859 if (! is_array($result) || count($result) < 1) {
860 return false;
862 return $result;
866 * array PMA_DBI_get_table_indexes($database, $table, $link = null)
868 * @param string $database name of database
869 * @param string $table name of the table whose indexes are to be retreived
870 * @param mixed $link mysql link resource
871 * @return array $indexes
874 function PMA_DBI_get_table_indexes($database, $table, $link = null)
877 $indexes = PMA_DBI_fetch_result(
878 'SHOW INDEXES FROM ' .PMA_backquote($database) . '.' . PMA_backquote($table),
879 null, null, $link);
881 if (! is_array($indexes) || count($indexes) < 1) {
882 return false;
884 return $indexes;
888 * returns value of given mysql server variable
890 * @param string $var mysql server variable name
891 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
892 * @param mixed $link mysql link resource|object
893 * @return mixed value for mysql server variable
897 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
899 if ($link === null) {
900 if (isset($GLOBALS['userlink'])) {
901 $link = $GLOBALS['userlink'];
902 } else {
903 return false;
907 switch ($type) {
908 case PMA_DBI_GETVAR_SESSION:
909 $modifier = ' SESSION';
910 break;
911 case PMA_DBI_GETVAR_GLOBAL:
912 $modifier = ' GLOBAL';
913 break;
914 default:
915 $modifier = '';
917 return PMA_DBI_fetch_value(
918 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
922 * Function called just after a connection to the MySQL database server has been established
923 * It sets the connection collation, and determins the version of MySQL which is running.
925 * @uses ./libraries/charset_conversion.lib.php
926 * @uses PMA_DBI_QUERY_STORE
927 * @uses PMA_MYSQL_INT_VERSION to set it
928 * @uses PMA_MYSQL_STR_VERSION to set it
929 * @uses $_SESSION['PMA_MYSQL_INT_VERSION'] for caching
930 * @uses $_SESSION['PMA_MYSQL_STR_VERSION'] for caching
931 * @uses PMA_DBI_GETVAR_SESSION
932 * @uses PMA_DBI_fetch_value()
933 * @uses PMA_DBI_query()
934 * @uses PMA_DBI_get_variable()
935 * @uses $GLOBALS['collation_connection']
936 * @uses $GLOBALS['available_languages']
937 * @uses $GLOBALS['mysql_charset_map']
938 * @uses $GLOBALS['charset']
939 * @uses $GLOBALS['lang']
940 * @uses $GLOBALS['server']
941 * @uses $GLOBALS['cfg']['Lang']
942 * @uses defined()
943 * @uses explode()
944 * @uses sprintf()
945 * @uses intval()
946 * @uses define()
947 * @uses defined()
948 * @uses substr()
949 * @uses count()
950 * @param mixed $link mysql link resource|object
951 * @param boolean $is_controluser
953 function PMA_DBI_postConnect($link, $is_controluser = false)
955 if (! defined('PMA_MYSQL_INT_VERSION')) {
956 if (PMA_cacheExists('PMA_MYSQL_INT_VERSION', true)) {
957 define('PMA_MYSQL_INT_VERSION', PMA_cacheGet('PMA_MYSQL_INT_VERSION', true));
958 define('PMA_MYSQL_STR_VERSION', PMA_cacheGet('PMA_MYSQL_STR_VERSION', true));
959 } else {
960 $mysql_version = PMA_DBI_fetch_value(
961 'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE);
962 if ($mysql_version) {
963 $match = explode('.', $mysql_version);
964 define('PMA_MYSQL_INT_VERSION',
965 (int) sprintf('%d%02d%02d', $match[0], $match[1],
966 intval($match[2])));
967 define('PMA_MYSQL_STR_VERSION', $mysql_version);
968 unset($mysql_version, $match);
969 } else {
970 define('PMA_MYSQL_INT_VERSION', 50015);
971 define('PMA_MYSQL_STR_VERSION', '5.00.15');
973 PMA_cacheSet('PMA_MYSQL_INT_VERSION', PMA_MYSQL_INT_VERSION, true);
974 PMA_cacheSet('PMA_MYSQL_STR_VERSION', PMA_MYSQL_STR_VERSION, true);
978 if (! empty($GLOBALS['collation_connection'])) {
979 PMA_DBI_query("SET CHARACTER SET 'utf8';", $link, PMA_DBI_QUERY_STORE);
980 $mysql_charset = explode('_', $GLOBALS['collation_connection']);
981 PMA_DBI_query("SET collation_connection = '" . PMA_sqlAddslashes($GLOBALS['collation_connection']) . "';", $link, PMA_DBI_QUERY_STORE);
982 } else {
983 PMA_DBI_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci';", $link, PMA_DBI_QUERY_STORE);
988 * returns a single value from the given result or query,
989 * if the query or the result has more than one row or field
990 * the first field of the first row is returned
992 * <code>
993 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
994 * $user_name = PMA_DBI_fetch_value($sql);
995 * // produces
996 * // $user_name = 'John Doe'
997 * </code>
999 * @uses is_string()
1000 * @uses is_int()
1001 * @uses PMA_DBI_try_query()
1002 * @uses PMA_DBI_num_rows()
1003 * @uses PMA_DBI_fetch_row()
1004 * @uses PMA_DBI_fetch_assoc()
1005 * @uses PMA_DBI_free_result()
1006 * @param string|mysql_result $result query or mysql result
1007 * @param integer $row_number row to fetch the value from,
1008 * starting at 0, with 0 beeing default
1009 * @param integer|string $field field to fetch the value from,
1010 * starting at 0, with 0 beeing default
1011 * @param resource $link mysql link
1012 * @param mixed $options
1013 * @return mixed value of first field in first row from result
1014 * or false if not found
1016 function PMA_DBI_fetch_value($result, $row_number = 0, $field = 0, $link = null, $options = 0) {
1017 $value = false;
1019 if (is_string($result)) {
1020 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1023 // return false if result is empty or false
1024 // or requested row is larger than rows in result
1025 if (PMA_DBI_num_rows($result) < ($row_number + 1)) {
1026 return $value;
1029 // if $field is an integer use non associative mysql fetch function
1030 if (is_int($field)) {
1031 $fetch_function = 'PMA_DBI_fetch_row';
1032 } else {
1033 $fetch_function = 'PMA_DBI_fetch_assoc';
1036 // get requested row
1037 for ($i = 0; $i <= $row_number; $i++) {
1038 $row = $fetch_function($result);
1040 PMA_DBI_free_result($result);
1042 // return requested field
1043 if (isset($row[$field])) {
1044 $value = $row[$field];
1046 unset($row);
1048 return $value;
1052 * returns only the first row from the result
1054 * <code>
1055 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
1056 * $user = PMA_DBI_fetch_single_row($sql);
1057 * // produces
1058 * // $user = array('id' => 123, 'name' => 'John Doe')
1059 * </code>
1061 * @uses is_string()
1062 * @uses PMA_DBI_try_query()
1063 * @uses PMA_DBI_num_rows()
1064 * @uses PMA_DBI_fetch_row()
1065 * @uses PMA_DBI_fetch_assoc()
1066 * @uses PMA_DBI_fetch_array()
1067 * @uses PMA_DBI_free_result()
1068 * @param string|mysql_result $result query or mysql result
1069 * @param string $type NUM|ASSOC|BOTH
1070 * returned array should either numeric
1071 * associativ or booth
1072 * @param resource $link mysql link
1073 * @param mixed $options
1074 * @return array|boolean first row from result
1075 * or false if result is empty
1077 function PMA_DBI_fetch_single_row($result, $type = 'ASSOC', $link = null, $options = 0) {
1078 if (is_string($result)) {
1079 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1082 // return null if result is empty or false
1083 if (! PMA_DBI_num_rows($result)) {
1084 return false;
1087 switch ($type) {
1088 case 'NUM' :
1089 $fetch_function = 'PMA_DBI_fetch_row';
1090 break;
1091 case 'ASSOC' :
1092 $fetch_function = 'PMA_DBI_fetch_assoc';
1093 break;
1094 case 'BOTH' :
1095 default :
1096 $fetch_function = 'PMA_DBI_fetch_array';
1097 break;
1100 $row = $fetch_function($result);
1101 PMA_DBI_free_result($result);
1102 return $row;
1106 * returns all rows in the resultset in one array
1108 * <code>
1109 * $sql = 'SELECT * FROM `user`';
1110 * $users = PMA_DBI_fetch_result($sql);
1111 * // produces
1112 * // $users[] = array('id' => 123, 'name' => 'John Doe')
1114 * $sql = 'SELECT `id`, `name` FROM `user`';
1115 * $users = PMA_DBI_fetch_result($sql, 'id');
1116 * // produces
1117 * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
1119 * $sql = 'SELECT `id`, `name` FROM `user`';
1120 * $users = PMA_DBI_fetch_result($sql, 0);
1121 * // produces
1122 * // $users['123'] = array(0 => 123, 1 => 'John Doe')
1124 * $sql = 'SELECT `id`, `name` FROM `user`';
1125 * $users = PMA_DBI_fetch_result($sql, 'id', 'name');
1126 * // or
1127 * $users = PMA_DBI_fetch_result($sql, 0, 1);
1128 * // produces
1129 * // $users['123'] = 'John Doe'
1131 * $sql = 'SELECT `name` FROM `user`';
1132 * $users = PMA_DBI_fetch_result($sql);
1133 * // produces
1134 * // $users[] = 'John Doe'
1136 * $sql = 'SELECT `group`, `name` FROM `user`'
1137 * $users = PMA_DBI_fetch_result($sql, array('group', null), 'name');
1138 * // produces
1139 * // $users['admin'][] = 'John Doe'
1141 * $sql = 'SELECT `group`, `name` FROM `user`'
1142 * $users = PMA_DBI_fetch_result($sql, array('group', 'name'), 'id');
1143 * // produces
1144 * // $users['admin']['John Doe'] = '123'
1145 * </code>
1147 * @uses is_string()
1148 * @uses is_int()
1149 * @uses PMA_DBI_try_query()
1150 * @uses PMA_DBI_num_rows()
1151 * @uses PMA_DBI_num_fields()
1152 * @uses PMA_DBI_fetch_row()
1153 * @uses PMA_DBI_fetch_assoc()
1154 * @uses PMA_DBI_free_result()
1155 * @param string|mysql_result $result query or mysql result
1156 * @param string|integer $key field-name or offset
1157 * used as key for array
1158 * @param string|integer $value value-name or offset
1159 * used as value for array
1160 * @param resource $link mysql link
1161 * @param mixed $options
1162 * @return array resultrows or values indexed by $key
1164 function PMA_DBI_fetch_result($result, $key = null, $value = null,
1165 $link = null, $options = 0)
1167 $resultrows = array();
1169 if (is_string($result)) {
1170 $result = PMA_DBI_try_query($result, $link, $options);
1173 // return empty array if result is empty or false
1174 if (! $result) {
1175 return $resultrows;
1178 $fetch_function = 'PMA_DBI_fetch_assoc';
1180 // no nested array if only one field is in result
1181 if (null === $key && 1 === PMA_DBI_num_fields($result)) {
1182 $value = 0;
1183 $fetch_function = 'PMA_DBI_fetch_row';
1186 // if $key is an integer use non associative mysql fetch function
1187 if (is_int($key)) {
1188 $fetch_function = 'PMA_DBI_fetch_row';
1191 if (null === $key && null === $value) {
1192 while ($row = $fetch_function($result)) {
1193 $resultrows[] = $row;
1195 } elseif (null === $key) {
1196 while ($row = $fetch_function($result)) {
1197 $resultrows[] = $row[$value];
1199 } elseif (null === $value) {
1200 if (is_array($key)) {
1201 while ($row = $fetch_function($result)) {
1202 $result_target =& $resultrows;
1203 foreach ($key as $key_index) {
1204 if (null === $key_index) {
1205 $result_target =& $result_target[];
1206 continue;
1209 if (! isset($result_target[$row[$key_index]])) {
1210 $result_target[$row[$key_index]] = array();
1212 $result_target =& $result_target[$row[$key_index]];
1214 $result_target = $row;
1216 } else {
1217 while ($row = $fetch_function($result)) {
1218 $resultrows[$row[$key]] = $row;
1221 } else {
1222 if (is_array($key)) {
1223 while ($row = $fetch_function($result)) {
1224 $result_target =& $resultrows;
1225 foreach ($key as $key_index) {
1226 if (null === $key_index) {
1227 $result_target =& $result_target[];
1228 continue;
1231 if (! isset($result_target[$row[$key_index]])) {
1232 $result_target[$row[$key_index]] = array();
1234 $result_target =& $result_target[$row[$key_index]];
1236 $result_target = $row[$value];
1238 } else {
1239 while ($row = $fetch_function($result)) {
1240 $resultrows[$row[$key]] = $row[$value];
1245 PMA_DBI_free_result($result);
1246 return $resultrows;
1250 * return default table engine for given database
1252 * @return string default table engine
1254 function PMA_DBI_get_default_engine()
1256 return PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'storage_engine\';', 0, 1);
1260 * Get supported SQL compatibility modes
1262 * @return array supported SQL compatibility modes
1264 function PMA_DBI_getCompatibilities()
1266 $compats = array('NONE');
1267 $compats[] = 'ANSI';
1268 $compats[] = 'DB2';
1269 $compats[] = 'MAXDB';
1270 $compats[] = 'MYSQL323';
1271 $compats[] = 'MYSQL40';
1272 $compats[] = 'MSSQL';
1273 $compats[] = 'ORACLE';
1274 // removed; in MySQL 5.0.33, this produces exports that
1275 // can't be read by POSTGRESQL (see our bug #1596328)
1276 //$compats[] = 'POSTGRESQL';
1277 $compats[] = 'TRADITIONAL';
1279 return $compats;
1283 * returns warnings for last query
1285 * @uses $GLOBALS['userlink']
1286 * @uses PMA_DBI_fetch_result()
1287 * @param resource mysql link $link mysql link resource
1288 * @return array warnings
1290 function PMA_DBI_get_warnings($link = null)
1292 if (empty($link)) {
1293 if (isset($GLOBALS['userlink'])) {
1294 $link = $GLOBALS['userlink'];
1295 } else {
1296 return array();
1300 return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1304 * returns true (int > 0) if current user is superuser
1305 * otherwise 0
1307 * @uses $_SESSION['is_superuser'] for caching
1308 * @uses $GLOBALS['userlink']
1309 * @uses $GLOBALS['server']
1310 * @uses PMA_DBI_try_query()
1311 * @uses PMA_DBI_QUERY_STORE
1312 * @return integer $is_superuser
1314 function PMA_isSuperuser()
1316 if (PMA_cacheExists('is_superuser', true)) {
1317 return PMA_cacheGet('is_superuser', true);
1320 // with mysql extension, when connection failed we don't have
1321 // a $userlink
1322 if (isset($GLOBALS['userlink'])) {
1323 $r = (bool) PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user', $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
1324 PMA_cacheSet('is_superuser', $r, true);
1325 } else {
1326 PMA_cacheSet('is_superuser', false, true);
1329 return PMA_cacheGet('is_superuser', true);
1333 * returns an array of PROCEDURE or FUNCTION names for a db
1335 * @uses PMA_DBI_free_result()
1336 * @param string $db db name
1337 * @param string $which PROCEDURE | FUNCTION
1338 * @param resource $link mysql link
1340 * @return array the procedure names or function names
1342 function PMA_DBI_get_procedures_or_functions($db, $which, $link = null)
1344 $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1345 $result = array();
1346 foreach ($shows as $one_show) {
1347 if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1348 $result[] = $one_show['Name'];
1351 return($result);
1355 * returns the definition of a specific PROCEDURE, FUNCTION or EVENT
1357 * @uses PMA_DBI_fetch_value()
1358 * @param string $db db name
1359 * @param string $which PROCEDURE | FUNCTION | EVENT
1360 * @param string $name the procedure|function|event name
1361 * @param resource $link mysql link
1363 * @return string the definition
1365 function PMA_DBI_get_definition($db, $which, $name, $link = null)
1367 $returned_field = array(
1368 'PROCEDURE' => 'Create Procedure',
1369 'FUNCTION' => 'Create Function',
1370 'EVENT' => 'Create Event'
1372 $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($name);
1373 return(PMA_DBI_fetch_value($query, 0, $returned_field[$which]));
1377 * returns details about the TRIGGERs of a specific table
1379 * @uses PMA_DBI_fetch_result()
1380 * @param string $db db name
1381 * @param string $table table name
1382 * @param string $delimiter the delimiter to use (may be empty)
1384 * @return array information about triggers (may be empty)
1386 function PMA_DBI_get_triggers($db, $table, $delimiter = '//')
1388 $result = array();
1390 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1391 // Note: in http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
1392 // their example uses WHERE TRIGGER_SCHEMA='dbname' so let's use this
1393 // instead of WHERE EVENT_OBJECT_SCHEMA='dbname'
1394 $triggers = PMA_DBI_fetch_result("SELECT TRIGGER_SCHEMA, TRIGGER_NAME, EVENT_MANIPULATION, ACTION_TIMING, ACTION_STATEMENT, EVENT_OBJECT_SCHEMA, EVENT_OBJECT_TABLE FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA= '" . PMA_sqlAddslashes($db,true) . "' and EVENT_OBJECT_TABLE = '" . PMA_sqlAddslashes($table, true) . "';");
1395 } else {
1396 $triggers = PMA_DBI_fetch_result("SHOW TRIGGERS FROM " . PMA_sqlAddslashes($db,true) . " LIKE '" . PMA_sqlAddslashes($table, true) . "';");
1399 if ($triggers) {
1400 foreach ($triggers as $trigger) {
1401 if ($GLOBALS['cfg']['Server']['DisableIS']) {
1402 $trigger['TRIGGER_NAME'] = $trigger['Trigger'];
1403 $trigger['ACTION_TIMING'] = $trigger['Timing'];
1404 $trigger['EVENT_MANIPULATION'] = $trigger['Event'];
1405 $trigger['EVENT_OBJECT_TABLE'] = $trigger['Table'];
1406 $trigger['ACTION_STATEMENT'] = $trigger['Statement'];
1408 $one_result = array();
1409 $one_result['name'] = $trigger['TRIGGER_NAME'];
1410 $one_result['action_timing'] = $trigger['ACTION_TIMING'];
1411 $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
1413 // do not prepend the schema name; this way, importing the
1414 // definition into another schema will work
1415 $one_result['full_trigger_name'] = PMA_backquote($trigger['TRIGGER_NAME']);
1416 $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
1417 $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";
1419 $result[] = $one_result;
1422 return($result);
1426 * Returns TRUE if $db.$view_name is a view, FALSE if not
1428 * @uses PMA_DBI_fetch_result()
1429 * @param string $db database name
1430 * @param string $view_name view/table name
1432 * @return bool TRUE if $db.$view_name is a view, FALSE if not
1434 function PMA_isView($db, $view_name)
1436 $result = PMA_DBI_fetch_result("SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$db."' and TABLE_NAME = '".$view_name."';");
1438 if ($result) {
1439 return TRUE;
1440 } else {
1441 return FALSE;