bug #2042032 Cannot detect PmaAbsoluteUri correctly on Windows
[phpmyadmin/madhuracj.git] / libraries / database_interface.lib.php
bloba7d9e72bb06dffc8627141688fb507ebe6bb6f32
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Common Option Constants For DBI Functions
6 * @version $Id$
7 * @package phpMyAdmin
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 // PMA_DBI_try_query()
17 define('PMA_DBI_QUERY_STORE', 1); // Force STORE_RESULT method, ignored by classic MySQL.
18 define('PMA_DBI_QUERY_UNBUFFERED', 2); // Do not read whole query
19 // PMA_DBI_get_variable()
20 define('PMA_DBI_GETVAR_SESSION', 1);
21 define('PMA_DBI_GETVAR_GLOBAL', 2);
23 /**
24 * Checks one of the mysql extensions
26 * @param string $extension mysql extension to check
28 function PMA_DBI_checkMysqlExtension($extension = 'mysql') {
29 if (! function_exists($extension . '_connect')) {
30 return false;
33 return true;
36 /**
37 * check for requested extension
39 if (! PMA_DBI_checkMysqlExtension($GLOBALS['cfg']['Server']['extension'])) {
41 // if it fails try alternative extension ...
42 // and display an error ...
44 /**
45 * @todo add different messages for alternative extension
46 * and complete fail (no alternative extension too)
48 $error =
49 sprintf(PMA_sanitize($GLOBALS['strCantLoad']),
50 $GLOBALS['cfg']['Server']['extension'])
51 .' - <a href="./Documentation.html#faqmysql" target="documentation">'
52 .$GLOBALS['strDocu'] . '</a>';
53 trigger_error($error, E_USER_ERROR);
55 if ($GLOBALS['cfg']['Server']['extension'] === 'mysql') {
56 $alternativ_extension = 'mysqli';
57 } else {
58 $alternativ_extension = 'mysql';
61 if (! PMA_DBI_checkMysqlExtension($alternativ_extension)) {
62 // if alternative fails too ...
63 PMA_fatalError(
64 sprintf($GLOBALS['strCantLoad'],
65 $GLOBALS['cfg']['Server']['extension'])
66 . ' - [a@./Documentation.html#faqmysql@documentation]'
67 . $GLOBALS['strDocu'] . '[/a]');
70 $GLOBALS['cfg']['Server']['extension'] = $alternativ_extension;
71 unset($alternativ_extension);
74 /**
75 * Including The DBI Plugin
77 require_once './libraries/dbi/' . $GLOBALS['cfg']['Server']['extension'] . '.dbi.lib.php';
79 /**
80 * Common Functions
82 function PMA_DBI_query($query, $link = null, $options = 0) {
83 $res = PMA_DBI_try_query($query, $link, $options)
84 or PMA_mysqlDie(PMA_DBI_getError($link), $query);
85 return $res;
88 /**
89 * converts charset of a mysql message, usually coming from mysql_error(),
90 * into PMA charset, usally UTF-8
91 * uses language to charset mapping from mysql/share/errmsg.txt
92 * and charset names to ISO charset from information_schema.CHARACTER_SETS
94 * @uses $GLOBALS['cfg']['IconvExtraParams']
95 * @uses $GLOBALS['charset'] as target charset
96 * @uses PMA_DBI_fetch_value() to get server_language
97 * @uses preg_match() to filter server_language
98 * @uses in_array()
99 * @uses function_exists() to check for a convert function
100 * @uses iconv() to convert message
101 * @uses libiconv() to convert message
102 * @uses recode_string() to convert message
103 * @uses mb_convert_encoding() to convert message
104 * @param string $message
105 * @return string $message
107 function PMA_DBI_convert_message($message) {
108 // latin always last!
109 $encodings = array(
110 'japanese' => 'EUC-JP', //'ujis',
111 'japanese-sjis' => 'Shift-JIS', //'sjis',
112 'korean' => 'EUC-KR', //'euckr',
113 'russian' => 'KOI8-R', //'koi8r',
114 'ukrainian' => 'KOI8-U', //'koi8u',
115 'greek' => 'ISO-8859-7', //'greek',
116 'serbian' => 'CP1250', //'cp1250',
117 'estonian' => 'ISO-8859-13', //'latin7',
118 'slovak' => 'ISO-8859-2', //'latin2',
119 'czech' => 'ISO-8859-2', //'latin2',
120 'hungarian' => 'ISO-8859-2', //'latin2',
121 'polish' => 'ISO-8859-2', //'latin2',
122 'romanian' => 'ISO-8859-2', //'latin2',
123 'spanish' => 'CP1252', //'latin1',
124 'swedish' => 'CP1252', //'latin1',
125 'italian' => 'CP1252', //'latin1',
126 'norwegian-ny' => 'CP1252', //'latin1',
127 'norwegian' => 'CP1252', //'latin1',
128 'portuguese' => 'CP1252', //'latin1',
129 'danish' => 'CP1252', //'latin1',
130 'dutch' => 'CP1252', //'latin1',
131 'english' => 'CP1252', //'latin1',
132 'french' => 'CP1252', //'latin1',
133 'german' => 'CP1252', //'latin1',
136 if ($server_language = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'language\';', 0, 1)) {
137 $found = array();
138 if (preg_match('&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $server_language, $found)) {
139 $server_language = $found[1];
143 if (! empty($server_language) && isset($encodings[$server_language])) {
144 if (function_exists('iconv')) {
145 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
146 require_once './libraries/iconv_wrapper.lib.php';
147 $message = PMA_aix_iconv_wrapper($encodings[$server_language],
148 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
149 } else {
150 $message = iconv($encodings[$server_language],
151 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
153 } elseif (function_exists('recode_string')) {
154 $message = recode_string($encodings[$server_language] . '..' . $GLOBALS['charset'],
155 $message);
156 } elseif (function_exists('libiconv')) {
157 $message = libiconv($encodings[$server_language], $GLOBALS['charset'], $message);
158 } elseif (function_exists('mb_convert_encoding')) {
159 // do not try unsupported charsets
160 if (! in_array($server_language, array('ukrainian', 'greek', 'serbian'))) {
161 $message = mb_convert_encoding($message, $GLOBALS['charset'],
162 $encodings[$server_language]);
165 } else {
167 * @todo lang not found, try all, what TODO ?
171 return $message;
175 * returns array with table names for given db
177 * @param string $database name of database
178 * @param mixed $link mysql link resource|object
179 * @return array tables names
181 function PMA_DBI_get_tables($database, $link = null)
183 return PMA_DBI_fetch_result('SHOW TABLES FROM ' . PMA_backquote($database) . ';',
184 null, 0, $link, PMA_DBI_QUERY_STORE);
188 * usort comparison callback
190 * @param string $a first argument to sort
191 * @param string $b second argument to sort
193 * @return integer a value representing whether $a should be before $b in the
194 * sorted array or not
196 * @global string the column the array shall be sorted by
197 * @global string the sorting order ('ASC' or 'DESC')
199 * @access private
201 function PMA_usort_comparison_callback($a, $b)
203 if ($GLOBALS['cfg']['NaturalOrder']) {
204 $sorter = 'strnatcasecmp';
205 } else {
206 $sorter = 'strcasecmp';
208 // produces f.e.:
209 // return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
210 return ($GLOBALS['callback_sort_order'] == 'ASC' ? 1 : -1) * $sorter($a[$GLOBALS['callback_sort_by']], $b[$GLOBALS['callback_sort_by']]);
211 } // end of the 'PMA_usort_comparison_callback()' function
214 * returns array of all tables in given db or dbs
215 * this function expects unquoted names:
216 * RIGHT: my_database
217 * WRONG: `my_database`
218 * WRONG: my\_database
219 * if $tbl_is_group is true, $table is used as filter for table names
220 * if $tbl_is_group is 'comment, $table is used as filter for table comments
222 * <code>
223 * PMA_DBI_get_tables_full('my_database');
224 * PMA_DBI_get_tables_full('my_database', 'my_table'));
225 * PMA_DBI_get_tables_full('my_database', 'my_tables_', true));
226 * PMA_DBI_get_tables_full('my_database', 'my_tables_', 'comment'));
227 * </code>
229 * @todo move into PMA_Table
230 * @uses PMA_DBI_fetch_result()
231 * @uses PMA_escape_mysql_wildcards()
232 * @uses PMA_backquote()
233 * @uses is_array()
234 * @uses addslashes()
235 * @uses strpos()
236 * @uses strtoupper()
237 * @param string $databases database
238 * @param string $table table
239 * @param boolean|string $tbl_is_group $table is a table group
240 * @param resource $link mysql link
241 * @param integer $limit_offset zero-based offset for the count
242 * @param boolean|integer $limit_count number of tables to return
243 * @param string $sort_by table attribute to sort by
244 * @param string $sort_order direction to sort (ASC or DESC)
245 * @return array list of tables in given db(s)
247 function PMA_DBI_get_tables_full($database, $table = false, $tbl_is_group = false, $link = null,
248 $limit_offset = 0, $limit_count = false, $sort_by = 'Name', $sort_order = 'ASC')
250 require_once './libraries/Table.class.php';
252 if (true === $limit_count) {
253 $limit_count = $GLOBALS['cfg']['MaxTableList'];
255 // prepare and check parameters
256 if (! is_array($database)) {
257 $databases = array($database);
258 } else {
259 $databases = $database;
262 $tables = array();
264 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
265 // get table information from information_schema
266 if ($table) {
267 if (true === $tbl_is_group) {
268 $sql_where_table = 'AND `TABLE_NAME` LIKE \''
269 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
270 } elseif ('comment' === $tbl_is_group) {
271 $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
272 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
273 } else {
274 $sql_where_table = 'AND `TABLE_NAME` = \'' . addslashes($table) . '\'';
276 } else {
277 $sql_where_table = '';
280 // for PMA bc:
281 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
283 // on non-Windows servers,
284 // added BINARY in the WHERE clause to force a case sensitive
285 // comparison (if we are looking for the db Aa we don't want
286 // to find the db aa)
287 $this_databases = array_map('PMA_sqlAddslashes', $databases);
289 $sql = '
290 SELECT *,
291 `TABLE_SCHEMA` AS `Db`,
292 `TABLE_NAME` AS `Name`,
293 `TABLE_TYPE` ÀS `TABLE_TYPE`,
294 `ENGINE` AS `Engine`,
295 `ENGINE` AS `Type`,
296 `VERSION` AS `Version`,
297 `ROW_FORMAT` AS `Row_format`,
298 `TABLE_ROWS` AS `Rows`,
299 `AVG_ROW_LENGTH` AS `Avg_row_length`,
300 `DATA_LENGTH` AS `Data_length`,
301 `MAX_DATA_LENGTH` AS `Max_data_length`,
302 `INDEX_LENGTH` AS `Index_length`,
303 `DATA_FREE` AS `Data_free`,
304 `AUTO_INCREMENT` AS `Auto_increment`,
305 `CREATE_TIME` AS `Create_time`,
306 `UPDATE_TIME` AS `Update_time`,
307 `CHECK_TIME` AS `Check_time`,
308 `TABLE_COLLATION` AS `Collation`,
309 `CHECKSUM` AS `Checksum`,
310 `CREATE_OPTIONS` AS `Create_options`,
311 `TABLE_COMMENT` AS `Comment`
312 FROM `information_schema`.`TABLES`
313 WHERE ' . (PMA_IS_WINDOWS ? '' : 'BINARY') . ' `TABLE_SCHEMA` IN (\'' . implode("', '", $this_databases) . '\')
314 ' . $sql_where_table;
316 // Sort the tables
317 $sql .= " ORDER BY $sort_by $sort_order";
319 if ($limit_count) {
320 $sql .= ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
323 $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
324 null, $link);
325 unset($sql_where_table, $sql);
326 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
327 // here, the array's first key is by schema name
328 foreach($tables as $one_database_name => $one_database_tables) {
329 uksort($one_database_tables, 'strnatcasecmp');
331 if ($sort_order == 'DESC') {
332 $one_database_tables = array_reverse($one_database_tables);
334 $tables[$one_database_name] = $one_database_tables;
337 } // end (get information from table schema)
339 // If permissions are wrong on even one database directory,
340 // information_schema does not return any table info for any database
341 // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002
342 if (empty($tables)) {
343 foreach ($databases as $each_database) {
344 if ($table || (true === $tbl_is_group)) {
345 $sql = 'SHOW TABLE STATUS FROM '
346 . PMA_backquote($each_database)
347 .' LIKE \'' . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
348 } else {
349 $sql = 'SHOW TABLE STATUS FROM '
350 . PMA_backquote($each_database);
353 $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
355 // Sort naturally if the config allows it and we're sorting
356 // the Name column.
357 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
358 uksort($each_tables, 'strnatcasecmp');
360 if ($sort_order == 'DESC') {
361 $each_tables = array_reverse($each_tables);
363 } else {
364 // Prepare to sort by creating array of the selected sort
365 // value to pass to array_multisort
366 foreach ($each_tables as $table_name => $table_data) {
367 ${$sort_by}[$table_name] = strtolower($table_data[$sort_by]);
370 if ($sort_order == 'DESC') {
371 array_multisort($$sort_by, SORT_DESC, $each_tables);
372 } else {
373 array_multisort($$sort_by, SORT_ASC, $each_tables);
376 // cleanup the temporary sort array
377 unset($$sort_by);
380 if ($limit_count) {
381 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
384 foreach ($each_tables as $table_name => $each_table) {
385 if ('comment' === $tbl_is_group
386 && 0 === strpos($each_table['Comment'], $table))
388 // remove table from list
389 unset($each_tables[$table_name]);
390 continue;
393 if (! isset($each_tables[$table_name]['Type'])
394 && isset($each_tables[$table_name]['Engine'])) {
395 // pma BC, same parts of PMA still uses 'Type'
396 $each_tables[$table_name]['Type']
397 =& $each_tables[$table_name]['Engine'];
398 } elseif (! isset($each_tables[$table_name]['Engine'])
399 && isset($each_tables[$table_name]['Type'])) {
400 // old MySQL reports Type, newer MySQL reports Engine
401 $each_tables[$table_name]['Engine']
402 =& $each_tables[$table_name]['Type'];
405 // MySQL forward compatibility
406 // so pma could use this array as if every server is of version >5.0
407 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
408 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
409 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
410 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
411 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
412 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
413 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
414 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
415 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
416 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
417 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
418 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
419 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
420 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
421 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
422 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
423 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
424 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
425 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
427 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW'
428 && $each_tables[$table_name]['Engine'] == NULL) {
429 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
430 } else {
432 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
434 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
438 $tables[$each_database] = $each_tables;
442 // cache table data
443 // so PMA_Table does not require to issue SHOW TABLE STATUS again
444 // Note: I don't see why we would need array_merge_recursive() here,
445 // as it creates double entries for the same table (for example a double
446 // entry for Comment when changing the storage engine in Operations)
447 // Note 2: Instead of array_merge(), simply use the + operator because
448 // array_merge() renumbers numeric keys starting with 0, therefore
449 // we would lose a db name thats consists only of numbers
450 foreach($tables as $one_database => $its_tables) {
451 if (isset(PMA_Table::$cache[$one_database])) {
452 PMA_Table::$cache[$one_database] = PMA_Table::$cache[$one_database] + $tables[$one_database];
453 } else {
454 PMA_Table::$cache[$one_database] = $tables[$one_database];
457 unset($one_database, $its_tables);
459 if (! is_array($database)) {
460 if (isset($tables[$database])) {
461 return $tables[$database];
462 } elseif (isset($tables[strtolower($database)])) {
463 // on windows with lower_case_table_names = 1
464 // MySQL returns
465 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
466 // but information_schema.TABLES gives `test`
467 // bug #1436171
468 // http://sf.net/support/tracker.php?aid=1436171
469 return $tables[strtolower($database)];
470 } else {
471 return $tables;
473 } else {
474 return $tables;
479 * returns array with databases containing extended infos about them
481 * @todo move into PMA_List_Database?
482 * @param string $databases database
483 * @param boolean $force_stats retrieve stats also for MySQL < 5
484 * @param resource $link mysql link
485 * @param string $sort_by column to order by
486 * @param string $sort_order ASC or DESC
487 * @param integer $limit_offset starting offset for LIMIT
488 * @param bool|int $limit_count row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
489 * @return array $databases
491 function PMA_DBI_get_databases_full($database = null, $force_stats = false,
492 $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
493 $limit_offset = 0, $limit_count = false)
495 $sort_order = strtoupper($sort_order);
497 if (true === $limit_count) {
498 $limit_count = $GLOBALS['cfg']['MaxDbList'];
501 // initialize to avoid errors when there are no databases
502 $databases = array();
504 $apply_limit_and_order_manual = true;
506 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
508 * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
509 * cause MySQL does not support natural ordering, we have to do it afterward
511 if ($GLOBALS['cfg']['NaturalOrder']) {
512 $limit = '';
513 } else {
514 if ($limit_count) {
515 $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
518 $apply_limit_and_order_manual = false;
521 // get table information from information_schema
522 if ($database) {
523 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
524 . addslashes($database) . '\'';
525 } else {
526 $sql_where_schema = '';
529 // for PMA bc:
530 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
531 $sql = '
532 SELECT `information_schema`.`SCHEMATA`.*';
533 if ($force_stats) {
534 $sql .= ',
535 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
536 AS `SCHEMA_TABLES`,
537 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
538 AS `SCHEMA_TABLE_ROWS`,
539 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
540 AS `SCHEMA_DATA_LENGTH`,
541 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
542 AS `SCHEMA_MAX_DATA_LENGTH`,
543 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
544 AS `SCHEMA_INDEX_LENGTH`,
545 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
546 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
547 AS `SCHEMA_LENGTH`,
548 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
549 AS `SCHEMA_DATA_FREE`';
551 $sql .= '
552 FROM `information_schema`.`SCHEMATA`';
553 if ($force_stats) {
554 $sql .= '
555 LEFT JOIN `information_schema`.`TABLES`
556 ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
557 = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
559 $sql .= '
560 ' . $sql_where_schema . '
561 GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
562 ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
563 . $limit;
564 $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
566 $mysql_error = PMA_DBI_getError($link);
567 if (! count($databases) && $GLOBALS['errno']) {
568 PMA_mysqlDie($mysql_error, $sql);
571 // display only databases also in official database list
572 // f.e. to apply hide_db and only_db
573 $drops = array_diff(array_keys($databases), (array) $GLOBALS['pma']->databases);
574 if (count($drops)) {
575 foreach ($drops as $drop) {
576 unset($databases[$drop]);
578 unset($drop);
580 unset($sql_where_schema, $sql, $drops);
581 } else {
582 foreach ($GLOBALS['pma']->databases as $database_name) {
583 // MySQL forward compatibility
584 // so pma could use this array as if every server is of version >5.0
585 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
587 if ($force_stats) {
588 require_once './libraries/mysql_charsets.lib.php';
590 $databases[$database_name]['DEFAULT_COLLATION_NAME']
591 = PMA_getDbCollation($database_name);
593 // get additional info about tables
594 $databases[$database_name]['SCHEMA_TABLES'] = 0;
595 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
596 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
597 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
598 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
599 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
600 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
602 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($database_name) . ';');
603 while ($row = PMA_DBI_fetch_assoc($res)) {
604 $databases[$database_name]['SCHEMA_TABLES']++;
605 $databases[$database_name]['SCHEMA_TABLE_ROWS']
606 += $row['Rows'];
607 $databases[$database_name]['SCHEMA_DATA_LENGTH']
608 += $row['Data_length'];
609 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
610 += $row['Max_data_length'];
611 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
612 += $row['Index_length'];
614 // for InnoDB, this does not contain the number of
615 // overhead bytes but the total free space
616 if ('InnoDB' != $row['Engine']) {
617 $databases[$database_name]['SCHEMA_DATA_FREE']
618 += $row['Data_free'];
620 $databases[$database_name]['SCHEMA_LENGTH']
621 += $row['Data_length'] + $row['Index_length'];
623 PMA_DBI_free_result($res);
624 unset($res);
631 * apply limit and order manually now
632 * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
634 if ($apply_limit_and_order_manual) {
635 $GLOBALS['callback_sort_order'] = $sort_order;
636 $GLOBALS['callback_sort_by'] = $sort_by;
637 usort($databases, 'PMA_usort_comparison_callback');
638 unset($GLOBALS['callback_sort_order'], $GLOBALS['callback_sort_by']);
641 * now apply limit
643 if ($limit_count) {
644 $databases = array_slice($databases, $limit_offset, $limit_count);
648 return $databases;
652 * returns detailed array with all columns for given table in database,
653 * or all tables/databases
655 * @param string $database name of database
656 * @param string $table name of table to retrieve columns from
657 * @param string $column name of specific column
658 * @param mixed $link mysql link resource
660 function PMA_DBI_get_columns_full($database = null, $table = null,
661 $column = null, $link = null)
663 $columns = array();
665 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
666 $sql_wheres = array();
667 $array_keys = array();
669 // get columns information from information_schema
670 if (null !== $database) {
671 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . addslashes($database) . '\' ';
672 } else {
673 $array_keys[] = 'TABLE_SCHEMA';
675 if (null !== $table) {
676 $sql_wheres[] = '`TABLE_NAME` = \'' . addslashes($table) . '\' ';
677 } else {
678 $array_keys[] = 'TABLE_NAME';
680 if (null !== $column) {
681 $sql_wheres[] = '`COLUMN_NAME` = \'' . addslashes($column) . '\' ';
682 } else {
683 $array_keys[] = 'COLUMN_NAME';
686 // for PMA bc:
687 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
688 $sql = '
689 SELECT *,
690 `COLUMN_NAME` AS `Field`,
691 `COLUMN_TYPE` AS `Type`,
692 `COLLATION_NAME` AS `Collation`,
693 `IS_NULLABLE` AS `Null`,
694 `COLUMN_KEY` AS `Key`,
695 `COLUMN_DEFAULT` AS `Default`,
696 `EXTRA` AS `Extra`,
697 `PRIVILEGES` AS `Privileges`,
698 `COLUMN_COMMENT` AS `Comment`
699 FROM `information_schema`.`COLUMNS`';
700 if (count($sql_wheres)) {
701 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
704 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
705 unset($sql_wheres, $sql);
706 } else {
707 if (null === $database) {
708 foreach ($GLOBALS['pma']->databases as $database) {
709 $columns[$database] = PMA_DBI_get_columns_full($database, null,
710 null, $link);
712 return $columns;
713 } elseif (null === $table) {
714 $tables = PMA_DBI_get_tables($database);
715 foreach ($tables as $table) {
716 $columns[$table] = PMA_DBI_get_columns_full(
717 $database, $table, null, $link);
719 return $columns;
722 $sql = 'SHOW FULL COLUMNS FROM '
723 . PMA_backquote($database) . '.' . PMA_backquote($table);
724 if (null !== $column) {
725 $sql .= " LIKE '" . $column . "'";
728 $columns = PMA_DBI_fetch_result($sql, 'Field', null, $link);
730 $ordinal_position = 1;
731 foreach ($columns as $column_name => $each_column) {
733 // MySQL forward compatibility
734 // so pma could use this array as if every server is of version >5.0
735 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
736 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
737 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
738 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
739 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
740 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
741 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
742 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
743 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
745 $columns[$column_name]['TABLE_CATALOG'] = null;
746 $columns[$column_name]['TABLE_SCHEMA'] = $database;
747 $columns[$column_name]['TABLE_NAME'] = $table;
748 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
749 $columns[$column_name]['DATA_TYPE'] =
750 substr($columns[$column_name]['COLUMN_TYPE'], 0,
751 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
753 * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
755 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
757 * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
759 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
760 $columns[$column_name]['NUMERIC_PRECISION'] = null;
761 $columns[$column_name]['NUMERIC_SCALE'] = null;
762 $columns[$column_name]['CHARACTER_SET_NAME'] =
763 substr($columns[$column_name]['COLLATION_NAME'], 0,
764 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
766 $ordinal_position++;
769 if (null !== $column) {
770 reset($columns);
771 $columns = current($columns);
775 return $columns;
779 * @todo should only return columns names, for more info use PMA_DBI_get_columns_full()
781 * @deprecated by PMA_DBI_get_columns() or PMA_DBI_get_columns_full()
782 * @param string $database name of database
783 * @param string $table name of table to retrieve columns from
784 * @param mixed $link mysql link resource
785 * @return array column info
787 function PMA_DBI_get_fields($database, $table, $link = null)
789 // here we use a try_query because when coming from
790 // tbl_create + tbl_properties.inc.php, the table does not exist
791 $fields = PMA_DBI_fetch_result(
792 'SHOW FULL COLUMNS
793 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
794 null, null, $link);
795 if (! is_array($fields) || count($fields) < 1) {
796 return false;
798 return $fields;
802 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
804 * @param string $database name of database
805 * @param string $table name of table to retrieve columns from
806 * @param boolean $full whether to return full info or only column names
807 * @param mixed $link mysql link resource
808 * @return array column names
810 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
812 $fields = PMA_DBI_fetch_result(
813 'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
814 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
815 'Field', ($full ? null : 'Field'), $link);
816 if (! is_array($fields) || count($fields) < 1) {
817 return false;
819 return $fields;
823 * array PMA_DBI_get_column_values (string $database, string $table, string $column , mysql db link $link = null)
825 * @param string $database name of database
826 * @param string $table name of table to retrieve columns from
827 * @param string $column name of the column to retrieve data from
828 * @param mixed $link mysql link resource
829 * @return array $field_values
832 function PMA_DBI_get_column_values($database, $table, $column, $link = null)
834 $query = 'SELECT ';
835 for($i=0; $i< sizeof($column); $i++)
837 $query.= PMA_backquote($column[$i]);
838 if($i < (sizeof($column)-1))
840 $query.= ', ';
843 $query.= ' FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table);
844 $field_values = PMA_DBI_fetch_result($query, null, null, $link);
846 if (! is_array($field_values) || count($field_values) < 1) {
847 return false;
849 return $field_values;
852 * array PMA_DBI_get_table_data (string $database, string $table, mysql db link $link = null)
854 * @param string $database name of database
855 * @param string $table name of table to retrieve columns from
856 * @param mixed $link mysql link resource
857 * @return array $result
860 function PMA_DBI_get_table_data($database, $table, $link = null)
863 $result = PMA_DBI_fetch_result(
864 'SELECT * FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
865 null,null, $link);
867 if (! is_array($result) || count($result) < 1) {
868 return false;
870 return $result;
874 * array PMA_DBI_get_table_indexes($database, $table, $link = null)
876 * @param string $database name of database
877 * @param string $table name of the table whose indexes are to be retreived
878 * @param mixed $link mysql link resource
879 * @return array $indexes
882 function PMA_DBI_get_table_indexes($database, $table, $link = null)
885 $indexes = PMA_DBI_fetch_result(
886 'SHOW INDEXES FROM ' .PMA_backquote($database) . '.' . PMA_backquote($table),
887 null, null, $link);
889 if (! is_array($indexes) || count($indexes) < 1) {
890 return false;
892 return $indexes;
896 * returns value of given mysql server variable
898 * @param string $var mysql server variable name
899 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
900 * @param mixed $link mysql link resource|object
901 * @return mixed value for mysql server variable
905 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
907 if ($link === null) {
908 if (isset($GLOBALS['userlink'])) {
909 $link = $GLOBALS['userlink'];
910 } else {
911 return false;
915 switch ($type) {
916 case PMA_DBI_GETVAR_SESSION:
917 $modifier = ' SESSION';
918 break;
919 case PMA_DBI_GETVAR_GLOBAL:
920 $modifier = ' GLOBAL';
921 break;
922 default:
923 $modifier = '';
925 return PMA_DBI_fetch_value(
926 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
930 * Function called just after a connection to the MySQL database server has been established
931 * It sets the connection collation, and determins the version of MySQL which is running.
933 * @uses ./libraries/charset_conversion.lib.php
934 * @uses PMA_DBI_QUERY_STORE
935 * @uses PMA_MYSQL_INT_VERSION to set it
936 * @uses PMA_MYSQL_STR_VERSION to set it
937 * @uses $_SESSION['PMA_MYSQL_INT_VERSION'] for caching
938 * @uses $_SESSION['PMA_MYSQL_STR_VERSION'] for caching
939 * @uses PMA_DBI_GETVAR_SESSION
940 * @uses PMA_DBI_fetch_value()
941 * @uses PMA_DBI_query()
942 * @uses PMA_DBI_get_variable()
943 * @uses $GLOBALS['collation_connection']
944 * @uses $GLOBALS['available_languages']
945 * @uses $GLOBALS['mysql_charset_map']
946 * @uses $GLOBALS['charset']
947 * @uses $GLOBALS['lang']
948 * @uses $GLOBALS['server']
949 * @uses $GLOBALS['cfg']['Lang']
950 * @uses defined()
951 * @uses explode()
952 * @uses sprintf()
953 * @uses intval()
954 * @uses define()
955 * @uses defined()
956 * @uses substr()
957 * @uses count()
958 * @param mixed $link mysql link resource|object
959 * @param boolean $is_controluser
961 function PMA_DBI_postConnect($link, $is_controluser = false)
963 if (! defined('PMA_MYSQL_INT_VERSION')) {
964 if (PMA_cacheExists('PMA_MYSQL_INT_VERSION', true)) {
965 define('PMA_MYSQL_INT_VERSION', PMA_cacheGet('PMA_MYSQL_INT_VERSION', true));
966 define('PMA_MYSQL_STR_VERSION', PMA_cacheGet('PMA_MYSQL_STR_VERSION', true));
967 } else {
968 $mysql_version = PMA_DBI_fetch_value(
969 'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE);
970 if ($mysql_version) {
971 $match = explode('.', $mysql_version);
972 define('PMA_MYSQL_INT_VERSION',
973 (int) sprintf('%d%02d%02d', $match[0], $match[1],
974 intval($match[2])));
975 define('PMA_MYSQL_STR_VERSION', $mysql_version);
976 unset($mysql_version, $match);
977 } else {
978 define('PMA_MYSQL_INT_VERSION', 50015);
979 define('PMA_MYSQL_STR_VERSION', '5.00.15');
981 PMA_cacheSet('PMA_MYSQL_INT_VERSION', PMA_MYSQL_INT_VERSION, true);
982 PMA_cacheSet('PMA_MYSQL_STR_VERSION', PMA_MYSQL_STR_VERSION, true);
986 if (! empty($GLOBALS['collation_connection'])) {
987 PMA_DBI_query("SET CHARACTER SET 'utf8';", $link, PMA_DBI_QUERY_STORE);
988 $mysql_charset = explode('_', $GLOBALS['collation_connection']);
989 PMA_DBI_query("SET collation_connection = '" . PMA_sqlAddslashes($GLOBALS['collation_connection']) . "';", $link, PMA_DBI_QUERY_STORE);
990 } else {
991 PMA_DBI_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci';", $link, PMA_DBI_QUERY_STORE);
996 * returns a single value from the given result or query,
997 * if the query or the result has more than one row or field
998 * the first field of the first row is returned
1000 * <code>
1001 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
1002 * $user_name = PMA_DBI_fetch_value($sql);
1003 * // produces
1004 * // $user_name = 'John Doe'
1005 * </code>
1007 * @uses is_string()
1008 * @uses is_int()
1009 * @uses PMA_DBI_try_query()
1010 * @uses PMA_DBI_num_rows()
1011 * @uses PMA_DBI_fetch_row()
1012 * @uses PMA_DBI_fetch_assoc()
1013 * @uses PMA_DBI_free_result()
1014 * @param string|mysql_result $result query or mysql result
1015 * @param integer $row_number row to fetch the value from,
1016 * starting at 0, with 0 beeing default
1017 * @param integer|string $field field to fetch the value from,
1018 * starting at 0, with 0 beeing default
1019 * @param resource $link mysql link
1020 * @param mixed $options
1021 * @return mixed value of first field in first row from result
1022 * or false if not found
1024 function PMA_DBI_fetch_value($result, $row_number = 0, $field = 0, $link = null, $options = 0) {
1025 $value = false;
1027 if (is_string($result)) {
1028 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1031 // return false if result is empty or false
1032 // or requested row is larger than rows in result
1033 if (PMA_DBI_num_rows($result) < ($row_number + 1)) {
1034 return $value;
1037 // if $field is an integer use non associative mysql fetch function
1038 if (is_int($field)) {
1039 $fetch_function = 'PMA_DBI_fetch_row';
1040 } else {
1041 $fetch_function = 'PMA_DBI_fetch_assoc';
1044 // get requested row
1045 for ($i = 0; $i <= $row_number; $i++) {
1046 $row = $fetch_function($result);
1048 PMA_DBI_free_result($result);
1050 // return requested field
1051 if (isset($row[$field])) {
1052 $value = $row[$field];
1054 unset($row);
1056 return $value;
1060 * returns only the first row from the result
1062 * <code>
1063 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
1064 * $user = PMA_DBI_fetch_single_row($sql);
1065 * // produces
1066 * // $user = array('id' => 123, 'name' => 'John Doe')
1067 * </code>
1069 * @uses is_string()
1070 * @uses PMA_DBI_try_query()
1071 * @uses PMA_DBI_num_rows()
1072 * @uses PMA_DBI_fetch_row()
1073 * @uses PMA_DBI_fetch_assoc()
1074 * @uses PMA_DBI_fetch_array()
1075 * @uses PMA_DBI_free_result()
1076 * @param string|mysql_result $result query or mysql result
1077 * @param string $type NUM|ASSOC|BOTH
1078 * returned array should either numeric
1079 * associativ or booth
1080 * @param resource $link mysql link
1081 * @param mixed $options
1082 * @return array|boolean first row from result
1083 * or false if result is empty
1085 function PMA_DBI_fetch_single_row($result, $type = 'ASSOC', $link = null, $options = 0) {
1086 if (is_string($result)) {
1087 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1090 // return null if result is empty or false
1091 if (! PMA_DBI_num_rows($result)) {
1092 return false;
1095 switch ($type) {
1096 case 'NUM' :
1097 $fetch_function = 'PMA_DBI_fetch_row';
1098 break;
1099 case 'ASSOC' :
1100 $fetch_function = 'PMA_DBI_fetch_assoc';
1101 break;
1102 case 'BOTH' :
1103 default :
1104 $fetch_function = 'PMA_DBI_fetch_array';
1105 break;
1108 $row = $fetch_function($result);
1109 PMA_DBI_free_result($result);
1110 return $row;
1114 * returns all rows in the resultset in one array
1116 * <code>
1117 * $sql = 'SELECT * FROM `user`';
1118 * $users = PMA_DBI_fetch_result($sql);
1119 * // produces
1120 * // $users[] = array('id' => 123, 'name' => 'John Doe')
1122 * $sql = 'SELECT `id`, `name` FROM `user`';
1123 * $users = PMA_DBI_fetch_result($sql, 'id');
1124 * // produces
1125 * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
1127 * $sql = 'SELECT `id`, `name` FROM `user`';
1128 * $users = PMA_DBI_fetch_result($sql, 0);
1129 * // produces
1130 * // $users['123'] = array(0 => 123, 1 => 'John Doe')
1132 * $sql = 'SELECT `id`, `name` FROM `user`';
1133 * $users = PMA_DBI_fetch_result($sql, 'id', 'name');
1134 * // or
1135 * $users = PMA_DBI_fetch_result($sql, 0, 1);
1136 * // produces
1137 * // $users['123'] = 'John Doe'
1139 * $sql = 'SELECT `name` FROM `user`';
1140 * $users = PMA_DBI_fetch_result($sql);
1141 * // produces
1142 * // $users[] = 'John Doe'
1144 * $sql = 'SELECT `group`, `name` FROM `user`'
1145 * $users = PMA_DBI_fetch_result($sql, array('group', null), 'name');
1146 * // produces
1147 * // $users['admin'][] = 'John Doe'
1149 * $sql = 'SELECT `group`, `name` FROM `user`'
1150 * $users = PMA_DBI_fetch_result($sql, array('group', 'name'), 'id');
1151 * // produces
1152 * // $users['admin']['John Doe'] = '123'
1153 * </code>
1155 * @uses is_string()
1156 * @uses is_int()
1157 * @uses PMA_DBI_try_query()
1158 * @uses PMA_DBI_num_rows()
1159 * @uses PMA_DBI_num_fields()
1160 * @uses PMA_DBI_fetch_row()
1161 * @uses PMA_DBI_fetch_assoc()
1162 * @uses PMA_DBI_free_result()
1163 * @param string|mysql_result $result query or mysql result
1164 * @param string|integer $key field-name or offset
1165 * used as key for array
1166 * @param string|integer $value value-name or offset
1167 * used as value for array
1168 * @param resource $link mysql link
1169 * @param mixed $options
1170 * @return array resultrows or values indexed by $key
1172 function PMA_DBI_fetch_result($result, $key = null, $value = null,
1173 $link = null, $options = 0)
1175 $resultrows = array();
1177 if (is_string($result)) {
1178 $result = PMA_DBI_try_query($result, $link, $options);
1181 // return empty array if result is empty or false
1182 if (! $result) {
1183 return $resultrows;
1186 $fetch_function = 'PMA_DBI_fetch_assoc';
1188 // no nested array if only one field is in result
1189 if (null === $key && 1 === PMA_DBI_num_fields($result)) {
1190 $value = 0;
1191 $fetch_function = 'PMA_DBI_fetch_row';
1194 // if $key is an integer use non associative mysql fetch function
1195 if (is_int($key)) {
1196 $fetch_function = 'PMA_DBI_fetch_row';
1199 if (null === $key && null === $value) {
1200 while ($row = $fetch_function($result)) {
1201 $resultrows[] = $row;
1203 } elseif (null === $key) {
1204 while ($row = $fetch_function($result)) {
1205 $resultrows[] = $row[$value];
1207 } elseif (null === $value) {
1208 if (is_array($key)) {
1209 while ($row = $fetch_function($result)) {
1210 $result_target =& $resultrows;
1211 foreach ($key as $key_index) {
1212 if (null === $key_index) {
1213 $result_target =& $result_target[];
1214 continue;
1217 if (! isset($result_target[$row[$key_index]])) {
1218 $result_target[$row[$key_index]] = array();
1220 $result_target =& $result_target[$row[$key_index]];
1222 $result_target = $row;
1224 } else {
1225 while ($row = $fetch_function($result)) {
1226 $resultrows[$row[$key]] = $row;
1229 } else {
1230 if (is_array($key)) {
1231 while ($row = $fetch_function($result)) {
1232 $result_target =& $resultrows;
1233 foreach ($key as $key_index) {
1234 if (null === $key_index) {
1235 $result_target =& $result_target[];
1236 continue;
1239 if (! isset($result_target[$row[$key_index]])) {
1240 $result_target[$row[$key_index]] = array();
1242 $result_target =& $result_target[$row[$key_index]];
1244 $result_target = $row[$value];
1246 } else {
1247 while ($row = $fetch_function($result)) {
1248 $resultrows[$row[$key]] = $row[$value];
1253 PMA_DBI_free_result($result);
1254 return $resultrows;
1258 * return default table engine for given database
1260 * @return string default table engine
1262 function PMA_DBI_get_default_engine()
1264 return PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'storage_engine\';', 0, 1);
1268 * Get supported SQL compatibility modes
1270 * @return array supported SQL compatibility modes
1272 function PMA_DBI_getCompatibilities()
1274 $compats = array('NONE');
1275 $compats[] = 'ANSI';
1276 $compats[] = 'DB2';
1277 $compats[] = 'MAXDB';
1278 $compats[] = 'MYSQL323';
1279 $compats[] = 'MYSQL40';
1280 $compats[] = 'MSSQL';
1281 $compats[] = 'ORACLE';
1282 // removed; in MySQL 5.0.33, this produces exports that
1283 // can't be read by POSTGRESQL (see our bug #1596328)
1284 //$compats[] = 'POSTGRESQL';
1285 $compats[] = 'TRADITIONAL';
1287 return $compats;
1291 * returns warnings for last query
1293 * @uses $GLOBALS['userlink']
1294 * @uses PMA_DBI_fetch_result()
1295 * @param resource mysql link $link mysql link resource
1296 * @return array warnings
1298 function PMA_DBI_get_warnings($link = null)
1300 if (empty($link)) {
1301 if (isset($GLOBALS['userlink'])) {
1302 $link = $GLOBALS['userlink'];
1303 } else {
1304 return array();
1308 return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1312 * returns true (int > 0) if current user is superuser
1313 * otherwise 0
1315 * @uses $_SESSION['is_superuser'] for caching
1316 * @uses $GLOBALS['userlink']
1317 * @uses $GLOBALS['server']
1318 * @uses PMA_DBI_try_query()
1319 * @uses PMA_DBI_QUERY_STORE
1320 * @return integer $is_superuser
1322 function PMA_isSuperuser()
1324 if (PMA_cacheExists('is_superuser', true)) {
1325 return PMA_cacheGet('is_superuser', true);
1328 // with mysql extension, when connection failed we don't have
1329 // a $userlink
1330 if (isset($GLOBALS['userlink'])) {
1331 $r = (bool) PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user', $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
1332 PMA_cacheSet('is_superuser', $r, true);
1333 } else {
1334 PMA_cacheSet('is_superuser', false, true);
1337 return PMA_cacheGet('is_superuser', true);
1341 * returns an array of PROCEDURE or FUNCTION names for a db
1343 * @uses PMA_DBI_free_result()
1344 * @param string $db db name
1345 * @param string $which PROCEDURE | FUNCTION
1346 * @param resource $link mysql link
1348 * @return array the procedure names or function names
1350 function PMA_DBI_get_procedures_or_functions($db, $which, $link = null)
1352 $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1353 $result = array();
1354 foreach ($shows as $one_show) {
1355 if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1356 $result[] = $one_show['Name'];
1359 return($result);
1363 * returns the definition of a specific PROCEDURE, FUNCTION or EVENT
1365 * @uses PMA_DBI_fetch_value()
1366 * @param string $db db name
1367 * @param string $which PROCEDURE | FUNCTION | EVENT
1368 * @param string $name the procedure|function|event name
1369 * @param resource $link mysql link
1371 * @return string the definition
1373 function PMA_DBI_get_definition($db, $which, $name, $link = null)
1375 $returned_field = array(
1376 'PROCEDURE' => 'Create Procedure',
1377 'FUNCTION' => 'Create Function',
1378 'EVENT' => 'Create Event'
1380 $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($name);
1381 return(PMA_DBI_fetch_value($query, 0, $returned_field[$which]));
1385 * returns details about the TRIGGERs of a specific table
1387 * @uses PMA_DBI_fetch_result()
1388 * @param string $db db name
1389 * @param string $table table name
1390 * @param string $delimiter the delimiter to use (may be empty)
1392 * @return array information about triggers (may be empty)
1394 function PMA_DBI_get_triggers($db, $table, $delimiter = '//')
1396 $result = array();
1398 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1399 // Note: in http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
1400 // their example uses WHERE TRIGGER_SCHEMA='dbname' so let's use this
1401 // instead of WHERE EVENT_OBJECT_SCHEMA='dbname'
1402 $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) . "';");
1403 } else {
1404 $triggers = PMA_DBI_fetch_result("SHOW TRIGGERS FROM " . PMA_sqlAddslashes($db,true) . " LIKE '" . PMA_sqlAddslashes($table, true) . "';");
1407 if ($triggers) {
1408 foreach ($triggers as $trigger) {
1409 if ($GLOBALS['cfg']['Server']['DisableIS']) {
1410 $trigger['TRIGGER_NAME'] = $trigger['Trigger'];
1411 $trigger['ACTION_TIMING'] = $trigger['Timing'];
1412 $trigger['EVENT_MANIPULATION'] = $trigger['Event'];
1413 $trigger['EVENT_OBJECT_TABLE'] = $trigger['Table'];
1414 $trigger['ACTION_STATEMENT'] = $trigger['Statement'];
1416 $one_result = array();
1417 $one_result['name'] = $trigger['TRIGGER_NAME'];
1418 $one_result['action_timing'] = $trigger['ACTION_TIMING'];
1419 $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
1421 // do not prepend the schema name; this way, importing the
1422 // definition into another schema will work
1423 $one_result['full_trigger_name'] = PMA_backquote($trigger['TRIGGER_NAME']);
1424 $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
1425 $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";
1427 $result[] = $one_result;
1430 return($result);
1434 * Returns TRUE if $db.$view_name is a view, FALSE if not
1436 * @uses PMA_DBI_fetch_result()
1437 * @param string $db database name
1438 * @param string $view_name view/table name
1440 * @return bool TRUE if $db.$view_name is a view, FALSE if not
1442 function PMA_isView($db, $view_name)
1444 $result = PMA_DBI_fetch_result("SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$db."' and TABLE_NAME = '".$view_name."';");
1446 if ($result) {
1447 return TRUE;
1448 } else {
1449 return FALSE;