Bug #3153409 [core] 0 row(s) affected
[phpmyadmin-themes.git] / libraries / database_interface.lib.php
blob6c321b1fe96fb6ef92b0d282005bd92eee150e8a
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, $cache_affected_rows = true) {
83 $res = PMA_DBI_try_query($query, $link, $options, $cache_affected_rows)
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 /* No sorting when key is not present */
209 if (!isset($a[$GLOBALS['callback_sort_by']]) || ! isset($b[$GLOBALS['callback_sort_by']])) {
210 return 0;
212 // produces f.e.:
213 // return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
214 return ($GLOBALS['callback_sort_order'] == 'ASC' ? 1 : -1) * $sorter($a[$GLOBALS['callback_sort_by']], $b[$GLOBALS['callback_sort_by']]);
215 } // end of the 'PMA_usort_comparison_callback()' function
218 * returns array of all tables in given db or dbs
219 * this function expects unquoted names:
220 * RIGHT: my_database
221 * WRONG: `my_database`
222 * WRONG: my\_database
223 * if $tbl_is_group is true, $table is used as filter for table names
224 * if $tbl_is_group is 'comment, $table is used as filter for table comments
226 * <code>
227 * PMA_DBI_get_tables_full('my_database');
228 * PMA_DBI_get_tables_full('my_database', 'my_table'));
229 * PMA_DBI_get_tables_full('my_database', 'my_tables_', true));
230 * PMA_DBI_get_tables_full('my_database', 'my_tables_', 'comment'));
231 * </code>
233 * @todo move into PMA_Table
234 * @uses PMA_DBI_fetch_result()
235 * @uses PMA_escape_mysql_wildcards()
236 * @uses PMA_backquote()
237 * @uses is_array()
238 * @uses addslashes()
239 * @uses strpos()
240 * @uses strtoupper()
241 * @param string $databases database
242 * @param string $table table
243 * @param boolean|string $tbl_is_group $table is a table group
244 * @param resource $link mysql link
245 * @param integer $limit_offset zero-based offset for the count
246 * @param boolean|integer $limit_count number of tables to return
247 * @param string $sort_by table attribute to sort by
248 * @param string $sort_order direction to sort (ASC or DESC)
249 * @return array list of tables in given db(s)
251 function PMA_DBI_get_tables_full($database, $table = false, $tbl_is_group = false, $link = null,
252 $limit_offset = 0, $limit_count = false, $sort_by = 'Name', $sort_order = 'ASC')
254 require_once './libraries/Table.class.php';
256 if (true === $limit_count) {
257 $limit_count = $GLOBALS['cfg']['MaxTableList'];
259 // prepare and check parameters
260 if (! is_array($database)) {
261 $databases = array($database);
262 } else {
263 $databases = $database;
266 $tables = array();
268 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
269 // get table information from information_schema
270 if ($table) {
271 if (true === $tbl_is_group) {
272 $sql_where_table = 'AND `TABLE_NAME` LIKE \''
273 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
274 } elseif ('comment' === $tbl_is_group) {
275 $sql_where_table = 'AND `TABLE_COMMENT` LIKE \''
276 . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
277 } else {
278 $sql_where_table = 'AND `TABLE_NAME` = \'' . addslashes($table) . '\'';
280 } else {
281 $sql_where_table = '';
284 // for PMA bc:
285 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
287 // on non-Windows servers,
288 // added BINARY in the WHERE clause to force a case sensitive
289 // comparison (if we are looking for the db Aa we don't want
290 // to find the db aa)
291 $this_databases = array_map('PMA_sqlAddslashes', $databases);
293 $sql = '
294 SELECT *,
295 `TABLE_SCHEMA` AS `Db`,
296 `TABLE_NAME` AS `Name`,
297 `TABLE_TYPE` ÀS `TABLE_TYPE`,
298 `ENGINE` AS `Engine`,
299 `ENGINE` AS `Type`,
300 `VERSION` AS `Version`,
301 `ROW_FORMAT` AS `Row_format`,
302 `TABLE_ROWS` AS `Rows`,
303 `AVG_ROW_LENGTH` AS `Avg_row_length`,
304 `DATA_LENGTH` AS `Data_length`,
305 `MAX_DATA_LENGTH` AS `Max_data_length`,
306 `INDEX_LENGTH` AS `Index_length`,
307 `DATA_FREE` AS `Data_free`,
308 `AUTO_INCREMENT` AS `Auto_increment`,
309 `CREATE_TIME` AS `Create_time`,
310 `UPDATE_TIME` AS `Update_time`,
311 `CHECK_TIME` AS `Check_time`,
312 `TABLE_COLLATION` AS `Collation`,
313 `CHECKSUM` AS `Checksum`,
314 `CREATE_OPTIONS` AS `Create_options`,
315 `TABLE_COMMENT` AS `Comment`
316 FROM `information_schema`.`TABLES`
317 WHERE ' . (PMA_IS_WINDOWS ? '' : 'BINARY') . ' `TABLE_SCHEMA` IN (\'' . implode("', '", $this_databases) . '\')
318 ' . $sql_where_table;
320 // Sort the tables
321 $sql .= " ORDER BY $sort_by $sort_order";
323 if ($limit_count) {
324 $sql .= ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
327 $tables = PMA_DBI_fetch_result($sql, array('TABLE_SCHEMA', 'TABLE_NAME'),
328 null, $link);
329 unset($sql_where_table, $sql);
330 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
331 // here, the array's first key is by schema name
332 foreach($tables as $one_database_name => $one_database_tables) {
333 uksort($one_database_tables, 'strnatcasecmp');
335 if ($sort_order == 'DESC') {
336 $one_database_tables = array_reverse($one_database_tables);
338 $tables[$one_database_name] = $one_database_tables;
341 } // end (get information from table schema)
343 // If permissions are wrong on even one database directory,
344 // information_schema does not return any table info for any database
345 // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002
346 if (empty($tables)) {
347 foreach ($databases as $each_database) {
348 if ($table || (true === $tbl_is_group)) {
349 $sql = 'SHOW TABLE STATUS FROM '
350 . PMA_backquote($each_database)
351 .' LIKE \'' . PMA_escape_mysql_wildcards(addslashes($table)) . '%\'';
352 } else {
353 $sql = 'SHOW TABLE STATUS FROM '
354 . PMA_backquote($each_database);
357 $each_tables = PMA_DBI_fetch_result($sql, 'Name', null, $link);
359 // Sort naturally if the config allows it and we're sorting
360 // the Name column.
361 if ($sort_by == 'Name' && $GLOBALS['cfg']['NaturalOrder']) {
362 uksort($each_tables, 'strnatcasecmp');
364 if ($sort_order == 'DESC') {
365 $each_tables = array_reverse($each_tables);
367 } else {
368 // Prepare to sort by creating array of the selected sort
369 // value to pass to array_multisort
371 // Size = Data_length + Index_length
372 if ($sort_by == 'Data_length') {
373 foreach ($each_tables as $table_name => $table_data) {
374 ${$sort_by}[$table_name] = strtolower($table_data['Data_length'] + $table_data['Index_length']);
376 } else {
377 foreach ($each_tables as $table_name => $table_data) {
378 ${$sort_by}[$table_name] = strtolower($table_data[$sort_by]);
382 if ($sort_order == 'DESC') {
383 array_multisort($$sort_by, SORT_DESC, $each_tables);
384 } else {
385 array_multisort($$sort_by, SORT_ASC, $each_tables);
388 // cleanup the temporary sort array
389 unset($$sort_by);
392 if ($limit_count) {
393 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
396 foreach ($each_tables as $table_name => $each_table) {
397 if ('comment' === $tbl_is_group
398 && 0 === strpos($each_table['Comment'], $table))
400 // remove table from list
401 unset($each_tables[$table_name]);
402 continue;
405 if (! isset($each_tables[$table_name]['Type'])
406 && isset($each_tables[$table_name]['Engine'])) {
407 // pma BC, same parts of PMA still uses 'Type'
408 $each_tables[$table_name]['Type']
409 =& $each_tables[$table_name]['Engine'];
410 } elseif (! isset($each_tables[$table_name]['Engine'])
411 && isset($each_tables[$table_name]['Type'])) {
412 // old MySQL reports Type, newer MySQL reports Engine
413 $each_tables[$table_name]['Engine']
414 =& $each_tables[$table_name]['Type'];
417 // MySQL forward compatibility
418 // so pma could use this array as if every server is of version >5.0
419 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
420 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
421 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
422 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
423 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
424 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
425 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
426 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
427 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
428 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
429 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
430 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
431 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
432 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
433 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
434 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
435 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
436 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
437 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
439 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW'
440 && $each_tables[$table_name]['Engine'] == NULL) {
441 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
442 } else {
444 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
446 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
450 $tables[$each_database] = $each_tables;
454 // cache table data
455 // so PMA_Table does not require to issue SHOW TABLE STATUS again
456 // Note: I don't see why we would need array_merge_recursive() here,
457 // as it creates double entries for the same table (for example a double
458 // entry for Comment when changing the storage engine in Operations)
459 // Note 2: Instead of array_merge(), simply use the + operator because
460 // array_merge() renumbers numeric keys starting with 0, therefore
461 // we would lose a db name thats consists only of numbers
462 foreach($tables as $one_database => $its_tables) {
463 if (isset(PMA_Table::$cache[$one_database])) {
464 PMA_Table::$cache[$one_database] = PMA_Table::$cache[$one_database] + $tables[$one_database];
465 } else {
466 PMA_Table::$cache[$one_database] = $tables[$one_database];
469 unset($one_database, $its_tables);
471 if (! is_array($database)) {
472 if (isset($tables[$database])) {
473 return $tables[$database];
474 } elseif (isset($tables[strtolower($database)])) {
475 // on windows with lower_case_table_names = 1
476 // MySQL returns
477 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
478 // but information_schema.TABLES gives `test`
479 // bug #1436171
480 // http://sf.net/support/tracker.php?aid=1436171
481 return $tables[strtolower($database)];
482 } else {
483 return $tables;
485 } else {
486 return $tables;
491 * returns array with databases containing extended infos about them
493 * @todo move into PMA_List_Database?
494 * @param string $databases database
495 * @param boolean $force_stats retrieve stats also for MySQL < 5
496 * @param resource $link mysql link
497 * @param string $sort_by column to order by
498 * @param string $sort_order ASC or DESC
499 * @param integer $limit_offset starting offset for LIMIT
500 * @param bool|int $limit_count row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
501 * @return array $databases
503 function PMA_DBI_get_databases_full($database = null, $force_stats = false,
504 $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
505 $limit_offset = 0, $limit_count = false)
507 $sort_order = strtoupper($sort_order);
509 if (true === $limit_count) {
510 $limit_count = $GLOBALS['cfg']['MaxDbList'];
513 // initialize to avoid errors when there are no databases
514 $databases = array();
516 $apply_limit_and_order_manual = true;
518 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
520 * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
521 * cause MySQL does not support natural ordering, we have to do it afterward
523 if ($GLOBALS['cfg']['NaturalOrder']) {
524 $limit = '';
525 } else {
526 if ($limit_count) {
527 $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
530 $apply_limit_and_order_manual = false;
533 // get table information from information_schema
534 if ($database) {
535 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
536 . addslashes($database) . '\'';
537 } else {
538 $sql_where_schema = '';
541 // for PMA bc:
542 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
543 $sql = '
544 SELECT `information_schema`.`SCHEMATA`.*';
545 if ($force_stats) {
546 $sql .= ',
547 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
548 AS `SCHEMA_TABLES`,
549 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
550 AS `SCHEMA_TABLE_ROWS`,
551 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
552 AS `SCHEMA_DATA_LENGTH`,
553 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
554 AS `SCHEMA_MAX_DATA_LENGTH`,
555 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
556 AS `SCHEMA_INDEX_LENGTH`,
557 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
558 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
559 AS `SCHEMA_LENGTH`,
560 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
561 AS `SCHEMA_DATA_FREE`';
563 $sql .= '
564 FROM `information_schema`.`SCHEMATA`';
565 if ($force_stats) {
566 $sql .= '
567 LEFT JOIN `information_schema`.`TABLES`
568 ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
569 = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
571 $sql .= '
572 ' . $sql_where_schema . '
573 GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
574 ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
575 . $limit;
576 $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
578 $mysql_error = PMA_DBI_getError($link);
579 if (! count($databases) && $GLOBALS['errno']) {
580 PMA_mysqlDie($mysql_error, $sql);
583 // display only databases also in official database list
584 // f.e. to apply hide_db and only_db
585 $drops = array_diff(array_keys($databases), (array) $GLOBALS['pma']->databases);
586 if (count($drops)) {
587 foreach ($drops as $drop) {
588 unset($databases[$drop]);
590 unset($drop);
592 unset($sql_where_schema, $sql, $drops);
593 } else {
594 foreach ($GLOBALS['pma']->databases as $database_name) {
595 // MySQL forward compatibility
596 // so pma could use this array as if every server is of version >5.0
597 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
599 if ($force_stats) {
600 require_once './libraries/mysql_charsets.lib.php';
602 $databases[$database_name]['DEFAULT_COLLATION_NAME']
603 = PMA_getDbCollation($database_name);
605 // get additional info about tables
606 $databases[$database_name]['SCHEMA_TABLES'] = 0;
607 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
608 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
609 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
610 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
611 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
612 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
614 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($database_name) . ';');
615 while ($row = PMA_DBI_fetch_assoc($res)) {
616 $databases[$database_name]['SCHEMA_TABLES']++;
617 $databases[$database_name]['SCHEMA_TABLE_ROWS']
618 += $row['Rows'];
619 $databases[$database_name]['SCHEMA_DATA_LENGTH']
620 += $row['Data_length'];
621 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
622 += $row['Max_data_length'];
623 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
624 += $row['Index_length'];
626 // for InnoDB, this does not contain the number of
627 // overhead bytes but the total free space
628 if ('InnoDB' != $row['Engine']) {
629 $databases[$database_name]['SCHEMA_DATA_FREE']
630 += $row['Data_free'];
632 $databases[$database_name]['SCHEMA_LENGTH']
633 += $row['Data_length'] + $row['Index_length'];
635 PMA_DBI_free_result($res);
636 unset($res);
643 * apply limit and order manually now
644 * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
646 if ($apply_limit_and_order_manual) {
647 $GLOBALS['callback_sort_order'] = $sort_order;
648 $GLOBALS['callback_sort_by'] = $sort_by;
649 usort($databases, 'PMA_usort_comparison_callback');
650 unset($GLOBALS['callback_sort_order'], $GLOBALS['callback_sort_by']);
653 * now apply limit
655 if ($limit_count) {
656 $databases = array_slice($databases, $limit_offset, $limit_count);
660 return $databases;
664 * returns detailed array with all columns for given table in database,
665 * or all tables/databases
667 * @param string $database name of database
668 * @param string $table name of table to retrieve columns from
669 * @param string $column name of specific column
670 * @param mixed $link mysql link resource
672 function PMA_DBI_get_columns_full($database = null, $table = null,
673 $column = null, $link = null)
675 $columns = array();
677 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
678 $sql_wheres = array();
679 $array_keys = array();
681 // get columns information from information_schema
682 if (null !== $database) {
683 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . addslashes($database) . '\' ';
684 } else {
685 $array_keys[] = 'TABLE_SCHEMA';
687 if (null !== $table) {
688 $sql_wheres[] = '`TABLE_NAME` = \'' . addslashes($table) . '\' ';
689 } else {
690 $array_keys[] = 'TABLE_NAME';
692 if (null !== $column) {
693 $sql_wheres[] = '`COLUMN_NAME` = \'' . addslashes($column) . '\' ';
694 } else {
695 $array_keys[] = 'COLUMN_NAME';
698 // for PMA bc:
699 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
700 $sql = '
701 SELECT *,
702 `COLUMN_NAME` AS `Field`,
703 `COLUMN_TYPE` AS `Type`,
704 `COLLATION_NAME` AS `Collation`,
705 `IS_NULLABLE` AS `Null`,
706 `COLUMN_KEY` AS `Key`,
707 `COLUMN_DEFAULT` AS `Default`,
708 `EXTRA` AS `Extra`,
709 `PRIVILEGES` AS `Privileges`,
710 `COLUMN_COMMENT` AS `Comment`
711 FROM `information_schema`.`COLUMNS`';
712 if (count($sql_wheres)) {
713 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
716 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
717 unset($sql_wheres, $sql);
718 } else {
719 if (null === $database) {
720 foreach ($GLOBALS['pma']->databases as $database) {
721 $columns[$database] = PMA_DBI_get_columns_full($database, null,
722 null, $link);
724 return $columns;
725 } elseif (null === $table) {
726 $tables = PMA_DBI_get_tables($database);
727 foreach ($tables as $table) {
728 $columns[$table] = PMA_DBI_get_columns_full(
729 $database, $table, null, $link);
731 return $columns;
734 $sql = 'SHOW FULL COLUMNS FROM '
735 . PMA_backquote($database) . '.' . PMA_backquote($table);
736 if (null !== $column) {
737 $sql .= " LIKE '" . $column . "'";
740 $columns = PMA_DBI_fetch_result($sql, 'Field', null, $link);
742 $ordinal_position = 1;
743 foreach ($columns as $column_name => $each_column) {
745 // MySQL forward compatibility
746 // so pma could use this array as if every server is of version >5.0
747 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
748 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
749 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
750 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
751 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
752 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
753 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
754 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
755 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
757 $columns[$column_name]['TABLE_CATALOG'] = null;
758 $columns[$column_name]['TABLE_SCHEMA'] = $database;
759 $columns[$column_name]['TABLE_NAME'] = $table;
760 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
761 $columns[$column_name]['DATA_TYPE'] =
762 substr($columns[$column_name]['COLUMN_TYPE'], 0,
763 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
765 * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
767 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
769 * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
771 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
772 $columns[$column_name]['NUMERIC_PRECISION'] = null;
773 $columns[$column_name]['NUMERIC_SCALE'] = null;
774 $columns[$column_name]['CHARACTER_SET_NAME'] =
775 substr($columns[$column_name]['COLLATION_NAME'], 0,
776 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
778 $ordinal_position++;
781 if (null !== $column) {
782 reset($columns);
783 $columns = current($columns);
787 return $columns;
791 * @todo should only return columns names, for more info use PMA_DBI_get_columns_full()
793 * @deprecated by PMA_DBI_get_columns() or PMA_DBI_get_columns_full()
794 * @param string $database name of database
795 * @param string $table name of table to retrieve columns from
796 * @param mixed $link mysql link resource
797 * @return array column info
799 function PMA_DBI_get_fields($database, $table, $link = null)
801 // here we use a try_query because when coming from
802 // tbl_create + tbl_properties.inc.php, the table does not exist
803 $fields = PMA_DBI_fetch_result(
804 'SHOW FULL COLUMNS
805 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
806 null, null, $link);
807 if (! is_array($fields) || count($fields) < 1) {
808 return false;
810 return $fields;
814 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
816 * @param string $database name of database
817 * @param string $table name of table to retrieve columns from
818 * @param boolean $full whether to return full info or only column names
819 * @param mixed $link mysql link resource
820 * @return array column names
822 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
824 $fields = PMA_DBI_fetch_result(
825 'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
826 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
827 'Field', ($full ? null : 'Field'), $link);
828 if (! is_array($fields) || count($fields) < 1) {
829 return false;
831 return $fields;
835 * array PMA_DBI_get_column_values (string $database, string $table, string $column , mysql db link $link = null)
837 * @param string $database name of database
838 * @param string $table name of table to retrieve columns from
839 * @param string $column name of the column to retrieve data from
840 * @param mixed $link mysql link resource
841 * @return array $field_values
844 function PMA_DBI_get_column_values($database, $table, $column, $link = null)
846 $query = 'SELECT ';
847 for($i=0; $i< sizeof($column); $i++)
849 $query.= PMA_backquote($column[$i]);
850 if($i < (sizeof($column)-1))
852 $query.= ', ';
855 $query.= ' FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table);
856 $field_values = PMA_DBI_fetch_result($query, null, null, $link);
858 if (! is_array($field_values) || count($field_values) < 1) {
859 return false;
861 return $field_values;
864 * array PMA_DBI_get_table_data (string $database, string $table, mysql db link $link = null)
866 * @param string $database name of database
867 * @param string $table name of table to retrieve columns from
868 * @param mixed $link mysql link resource
869 * @return array $result
872 function PMA_DBI_get_table_data($database, $table, $link = null)
875 $result = PMA_DBI_fetch_result(
876 'SELECT * FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
877 null,null, $link);
879 if (! is_array($result) || count($result) < 1) {
880 return false;
882 return $result;
886 * array PMA_DBI_get_table_indexes($database, $table, $link = null)
888 * @param string $database name of database
889 * @param string $table name of the table whose indexes are to be retreived
890 * @param mixed $link mysql link resource
891 * @return array $indexes
894 function PMA_DBI_get_table_indexes($database, $table, $link = null)
897 $indexes = PMA_DBI_fetch_result(
898 'SHOW INDEXES FROM ' .PMA_backquote($database) . '.' . PMA_backquote($table),
899 null, null, $link);
901 if (! is_array($indexes) || count($indexes) < 1) {
902 return false;
904 return $indexes;
908 * returns value of given mysql server variable
910 * @param string $var mysql server variable name
911 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
912 * @param mixed $link mysql link resource|object
913 * @return mixed value for mysql server variable
917 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
919 if ($link === null) {
920 if (isset($GLOBALS['userlink'])) {
921 $link = $GLOBALS['userlink'];
922 } else {
923 return false;
927 switch ($type) {
928 case PMA_DBI_GETVAR_SESSION:
929 $modifier = ' SESSION';
930 break;
931 case PMA_DBI_GETVAR_GLOBAL:
932 $modifier = ' GLOBAL';
933 break;
934 default:
935 $modifier = '';
937 return PMA_DBI_fetch_value(
938 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
942 * Function called just after a connection to the MySQL database server has been established
943 * It sets the connection collation, and determins the version of MySQL which is running.
945 * @uses ./libraries/charset_conversion.lib.php
946 * @uses PMA_DBI_QUERY_STORE
947 * @uses PMA_MYSQL_INT_VERSION to set it
948 * @uses PMA_MYSQL_STR_VERSION to set it
949 * @uses $_SESSION['PMA_MYSQL_INT_VERSION'] for caching
950 * @uses $_SESSION['PMA_MYSQL_STR_VERSION'] for caching
951 * @uses PMA_DBI_GETVAR_SESSION
952 * @uses PMA_DBI_fetch_value()
953 * @uses PMA_DBI_query()
954 * @uses PMA_DBI_get_variable()
955 * @uses $GLOBALS['collation_connection']
956 * @uses $GLOBALS['available_languages']
957 * @uses $GLOBALS['mysql_charset_map']
958 * @uses $GLOBALS['charset']
959 * @uses $GLOBALS['lang']
960 * @uses $GLOBALS['server']
961 * @uses $GLOBALS['cfg']['Lang']
962 * @uses defined()
963 * @uses explode()
964 * @uses sprintf()
965 * @uses intval()
966 * @uses define()
967 * @uses defined()
968 * @uses substr()
969 * @uses count()
970 * @param mixed $link mysql link resource|object
971 * @param boolean $is_controluser
973 function PMA_DBI_postConnect($link, $is_controluser = false)
975 if (! defined('PMA_MYSQL_INT_VERSION')) {
976 if (PMA_cacheExists('PMA_MYSQL_INT_VERSION', true)) {
977 define('PMA_MYSQL_INT_VERSION', PMA_cacheGet('PMA_MYSQL_INT_VERSION', true));
978 define('PMA_MYSQL_STR_VERSION', PMA_cacheGet('PMA_MYSQL_STR_VERSION', true));
979 } else {
980 $mysql_version = PMA_DBI_fetch_value(
981 'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE);
982 if ($mysql_version) {
983 $match = explode('.', $mysql_version);
984 define('PMA_MYSQL_INT_VERSION',
985 (int) sprintf('%d%02d%02d', $match[0], $match[1],
986 intval($match[2])));
987 define('PMA_MYSQL_STR_VERSION', $mysql_version);
988 unset($mysql_version, $match);
989 } else {
990 define('PMA_MYSQL_INT_VERSION', 50015);
991 define('PMA_MYSQL_STR_VERSION', '5.00.15');
993 PMA_cacheSet('PMA_MYSQL_INT_VERSION', PMA_MYSQL_INT_VERSION, true);
994 PMA_cacheSet('PMA_MYSQL_STR_VERSION', PMA_MYSQL_STR_VERSION, true);
998 if (! empty($GLOBALS['collation_connection'])) {
999 PMA_DBI_query("SET CHARACTER SET 'utf8';", $link, PMA_DBI_QUERY_STORE);
1000 $mysql_charset = explode('_', $GLOBALS['collation_connection']);
1001 PMA_DBI_query("SET collation_connection = '" . PMA_sqlAddslashes($GLOBALS['collation_connection']) . "';", $link, PMA_DBI_QUERY_STORE);
1002 } else {
1003 PMA_DBI_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci';", $link, PMA_DBI_QUERY_STORE);
1008 * returns a single value from the given result or query,
1009 * if the query or the result has more than one row or field
1010 * the first field of the first row is returned
1012 * <code>
1013 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
1014 * $user_name = PMA_DBI_fetch_value($sql);
1015 * // produces
1016 * // $user_name = 'John Doe'
1017 * </code>
1019 * @uses is_string()
1020 * @uses is_int()
1021 * @uses PMA_DBI_try_query()
1022 * @uses PMA_DBI_num_rows()
1023 * @uses PMA_DBI_fetch_row()
1024 * @uses PMA_DBI_fetch_assoc()
1025 * @uses PMA_DBI_free_result()
1026 * @param string|mysql_result $result query or mysql result
1027 * @param integer $row_number row to fetch the value from,
1028 * starting at 0, with 0 beeing default
1029 * @param integer|string $field field to fetch the value from,
1030 * starting at 0, with 0 beeing default
1031 * @param resource $link mysql link
1032 * @param mixed $options
1033 * @return mixed value of first field in first row from result
1034 * or false if not found
1036 function PMA_DBI_fetch_value($result, $row_number = 0, $field = 0, $link = null, $options = 0) {
1037 $value = false;
1039 if (is_string($result)) {
1040 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1043 // return false if result is empty or false
1044 // or requested row is larger than rows in result
1045 if (PMA_DBI_num_rows($result) < ($row_number + 1)) {
1046 return $value;
1049 // if $field is an integer use non associative mysql fetch function
1050 if (is_int($field)) {
1051 $fetch_function = 'PMA_DBI_fetch_row';
1052 } else {
1053 $fetch_function = 'PMA_DBI_fetch_assoc';
1056 // get requested row
1057 for ($i = 0; $i <= $row_number; $i++) {
1058 $row = $fetch_function($result);
1060 PMA_DBI_free_result($result);
1062 // return requested field
1063 if (isset($row[$field])) {
1064 $value = $row[$field];
1066 unset($row);
1068 return $value;
1072 * returns only the first row from the result
1074 * <code>
1075 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
1076 * $user = PMA_DBI_fetch_single_row($sql);
1077 * // produces
1078 * // $user = array('id' => 123, 'name' => 'John Doe')
1079 * </code>
1081 * @uses is_string()
1082 * @uses PMA_DBI_try_query()
1083 * @uses PMA_DBI_num_rows()
1084 * @uses PMA_DBI_fetch_row()
1085 * @uses PMA_DBI_fetch_assoc()
1086 * @uses PMA_DBI_fetch_array()
1087 * @uses PMA_DBI_free_result()
1088 * @param string|mysql_result $result query or mysql result
1089 * @param string $type NUM|ASSOC|BOTH
1090 * returned array should either numeric
1091 * associativ or booth
1092 * @param resource $link mysql link
1093 * @param mixed $options
1094 * @return array|boolean first row from result
1095 * or false if result is empty
1097 function PMA_DBI_fetch_single_row($result, $type = 'ASSOC', $link = null, $options = 0) {
1098 if (is_string($result)) {
1099 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1102 // return null if result is empty or false
1103 if (! PMA_DBI_num_rows($result)) {
1104 return false;
1107 switch ($type) {
1108 case 'NUM' :
1109 $fetch_function = 'PMA_DBI_fetch_row';
1110 break;
1111 case 'ASSOC' :
1112 $fetch_function = 'PMA_DBI_fetch_assoc';
1113 break;
1114 case 'BOTH' :
1115 default :
1116 $fetch_function = 'PMA_DBI_fetch_array';
1117 break;
1120 $row = $fetch_function($result);
1121 PMA_DBI_free_result($result);
1122 return $row;
1126 * returns all rows in the resultset in one array
1128 * <code>
1129 * $sql = 'SELECT * FROM `user`';
1130 * $users = PMA_DBI_fetch_result($sql);
1131 * // produces
1132 * // $users[] = array('id' => 123, 'name' => 'John Doe')
1134 * $sql = 'SELECT `id`, `name` FROM `user`';
1135 * $users = PMA_DBI_fetch_result($sql, 'id');
1136 * // produces
1137 * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
1139 * $sql = 'SELECT `id`, `name` FROM `user`';
1140 * $users = PMA_DBI_fetch_result($sql, 0);
1141 * // produces
1142 * // $users['123'] = array(0 => 123, 1 => 'John Doe')
1144 * $sql = 'SELECT `id`, `name` FROM `user`';
1145 * $users = PMA_DBI_fetch_result($sql, 'id', 'name');
1146 * // or
1147 * $users = PMA_DBI_fetch_result($sql, 0, 1);
1148 * // produces
1149 * // $users['123'] = 'John Doe'
1151 * $sql = 'SELECT `name` FROM `user`';
1152 * $users = PMA_DBI_fetch_result($sql);
1153 * // produces
1154 * // $users[] = 'John Doe'
1156 * $sql = 'SELECT `group`, `name` FROM `user`'
1157 * $users = PMA_DBI_fetch_result($sql, array('group', null), 'name');
1158 * // produces
1159 * // $users['admin'][] = 'John Doe'
1161 * $sql = 'SELECT `group`, `name` FROM `user`'
1162 * $users = PMA_DBI_fetch_result($sql, array('group', 'name'), 'id');
1163 * // produces
1164 * // $users['admin']['John Doe'] = '123'
1165 * </code>
1167 * @uses is_string()
1168 * @uses is_int()
1169 * @uses PMA_DBI_try_query()
1170 * @uses PMA_DBI_num_rows()
1171 * @uses PMA_DBI_num_fields()
1172 * @uses PMA_DBI_fetch_row()
1173 * @uses PMA_DBI_fetch_assoc()
1174 * @uses PMA_DBI_free_result()
1175 * @param string|mysql_result $result query or mysql result
1176 * @param string|integer $key field-name or offset
1177 * used as key for array
1178 * @param string|integer $value value-name or offset
1179 * used as value for array
1180 * @param resource $link mysql link
1181 * @param mixed $options
1182 * @return array resultrows or values indexed by $key
1184 function PMA_DBI_fetch_result($result, $key = null, $value = null,
1185 $link = null, $options = 0)
1187 $resultrows = array();
1189 if (is_string($result)) {
1190 $result = PMA_DBI_try_query($result, $link, $options);
1193 // return empty array if result is empty or false
1194 if (! $result) {
1195 return $resultrows;
1198 $fetch_function = 'PMA_DBI_fetch_assoc';
1200 // no nested array if only one field is in result
1201 if (null === $key && 1 === PMA_DBI_num_fields($result)) {
1202 $value = 0;
1203 $fetch_function = 'PMA_DBI_fetch_row';
1206 // if $key is an integer use non associative mysql fetch function
1207 if (is_int($key)) {
1208 $fetch_function = 'PMA_DBI_fetch_row';
1211 if (null === $key && null === $value) {
1212 while ($row = $fetch_function($result)) {
1213 $resultrows[] = $row;
1215 } elseif (null === $key) {
1216 while ($row = $fetch_function($result)) {
1217 $resultrows[] = $row[$value];
1219 } elseif (null === $value) {
1220 if (is_array($key)) {
1221 while ($row = $fetch_function($result)) {
1222 $result_target =& $resultrows;
1223 foreach ($key as $key_index) {
1224 if (null === $key_index) {
1225 $result_target =& $result_target[];
1226 continue;
1229 if (! isset($result_target[$row[$key_index]])) {
1230 $result_target[$row[$key_index]] = array();
1232 $result_target =& $result_target[$row[$key_index]];
1234 $result_target = $row;
1236 } else {
1237 while ($row = $fetch_function($result)) {
1238 $resultrows[$row[$key]] = $row;
1241 } else {
1242 if (is_array($key)) {
1243 while ($row = $fetch_function($result)) {
1244 $result_target =& $resultrows;
1245 foreach ($key as $key_index) {
1246 if (null === $key_index) {
1247 $result_target =& $result_target[];
1248 continue;
1251 if (! isset($result_target[$row[$key_index]])) {
1252 $result_target[$row[$key_index]] = array();
1254 $result_target =& $result_target[$row[$key_index]];
1256 $result_target = $row[$value];
1258 } else {
1259 while ($row = $fetch_function($result)) {
1260 $resultrows[$row[$key]] = $row[$value];
1265 PMA_DBI_free_result($result);
1266 return $resultrows;
1270 * return default table engine for given database
1272 * @return string default table engine
1274 function PMA_DBI_get_default_engine()
1276 return PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'storage_engine\';', 0, 1);
1280 * Get supported SQL compatibility modes
1282 * @return array supported SQL compatibility modes
1284 function PMA_DBI_getCompatibilities()
1286 $compats = array('NONE');
1287 $compats[] = 'ANSI';
1288 $compats[] = 'DB2';
1289 $compats[] = 'MAXDB';
1290 $compats[] = 'MYSQL323';
1291 $compats[] = 'MYSQL40';
1292 $compats[] = 'MSSQL';
1293 $compats[] = 'ORACLE';
1294 // removed; in MySQL 5.0.33, this produces exports that
1295 // can't be read by POSTGRESQL (see our bug #1596328)
1296 //$compats[] = 'POSTGRESQL';
1297 $compats[] = 'TRADITIONAL';
1299 return $compats;
1303 * returns warnings for last query
1305 * @uses $GLOBALS['userlink']
1306 * @uses PMA_DBI_fetch_result()
1307 * @param resource mysql link $link mysql link resource
1308 * @return array warnings
1310 function PMA_DBI_get_warnings($link = null)
1312 if (empty($link)) {
1313 if (isset($GLOBALS['userlink'])) {
1314 $link = $GLOBALS['userlink'];
1315 } else {
1316 return array();
1320 return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1324 * returns true (int > 0) if current user is superuser
1325 * otherwise 0
1327 * @uses $_SESSION['is_superuser'] for caching
1328 * @uses $GLOBALS['userlink']
1329 * @uses $GLOBALS['server']
1330 * @uses PMA_DBI_try_query()
1331 * @uses PMA_DBI_QUERY_STORE
1332 * @return integer $is_superuser
1334 function PMA_isSuperuser()
1336 if (PMA_cacheExists('is_superuser', true)) {
1337 return PMA_cacheGet('is_superuser', true);
1340 // with mysql extension, when connection failed we don't have
1341 // a $userlink
1342 if (isset($GLOBALS['userlink'])) {
1343 $r = (bool) PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user', $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
1344 PMA_cacheSet('is_superuser', $r, true);
1345 } else {
1346 PMA_cacheSet('is_superuser', false, true);
1349 return PMA_cacheGet('is_superuser', true);
1353 * returns an array of PROCEDURE or FUNCTION names for a db
1355 * @uses PMA_DBI_free_result()
1356 * @param string $db db name
1357 * @param string $which PROCEDURE | FUNCTION
1358 * @param resource $link mysql link
1360 * @return array the procedure names or function names
1362 function PMA_DBI_get_procedures_or_functions($db, $which, $link = null)
1364 $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1365 $result = array();
1366 foreach ($shows as $one_show) {
1367 if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1368 $result[] = $one_show['Name'];
1371 return($result);
1375 * returns the definition of a specific PROCEDURE, FUNCTION or EVENT
1377 * @uses PMA_DBI_fetch_value()
1378 * @param string $db db name
1379 * @param string $which PROCEDURE | FUNCTION | EVENT
1380 * @param string $name the procedure|function|event name
1381 * @param resource $link mysql link
1383 * @return string the definition
1385 function PMA_DBI_get_definition($db, $which, $name, $link = null)
1387 $returned_field = array(
1388 'PROCEDURE' => 'Create Procedure',
1389 'FUNCTION' => 'Create Function',
1390 'EVENT' => 'Create Event'
1392 $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($name);
1393 return(PMA_DBI_fetch_value($query, 0, $returned_field[$which]));
1397 * returns details about the TRIGGERs of a specific table
1399 * @uses PMA_DBI_fetch_result()
1400 * @param string $db db name
1401 * @param string $table table name
1402 * @param string $delimiter the delimiter to use (may be empty)
1404 * @return array information about triggers (may be empty)
1406 function PMA_DBI_get_triggers($db, $table, $delimiter = '//')
1408 $result = array();
1410 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1411 // Note: in http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
1412 // their example uses WHERE TRIGGER_SCHEMA='dbname' so let's use this
1413 // instead of WHERE EVENT_OBJECT_SCHEMA='dbname'
1414 $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) . "';");
1415 } else {
1416 $triggers = PMA_DBI_fetch_result("SHOW TRIGGERS FROM " . PMA_backquote(PMA_sqlAddslashes($db,true)) . " LIKE '" . PMA_sqlAddslashes($table, true) . "';");
1419 if ($triggers) {
1420 foreach ($triggers as $trigger) {
1421 if ($GLOBALS['cfg']['Server']['DisableIS']) {
1422 $trigger['TRIGGER_NAME'] = $trigger['Trigger'];
1423 $trigger['ACTION_TIMING'] = $trigger['Timing'];
1424 $trigger['EVENT_MANIPULATION'] = $trigger['Event'];
1425 $trigger['EVENT_OBJECT_TABLE'] = $trigger['Table'];
1426 $trigger['ACTION_STATEMENT'] = $trigger['Statement'];
1428 $one_result = array();
1429 $one_result['name'] = $trigger['TRIGGER_NAME'];
1430 $one_result['action_timing'] = $trigger['ACTION_TIMING'];
1431 $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
1433 // do not prepend the schema name; this way, importing the
1434 // definition into another schema will work
1435 $one_result['full_trigger_name'] = PMA_backquote($trigger['TRIGGER_NAME']);
1436 $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
1437 $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";
1439 $result[] = $one_result;
1442 return($result);
1446 * Returns TRUE if $db.$view_name is a view, FALSE if not
1448 * @uses PMA_DBI_fetch_result()
1449 * @param string $db database name
1450 * @param string $view_name view/table name
1452 * @return bool TRUE if $db.$view_name is a view, FALSE if not
1454 function PMA_isView($db, $view_name)
1456 $result = PMA_DBI_fetch_result("SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$db."' and TABLE_NAME = '".$view_name."';");
1458 if ($result) {
1459 return TRUE;
1460 } else {
1461 return FALSE;