📄 table.class.php
字号:
<?php/* vim: set expandtab sw=4 ts=4 sts=4: *//** * * @version $Id: Table.class.php 11579 2008-09-08 17:10:58Z lem9 $ *//** * @todo make use of PMA_Message and PMA_Error */class PMA_Table{ static $cache = array(); /** * @var string table name */ var $name = ''; /** * @var string database name */ var $db_name = ''; /** * @var string engine (innodb, myisam, bdb, ...) */ var $engine = ''; /** * @var string type (view, base table, system view) */ var $type = ''; /** * @var array settings */ var $settings = array(); /** * @var array errors occured */ var $errors = array(); /** * @var array messages */ var $messages = array(); /** * Constructor * * @param string $table_name table name * @param string $db_name database name */ function __construct($table_name, $db_name) { $this->setName($table_name); $this->setDbName($db_name); } /** * @see PMA_Table::getName() */ function __toString() { return $this->getName(); } function getLastError() { return end($this->errors); } function getLastMessage() { return end($this->messages); } /** * sets table name * * @uses $this->name to set it * @param string $table_name new table name */ function setName($table_name) { $this->name = $table_name; } /** * returns table name * * @uses $this->name as return value * @param boolean whether to quote name with backticks `` * @return string table name */ function getName($quoted = false) { if ($quoted) { return PMA_backquote($this->name); } return $this->name; } /** * sets database name for this table * * @uses $this->db_name to set it * @param string $db_name */ function setDbName($db_name) { $this->db_name = $db_name; } /** * returns database name for this table * * @uses $this->db_name as return value * @param boolean whether to quote name with backticks `` * @return string database name for this table */ function getDbName($quoted = false) { if ($quoted) { return PMA_backquote($this->db_name); } return $this->db_name; } /** * returns full name for table, including database name * * @param boolean whether to quote name with backticks `` */ function getFullName($quoted = false) { return $this->getDbName($quoted) . '.' . $this->getName($quoted); } static public function isView($db = null, $table = null) { if (strlen($db) && strlen($table)) { return PMA_Table::_isView($db, $table); } if (isset($this) && strpos($this->get('TABLE TYPE'), 'VIEW')) { return true; } return false; } /** * sets given $value for given $param * * @uses $this->settings to add or change value * @param string param name * @param mixed param value */ function set($param, $value) { $this->settings[$param] = $value; } /** * returns value for given setting/param * * @uses $this->settings to return value * @param string name for value to return * @return mixed value for $param */ function get($param) { if (isset($this->settings[$param])) { return $this->settings[$param]; } return null; } /** * loads structure data * (this function is work in progress? not yet used) */ function loadStructure() { $table_info = PMA_DBI_get_tables_full($this->getDbName(), $this->getName()); if (false === $table_info) { return false; } $this->settings = $table_info; if ($this->get('TABLE_ROWS') === null) { $this->set('TABLE_ROWS', PMA_Table::countRecords($this->getDbName(), $this->getName(), true, true)); } $create_options = explode(' ', $this->get('TABLE_ROWS')); // export create options by its name as variables into gloabel namespace // f.e. pack_keys=1 becomes available as $pack_keys with value of '1' foreach ($create_options as $each_create_option) { $each_create_option = explode('=', $each_create_option); if (isset($each_create_option[1])) { $this->set($$each_create_option[0], $each_create_option[1]); } } } /** * Checks if this "table" is a view * * @deprecated * @todo see what we could do with the possible existence of $table_is_view * @param string the database name * @param string the table name * * @return boolean whether this is a view * * @access public */ static protected function _isView($db, $table) { // maybe we already know if the table is a view if (isset($GLOBALS['tbl_is_view']) && $GLOBALS['tbl_is_view']) { return true; } // This would be the correct way of doing the check but at least in // MySQL 5.0.33 it's too slow when there are hundreds of databases // and/or tables (more than 3 minutes for 400 tables) /*if (false === PMA_DBI_fetch_value('SELECT TABLE_NAME FROM `information_schema`.`VIEWS` WHERE `TABLE_SCHEMA` = \'' . $db . '\' AND `TABLE_NAME` = \'' . $table . '\';')) { return false; } else { return true; } */ // A more complete verification would be to check if all columns // from the result set are NULL except Name and Comment. // MySQL from 5.0.0 to 5.0.12 returns 'view', // from 5.0.13 returns 'VIEW'. // use substr() because the comment might contain something like: // (VIEW 'BASE2.VTEST' REFERENCES INVALID TABLE(S) OR COLUMN(S) OR FUNCTION) $comment = strtoupper(PMA_Table::sGetStatusInfo($db, $table, 'Comment')); return substr($comment, 0, 4) == 'VIEW'; } static public function sGetToolTip($db, $table) { return PMA_Table::sGetStatusInfo($db, $table, 'Comment') . ' (' . PMA_Table::countRecords($db, $table, true) . ')'; } static public function sGetStatusInfo($db, $table, $info = null, $force_read = false) { if (! isset(PMA_Table::$cache[$db][$table]) || $force_read) { PMA_Table::$cache[$db][$table] = PMA_DBI_fetch_single_row('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . $table . '\''); } if (null === $info) { return PMA_Table::$cache[$db][$table]; } if (! isset(PMA_Table::$cache[$db][$table][$info]) || $force_read) { PMA_Table::$cache[$db][$table] = PMA_DBI_fetch_single_row('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . $table . '\''); } return PMA_Table::$cache[$db][$table][$info]; } /** * generates column/field specification for ALTER or CREATE TABLE syntax * * @todo move into class PMA_Column * @todo on the interface, some js to clear the default value when the default * current_timestamp is checked * @static * @param string $name name * @param string $type type ('INT', 'VARCHAR', 'BIT', ...) * @param string $length length ('2', '5,2', '', ...) * @param string $attribute * @param string $collation * @param string $null with 'NULL' or 'NOT NULL' * @param string $default_type whether default is CURRENT_TIMESTAMP, * NULL, NONE, USER_DEFINED * @param boolean $default_value default value for USER_DEFINED default type
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -