2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 * Get some core libraries
10 require_once 'libraries/common.inc.php';
12 $action = 'tbl_create.php';
14 $titles = PMA_Util
::buildActionTitles();
17 PMA_Util
::checkParameters(array('db'));
19 /* Check if database name is empty */
20 if (strlen($db) == 0) {
22 __('The database name is empty!'), '', '', 'index.php'
27 * Defines the url to return to in case of error in a sql statement
29 if (PMA_DBI_get_columns($db, $table)) {
30 // table exists already
32 sprintf(__('Table %s already exists!'), htmlspecialchars($table)),
35 'db_structure.php?' . PMA_generate_common_url($db)
39 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
41 // check number of fields to be created
42 if (isset($_REQUEST['submit_num_fields'])) {
43 $regenerate = true; // for libraries/tbl_columns_definition_form.inc.php
44 $num_fields = $_REQUEST['orig_num_fields'] +
$_REQUEST['added_fields'];
45 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
46 $num_fields = (int) $_REQUEST['num_fields'];
52 * Selects the database to work with
54 if (!PMA_DBI_select_db($db)) {
56 sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
64 * The form used to define the structure of the table has been submitted
66 if (isset($_REQUEST['do_save_data'])) {
69 // Transforms the radio button field_key into 3 arrays
70 $field_cnt = count($_REQUEST['field_name']);
71 for ($i = 0; $i < $field_cnt; ++
$i) {
72 if (isset($_REQUEST['field_key'][$i])) {
73 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
74 $field_primary[] = $i;
76 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
79 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
85 // Builds the fields creation statements
86 for ($i = 0; $i < $field_cnt; $i++
) {
87 // '0' is also empty for php :-(
88 if (empty($_REQUEST['field_name'][$i])
89 && $_REQUEST['field_name'][$i] != '0'
94 $query = PMA_Table
::generateFieldSpec(
95 $_REQUEST['field_name'][$i],
96 $_REQUEST['field_type'][$i],
98 $_REQUEST['field_length'][$i],
99 $_REQUEST['field_attribute'][$i],
100 isset($_REQUEST['field_collation'][$i])
101 ?
$_REQUEST['field_collation'][$i]
103 isset($_REQUEST['field_null'][$i])
104 ?
$_REQUEST['field_null'][$i]
106 $_REQUEST['field_default_type'][$i],
107 $_REQUEST['field_default_value'][$i],
108 isset($_REQUEST['field_extra'][$i])
109 ?
$_REQUEST['field_extra'][$i]
111 isset($_REQUEST['field_comments'][$i])
112 ?
$_REQUEST['field_comments'][$i]
119 $sql_query .= $query;
121 unset($field_cnt, $query);
122 $sql_query = preg_replace('@, $@', '', $sql_query);
124 // Builds the primary keys statements
126 $primary_cnt = (isset($field_primary) ?
count($field_primary) : 0);
127 for ($i = 0; $i < $primary_cnt; $i++
) {
128 $j = $field_primary[$i];
129 if (isset($_REQUEST['field_name'][$j])
130 && strlen($_REQUEST['field_name'][$j])
132 $primary .= PMA_Util
::backquote($_REQUEST['field_name'][$j]) . ', ';
136 $primary = preg_replace('@, $@', '', $primary);
137 if (strlen($primary)) {
138 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
142 // Builds the indexes statements
144 $index_cnt = (isset($field_index) ?
count($field_index) : 0);
145 for ($i = 0;$i < $index_cnt; $i++
) {
146 $j = $field_index[$i];
147 if (isset($_REQUEST['field_name'][$j])
148 && strlen($_REQUEST['field_name'][$j])
150 $index .= PMA_Util
::backquote($_REQUEST['field_name'][$j]) . ', ';
154 $index = preg_replace('@, $@', '', $index);
155 if (strlen($index)) {
156 $sql_query .= ', INDEX (' . $index . ')';
160 // Builds the uniques statements
162 $unique_cnt = (isset($field_unique) ?
count($field_unique) : 0);
163 for ($i = 0; $i < $unique_cnt; $i++
) {
164 $j = $field_unique[$i];
165 if (isset($_REQUEST['field_name'][$j])
166 && strlen($_REQUEST['field_name'][$j])
168 $unique .= PMA_Util
::backquote($_REQUEST['field_name'][$j]) . ', ';
172 $unique = preg_replace('@, $@', '', $unique);
173 if (strlen($unique)) {
174 $sql_query .= ', UNIQUE (' . $unique . ')';
178 // Builds the FULLTEXT statements
180 $fulltext_cnt = (isset($field_fulltext) ?
count($field_fulltext) : 0);
181 for ($i = 0; $i < $fulltext_cnt; $i++
) {
182 $j = $field_fulltext[$i];
183 if (isset($_REQUEST['field_name'][$j])
184 && strlen($_REQUEST['field_name'][$j])
186 $fulltext .= PMA_Util
::backquote($_REQUEST['field_name'][$j]) . ', ';
190 $fulltext = preg_replace('@, $@', '', $fulltext);
191 if (strlen($fulltext)) {
192 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
196 // Builds the 'create table' statement
197 $sql_query = 'CREATE TABLE ' . PMA_Util
::backquote($db) . '.'
198 . PMA_Util
::backquote($table) . ' (' . $sql_query . ')';
200 // Adds table type, character set, comments and partition definition
201 if (!empty($_REQUEST['tbl_storage_engine'])
202 && ($_REQUEST['tbl_storage_engine'] != 'Default')
204 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine'];
206 if (!empty($_REQUEST['tbl_collation'])) {
207 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
209 if (!empty($_REQUEST['comment'])) {
210 $sql_query .= ' COMMENT = \''
211 . PMA_Util
::sqlAddSlashes($_REQUEST['comment']) . '\'';
213 if (!empty($_REQUEST['partition_definition'])) {
214 $sql_query .= ' ' . PMA_Util
::sqlAddSlashes(
215 $_REQUEST['partition_definition']
220 // Executes the query
221 $result = PMA_DBI_try_query($sql_query);
225 // If comments were sent, enable relation stuff
226 include_once 'libraries/transformations.lib.php';
228 // Update comment table for mime types [MIME]
229 if (isset($_REQUEST['field_mimetype'])
230 && is_array($_REQUEST['field_mimetype'])
231 && $cfg['BrowseMIME']
233 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
234 if (isset($_REQUEST['field_name'][$fieldindex])
235 && strlen($_REQUEST['field_name'][$fieldindex])
238 $db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
239 $_REQUEST['field_transformation'][$fieldindex],
240 $_REQUEST['field_transformation_options'][$fieldindex]
246 $message = PMA_Message
::success(__('Table %1$s has been created.'));
248 PMA_Util
::backquote($db) . '.' . PMA_Util
::backquote($table)
251 if ($GLOBALS['is_ajax_request'] == true) {
254 * construct the html for the newly created table's row to be appended
255 * to the list of tables.
257 * Logic taken from db_structure.php
260 $tbl_url_params = array();
261 $tbl_url_params['db'] = $db;
262 $tbl_url_params['table'] = $table;
263 $is_show_stats = $cfg['ShowStats'];
265 $tbl_stats_result = PMA_DBI_query(
266 'SHOW TABLE STATUS FROM ' . PMA_Util
::backquote($db)
267 . ' LIKE \'' . PMA_Util
::sqlAddSlashes($table, true) . '\';'
269 $tbl_stats = PMA_DBI_fetch_assoc($tbl_stats_result);
270 PMA_DBI_free_result($tbl_stats_result);
271 unset($tbl_stats_result);
273 if ($is_show_stats) {
274 $sum_size = (double) 0;
275 $overhead_size = (double) 0;
276 $overhead_check = '';
278 $tblsize = doubleval($tbl_stats['Data_length'])
279 +
doubleval($tbl_stats['Index_length']);
280 $sum_size +
= $tblsize;
281 list($formatted_size, $unit) = PMA_Util
::formatByteDown(
284 ($tblsize > 0) ?
1 : 0
286 if (isset($tbl_stats['Data_free']) && $tbl_stats['Data_free'] > 0) {
287 list($formatted_overhead, $overhead_unit) = PMA_Util
::formatByteDown(
288 $tbl_stats['Data_free'],
290 ($tbl_stats['Data_free'] > 0) ?
1 : 0
292 $overhead_size +
= $tbl_stats['Data_free'];
295 if (isset($formatted_overhead)) {
296 $overhead = '<span>' . $formatted_overhead . '</span>'
297 . '<span class="unit">' . $overhead_unit . '</span>';
298 unset($formatted_overhead);
304 $new_table_string = '<tr>' . "\n";
305 $new_table_string .= '<td class="center">'
306 . '<input type="checkbox" id="checkbox_tbl_"'
307 . ' name="selected_tbl[]" value="'.htmlspecialchars($table).'" />'
310 $new_table_string .= '<th>';
311 $new_table_string .= '<a href="sql.php'
312 . PMA_generate_common_url($tbl_url_params) . '">'
313 . htmlspecialchars($table) . '</a>';
315 if (PMA_Tracker
::isActive()) {
316 $truename = str_replace(' ', ' ', htmlspecialchars($table));
317 if (PMA_Tracker
::isTracked($db, $truename)) {
318 $new_table_string .= '<a href="tbl_tracking.php'
319 . PMA_generate_common_url($tbl_url_params) . '">';
320 $new_table_string .= PMA_Util
::getImage(
321 'eye.png', __('Tracking is active.')
323 } elseif (PMA_Tracker
::getVersion($db, $truename) > 0) {
324 $new_table_string .= '<a href="tbl_tracking.php'
325 . PMA_generate_common_url($tbl_url_params) . '">';
326 $new_table_string .= PMA_Util
::getImage(
327 'eye_grey.png', __('Tracking is not active.')
332 $new_table_string .= '</th>' . "\n";
334 $new_table_string .= '<td>' . $titles['NoBrowse'] . '</td>' . "\n";
336 $new_table_string .= '<td>'
337 . '<a href="tbl_structure.php'
338 . PMA_generate_common_url($tbl_url_params) . '">'
339 . $titles['Structure']
343 $new_table_string .= '<td>' . $titles['NoSearch'] . '</td>' . "\n";
345 $new_table_string .= '<td>'
346 . '<a href="tbl_change.php'
347 . PMA_generate_common_url($tbl_url_params) . '">'
352 $new_table_string .= '<td>' . $titles['NoEmpty'] . '</td>' . "\n";
354 $new_table_string .= '<td>'
355 . '<a class="drop_table_anchor" href="sql.php'
356 . PMA_generate_common_url($tbl_url_params) . '&sql_query='
357 . urlencode('DROP TABLE ' . PMA_Util
::backquote($table)) . '">'
362 $new_table_string .= '<td class="value">'
366 $new_table_string .= '<td class="nowrap">'
367 . $tbl_stats['Engine']
370 $new_table_string .= '<td>'
371 . '<dfn title="' . PMA_getCollationDescr($tbl_stats['Collation']) . '">'
372 . $tbl_stats['Collation']
376 if ($is_show_stats) {
377 $new_table_string .= '<td class="value tbl_size">'
378 . '<a href="tbl_structure.php'
379 . PMA_generate_common_url($tbl_url_params) . '#showusage" >'
380 . '<span>' . $formatted_size . '</span>'
381 . '<span class="unit">' . $unit . '</span>'
385 $new_table_string .= '<td class="value tbl_overhead">'
389 $new_table_string .= '</tr>' . "\n";
391 $formatted_sql = PMA_Util
::getMessage(
392 $message, $sql_query, 'success'
395 $response = PMA_Response
::getInstance();
396 $response->addJSON('message', $message);
397 $response->addJSON('formatted_sql', $formatted_sql);
398 $response->addJSON('new_table_string', $new_table_string);
401 $display_query = $sql_query;
404 // read table info on this newly created table, in case
405 // the next page is Structure
407 include 'libraries/tbl_info.inc.php';
409 // do not switch to sql.php
410 // as there is no row to be displayed on a new table
411 if ($cfg['DefaultTabTable'] === 'sql.php') {
412 include 'tbl_structure.php';
414 include '' . $cfg['DefaultTabTable'];
418 if ($GLOBALS['is_ajax_request'] == true) {
419 $response = PMA_Response
::getInstance();
420 $response->isSuccess(false);
421 $response->addJSON('message', PMA_DBI_getError());
423 echo PMA_Util
::mysqlDie('', '', '', $err_url, false);
424 // An error happened while inserting/updating a table definition.
425 // To prevent total loss of that data, we embed the form once again.
426 // The variable $regenerate will be used to restore data in
427 // libraries/tbl_columns_definition_form.inc.php
428 $num_fields = $_REQUEST['orig_num_fields'];
433 } // end do create table
436 * Displays the form used to define the structure of the table
439 // This div is used to show the content(eg: create table form with more columns)
440 // fetched with AJAX subsequently.
441 if ($GLOBALS['is_ajax_request'] != true) {
442 echo('<div id="create_table_div">');
445 require 'libraries/tbl_columns_definition_form.inc.php';
447 if ($GLOBALS['is_ajax_request'] != true) {