Common strings for descriptions of DATE, TIME, DATETIME and VARCHAR2
[phpmyadmin.git] / tbl_create.php
blob978c83a742b712d8463b3870f8bec7a37f13032d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @package PhpMyAdmin
5 */
7 /**
8 * Get some core libraries
9 */
10 require_once 'libraries/common.inc.php';
12 $action = 'tbl_create.php';
14 require_once 'libraries/header.inc.php';
15 $titles = PMA_buildActionTitles();
17 // Check parameters
18 PMA_checkParameters(array('db'));
20 /* Check if database name is empty */
21 if (strlen($db) == 0) {
22 PMA_mysqlDie(__('The database name is empty!'), '', '', 'main.php');
25 /**
26 * Defines the url to return to in case of error in a sql statement
28 if (PMA_DBI_get_columns($db, $table)) {
29 // table exists already
30 PMA_mysqlDie(
31 sprintf(__('Table %s already exists!'), htmlspecialchars($table)),
32 '',
33 '',
34 'db_structure.php?' . PMA_generate_common_url($db)
38 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
40 // check number of fields to be created
41 if (isset($_REQUEST['submit_num_fields'])) {
42 $regenerate = true; // for libraries/tbl_properties.inc.php
43 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
44 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
45 $num_fields = (int) $_REQUEST['num_fields'];
46 } else {
47 $num_fields = 4;
50 /**
51 * Selects the database to work with
53 if (!PMA_DBI_select_db($db)) {
54 PMA_mysqlDie(
55 sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
56 '',
57 '',
58 'main.php'
62 /**
63 * The form used to define the structure of the table has been submitted
65 if (isset($_REQUEST['do_save_data'])) {
66 $sql_query = '';
68 // Transforms the radio button field_key into 3 arrays
69 $field_cnt = count($_REQUEST['field_name']);
70 for ($i = 0; $i < $field_cnt; ++$i) {
71 if (isset($_REQUEST['field_key'][$i])) {
72 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
73 $field_primary[] = $i;
75 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
76 $field_index[] = $i;
78 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
79 $field_unique[] = $i;
81 } // end if
82 } // end for
84 // Builds the fields creation statements
85 for ($i = 0; $i < $field_cnt; $i++) {
86 // '0' is also empty for php :-(
87 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
88 continue;
91 $query = PMA_Table::generateFieldSpec(
92 $_REQUEST['field_name'][$i],
93 $_REQUEST['field_type'][$i],
94 $_REQUEST['field_length'][$i],
95 $_REQUEST['field_attribute'][$i],
96 isset($_REQUEST['field_collation'][$i])
97 ? $_REQUEST['field_collation'][$i]
98 : '',
99 isset($_REQUEST['field_null'][$i])
100 ? $_REQUEST['field_null'][$i]
101 : 'NOT NULL',
102 $_REQUEST['field_default_type'][$i],
103 $_REQUEST['field_default_value'][$i],
104 isset($_REQUEST['field_extra'][$i])
105 ? $_REQUEST['field_extra'][$i]
106 : false,
107 isset($_REQUEST['field_comments'][$i])
108 ? $_REQUEST['field_comments'][$i]
109 : '',
110 $field_primary,
114 $query .= ', ';
115 $sql_query .= $query;
116 } // end for
117 unset($field_cnt, $query);
118 $sql_query = preg_replace('@, $@', '', $sql_query);
120 // Builds the primary keys statements
121 $primary = '';
122 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
123 for ($i = 0; $i < $primary_cnt; $i++) {
124 $j = $field_primary[$i];
125 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
126 $primary .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
128 } // end for
129 unset($primary_cnt);
130 $primary = preg_replace('@, $@', '', $primary);
131 if (strlen($primary)) {
132 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
134 unset($primary);
136 // Builds the indexes statements
137 $index = '';
138 $index_cnt = (isset($field_index) ? count($field_index) : 0);
139 for ($i = 0;$i < $index_cnt; $i++) {
140 $j = $field_index[$i];
141 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
142 $index .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
144 } // end for
145 unset($index_cnt);
146 $index = preg_replace('@, $@', '', $index);
147 if (strlen($index)) {
148 $sql_query .= ', INDEX (' . $index . ')';
150 unset($index);
152 // Builds the uniques statements
153 $unique = '';
154 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
155 for ($i = 0; $i < $unique_cnt; $i++) {
156 $j = $field_unique[$i];
157 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
158 $unique .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
160 } // end for
161 unset($unique_cnt);
162 $unique = preg_replace('@, $@', '', $unique);
163 if (strlen($unique)) {
164 $sql_query .= ', UNIQUE (' . $unique . ')';
166 unset($unique);
168 // Builds the FULLTEXT statements
169 $fulltext = '';
170 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
171 for ($i = 0; $i < $fulltext_cnt; $i++) {
172 $j = $field_fulltext[$i];
173 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
174 $fulltext .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
176 } // end for
178 $fulltext = preg_replace('@, $@', '', $fulltext);
179 if (strlen($fulltext)) {
180 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
182 unset($fulltext);
184 // Builds the 'create table' statement
185 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
186 . ' (' . $sql_query . ')';
188 // Adds table type, character set, comments and partition definition
189 if (!empty($_REQUEST['tbl_storage_engine']) && ($_REQUEST['tbl_storage_engine'] != 'Default')) {
190 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine'];
192 if (!empty($_REQUEST['tbl_collation'])) {
193 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
195 if (!empty($_REQUEST['comment'])) {
196 $sql_query .= ' COMMENT = \'' . PMA_sqlAddSlashes($_REQUEST['comment']) . '\'';
198 if (!empty($_REQUEST['partition_definition'])) {
199 $sql_query .= ' ' . PMA_sqlAddSlashes($_REQUEST['partition_definition']);
201 $sql_query .= ';';
203 // Executes the query
204 $result = PMA_DBI_try_query($sql_query);
206 if ($result) {
208 // If comments were sent, enable relation stuff
209 include_once 'libraries/transformations.lib.php';
211 // Update comment table for mime types [MIME]
212 if (isset($_REQUEST['field_mimetype'])
213 && is_array($_REQUEST['field_mimetype'])
214 && $cfg['BrowseMIME']
216 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
217 if (isset($_REQUEST['field_name'][$fieldindex])
218 && strlen($_REQUEST['field_name'][$fieldindex])
220 PMA_setMIME(
221 $db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
222 $_REQUEST['field_transformation'][$fieldindex],
223 $_REQUEST['field_transformation_options'][$fieldindex]
229 $message = PMA_Message::success(__('Table %1$s has been created.'));
230 $message->addParam(PMA_backquote($db) . '.' . PMA_backquote($table));
232 if ($GLOBALS['is_ajax_request'] == true) {
235 * construct the html for the newly created table's row to be appended
236 * to the list of tables.
238 * Logic taken from db_structure.php
241 $tbl_url_params = array();
242 $tbl_url_params['db'] = $db;
243 $tbl_url_params['table'] = $table;
244 $is_show_stats = $cfg['ShowStats'];
246 $tbl_stats_result = PMA_DBI_query(
247 'SHOW TABLE STATUS FROM ' . PMA_backquote($db)
248 . ' LIKE \'' . PMA_sqlAddSlashes($table, true) . '\';'
250 $tbl_stats = PMA_DBI_fetch_assoc($tbl_stats_result);
251 PMA_DBI_free_result($tbl_stats_result);
252 unset($tbl_stats_result);
254 if ($is_show_stats) {
255 $sum_size = (double) 0;
256 $overhead_size = (double) 0;
257 $overhead_check = '';
259 $tblsize = doubleval($tbl_stats['Data_length']) + doubleval($tbl_stats['Index_length']);
260 $sum_size += $tblsize;
261 list($formatted_size, $unit) = PMA_formatByteDown($tblsize, 3, ($tblsize > 0) ? 1 : 0);
262 if (isset($tbl_stats['Data_free']) && $tbl_stats['Data_free'] > 0) {
263 list($formatted_overhead, $overhead_unit) = PMA_formatByteDown($tbl_stats['Data_free'], 3, ($tbl_stats['Data_free'] > 0) ? 1 : 0);
264 $overhead_size += $tbl_stats['Data_free'];
267 if (isset($formatted_overhead)) {
268 $overhead = '<span>' . $formatted_overhead . '</span> <span class="unit">' . $overhead_unit . '</span>';
269 unset($formatted_overhead);
270 } else {
271 $overhead = '-';
275 $new_table_string = '<tr>' . "\n";
276 $new_table_string .= '<td class="center"> <input type="checkbox" id="checkbox_tbl_" name="selected_tbl[]" value="'.htmlspecialchars($table).'" /> </td>' . "\n";
278 $new_table_string .= '<th>';
279 $new_table_string .= '<a href="sql.php' . PMA_generate_common_url($tbl_url_params) . '">'. $table . '</a>';
281 if (PMA_Tracker::isActive()) {
282 $truename = str_replace(' ', '&nbsp;', htmlspecialchars($table));
283 if (PMA_Tracker::isTracked($db, $truename)) {
284 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '">';
285 $new_table_string .= PMA_getImage('eye.png', __('Tracking is active.'));
286 } elseif (PMA_Tracker::getVersion($db, $truename) > 0) {
287 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '">';
288 $new_table_string .= PMA_getImage('eye_grey.png', __('Tracking is not active.'));
290 unset($truename);
292 $new_table_string .= '</th>' . "\n";
294 $new_table_string .= '<td>' . $titles['NoBrowse'] . '</td>' . "\n";
296 $new_table_string .= '<td><a href="tbl_structure.php' . PMA_generate_common_url($tbl_url_params) . '">' . $titles['Structure'] . '</a></td>' . "\n";
298 $new_table_string .= '<td>' . $titles['NoSearch'] . '</td>' . "\n";
300 $new_table_string .= '<td><a href="tbl_change.php' . PMA_generate_common_url($tbl_url_params) . '">' . $titles['Insert'] . '</a></td>' . "\n";
302 $new_table_string .= '<td>' . $titles['NoEmpty'] . '</td>' . "\n";
304 $new_table_string .= '<td><a class="drop_table_anchor" href="sql.php' . PMA_generate_common_url($tbl_url_params) . '&amp;sql_query=';
305 $new_table_string .= urlencode('DROP TABLE ' . PMA_backquote($table));
306 $new_table_string .= '">';
307 $new_table_string .= $titles['Drop'];
308 $new_table_string .= '</a></td>' . "\n";
310 $new_table_string .= '<td class="value">' . $tbl_stats['Rows'] . '</td>' . "\n";
312 $new_table_string .= '<td class="nowrap">' . $tbl_stats['Engine'] . '</td>' . "\n";
314 $new_table_string .= '<td> <dfn title="' . PMA_getCollationDescr($tbl_stats['Collation']) . '">'. $tbl_stats['Collation'] .'</dfn></td>' . "\n";
316 if ($is_show_stats) {
317 $new_table_string .= '<td class="value tbl_size"> <a href="tbl_structure.php' . PMA_generate_common_url($tbl_url_params) . '#showusage" ><span>' . $formatted_size . '</span> <span class="unit">' . $unit . '</class></a> </td>' . "\n" ;
318 $new_table_string .= '<td class="value tbl_overhead">' . $overhead . '</td>' . "\n" ;
321 $new_table_string .= '</tr>' . "\n";
323 $extra_data['new_table_string'] = $new_table_string;
325 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
328 $display_query = $sql_query;
329 $sql_query = '';
331 // read table info on this newly created table, in case
332 // the next page is Structure
333 $reread_info = true;
334 include 'libraries/tbl_info.inc.php';
336 // do not switch to sql.php - as there is no row to be displayed on a new table
337 if ($cfg['DefaultTabTable'] === 'sql.php') {
338 include 'tbl_structure.php';
339 } else {
340 include '' . $cfg['DefaultTabTable'];
342 exit;
343 } else {
344 if ($GLOBALS['is_ajax_request'] == true) {
345 PMA_ajaxResponse(PMA_DBI_getError(), false);
346 } else {
347 PMA_mysqlDie('', '', '', $err_url, false);
348 // An error happened while inserting/updating a table definition.
349 // to prevent total loss of that data, we embed the form once again.
350 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
351 $num_fields = $_REQUEST['orig_num_fields'];
352 $regenerate = true;
355 } // end do create table
358 * Displays the form used to define the structure of the table
361 // This div is used to show the content(eg: create table form with more columns) fetched with AJAX subsequently.
362 if ($GLOBALS['is_ajax_request'] != true) {
363 echo('<div id="create_table_div">');
366 require 'libraries/tbl_properties.inc.php';
367 // Displays the footer
368 require 'libraries/footer.inc.php';
370 if ($GLOBALS['is_ajax_request'] != true) {
371 echo('</div>');