Translation update done using Pootle.
[phpmyadmin-themes.git] / libraries / database_interface.lib.php
blob012eb7c1f637df9830d83c2267ca1707cb3d042f
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, $cache_affected_rows = true) {
73 $res = PMA_DBI_try_query($query, $link, $options, $cache_affected_rows)
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
359 // Size = Data_length + Index_length
360 if ($sort_by == 'Data_length') {
361 foreach ($each_tables as $table_name => $table_data) {
362 ${$sort_by}[$table_name] = strtolower($table_data['Data_length'] + $table_data['Index_length']);
364 } else {
365 foreach ($each_tables as $table_name => $table_data) {
366 ${$sort_by}[$table_name] = strtolower($table_data[$sort_by]);
370 if ($sort_order == 'DESC') {
371 array_multisort($$sort_by, SORT_DESC, $each_tables);
372 } else {
373 array_multisort($$sort_by, SORT_ASC, $each_tables);
376 // cleanup the temporary sort array
377 unset($$sort_by);
380 if ($limit_count) {
381 $each_tables = array_slice($each_tables, $limit_offset, $limit_count);
384 foreach ($each_tables as $table_name => $each_table) {
385 if ('comment' === $tbl_is_group
386 && 0 === strpos($each_table['Comment'], $table))
388 // remove table from list
389 unset($each_tables[$table_name]);
390 continue;
393 if (! isset($each_tables[$table_name]['Type'])
394 && isset($each_tables[$table_name]['Engine'])) {
395 // pma BC, same parts of PMA still uses 'Type'
396 $each_tables[$table_name]['Type']
397 =& $each_tables[$table_name]['Engine'];
398 } elseif (! isset($each_tables[$table_name]['Engine'])
399 && isset($each_tables[$table_name]['Type'])) {
400 // old MySQL reports Type, newer MySQL reports Engine
401 $each_tables[$table_name]['Engine']
402 =& $each_tables[$table_name]['Type'];
405 // MySQL forward compatibility
406 // so pma could use this array as if every server is of version >5.0
407 $each_tables[$table_name]['TABLE_SCHEMA'] = $each_database;
408 $each_tables[$table_name]['TABLE_NAME'] =& $each_tables[$table_name]['Name'];
409 $each_tables[$table_name]['ENGINE'] =& $each_tables[$table_name]['Engine'];
410 $each_tables[$table_name]['VERSION'] =& $each_tables[$table_name]['Version'];
411 $each_tables[$table_name]['ROW_FORMAT'] =& $each_tables[$table_name]['Row_format'];
412 $each_tables[$table_name]['TABLE_ROWS'] =& $each_tables[$table_name]['Rows'];
413 $each_tables[$table_name]['AVG_ROW_LENGTH'] =& $each_tables[$table_name]['Avg_row_length'];
414 $each_tables[$table_name]['DATA_LENGTH'] =& $each_tables[$table_name]['Data_length'];
415 $each_tables[$table_name]['MAX_DATA_LENGTH'] =& $each_tables[$table_name]['Max_data_length'];
416 $each_tables[$table_name]['INDEX_LENGTH'] =& $each_tables[$table_name]['Index_length'];
417 $each_tables[$table_name]['DATA_FREE'] =& $each_tables[$table_name]['Data_free'];
418 $each_tables[$table_name]['AUTO_INCREMENT'] =& $each_tables[$table_name]['Auto_increment'];
419 $each_tables[$table_name]['CREATE_TIME'] =& $each_tables[$table_name]['Create_time'];
420 $each_tables[$table_name]['UPDATE_TIME'] =& $each_tables[$table_name]['Update_time'];
421 $each_tables[$table_name]['CHECK_TIME'] =& $each_tables[$table_name]['Check_time'];
422 $each_tables[$table_name]['TABLE_COLLATION'] =& $each_tables[$table_name]['Collation'];
423 $each_tables[$table_name]['CHECKSUM'] =& $each_tables[$table_name]['Checksum'];
424 $each_tables[$table_name]['CREATE_OPTIONS'] =& $each_tables[$table_name]['Create_options'];
425 $each_tables[$table_name]['TABLE_COMMENT'] =& $each_tables[$table_name]['Comment'];
427 if (strtoupper($each_tables[$table_name]['Comment']) === 'VIEW'
428 && $each_tables[$table_name]['Engine'] == NULL) {
429 $each_tables[$table_name]['TABLE_TYPE'] = 'VIEW';
430 } else {
432 * @todo difference between 'TEMPORARY' and 'BASE TABLE' but how to detect?
434 $each_tables[$table_name]['TABLE_TYPE'] = 'BASE TABLE';
438 $tables[$each_database] = $each_tables;
442 // cache table data
443 // so PMA_Table does not require to issue SHOW TABLE STATUS again
444 // Note: I don't see why we would need array_merge_recursive() here,
445 // as it creates double entries for the same table (for example a double
446 // entry for Comment when changing the storage engine in Operations)
447 // Note 2: Instead of array_merge(), simply use the + operator because
448 // array_merge() renumbers numeric keys starting with 0, therefore
449 // we would lose a db name thats consists only of numbers
450 foreach($tables as $one_database => $its_tables) {
451 if (isset(PMA_Table::$cache[$one_database])) {
452 PMA_Table::$cache[$one_database] = PMA_Table::$cache[$one_database] + $tables[$one_database];
453 } else {
454 PMA_Table::$cache[$one_database] = $tables[$one_database];
457 unset($one_database, $its_tables);
459 if (! is_array($database)) {
460 if (isset($tables[$database])) {
461 return $tables[$database];
462 } elseif (isset($tables[strtolower($database)])) {
463 // on windows with lower_case_table_names = 1
464 // MySQL returns
465 // with SHOW DATABASES or information_schema.SCHEMATA: `Test`
466 // but information_schema.TABLES gives `test`
467 // bug #1436171
468 // http://sf.net/support/tracker.php?aid=1436171
469 return $tables[strtolower($database)];
470 } else {
471 return $tables;
473 } else {
474 return $tables;
479 * returns array with databases containing extended infos about them
481 * @todo move into PMA_List_Database?
482 * @param string $databases database
483 * @param boolean $force_stats retrieve stats also for MySQL < 5
484 * @param resource $link mysql link
485 * @param string $sort_by column to order by
486 * @param string $sort_order ASC or DESC
487 * @param integer $limit_offset starting offset for LIMIT
488 * @param bool|int $limit_count row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
489 * @return array $databases
491 function PMA_DBI_get_databases_full($database = null, $force_stats = false,
492 $link = null, $sort_by = 'SCHEMA_NAME', $sort_order = 'ASC',
493 $limit_offset = 0, $limit_count = false)
495 $sort_order = strtoupper($sort_order);
497 if (true === $limit_count) {
498 $limit_count = $GLOBALS['cfg']['MaxDbList'];
501 // initialize to avoid errors when there are no databases
502 $databases = array();
504 $apply_limit_and_order_manual = true;
506 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
508 * if $GLOBALS['cfg']['NaturalOrder'] is enabled, we cannot use LIMIT
509 * cause MySQL does not support natural ordering, we have to do it afterward
511 if ($GLOBALS['cfg']['NaturalOrder']) {
512 $limit = '';
513 } else {
514 if ($limit_count) {
515 $limit = ' LIMIT ' . $limit_count . ' OFFSET ' . $limit_offset;
518 $apply_limit_and_order_manual = false;
521 // get table information from information_schema
522 if ($database) {
523 $sql_where_schema = 'WHERE `SCHEMA_NAME` LIKE \''
524 . addslashes($database) . '\'';
525 } else {
526 $sql_where_schema = '';
529 // for PMA bc:
530 // `SCHEMA_FIELD_NAME` AS `SHOW_TABLE_STATUS_FIELD_NAME`
531 $sql = '
532 SELECT `information_schema`.`SCHEMATA`.*';
533 if ($force_stats) {
534 $sql .= ',
535 COUNT(`information_schema`.`TABLES`.`TABLE_SCHEMA`)
536 AS `SCHEMA_TABLES`,
537 SUM(`information_schema`.`TABLES`.`TABLE_ROWS`)
538 AS `SCHEMA_TABLE_ROWS`,
539 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`)
540 AS `SCHEMA_DATA_LENGTH`,
541 SUM(`information_schema`.`TABLES`.`MAX_DATA_LENGTH`)
542 AS `SCHEMA_MAX_DATA_LENGTH`,
543 SUM(`information_schema`.`TABLES`.`INDEX_LENGTH`)
544 AS `SCHEMA_INDEX_LENGTH`,
545 SUM(`information_schema`.`TABLES`.`DATA_LENGTH`
546 + `information_schema`.`TABLES`.`INDEX_LENGTH`)
547 AS `SCHEMA_LENGTH`,
548 SUM(`information_schema`.`TABLES`.`DATA_FREE`)
549 AS `SCHEMA_DATA_FREE`';
551 $sql .= '
552 FROM `information_schema`.`SCHEMATA`';
553 if ($force_stats) {
554 $sql .= '
555 LEFT JOIN `information_schema`.`TABLES`
556 ON BINARY `information_schema`.`TABLES`.`TABLE_SCHEMA`
557 = BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`';
559 $sql .= '
560 ' . $sql_where_schema . '
561 GROUP BY BINARY `information_schema`.`SCHEMATA`.`SCHEMA_NAME`
562 ORDER BY BINARY ' . PMA_backquote($sort_by) . ' ' . $sort_order
563 . $limit;
564 $databases = PMA_DBI_fetch_result($sql, 'SCHEMA_NAME', null, $link);
566 $mysql_error = PMA_DBI_getError($link);
567 if (! count($databases) && $GLOBALS['errno']) {
568 PMA_mysqlDie($mysql_error, $sql);
571 // display only databases also in official database list
572 // f.e. to apply hide_db and only_db
573 $drops = array_diff(array_keys($databases), (array) $GLOBALS['pma']->databases);
574 if (count($drops)) {
575 foreach ($drops as $drop) {
576 unset($databases[$drop]);
578 unset($drop);
580 unset($sql_where_schema, $sql, $drops);
581 } else {
582 foreach ($GLOBALS['pma']->databases as $database_name) {
583 // MySQL forward compatibility
584 // so pma could use this array as if every server is of version >5.0
585 $databases[$database_name]['SCHEMA_NAME'] = $database_name;
587 if ($force_stats) {
588 require_once './libraries/mysql_charsets.lib.php';
590 $databases[$database_name]['DEFAULT_COLLATION_NAME']
591 = PMA_getDbCollation($database_name);
593 // get additional info about tables
594 $databases[$database_name]['SCHEMA_TABLES'] = 0;
595 $databases[$database_name]['SCHEMA_TABLE_ROWS'] = 0;
596 $databases[$database_name]['SCHEMA_DATA_LENGTH'] = 0;
597 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH'] = 0;
598 $databases[$database_name]['SCHEMA_INDEX_LENGTH'] = 0;
599 $databases[$database_name]['SCHEMA_LENGTH'] = 0;
600 $databases[$database_name]['SCHEMA_DATA_FREE'] = 0;
602 $res = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($database_name) . ';');
603 while ($row = PMA_DBI_fetch_assoc($res)) {
604 $databases[$database_name]['SCHEMA_TABLES']++;
605 $databases[$database_name]['SCHEMA_TABLE_ROWS']
606 += $row['Rows'];
607 $databases[$database_name]['SCHEMA_DATA_LENGTH']
608 += $row['Data_length'];
609 $databases[$database_name]['SCHEMA_MAX_DATA_LENGTH']
610 += $row['Max_data_length'];
611 $databases[$database_name]['SCHEMA_INDEX_LENGTH']
612 += $row['Index_length'];
614 // for InnoDB, this does not contain the number of
615 // overhead bytes but the total free space
616 if ('InnoDB' != $row['Engine']) {
617 $databases[$database_name]['SCHEMA_DATA_FREE']
618 += $row['Data_free'];
620 $databases[$database_name]['SCHEMA_LENGTH']
621 += $row['Data_length'] + $row['Index_length'];
623 PMA_DBI_free_result($res);
624 unset($res);
631 * apply limit and order manually now
632 * (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
634 if ($apply_limit_and_order_manual) {
635 $GLOBALS['callback_sort_order'] = $sort_order;
636 $GLOBALS['callback_sort_by'] = $sort_by;
637 usort($databases, 'PMA_usort_comparison_callback');
638 unset($GLOBALS['callback_sort_order'], $GLOBALS['callback_sort_by']);
641 * now apply limit
643 if ($limit_count) {
644 $databases = array_slice($databases, $limit_offset, $limit_count);
648 return $databases;
652 * returns detailed array with all columns for given table in database,
653 * or all tables/databases
655 * @param string $database name of database
656 * @param string $table name of table to retrieve columns from
657 * @param string $column name of specific column
658 * @param mixed $link mysql link resource
660 function PMA_DBI_get_columns_full($database = null, $table = null,
661 $column = null, $link = null)
663 $columns = array();
665 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
666 $sql_wheres = array();
667 $array_keys = array();
669 // get columns information from information_schema
670 if (null !== $database) {
671 $sql_wheres[] = '`TABLE_SCHEMA` = \'' . addslashes($database) . '\' ';
672 } else {
673 $array_keys[] = 'TABLE_SCHEMA';
675 if (null !== $table) {
676 $sql_wheres[] = '`TABLE_NAME` = \'' . addslashes($table) . '\' ';
677 } else {
678 $array_keys[] = 'TABLE_NAME';
680 if (null !== $column) {
681 $sql_wheres[] = '`COLUMN_NAME` = \'' . addslashes($column) . '\' ';
682 } else {
683 $array_keys[] = 'COLUMN_NAME';
686 // for PMA bc:
687 // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]`
688 $sql = '
689 SELECT *,
690 `COLUMN_NAME` AS `Field`,
691 `COLUMN_TYPE` AS `Type`,
692 `COLLATION_NAME` AS `Collation`,
693 `IS_NULLABLE` AS `Null`,
694 `COLUMN_KEY` AS `Key`,
695 `COLUMN_DEFAULT` AS `Default`,
696 `EXTRA` AS `Extra`,
697 `PRIVILEGES` AS `Privileges`,
698 `COLUMN_COMMENT` AS `Comment`
699 FROM `information_schema`.`COLUMNS`';
700 if (count($sql_wheres)) {
701 $sql .= "\n" . ' WHERE ' . implode(' AND ', $sql_wheres);
704 $columns = PMA_DBI_fetch_result($sql, $array_keys, null, $link);
705 unset($sql_wheres, $sql);
706 } else {
707 if (null === $database) {
708 foreach ($GLOBALS['pma']->databases as $database) {
709 $columns[$database] = PMA_DBI_get_columns_full($database, null,
710 null, $link);
712 return $columns;
713 } elseif (null === $table) {
714 $tables = PMA_DBI_get_tables($database);
715 foreach ($tables as $table) {
716 $columns[$table] = PMA_DBI_get_columns_full(
717 $database, $table, null, $link);
719 return $columns;
722 $sql = 'SHOW FULL COLUMNS FROM '
723 . PMA_backquote($database) . '.' . PMA_backquote($table);
724 if (null !== $column) {
725 $sql .= " LIKE '" . $column . "'";
728 $columns = PMA_DBI_fetch_result($sql, 'Field', null, $link);
730 $ordinal_position = 1;
731 foreach ($columns as $column_name => $each_column) {
733 // MySQL forward compatibility
734 // so pma could use this array as if every server is of version >5.0
735 $columns[$column_name]['COLUMN_NAME'] =& $columns[$column_name]['Field'];
736 $columns[$column_name]['COLUMN_TYPE'] =& $columns[$column_name]['Type'];
737 $columns[$column_name]['COLLATION_NAME'] =& $columns[$column_name]['Collation'];
738 $columns[$column_name]['IS_NULLABLE'] =& $columns[$column_name]['Null'];
739 $columns[$column_name]['COLUMN_KEY'] =& $columns[$column_name]['Key'];
740 $columns[$column_name]['COLUMN_DEFAULT'] =& $columns[$column_name]['Default'];
741 $columns[$column_name]['EXTRA'] =& $columns[$column_name]['Extra'];
742 $columns[$column_name]['PRIVILEGES'] =& $columns[$column_name]['Privileges'];
743 $columns[$column_name]['COLUMN_COMMENT'] =& $columns[$column_name]['Comment'];
745 $columns[$column_name]['TABLE_CATALOG'] = null;
746 $columns[$column_name]['TABLE_SCHEMA'] = $database;
747 $columns[$column_name]['TABLE_NAME'] = $table;
748 $columns[$column_name]['ORDINAL_POSITION'] = $ordinal_position;
749 $columns[$column_name]['DATA_TYPE'] =
750 substr($columns[$column_name]['COLUMN_TYPE'], 0,
751 strpos($columns[$column_name]['COLUMN_TYPE'], '('));
753 * @todo guess CHARACTER_MAXIMUM_LENGTH from COLUMN_TYPE
755 $columns[$column_name]['CHARACTER_MAXIMUM_LENGTH'] = null;
757 * @todo guess CHARACTER_OCTET_LENGTH from CHARACTER_MAXIMUM_LENGTH
759 $columns[$column_name]['CHARACTER_OCTET_LENGTH'] = null;
760 $columns[$column_name]['NUMERIC_PRECISION'] = null;
761 $columns[$column_name]['NUMERIC_SCALE'] = null;
762 $columns[$column_name]['CHARACTER_SET_NAME'] =
763 substr($columns[$column_name]['COLLATION_NAME'], 0,
764 strpos($columns[$column_name]['COLLATION_NAME'], '_'));
766 $ordinal_position++;
769 if (null !== $column) {
770 reset($columns);
771 $columns = current($columns);
775 return $columns;
779 * @todo should only return columns names, for more info use PMA_DBI_get_columns_full()
781 * @deprecated by PMA_DBI_get_columns() or PMA_DBI_get_columns_full()
782 * @param string $database name of database
783 * @param string $table name of table to retrieve columns from
784 * @param mixed $link mysql link resource
785 * @return array column info
787 function PMA_DBI_get_fields($database, $table, $link = null)
789 // here we use a try_query because when coming from
790 // tbl_create + tbl_properties.inc.php, the table does not exist
791 $fields = PMA_DBI_fetch_result(
792 'SHOW FULL COLUMNS
793 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
794 null, null, $link);
795 if (! is_array($fields) || count($fields) < 1) {
796 return false;
798 return $fields;
802 * array PMA_DBI_get_columns(string $database, string $table, bool $full = false, mysql db link $link = null)
804 * @param string $database name of database
805 * @param string $table name of table to retrieve columns from
806 * @param boolean $full whether to return full info or only column names
807 * @param mixed $link mysql link resource
808 * @return array column names
810 function PMA_DBI_get_columns($database, $table, $full = false, $link = null)
812 $fields = PMA_DBI_fetch_result(
813 'SHOW ' . ($full ? 'FULL' : '') . ' COLUMNS
814 FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
815 'Field', ($full ? null : 'Field'), $link);
816 if (! is_array($fields) || count($fields) < 1) {
817 return false;
819 return $fields;
823 * array PMA_DBI_get_column_values (string $database, string $table, string $column , mysql db link $link = null)
825 * @param string $database name of database
826 * @param string $table name of table to retrieve columns from
827 * @param string $column name of the column to retrieve data from
828 * @param mixed $link mysql link resource
829 * @return array $field_values
832 function PMA_DBI_get_column_values($database, $table, $column, $link = null)
834 $query = 'SELECT ';
835 for($i=0; $i< sizeof($column); $i++)
837 $query.= PMA_backquote($column[$i]);
838 if($i < (sizeof($column)-1))
840 $query.= ', ';
843 $query.= ' FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table);
844 $field_values = PMA_DBI_fetch_result($query, null, null, $link);
846 if (! is_array($field_values) || count($field_values) < 1) {
847 return false;
849 return $field_values;
852 * array PMA_DBI_get_table_data (string $database, string $table, mysql db link $link = null)
854 * @param string $database name of database
855 * @param string $table name of table to retrieve columns from
856 * @param mixed $link mysql link resource
857 * @return array $result
860 function PMA_DBI_get_table_data($database, $table, $link = null)
863 $result = PMA_DBI_fetch_result(
864 'SELECT * FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table),
865 null,null, $link);
867 if (! is_array($result) || count($result) < 1) {
868 return false;
870 return $result;
874 * array PMA_DBI_get_table_indexes($database, $table, $link = null)
876 * @param string $database name of database
877 * @param string $table name of the table whose indexes are to be retreived
878 * @param mixed $link mysql link resource
879 * @return array $indexes
882 function PMA_DBI_get_table_indexes($database, $table, $link = null)
885 $indexes = PMA_DBI_fetch_result(
886 'SHOW INDEXES FROM ' .PMA_backquote($database) . '.' . PMA_backquote($table),
887 null, null, $link);
889 if (! is_array($indexes) || count($indexes) < 1) {
890 return false;
892 return $indexes;
896 * returns value of given mysql server variable
898 * @param string $var mysql server variable name
899 * @param int $type PMA_DBI_GETVAR_SESSION|PMA_DBI_GETVAR_GLOBAL
900 * @param mixed $link mysql link resource|object
901 * @return mixed value for mysql server variable
905 function PMA_DBI_get_variable($var, $type = PMA_DBI_GETVAR_SESSION, $link = null)
907 if ($link === null) {
908 if (isset($GLOBALS['userlink'])) {
909 $link = $GLOBALS['userlink'];
910 } else {
911 return false;
915 switch ($type) {
916 case PMA_DBI_GETVAR_SESSION:
917 $modifier = ' SESSION';
918 break;
919 case PMA_DBI_GETVAR_GLOBAL:
920 $modifier = ' GLOBAL';
921 break;
922 default:
923 $modifier = '';
925 return PMA_DBI_fetch_value(
926 'SHOW' . $modifier . ' VARIABLES LIKE \'' . $var . '\';', 0, 1, $link);
930 * Function called just after a connection to the MySQL database server has been established
931 * It sets the connection collation, and determins the version of MySQL which is running.
933 * @uses ./libraries/charset_conversion.lib.php
934 * @uses PMA_DBI_QUERY_STORE
935 * @uses PMA_MYSQL_INT_VERSION to set it
936 * @uses PMA_MYSQL_STR_VERSION to set it
937 * @uses $_SESSION['PMA_MYSQL_INT_VERSION'] for caching
938 * @uses $_SESSION['PMA_MYSQL_STR_VERSION'] for caching
939 * @uses PMA_DBI_GETVAR_SESSION
940 * @uses PMA_DBI_fetch_value()
941 * @uses PMA_DBI_query()
942 * @uses PMA_DBI_get_variable()
943 * @uses $GLOBALS['collation_connection']
944 * @uses $GLOBALS['available_languages']
945 * @uses $GLOBALS['mysql_charset_map']
946 * @uses $GLOBALS['charset']
947 * @uses $GLOBALS['lang']
948 * @uses $GLOBALS['server']
949 * @uses $GLOBALS['cfg']['Lang']
950 * @uses defined()
951 * @uses explode()
952 * @uses sprintf()
953 * @uses intval()
954 * @uses define()
955 * @uses defined()
956 * @uses substr()
957 * @uses count()
958 * @param mixed $link mysql link resource|object
959 * @param boolean $is_controluser
961 function PMA_DBI_postConnect($link, $is_controluser = false)
963 if (! defined('PMA_MYSQL_INT_VERSION')) {
964 if (PMA_cacheExists('PMA_MYSQL_INT_VERSION', true)) {
965 define('PMA_MYSQL_INT_VERSION', PMA_cacheGet('PMA_MYSQL_INT_VERSION', true));
966 define('PMA_MYSQL_MAJOR_VERSION', PMA_cacheGet('PMA_MYSQL_MAJOR_VERSION', true));
967 define('PMA_MYSQL_STR_VERSION', PMA_cacheGet('PMA_MYSQL_STR_VERSION', true));
968 } else {
969 $mysql_version = PMA_DBI_fetch_value(
970 'SELECT VERSION()', 0, 0, $link, PMA_DBI_QUERY_STORE);
971 if ($mysql_version) {
972 $match = explode('.', $mysql_version);
973 define('PMA_MYSQL_MAJOR_VERSION', (int)$match[0]);
974 define('PMA_MYSQL_INT_VERSION',
975 (int) sprintf('%d%02d%02d', $match[0], $match[1],
976 intval($match[2])));
977 define('PMA_MYSQL_STR_VERSION', $mysql_version);
978 unset($mysql_version, $match);
979 } else {
980 define('PMA_MYSQL_INT_VERSION', 50015);
981 define('PMA_MYSQL_MAJOR_VERSION', 5);
982 define('PMA_MYSQL_STR_VERSION', '5.00.15');
984 PMA_cacheSet('PMA_MYSQL_INT_VERSION', PMA_MYSQL_INT_VERSION, true);
985 PMA_cacheSet('PMA_MYSQL_MAJOR_VERSION', PMA_MYSQL_MAJOR_VERSION, true);
986 PMA_cacheSet('PMA_MYSQL_STR_VERSION', PMA_MYSQL_STR_VERSION, true);
990 /* Skip charsets for Drizzle */
991 if (PMA_MYSQL_MAJOR_VERSION < 2009) {
992 if (! empty($GLOBALS['collation_connection'])) {
993 PMA_DBI_query("SET CHARACTER SET 'utf8';", $link, PMA_DBI_QUERY_STORE);
994 $mysql_charset = explode('_', $GLOBALS['collation_connection']);
995 PMA_DBI_query("SET collation_connection = '" . PMA_sqlAddslashes($GLOBALS['collation_connection']) . "';", $link, PMA_DBI_QUERY_STORE);
996 } else {
997 PMA_DBI_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci';", $link, PMA_DBI_QUERY_STORE);
1003 * returns a single value from the given result or query,
1004 * if the query or the result has more than one row or field
1005 * the first field of the first row is returned
1007 * <code>
1008 * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
1009 * $user_name = PMA_DBI_fetch_value($sql);
1010 * // produces
1011 * // $user_name = 'John Doe'
1012 * </code>
1014 * @uses is_string()
1015 * @uses is_int()
1016 * @uses PMA_DBI_try_query()
1017 * @uses PMA_DBI_num_rows()
1018 * @uses PMA_DBI_fetch_row()
1019 * @uses PMA_DBI_fetch_assoc()
1020 * @uses PMA_DBI_free_result()
1021 * @param string|mysql_result $result query or mysql result
1022 * @param integer $row_number row to fetch the value from,
1023 * starting at 0, with 0 beeing default
1024 * @param integer|string $field field to fetch the value from,
1025 * starting at 0, with 0 beeing default
1026 * @param resource $link mysql link
1027 * @param mixed $options
1028 * @return mixed value of first field in first row from result
1029 * or false if not found
1031 function PMA_DBI_fetch_value($result, $row_number = 0, $field = 0, $link = null, $options = 0) {
1032 $value = false;
1034 if (is_string($result)) {
1035 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1038 // return false if result is empty or false
1039 // or requested row is larger than rows in result
1040 if (PMA_DBI_num_rows($result) < ($row_number + 1)) {
1041 return $value;
1044 // if $field is an integer use non associative mysql fetch function
1045 if (is_int($field)) {
1046 $fetch_function = 'PMA_DBI_fetch_row';
1047 } else {
1048 $fetch_function = 'PMA_DBI_fetch_assoc';
1051 // get requested row
1052 for ($i = 0; $i <= $row_number; $i++) {
1053 $row = $fetch_function($result);
1055 PMA_DBI_free_result($result);
1057 // return requested field
1058 if (isset($row[$field])) {
1059 $value = $row[$field];
1061 unset($row);
1063 return $value;
1067 * returns only the first row from the result
1069 * <code>
1070 * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
1071 * $user = PMA_DBI_fetch_single_row($sql);
1072 * // produces
1073 * // $user = array('id' => 123, 'name' => 'John Doe')
1074 * </code>
1076 * @uses is_string()
1077 * @uses PMA_DBI_try_query()
1078 * @uses PMA_DBI_num_rows()
1079 * @uses PMA_DBI_fetch_row()
1080 * @uses PMA_DBI_fetch_assoc()
1081 * @uses PMA_DBI_fetch_array()
1082 * @uses PMA_DBI_free_result()
1083 * @param string|mysql_result $result query or mysql result
1084 * @param string $type NUM|ASSOC|BOTH
1085 * returned array should either numeric
1086 * associativ or booth
1087 * @param resource $link mysql link
1088 * @param mixed $options
1089 * @return array|boolean first row from result
1090 * or false if result is empty
1092 function PMA_DBI_fetch_single_row($result, $type = 'ASSOC', $link = null, $options = 0) {
1093 if (is_string($result)) {
1094 $result = PMA_DBI_try_query($result, $link, $options | PMA_DBI_QUERY_STORE);
1097 // return null if result is empty or false
1098 if (! PMA_DBI_num_rows($result)) {
1099 return false;
1102 switch ($type) {
1103 case 'NUM' :
1104 $fetch_function = 'PMA_DBI_fetch_row';
1105 break;
1106 case 'ASSOC' :
1107 $fetch_function = 'PMA_DBI_fetch_assoc';
1108 break;
1109 case 'BOTH' :
1110 default :
1111 $fetch_function = 'PMA_DBI_fetch_array';
1112 break;
1115 $row = $fetch_function($result);
1116 PMA_DBI_free_result($result);
1117 return $row;
1121 * returns all rows in the resultset in one array
1123 * <code>
1124 * $sql = 'SELECT * FROM `user`';
1125 * $users = PMA_DBI_fetch_result($sql);
1126 * // produces
1127 * // $users[] = array('id' => 123, 'name' => 'John Doe')
1129 * $sql = 'SELECT `id`, `name` FROM `user`';
1130 * $users = PMA_DBI_fetch_result($sql, 'id');
1131 * // produces
1132 * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
1134 * $sql = 'SELECT `id`, `name` FROM `user`';
1135 * $users = PMA_DBI_fetch_result($sql, 0);
1136 * // produces
1137 * // $users['123'] = array(0 => 123, 1 => 'John Doe')
1139 * $sql = 'SELECT `id`, `name` FROM `user`';
1140 * $users = PMA_DBI_fetch_result($sql, 'id', 'name');
1141 * // or
1142 * $users = PMA_DBI_fetch_result($sql, 0, 1);
1143 * // produces
1144 * // $users['123'] = 'John Doe'
1146 * $sql = 'SELECT `name` FROM `user`';
1147 * $users = PMA_DBI_fetch_result($sql);
1148 * // produces
1149 * // $users[] = 'John Doe'
1151 * $sql = 'SELECT `group`, `name` FROM `user`'
1152 * $users = PMA_DBI_fetch_result($sql, array('group', null), 'name');
1153 * // produces
1154 * // $users['admin'][] = 'John Doe'
1156 * $sql = 'SELECT `group`, `name` FROM `user`'
1157 * $users = PMA_DBI_fetch_result($sql, array('group', 'name'), 'id');
1158 * // produces
1159 * // $users['admin']['John Doe'] = '123'
1160 * </code>
1162 * @uses is_string()
1163 * @uses is_int()
1164 * @uses PMA_DBI_try_query()
1165 * @uses PMA_DBI_num_rows()
1166 * @uses PMA_DBI_num_fields()
1167 * @uses PMA_DBI_fetch_row()
1168 * @uses PMA_DBI_fetch_assoc()
1169 * @uses PMA_DBI_free_result()
1170 * @param string|mysql_result $result query or mysql result
1171 * @param string|integer $key field-name or offset
1172 * used as key for array
1173 * @param string|integer $value value-name or offset
1174 * used as value for array
1175 * @param resource $link mysql link
1176 * @param mixed $options
1177 * @return array resultrows or values indexed by $key
1179 function PMA_DBI_fetch_result($result, $key = null, $value = null,
1180 $link = null, $options = 0)
1182 $resultrows = array();
1184 if (is_string($result)) {
1185 $result = PMA_DBI_try_query($result, $link, $options);
1188 // return empty array if result is empty or false
1189 if (! $result) {
1190 return $resultrows;
1193 $fetch_function = 'PMA_DBI_fetch_assoc';
1195 // no nested array if only one field is in result
1196 if (null === $key && 1 === PMA_DBI_num_fields($result)) {
1197 $value = 0;
1198 $fetch_function = 'PMA_DBI_fetch_row';
1201 // if $key is an integer use non associative mysql fetch function
1202 if (is_int($key)) {
1203 $fetch_function = 'PMA_DBI_fetch_row';
1206 if (null === $key && null === $value) {
1207 while ($row = $fetch_function($result)) {
1208 $resultrows[] = $row;
1210 } elseif (null === $key) {
1211 while ($row = $fetch_function($result)) {
1212 $resultrows[] = $row[$value];
1214 } elseif (null === $value) {
1215 if (is_array($key)) {
1216 while ($row = $fetch_function($result)) {
1217 $result_target =& $resultrows;
1218 foreach ($key as $key_index) {
1219 if (null === $key_index) {
1220 $result_target =& $result_target[];
1221 continue;
1224 if (! isset($result_target[$row[$key_index]])) {
1225 $result_target[$row[$key_index]] = array();
1227 $result_target =& $result_target[$row[$key_index]];
1229 $result_target = $row;
1231 } else {
1232 while ($row = $fetch_function($result)) {
1233 $resultrows[$row[$key]] = $row;
1236 } else {
1237 if (is_array($key)) {
1238 while ($row = $fetch_function($result)) {
1239 $result_target =& $resultrows;
1240 foreach ($key as $key_index) {
1241 if (null === $key_index) {
1242 $result_target =& $result_target[];
1243 continue;
1246 if (! isset($result_target[$row[$key_index]])) {
1247 $result_target[$row[$key_index]] = array();
1249 $result_target =& $result_target[$row[$key_index]];
1251 $result_target = $row[$value];
1253 } else {
1254 while ($row = $fetch_function($result)) {
1255 $resultrows[$row[$key]] = $row[$value];
1260 PMA_DBI_free_result($result);
1261 return $resultrows;
1265 * return default table engine for given database
1267 * @return string default table engine
1269 function PMA_DBI_get_default_engine()
1271 return PMA_DBI_fetch_value('SHOW VARIABLES LIKE \'storage_engine\';', 0, 1);
1275 * Get supported SQL compatibility modes
1277 * @return array supported SQL compatibility modes
1279 function PMA_DBI_getCompatibilities()
1281 $compats = array('NONE');
1282 $compats[] = 'ANSI';
1283 $compats[] = 'DB2';
1284 $compats[] = 'MAXDB';
1285 $compats[] = 'MYSQL323';
1286 $compats[] = 'MYSQL40';
1287 $compats[] = 'MSSQL';
1288 $compats[] = 'ORACLE';
1289 // removed; in MySQL 5.0.33, this produces exports that
1290 // can't be read by POSTGRESQL (see our bug #1596328)
1291 //$compats[] = 'POSTGRESQL';
1292 $compats[] = 'TRADITIONAL';
1294 return $compats;
1298 * returns warnings for last query
1300 * @uses $GLOBALS['userlink']
1301 * @uses PMA_DBI_fetch_result()
1302 * @param resource mysql link $link mysql link resource
1303 * @return array warnings
1305 function PMA_DBI_get_warnings($link = null)
1307 if (empty($link)) {
1308 if (isset($GLOBALS['userlink'])) {
1309 $link = $GLOBALS['userlink'];
1310 } else {
1311 return array();
1315 return PMA_DBI_fetch_result('SHOW WARNINGS', null, null, $link);
1319 * returns true (int > 0) if current user is superuser
1320 * otherwise 0
1322 * @uses $_SESSION['is_superuser'] for caching
1323 * @uses $GLOBALS['userlink']
1324 * @uses $GLOBALS['server']
1325 * @uses PMA_DBI_try_query()
1326 * @uses PMA_DBI_QUERY_STORE
1327 * @return integer $is_superuser
1329 function PMA_isSuperuser()
1331 if (PMA_cacheExists('is_superuser', true)) {
1332 return PMA_cacheGet('is_superuser', true);
1335 // with mysql extension, when connection failed we don't have
1336 // a $userlink
1337 if (isset($GLOBALS['userlink'])) {
1338 $r = (bool) PMA_DBI_try_query('SELECT COUNT(*) FROM mysql.user', $GLOBALS['userlink'], PMA_DBI_QUERY_STORE);
1339 PMA_cacheSet('is_superuser', $r, true);
1340 } else {
1341 PMA_cacheSet('is_superuser', false, true);
1344 return PMA_cacheGet('is_superuser', true);
1348 * returns an array of PROCEDURE or FUNCTION names for a db
1350 * @uses PMA_DBI_free_result()
1351 * @param string $db db name
1352 * @param string $which PROCEDURE | FUNCTION
1353 * @param resource $link mysql link
1355 * @return array the procedure names or function names
1357 function PMA_DBI_get_procedures_or_functions($db, $which, $link = null)
1359 $shows = PMA_DBI_fetch_result('SHOW ' . $which . ' STATUS;', null, null, $link);
1360 $result = array();
1361 foreach ($shows as $one_show) {
1362 if ($one_show['Db'] == $db && $one_show['Type'] == $which) {
1363 $result[] = $one_show['Name'];
1366 return($result);
1370 * returns the definition of a specific PROCEDURE, FUNCTION or EVENT
1372 * @uses PMA_DBI_fetch_value()
1373 * @param string $db db name
1374 * @param string $which PROCEDURE | FUNCTION | EVENT
1375 * @param string $name the procedure|function|event name
1376 * @param resource $link mysql link
1378 * @return string the definition
1380 function PMA_DBI_get_definition($db, $which, $name, $link = null)
1382 $returned_field = array(
1383 'PROCEDURE' => 'Create Procedure',
1384 'FUNCTION' => 'Create Function',
1385 'EVENT' => 'Create Event'
1387 $query = 'SHOW CREATE ' . $which . ' ' . PMA_backquote($db) . '.' . PMA_backquote($name);
1388 return(PMA_DBI_fetch_value($query, 0, $returned_field[$which]));
1392 * returns details about the TRIGGERs of a specific table
1394 * @uses PMA_DBI_fetch_result()
1395 * @param string $db db name
1396 * @param string $table table name
1397 * @param string $delimiter the delimiter to use (may be empty)
1399 * @return array information about triggers (may be empty)
1401 function PMA_DBI_get_triggers($db, $table, $delimiter = '//')
1403 $result = array();
1405 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1406 // Note: in http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
1407 // their example uses WHERE TRIGGER_SCHEMA='dbname' so let's use this
1408 // instead of WHERE EVENT_OBJECT_SCHEMA='dbname'
1409 $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) . "';");
1410 } else {
1411 $triggers = PMA_DBI_fetch_result("SHOW TRIGGERS FROM " . PMA_backquote(PMA_sqlAddslashes($db,true)) . " LIKE '" . PMA_sqlAddslashes($table, true) . "';");
1414 if ($triggers) {
1415 foreach ($triggers as $trigger) {
1416 if ($GLOBALS['cfg']['Server']['DisableIS']) {
1417 $trigger['TRIGGER_NAME'] = $trigger['Trigger'];
1418 $trigger['ACTION_TIMING'] = $trigger['Timing'];
1419 $trigger['EVENT_MANIPULATION'] = $trigger['Event'];
1420 $trigger['EVENT_OBJECT_TABLE'] = $trigger['Table'];
1421 $trigger['ACTION_STATEMENT'] = $trigger['Statement'];
1423 $one_result = array();
1424 $one_result['name'] = $trigger['TRIGGER_NAME'];
1425 $one_result['action_timing'] = $trigger['ACTION_TIMING'];
1426 $one_result['event_manipulation'] = $trigger['EVENT_MANIPULATION'];
1428 // do not prepend the schema name; this way, importing the
1429 // definition into another schema will work
1430 $one_result['full_trigger_name'] = PMA_backquote($trigger['TRIGGER_NAME']);
1431 $one_result['drop'] = 'DROP TRIGGER IF EXISTS ' . $one_result['full_trigger_name'];
1432 $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";
1434 $result[] = $one_result;
1437 return($result);
1441 * Returns TRUE if $db.$view_name is a view, FALSE if not
1443 * @uses PMA_DBI_fetch_result()
1444 * @param string $db database name
1445 * @param string $view_name view/table name
1447 * @return bool TRUE if $db.$view_name is a view, FALSE if not
1449 function PMA_isView($db, $view_name)
1451 $result = PMA_DBI_fetch_result("SELECT TABLE_NAME FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$db."' and TABLE_NAME = '".$view_name."';");
1453 if ($result) {
1454 return TRUE;
1455 } else {
1456 return FALSE;