Moved duplicated codes in “tbl_addfield.php” and “tbl_create.php” to single a file "create_addfield.lib.php".
Refactored the code using “Extract method” refactoring technique.
This commit is contained in:
parent
4f91c74b89
commit
c2ba8f7a80
214
libraries/create_addfield.lib.php
Normal file
214
libraries/create_addfield.lib.php
Normal file
@ -0,0 +1,214 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* set of functions with the create/addfield features in pma
|
||||
*
|
||||
* @package PhpMyAdmin
|
||||
*/
|
||||
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the radio button field_key into 4 arrays
|
||||
*
|
||||
* @return array An array of arrays which represents field keys for eaxh index type
|
||||
*/
|
||||
function PMA_getIndexedFields()
|
||||
{
|
||||
$field_cnt = count($_REQUEST['field_name']);
|
||||
$field_primary = array();
|
||||
$field_index = array();
|
||||
$field_unique = array();
|
||||
$field_fulltext = array();
|
||||
for ($i = 0; $i < $field_cnt; ++$i) {
|
||||
if (isset($_REQUEST['field_key'][$i])
|
||||
&& strlen($_REQUEST['field_name'][$i])
|
||||
) {
|
||||
if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
|
||||
$field_primary[] = $i;
|
||||
}
|
||||
if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
|
||||
$field_index[] = $i;
|
||||
}
|
||||
if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
|
||||
$field_unique[] = $i;
|
||||
}
|
||||
if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
|
||||
$field_fulltext[] = $i;
|
||||
}
|
||||
} // end if
|
||||
} // end for
|
||||
|
||||
return array( $field_cnt, $field_primary, $field_index, $field_unique, $field_fulltext );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate the field creation statement according to the table creation or
|
||||
* add fields to a exsiting table
|
||||
*
|
||||
* @param int $field_cnt number of fields
|
||||
* @param boolean $is_create_tbl true if requirement is to get the statement
|
||||
* for table creation
|
||||
*
|
||||
* @return array $definitions An array of initial sql statements
|
||||
* according to the request
|
||||
*/
|
||||
function PMA_buildFieldCreationStatement($field_cnt ,$is_create_tbl = true)
|
||||
{
|
||||
$definitions = array();
|
||||
for ($i = 0; $i < $field_cnt; ++$i) {
|
||||
// '0' is also empty for php :-(
|
||||
if (empty($_REQUEST['field_name'][$i])
|
||||
&& $_REQUEST['field_name'][$i] != '0'
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$definition = getStatementPrefix($is_create_tbl) .
|
||||
PMA_Table::generateFieldSpec(
|
||||
$_REQUEST['field_name'][$i],
|
||||
$_REQUEST['field_type'][$i],
|
||||
$i,
|
||||
$_REQUEST['field_length'][$i],
|
||||
$_REQUEST['field_attribute'][$i],
|
||||
isset($_REQUEST['field_collation'][$i])
|
||||
? $_REQUEST['field_collation'][$i]
|
||||
: '',
|
||||
isset($_REQUEST['field_null'][$i])
|
||||
? $_REQUEST['field_null'][$i]
|
||||
: 'NOT NULL',
|
||||
$_REQUEST['field_default_type'][$i],
|
||||
$_REQUEST['field_default_value'][$i],
|
||||
isset($_REQUEST['field_extra'][$i])
|
||||
? $_REQUEST['field_extra'][$i]
|
||||
: false,
|
||||
isset($_REQUEST['field_comments'][$i])
|
||||
? $_REQUEST['field_comments'][$i]
|
||||
: '',
|
||||
$field_primary
|
||||
);
|
||||
|
||||
|
||||
$definition .= PMA_setFieldCreationStatementSuffix($i, $is_create_tbl);
|
||||
$definitions[] = $definition;
|
||||
} // end for
|
||||
|
||||
return $definitions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set field creation suffix according to requested position of the new field
|
||||
*
|
||||
* @param int $current_field_num current field number
|
||||
* @param boolean $is_create_tbl true if requirement is to get the statement
|
||||
* for table creation
|
||||
*
|
||||
* @return string $sql_suffix suffix
|
||||
*/
|
||||
function PMA_setFieldCreationStatementSuffix($current_field_num ,$is_create_tbl = true)
|
||||
{
|
||||
// no suffix is needed if request is a table creation
|
||||
$sql_suffix = " ";
|
||||
if (!$is_create_tbl) {
|
||||
if ($_REQUEST['field_where'] != 'last') {
|
||||
// Only the first field can be added somewhere other than at the end
|
||||
if ($current_field_num == 0) {
|
||||
if ($_REQUEST['field_where'] == 'first') {
|
||||
$sql_suffix .= ' FIRST';
|
||||
} else {
|
||||
$sql_suffix .= ' AFTER '
|
||||
. PMA_Util::backquote($_REQUEST['after_field']);
|
||||
}
|
||||
} else {
|
||||
$sql_suffix .= ' AFTER '
|
||||
. PMA_Util::backquote($_REQUEST['field_name'][$current_field_num - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $sql_suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create relevent index statements
|
||||
*
|
||||
* @param array $indexed_fields an array of index fields
|
||||
* @param string $index_type index type that which represents
|
||||
* the index type of $indexed_fields
|
||||
* @param boolean $is_create_tbl true if requirement is to get the statement
|
||||
* for table creation
|
||||
*
|
||||
* @return array an array of sql statements for indexes
|
||||
*/
|
||||
function PMA_buildIndexStatements($indexed_fields, $index_type, $is_create_tbl = true)
|
||||
{
|
||||
$statement = array();
|
||||
if (count($indexed_fields)) {
|
||||
$fields = array();
|
||||
foreach ($indexed_fields as $field_nr) {
|
||||
$fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
|
||||
}
|
||||
$statement[] = getStatementPrefix($is_create_tbl).' '.$index_type.' (' . implode(', ', $fields) . ') ';
|
||||
unset($fields);
|
||||
}
|
||||
|
||||
return $statement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Statement prefix for the PMA_buildFieldCreationStatement()
|
||||
*
|
||||
* @param boolean $is_create_tbl true if requirement is to get the statement
|
||||
* for table creation
|
||||
* @return string $sql_prefix prefix
|
||||
*/
|
||||
function getStatementPrefix($is_create_tbl = true)
|
||||
{
|
||||
$sql_prefix = " ";
|
||||
if ( !$is_create_tbl){
|
||||
$sql_prefix = ' ADD ';
|
||||
}
|
||||
return $sql_prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns sql stament according to the field and index specifictions as requested
|
||||
* @param boolean $is_create_tbl true if requirement is to get the statement
|
||||
* for table creation
|
||||
*
|
||||
* @return string sql statement
|
||||
*/
|
||||
function PMA_getFieldCreationStatements($is_create_tbl = true)
|
||||
{
|
||||
$definitions = array();
|
||||
$sql_statement = "";
|
||||
list($field_cnt, $field_primary, $field_index,
|
||||
$field_unique, $field_fulltext
|
||||
) = PMA_getIndexedFields();
|
||||
$definitions = PMA_buildFieldCreationStatement($field_cnt, $is_create_tbl);
|
||||
|
||||
// Builds the primary keys statements
|
||||
$primary_key_statements = PMA_buildIndexStatements($field_primary," PRIMARY KEY ", $is_create_tbl);
|
||||
$definitions = array_merge($definitions, $primary_key_statements);
|
||||
|
||||
// Builds the indexes statements
|
||||
$index_statements = PMA_buildIndexStatements($field_index," INDEX ", $is_create_tbl);
|
||||
$definitions = array_merge($definitions, $index_statements);
|
||||
|
||||
// Builds the uniques statements
|
||||
$unique_statements = PMA_buildIndexStatements($field_unique," UNIQUE ", $is_create_tbl);
|
||||
$definitions = array_merge($definitions, $unique_statements);
|
||||
|
||||
// Builds the fulltext statements
|
||||
$fulltext_statements = PMA_buildIndexStatements($field_fulltext," FULLTEXT ", $is_create_tbl);
|
||||
$definitions = array_merge($definitions, $fulltext_statements);
|
||||
|
||||
if (count($definitions)) {
|
||||
$sql_statement = implode(', ', $definitions);
|
||||
}
|
||||
|
||||
return $sql_statement;
|
||||
|
||||
}
|
||||
?>
|
||||
123
tbl_addfield.php
123
tbl_addfield.php
@ -50,123 +50,10 @@ if (isset($_REQUEST['do_save_data'])) {
|
||||
//avoid an incorrect calling of PMA_updateColumns() via
|
||||
//tbl_structure.php below
|
||||
unset($_REQUEST['do_save_data']);
|
||||
|
||||
$query = '';
|
||||
$definitions = array();
|
||||
|
||||
// Transforms the radio button field_key into 3 arrays
|
||||
$field_cnt = count($_REQUEST['field_name']);
|
||||
$field_primary = array();
|
||||
$field_index = array();
|
||||
$field_unique = array();
|
||||
$field_fulltext = array();
|
||||
for ($i = 0; $i < $field_cnt; ++$i) {
|
||||
if (isset($_REQUEST['field_key'][$i])
|
||||
&& strlen($_REQUEST['field_name'][$i])
|
||||
) {
|
||||
if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
|
||||
$field_primary[] = $i;
|
||||
}
|
||||
if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
|
||||
$field_index[] = $i;
|
||||
}
|
||||
if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
|
||||
$field_unique[] = $i;
|
||||
}
|
||||
if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
|
||||
$field_fulltext[] = $i;
|
||||
}
|
||||
} // end if
|
||||
} // end for
|
||||
|
||||
// Builds the field creation statement and alters the table
|
||||
for ($i = 0; $i < $field_cnt; ++$i) {
|
||||
// '0' is also empty for php :-(
|
||||
if (empty($_REQUEST['field_name'][$i])
|
||||
&& $_REQUEST['field_name'][$i] != '0'
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$definition = ' ADD ' . PMA_Table::generateFieldSpec(
|
||||
$_REQUEST['field_name'][$i],
|
||||
$_REQUEST['field_type'][$i],
|
||||
$i,
|
||||
$_REQUEST['field_length'][$i],
|
||||
$_REQUEST['field_attribute'][$i],
|
||||
isset($_REQUEST['field_collation'][$i])
|
||||
? $_REQUEST['field_collation'][$i]
|
||||
: '',
|
||||
isset($_REQUEST['field_null'][$i])
|
||||
? $_REQUEST['field_null'][$i]
|
||||
: 'NOT NULL',
|
||||
$_REQUEST['field_default_type'][$i],
|
||||
$_REQUEST['field_default_value'][$i],
|
||||
isset($_REQUEST['field_extra'][$i])
|
||||
? $_REQUEST['field_extra'][$i]
|
||||
: false,
|
||||
isset($_REQUEST['field_comments'][$i])
|
||||
? $_REQUEST['field_comments'][$i]
|
||||
: '',
|
||||
$field_primary
|
||||
);
|
||||
|
||||
if ($_REQUEST['field_where'] != 'last') {
|
||||
// Only the first field can be added somewhere other than at the end
|
||||
if ($i == 0) {
|
||||
if ($_REQUEST['field_where'] == 'first') {
|
||||
$definition .= ' FIRST';
|
||||
} else {
|
||||
$definition .= ' AFTER '
|
||||
. PMA_Util::backquote($_REQUEST['after_field']);
|
||||
}
|
||||
} else {
|
||||
$definition .= ' AFTER '
|
||||
. PMA_Util::backquote($_REQUEST['field_name'][$i-1]);
|
||||
}
|
||||
}
|
||||
$definitions[] = $definition;
|
||||
} // end for
|
||||
|
||||
// Builds the primary keys statements and updates the table
|
||||
if (count($field_primary)) {
|
||||
$fields = array();
|
||||
foreach ($field_primary as $field_nr) {
|
||||
$fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
|
||||
}
|
||||
$definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
|
||||
unset($fields);
|
||||
}
|
||||
|
||||
// Builds the indexes statements and updates the table
|
||||
if (count($field_index)) {
|
||||
$fields = array();
|
||||
foreach ($field_index as $field_nr) {
|
||||
$fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
|
||||
}
|
||||
$definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
|
||||
unset($fields);
|
||||
}
|
||||
|
||||
// Builds the uniques statements and updates the table
|
||||
if (count($field_unique)) {
|
||||
$fields = array();
|
||||
foreach ($field_unique as $field_nr) {
|
||||
$fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
|
||||
}
|
||||
$definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
|
||||
unset($fields);
|
||||
}
|
||||
|
||||
// Builds the fulltext statements and updates the table
|
||||
if (count($field_fulltext)) {
|
||||
$fields = array();
|
||||
foreach ($field_fulltext as $field_nr) {
|
||||
$fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
|
||||
}
|
||||
$definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
|
||||
unset($fields);
|
||||
}
|
||||
|
||||
require_once 'libraries/create_addfield.lib.php';
|
||||
// get field addition statements
|
||||
$sql_statement = PMA_getFieldCreationStatements(false);
|
||||
|
||||
// To allow replication, we first select the db to use and then run queries
|
||||
// on this db.
|
||||
@ -175,7 +62,7 @@ if (isset($_REQUEST['do_save_data'])) {
|
||||
PMA_DBI_getError(), 'USE ' . PMA_Util::backquote($db), '', $err_url
|
||||
);
|
||||
$sql_query = 'ALTER TABLE ' .
|
||||
PMA_Util::backquote($table) . ' ' . implode(', ', $definitions) . ';';
|
||||
PMA_Util::backquote($table) . ' ' . $sql_statement . ';';
|
||||
$result = PMA_DBI_tryQuery($sql_query);
|
||||
|
||||
if ($result === true) {
|
||||
|
||||
136
tbl_create.php
136
tbl_create.php
@ -67,137 +67,15 @@ if (!PMA_DBI_selectDb($db)) {
|
||||
*/
|
||||
if (isset($_REQUEST['do_save_data'])) {
|
||||
$sql_query = '';
|
||||
|
||||
// Transforms the radio button field_key into 3 arrays
|
||||
$field_cnt = count($_REQUEST['field_name']);
|
||||
for ($i = 0; $i < $field_cnt; ++$i) {
|
||||
if (isset($_REQUEST['field_key'][$i])) {
|
||||
if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
|
||||
$field_primary[] = $i;
|
||||
}
|
||||
if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
|
||||
$field_index[] = $i;
|
||||
}
|
||||
if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
|
||||
$field_unique[] = $i;
|
||||
}
|
||||
} // end if
|
||||
} // end for
|
||||
|
||||
// Builds the fields creation statements
|
||||
for ($i = 0; $i < $field_cnt; $i++) {
|
||||
// '0' is also empty for php :-(
|
||||
if (empty($_REQUEST['field_name'][$i])
|
||||
&& $_REQUEST['field_name'][$i] != '0'
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$query = PMA_Table::generateFieldSpec(
|
||||
$_REQUEST['field_name'][$i],
|
||||
$_REQUEST['field_type'][$i],
|
||||
$i,
|
||||
$_REQUEST['field_length'][$i],
|
||||
$_REQUEST['field_attribute'][$i],
|
||||
isset($_REQUEST['field_collation'][$i])
|
||||
? $_REQUEST['field_collation'][$i]
|
||||
: '',
|
||||
isset($_REQUEST['field_null'][$i])
|
||||
? $_REQUEST['field_null'][$i]
|
||||
: 'NOT NULL',
|
||||
$_REQUEST['field_default_type'][$i],
|
||||
$_REQUEST['field_default_value'][$i],
|
||||
isset($_REQUEST['field_extra'][$i])
|
||||
? $_REQUEST['field_extra'][$i]
|
||||
: false,
|
||||
isset($_REQUEST['field_comments'][$i])
|
||||
? $_REQUEST['field_comments'][$i]
|
||||
: '',
|
||||
$field_primary,
|
||||
''
|
||||
);
|
||||
|
||||
$query .= ', ';
|
||||
$sql_query .= $query;
|
||||
} // end for
|
||||
unset($field_cnt, $query);
|
||||
$sql_query = preg_replace('@, $@', '', $sql_query);
|
||||
|
||||
// Builds the primary keys statements
|
||||
$primary = '';
|
||||
$primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
|
||||
for ($i = 0; $i < $primary_cnt; $i++) {
|
||||
$j = $field_primary[$i];
|
||||
if (isset($_REQUEST['field_name'][$j])
|
||||
&& strlen($_REQUEST['field_name'][$j])
|
||||
) {
|
||||
$primary .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
|
||||
}
|
||||
} // end for
|
||||
unset($primary_cnt);
|
||||
$primary = preg_replace('@, $@', '', $primary);
|
||||
if (strlen($primary)) {
|
||||
$sql_query .= ', PRIMARY KEY (' . $primary . ')';
|
||||
}
|
||||
unset($primary);
|
||||
|
||||
// Builds the indexes statements
|
||||
$index = '';
|
||||
$index_cnt = (isset($field_index) ? count($field_index) : 0);
|
||||
for ($i = 0;$i < $index_cnt; $i++) {
|
||||
$j = $field_index[$i];
|
||||
if (isset($_REQUEST['field_name'][$j])
|
||||
&& strlen($_REQUEST['field_name'][$j])
|
||||
) {
|
||||
$index .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
|
||||
}
|
||||
} // end for
|
||||
unset($index_cnt);
|
||||
$index = preg_replace('@, $@', '', $index);
|
||||
if (strlen($index)) {
|
||||
$sql_query .= ', INDEX (' . $index . ')';
|
||||
}
|
||||
unset($index);
|
||||
|
||||
// Builds the uniques statements
|
||||
$unique = '';
|
||||
$unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
|
||||
for ($i = 0; $i < $unique_cnt; $i++) {
|
||||
$j = $field_unique[$i];
|
||||
if (isset($_REQUEST['field_name'][$j])
|
||||
&& strlen($_REQUEST['field_name'][$j])
|
||||
) {
|
||||
$unique .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
|
||||
}
|
||||
} // end for
|
||||
unset($unique_cnt);
|
||||
$unique = preg_replace('@, $@', '', $unique);
|
||||
if (strlen($unique)) {
|
||||
$sql_query .= ', UNIQUE (' . $unique . ')';
|
||||
}
|
||||
unset($unique);
|
||||
|
||||
// Builds the FULLTEXT statements
|
||||
$fulltext = '';
|
||||
$fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
|
||||
for ($i = 0; $i < $fulltext_cnt; $i++) {
|
||||
$j = $field_fulltext[$i];
|
||||
if (isset($_REQUEST['field_name'][$j])
|
||||
&& strlen($_REQUEST['field_name'][$j])
|
||||
) {
|
||||
$fulltext .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
|
||||
}
|
||||
} // end for
|
||||
|
||||
$fulltext = preg_replace('@, $@', '', $fulltext);
|
||||
if (strlen($fulltext)) {
|
||||
$sql_query .= ', FULLTEXT (' . $fulltext . ')';
|
||||
}
|
||||
unset($fulltext);
|
||||
|
||||
|
||||
require_once 'libraries/create_addfield.lib.php';
|
||||
// get field addition statements
|
||||
$sql_statement = PMA_getFieldCreationStatements(true);
|
||||
$sql_statement = preg_replace('@, $@', '', $sql_statement);
|
||||
|
||||
// Builds the 'create table' statement
|
||||
$sql_query = 'CREATE TABLE ' . PMA_Util::backquote($db) . '.'
|
||||
. PMA_Util::backquote($table) . ' (' . $sql_query . ')';
|
||||
. PMA_Util::backquote($table) . ' (' . $sql_statement . ')';
|
||||
|
||||
// Adds table type, character set, comments and partition definition
|
||||
if (!empty($_REQUEST['tbl_storage_engine'])
|
||||
|
||||
Loading…
Reference in New Issue
Block a user