Cannot create a table in information_schema
[phpmyadmin/ammaryasirr.git] / libraries / db_info.inc.php
blobd8610f29a5ae08b81fc5fc009fca572dfa543579
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Gets the list of the table in the current db and informations about these
5 * tables if possible
7 * fills tooltip arrays and provides $tables, $num_tables, $is_show_stats
8 * and $db_is_information_schema
10 * speedup view on locked tables
12 * @uses $cfg['ShowStats']
13 * @uses $cfg['ShowTooltip']
14 * @uses $cfg['ShowTooltipAliasTB']
15 * @uses $cfg['SkipLockedTables']
16 * @uses $GLOBALS['db']
17 * @uses PMA_fillTooltip()
18 * @uses PMA_checkParameters()
19 * @uses PMA_escape_mysql_wildcards()
20 * @uses PMA_DBI_query()
21 * @uses PMA_backquote()
22 * @uses PMA_DBI_num_rows()
23 * @uses PMA_DBI_fetch_row()
24 * @uses PMA_DBI_fetch_assoc()
25 * @uses PMA_DBI_free_result()
26 * @uses PMA_DBI_get_tables_full()
27 * @uses PMA_isValid()
28 * @uses preg_match()
29 * @uses preg_quote()
30 * @uses uksort()
31 * @uses strnatcasecmp()
32 * @uses count()
33 * @uses addslashes()
34 * @package phpMyAdmin
36 if (! defined('PHPMYADMIN')) {
37 exit;
40 /**
41 * requirements
43 require_once './libraries/common.inc.php';
45 /**
46 * limits for table list
48 if (! isset($_SESSION['tmp_user_values']['table_limit_offset']) || $_SESSION['tmp_user_values']['table_limit_offset_db'] != $db) {
49 $_SESSION['tmp_user_values']['table_limit_offset'] = 0;
50 $_SESSION['tmp_user_values']['table_limit_offset_db'] = $db;
52 if (isset($_REQUEST['pos'])) {
53 $_SESSION['tmp_user_values']['table_limit_offset'] = (int) $_REQUEST['pos'];
55 $pos = $_SESSION['tmp_user_values']['table_limit_offset'];
57 /**
58 * fills given tooltip arrays
60 * @uses $cfg['ShowTooltipAliasTB']
61 * @uses PMA_localisedDate()
62 * @uses strtotime()
63 * @param array $tooltip_truename tooltip data
64 * @param array $tooltip_aliasname tooltip data
65 * @param array $table tabledata
67 function PMA_fillTooltip(&$tooltip_truename, &$tooltip_aliasname, $table)
69 if (empty($table['Comment'])) {
70 $table['Comment'] = $table['Name'];
71 } else {
72 // why?
73 $table['Comment'] .= ' ';
76 if ($GLOBALS['cfg']['ShowTooltipAliasTB']
77 && $GLOBALS['cfg']['ShowTooltipAliasTB'] != 'nested') {
78 $tooltip_truename[$table['Name']] = $table['Comment'];
79 $tooltip_aliasname[$table['Name']] = $table['Name'];
80 } else {
81 $tooltip_truename[$table['Name']] = $table['Name'];
82 $tooltip_aliasname[$table['Name']] = $table['Comment'];
85 if (isset($table['Create_time']) && !empty($table['Create_time'])) {
86 $tooltip_aliasname[$table['Name']] .= ', ' . __('Creation')
87 . ': ' . PMA_localisedDate(strtotime($table['Create_time']));
90 if (! empty($table['Update_time'])) {
91 $tooltip_aliasname[$table['Name']] .= ', ' . __('Last update')
92 . ': ' . PMA_localisedDate(strtotime($table['Update_time']));
95 if (! empty($table['Check_time'])) {
96 $tooltip_aliasname[$table['Name']] .= ', ' . __('Last check')
97 . ': ' . PMA_localisedDate(strtotime($table['Check_time']));
101 PMA_checkParameters(array('db'));
104 * @global bool whether to display extended stats
106 $is_show_stats = $cfg['ShowStats'];
109 * @global bool whether selected db is information_schema
111 $db_is_information_schema = false;
113 if ($db == 'information_schema') {
114 $is_show_stats = false;
115 $db_is_information_schema = true;
119 * @global array information about tables in db
121 $tables = array();
123 // When used in Nested table group mode, only show tables matching the given groupname
124 if (PMA_isValid($tbl_group) && !$cfg['ShowTooltipAliasTB']) {
125 $tbl_group_sql = ' LIKE "' . PMA_escape_mysql_wildcards($tbl_group) . '%"';
126 } else {
127 $tbl_group_sql = '';
130 if ($cfg['ShowTooltip']) {
131 $tooltip_truename = array();
132 $tooltip_aliasname = array();
135 // Special speedup for newer MySQL Versions (in 4.0 format changed)
136 if (true === $cfg['SkipLockedTables']) {
137 $db_info_result = PMA_DBI_query('SHOW OPEN TABLES FROM ' . PMA_backquote($db) . ';');
139 // Blending out tables in use
140 if ($db_info_result && PMA_DBI_num_rows($db_info_result) > 0) {
141 while ($tmp = PMA_DBI_fetch_row($db_info_result)) {
142 // if in use memorize tablename
143 if (preg_match('@in_use=[1-9]+@i', $tmp[1])) {
144 $sot_cache[$tmp[0]] = true;
147 PMA_DBI_free_result($db_info_result);
149 if (isset($sot_cache)) {
150 $db_info_result = PMA_DBI_query(
151 'SHOW TABLES FROM ' . PMA_backquote($db) . $tbl_group_sql . ';',
152 null, PMA_DBI_QUERY_STORE);
153 if ($db_info_result && PMA_DBI_num_rows($db_info_result) > 0) {
154 while ($tmp = PMA_DBI_fetch_row($db_info_result)) {
155 if (!isset($sot_cache[$tmp[0]])) {
156 $sts_result = PMA_DBI_query(
157 'SHOW TABLE STATUS FROM ' . PMA_backquote($db)
158 . ' LIKE \'' . addslashes($tmp[0]) . '\';');
159 $sts_tmp = PMA_DBI_fetch_assoc($sts_result);
160 PMA_DBI_free_result($sts_result);
161 unset($sts_result);
163 if (!isset($sts_tmp['Type']) && isset($sts_tmp['Engine'])) {
164 $sts_tmp['Type'] =& $sts_tmp['Engine'];
167 if (!empty($tbl_group) && $cfg['ShowTooltipAliasTB']
168 && !preg_match('@' . preg_quote($tbl_group, '@') . '@i', $sts_tmp['Comment'])) {
169 continue;
172 if ($cfg['ShowTooltip']) {
173 PMA_fillTooltip($tooltip_truename, $tooltip_aliasname, $sts_tmp);
176 $tables[$sts_tmp['Name']] = $sts_tmp;
177 } else { // table in use
178 $tables[$tmp[0]] = array('Name' => $tmp[0]);
181 if ($GLOBALS['cfg']['NaturalOrder']) {
182 uksort($tables, 'strnatcasecmp');
185 $sot_ready = true;
186 } elseif ($db_info_result) {
187 PMA_DBI_free_result($db_info_result);
189 unset($sot_cache);
191 unset($tmp);
192 } elseif ($db_info_result) {
193 PMA_DBI_free_result($db_info_result);
197 if (! isset($sot_ready)) {
199 // Set some sorting defaults
200 $sort = 'Name';
201 $sort_order = 'ASC';
203 if (isset($_REQUEST['sort'])) {
204 $sortable_name_mappings = array(
205 'table' => 'Name',
206 'records' => 'Rows',
207 'type' => 'Engine',
208 'collation' => 'Collation',
209 'size' => 'Data_length',
210 'overhead' => 'Data_free'
213 // Make sure the sort type is implemented
214 if (isset($sortable_name_mappings[$_REQUEST['sort']])) {
215 $sort = $sortable_name_mappings[$_REQUEST['sort']];
216 if ($_REQUEST['sort_order'] == 'DESC') {
217 $sort_order = 'DESC';
222 if (! empty($tbl_group) && ! $cfg['ShowTooltipAliasTB']) {
223 // only tables for selected group
224 $tables = PMA_DBI_get_tables_full($db, $tbl_group, true, null, 0, false, $sort, $sort_order);
225 } elseif (! empty($tbl_group) && $cfg['ShowTooltipAliasTB']) {
226 // only tables for selected group,
227 // but grouping is done on comment ...
228 $tables = PMA_DBI_get_tables_full($db, $tbl_group, 'comment', null, 0, false, $sort, $sort_order);
229 } else {
230 // all tables in db
231 // - get the total number of tables
232 // (needed for proper working of the MaxTableList feature)
233 $tables = PMA_DBI_get_tables($db);
234 $total_num_tables = count($tables);
235 if (isset($sub_part) && $sub_part == '_export') {
236 // (don't fetch only a subset if we are coming from db_export.php,
237 // because I think it's too risky to display only a subset of the
238 // table names when exporting a db)
241 * @todo Page selector for table names?
243 $tables = PMA_DBI_get_tables_full($db, false, false, null, 0, false, $sort, $sort_order);
244 } else {
245 // fetch the details for a possible limited subset
246 $tables = PMA_DBI_get_tables_full($db, false, false, null, $pos, true, $sort, $sort_order);
250 if ($cfg['ShowTooltip']) {
251 foreach ($tables as $each_table) {
252 PMA_fillTooltip($tooltip_truename, $tooltip_aliasname, $each_table);
258 * @global int count of tables in db
260 $num_tables = count($tables);
261 // (needed for proper working of the MaxTableList feature)
262 if (! isset($total_num_tables)) {
263 $total_num_tables = $num_tables;
267 * cleanup
269 unset($each_table, $tbl_group_sql, $db_info_result);
272 * Displays top menu links
273 * If in an Ajax request, we do not need to show this
275 if($GLOBALS['is_ajax_request'] != true) {
276 require './libraries/db_links.inc.php';