Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / Table.class.php
blob49ea68e79d2363c85738d61618d9f3abe6a1acf5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds the PMA_Table class
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Handles everything related to tables
15 * @todo make use of PMA_Message and PMA_Error
16 * @package PhpMyAdmin
18 class PMA_Table
20 /**
21 * UI preferences properties
23 const PROP_SORTED_COLUMN = 'sorted_col';
24 const PROP_COLUMN_ORDER = 'col_order';
25 const PROP_COLUMN_VISIB = 'col_visib';
27 static $cache = array();
29 /**
30 * @var string table name
32 var $name = '';
34 /**
35 * @var string database name
37 var $db_name = '';
39 /**
40 * @var string engine (innodb, myisam, bdb, ...)
42 var $engine = '';
44 /**
45 * @var string type (view, base table, system view)
47 var $type = '';
49 /**
50 * @var array settings
52 var $settings = array();
54 /**
55 * @var array UI preferences
57 var $uiprefs;
59 /**
60 * @var array errors occured
62 var $errors = array();
64 /**
65 * @var array messages
67 var $messages = array();
69 /**
70 * Constructor
72 * @param string $table_name table name
73 * @param string $db_name database name
75 function __construct($table_name, $db_name)
77 $this->setName($table_name);
78 $this->setDbName($db_name);
81 /**
82 * returns table name
84 * @see PMA_Table::getName()
85 * @return string table name
87 function __toString()
89 return $this->getName();
92 /**
93 * return the last error
95 * @return the last error
97 function getLastError()
99 return end($this->errors);
103 * return the last message
105 * @return the last message
107 function getLastMessage()
109 return end($this->messages);
113 * sets table name
115 * @param string $table_name new table name
117 * @return void
119 function setName($table_name)
121 $this->name = $table_name;
125 * returns table name
127 * @param boolean $backquoted whether to quote name with backticks ``
129 * @return string table name
131 function getName($backquoted = false)
133 if ($backquoted) {
134 return PMA_Util::backquote($this->name);
136 return $this->name;
140 * sets database name for this table
142 * @param string $db_name database name
144 * @return void
146 function setDbName($db_name)
148 $this->db_name = $db_name;
152 * returns database name for this table
154 * @param boolean $backquoted whether to quote name with backticks ``
156 * @return string database name for this table
158 function getDbName($backquoted = false)
160 if ($backquoted) {
161 return PMA_Util::backquote($this->db_name);
163 return $this->db_name;
167 * returns full name for table, including database name
169 * @param boolean $backquoted whether to quote name with backticks ``
171 * @return string
173 function getFullName($backquoted = false)
175 return $this->getDbName($backquoted) . '.'
176 . $this->getName($backquoted);
180 * returns whether the table is actually a view
182 * @param string $db database
183 * @param string $table table
185 * @return whether the given is a view
187 static public function isView($db = null, $table = null)
189 if (empty($db) || empty($table)) {
190 return false;
193 // use cached data or load information with SHOW command
194 if (isset(PMA_Table::$cache[$db][$table])
195 || $GLOBALS['cfg']['Server']['DisableIS']
197 $type = PMA_Table::sGetStatusInfo($db, $table, 'TABLE_TYPE');
198 return $type == 'VIEW';
201 // query information_schema
202 $result = PMA_DBI_fetch_result(
203 "SELECT TABLE_NAME
204 FROM information_schema.VIEWS
205 WHERE TABLE_SCHEMA = '" . PMA_Util::sqlAddSlashes($db) . "'
206 AND TABLE_NAME = '" . PMA_Util::sqlAddSlashes($table) . "'"
208 return $result ? true : false;
212 * Returns whether the table is actually an updatable view
214 * @param string $db database
215 * @param string $table table
217 * @return boolean whether the given is an updatable view
219 static public function isUpdatableView($db = null, $table = null)
221 if (empty($db) || empty($table)) {
222 return false;
225 $result = PMA_DBI_fetch_result(
226 "SELECT TABLE_NAME
227 FROM information_schema.VIEWS
228 WHERE TABLE_SCHEMA = '" . PMA_Util::sqlAddSlashes($db) . "'
229 AND TABLE_NAME = '" . PMA_Util::sqlAddSlashes($table) . "'
230 AND IS_UPDATABLE = 'YES'"
232 return $result ? true : false;
236 * Returns the analysis of 'SHOW CREATE TABLE' query for the table.
237 * In case of a view, the values are taken from the information_schema.
239 * @param string $db database
240 * @param string $table table
242 * @return array analysis of 'SHOW CREATE TABLE' query for the table
244 static public function analyzeStructure($db = null, $table = null)
246 if (empty($db) || empty($table)) {
247 return false;
250 $analyzed_sql = array();
251 if (self::isView($db, $table)) {
252 // For a view, 'SHOW CREATE TABLE' returns the definition,
253 // but the structure of the view. So, we try to mock
254 // the result of analyzing 'SHOW CREATE TABLE' query.
255 $analyzed_sql[0] = array();
256 $analyzed_sql[0]['create_table_fields'] = array();
258 $results = PMA_DBI_fetch_result(
259 "SELECT COLUMN_NAME, DATA_TYPE
260 FROM information_schema.COLUMNS
261 WHERE TABLE_SCHEMA = '" . PMA_Util::sqlAddSlashes($db) . "'
262 AND TABLE_NAME = '" . PMA_Util::sqlAddSlashes($table) . "'"
264 foreach ($results as $result) {
265 $analyzed_sql[0]['create_table_fields'][$result['COLUMN_NAME']]
266 = array('type' => strtoupper($result['DATA_TYPE']));
268 } else {
269 $show_create_table = PMA_DBI_fetch_value(
270 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table),
274 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
276 return $analyzed_sql;
280 * sets given $value for given $param
282 * @param string $param name
283 * @param mixed $value value
285 * @return void
287 function set($param, $value)
289 $this->settings[$param] = $value;
293 * returns value for given setting/param
295 * @param string $param name for value to return
297 * @return mixed value for $param
299 function get($param)
301 if (isset($this->settings[$param])) {
302 return $this->settings[$param];
305 return null;
309 * Checks if this is a merge table
311 * If the ENGINE of the table is MERGE or MRG_MYISAM (alias),
312 * this is a merge table.
314 * @param string $db the database name
315 * @param string $table the table name
317 * @return boolean true if it is a merge table
319 static public function isMerge($db = null, $table = null)
321 $engine = null;
322 // if called static, with parameters
323 if (! empty($db) && ! empty($table)) {
324 $engine = PMA_Table::sGetStatusInfo(
325 $db, $table, 'ENGINE', null, true
329 // did we get engine?
330 if (empty($engine)) {
331 return false;
334 // any of known merge engines?
335 return in_array(strtoupper($engine), array('MERGE', 'MRG_MYISAM'));
339 * Returns tooltip for the table
340 * Format : <table_comment> (<number_of_rows>)
342 * @param string $db database name
343 * @param string $table table name
345 * @return string tooltip fot the table
347 static public function sGetToolTip($db, $table)
349 return PMA_Table::sGetStatusInfo($db, $table, 'Comment')
350 . ' (' . PMA_Table::countRecords($db, $table)
351 . ' ' . __('Rows') . ')';
355 * Returns full table status info, or specific if $info provided
356 * this info is collected from information_schema
358 * @param string $db database name
359 * @param string $table table name
360 * @param string $info specific information to be fetched
361 * @param boolean $force_read read new rather than serving from cache
362 * @param boolean $disable_error if true, disables error message
364 * @todo PMA_DBI_get_tables_full needs to be merged somehow into this class
365 * or at least better documented
367 * @return mixed
369 static public function sGetStatusInfo($db, $table, $info = null,
370 $force_read = false, $disable_error = false
372 if (! empty($_SESSION['is_multi_query'])) {
373 $disable_error = true;
376 if (! isset(PMA_Table::$cache[$db][$table]) || $force_read) {
377 PMA_DBI_get_tables_full($db, $table);
380 if (! isset(PMA_Table::$cache[$db][$table])) {
381 // happens when we enter the table creation dialog
382 // or when we really did not get any status info, for example
383 // when $table == 'TABLE_NAMES' after the user tried SHOW TABLES
384 return '';
387 if (null === $info) {
388 return PMA_Table::$cache[$db][$table];
391 // array_key_exists allows for null values
392 if (!array_key_exists($info, PMA_Table::$cache[$db][$table])) {
393 if (! $disable_error) {
394 trigger_error(
395 __('unknown table status: ') . $info,
396 E_USER_WARNING
399 return false;
402 return PMA_Table::$cache[$db][$table][$info];
406 * generates column specification for ALTER or CREATE TABLE syntax
408 * @param string $name name
409 * @param string $type type ('INT', 'VARCHAR', 'BIT', ...)
410 * @param string $index index
411 * @param string $length length ('2', '5,2', '', ...)
412 * @param string $attribute attribute
413 * @param string $collation collation
414 * @param bool|string $null with 'NULL' or 'NOT NULL'
415 * @param string $default_type whether default is CURRENT_TIMESTAMP,
416 * NULL, NONE, USER_DEFINED
417 * @param string $default_value default value for USER_DEFINED
418 * default type
419 * @param string $extra 'AUTO_INCREMENT'
420 * @param string $comment field comment
421 * @param array &$field_primary list of fields for PRIMARY KEY
422 * @param string $move_to new position for column
424 * @todo move into class PMA_Column
425 * @todo on the interface, some js to clear the default value when the
426 * default current_timestamp is checked
428 * @return string field specification
430 static function generateFieldSpec($name, $type, $index, $length = '',
431 $attribute = '', $collation = '', $null = false,
432 $default_type = 'USER_DEFINED', $default_value = '', $extra = '',
433 $comment = '', &$field_primary = null, $move_to = ''
435 $is_timestamp = strpos(strtoupper($type), 'TIMESTAMP') !== false;
437 $query = PMA_Util::backquote($name) . ' ' . $type;
439 if ($length != ''
440 && ! preg_match(
441 '@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|'
442 . 'MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN|UUID)$@i',
443 $type
446 $query .= '(' . $length . ')';
449 if ($attribute != '') {
450 $query .= ' ' . $attribute;
453 $matches = preg_match(
454 '@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR|ENUM|SET)$@i',
455 $type
457 if (! empty($collation) && $collation != 'NULL' && $matches) {
458 $query .= PMA_generateCharsetQueryPart($collation);
461 if ($null !== false) {
462 if ($null == 'NULL') {
463 $query .= ' NULL';
464 } else {
465 $query .= ' NOT NULL';
469 switch ($default_type) {
470 case 'USER_DEFINED' :
471 if ($is_timestamp && $default_value === '0') {
472 // a TIMESTAMP does not accept DEFAULT '0'
473 // but DEFAULT 0 works
474 $query .= ' DEFAULT 0';
475 } elseif ($type == 'BIT') {
476 $query .= ' DEFAULT b\''
477 . preg_replace('/[^01]/', '0', $default_value)
478 . '\'';
479 } elseif ($type == 'BOOLEAN') {
480 if (preg_match('/^1|T|TRUE|YES$/i', $default_value)) {
481 $query .= ' DEFAULT TRUE';
482 } elseif (preg_match('/^0|F|FALSE|NO$/i', $default_value)) {
483 $query .= ' DEFAULT FALSE';
484 } else {
485 // Invalid BOOLEAN value
486 $query .= ' DEFAULT \''
487 . PMA_Util::sqlAddSlashes($default_value) . '\'';
489 } else {
490 $query .= ' DEFAULT \'' . PMA_Util::sqlAddSlashes($default_value) . '\'';
492 break;
493 case 'NULL' :
494 //If user uncheck null checkbox and not change default value null,
495 //default value will be ignored.
496 if ($null !== false && $null != 'NULL') {
497 break;
499 case 'CURRENT_TIMESTAMP' :
500 $query .= ' DEFAULT ' . $default_type;
501 break;
502 case 'NONE' :
503 default :
504 break;
507 if (!empty($extra)) {
508 $query .= ' ' . $extra;
509 // Force an auto_increment field to be part of the primary key
510 // even if user did not tick the PK box;
511 if ($extra == 'AUTO_INCREMENT') {
512 $primary_cnt = count($field_primary);
513 if (1 == $primary_cnt) {
514 for ($j = 0; $j < $primary_cnt; $j++) {
515 if ($field_primary[$j] == $index) {
516 break;
519 if (isset($field_primary[$j]) && $field_primary[$j] == $index) {
520 $query .= ' PRIMARY KEY';
521 unset($field_primary[$j]);
523 } else {
524 // but the PK could contain other columns so do not append
525 // a PRIMARY KEY clause, just add a member to $field_primary
526 $found_in_pk = false;
527 for ($j = 0; $j < $primary_cnt; $j++) {
528 if ($field_primary[$j] == $index) {
529 $found_in_pk = true;
530 break;
532 } // end for
533 if (! $found_in_pk) {
534 $field_primary[] = $index;
537 } // end if (auto_increment)
539 if (!empty($comment)) {
540 $query .= " COMMENT '" . PMA_Util::sqlAddSlashes($comment) . "'";
543 // move column
544 if ($move_to == '-first') { // dash can't appear as part of column name
545 $query .= ' FIRST';
546 } elseif ($move_to != '') {
547 $query .= ' AFTER ' . PMA_Util::backquote($move_to);
549 return $query;
550 } // end function
553 * Counts and returns (or displays) the number of records in a table
555 * @param string $db the current database name
556 * @param string $table the current table name
557 * @param bool $force_exact whether to force an exact count
558 * @param bool $is_view whether the table is a view
560 * @return mixed the number of records if "retain" param is true,
561 * otherwise true
563 static public function countRecords($db, $table, $force_exact = false,
564 $is_view = null
566 if (isset(PMA_Table::$cache[$db][$table]['ExactRows'])) {
567 $row_count = PMA_Table::$cache[$db][$table]['ExactRows'];
568 } else {
569 $row_count = false;
571 if (null === $is_view) {
572 $is_view = PMA_Table::isView($db, $table);
575 if (! $force_exact) {
576 if (! isset(PMA_Table::$cache[$db][$table]['Rows']) && ! $is_view) {
577 $tmp_tables = PMA_DBI_get_tables_full($db, $table);
578 if (isset($tmp_tables[$table])) {
579 PMA_Table::$cache[$db][$table] = $tmp_tables[$table];
582 if (isset(PMA_Table::$cache[$db][$table]['Rows'])) {
583 $row_count = PMA_Table::$cache[$db][$table]['Rows'];
584 } else {
585 $row_count = false;
589 // for a VIEW, $row_count is always false at this point
590 if (false === $row_count
591 || $row_count < $GLOBALS['cfg']['MaxExactCount']
593 // Make an exception for views in I_S and D_D schema in
594 // Drizzle, as these map to in-memory data and should execute
595 // fast enough
596 if (! $is_view || (PMA_DRIZZLE && PMA_is_system_schema($db))) {
597 $row_count = PMA_DBI_fetch_value(
598 'SELECT COUNT(*) FROM ' . PMA_Util::backquote($db) . '.'
599 . PMA_Util::backquote($table)
601 } else {
602 // For complex views, even trying to get a partial record
603 // count could bring down a server, so we offer an
604 // alternative: setting MaxExactCountViews to 0 will bypass
605 // completely the record counting for views
607 if ($GLOBALS['cfg']['MaxExactCountViews'] == 0) {
608 $row_count = 0;
609 } else {
610 // Counting all rows of a VIEW could be too long,
611 // so use a LIMIT clause.
612 // Use try_query because it can fail (when a VIEW is
613 // based on a table that no longer exists)
614 $result = PMA_DBI_try_query(
615 'SELECT 1 FROM ' . PMA_Util::backquote($db) . '.'
616 . PMA_Util::backquote($table) . ' LIMIT '
617 . $GLOBALS['cfg']['MaxExactCountViews'],
618 null,
619 PMA_DBI_QUERY_STORE
621 if (!PMA_DBI_getError()) {
622 $row_count = PMA_DBI_num_rows($result);
623 PMA_DBI_free_result($result);
627 if ($row_count) {
628 PMA_Table::$cache[$db][$table]['ExactRows'] = $row_count;
633 return $row_count;
634 } // end of the 'PMA_Table::countRecords()' function
637 * Generates column specification for ALTER syntax
639 * @param string $oldcol old column name
640 * @param string $newcol new column name
641 * @param string $type type ('INT', 'VARCHAR', 'BIT', ...)
642 * @param string $length length ('2', '5,2', '', ...)
643 * @param string $attribute attribute
644 * @param string $collation collation
645 * @param bool|string $null with 'NULL' or 'NOT NULL'
646 * @param string $default_type whether default is CURRENT_TIMESTAMP,
647 * NULL, NONE, USER_DEFINED
648 * @param string $default_value default value for USER_DEFINED default
649 * type
650 * @param string $extra 'AUTO_INCREMENT'
651 * @param string $comment field comment
652 * @param array &$field_primary list of fields for PRIMARY KEY
653 * @param string $index index
654 * @param string $move_to new position for column
656 * @see PMA_Table::generateFieldSpec()
658 * @return string field specification
660 static public function generateAlter($oldcol, $newcol, $type, $length,
661 $attribute, $collation, $null, $default_type, $default_value,
662 $extra, $comment, &$field_primary, $index, $move_to
664 return PMA_Util::backquote($oldcol) . ' '
665 . PMA_Table::generateFieldSpec(
666 $newcol, $type, $index, $length, $attribute,
667 $collation, $null, $default_type, $default_value, $extra,
668 $comment, $field_primary, $move_to
670 } // end function
673 * Inserts existing entries in a PMA_* table by reading a value from an old
674 * entry
676 * @param string $work The array index, which Relation feature to
677 * check ('relwork', 'commwork', ...)
678 * @param string $pma_table The array index, which PMA-table to update
679 * ('bookmark', 'relation', ...)
680 * @param array $get_fields Which fields will be SELECT'ed from the old entry
681 * @param array $where_fields Which fields will be used for the WHERE query
682 * (array('FIELDNAME' => 'FIELDVALUE'))
683 * @param array $new_fields Which fields will be used as new VALUES.
684 * These are the important keys which differ
685 * from the old entry
686 * (array('FIELDNAME' => 'NEW FIELDVALUE'))
688 * @global relation variable
690 * @return int|true
692 static public function duplicateInfo($work, $pma_table, $get_fields,
693 $where_fields, $new_fields
695 $last_id = -1;
697 if (isset($GLOBALS['cfgRelation']) && $GLOBALS['cfgRelation'][$work]) {
698 $select_parts = array();
699 $row_fields = array();
700 foreach ($get_fields as $get_field) {
701 $select_parts[] = PMA_Util::backquote($get_field);
702 $row_fields[$get_field] = 'cc';
705 $where_parts = array();
706 foreach ($where_fields as $_where => $_value) {
707 $where_parts[] = PMA_Util::backquote($_where) . ' = \''
708 . PMA_Util::sqlAddSlashes($_value) . '\'';
711 $new_parts = array();
712 $new_value_parts = array();
713 foreach ($new_fields as $_where => $_value) {
714 $new_parts[] = PMA_Util::backquote($_where);
715 $new_value_parts[] = PMA_Util::sqlAddSlashes($_value);
718 $table_copy_query = '
719 SELECT ' . implode(', ', $select_parts) . '
720 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
721 . PMA_Util::backquote($GLOBALS['cfgRelation'][$pma_table]) . '
722 WHERE ' . implode(' AND ', $where_parts);
724 // must use PMA_DBI_QUERY_STORE here, since we execute another
725 // query inside the loop
726 $table_copy_rs = PMA_queryAsControlUser(
727 $table_copy_query, true, PMA_DBI_QUERY_STORE
730 while ($table_copy_row = @PMA_DBI_fetch_assoc($table_copy_rs)) {
731 $value_parts = array();
732 foreach ($table_copy_row as $_key => $_val) {
733 if (isset($row_fields[$_key]) && $row_fields[$_key] == 'cc') {
734 $value_parts[] = PMA_Util::sqlAddSlashes($_val);
738 $new_table_query = 'INSERT IGNORE INTO '
739 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
740 . '.' . PMA_Util::backquote($GLOBALS['cfgRelation'][$pma_table]) . '
741 (' . implode(', ', $select_parts) . ',
742 ' . implode(', ', $new_parts) . ')
743 VALUES
744 (\'' . implode('\', \'', $value_parts) . '\',
745 \'' . implode('\', \'', $new_value_parts) . '\')';
747 PMA_queryAsControlUser($new_table_query);
748 $last_id = PMA_DBI_insert_id();
749 } // end while
751 PMA_DBI_free_result($table_copy_rs);
753 return $last_id;
756 return true;
757 } // end of 'PMA_Table::duplicateInfo()' function
760 * Copies or renames table
762 * @param string $source_db source database
763 * @param string $source_table source table
764 * @param string $target_db target database
765 * @param string $target_table target table
766 * @param string $what what to be moved or copied (data, dataonly)
767 * @param bool $move whether to move
768 * @param string $mode mode
770 * @return bool true if success, false otherwise
772 static public function moveCopy($source_db, $source_table, $target_db,
773 $target_table, $what, $move, $mode
775 global $err_url;
777 /* Try moving table directly */
778 if ($move && $what == 'data') {
779 $tbl = new PMA_Table($source_table, $source_db);
780 $result = $tbl->rename($target_table, $target_db);
781 if ($result) {
782 $GLOBALS['message'] = $tbl->getLastMessage();
783 return true;
787 // set export settings we need
788 $GLOBALS['sql_backquotes'] = 1;
789 $GLOBALS['asfile'] = 1;
791 // Ensure the target is valid
792 if (! $GLOBALS['pma']->databases->exists($source_db, $target_db)) {
793 if (! $GLOBALS['pma']->databases->exists($source_db)) {
794 $GLOBALS['message'] = PMA_Message::rawError(
795 sprintf(
796 __('Source database `%s` was not found!'),
797 htmlspecialchars($source_db)
801 if (! $GLOBALS['pma']->databases->exists($target_db)) {
802 $GLOBALS['message'] = PMA_Message::rawError(
803 sprintf(
804 __('Target database `%s` was not found!'),
805 htmlspecialchars($target_db)
809 return false;
812 $source = PMA_Util::backquote($source_db) . '.' . PMA_Util::backquote($source_table);
813 if (! isset($target_db) || ! strlen($target_db)) {
814 $target_db = $source_db;
817 // Doing a select_db could avoid some problems with replicated databases,
818 // when moving table from replicated one to not replicated one
819 PMA_DBI_select_db($target_db);
821 $target = PMA_Util::backquote($target_db) . '.' . PMA_Util::backquote($target_table);
823 // do not create the table if dataonly
824 if ($what != 'dataonly') {
825 include_once "libraries/plugin_interface.lib.php";
826 // get Export SQL instance
827 $export_sql_plugin = PMA_getPlugin(
828 "export",
829 "sql",
830 'libraries/plugins/export/',
831 array(
832 'export_type' => 'table',
833 'single_table' => isset($single_table)
837 $no_constraints_comments = true;
838 $GLOBALS['sql_constraints_query'] = '';
840 $sql_structure = $export_sql_plugin->getTableDef(
841 $source_db, $source_table, "\n", $err_url, false, false
843 unset($no_constraints_comments);
844 $parsed_sql = PMA_SQP_parse($sql_structure);
845 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
846 $i = 0;
847 if (empty($analyzed_sql[0]['create_table_fields'])) {
848 // this is not a CREATE TABLE, so find the first VIEW
849 $target_for_view = PMA_Util::backquote($target_db);
850 while (true) {
851 if ($parsed_sql[$i]['type'] == 'alpha_reservedWord'
852 && $parsed_sql[$i]['data'] == 'VIEW'
854 break;
856 $i++;
859 unset($analyzed_sql);
860 if (PMA_DRIZZLE) {
861 $table_delimiter = 'quote_backtick';
862 } else {
863 $server_sql_mode = PMA_DBI_fetch_value(
864 "SHOW VARIABLES LIKE 'sql_mode'",
868 // ANSI_QUOTES might be a subset of sql_mode, for example
869 // REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ANSI
870 if (false !== strpos($server_sql_mode, 'ANSI_QUOTES')) {
871 $table_delimiter = 'quote_double';
872 } else {
873 $table_delimiter = 'quote_backtick';
875 unset($server_sql_mode);
878 /* Find table name in query and replace it */
879 while ($parsed_sql[$i]['type'] != $table_delimiter) {
880 $i++;
883 /* no need to backquote() */
884 if (isset($target_for_view)) {
885 // this a view definition; we just found the first db name
886 // that follows DEFINER VIEW
887 // so change it for the new db name
888 $parsed_sql[$i]['data'] = $target_for_view;
889 // then we have to find all references to the source db
890 // and change them to the target db, ensuring we stay into
891 // the $parsed_sql limits
892 $last = $parsed_sql['len'] - 1;
893 $backquoted_source_db = PMA_Util::backquote($source_db);
894 for (++$i; $i <= $last; $i++) {
895 if ($parsed_sql[$i]['type'] == $table_delimiter
896 && $parsed_sql[$i]['data'] == $backquoted_source_db
897 && $parsed_sql[$i - 1]['type'] != 'punct_qualifier'
899 $parsed_sql[$i]['data'] = $target_for_view;
902 unset($last,$backquoted_source_db);
903 } else {
904 $parsed_sql[$i]['data'] = $target;
907 /* Generate query back */
908 $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only');
909 // If table exists, and 'add drop table' is selected: Drop it!
910 $drop_query = '';
911 if (isset($_REQUEST['drop_if_exists'])
912 && $_REQUEST['drop_if_exists'] == 'true'
914 if (PMA_Table::isView($target_db, $target_table)) {
915 $drop_query = 'DROP VIEW';
916 } else {
917 $drop_query = 'DROP TABLE';
919 $drop_query .= ' IF EXISTS '
920 . PMA_Util::backquote($target_db) . '.'
921 . PMA_Util::backquote($target_table);
922 PMA_DBI_query($drop_query);
924 $GLOBALS['sql_query'] .= "\n" . $drop_query . ';';
926 // If an existing table gets deleted, maintain any
927 // entries for the PMA_* tables
928 $maintain_relations = true;
931 @PMA_DBI_query($sql_structure);
932 $GLOBALS['sql_query'] .= "\n" . $sql_structure . ';';
934 if (($move || isset($GLOBALS['add_constraints']))
935 && !empty($GLOBALS['sql_constraints_query'])
937 $parsed_sql = PMA_SQP_parse($GLOBALS['sql_constraints_query']);
938 $i = 0;
940 // find the first $table_delimiter, it must be the source
941 // table name
942 while ($parsed_sql[$i]['type'] != $table_delimiter) {
943 $i++;
944 // maybe someday we should guard against going over limit
945 //if ($i == $parsed_sql['len']) {
946 // break;
950 // replace it by the target table name, no need
951 // to backquote()
952 $parsed_sql[$i]['data'] = $target;
954 // now we must remove all $table_delimiter that follow a
955 // CONSTRAINT keyword, because a constraint name must be
956 // unique in a db
958 $cnt = $parsed_sql['len'] - 1;
960 for ($j = $i; $j < $cnt; $j++) {
961 if ($parsed_sql[$j]['type'] == 'alpha_reservedWord'
962 && strtoupper($parsed_sql[$j]['data']) == 'CONSTRAINT'
964 if ($parsed_sql[$j+1]['type'] == $table_delimiter) {
965 $parsed_sql[$j+1]['data'] = '';
970 // Generate query back
971 $GLOBALS['sql_constraints_query'] = PMA_SQP_formatHtml(
972 $parsed_sql, 'query_only'
974 if ($mode == 'one_table') {
975 PMA_DBI_query($GLOBALS['sql_constraints_query']);
977 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query'];
978 if ($mode == 'one_table') {
979 unset($GLOBALS['sql_constraints_query']);
982 } else {
983 $GLOBALS['sql_query'] = '';
986 // Copy the data unless this is a VIEW
987 if (($what == 'data' || $what == 'dataonly')
988 && ! PMA_Table::isView($target_db, $target_table)
990 $sql_set_mode = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'";
991 PMA_DBI_query($sql_set_mode);
992 $GLOBALS['sql_query'] .= "\n\n" . $sql_set_mode . ';';
994 $sql_insert_data = 'INSERT INTO ' . $target
995 . ' SELECT * FROM ' . $source;
996 PMA_DBI_query($sql_insert_data);
997 $GLOBALS['sql_query'] .= "\n\n" . $sql_insert_data . ';';
1000 $GLOBALS['cfgRelation'] = PMA_getRelationsParam();
1002 // Drops old table if the user has requested to move it
1003 if ($move) {
1005 // This could avoid some problems with replicated databases, when
1006 // moving table from replicated one to not replicated one
1007 PMA_DBI_select_db($source_db);
1009 if (PMA_Table::isView($source_db, $source_table)) {
1010 $sql_drop_query = 'DROP VIEW';
1011 } else {
1012 $sql_drop_query = 'DROP TABLE';
1014 $sql_drop_query .= ' ' . $source;
1015 PMA_DBI_query($sql_drop_query);
1017 // Renable table in configuration storage
1018 PMA_REL_renameTable(
1019 $source_db, $target_db,
1020 $source_table, $target_table
1023 $GLOBALS['sql_query'] .= "\n\n" . $sql_drop_query . ';';
1024 // end if ($move)
1025 } else {
1026 // we are copying
1027 // Create new entries as duplicates from old PMA DBs
1028 if ($what != 'dataonly' && ! isset($maintain_relations)) {
1029 if ($GLOBALS['cfgRelation']['commwork']) {
1030 // Get all comments and MIME-Types for current table
1031 $comments_copy_query = 'SELECT
1032 column_name, comment' . ($GLOBALS['cfgRelation']['mimework'] ? ', mimetype, transformation, transformation_options' : '') . '
1033 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($GLOBALS['cfgRelation']['column_info']) . '
1034 WHERE
1035 db_name = \'' . PMA_Util::sqlAddSlashes($source_db) . '\' AND
1036 table_name = \'' . PMA_Util::sqlAddSlashes($source_table) . '\'';
1037 $comments_copy_rs = PMA_queryAsControlUser($comments_copy_query);
1039 // Write every comment as new copied entry. [MIME]
1040 while ($comments_copy_row = PMA_DBI_fetch_assoc($comments_copy_rs)) {
1041 $new_comment_query = 'REPLACE INTO ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($GLOBALS['cfgRelation']['column_info'])
1042 . ' (db_name, table_name, column_name, comment' . ($GLOBALS['cfgRelation']['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ') '
1043 . ' VALUES('
1044 . '\'' . PMA_Util::sqlAddSlashes($target_db) . '\','
1045 . '\'' . PMA_Util::sqlAddSlashes($target_table) . '\','
1046 . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['column_name']) . '\''
1047 . ($GLOBALS['cfgRelation']['mimework'] ? ',\'' . PMA_Util::sqlAddSlashes($comments_copy_row['comment']) . '\','
1048 . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['mimetype']) . '\','
1049 . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['transformation']) . '\','
1050 . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['transformation_options']) . '\'' : '')
1051 . ')';
1052 PMA_queryAsControlUser($new_comment_query);
1053 } // end while
1054 PMA_DBI_free_result($comments_copy_rs);
1055 unset($comments_copy_rs);
1058 // duplicating the bookmarks must not be done here, but
1059 // just once per db
1061 $get_fields = array('display_field');
1062 $where_fields = array(
1063 'db_name' => $source_db,
1064 'table_name' => $source_table
1066 $new_fields = array(
1067 'db_name' => $target_db,
1068 'table_name' => $target_table
1070 PMA_Table::duplicateInfo(
1071 'displaywork',
1072 'table_info',
1073 $get_fields,
1074 $where_fields,
1075 $new_fields
1080 * @todo revise this code when we support cross-db relations
1082 $get_fields = array(
1083 'master_field',
1084 'foreign_table',
1085 'foreign_field'
1087 $where_fields = array(
1088 'master_db' => $source_db,
1089 'master_table' => $source_table
1091 $new_fields = array(
1092 'master_db' => $target_db,
1093 'foreign_db' => $target_db,
1094 'master_table' => $target_table
1096 PMA_Table::duplicateInfo(
1097 'relwork',
1098 'relation',
1099 $get_fields,
1100 $where_fields,
1101 $new_fields
1105 $get_fields = array(
1106 'foreign_field',
1107 'master_table',
1108 'master_field'
1110 $where_fields = array(
1111 'foreign_db' => $source_db,
1112 'foreign_table' => $source_table
1114 $new_fields = array(
1115 'master_db' => $target_db,
1116 'foreign_db' => $target_db,
1117 'foreign_table' => $target_table
1119 PMA_Table::duplicateInfo(
1120 'relwork',
1121 'relation',
1122 $get_fields,
1123 $where_fields,
1124 $new_fields
1128 $get_fields = array('x', 'y', 'v', 'h');
1129 $where_fields = array(
1130 'db_name' => $source_db,
1131 'table_name' => $source_table
1133 $new_fields = array(
1134 'db_name' => $target_db,
1135 'table_name' => $target_table
1137 PMA_Table::duplicateInfo(
1138 'designerwork',
1139 'designer_coords',
1140 $get_fields,
1141 $where_fields,
1142 $new_fields
1146 * @todo Can't get duplicating PDFs the right way. The
1147 * page numbers always get screwed up independently from
1148 * duplication because the numbers do not seem to be stored on a
1149 * per-database basis. Would the author of pdf support please
1150 * have a look at it?
1152 $get_fields = array('page_descr');
1153 $where_fields = array('db_name' => $source_db);
1154 $new_fields = array('db_name' => $target_db);
1155 $last_id = PMA_Table::duplicateInfo(
1156 'pdfwork',
1157 'pdf_pages',
1158 $get_fields,
1159 $where_fields,
1160 $new_fields
1163 if (isset($last_id) && $last_id >= 0) {
1164 $get_fields = array('x', 'y');
1165 $where_fields = array(
1166 'db_name' => $source_db,
1167 'table_name' => $source_table
1169 $new_fields = array(
1170 'db_name' => $target_db,
1171 'table_name' => $target_table,
1172 'pdf_page_number' => $last_id
1174 PMA_Table::duplicateInfo(
1175 'pdfwork',
1176 'table_coords',
1177 $get_fields,
1178 $where_fields,
1179 $new_fields
1185 return true;
1189 * checks if given name is a valid table name,
1190 * currently if not empty, trailing spaces, '.', '/' and '\'
1192 * @param string $table_name name to check
1194 * @todo add check for valid chars in filename on current system/os
1195 * @see http://dev.mysql.com/doc/refman/5.0/en/legal-names.html
1197 * @return boolean whether the string is valid or not
1199 static function isValidName($table_name)
1201 if ($table_name !== trim($table_name)) {
1202 // trailing spaces
1203 return false;
1206 if (! strlen($table_name)) {
1207 // zero length
1208 return false;
1211 if (preg_match('/[.\/\\\\]+/i', $table_name)) {
1212 // illegal char . / \
1213 return false;
1216 return true;
1220 * renames table
1222 * @param string $new_name new table name
1223 * @param string $new_db new database name
1225 * @return bool success
1227 function rename($new_name, $new_db = null)
1229 if (null !== $new_db && $new_db !== $this->getDbName()) {
1230 // Ensure the target is valid
1231 if (! $GLOBALS['pma']->databases->exists($new_db)) {
1232 $this->errors[] = __('Invalid database') . ': ' . $new_db;
1233 return false;
1235 } else {
1236 $new_db = $this->getDbName();
1239 $new_table = new PMA_Table($new_name, $new_db);
1241 if ($this->getFullName() === $new_table->getFullName()) {
1242 return true;
1245 if (! PMA_Table::isValidName($new_name)) {
1246 $this->errors[] = __('Invalid table name') . ': '
1247 . $new_table->getFullName();
1248 return false;
1251 // If the table is moved to a different database drop its triggers first
1252 $triggers = PMA_DBI_get_triggers($this->getDbName(), $this->getName(), '');
1253 $handle_triggers = $this->getDbName() != $new_db && $triggers;
1254 if ($handle_triggers) {
1255 foreach ($triggers as $trigger) {
1256 $sql = 'DROP TRIGGER IF EXISTS ' . PMA_Util::backquote($this->getDbName())
1257 . '.' . PMA_Util::backquote($trigger['name']) . ';';
1258 PMA_DBI_query($sql);
1263 * tested also for a view, in MySQL 5.0.92, 5.1.55 and 5.5.13
1265 $GLOBALS['sql_query'] = '
1266 RENAME TABLE ' . $this->getFullName(true) . '
1267 TO ' . $new_table->getFullName(true) . ';';
1268 // I don't think a specific error message for views is necessary
1269 if (! PMA_DBI_query($GLOBALS['sql_query'])) {
1270 // Restore triggers in the old database
1271 if ($handle_triggers) {
1272 PMA_DBI_select_db($this->getDbName());
1273 foreach ($triggers as $trigger) {
1274 PMA_DBI_query($trigger['create']);
1277 $this->errors[] = sprintf(
1278 __('Error renaming table %1$s to %2$s'),
1279 $this->getFullName(),
1280 $new_table->getFullName()
1282 return false;
1285 $old_name = $this->getName();
1286 $old_db = $this->getDbName();
1287 $this->setName($new_name);
1288 $this->setDbName($new_db);
1290 // Renable table in configuration storage
1291 PMA_REL_renameTable(
1292 $old_db, $new_db,
1293 $old_name, $new_name
1296 $this->messages[] = sprintf(
1297 __('Table %1$s has been renamed to %2$s.'),
1298 htmlspecialchars($old_name),
1299 htmlspecialchars($new_name)
1301 return true;
1305 * Get all unique columns
1307 * returns an array with all columns with unqiue content, in fact these are
1308 * all columns being single indexed in PRIMARY or UNIQUE
1310 * e.g.
1311 * - PRIMARY(id) // id
1312 * - UNIQUE(name) // name
1313 * - PRIMARY(fk_id1, fk_id2) // NONE
1314 * - UNIQUE(x,y) // NONE
1316 * @param bool $backquoted whether to quote name with backticks ``
1318 * @return array
1320 public function getUniqueColumns($backquoted = true)
1322 $sql = PMA_DBI_get_table_indexes_sql(
1323 $this->getDbName(),
1324 $this->getName(),
1325 'Non_unique = 0'
1327 $uniques = PMA_DBI_fetch_result(
1328 $sql,
1329 array('Key_name', null),
1330 'Column_name'
1333 $return = array();
1334 foreach ($uniques as $index) {
1335 if (count($index) > 1) {
1336 continue;
1338 $return[] = $this->getFullName($backquoted) . '.'
1339 . ($backquoted ? PMA_Util::backquote($index[0]) : $index[0]);
1342 return $return;
1346 * Get all indexed columns
1348 * returns an array with all columns make use of an index, in fact only
1349 * first columns in an index
1351 * e.g. index(col1, col2) would only return col1
1353 * @param bool $backquoted whether to quote name with backticks ``
1355 * @return array
1357 public function getIndexedColumns($backquoted = true)
1359 $sql = PMA_DBI_get_table_indexes_sql(
1360 $this->getDbName(),
1361 $this->getName(),
1362 'Seq_in_index = 1'
1364 $indexed = PMA_DBI_fetch_result($sql, 'Column_name', 'Column_name');
1366 $return = array();
1367 foreach ($indexed as $column) {
1368 $return[] = $this->getFullName($backquoted) . '.'
1369 . ($backquoted ? PMA_Util::backquote($column) : $column);
1372 return $return;
1376 * Get all columns
1378 * returns an array with all columns
1380 * @param bool $backquoted whether to quote name with backticks ``
1382 * @return array
1384 public function getColumns($backquoted = true)
1386 $sql = 'SHOW COLUMNS FROM ' . $this->getFullName(true);
1387 $indexed = PMA_DBI_fetch_result($sql, 'Field', 'Field');
1389 $return = array();
1390 foreach ($indexed as $column) {
1391 $return[] = $this->getFullName($backquoted) . '.'
1392 . ($backquoted ? PMA_Util::backquote($column) : $column);
1395 return $return;
1399 * Return UI preferences for this table from phpMyAdmin database.
1401 * @return array
1403 protected function getUiPrefsFromDb()
1405 $pma_table = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) ."."
1406 . PMA_Util::backquote($GLOBALS['cfg']['Server']['table_uiprefs']);
1408 // Read from phpMyAdmin database
1409 $sql_query = " SELECT `prefs` FROM " . $pma_table
1410 . " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'"
1411 . " AND `db_name` = '" . PMA_Util::sqlAddSlashes($this->db_name) . "'"
1412 . " AND `table_name` = '" . PMA_Util::sqlAddSlashes($this->name) . "'";
1414 $row = PMA_DBI_fetch_array(PMA_queryAsControlUser($sql_query));
1415 if (isset($row[0])) {
1416 return json_decode($row[0], true);
1417 } else {
1418 return array();
1423 * Save this table's UI preferences into phpMyAdmin database.
1425 * @return true|PMA_Message
1427 protected function saveUiPrefsToDb()
1429 $pma_table = PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
1430 . PMA_Util::backquote($GLOBALS['cfg']['Server']['table_uiprefs']);
1432 $username = $GLOBALS['cfg']['Server']['user'];
1433 $sql_query = " REPLACE INTO " . $pma_table
1434 . " VALUES ('" . $username . "', '" . PMA_Util::sqlAddSlashes($this->db_name)
1435 . "', '" . PMA_Util::sqlAddSlashes($this->name) . "', '"
1436 . PMA_Util::sqlAddSlashes(json_encode($this->uiprefs)) . "', NULL)";
1438 $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);
1440 if (!$success) {
1441 $message = PMA_Message::error(__('Could not save table UI preferences'));
1442 $message->addMessage('<br /><br />');
1443 $message->addMessage(
1444 PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink']))
1446 return $message;
1449 // Remove some old rows in table_uiprefs if it exceeds the configured
1450 // maximum rows
1451 $sql_query = 'SELECT COUNT(*) FROM ' . $pma_table;
1452 $rows_count = PMA_DBI_fetch_value($sql_query);
1453 $max_rows = $GLOBALS['cfg']['Server']['MaxTableUiprefs'];
1454 if ($rows_count > $max_rows) {
1455 $num_rows_to_delete = $rows_count - $max_rows;
1456 $sql_query
1457 = ' DELETE FROM ' . $pma_table .
1458 ' ORDER BY last_update ASC' .
1459 ' LIMIT ' . $num_rows_to_delete;
1460 $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']);
1462 if (!$success) {
1463 $message = PMA_Message::error(
1464 sprintf(
1465 __('Failed to cleanup table UI preferences (see $cfg[\'Servers\'][$i][\'MaxTableUiprefs\'] %s)'),
1466 PMA_Util::showDocu('config', 'cfg_Servers_MaxTableUiprefs')
1469 $message->addMessage('<br /><br />');
1470 $message->addMessage(
1471 PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink']))
1473 print_r($message);
1474 return $message;
1478 return true;
1482 * Loads the UI preferences for this table.
1483 * If pmadb and table_uiprefs is set, it will load the UI preferences from
1484 * phpMyAdmin database.
1486 * @return void
1488 protected function loadUiPrefs()
1490 $server_id = $GLOBALS['server'];
1491 // set session variable if it's still undefined
1492 if (! isset($_SESSION['tmp_user_values']['table_uiprefs'][$server_id][$this->db_name][$this->name])) {
1493 // check whether we can get from pmadb
1494 $_SESSION['tmp_user_values']['table_uiprefs'][$server_id][$this->db_name][$this->name]
1495 = (strlen($GLOBALS['cfg']['Server']['pmadb'])
1496 && strlen($GLOBALS['cfg']['Server']['table_uiprefs']))
1497 ? $this->getUiPrefsFromDb()
1498 : array();
1500 $this->uiprefs =& $_SESSION['tmp_user_values']['table_uiprefs'][$server_id]
1501 [$this->db_name][$this->name];
1505 * Get a property from UI preferences.
1506 * Return false if the property is not found.
1507 * Available property:
1508 * - PROP_SORTED_COLUMN
1509 * - PROP_COLUMN_ORDER
1510 * - PROP_COLUMN_VISIB
1512 * @param string $property property
1514 * @return mixed
1516 public function getUiProp($property)
1518 if (! isset($this->uiprefs)) {
1519 $this->loadUiPrefs();
1521 // do checking based on property
1522 if ($property == self::PROP_SORTED_COLUMN) {
1523 if (isset($this->uiprefs[$property])) {
1524 // check if the column name is exist in this table
1525 $tmp = explode(' ', $this->uiprefs[$property]);
1526 $colname = $tmp[0];
1527 $avail_columns = $this->getColumns();
1528 foreach ($avail_columns as $each_col) {
1529 // check if $each_col ends with $colname
1530 if (substr_compare($each_col, $colname, strlen($each_col) - strlen($colname)) === 0) {
1531 return $this->uiprefs[$property];
1534 // remove the property, since it is not exist anymore in database
1535 $this->removeUiProp(self::PROP_SORTED_COLUMN);
1536 return false;
1537 } else {
1538 return false;
1540 } elseif ($property == self::PROP_COLUMN_ORDER
1541 || $property == self::PROP_COLUMN_VISIB
1543 if (! PMA_Table::isView($this->db_name, $this->name)
1544 && isset($this->uiprefs[$property])
1546 // check if the table has not been modified
1547 if (self::sGetStatusInfo($this->db_name, $this->name, 'Create_time') == $this->uiprefs['CREATE_TIME']) {
1548 return $this->uiprefs[$property];
1549 } else {
1550 // remove the property, since the table has been modified
1551 $this->removeUiProp(self::PROP_COLUMN_ORDER);
1552 return false;
1554 } else {
1555 return false;
1558 // default behaviour for other property:
1559 return isset($this->uiprefs[$property]) ? $this->uiprefs[$property] : false;
1563 * Set a property from UI preferences.
1564 * If pmadb and table_uiprefs is set, it will save the UI preferences to
1565 * phpMyAdmin database.
1566 * Available property:
1567 * - PROP_SORTED_COLUMN
1568 * - PROP_COLUMN_ORDER
1569 * - PROP_COLUMN_VISIB
1571 * @param string $property Property
1572 * @param mixed $value Value for the property
1573 * @param string $table_create_time Needed for PROP_COLUMN_ORDER
1574 * and PROP_COLUMN_VISIB
1576 * @return boolean|PMA_Message
1578 public function setUiProp($property, $value, $table_create_time = null)
1580 if (! isset($this->uiprefs)) {
1581 $this->loadUiPrefs();
1583 // we want to save the create time if the property is PROP_COLUMN_ORDER
1584 if (! PMA_Table::isView($this->db_name, $this->name)
1585 && ($property == self::PROP_COLUMN_ORDER
1586 || $property == self::PROP_COLUMN_VISIB)
1588 $curr_create_time = self::sGetStatusInfo(
1589 $this->db_name,
1590 $this->name,
1591 'CREATE_TIME'
1593 if (isset($table_create_time)
1594 && $table_create_time == $curr_create_time
1596 $this->uiprefs['CREATE_TIME'] = $curr_create_time;
1597 } else {
1598 // there is no $table_create_time, or
1599 // supplied $table_create_time is older than current create time,
1600 // so don't save
1601 return PMA_Message::error(
1602 sprintf(
1603 __('Cannot save UI property "%s". The changes made will not be persistent after you refresh this page. Please check if the table structure has been changed.'),
1604 $property
1609 // save the value
1610 $this->uiprefs[$property] = $value;
1611 // check if pmadb is set
1612 if (strlen($GLOBALS['cfg']['Server']['pmadb'])
1613 && strlen($GLOBALS['cfg']['Server']['table_uiprefs'])
1615 return $this->saveUiprefsToDb();
1617 return true;
1621 * Remove a property from UI preferences.
1623 * @param string $property the property
1625 * @return true|PMA_Message
1627 public function removeUiProp($property)
1629 if (! isset($this->uiprefs)) {
1630 $this->loadUiPrefs();
1632 if (isset($this->uiprefs[$property])) {
1633 unset($this->uiprefs[$property]);
1634 // check if pmadb is set
1635 if (strlen($GLOBALS['cfg']['Server']['pmadb'])
1636 && strlen($GLOBALS['cfg']['Server']['table_uiprefs'])
1638 return $this->saveUiprefsToDb();
1641 return true;
1645 * Get all column names which are MySQL reserved words
1647 * @return array
1648 * @access public
1650 public function getReservedColumnNames()
1652 $columns = $this->getColumns($backquoted = false);
1653 $return = array();
1654 foreach ($columns as $column) {
1655 $temp = explode('.', $column);
1656 $column_name = $temp[2];
1657 if (PMA_SQP_isKeyWord($column_name)) {
1658 $return[] = $column_name;
1661 return $return;