Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / create_addfield.lib.php
blob2c43ef3878a3c7de6abc4433a660b8865e66af93
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * set of functions for tbl_create.php and tbl_addfield.php
6 * @package PhpMyAdmin
7 */
9 use PhpMyAdmin\Core;
10 use PhpMyAdmin\Table;
11 use PhpMyAdmin\Util;
13 /**
14 * Transforms the radio button field_key into 4 arrays
16 * @return array An array of arrays which represents column keys for each index type
18 function PMA_getIndexedColumns()
20 $field_cnt = count($_REQUEST['field_name']);
21 $field_primary = json_decode($_REQUEST['primary_indexes'], true);
22 $field_index = json_decode($_REQUEST['indexes'], true);
23 $field_unique = json_decode($_REQUEST['unique_indexes'], true);
24 $field_fulltext = json_decode($_REQUEST['fulltext_indexes'], true);
25 $field_spatial = json_decode($_REQUEST['spatial_indexes'], true);
27 return array(
28 $field_cnt, $field_primary, $field_index, $field_unique,
29 $field_fulltext, $field_spatial
33 /**
34 * Initiate the column creation statement according to the table creation or
35 * add columns to a existing table
37 * @param int $field_cnt number of columns
38 * @param boolean $is_create_tbl true if requirement is to get the statement
39 * for table creation
41 * @return array $definitions An array of initial sql statements
42 * according to the request
44 function PMA_buildColumnCreationStatement(
45 $field_cnt, $is_create_tbl = true
46 ) {
47 $definitions = array();
48 for ($i = 0; $i < $field_cnt; ++$i) {
49 // '0' is also empty for php :-(
50 if (empty($_REQUEST['field_name'][$i])
51 && $_REQUEST['field_name'][$i] != '0'
52 ) {
53 continue;
56 $definition = PMA_getStatementPrefix($is_create_tbl) .
57 Table::generateFieldSpec(
58 trim($_REQUEST['field_name'][$i]),
59 $_REQUEST['field_type'][$i],
60 $_REQUEST['field_length'][$i],
61 $_REQUEST['field_attribute'][$i],
62 isset($_REQUEST['field_collation'][$i])
63 ? $_REQUEST['field_collation'][$i]
64 : '',
65 isset($_REQUEST['field_null'][$i])
66 ? $_REQUEST['field_null'][$i]
67 : 'NOT NULL',
68 $_REQUEST['field_default_type'][$i],
69 $_REQUEST['field_default_value'][$i],
70 isset($_REQUEST['field_extra'][$i])
71 ? $_REQUEST['field_extra'][$i]
72 : false,
73 isset($_REQUEST['field_comments'][$i])
74 ? $_REQUEST['field_comments'][$i]
75 : '',
76 isset($_REQUEST['field_virtuality'][$i])
77 ? $_REQUEST['field_virtuality'][$i]
78 : '',
79 isset($_REQUEST['field_expression'][$i])
80 ? $_REQUEST['field_expression'][$i]
81 : ''
84 $definition .= PMA_setColumnCreationStatementSuffix($i, $is_create_tbl);
85 $definitions[] = $definition;
86 } // end for
88 return $definitions;
91 /**
92 * Set column creation suffix according to requested position of the new column
94 * @param int $current_field_num current column number
95 * @param boolean $is_create_tbl true if requirement is to get the statement
96 * for table creation
98 * @return string $sql_suffix suffix
100 function PMA_setColumnCreationStatementSuffix($current_field_num,
101 $is_create_tbl = true
103 // no suffix is needed if request is a table creation
104 $sql_suffix = ' ';
105 if ($is_create_tbl) {
106 return $sql_suffix;
109 if ((string) $_REQUEST['field_where'] === 'last') {
110 return $sql_suffix;
113 // Only the first field can be added somewhere other than at the end
114 if ((int) $current_field_num === 0) {
115 if ((string) $_REQUEST['field_where'] === 'first') {
116 $sql_suffix .= ' FIRST';
117 } else {
118 $sql_suffix .= ' AFTER '
119 . PhpMyAdmin\Util::backquote($_REQUEST['after_field']);
121 } else {
122 $sql_suffix .= ' AFTER '
123 . PhpMyAdmin\Util::backquote(
124 $_REQUEST['field_name'][$current_field_num - 1]
128 return $sql_suffix;
132 * Create relevant index statements
134 * @param array $index an array of index columns
135 * @param string $index_choice index choice that which represents
136 * the index type of $indexed_fields
137 * @param boolean $is_create_tbl true if requirement is to get the statement
138 * for table creation
140 * @return array an array of sql statements for indexes
142 function PMA_buildIndexStatements($index, $index_choice,
143 $is_create_tbl = true
145 $statement = array();
146 if (!count($index)) {
147 return $statement;
150 $sql_query = PMA_getStatementPrefix($is_create_tbl)
151 . ' ' . $index_choice;
153 if (! empty($index['Key_name']) && $index['Key_name'] != 'PRIMARY') {
154 $sql_query .= ' ' . PhpMyAdmin\Util::backquote($index['Key_name']);
157 $index_fields = array();
158 foreach ($index['columns'] as $key => $column) {
159 $index_fields[$key] = PhpMyAdmin\Util::backquote(
160 $_REQUEST['field_name'][$column['col_index']]
162 if ($column['size']) {
163 $index_fields[$key] .= '(' . $column['size'] . ')';
165 } // end while
167 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
169 $keyBlockSizes = $index['Key_block_size'];
170 if (! empty($keyBlockSizes)) {
171 $sql_query .= " KEY_BLOCK_SIZE = "
172 . $GLOBALS['dbi']->escapeString($keyBlockSizes);
175 // specifying index type is allowed only for primary, unique and index only
176 $type = $index['Index_type'];
177 if ($index['Index_choice'] != 'SPATIAL'
178 && $index['Index_choice'] != 'FULLTEXT'
179 && in_array($type, PhpMyAdmin\Index::getIndexTypes())
181 $sql_query .= ' USING ' . $type;
184 $parser = $index['Parser'];
185 if ($index['Index_choice'] == 'FULLTEXT' && ! empty($parser)) {
186 $sql_query .= " WITH PARSER " . $GLOBALS['dbi']->escapeString($parser);
189 $comment = $index['Index_comment'];
190 if (! empty($comment)) {
191 $sql_query .= " COMMENT '" . $GLOBALS['dbi']->escapeString($comment)
192 . "'";
195 $statement[] = $sql_query;
197 return $statement;
201 * Statement prefix for the PMA_buildColumnCreationStatement()
203 * @param boolean $is_create_tbl true if requirement is to get the statement
204 * for table creation
206 * @return string $sql_prefix prefix
208 function PMA_getStatementPrefix($is_create_tbl = true)
210 $sql_prefix = " ";
211 if (! $is_create_tbl) {
212 $sql_prefix = ' ADD ';
214 return $sql_prefix;
218 * Merge index definitions for one type of index
220 * @param array $definitions the index definitions to merge to
221 * @param boolean $is_create_tbl true if requirement is to get the statement
222 * for table creation
223 * @param array $indexed_columns the columns for one type of index
224 * @param string $index_keyword the index keyword to use in the definition
226 * @return array $index_definitions
228 function PMA_mergeIndexStatements(
229 $definitions, $is_create_tbl, $indexed_columns, $index_keyword
231 foreach ($indexed_columns as $index) {
232 $statements = PMA_buildIndexStatements(
233 $index, " " . $index_keyword . " ", $is_create_tbl
235 $definitions = array_merge($definitions, $statements);
237 return $definitions;
241 * Returns sql statement according to the column and index specifications as
242 * requested
244 * @param boolean $is_create_tbl true if requirement is to get the statement
245 * for table creation
247 * @return string sql statement
249 function PMA_getColumnCreationStatements($is_create_tbl = true)
251 $sql_statement = "";
252 list($field_cnt, $field_primary, $field_index,
253 $field_unique, $field_fulltext, $field_spatial
254 ) = PMA_getIndexedColumns();
255 $definitions = PMA_buildColumnCreationStatement(
256 $field_cnt, $is_create_tbl
259 // Builds the PRIMARY KEY statements
260 $primary_key_statements = PMA_buildIndexStatements(
261 isset($field_primary[0]) ? $field_primary[0] : array(),
262 " PRIMARY KEY ",
263 $is_create_tbl
265 $definitions = array_merge($definitions, $primary_key_statements);
267 // Builds the INDEX statements
268 $definitions = PMA_mergeIndexStatements(
269 $definitions, $is_create_tbl, $field_index, "INDEX"
272 // Builds the UNIQUE statements
273 $definitions = PMA_mergeIndexStatements(
274 $definitions, $is_create_tbl, $field_unique, "UNIQUE"
277 // Builds the FULLTEXT statements
278 $definitions = PMA_mergeIndexStatements(
279 $definitions, $is_create_tbl, $field_fulltext, "FULLTEXT"
282 // Builds the SPATIAL statements
283 $definitions = PMA_mergeIndexStatements(
284 $definitions, $is_create_tbl, $field_spatial, "SPATIAL"
287 if (count($definitions)) {
288 $sql_statement = implode(', ', $definitions);
290 $sql_statement = preg_replace('@, $@', '', $sql_statement);
292 return $sql_statement;
297 * Returns the partitioning clause
299 * @return string partitioning clause
301 function PMA_getPartitionsDefinition()
303 $sql_query = "";
304 if (! empty($_REQUEST['partition_by'])
305 && ! empty($_REQUEST['partition_expr'])
306 && ! empty($_REQUEST['partition_count'])
307 && $_REQUEST['partition_count'] > 1
309 $sql_query .= " PARTITION BY " . $_REQUEST['partition_by']
310 . " (" . $_REQUEST['partition_expr'] . ")"
311 . " PARTITIONS " . $_REQUEST['partition_count'];
314 if (! empty($_REQUEST['subpartition_by'])
315 && ! empty($_REQUEST['subpartition_expr'])
316 && ! empty($_REQUEST['subpartition_count'])
317 && $_REQUEST['subpartition_count'] > 1
319 $sql_query .= " SUBPARTITION BY " . $_REQUEST['subpartition_by']
320 . " (" . $_REQUEST['subpartition_expr'] . ")"
321 . " SUBPARTITIONS " . $_REQUEST['subpartition_count'];
324 if (! empty($_REQUEST['partitions'])) {
325 $i = 0;
326 $partitions = array();
327 foreach ($_REQUEST['partitions'] as $partition) {
328 $partitions[] = PMA_getPartitionDefinition($partition);
329 $i++;
331 $sql_query .= " (" . implode(", ", $partitions) . ")";
334 return $sql_query;
338 * Returns the definition of a partition/subpartition
340 * @param array $partition array of partition/subpartition detiails
341 * @param boolean $isSubPartition whether a subpartition
343 * @return string partition/subpartition definition
345 function PMA_getPartitionDefinition($partition, $isSubPartition = false)
347 $sql_query = " " . ($isSubPartition ? "SUB" : "") . "PARTITION ";
348 $sql_query .= $partition['name'];
350 if (! empty($partition['value_type'])) {
351 $sql_query .= " VALUES " . $partition['value_type'];
353 if ($partition['value_type'] != 'LESS THAN MAXVALUE') {
354 $sql_query .= " (" . $partition['value'] . ")";
358 if (! empty($partition['engine'])) {
359 $sql_query .= " ENGINE = " . $partition['engine'];
361 if (! empty($partition['comment'])) {
362 $sql_query .= " COMMENT = '" . $partition['comment'] . "'";
364 if (! empty($partition['data_directory'])) {
365 $sql_query .= " DATA DIRECTORY = '" . $partition['data_directory'] . "'";
367 if (! empty($partition['index_directory'])) {
368 $sql_query .= " INDEX_DIRECTORY = '" . $partition['index_directory'] . "'";
370 if (! empty($partition['max_rows'])) {
371 $sql_query .= " MAX_ROWS = " . $partition['max_rows'];
373 if (! empty($partition['min_rows'])) {
374 $sql_query .= " MIN_ROWS = " . $partition['min_rows'];
376 if (! empty($partition['tablespace'])) {
377 $sql_query .= " TABLESPACE = " . $partition['tablespace'];
379 if (! empty($partition['node_group'])) {
380 $sql_query .= " NODEGROUP = " . $partition['node_group'];
383 if (! empty($partition['subpartitions'])) {
384 $j = 0;
385 $subpartitions = array();
386 foreach ($partition['subpartitions'] as $subpartition) {
387 $subpartitions[] = PMA_getPartitionDefinition(
388 $subpartition,
389 true
391 $j++;
393 $sql_query .= " (" . implode(", ", $subpartitions) . ")";
396 return $sql_query;
400 * Function to get table creation sql query
402 * @param string $db database name
403 * @param string $table table name
405 * @return string
407 function PMA_getTableCreationQuery($db, $table)
409 // get column addition statements
410 $sql_statement = PMA_getColumnCreationStatements(true);
412 // Builds the 'create table' statement
413 $sql_query = 'CREATE TABLE ' . PhpMyAdmin\Util::backquote($db) . '.'
414 . PhpMyAdmin\Util::backquote(trim($table)) . ' (' . $sql_statement . ')';
416 // Adds table type, character set, comments and partition definition
417 if (!empty($_REQUEST['tbl_storage_engine'])
418 && ($_REQUEST['tbl_storage_engine'] != 'Default')
420 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine'];
422 if (!empty($_REQUEST['tbl_collation'])) {
423 $sql_query .= Util::getCharsetQueryPart($_REQUEST['tbl_collation']);
425 if (! empty($_REQUEST['connection'])
426 && ! empty($_REQUEST['tbl_storage_engine'])
427 && $_REQUEST['tbl_storage_engine'] == 'FEDERATED'
429 $sql_query .= " CONNECTION = '"
430 . $GLOBALS['dbi']->escapeString($_REQUEST['connection']) . "'";
432 if (!empty($_REQUEST['comment'])) {
433 $sql_query .= ' COMMENT = \''
434 . $GLOBALS['dbi']->escapeString($_REQUEST['comment']) . '\'';
436 $sql_query .= PMA_getPartitionsDefinition();
437 $sql_query .= ';';
439 return $sql_query;
443 * Function to get the number of fields for the table creation form
445 * @return int
447 function PMA_getNumberOfFieldsFromRequest()
449 if (isset($_REQUEST['submit_num_fields'])) { // adding new fields
450 $num_fields = intval($_REQUEST['orig_num_fields']) + intval($_REQUEST['added_fields']);
451 } elseif (isset($_REQUEST['orig_num_fields'])) { // retaining existing fields
452 $num_fields = intval($_REQUEST['orig_num_fields']);
453 } elseif (isset($_REQUEST['num_fields'])
454 && intval($_REQUEST['num_fields']) > 0
455 ) { // new table with specified number of fields
456 $num_fields = intval($_REQUEST['num_fields']);
457 } else { // new table with unspecified number of fields
458 $num_fields = 4;
461 // Limit to 4096 fields (MySQL maximal value)
462 return min($num_fields, 4096);
466 * Function to execute the column creation statement
468 * @param string $db current database
469 * @param string $table current table
470 * @param string $err_url error page url
472 * @return array
474 function PMA_tryColumnCreationQuery($db, $table, $err_url)
476 // get column addition statements
477 $sql_statement = PMA_getColumnCreationStatements(false);
479 // To allow replication, we first select the db to use and then run queries
480 // on this db.
481 if (!($GLOBALS['dbi']->selectDb($db))) {
482 PhpMyAdmin\Util::mysqlDie(
483 $GLOBALS['dbi']->getError(),
484 'USE ' . PhpMyAdmin\Util::backquote($db), false,
485 $err_url
488 $sql_query = 'ALTER TABLE ' .
489 PhpMyAdmin\Util::backquote($table) . ' ' . $sql_statement . ';';
490 // If there is a request for SQL previewing.
491 if (isset($_REQUEST['preview_sql'])) {
492 Core::previewSQL($sql_query);
494 return array($GLOBALS['dbi']->tryQuery($sql_query) , $sql_query);