Merge remote branch 'origin/master'
[phpmyadmin/dkf.git] / libraries / database_interface.lib.php
blobc5b3ca450370a5db0263376be58db5c8153f2680
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 // produces f.e.:
199 // return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
200 return ($GLOBALS['callback_sort_order'] == 'ASC' ? 1 : -1) * $sorter($a[$GLOBALS['callback_sort_by']], $b[$GLOBALS['callback_sort_by']]);
201 } // end of the 'PMA_usort_comparison_callback()' function
204 * returns array of all tables in given db or dbs
205 * this function expects unquoted names:
206 * RIGHT: my_database
207 * WRONG: `my_database`
208 * WRONG: my\_database
209 * if $tbl_is_group is true, $table is used as filter for table names
210 * if $tbl_is_group is 'comment, $table is used as filter for table comments
212 * <code>
213 * PMA_DBI_get_tables_full('my_database');
214 * PMA_DBI_get_tables_full('my_database', 'my_table'));
215 * PMA_DBI_get_tables_full('my_database', 'my_tables_', true));
216 * PMA_DBI_get_tables_full('my_database', 'my_tables_', 'comment'));
217 * </code>
219 * @todo move into PMA_Table
220 * @uses PMA_DBI_fetch_result()
221 * @uses PMA_escape_mysql_wildcards()
222 * @uses PMA_backquote()
223 * @uses is_array()
224 * @uses addslashes()
225 * @uses strpos()
226 * @uses strtoupper()
227 * @param string $databases database
228 * @param string $table table
229 * @param boolean|string $tbl_is_group $table is a table group
230 * @param resource $link mysql link
231 * @param integer $limit_offset zero-based offset for the count
232 * @param boolean|integer $limit_count number of tables to return
233 * @param string $sort_by table attribute to sort by
234 * @param string $sort_order direction to sort (ASC or DESC)
235 * @return array list of tables in given db(s)
237 function PMA_DBI_get_tables_full($database, $table = false, $tbl_is_group = false, $link = null,
238 $limit_offset = 0, $limit_count = false, $sort_by = 'Name', $sort_order = 'ASC')
240 if (true === $limit_count) {
241 $limit_count = $GLOBALS['cfg']['MaxTableList'];
243 // prepare and check parameters
244 if (! is_array($database)) {
245 $databases = array($database);
246 } else {
247 $databases = $database;
250 $tables = array();
252 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
253 // get table information from information_schema
254 if ($table) {
255 if (true === $tbl_is_group) {
256 $sql_where_table = 'AND `TABLE_NAME` LIKE \''
257 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
258 } elseif ('comment' === $tbl_is_group) {
259 $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
260 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
261 } else {
262 $sql_where_table = 'AND `TABLE_NAME` = \'' . addslashes($table) . '\'';
264 } else {
265 $sql_where_table = '';
268 // for PMA bc:
269 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
271 // on non-Windows servers,
272 // added BINARY in the WHERE clause to force a case sensitive
273 // comparison (if we are looking for the db Aa we don't want
274 // to find the db aa)
275 $this_databases = array_map('PMA_sqlAddslashes', $databases);
277 $sql = '
278 SELECT *,
279 `TABLE_SCHEMA` AS `Db`,
280 `TABLE_NAME` AS `Name`,
281 `TABLE_TYPE` ÀS `TABLE_TYPE`,
282 `ENGINE` AS `Engine`,
283 `ENGINE` AS `Type`,
284 `VERSION` AS `Version`,
285 `ROW_FORMAT` AS `Row_format`,
286 `TABLE_ROWS` AS `Rows`,
287 `AVG_ROW_LENGTH` AS `Avg_row_length`,
288 `DATA_LENGTH` AS `Data_length`,
289 `MAX_DATA_LENGTH` AS `Max_data_length`,
290 `INDEX_LENGTH` AS `Index_length`,
291 `DATA_FREE` AS `Data_free`,
292 `AUTO_INCREMENT` AS `Auto_increment`,
293 `CREATE_TIME` AS `Create_time`,
294 `UPDATE_TIME` AS `Update_time`,
295 `CHECK_TIME` AS `Check_time`,
296 `TABLE_COLLATION` AS `Collation`,
297 `CHECKSUM` AS `Checksum`,
298 `CREATE_OPTIONS` AS `Create_options`,
299 `TABLE_COMMENT` AS `Comment`
300 FROM `information_schema`.`TABLES`
301 WHERE ' . (PMA_IS_WINDOWS ? '' : 'BINARY') . ' `TABLE_SCHEMA` IN (\'' . implode("', '", $this_databases) . '\')
302 ' . $sql_where_table;
304 // Sort the tables
305 $sql .= " ORDER BY $sort_by $sort_order";
307 if ($limit_count) {
308 $sql .= ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
311 $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
312 null, $link);
313 unset($sql_where_table, $sql);
314 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
315 // here, the array's first key is by schema name
316 foreach($tables as $one_database_name => $one_database_tables) {
317 uksort($one_database_tables, 'strnatcasecmp');
319 if ($sort_order == 'DESC') {
320 $one_database_tables = array_reverse($one_database_tables);
322 $tables[$one_database_name] = $one_database_tables;
325 } // end (get information from table schema)
327 // If permissions are wrong on even one database directory,
328 // information_schema does not return any table info for any database
329 // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002
330 if (empty($tables)) {
331 foreach ($databases as $each_database) {
332 if ($table || (true === $tbl_is_group)) {
333 $sql = 'SHOW TABLE STATUS FROM '
334 . PMA_backquote($each_database)
335 .' LIKE \'' . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
336 } else {
337 $sql = 'SHOW TABLE STATUS FROM '
338 . PMA_backquote($each_database);
341 $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
343 // Sort naturally if the config allows it and we're sorting
344 // the Name column.
345 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
346 uksort($each_tables, 'strnatcasecmp');
348 if ($sort_order == 'DESC') {
349 $each_tables = array_reverse($each_tables);
351 } else {
352 // Prepare to sort by creating array of the selected sort
353 // value to pass to array_multisort
354 foreach ($each_tables as $table_name => $table_data) {
355 ${$sort_by}[$table_name] = strtolower($table_data[$sort_by]);
358 if ($sort_order == 'DESC') {
359 array_multisort($$sort_by, SORT_DESC, $each_tables);
360 } else {
361 array_multisort($$sort_by, SORT_ASC, $each_tables);
364 // cleanup the temporary sort array
365 unset($$sort_by);
368 if ($limit_count) {
369 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
372 foreach ($each_tables as $table_name => $each_table) {
373 if ('comment' === $tbl_is_group
374 && 0 === strpos($each_table['Comment'], $table))
376 // remove table from list
377 unset($each_tables[$table_name]);
378 continue;
381 if (! isset($each_tables[$table_name]['Type'])
382 && isset($each_tables[$table_name]['Engine'])) {
383 // pma BC, same parts of PMA still uses 'Type'
384 $each_tables[$table_name]['Type']
385 =& $each_tables[$table_name]['Engine'];
386 } elseif (! isset($each_tables[$table_name]['Engine'])
387 && isset($each_tables[$table_name]['Type'])) {
388 // old MySQL reports Type, newer MySQL reports Engine
389 $each_tables[$table_name]['Engine']
390 =& $each_tables[$table_name]['Type'];
393 // MySQL forward compatibility
394 // so pma could use this array as if every server is of version >5.0
395 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
396 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
397 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
398 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
399 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
400 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
401 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
402 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
403 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
404 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
405 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
406 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
407 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
408 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
409 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
410 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
411 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
412 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
413 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
415 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW'
416 && $each_tables[$table_name]['Engine'] == NULL) {
417 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
418 } else {
420 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
422 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
426 $tables[$each_database] = $each_tables;
430 // cache table data
431 // so PMA_Table does not require to issue SHOW TABLE STATUS again
432 // Note: I don't see why we would need array_merge_recursive() here,
433 // as it creates double entries for the same table (for example a double
434 // entry for Comment when changing the storage engine in Operations)
435 // Note 2: Instead of array_merge(), simply use the + operator because
436 // array_merge() renumbers numeric keys starting with 0, therefore
437 // we would lose a db name thats consists only of numbers
438 foreach($tables as $one_database => $its_tables) {
439 if (isset(PMA_Table::$cache[$one_database])) {
440 PMA_Table::$cache[$one_database] = PMA_Table::$cache[$one_database] + $tables[$one_database];
441 } else {
442 PMA_Table::$cache[$one_database] = $tables[$one_database];
445 unset($one_database, $its_tables);
447 if (! is_array($database)) {
448 if (isset($tables[$database])) {
449 return $tables[$database];
450 } elseif (isset($tables[strtolower($database)])) {
451 // on windows with lower_case_table_names = 1
452 // MySQL returns
453 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
454 // but information_schema.TABLES gives `test`
455 // bug #1436171
456 // http://sf.net/support/tracker.php?aid=1436171
457 return $tables[strtolower($database)];
458 } else {
459 return $tables;
461 } else {
462 return $tables;
467 * returns array with databases containing extended infos about them
469 * @todo move into PMA_List_Database?
470 * @param string $databases database
471 * @param boolean $force_stats retrieve stats also for MySQL < 5
472 * @param resource $link mysql link
473 * @param string $sort_by column to order by
474 * @param string $sort_order ASC or DESC
475 * @param integer $limit_offset starting offset for LIMIT
476 * @param bool|int $limit_count row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
477 * @return array $databases
479 function PMA_DBI_get_databases_full($database = null, $force_stats = false,
480 $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
481 $limit_offset = 0, $limit_count = false)
483 $sort_order = strtoupper($sort_order);
485 if (true === $limit_count) {
486 $limit_count = $GLOBALS['cfg']['MaxDbList'];
489 // initialize to avoid errors when there are no databases
490 $databases = array();
492 $apply_limit_and_order_manual = true;
494 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
496 * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
497 * cause MySQL does not support natural ordering, we have to do it afterward
499 if ($GLOBALS['cfg']['NaturalOrder']) {
500 $limit = '';
501 } else {
502 if ($limit_count) {
503 $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
506 $apply_limit_and_order_manual = false;
509 // get table information from information_schema
510 if ($database) {
511 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
512 . addslashes($database) . '\'';
513 } else {
514 $sql_where_schema = '';
517 // for PMA bc:
518 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
519 $sql = '
520 SELECT `information_schema`.`SCHEMATA`.*';
521 if ($force_stats) {
522 $sql .= ',
523 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
524 AS `SCHEMA_TABLES`,
525 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
526 AS `SCHEMA_TABLE_ROWS`,
527 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
528 AS `SCHEMA_DATA_LENGTH`,
529 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
530 AS `SCHEMA_MAX_DATA_LENGTH`,
531 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
532 AS `SCHEMA_INDEX_LENGTH`,
533 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
534 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
535 AS `SCHEMA_LENGTH`,
536 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
537 AS `SCHEMA_DATA_FREE`';
539 $sql .= '
540 FROM `information_schema`.`SCHEMATA`';
541 if ($force_stats) {
542 $sql .= '
543 LEFT JOIN `information_schema`.`TABLES`
544 ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
545 = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
547 $sql .= '
548 ' . $sql_where_schema . '
549 GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
550 ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
551 . $limit;
552 $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
554 $mysql_error = PMA_DBI_getError($link);
555 if (! count($databases) && $GLOBALS['errno']) {
556 PMA_mysqlDie($mysql_error, $sql);
559 // display only databases also in official database list
560 // f.e. to apply hide_db and only_db
561 $drops = array_diff(array_keys($databases), (array) $GLOBALS['pma']->databases);
562 if (count($drops)) {
563 foreach ($drops as $drop) {
564 unset($databases[$drop]);
566 unset($drop);
568 unset($sql_where_schema, $sql, $drops);
569 } else {
570 foreach ($GLOBALS['pma']->databases as $database_name) {
571 // MySQL forward compatibility
572 // so pma could use this array as if every server is of version >5.0
573 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
575 if ($force_stats) {
576 require_once './libraries/mysql_charsets.lib.php';
578 $databases[$database_name]['DEFAULT_COLLATION_NAME']
579 = PMA_getDbCollation($database_name);
581 // get additional info about tables
582 $databases[$database_name]['SCHEMA_TABLES'] = 0;
583 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
584 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
585 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
586 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
587 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
588 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
590 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($database_name) . ';');
591 while ($row = PMA_DBI_fetch_assoc($res)) {
592 $databases[$database_name]['SCHEMA_TABLES']++;
593 $databases[$database_name]['SCHEMA_TABLE_ROWS']
594 += $row['Rows'];
595 $databases[$database_name]['SCHEMA_DATA_LENGTH']
596 += $row['Data_length'];
597 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
598 += $row['Max_data_length'];
599 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
600 += $row['Index_length'];
602 // for InnoDB, this does not contain the number of
603 // overhead bytes but the total free space
604 if ('InnoDB' != $row['Engine']) {
605 $databases[$database_name]['SCHEMA_DATA_FREE']
606 += $row['Data_free'];
608 $databases[$database_name]['SCHEMA_LENGTH']
609 += $row['Data_length'] + $row['Index_length'];
611 PMA_DBI_free_result($res);
612 unset($res);
619 * apply limit and order manually now
620 * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
622 if ($apply_limit_and_order_manual) {
623 $GLOBALS['callback_sort_order'] = $sort_order;
624 $GLOBALS['callback_sort_by'] = $sort_by;
625 usort($databases, 'PMA_usort_comparison_callback');
626 unset($GLOBALS['callback_sort_order'], $GLOBALS['callback_sort_by']);
629 * now apply limit
631 if ($limit_count) {
632 $databases = array_slice($databases, $limit_offset, $limit_count);
636 return $databases;
640 * returns detailed array with all columns for given table in database,
641 * or all tables/databases
643 * @param string $database name of database
644 * @param string $table name of table to retrieve columns from
645 * @param string $column name of specific column
646 * @param mixed $link mysql link resource
648 function PMA_DBI_get_columns_full($database = null, $table = null,
649 $column = null, $link = null)
651 $columns = array();
653 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
654 $sql_wheres = array();
655 $array_keys = array();
657 // get columns information from information_schema
658 if (null !== $database) {
659 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . addslashes($database) . '\' ';
660 } else {
661 $array_keys[] = 'TABLE_SCHEMA';
663 if (null !== $table) {
664 $sql_wheres[] = '`TABLE_NAME` = \'' . addslashes($table) . '\' ';
665 } else {
666 $array_keys[] = 'TABLE_NAME';
668 if (null !== $column) {
669 $sql_wheres[] = '`COLUMN_NAME` = \'' . addslashes($column) . '\' ';
670 } else {
671 $array_keys[] = 'COLUMN_NAME';
674 // for PMA bc:
675 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
676 $sql = '
677 SELECT *,
678 `COLUMN_NAME` AS `Field`,
679 `COLUMN_TYPE` AS `Type`,
680 `COLLATION_NAME` AS `Collation`,
681 `IS_NULLABLE` AS `Null`,
682 `COLUMN_KEY` AS `Key`,
683 `COLUMN_DEFAULT` AS `Default`,
684 `EXTRA` AS `Extra`,
685 `PRIVILEGES` AS `Privileges`,
686 `COLUMN_COMMENT` AS `Comment`
687 FROM `information_schema`.`COLUMNS`';
688 if (count($sql_wheres)) {
689 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
692 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
693 unset($sql_wheres, $sql);
694 } else {
695 if (null === $database) {
696 foreach ($GLOBALS['pma']->databases as $database) {
697 $columns[$database] = PMA_DBI_get_columns_full($database, null,
698 null, $link);
700 return $columns;
701 } elseif (null === $table) {
702 $tables = PMA_DBI_get_tables($database);
703 foreach ($tables as $table) {
704 $columns[$table] = PMA_DBI_get_columns_full(
705 $database, $table, null, $link);
707 return $columns;
710 $sql = 'SHOW FULL COLUMNS FROM '
711 . PMA_backquote($database) . '.' . PMA_backquote($table);
712 if (null !== $column) {
713 $sql .= " LIKE '" . $column . "'";
716 $columns = PMA_DBI_fetch_result($sql, 'Field', null, $link);
718 $ordinal_position = 1;
719 foreach ($columns as $column_name => $each_column) {
721 // MySQL forward compatibility
722 // so pma could use this array as if every server is of version >5.0
723 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
724 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
725 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
726 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
727 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
728 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
729 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
730 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
731 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
733 $columns[$column_name]['TABLE_CATALOG'] = null;
734 $columns[$column_name]['TABLE_SCHEMA'] = $database;
735 $columns[$column_name]['TABLE_NAME'] = $table;
736 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
737 $columns[$column_name]['DATA_TYPE'] =
738 substr($columns[$column_name]['COLUMN_TYPE'], 0,
739 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
741 * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
743 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
745 * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
747 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
748 $columns[$column_name]['NUMERIC_PRECISION'] = null;
749 $columns[$column_name]['NUMERIC_SCALE'] = null;
750 $columns[$column_name]['CHARACTER_SET_NAME'] =
751 substr($columns[$column_name]['COLLATION_NAME'], 0,
752 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
754 $ordinal_position++;
757 if (null !== $column) {
758 reset($columns);
759 $columns = current($columns);
763 return $columns;
767 * @todo should only return columns names, for more info use PMA_DBI_get_columns_full()
769 * @deprecated by PMA_DBI_get_columns() or PMA_DBI_get_columns_full()
770 * @param string $database name of database
771 * @param string $table name of table to retrieve columns from
772 * @param mixed $link mysql link resource
773 * @return array column info
775 function PMA_DBI_get_fields($database, $table, $link = null)
777 // here we use a try_query because when coming from
778 // tbl_create + tbl_properties.inc.php, the table does not exist
779 $fields = PMA_DBI_fetch_result(
780 'SHOW FULL COLUMNS
781 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
782 null, null, $link);
783 if (! is_array($fields) || count($fields) < 1) {
784 return false;
786 return $fields;
790 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
792 * @param string $database name of database
793 * @param string $table name of table to retrieve columns from
794 * @param boolean $full whether to return full info or only column names
795 * @param mixed $link mysql link resource
796 * @return array column names
798 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
800 $fields = PMA_DBI_fetch_result(
801 'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
802 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
803 'Field', ($full ? null : 'Field'), $link);
804 if (! is_array($fields) || count($fields) < 1) {
805 return false;
807 return $fields;
811 * array PMA_DBI_get_column_values (string $database, string $table, string $column , mysql db link $link = null)
813 * @param string $database name of database
814 * @param string $table name of table to retrieve columns from
815 * @param string $column name of the column to retrieve data from
816 * @param mixed $link mysql link resource
817 * @return array $field_values
820 function PMA_DBI_get_column_values($database, $table, $column, $link = null)
822 $query = 'SELECT ';
823 for($i=0; $i< sizeof($column); $i++)
825 $query.= PMA_backquote($column[$i]);
826 if($i < (sizeof($column)-1))
828 $query.= ', ';
831 $query.= ' FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table);
832 $field_values = PMA_DBI_fetch_result($query, null, null, $link);
834 if (! is_array($field_values) || count($field_values) < 1) {
835 return false;
837 return $field_values;
840 * array PMA_DBI_get_table_data (string $database, string $table, mysql db link $link = null)
842 * @param string $database name of database
843 * @param string $table name of table to retrieve columns from
844 * @param mixed $link mysql link resource
845 * @return array $result
848 function PMA_DBI_get_table_data($database, $table, $link = null)
851 $result = PMA_DBI_fetch_result(
852 'SELECT * FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
853 null,null, $link);
855 if (! is_array($result) || count($result) < 1) {
856 return false;
858 return $result;
862 * array PMA_DBI_get_table_indexes($database, $table, $link = null)
864 * @param string $database name of database
865 * @param string $table name of the table whose indexes are to be retreived
866 * @param mixed $link mysql link resource
867 * @return array $indexes
870 function PMA_DBI_get_table_indexes($database, $table, $link = null)
873 $indexes = PMA_DBI_fetch_result(
874 'SHOW INDEXES FROM ' .PMA_backquote($database) . '.' . PMA_backquote($table),
875 null, null, $link);
877 if (! is_array($indexes) || count($indexes) < 1) {
878 return false;
880 return $indexes;
884 * returns value of given mysql server variable
886 * @param string $var mysql server variable name
887 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
888 * @param mixed $link mysql link resource|object
889 * @return mixed value for mysql server variable
893 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
895 if ($link === null) {
896 if (isset($GLOBALS['userlink'])) {
897 $link = $GLOBALS['userlink'];
898 } else {
899 return false;
903 switch ($type) {
904 case PMA_DBI_GETVAR_SESSION:
905 $modifier = ' SESSION';
906 break;
907 case PMA_DBI_GETVAR_GLOBAL:
908 $modifier = ' GLOBAL';
909 break;
910 default:
911 $modifier = '';
913 return PMA_DBI_fetch_value(
914 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
918 * Function called just after a connection to the MySQL database server has been established
919 * It sets the connection collation, and determins the version of MySQL which is running.
921 * @uses ./libraries/charset_conversion.lib.php
922 * @uses PMA_DBI_QUERY_STORE
923 * @uses PMA_MYSQL_INT_VERSION to set it
924 * @uses PMA_MYSQL_STR_VERSION to set it
925 * @uses $_SESSION['PMA_MYSQL_INT_VERSION'] for caching
926 * @uses $_SESSION['PMA_MYSQL_STR_VERSION'] for caching
927 * @uses PMA_DBI_GETVAR_SESSION
928 * @uses PMA_DBI_fetch_value()
929 * @uses PMA_DBI_query()
930 * @uses PMA_DBI_get_variable()
931 * @uses $GLOBALS['collation_connection']
932 * @uses $GLOBALS['available_languages']
933 * @uses $GLOBALS['mysql_charset_map']
934 * @uses $GLOBALS['charset']
935 * @uses $GLOBALS['lang']
936 * @uses $GLOBALS['server']
937 * @uses $GLOBALS['cfg']['Lang']
938 * @uses defined()
939 * @uses explode()
940 * @uses sprintf()
941 * @uses intval()
942 * @uses define()
943 * @uses defined()
944 * @uses substr()
945 * @uses count()
946 * @param mixed $link mysql link resource|object
947 * @param boolean $is_controluser
949 function PMA_DBI_postConnect($link, $is_controluser = false)
951 if (! defined('PMA_MYSQL_INT_VERSION')) {
952 if (PMA_cacheExists('PMA_MYSQL_INT_VERSION', true)) {
953 define('PMA_MYSQL_INT_VERSION', PMA_cacheGet('PMA_MYSQL_INT_VERSION', true));
954 define('PMA_MYSQL_STR_VERSION', PMA_cacheGet('PMA_MYSQL_STR_VERSION', true));
955 } else {
956 $mysql_version = PMA_DBI_fetch_value(
957 'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE);
958 if ($mysql_version) {
959 $match = explode('.', $mysql_version);
960 define('PMA_MYSQL_INT_VERSION',
961 (int) sprintf('%d%02d%02d', $match[0], $match[1],
962 intval($match[2])));
963 define('PMA_MYSQL_STR_VERSION', $mysql_version);
964 unset($mysql_version, $match);
965 } else {
966 define('PMA_MYSQL_INT_VERSION', 50015);
967 define('PMA_MYSQL_STR_VERSION', '5.00.15');
969 PMA_cacheSet('PMA_MYSQL_INT_VERSION', PMA_MYSQL_INT_VERSION, true);
970 PMA_cacheSet('PMA_MYSQL_STR_VERSION', PMA_MYSQL_STR_VERSION, true);
974 if (! empty($GLOBALS['collation_connection'])) {
975 PMA_DBI_query("SET CHARACTER SET 'utf8';", $link, PMA_DBI_QUERY_STORE);
976 $mysql_charset = explode('_', $GLOBALS['collation_connection']);
977 PMA_DBI_query("SET collation_connection = '" . PMA_sqlAddslashes($GLOBALS['collation_connection']) . "';", $link, PMA_DBI_QUERY_STORE);
978 } else {
979 PMA_DBI_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci';", $link, PMA_DBI_QUERY_STORE);
984 * returns a single value from the given result or query,
985 * if the query or the result has more than one row or field
986 * the first field of the first row is returned
988 * <code>
989 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
990 * $user_name = PMA_DBI_fetch_value($sql);
991 * // produces
992 * // $user_name = 'John Doe'
993 * </code>
995 * @uses is_string()
996 * @uses is_int()
997 * @uses PMA_DBI_try_query()
998 * @uses PMA_DBI_num_rows()
999 * @uses PMA_DBI_fetch_row()
1000 * @uses PMA_DBI_fetch_assoc()
1001 * @uses PMA_DBI_free_result()
1002 * @param string|mysql_result $result query or mysql result
1003 * @param integer $row_number row to fetch the value from,
1004 * starting at 0, with 0 beeing default
1005 * @param integer|string $field field to fetch the value from,
1006 * starting at 0, with 0 beeing default
1007 * @param resource $link mysql link
1008 * @param mixed $options
1009 * @return mixed value of first field in first row from result
1010 * or false if not found
1012 function PMA_DBI_fetch_value($result, $row_number = 0, $field = 0, $link = null, $options = 0) {
1013 $value = false;
1015 if (is_string($result)) {
1016 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1019 // return false if result is empty or false
1020 // or requested row is larger than rows in result
1021 if (PMA_DBI_num_rows($result) < ($row_number + 1)) {
1022 return $value;
1025 // if $field is an integer use non associative mysql fetch function
1026 if (is_int($field)) {
1027 $fetch_function = 'PMA_DBI_fetch_row';
1028 } else {
1029 $fetch_function = 'PMA_DBI_fetch_assoc';
1032 // get requested row
1033 for ($i = 0; $i <= $row_number; $i++) {
1034 $row = $fetch_function($result);
1036 PMA_DBI_free_result($result);
1038 // return requested field
1039 if (isset($row[$field])) {
1040 $value = $row[$field];
1042 unset($row);
1044 return $value;
1048 * returns only the first row from the result
1050 * <code>
1051 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
1052 * $user = PMA_DBI_fetch_single_row($sql);
1053 * // produces
1054 * // $user = array('id' => 123, 'name' => 'John Doe')
1055 * </code>
1057 * @uses is_string()
1058 * @uses PMA_DBI_try_query()
1059 * @uses PMA_DBI_num_rows()
1060 * @uses PMA_DBI_fetch_row()
1061 * @uses PMA_DBI_fetch_assoc()
1062 * @uses PMA_DBI_fetch_array()
1063 * @uses PMA_DBI_free_result()
1064 * @param string|mysql_result $result query or mysql result
1065 * @param string $type NUM|ASSOC|BOTH
1066 * returned array should either numeric
1067 * associativ or booth
1068 * @param resource $link mysql link
1069 * @param mixed $options
1070 * @return array|boolean first row from result
1071 * or false if result is empty
1073 function PMA_DBI_fetch_single_row($result, $type = 'ASSOC', $link = null, $options = 0) {
1074 if (is_string($result)) {
1075 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1078 // return null if result is empty or false
1079 if (! PMA_DBI_num_rows($result)) {
1080 return false;
1083 switch ($type) {
1084 case 'NUM' :
1085 $fetch_function = 'PMA_DBI_fetch_row';
1086 break;
1087 case 'ASSOC' :
1088 $fetch_function = 'PMA_DBI_fetch_assoc';
1089 break;
1090 case 'BOTH' :
1091 default :
1092 $fetch_function = 'PMA_DBI_fetch_array';
1093 break;
1096 $row = $fetch_function($result);
1097 PMA_DBI_free_result($result);
1098 return $row;
1102 * returns all rows in the resultset in one array
1104 * <code>
1105 * $sql = 'SELECT * FROM `user`';
1106 * $users = PMA_DBI_fetch_result($sql);
1107 * // produces
1108 * // $users[] = array('id' => 123, 'name' => 'John Doe')
1110 * $sql = 'SELECT `id`, `name` FROM `user`';
1111 * $users = PMA_DBI_fetch_result($sql, 'id');
1112 * // produces
1113 * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
1115 * $sql = 'SELECT `id`, `name` FROM `user`';
1116 * $users = PMA_DBI_fetch_result($sql, 0);
1117 * // produces
1118 * // $users['123'] = array(0 => 123, 1 => 'John Doe')
1120 * $sql = 'SELECT `id`, `name` FROM `user`';
1121 * $users = PMA_DBI_fetch_result($sql, 'id', 'name');
1122 * // or
1123 * $users = PMA_DBI_fetch_result($sql, 0, 1);
1124 * // produces
1125 * // $users['123'] = 'John Doe'
1127 * $sql = 'SELECT `name` FROM `user`';
1128 * $users = PMA_DBI_fetch_result($sql);
1129 * // produces
1130 * // $users[] = 'John Doe'
1132 * $sql = 'SELECT `group`, `name` FROM `user`'
1133 * $users = PMA_DBI_fetch_result($sql, array('group', null), 'name');
1134 * // produces
1135 * // $users['admin'][] = 'John Doe'
1137 * $sql = 'SELECT `group`, `name` FROM `user`'
1138 * $users = PMA_DBI_fetch_result($sql, array('group', 'name'), 'id');
1139 * // produces
1140 * // $users['admin']['John Doe'] = '123'
1141 * </code>
1143 * @uses is_string()
1144 * @uses is_int()
1145 * @uses PMA_DBI_try_query()
1146 * @uses PMA_DBI_num_rows()
1147 * @uses PMA_DBI_num_fields()
1148 * @uses PMA_DBI_fetch_row()
1149 * @uses PMA_DBI_fetch_assoc()
1150 * @uses PMA_DBI_free_result()
1151 * @param string|mysql_result $result query or mysql result
1152 * @param string|integer $key field-name or offset
1153 * used as key for array
1154 * @param string|integer $value value-name or offset
1155 * used as value for array
1156 * @param resource $link mysql link
1157 * @param mixed $options
1158 * @return array resultrows or values indexed by $key
1160 function PMA_DBI_fetch_result($result, $key = null, $value = null,
1161 $link = null, $options = 0)
1163 $resultrows = array();
1165 if (is_string($result)) {
1166 $result = PMA_DBI_try_query($result, $link, $options);
1169 // return empty array if result is empty or false
1170 if (! $result) {
1171 return $resultrows;
1174 $fetch_function = 'PMA_DBI_fetch_assoc';
1176 // no nested array if only one field is in result
1177 if (null === $key && 1 === PMA_DBI_num_fields($result)) {
1178 $value = 0;
1179 $fetch_function = 'PMA_DBI_fetch_row';
1182 // if $key is an integer use non associative mysql fetch function
1183 if (is_int($key)) {
1184 $fetch_function = 'PMA_DBI_fetch_row';
1187 if (null === $key && null === $value) {
1188 while ($row = $fetch_function($result)) {
1189 $resultrows[] = $row;
1191 } elseif (null === $key) {
1192 while ($row = $fetch_function($result)) {
1193 $resultrows[] = $row[$value];
1195 } elseif (null === $value) {
1196 if (is_array($key)) {
1197 while ($row = $fetch_function($result)) {
1198 $result_target =& $resultrows;
1199 foreach ($key as $key_index) {
1200 if (null === $key_index) {
1201 $result_target =& $result_target[];
1202 continue;
1205 if (! isset($result_target[$row[$key_index]])) {
1206 $result_target[$row[$key_index]] = array();
1208 $result_target =& $result_target[$row[$key_index]];
1210 $result_target = $row;
1212 } else {
1213 while ($row = $fetch_function($result)) {
1214 $resultrows[$row[$key]] = $row;
1217 } else {
1218 if (is_array($key)) {
1219 while ($row = $fetch_function($result)) {
1220 $result_target =& $resultrows;
1221 foreach ($key as $key_index) {
1222 if (null === $key_index) {
1223 $result_target =& $result_target[];
1224 continue;
1227 if (! isset($result_target[$row[$key_index]])) {
1228 $result_target[$row[$key_index]] = array();
1230 $result_target =& $result_target[$row[$key_index]];
1232 $result_target = $row[$value];
1234 } else {
1235 while ($row = $fetch_function($result)) {
1236 $resultrows[$row[$key]] = $row[$value];
1241 PMA_DBI_free_result($result);
1242 return $resultrows;
1246 * return default table engine for given database
1248 * @return string default table engine
1250 function PMA_DBI_get_default_engine()
1252 return PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'storage_engine\';', 0, 1);
1256 * Get supported SQL compatibility modes
1258 * @return array supported SQL compatibility modes
1260 function PMA_DBI_getCompatibilities()
1262 $compats = array('NONE');
1263 $compats[] = 'ANSI';
1264 $compats[] = 'DB2';
1265 $compats[] = 'MAXDB';
1266 $compats[] = 'MYSQL323';
1267 $compats[] = 'MYSQL40';
1268 $compats[] = 'MSSQL';
1269 $compats[] = 'ORACLE';
1270 // removed; in MySQL 5.0.33, this produces exports that
1271 // can't be read by POSTGRESQL (see our bug #1596328)
1272 //$compats[] = 'POSTGRESQL';
1273 $compats[] = 'TRADITIONAL';
1275 return $compats;
1279 * returns warnings for last query
1281 * @uses $GLOBALS['userlink']
1282 * @uses PMA_DBI_fetch_result()
1283 * @param resource mysql link $link mysql link resource
1284 * @return array warnings
1286 function PMA_DBI_get_warnings($link = null)
1288 if (empty($link)) {
1289 if (isset($GLOBALS['userlink'])) {
1290 $link = $GLOBALS['userlink'];
1291 } else {
1292 return array();
1296 return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1300 * returns true (int > 0) if current user is superuser
1301 * otherwise 0
1303 * @uses $_SESSION['is_superuser'] for caching
1304 * @uses $GLOBALS['userlink']
1305 * @uses $GLOBALS['server']
1306 * @uses PMA_DBI_try_query()
1307 * @uses PMA_DBI_QUERY_STORE
1308 * @return integer $is_superuser
1310 function PMA_isSuperuser()
1312 if (PMA_cacheExists('is_superuser', true)) {
1313 return PMA_cacheGet('is_superuser', true);
1316 // with mysql extension, when connection failed we don't have
1317 // a $userlink
1318 if (isset($GLOBALS['userlink'])) {
1319 $r = (bool) PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user', $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
1320 PMA_cacheSet('is_superuser', $r, true);
1321 } else {
1322 PMA_cacheSet('is_superuser', false, true);
1325 return PMA_cacheGet('is_superuser', true);
1329 * returns an array of PROCEDURE or FUNCTION names for a db
1331 * @uses PMA_DBI_free_result()
1332 * @param string $db db name
1333 * @param string $which PROCEDURE | FUNCTION
1334 * @param resource $link mysql link
1336 * @return array the procedure names or function names
1338 function PMA_DBI_get_procedures_or_functions($db, $which, $link = null)
1340 $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1341 $result = array();
1342 foreach ($shows as $one_show) {
1343 if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1344 $result[] = $one_show['Name'];
1347 return($result);
1351 * returns the definition of a specific PROCEDURE, FUNCTION or EVENT
1353 * @uses PMA_DBI_fetch_value()
1354 * @param string $db db name
1355 * @param string $which PROCEDURE | FUNCTION | EVENT
1356 * @param string $name the procedure|function|event name
1357 * @param resource $link mysql link
1359 * @return string the definition
1361 function PMA_DBI_get_definition($db, $which, $name, $link = null)
1363 $returned_field = array(
1364 'PROCEDURE' => 'Create Procedure',
1365 'FUNCTION' => 'Create Function',
1366 'EVENT' => 'Create Event'
1368 $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($name);
1369 return(PMA_DBI_fetch_value($query, 0, $returned_field[$which]));
1373 * returns details about the TRIGGERs of a specific table
1375 * @uses PMA_DBI_fetch_result()
1376 * @param string $db db name
1377 * @param string $table table name
1378 * @param string $delimiter the delimiter to use (may be empty)
1380 * @return array information about triggers (may be empty)
1382 function PMA_DBI_get_triggers($db, $table, $delimiter = '//')
1384 $result = array();
1386 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1387 // Note: in http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
1388 // their example uses WHERE TRIGGER_SCHEMA='dbname' so let's use this
1389 // instead of WHERE EVENT_OBJECT_SCHEMA='dbname'
1390 $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) . "';");
1391 } else {
1392 $triggers = PMA_DBI_fetch_result("SHOW TRIGGERS FROM " . PMA_sqlAddslashes($db,true) . " LIKE '" . PMA_sqlAddslashes($table, true) . "';");
1395 if ($triggers) {
1396 foreach ($triggers as $trigger) {
1397 if ($GLOBALS['cfg']['Server']['DisableIS']) {
1398 $trigger['TRIGGER_NAME'] = $trigger['Trigger'];
1399 $trigger['ACTION_TIMING'] = $trigger['Timing'];
1400 $trigger['EVENT_MANIPULATION'] = $trigger['Event'];
1401 $trigger['EVENT_OBJECT_TABLE'] = $trigger['Table'];
1402 $trigger['ACTION_STATEMENT'] = $trigger['Statement'];
1404 $one_result = array();
1405 $one_result['name'] = $trigger['TRIGGER_NAME'];
1406 $one_result['action_timing'] = $trigger['ACTION_TIMING'];
1407 $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
1409 // do not prepend the schema name; this way, importing the
1410 // definition into another schema will work
1411 $one_result['full_trigger_name'] = PMA_backquote($trigger['TRIGGER_NAME']);
1412 $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
1413 $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";
1415 $result[] = $one_result;
1418 return($result);
1422 * Returns TRUE if $db.$view_name is a view, FALSE if not
1424 * @uses PMA_DBI_fetch_result()
1425 * @param string $db database name
1426 * @param string $view_name view/table name
1428 * @return bool TRUE if $db.$view_name is a view, FALSE if not
1430 function PMA_isView($db, $view_name)
1432 $result = PMA_DBI_fetch_result("SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$db."' and TABLE_NAME = '".$view_name."';");
1434 if ($result) {
1435 return TRUE;
1436 } else {
1437 return FALSE;