2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * set of functions for tbl_create.php and tbl_addfield.php
9 if (! defined('PHPMYADMIN')) {
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 = array();
22 $field_index = array();
23 $field_unique = array();
24 $field_fulltext = array();
25 for ($i = 0; $i < $field_cnt; ++
$i) {
26 if (isset($_REQUEST['field_key'][$i])
27 && strlen($_REQUEST['field_name'][$i])
29 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
30 $field_primary[] = $i;
32 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
35 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
38 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
39 $field_fulltext[] = $i;
45 $field_cnt, $field_primary, $field_index, $field_unique,
51 * Initiate the column creation statement according to the table creation or
52 * add columns to a existing table
54 * @param int $field_cnt number of columns
55 * @param int &$field_primary primary index field
56 * @param boolean $is_create_tbl true if requirement is to get the statement
59 * @return array $definitions An array of initial sql statements
60 * according to the request
62 function PMA_buildColumnCreationStatement(
63 $field_cnt, &$field_primary, $is_create_tbl = true
65 $definitions = array();
66 for ($i = 0; $i < $field_cnt; ++
$i) {
67 // '0' is also empty for php :-(
68 if (empty($_REQUEST['field_name'][$i])
69 && $_REQUEST['field_name'][$i] != '0'
74 $definition = PMA_getStatementPrefix($is_create_tbl) .
75 PMA_Table
::generateFieldSpec(
76 $_REQUEST['field_name'][$i],
77 $_REQUEST['field_type'][$i],
79 $_REQUEST['field_length'][$i],
80 $_REQUEST['field_attribute'][$i],
81 isset($_REQUEST['field_collation'][$i])
82 ?
$_REQUEST['field_collation'][$i]
84 isset($_REQUEST['field_null'][$i])
85 ?
$_REQUEST['field_null'][$i]
87 $_REQUEST['field_default_type'][$i],
88 $_REQUEST['field_default_value'][$i],
89 isset($_REQUEST['field_extra'][$i])
90 ?
$_REQUEST['field_extra'][$i]
92 isset($_REQUEST['field_comments'][$i])
93 ?
$_REQUEST['field_comments'][$i]
99 $definition .= PMA_setColumnCreationStatementSuffix($i, $is_create_tbl);
100 $definitions[] = $definition;
107 * Set column creation suffix according to requested position of the new column
109 * @param int $current_field_num current column number
110 * @param boolean $is_create_tbl true if requirement is to get the statement
113 * @return string $sql_suffix suffix
115 function PMA_setColumnCreationStatementSuffix($current_field_num,
116 $is_create_tbl = true
118 // no suffix is needed if request is a table creation
120 if ($is_create_tbl) {
124 if ($_REQUEST['field_where'] == 'last') {
128 // Only the first field can be added somewhere other than at the end
129 if ($current_field_num == 0) {
130 if ($_REQUEST['field_where'] == 'first') {
131 $sql_suffix .= ' FIRST';
133 $sql_suffix .= ' AFTER '
134 . PMA_Util
::backquote($_REQUEST['after_field']);
137 $sql_suffix .= ' AFTER '
138 . PMA_Util
::backquote(
139 $_REQUEST['field_name'][$current_field_num - 1]
147 * Create relevant index statements
149 * @param array $indexed_fields an array of index columns
150 * @param string $index_type index type that which represents
151 * the index type of $indexed_fields
152 * @param boolean $is_create_tbl true if requirement is to get the statement
155 * @return array an array of sql statements for indexes
157 function PMA_buildIndexStatements($indexed_fields, $index_type,
158 $is_create_tbl = true
160 $statement = array();
161 if (!count($indexed_fields)) {
166 foreach ($indexed_fields as $field_nr) {
167 $fields[] = PMA_Util
::backquote($_REQUEST['field_name'][$field_nr]);
169 $statement[] = PMA_getStatementPrefix($is_create_tbl)
170 . ' ' . $index_type . ' (' . implode(', ', $fields) . ') ';
177 * Statement prefix for the PMA_buildColumnCreationStatement()
179 * @param boolean $is_create_tbl true if requirement is to get the statement
182 * @return string $sql_prefix prefix
184 function PMA_getStatementPrefix($is_create_tbl = true)
187 if (! $is_create_tbl) {
188 $sql_prefix = ' ADD ';
194 * Returns sql statement according to the column and index specifications as
197 * @param boolean $is_create_tbl true if requirement is to get the statement
200 * @return string sql statement
202 function PMA_getColumnCreationStatements($is_create_tbl = true)
204 $definitions = array();
206 list($field_cnt, $field_primary, $field_index,
207 $field_unique, $field_fulltext
208 ) = PMA_getIndexedColumns();
209 $definitions = PMA_buildColumnCreationStatement(
210 $field_cnt, $field_primary, $is_create_tbl
213 // Builds the primary keys statements
214 $primary_key_statements = PMA_buildIndexStatements(
215 $field_primary, " PRIMARY KEY ", $is_create_tbl
217 $definitions = array_merge($definitions, $primary_key_statements);
219 // Builds the indexes statements
220 $index_statements = PMA_buildIndexStatements(
221 $field_index, " INDEX ", $is_create_tbl
223 $definitions = array_merge($definitions, $index_statements);
225 // Builds the uniques statements
226 $unique_statements = PMA_buildIndexStatements(
227 $field_unique, " UNIQUE ", $is_create_tbl
229 $definitions = array_merge($definitions, $unique_statements);
231 // Builds the fulltext statements
232 $fulltext_statements = PMA_buildIndexStatements(
233 $field_fulltext, " FULLTEXT ", $is_create_tbl
235 $definitions = array_merge($definitions, $fulltext_statements);
237 if (count($definitions)) {
238 $sql_statement = implode(', ', $definitions);
240 $sql_statement = preg_replace('@, $@', '', $sql_statement);
242 return $sql_statement;
247 * Function to get table creation sql query
249 * @param string $db database name
250 * @param string $table table name
254 function PMA_getTableCreationQuery($db, $table)
256 // get column addition statements
257 $sql_statement = PMA_getColumnCreationStatements(true);
259 // Builds the 'create table' statement
260 $sql_query = 'CREATE TABLE ' . PMA_Util
::backquote($db) . '.'
261 . PMA_Util
::backquote($table) . ' (' . $sql_statement . ')';
263 // Adds table type, character set, comments and partition definition
264 if (!empty($_REQUEST['tbl_storage_engine'])
265 && ($_REQUEST['tbl_storage_engine'] != 'Default')
267 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine'];
269 if (!empty($_REQUEST['tbl_collation'])) {
270 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
272 if (!empty($_REQUEST['comment'])) {
273 $sql_query .= ' COMMENT = \''
274 . PMA_Util
::sqlAddSlashes($_REQUEST['comment']) . '\'';
276 if (!empty($_REQUEST['partition_definition'])) {
277 $sql_query .= ' ' . PMA_Util
::sqlAddSlashes(
278 $_REQUEST['partition_definition']
287 * Function to get the number of fields for the table creation form
291 function PMA_getNumberOfFieldsFromRequest()
293 if (isset($_REQUEST['submit_num_fields'])) {
294 $num_fields = $_REQUEST['orig_num_fields'] +
$_REQUEST['added_fields'];
295 } elseif (isset($_REQUEST['num_fields'])
296 && intval($_REQUEST['num_fields']) > 0
298 $num_fields = (int) $_REQUEST['num_fields'];
307 * Function to execute the column creation statement
309 * @param string $db current database
310 * @param string $table current table
311 * @param string $err_url error page url
315 function PMA_tryColumnCreationQuery($db, $table, $err_url)
317 // get column addition statements
318 $sql_statement = PMA_getColumnCreationStatements(false);
320 // To allow replication, we first select the db to use and then run queries
322 $GLOBALS['dbi']->selectDb($db)
323 or PMA_Util
::mysqlDie(
324 $GLOBALS['dbi']->getError(),
325 'USE ' . PMA_Util
::backquote($db),
329 $sql_query = 'ALTER TABLE ' .
330 PMA_Util
::backquote($table) . ' ' . $sql_statement . ';';
331 return array($GLOBALS['dbi']->tryQuery($sql_query) , $sql_query);