3.3.8-dev
[phpmyadmin/last10db.git] / libraries / database_interface.lib.php
blob300a9255413d4737b09b257e5dc20f8947251dfb
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Common Option Constants For DBI Functions
6 * @version $Id$
7 * @package phpMyAdmin
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 // PMA_DBI_try_query()
17 define('PMA_DBI_QUERY_STORE', 1); // Force STORE_RESULT method, ignored by classic MySQL.
18 define('PMA_DBI_QUERY_UNBUFFERED', 2); // Do not read whole query
19 // PMA_DBI_get_variable()
20 define('PMA_DBI_GETVAR_SESSION', 1);
21 define('PMA_DBI_GETVAR_GLOBAL', 2);
23 /**
24 * Checks one of the mysql extensions
26 * @param string $extension mysql extension to check
28 function PMA_DBI_checkMysqlExtension($extension = 'mysql') {
29 if (! function_exists($extension . '_connect')) {
30 return false;
33 return true;
36 /**
37 * check for requested extension
39 if (! PMA_DBI_checkMysqlExtension($GLOBALS['cfg']['Server']['extension'])) {
41 // if it fails try alternative extension ...
42 // and display an error ...
44 /**
45 * @todo add different messages for alternative extension
46 * and complete fail (no alternative extension too)
48 $error =
49 sprintf(PMA_sanitize($GLOBALS['strCantLoad']),
50 $GLOBALS['cfg']['Server']['extension'])
51 .' - <a href="./Documentation.html#faqmysql" target="documentation">'
52 .$GLOBALS['strDocu'] . '</a>';
53 trigger_error($error, E_USER_ERROR);
55 if ($GLOBALS['cfg']['Server']['extension'] === 'mysql') {
56 $alternativ_extension = 'mysqli';
57 } else {
58 $alternativ_extension = 'mysql';
61 if (! PMA_DBI_checkMysqlExtension($alternativ_extension)) {
62 // if alternative fails too ...
63 PMA_fatalError(
64 sprintf($GLOBALS['strCantLoad'],
65 $GLOBALS['cfg']['Server']['extension'])
66 . ' - [a@./Documentation.html#faqmysql@documentation]'
67 . $GLOBALS['strDocu'] . '[/a]');
70 $GLOBALS['cfg']['Server']['extension'] = $alternativ_extension;
71 unset($alternativ_extension);
74 /**
75 * Including The DBI Plugin
77 require_once './libraries/dbi/' . $GLOBALS['cfg']['Server']['extension'] . '.dbi.lib.php';
79 /**
80 * Common Functions
82 function PMA_DBI_query($query, $link = null, $options = 0) {
83 $res = PMA_DBI_try_query($query, $link, $options)
84 or PMA_mysqlDie(PMA_DBI_getError($link), $query);
85 return $res;
88 /**
89 * converts charset of a mysql message, usually coming from mysql_error(),
90 * into PMA charset, usally UTF-8
91 * uses language to charset mapping from mysql/share/errmsg.txt
92 * and charset names to ISO charset from information_schema.CHARACTER_SETS
94 * @uses $GLOBALS['cfg']['IconvExtraParams']
95 * @uses $GLOBALS['charset'] as target charset
96 * @uses PMA_DBI_fetch_value() to get server_language
97 * @uses preg_match() to filter server_language
98 * @uses in_array()
99 * @uses function_exists() to check for a convert function
100 * @uses iconv() to convert message
101 * @uses libiconv() to convert message
102 * @uses recode_string() to convert message
103 * @uses mb_convert_encoding() to convert message
104 * @param string $message
105 * @return string $message
107 function PMA_DBI_convert_message($message) {
108 // latin always last!
109 $encodings = array(
110 'japanese' => 'EUC-JP', //'ujis',
111 'japanese-sjis' => 'Shift-JIS', //'sjis',
112 'korean' => 'EUC-KR', //'euckr',
113 'russian' => 'KOI8-R', //'koi8r',
114 'ukrainian' => 'KOI8-U', //'koi8u',
115 'greek' => 'ISO-8859-7', //'greek',
116 'serbian' => 'CP1250', //'cp1250',
117 'estonian' => 'ISO-8859-13', //'latin7',
118 'slovak' => 'ISO-8859-2', //'latin2',
119 'czech' => 'ISO-8859-2', //'latin2',
120 'hungarian' => 'ISO-8859-2', //'latin2',
121 'polish' => 'ISO-8859-2', //'latin2',
122 'romanian' => 'ISO-8859-2', //'latin2',
123 'spanish' => 'CP1252', //'latin1',
124 'swedish' => 'CP1252', //'latin1',
125 'italian' => 'CP1252', //'latin1',
126 'norwegian-ny' => 'CP1252', //'latin1',
127 'norwegian' => 'CP1252', //'latin1',
128 'portuguese' => 'CP1252', //'latin1',
129 'danish' => 'CP1252', //'latin1',
130 'dutch' => 'CP1252', //'latin1',
131 'english' => 'CP1252', //'latin1',
132 'french' => 'CP1252', //'latin1',
133 'german' => 'CP1252', //'latin1',
136 if ($server_language = PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'language\';', 0, 1)) {
137 $found = array();
138 if (preg_match('&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $server_language, $found)) {
139 $server_language = $found[1];
143 if (! empty($server_language) && isset($encodings[$server_language])) {
144 if (function_exists('iconv')) {
145 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
146 require_once './libraries/iconv_wrapper.lib.php';
147 $message = PMA_aix_iconv_wrapper($encodings[$server_language],
148 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
149 } else {
150 $message = iconv($encodings[$server_language],
151 $GLOBALS['charset'] . $GLOBALS['cfg']['IconvExtraParams'], $message);
153 } elseif (function_exists('recode_string')) {
154 $message = recode_string($encodings[$server_language] . '..' . $GLOBALS['charset'],
155 $message);
156 } elseif (function_exists('libiconv')) {
157 $message = libiconv($encodings[$server_language], $GLOBALS['charset'], $message);
158 } elseif (function_exists('mb_convert_encoding')) {
159 // do not try unsupported charsets
160 if (! in_array($server_language, array('ukrainian', 'greek', 'serbian'))) {
161 $message = mb_convert_encoding($message, $GLOBALS['charset'],
162 $encodings[$server_language]);
165 } else {
167 * @todo lang not found, try all, what TODO ?
171 return $message;
175 * returns array with table names for given db
177 * @param string $database name of database
178 * @param mixed $link mysql link resource|object
179 * @return array tables names
181 function PMA_DBI_get_tables($database, $link = null)
183 return PMA_DBI_fetch_result('SHOW TABLES FROM ' . PMA_backquote($database) . ';',
184 null, 0, $link, PMA_DBI_QUERY_STORE);
188 * usort comparison callback
190 * @param string $a first argument to sort
191 * @param string $b second argument to sort
193 * @return integer a value representing whether $a should be before $b in the
194 * sorted array or not
196 * @global string the column the array shall be sorted by
197 * @global string the sorting order ('ASC' or 'DESC')
199 * @access private
201 function PMA_usort_comparison_callback($a, $b)
203 if ($GLOBALS['cfg']['NaturalOrder']) {
204 $sorter = 'strnatcasecmp';
205 } else {
206 $sorter = 'strcasecmp';
208 /* 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
370 foreach ($each_tables as $table_name => $table_data) {
371 ${$sort_by}[$table_name] = strtolower($table_data[$sort_by]);
374 if ($sort_order == 'DESC') {
375 array_multisort($$sort_by, SORT_DESC, $each_tables);
376 } else {
377 array_multisort($$sort_by, SORT_ASC, $each_tables);
380 // cleanup the temporary sort array
381 unset($$sort_by);
384 if ($limit_count) {
385 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
388 foreach ($each_tables as $table_name => $each_table) {
389 if ('comment' === $tbl_is_group
390 && 0 === strpos($each_table['Comment'], $table))
392 // remove table from list
393 unset($each_tables[$table_name]);
394 continue;
397 if (! isset($each_tables[$table_name]['Type'])
398 && isset($each_tables[$table_name]['Engine'])) {
399 // pma BC, same parts of PMA still uses 'Type'
400 $each_tables[$table_name]['Type']
401 =& $each_tables[$table_name]['Engine'];
402 } elseif (! isset($each_tables[$table_name]['Engine'])
403 && isset($each_tables[$table_name]['Type'])) {
404 // old MySQL reports Type, newer MySQL reports Engine
405 $each_tables[$table_name]['Engine']
406 =& $each_tables[$table_name]['Type'];
409 // MySQL forward compatibility
410 // so pma could use this array as if every server is of version >5.0
411 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
412 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
413 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
414 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
415 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
416 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
417 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
418 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
419 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
420 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
421 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
422 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
423 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
424 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
425 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
426 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
427 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
428 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
429 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
431 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW'
432 && $each_tables[$table_name]['Engine'] == NULL) {
433 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
434 } else {
436 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
438 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
442 $tables[$each_database] = $each_tables;
446 // cache table data
447 // so PMA_Table does not require to issue SHOW TABLE STATUS again
448 // Note: I don't see why we would need array_merge_recursive() here,
449 // as it creates double entries for the same table (for example a double
450 // entry for Comment when changing the storage engine in Operations)
451 // Note 2: Instead of array_merge(), simply use the + operator because
452 // array_merge() renumbers numeric keys starting with 0, therefore
453 // we would lose a db name thats consists only of numbers
454 foreach($tables as $one_database => $its_tables) {
455 if (isset(PMA_Table::$cache[$one_database])) {
456 PMA_Table::$cache[$one_database] = PMA_Table::$cache[$one_database] + $tables[$one_database];
457 } else {
458 PMA_Table::$cache[$one_database] = $tables[$one_database];
461 unset($one_database, $its_tables);
463 if (! is_array($database)) {
464 if (isset($tables[$database])) {
465 return $tables[$database];
466 } elseif (isset($tables[strtolower($database)])) {
467 // on windows with lower_case_table_names = 1
468 // MySQL returns
469 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
470 // but information_schema.TABLES gives `test`
471 // bug #1436171
472 // http://sf.net/support/tracker.php?aid=1436171
473 return $tables[strtolower($database)];
474 } else {
475 return $tables;
477 } else {
478 return $tables;
483 * returns array with databases containing extended infos about them
485 * @todo move into PMA_List_Database?
486 * @param string $databases database
487 * @param boolean $force_stats retrieve stats also for MySQL < 5
488 * @param resource $link mysql link
489 * @param string $sort_by column to order by
490 * @param string $sort_order ASC or DESC
491 * @param integer $limit_offset starting offset for LIMIT
492 * @param bool|int $limit_count row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
493 * @return array $databases
495 function PMA_DBI_get_databases_full($database = null, $force_stats = false,
496 $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
497 $limit_offset = 0, $limit_count = false)
499 $sort_order = strtoupper($sort_order);
501 if (true === $limit_count) {
502 $limit_count = $GLOBALS['cfg']['MaxDbList'];
505 // initialize to avoid errors when there are no databases
506 $databases = array();
508 $apply_limit_and_order_manual = true;
510 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
512 * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
513 * cause MySQL does not support natural ordering, we have to do it afterward
515 if ($GLOBALS['cfg']['NaturalOrder']) {
516 $limit = '';
517 } else {
518 if ($limit_count) {
519 $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
522 $apply_limit_and_order_manual = false;
525 // get table information from information_schema
526 if ($database) {
527 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
528 . addslashes($database) . '\'';
529 } else {
530 $sql_where_schema = '';
533 // for PMA bc:
534 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
535 $sql = '
536 SELECT `information_schema`.`SCHEMATA`.*';
537 if ($force_stats) {
538 $sql .= ',
539 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
540 AS `SCHEMA_TABLES`,
541 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
542 AS `SCHEMA_TABLE_ROWS`,
543 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
544 AS `SCHEMA_DATA_LENGTH`,
545 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
546 AS `SCHEMA_MAX_DATA_LENGTH`,
547 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
548 AS `SCHEMA_INDEX_LENGTH`,
549 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
550 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
551 AS `SCHEMA_LENGTH`,
552 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
553 AS `SCHEMA_DATA_FREE`';
555 $sql .= '
556 FROM `information_schema`.`SCHEMATA`';
557 if ($force_stats) {
558 $sql .= '
559 LEFT JOIN `information_schema`.`TABLES`
560 ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
561 = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
563 $sql .= '
564 ' . $sql_where_schema . '
565 GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
566 ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
567 . $limit;
568 $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
570 $mysql_error = PMA_DBI_getError($link);
571 if (! count($databases) && $GLOBALS['errno']) {
572 PMA_mysqlDie($mysql_error, $sql);
575 // display only databases also in official database list
576 // f.e. to apply hide_db and only_db
577 $drops = array_diff(array_keys($databases), (array) $GLOBALS['pma']->databases);
578 if (count($drops)) {
579 foreach ($drops as $drop) {
580 unset($databases[$drop]);
582 unset($drop);
584 unset($sql_where_schema, $sql, $drops);
585 } else {
586 foreach ($GLOBALS['pma']->databases as $database_name) {
587 // MySQL forward compatibility
588 // so pma could use this array as if every server is of version >5.0
589 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
591 if ($force_stats) {
592 require_once './libraries/mysql_charsets.lib.php';
594 $databases[$database_name]['DEFAULT_COLLATION_NAME']
595 = PMA_getDbCollation($database_name);
597 // get additional info about tables
598 $databases[$database_name]['SCHEMA_TABLES'] = 0;
599 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
600 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
601 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
602 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
603 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
604 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
606 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($database_name) . ';');
607 while ($row = PMA_DBI_fetch_assoc($res)) {
608 $databases[$database_name]['SCHEMA_TABLES']++;
609 $databases[$database_name]['SCHEMA_TABLE_ROWS']
610 += $row['Rows'];
611 $databases[$database_name]['SCHEMA_DATA_LENGTH']
612 += $row['Data_length'];
613 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
614 += $row['Max_data_length'];
615 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
616 += $row['Index_length'];
618 // for InnoDB, this does not contain the number of
619 // overhead bytes but the total free space
620 if ('InnoDB' != $row['Engine']) {
621 $databases[$database_name]['SCHEMA_DATA_FREE']
622 += $row['Data_free'];
624 $databases[$database_name]['SCHEMA_LENGTH']
625 += $row['Data_length'] + $row['Index_length'];
627 PMA_DBI_free_result($res);
628 unset($res);
635 * apply limit and order manually now
636 * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
638 if ($apply_limit_and_order_manual) {
639 $GLOBALS['callback_sort_order'] = $sort_order;
640 $GLOBALS['callback_sort_by'] = $sort_by;
641 usort($databases, 'PMA_usort_comparison_callback');
642 unset($GLOBALS['callback_sort_order'], $GLOBALS['callback_sort_by']);
645 * now apply limit
647 if ($limit_count) {
648 $databases = array_slice($databases, $limit_offset, $limit_count);
652 return $databases;
656 * returns detailed array with all columns for given table in database,
657 * or all tables/databases
659 * @param string $database name of database
660 * @param string $table name of table to retrieve columns from
661 * @param string $column name of specific column
662 * @param mixed $link mysql link resource
664 function PMA_DBI_get_columns_full($database = null, $table = null,
665 $column = null, $link = null)
667 $columns = array();
669 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
670 $sql_wheres = array();
671 $array_keys = array();
673 // get columns information from information_schema
674 if (null !== $database) {
675 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . addslashes($database) . '\' ';
676 } else {
677 $array_keys[] = 'TABLE_SCHEMA';
679 if (null !== $table) {
680 $sql_wheres[] = '`TABLE_NAME` = \'' . addslashes($table) . '\' ';
681 } else {
682 $array_keys[] = 'TABLE_NAME';
684 if (null !== $column) {
685 $sql_wheres[] = '`COLUMN_NAME` = \'' . addslashes($column) . '\' ';
686 } else {
687 $array_keys[] = 'COLUMN_NAME';
690 // for PMA bc:
691 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
692 $sql = '
693 SELECT *,
694 `COLUMN_NAME` AS `Field`,
695 `COLUMN_TYPE` AS `Type`,
696 `COLLATION_NAME` AS `Collation`,
697 `IS_NULLABLE` AS `Null`,
698 `COLUMN_KEY` AS `Key`,
699 `COLUMN_DEFAULT` AS `Default`,
700 `EXTRA` AS `Extra`,
701 `PRIVILEGES` AS `Privileges`,
702 `COLUMN_COMMENT` AS `Comment`
703 FROM `information_schema`.`COLUMNS`';
704 if (count($sql_wheres)) {
705 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
708 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
709 unset($sql_wheres, $sql);
710 } else {
711 if (null === $database) {
712 foreach ($GLOBALS['pma']->databases as $database) {
713 $columns[$database] = PMA_DBI_get_columns_full($database, null,
714 null, $link);
716 return $columns;
717 } elseif (null === $table) {
718 $tables = PMA_DBI_get_tables($database);
719 foreach ($tables as $table) {
720 $columns[$table] = PMA_DBI_get_columns_full(
721 $database, $table, null, $link);
723 return $columns;
726 $sql = 'SHOW FULL COLUMNS FROM '
727 . PMA_backquote($database) . '.' . PMA_backquote($table);
728 if (null !== $column) {
729 $sql .= " LIKE '" . $column . "'";
732 $columns = PMA_DBI_fetch_result($sql, 'Field', null, $link);
734 $ordinal_position = 1;
735 foreach ($columns as $column_name => $each_column) {
737 // MySQL forward compatibility
738 // so pma could use this array as if every server is of version >5.0
739 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
740 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
741 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
742 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
743 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
744 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
745 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
746 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
747 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
749 $columns[$column_name]['TABLE_CATALOG'] = null;
750 $columns[$column_name]['TABLE_SCHEMA'] = $database;
751 $columns[$column_name]['TABLE_NAME'] = $table;
752 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
753 $columns[$column_name]['DATA_TYPE'] =
754 substr($columns[$column_name]['COLUMN_TYPE'], 0,
755 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
757 * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
759 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
761 * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
763 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
764 $columns[$column_name]['NUMERIC_PRECISION'] = null;
765 $columns[$column_name]['NUMERIC_SCALE'] = null;
766 $columns[$column_name]['CHARACTER_SET_NAME'] =
767 substr($columns[$column_name]['COLLATION_NAME'], 0,
768 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
770 $ordinal_position++;
773 if (null !== $column) {
774 reset($columns);
775 $columns = current($columns);
779 return $columns;
783 * @todo should only return columns names, for more info use PMA_DBI_get_columns_full()
785 * @deprecated by PMA_DBI_get_columns() or PMA_DBI_get_columns_full()
786 * @param string $database name of database
787 * @param string $table name of table to retrieve columns from
788 * @param mixed $link mysql link resource
789 * @return array column info
791 function PMA_DBI_get_fields($database, $table, $link = null)
793 // here we use a try_query because when coming from
794 // tbl_create + tbl_properties.inc.php, the table does not exist
795 $fields = PMA_DBI_fetch_result(
796 'SHOW FULL COLUMNS
797 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
798 null, null, $link);
799 if (! is_array($fields) || count($fields) < 1) {
800 return false;
802 return $fields;
806 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
808 * @param string $database name of database
809 * @param string $table name of table to retrieve columns from
810 * @param boolean $full whether to return full info or only column names
811 * @param mixed $link mysql link resource
812 * @return array column names
814 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
816 $fields = PMA_DBI_fetch_result(
817 'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
818 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
819 'Field', ($full ? null : 'Field'), $link);
820 if (! is_array($fields) || count($fields) < 1) {
821 return false;
823 return $fields;
827 * array PMA_DBI_get_column_values (string $database, string $table, string $column , mysql db link $link = null)
829 * @param string $database name of database
830 * @param string $table name of table to retrieve columns from
831 * @param string $column name of the column to retrieve data from
832 * @param mixed $link mysql link resource
833 * @return array $field_values
836 function PMA_DBI_get_column_values($database, $table, $column, $link = null)
838 $query = 'SELECT ';
839 for($i=0; $i< sizeof($column); $i++)
841 $query.= PMA_backquote($column[$i]);
842 if($i < (sizeof($column)-1))
844 $query.= ', ';
847 $query.= ' FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table);
848 $field_values = PMA_DBI_fetch_result($query, null, null, $link);
850 if (! is_array($field_values) || count($field_values) < 1) {
851 return false;
853 return $field_values;
856 * array PMA_DBI_get_table_data (string $database, string $table, mysql db link $link = null)
858 * @param string $database name of database
859 * @param string $table name of table to retrieve columns from
860 * @param mixed $link mysql link resource
861 * @return array $result
864 function PMA_DBI_get_table_data($database, $table, $link = null)
867 $result = PMA_DBI_fetch_result(
868 'SELECT * FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
869 null,null, $link);
871 if (! is_array($result) || count($result) < 1) {
872 return false;
874 return $result;
878 * array PMA_DBI_get_table_indexes($database, $table, $link = null)
880 * @param string $database name of database
881 * @param string $table name of the table whose indexes are to be retreived
882 * @param mixed $link mysql link resource
883 * @return array $indexes
886 function PMA_DBI_get_table_indexes($database, $table, $link = null)
889 $indexes = PMA_DBI_fetch_result(
890 'SHOW INDEXES FROM ' .PMA_backquote($database) . '.' . PMA_backquote($table),
891 null, null, $link);
893 if (! is_array($indexes) || count($indexes) < 1) {
894 return false;
896 return $indexes;
900 * returns value of given mysql server variable
902 * @param string $var mysql server variable name
903 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
904 * @param mixed $link mysql link resource|object
905 * @return mixed value for mysql server variable
909 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
911 if ($link === null) {
912 if (isset($GLOBALS['userlink'])) {
913 $link = $GLOBALS['userlink'];
914 } else {
915 return false;
919 switch ($type) {
920 case PMA_DBI_GETVAR_SESSION:
921 $modifier = ' SESSION';
922 break;
923 case PMA_DBI_GETVAR_GLOBAL:
924 $modifier = ' GLOBAL';
925 break;
926 default:
927 $modifier = '';
929 return PMA_DBI_fetch_value(
930 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
934 * Function called just after a connection to the MySQL database server has been established
935 * It sets the connection collation, and determins the version of MySQL which is running.
937 * @uses ./libraries/charset_conversion.lib.php
938 * @uses PMA_DBI_QUERY_STORE
939 * @uses PMA_MYSQL_INT_VERSION to set it
940 * @uses PMA_MYSQL_STR_VERSION to set it
941 * @uses $_SESSION['PMA_MYSQL_INT_VERSION'] for caching
942 * @uses $_SESSION['PMA_MYSQL_STR_VERSION'] for caching
943 * @uses PMA_DBI_GETVAR_SESSION
944 * @uses PMA_DBI_fetch_value()
945 * @uses PMA_DBI_query()
946 * @uses PMA_DBI_get_variable()
947 * @uses $GLOBALS['collation_connection']
948 * @uses $GLOBALS['available_languages']
949 * @uses $GLOBALS['mysql_charset_map']
950 * @uses $GLOBALS['charset']
951 * @uses $GLOBALS['lang']
952 * @uses $GLOBALS['server']
953 * @uses $GLOBALS['cfg']['Lang']
954 * @uses defined()
955 * @uses explode()
956 * @uses sprintf()
957 * @uses intval()
958 * @uses define()
959 * @uses defined()
960 * @uses substr()
961 * @uses count()
962 * @param mixed $link mysql link resource|object
963 * @param boolean $is_controluser
965 function PMA_DBI_postConnect($link, $is_controluser = false)
967 if (! defined('PMA_MYSQL_INT_VERSION')) {
968 if (PMA_cacheExists('PMA_MYSQL_INT_VERSION', true)) {
969 define('PMA_MYSQL_INT_VERSION', PMA_cacheGet('PMA_MYSQL_INT_VERSION', true));
970 define('PMA_MYSQL_STR_VERSION', PMA_cacheGet('PMA_MYSQL_STR_VERSION', true));
971 } else {
972 $mysql_version = PMA_DBI_fetch_value(
973 'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE);
974 if ($mysql_version) {
975 $match = explode('.', $mysql_version);
976 define('PMA_MYSQL_INT_VERSION',
977 (int) sprintf('%d%02d%02d', $match[0], $match[1],
978 intval($match[2])));
979 define('PMA_MYSQL_STR_VERSION', $mysql_version);
980 unset($mysql_version, $match);
981 } else {
982 define('PMA_MYSQL_INT_VERSION', 50015);
983 define('PMA_MYSQL_STR_VERSION', '5.00.15');
985 PMA_cacheSet('PMA_MYSQL_INT_VERSION', PMA_MYSQL_INT_VERSION, true);
986 PMA_cacheSet('PMA_MYSQL_STR_VERSION', PMA_MYSQL_STR_VERSION, true);
990 if (! empty($GLOBALS['collation_connection'])) {
991 PMA_DBI_query("SET CHARACTER SET 'utf8';", $link, PMA_DBI_QUERY_STORE);
992 $mysql_charset = explode('_', $GLOBALS['collation_connection']);
993 PMA_DBI_query("SET collation_connection = '" . PMA_sqlAddslashes($GLOBALS['collation_connection']) . "';", $link, PMA_DBI_QUERY_STORE);
994 } else {
995 PMA_DBI_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci';", $link, PMA_DBI_QUERY_STORE);
1000 * returns a single value from the given result or query,
1001 * if the query or the result has more than one row or field
1002 * the first field of the first row is returned
1004 * <code>
1005 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
1006 * $user_name = PMA_DBI_fetch_value($sql);
1007 * // produces
1008 * // $user_name = 'John Doe'
1009 * </code>
1011 * @uses is_string()
1012 * @uses is_int()
1013 * @uses PMA_DBI_try_query()
1014 * @uses PMA_DBI_num_rows()
1015 * @uses PMA_DBI_fetch_row()
1016 * @uses PMA_DBI_fetch_assoc()
1017 * @uses PMA_DBI_free_result()
1018 * @param string|mysql_result $result query or mysql result
1019 * @param integer $row_number row to fetch the value from,
1020 * starting at 0, with 0 beeing default
1021 * @param integer|string $field field to fetch the value from,
1022 * starting at 0, with 0 beeing default
1023 * @param resource $link mysql link
1024 * @param mixed $options
1025 * @return mixed value of first field in first row from result
1026 * or false if not found
1028 function PMA_DBI_fetch_value($result, $row_number = 0, $field = 0, $link = null, $options = 0) {
1029 $value = false;
1031 if (is_string($result)) {
1032 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1035 // return false if result is empty or false
1036 // or requested row is larger than rows in result
1037 if (PMA_DBI_num_rows($result) < ($row_number + 1)) {
1038 return $value;
1041 // if $field is an integer use non associative mysql fetch function
1042 if (is_int($field)) {
1043 $fetch_function = 'PMA_DBI_fetch_row';
1044 } else {
1045 $fetch_function = 'PMA_DBI_fetch_assoc';
1048 // get requested row
1049 for ($i = 0; $i <= $row_number; $i++) {
1050 $row = $fetch_function($result);
1052 PMA_DBI_free_result($result);
1054 // return requested field
1055 if (isset($row[$field])) {
1056 $value = $row[$field];
1058 unset($row);
1060 return $value;
1064 * returns only the first row from the result
1066 * <code>
1067 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
1068 * $user = PMA_DBI_fetch_single_row($sql);
1069 * // produces
1070 * // $user = array('id' => 123, 'name' => 'John Doe')
1071 * </code>
1073 * @uses is_string()
1074 * @uses PMA_DBI_try_query()
1075 * @uses PMA_DBI_num_rows()
1076 * @uses PMA_DBI_fetch_row()
1077 * @uses PMA_DBI_fetch_assoc()
1078 * @uses PMA_DBI_fetch_array()
1079 * @uses PMA_DBI_free_result()
1080 * @param string|mysql_result $result query or mysql result
1081 * @param string $type NUM|ASSOC|BOTH
1082 * returned array should either numeric
1083 * associativ or booth
1084 * @param resource $link mysql link
1085 * @param mixed $options
1086 * @return array|boolean first row from result
1087 * or false if result is empty
1089 function PMA_DBI_fetch_single_row($result, $type = 'ASSOC', $link = null, $options = 0) {
1090 if (is_string($result)) {
1091 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1094 // return null if result is empty or false
1095 if (! PMA_DBI_num_rows($result)) {
1096 return false;
1099 switch ($type) {
1100 case 'NUM' :
1101 $fetch_function = 'PMA_DBI_fetch_row';
1102 break;
1103 case 'ASSOC' :
1104 $fetch_function = 'PMA_DBI_fetch_assoc';
1105 break;
1106 case 'BOTH' :
1107 default :
1108 $fetch_function = 'PMA_DBI_fetch_array';
1109 break;
1112 $row = $fetch_function($result);
1113 PMA_DBI_free_result($result);
1114 return $row;
1118 * returns all rows in the resultset in one array
1120 * <code>
1121 * $sql = 'SELECT * FROM `user`';
1122 * $users = PMA_DBI_fetch_result($sql);
1123 * // produces
1124 * // $users[] = array('id' => 123, 'name' => 'John Doe')
1126 * $sql = 'SELECT `id`, `name` FROM `user`';
1127 * $users = PMA_DBI_fetch_result($sql, 'id');
1128 * // produces
1129 * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
1131 * $sql = 'SELECT `id`, `name` FROM `user`';
1132 * $users = PMA_DBI_fetch_result($sql, 0);
1133 * // produces
1134 * // $users['123'] = array(0 => 123, 1 => 'John Doe')
1136 * $sql = 'SELECT `id`, `name` FROM `user`';
1137 * $users = PMA_DBI_fetch_result($sql, 'id', 'name');
1138 * // or
1139 * $users = PMA_DBI_fetch_result($sql, 0, 1);
1140 * // produces
1141 * // $users['123'] = 'John Doe'
1143 * $sql = 'SELECT `name` FROM `user`';
1144 * $users = PMA_DBI_fetch_result($sql);
1145 * // produces
1146 * // $users[] = 'John Doe'
1148 * $sql = 'SELECT `group`, `name` FROM `user`'
1149 * $users = PMA_DBI_fetch_result($sql, array('group', null), 'name');
1150 * // produces
1151 * // $users['admin'][] = 'John Doe'
1153 * $sql = 'SELECT `group`, `name` FROM `user`'
1154 * $users = PMA_DBI_fetch_result($sql, array('group', 'name'), 'id');
1155 * // produces
1156 * // $users['admin']['John Doe'] = '123'
1157 * </code>
1159 * @uses is_string()
1160 * @uses is_int()
1161 * @uses PMA_DBI_try_query()
1162 * @uses PMA_DBI_num_rows()
1163 * @uses PMA_DBI_num_fields()
1164 * @uses PMA_DBI_fetch_row()
1165 * @uses PMA_DBI_fetch_assoc()
1166 * @uses PMA_DBI_free_result()
1167 * @param string|mysql_result $result query or mysql result
1168 * @param string|integer $key field-name or offset
1169 * used as key for array
1170 * @param string|integer $value value-name or offset
1171 * used as value for array
1172 * @param resource $link mysql link
1173 * @param mixed $options
1174 * @return array resultrows or values indexed by $key
1176 function PMA_DBI_fetch_result($result, $key = null, $value = null,
1177 $link = null, $options = 0)
1179 $resultrows = array();
1181 if (is_string($result)) {
1182 $result = PMA_DBI_try_query($result, $link, $options);
1185 // return empty array if result is empty or false
1186 if (! $result) {
1187 return $resultrows;
1190 $fetch_function = 'PMA_DBI_fetch_assoc';
1192 // no nested array if only one field is in result
1193 if (null === $key && 1 === PMA_DBI_num_fields($result)) {
1194 $value = 0;
1195 $fetch_function = 'PMA_DBI_fetch_row';
1198 // if $key is an integer use non associative mysql fetch function
1199 if (is_int($key)) {
1200 $fetch_function = 'PMA_DBI_fetch_row';
1203 if (null === $key && null === $value) {
1204 while ($row = $fetch_function($result)) {
1205 $resultrows[] = $row;
1207 } elseif (null === $key) {
1208 while ($row = $fetch_function($result)) {
1209 $resultrows[] = $row[$value];
1211 } elseif (null === $value) {
1212 if (is_array($key)) {
1213 while ($row = $fetch_function($result)) {
1214 $result_target =& $resultrows;
1215 foreach ($key as $key_index) {
1216 if (null === $key_index) {
1217 $result_target =& $result_target[];
1218 continue;
1221 if (! isset($result_target[$row[$key_index]])) {
1222 $result_target[$row[$key_index]] = array();
1224 $result_target =& $result_target[$row[$key_index]];
1226 $result_target = $row;
1228 } else {
1229 while ($row = $fetch_function($result)) {
1230 $resultrows[$row[$key]] = $row;
1233 } else {
1234 if (is_array($key)) {
1235 while ($row = $fetch_function($result)) {
1236 $result_target =& $resultrows;
1237 foreach ($key as $key_index) {
1238 if (null === $key_index) {
1239 $result_target =& $result_target[];
1240 continue;
1243 if (! isset($result_target[$row[$key_index]])) {
1244 $result_target[$row[$key_index]] = array();
1246 $result_target =& $result_target[$row[$key_index]];
1248 $result_target = $row[$value];
1250 } else {
1251 while ($row = $fetch_function($result)) {
1252 $resultrows[$row[$key]] = $row[$value];
1257 PMA_DBI_free_result($result);
1258 return $resultrows;
1262 * return default table engine for given database
1264 * @return string default table engine
1266 function PMA_DBI_get_default_engine()
1268 return PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'storage_engine\';', 0, 1);
1272 * Get supported SQL compatibility modes
1274 * @return array supported SQL compatibility modes
1276 function PMA_DBI_getCompatibilities()
1278 $compats = array('NONE');
1279 $compats[] = 'ANSI';
1280 $compats[] = 'DB2';
1281 $compats[] = 'MAXDB';
1282 $compats[] = 'MYSQL323';
1283 $compats[] = 'MYSQL40';
1284 $compats[] = 'MSSQL';
1285 $compats[] = 'ORACLE';
1286 // removed; in MySQL 5.0.33, this produces exports that
1287 // can't be read by POSTGRESQL (see our bug #1596328)
1288 //$compats[] = 'POSTGRESQL';
1289 $compats[] = 'TRADITIONAL';
1291 return $compats;
1295 * returns warnings for last query
1297 * @uses $GLOBALS['userlink']
1298 * @uses PMA_DBI_fetch_result()
1299 * @param resource mysql link $link mysql link resource
1300 * @return array warnings
1302 function PMA_DBI_get_warnings($link = null)
1304 if (empty($link)) {
1305 if (isset($GLOBALS['userlink'])) {
1306 $link = $GLOBALS['userlink'];
1307 } else {
1308 return array();
1312 return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1316 * returns true (int > 0) if current user is superuser
1317 * otherwise 0
1319 * @uses $_SESSION['is_superuser'] for caching
1320 * @uses $GLOBALS['userlink']
1321 * @uses $GLOBALS['server']
1322 * @uses PMA_DBI_try_query()
1323 * @uses PMA_DBI_QUERY_STORE
1324 * @return integer $is_superuser
1326 function PMA_isSuperuser()
1328 if (PMA_cacheExists('is_superuser', true)) {
1329 return PMA_cacheGet('is_superuser', true);
1332 // with mysql extension, when connection failed we don't have
1333 // a $userlink
1334 if (isset($GLOBALS['userlink'])) {
1335 $r = (bool) PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user', $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
1336 PMA_cacheSet('is_superuser', $r, true);
1337 } else {
1338 PMA_cacheSet('is_superuser', false, true);
1341 return PMA_cacheGet('is_superuser', true);
1345 * returns an array of PROCEDURE or FUNCTION names for a db
1347 * @uses PMA_DBI_free_result()
1348 * @param string $db db name
1349 * @param string $which PROCEDURE | FUNCTION
1350 * @param resource $link mysql link
1352 * @return array the procedure names or function names
1354 function PMA_DBI_get_procedures_or_functions($db, $which, $link = null)
1356 $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1357 $result = array();
1358 foreach ($shows as $one_show) {
1359 if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1360 $result[] = $one_show['Name'];
1363 return($result);
1367 * returns the definition of a specific PROCEDURE, FUNCTION or EVENT
1369 * @uses PMA_DBI_fetch_value()
1370 * @param string $db db name
1371 * @param string $which PROCEDURE | FUNCTION | EVENT
1372 * @param string $name the procedure|function|event name
1373 * @param resource $link mysql link
1375 * @return string the definition
1377 function PMA_DBI_get_definition($db, $which, $name, $link = null)
1379 $returned_field = array(
1380 'PROCEDURE' => 'Create Procedure',
1381 'FUNCTION' => 'Create Function',
1382 'EVENT' => 'Create Event'
1384 $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($name);
1385 return(PMA_DBI_fetch_value($query, 0, $returned_field[$which]));
1389 * returns details about the TRIGGERs of a specific table
1391 * @uses PMA_DBI_fetch_result()
1392 * @param string $db db name
1393 * @param string $table table name
1394 * @param string $delimiter the delimiter to use (may be empty)
1396 * @return array information about triggers (may be empty)
1398 function PMA_DBI_get_triggers($db, $table, $delimiter = '//')
1400 $result = array();
1402 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1403 // Note: in http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
1404 // their example uses WHERE TRIGGER_SCHEMA='dbname' so let's use this
1405 // instead of WHERE EVENT_OBJECT_SCHEMA='dbname'
1406 $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) . "';");
1407 } else {
1408 $triggers = PMA_DBI_fetch_result("SHOW TRIGGERS FROM " . PMA_backquote(PMA_sqlAddslashes($db,true)) . " LIKE '" . PMA_sqlAddslashes($table, true) . "';");
1411 if ($triggers) {
1412 foreach ($triggers as $trigger) {
1413 if ($GLOBALS['cfg']['Server']['DisableIS']) {
1414 $trigger['TRIGGER_NAME'] = $trigger['Trigger'];
1415 $trigger['ACTION_TIMING'] = $trigger['Timing'];
1416 $trigger['EVENT_MANIPULATION'] = $trigger['Event'];
1417 $trigger['EVENT_OBJECT_TABLE'] = $trigger['Table'];
1418 $trigger['ACTION_STATEMENT'] = $trigger['Statement'];
1420 $one_result = array();
1421 $one_result['name'] = $trigger['TRIGGER_NAME'];
1422 $one_result['action_timing'] = $trigger['ACTION_TIMING'];
1423 $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
1425 // do not prepend the schema name; this way, importing the
1426 // definition into another schema will work
1427 $one_result['full_trigger_name'] = PMA_backquote($trigger['TRIGGER_NAME']);
1428 $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
1429 $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";
1431 $result[] = $one_result;
1434 return($result);
1438 * Returns TRUE if $db.$view_name is a view, FALSE if not
1440 * @uses PMA_DBI_fetch_result()
1441 * @param string $db database name
1442 * @param string $view_name view/table name
1444 * @return bool TRUE if $db.$view_name is a view, FALSE if not
1446 function PMA_isView($db, $view_name)
1448 $result = PMA_DBI_fetch_result("SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$db."' and TABLE_NAME = '".$view_name."';");
1450 if ($result) {
1451 return TRUE;
1452 } else {
1453 return FALSE;