1. Check existence of mb_string, mysql and xml extensions before installation.
[openemr.git] / phpmyadmin / libraries / create_addfield.lib.php
blob89598e1c2d96ebc8a269cf8dd15c7caa16d92c93
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 if (! defined('PHPMYADMIN')) {
10 exit;
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 PMA_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 . PMA_Util::backquote($_REQUEST['after_field']);
121 } else {
122 $sql_suffix .= ' AFTER '
123 . PMA_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 .= ' ' . PMA_Util::backquote($index['Key_name']);
157 $index_fields = array();
158 foreach ($index['columns'] as $key => $column) {
159 $index_fields[$key] = PMA_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 . PMA_Util::sqlAddSlashes($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, PMA_Index::getIndexTypes())
181 $sql_query .= ' USING ' . $type;
184 $parser = $index['Parser'];
185 if ($index['Index_choice'] == 'FULLTEXT' && ! empty($parser)) {
186 $sql_query .= " WITH PARSER " . PMA_Util::sqlAddSlashes($parser);
189 $comment = $index['Index_comment'];
190 if (! empty($comment)) {
191 $sql_query .= " COMMENT '" . PMA_Util::sqlAddSlashes($comment) . "'";
194 $statement[] = $sql_query;
196 return $statement;
200 * Statement prefix for the PMA_buildColumnCreationStatement()
202 * @param boolean $is_create_tbl true if requirement is to get the statement
203 * for table creation
205 * @return string $sql_prefix prefix
207 function PMA_getStatementPrefix($is_create_tbl = true)
209 $sql_prefix = " ";
210 if (! $is_create_tbl) {
211 $sql_prefix = ' ADD ';
213 return $sql_prefix;
217 * Merge index definitions for one type of index
219 * @param array $definitions the index definitions to merge to
220 * @param boolean $is_create_tbl true if requirement is to get the statement
221 * for table creation
222 * @param array $indexed_columns the columns for one type of index
223 * @param string $index_keyword the index keyword to use in the definition
225 * @return array $index_definitions
227 function PMA_mergeIndexStatements(
228 $definitions, $is_create_tbl, $indexed_columns, $index_keyword
230 foreach ($indexed_columns as $index) {
231 $statements = PMA_buildIndexStatements(
232 $index, " " . $index_keyword . " ", $is_create_tbl
234 $definitions = array_merge($definitions, $statements);
236 return $definitions;
240 * Returns sql statement according to the column and index specifications as
241 * requested
243 * @param boolean $is_create_tbl true if requirement is to get the statement
244 * for table creation
246 * @return string sql statement
248 function PMA_getColumnCreationStatements($is_create_tbl = true)
250 $sql_statement = "";
251 list($field_cnt, $field_primary, $field_index,
252 $field_unique, $field_fulltext, $field_spatial
253 ) = PMA_getIndexedColumns();
254 $definitions = PMA_buildColumnCreationStatement(
255 $field_cnt, $is_create_tbl
258 // Builds the PRIMARY KEY statements
259 $primary_key_statements = PMA_buildIndexStatements(
260 isset($field_primary[0]) ? $field_primary[0] : array(),
261 " PRIMARY KEY ",
262 $is_create_tbl
264 $definitions = array_merge($definitions, $primary_key_statements);
266 // Builds the INDEX statements
267 $definitions = PMA_mergeIndexStatements(
268 $definitions, $is_create_tbl, $field_index, "INDEX"
271 // Builds the UNIQUE statements
272 $definitions = PMA_mergeIndexStatements(
273 $definitions, $is_create_tbl, $field_unique, "UNIQUE"
276 // Builds the FULLTEXT statements
277 $definitions = PMA_mergeIndexStatements(
278 $definitions, $is_create_tbl, $field_fulltext, "FULLTEXT"
281 // Builds the SPATIAL statements
282 $definitions = PMA_mergeIndexStatements(
283 $definitions, $is_create_tbl, $field_spatial, "SPATIAL"
286 if (count($definitions)) {
287 $sql_statement = implode(', ', $definitions);
289 $sql_statement = preg_replace('@, $@', '', $sql_statement);
291 return $sql_statement;
296 * Function to get table creation sql query
298 * @param string $db database name
299 * @param string $table table name
301 * @return string
303 function PMA_getTableCreationQuery($db, $table)
305 // get column addition statements
306 $sql_statement = PMA_getColumnCreationStatements(true);
308 // Builds the 'create table' statement
309 $sql_query = 'CREATE TABLE ' . PMA_Util::backquote($db) . '.'
310 . PMA_Util::backquote(trim($table)) . ' (' . $sql_statement . ')';
312 // Adds table type, character set, comments and partition definition
313 if (!empty($_REQUEST['tbl_storage_engine'])
314 && ($_REQUEST['tbl_storage_engine'] != 'Default')
316 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine'];
318 if (!empty($_REQUEST['tbl_collation'])) {
319 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
321 if (! empty($_REQUEST['connection'])
322 && ! empty($_REQUEST['tbl_storage_engine'])
323 && $_REQUEST['tbl_storage_engine'] == 'FEDERATED'
325 $sql_query .= " CONNECTION = '"
326 . PMA_Util::sqlAddSlashes($_REQUEST['connection']) . "'";
328 if (!empty($_REQUEST['comment'])) {
329 $sql_query .= ' COMMENT = \''
330 . PMA_Util::sqlAddSlashes($_REQUEST['comment']) . '\'';
332 if (!empty($_REQUEST['partition_definition'])) {
333 $sql_query .= ' ' . PMA_Util::sqlAddSlashes(
334 $_REQUEST['partition_definition']
337 $sql_query .= ';';
339 return $sql_query;
343 * Function to get the number of fields for the table creation form
345 * @return int
347 function PMA_getNumberOfFieldsFromRequest()
349 if (isset($_REQUEST['submit_num_fields'])) {
350 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
351 } elseif (isset($_REQUEST['num_fields'])
352 && intval($_REQUEST['num_fields']) > 0
354 $num_fields = (int) $_REQUEST['num_fields'];
355 } else {
356 $num_fields = 4;
359 return $num_fields;
363 * Function to execute the column creation statement
365 * @param string $db current database
366 * @param string $table current table
367 * @param string $err_url error page url
369 * @return array
371 function PMA_tryColumnCreationQuery($db, $table, $err_url)
373 // get column addition statements
374 $sql_statement = PMA_getColumnCreationStatements(false);
376 // To allow replication, we first select the db to use and then run queries
377 // on this db.
378 $GLOBALS['dbi']->selectDb($db)
379 or PMA_Util::mysqlDie(
380 $GLOBALS['dbi']->getError(),
381 'USE ' . PMA_Util::backquote($db), false,
382 $err_url
384 $sql_query = 'ALTER TABLE ' .
385 PMA_Util::backquote($table) . ' ' . $sql_statement . ';';
386 // If there is a request for SQL previewing.
387 if (isset($_REQUEST['preview_sql'])) {
388 PMA_previewSQL($sql_query);
390 return array($GLOBALS['dbi']->tryQuery($sql_query) , $sql_query);