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