Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / database_interface.lib.php
blobc1a97b43712d1c62dc13479f343b1cef4ff0bcf3
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) {
73 $res = PMA_DBI_try_query($query, $link, $options)
74 or PMA_mysqlDie(PMA_DBI_getError($link), $query);
75 return $res;
78 /**
79 * converts charset of a mysql message, usually coming from mysql_error(),
80 * into PMA charset, usally UTF-8
81 * uses language to charset mapping from mysql/share/errmsg.txt
82 * and charset names to ISO charset from information_schema.CHARACTER_SETS
84 * @uses $GLOBALS['cfg']['IconvExtraParams']
85 * @uses $GLOBALS['charset'] as target charset
86 * @uses PMA_DBI_fetch_value() to get server_language
87 * @uses preg_match() to filter server_language
88 * @uses in_array()
89 * @uses function_exists() to check for a convert function
90 * @uses iconv() to convert message
91 * @uses libiconv() to convert message
92 * @uses recode_string() to convert message
93 * @uses mb_convert_encoding() to convert message
94 * @param string $message
95 * @return string $message
97 function PMA_DBI_convert_message($message) {
98 // latin always last!
99 $encodings = array(
100 'japanese' => 'EUC-JP', //'ujis',
101 'japanese-sjis' => 'Shift-JIS', //'sjis',
102 'korean' => 'EUC-KR', //'euckr',
103 'russian' => 'KOI8-R', //'koi8r',
104 'ukrainian' => 'KOI8-U', //'koi8u',
105 'greek' => 'ISO-8859-7', //'greek',
106 'serbian' => 'CP1250', //'cp1250',
107 'estonian' => 'ISO-8859-13', //'latin7',
108 'slovak' => 'ISO-8859-2', //'latin2',
109 'czech' => 'ISO-8859-2', //'latin2',
110 'hungarian' => 'ISO-8859-2', //'latin2',
111 'polish' => 'ISO-8859-2', //'latin2',
112 'romanian' => 'ISO-8859-2', //'latin2',
113 'spanish' => 'CP1252', //'latin1',
114 'swedish' => 'CP1252', //'latin1',
115 'italian' => 'CP1252', //'latin1',
116 'norwegian-ny' => 'CP1252', //'latin1',
117 'norwegian' => 'CP1252', //'latin1',
118 'portuguese' => 'CP1252', //'latin1',
119 'danish' => 'CP1252', //'latin1',
120 'dutch' => 'CP1252', //'latin1',
121 'english' => 'CP1252', //'latin1',
122 'french' => 'CP1252', //'latin1',
123 'german' => 'CP1252', //'latin1',
126 if ($server_language = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'language\';', 0, 1)) {
127 $found = array();
128 if (preg_match('&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $server_language, $found)) {
129 $server_language = $found[1];
133 if (! empty($server_language) && isset($encodings[$server_language])) {
134 if (function_exists('iconv')) {
135 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
136 require_once './libraries/iconv_wrapper.lib.php';
137 $message = PMA_aix_iconv_wrapper($encodings[$server_language],
138 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
139 } else {
140 $message = iconv($encodings[$server_language],
141 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
143 } elseif (function_exists('recode_string')) {
144 $message = recode_string($encodings[$server_language] . '..' . $GLOBALS['charset'],
145 $message);
146 } elseif (function_exists('libiconv')) {
147 $message = libiconv($encodings[$server_language], $GLOBALS['charset'], $message);
148 } elseif (function_exists('mb_convert_encoding')) {
149 // do not try unsupported charsets
150 if (! in_array($server_language, array('ukrainian', 'greek', 'serbian'))) {
151 $message = mb_convert_encoding($message, $GLOBALS['charset'],
152 $encodings[$server_language]);
155 } else {
157 * @todo lang not found, try all, what TODO ?
161 return $message;
165 * returns array with table names for given db
167 * @param string $database name of database
168 * @param mixed $link mysql link resource|object
169 * @return array tables names
171 function PMA_DBI_get_tables($database, $link = null)
173 return PMA_DBI_fetch_result('SHOW TABLES FROM ' . PMA_backquote($database) . ';',
174 null, 0, $link, PMA_DBI_QUERY_STORE);
178 * usort comparison callback
180 * @param string $a first argument to sort
181 * @param string $b second argument to sort
183 * @return integer a value representing whether $a should be before $b in the
184 * sorted array or not
186 * @global string the column the array shall be sorted by
187 * @global string the sorting order ('ASC' or 'DESC')
189 * @access private
191 function PMA_usort_comparison_callback($a, $b)
193 if ($GLOBALS['cfg']['NaturalOrder']) {
194 $sorter = 'strnatcasecmp';
195 } else {
196 $sorter = 'strcasecmp';
198 /* No sorting when key is not present */
199 if (!isset($a[$GLOBALS['callback_sort_by']]) || ! isset($b[$GLOBALS['callback_sort_by']])) {
200 return 0;
202 // produces f.e.:
203 // return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
204 return ($GLOBALS['callback_sort_order'] == 'ASC' ? 1 : -1) * $sorter($a[$GLOBALS['callback_sort_by']], $b[$GLOBALS['callback_sort_by']]);
205 } // end of the 'PMA_usort_comparison_callback()' function
208 * returns array of all tables in given db or dbs
209 * this function expects unquoted names:
210 * RIGHT: my_database
211 * WRONG: `my_database`
212 * WRONG: my\_database
213 * if $tbl_is_group is true, $table is used as filter for table names
214 * if $tbl_is_group is 'comment, $table is used as filter for table comments
216 * <code>
217 * PMA_DBI_get_tables_full('my_database');
218 * PMA_DBI_get_tables_full('my_database', 'my_table'));
219 * PMA_DBI_get_tables_full('my_database', 'my_tables_', true));
220 * PMA_DBI_get_tables_full('my_database', 'my_tables_', 'comment'));
221 * </code>
223 * @todo move into PMA_Table
224 * @uses PMA_DBI_fetch_result()
225 * @uses PMA_escape_mysql_wildcards()
226 * @uses PMA_backquote()
227 * @uses is_array()
228 * @uses addslashes()
229 * @uses strpos()
230 * @uses strtoupper()
231 * @param string $databases database
232 * @param string $table table
233 * @param boolean|string $tbl_is_group $table is a table group
234 * @param resource $link mysql link
235 * @param integer $limit_offset zero-based offset for the count
236 * @param boolean|integer $limit_count number of tables to return
237 * @param string $sort_by table attribute to sort by
238 * @param string $sort_order direction to sort (ASC or DESC)
239 * @return array list of tables in given db(s)
241 function PMA_DBI_get_tables_full($database, $table = false, $tbl_is_group = false, $link = null,
242 $limit_offset = 0, $limit_count = false, $sort_by = 'Name', $sort_order = 'ASC')
244 if (true === $limit_count) {
245 $limit_count = $GLOBALS['cfg']['MaxTableList'];
247 // prepare and check parameters
248 if (! is_array($database)) {
249 $databases = array($database);
250 } else {
251 $databases = $database;
254 $tables = array();
256 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
257 // get table information from information_schema
258 if ($table) {
259 if (true === $tbl_is_group) {
260 $sql_where_table = 'AND `TABLE_NAME` LIKE \''
261 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
262 } elseif ('comment' === $tbl_is_group) {
263 $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
264 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
265 } else {
266 $sql_where_table = 'AND `TABLE_NAME` = \'' . addslashes($table) . '\'';
268 } else {
269 $sql_where_table = '';
272 // for PMA bc:
273 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
275 // on non-Windows servers,
276 // added BINARY in the WHERE clause to force a case sensitive
277 // comparison (if we are looking for the db Aa we don't want
278 // to find the db aa)
279 $this_databases = array_map('PMA_sqlAddslashes', $databases);
281 $sql = '
282 SELECT *,
283 `TABLE_SCHEMA` AS `Db`,
284 `TABLE_NAME` AS `Name`,
285 `TABLE_TYPE` ÀS `TABLE_TYPE`,
286 `ENGINE` AS `Engine`,
287 `ENGINE` AS `Type`,
288 `VERSION` AS `Version`,
289 `ROW_FORMAT` AS `Row_format`,
290 `TABLE_ROWS` AS `Rows`,
291 `AVG_ROW_LENGTH` AS `Avg_row_length`,
292 `DATA_LENGTH` AS `Data_length`,
293 `MAX_DATA_LENGTH` AS `Max_data_length`,
294 `INDEX_LENGTH` AS `Index_length`,
295 `DATA_FREE` AS `Data_free`,
296 `AUTO_INCREMENT` AS `Auto_increment`,
297 `CREATE_TIME` AS `Create_time`,
298 `UPDATE_TIME` AS `Update_time`,
299 `CHECK_TIME` AS `Check_time`,
300 `TABLE_COLLATION` AS `Collation`,
301 `CHECKSUM` AS `Checksum`,
302 `CREATE_OPTIONS` AS `Create_options`,
303 `TABLE_COMMENT` AS `Comment`
304 FROM `information_schema`.`TABLES`
305 WHERE ' . (PMA_IS_WINDOWS ? '' : 'BINARY') . ' `TABLE_SCHEMA` IN (\'' . implode("', '", $this_databases) . '\')
306 ' . $sql_where_table;
308 // Sort the tables
309 $sql .= " ORDER BY $sort_by $sort_order";
311 if ($limit_count) {
312 $sql .= ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
315 $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
316 null, $link);
317 unset($sql_where_table, $sql);
318 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
319 // here, the array's first key is by schema name
320 foreach($tables as $one_database_name => $one_database_tables) {
321 uksort($one_database_tables, 'strnatcasecmp');
323 if ($sort_order == 'DESC') {
324 $one_database_tables = array_reverse($one_database_tables);
326 $tables[$one_database_name] = $one_database_tables;
329 } // end (get information from table schema)
331 // If permissions are wrong on even one database directory,
332 // information_schema does not return any table info for any database
333 // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002
334 if (empty($tables)) {
335 foreach ($databases as $each_database) {
336 if ($table || (true === $tbl_is_group)) {
337 $sql = 'SHOW TABLE STATUS FROM '
338 . PMA_backquote($each_database)
339 .' LIKE \'' . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
340 } else {
341 $sql = 'SHOW TABLE STATUS FROM '
342 . PMA_backquote($each_database);
345 $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
347 // Sort naturally if the config allows it and we're sorting
348 // the Name column.
349 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
350 uksort($each_tables, 'strnatcasecmp');
352 if ($sort_order == 'DESC') {
353 $each_tables = array_reverse($each_tables);
355 } else {
356 // Prepare to sort by creating array of the selected sort
357 // value to pass to array_multisort
358 foreach ($each_tables as $table_name => $table_data) {
359 ${$sort_by}[$table_name] = strtolower($table_data[$sort_by]);
362 if ($sort_order == 'DESC') {
363 array_multisort($$sort_by, SORT_DESC, $each_tables);
364 } else {
365 array_multisort($$sort_by, SORT_ASC, $each_tables);
368 // cleanup the temporary sort array
369 unset($$sort_by);
372 if ($limit_count) {
373 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
376 foreach ($each_tables as $table_name => $each_table) {
377 if ('comment' === $tbl_is_group
378 && 0 === strpos($each_table['Comment'], $table))
380 // remove table from list
381 unset($each_tables[$table_name]);
382 continue;
385 if (! isset($each_tables[$table_name]['Type'])
386 && isset($each_tables[$table_name]['Engine'])) {
387 // pma BC, same parts of PMA still uses 'Type'
388 $each_tables[$table_name]['Type']
389 =& $each_tables[$table_name]['Engine'];
390 } elseif (! isset($each_tables[$table_name]['Engine'])
391 && isset($each_tables[$table_name]['Type'])) {
392 // old MySQL reports Type, newer MySQL reports Engine
393 $each_tables[$table_name]['Engine']
394 =& $each_tables[$table_name]['Type'];
397 // MySQL forward compatibility
398 // so pma could use this array as if every server is of version >5.0
399 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
400 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
401 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
402 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
403 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
404 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
405 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
406 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
407 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
408 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
409 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
410 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
411 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
412 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
413 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
414 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
415 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
416 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
417 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
419 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW'
420 && $each_tables[$table_name]['Engine'] == NULL) {
421 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
422 } else {
424 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
426 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
430 $tables[$each_database] = $each_tables;
434 // cache table data
435 // so PMA_Table does not require to issue SHOW TABLE STATUS again
436 // Note: I don't see why we would need array_merge_recursive() here,
437 // as it creates double entries for the same table (for example a double
438 // entry for Comment when changing the storage engine in Operations)
439 // Note 2: Instead of array_merge(), simply use the + operator because
440 // array_merge() renumbers numeric keys starting with 0, therefore
441 // we would lose a db name thats consists only of numbers
442 foreach($tables as $one_database => $its_tables) {
443 if (isset(PMA_Table::$cache[$one_database])) {
444 PMA_Table::$cache[$one_database] = PMA_Table::$cache[$one_database] + $tables[$one_database];
445 } else {
446 PMA_Table::$cache[$one_database] = $tables[$one_database];
449 unset($one_database, $its_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_backquote(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;