From c2ba8f7a80d27ece83caa4d7aedfa31893a39622 Mon Sep 17 00:00:00 2001 From: Samith Dassanayake Date: Wed, 8 May 2013 20:28:37 +0530 Subject: [PATCH 1/4] =?UTF-8?q?Moved=20duplicated=20codes=20in=20=E2=80=9C?= =?UTF-8?q?tbl=5Faddfield.php=E2=80=9D=20and=20=E2=80=9Ctbl=5Fcreate.php?= =?UTF-8?q?=E2=80=9D=20to=20single=20a=20file=20"create=5Faddfield.lib.php?= =?UTF-8?q?".=20Refactored=20the=20code=20using=20=E2=80=9CExtract=20metho?= =?UTF-8?q?d=E2=80=9D=20refactoring=20technique.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/create_addfield.lib.php | 214 ++++++++++++++++++++++++++++++ tbl_addfield.php | 123 +---------------- tbl_create.php | 136 +------------------ 3 files changed, 226 insertions(+), 247 deletions(-) create mode 100644 libraries/create_addfield.lib.php diff --git a/libraries/create_addfield.lib.php b/libraries/create_addfield.lib.php new file mode 100644 index 0000000000..01a1cf4ab5 --- /dev/null +++ b/libraries/create_addfield.lib.php @@ -0,0 +1,214 @@ + diff --git a/tbl_addfield.php b/tbl_addfield.php index 259581025b..fda92124be 100644 --- a/tbl_addfield.php +++ b/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) { diff --git a/tbl_create.php b/tbl_create.php index 510e16a191..036a83b40c 100644 --- a/tbl_create.php +++ b/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']) From 73f674d7559c8715e7502eed1844a3e76b455dd0 Mon Sep 17 00:00:00 2001 From: Samith Dassanayake Date: Sun, 12 May 2013 19:30:36 +0530 Subject: [PATCH 2/4] Updated the description of the "create_addfield.lib.php" file --- libraries/create_addfield.lib.php | 3 ++- tbl_create.php | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/create_addfield.lib.php b/libraries/create_addfield.lib.php index 01a1cf4ab5..34df482dab 100644 --- a/libraries/create_addfield.lib.php +++ b/libraries/create_addfield.lib.php @@ -1,7 +1,7 @@ Date: Tue, 14 May 2013 22:48:51 +0530 Subject: [PATCH 3/4] Renamed the function names, using "column" instead of "field". Corrected indentation according to cording standards. --- libraries/create_addfield.lib.php | 74 ++++++++++++++++++------------- tbl_addfield.php | 4 +- tbl_create.php | 4 +- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/libraries/create_addfield.lib.php b/libraries/create_addfield.lib.php index 34df482dab..ffa41d7c77 100644 --- a/libraries/create_addfield.lib.php +++ b/libraries/create_addfield.lib.php @@ -13,9 +13,9 @@ if (! defined('PHPMYADMIN')) { /** * Transforms the radio button field_key into 4 arrays * - * @return array An array of arrays which represents field keys for eaxh index type + * @return array An array of arrays which represents coulmn keys for each index type */ -function PMA_getIndexedFields() +function PMA_getIndexedColumns() { $field_cnt = count($_REQUEST['field_name']); $field_primary = array(); @@ -41,21 +41,22 @@ function PMA_getIndexedFields() } // end if } // end for - return array( $field_cnt, $field_primary, $field_index, $field_unique, $field_fulltext ); + 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 + * Initiate the coulmn creation statement according to the table creation or + * add coulmns to a exsiting table * - * @param int $field_cnt number of fields + * @param int $field_cnt number of coulmns * @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) +function PMA_buildColumnCreationStatement($field_cnt ,$is_create_tbl = true) { $definitions = array(); for ($i = 0; $i < $field_cnt; ++$i) { @@ -91,7 +92,7 @@ function PMA_buildFieldCreationStatement($field_cnt ,$is_create_tbl = true) ); - $definition .= PMA_setFieldCreationStatementSuffix($i, $is_create_tbl); + $definition .= PMA_setColumnCreationStatementSuffix($i, $is_create_tbl); $definitions[] = $definition; } // end for @@ -99,15 +100,15 @@ function PMA_buildFieldCreationStatement($field_cnt ,$is_create_tbl = true) } /** - * Set field creation suffix according to requested position of the new field + * Set coulmn creation suffix according to requested position of the new coulmn * - * @param int $current_field_num current field number - * @param boolean $is_create_tbl true if requirement is to get the statement - * for table creation + * @param int $current_field_num current coulmn 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) +function PMA_setColumnCreationStatementSuffix($current_field_num ,$is_create_tbl = true) { // no suffix is needed if request is a table creation $sql_suffix = " "; @@ -123,7 +124,9 @@ function PMA_setFieldCreationStatementSuffix($current_field_num ,$is_create_tbl } } else { $sql_suffix .= ' AFTER ' - . PMA_Util::backquote($_REQUEST['field_name'][$current_field_num - 1]); + . PMA_Util::backquote( + $_REQUEST['field_name'][$current_field_num - 1] + ); } } } @@ -133,11 +136,11 @@ function PMA_setFieldCreationStatementSuffix($current_field_num ,$is_create_tbl /** * 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 + * @param array $indexed_fields an array of index coulmns + * @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 */ @@ -149,7 +152,8 @@ function PMA_buildIndexStatements($indexed_fields, $index_type, $is_create_tbl foreach ($indexed_fields as $field_nr) { $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]); } - $statement[] = getStatementPrefix($is_create_tbl).' '.$index_type.' (' . implode(', ', $fields) . ') '; + $statement[] = getStatementPrefix($is_create_tbl) + .' '.$index_type.' (' . implode(', ', $fields) . ') '; unset($fields); } @@ -157,51 +161,61 @@ function PMA_buildIndexStatements($indexed_fields, $index_type, $is_create_tbl } /** - * Statement prefix for the PMA_buildFieldCreationStatement() + * Statement prefix for the PMA_buildCoulmnCreationStatement() * * @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){ + if ( !$is_create_tbl) { $sql_prefix = ' ADD '; } return $sql_prefix; } /** - * Returns sql stament according to the field and index specifictions as requested + * Returns sql statement according to the coulmn and index specifications 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) +function PMA_getColumnCreationStatements($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); + ) = PMA_getIndexedColumns(); + $definitions = PMA_buildColumnCreationStatement($field_cnt, $is_create_tbl); // Builds the primary keys statements - $primary_key_statements = PMA_buildIndexStatements($field_primary," PRIMARY KEY ", $is_create_tbl); + $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); + $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); + $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); + $fulltext_statements = PMA_buildIndexStatements( + $field_fulltext, " FULLTEXT ", $is_create_tbl + ); $definitions = array_merge($definitions, $fulltext_statements); if (count($definitions)) { diff --git a/tbl_addfield.php b/tbl_addfield.php index fda92124be..99bd1b617c 100644 --- a/tbl_addfield.php +++ b/tbl_addfield.php @@ -52,8 +52,8 @@ if (isset($_REQUEST['do_save_data'])) { unset($_REQUEST['do_save_data']); require_once 'libraries/create_addfield.lib.php'; - // get field addition statements - $sql_statement = PMA_getFieldCreationStatements(false); + // get column addition statements + $sql_statement = PMA_getColumnCreationStatements(false); // To allow replication, we first select the db to use and then run queries // on this db. diff --git a/tbl_create.php b/tbl_create.php index ebd74d48c1..160b62607a 100644 --- a/tbl_create.php +++ b/tbl_create.php @@ -69,8 +69,8 @@ if (isset($_REQUEST['do_save_data'])) { $sql_query = ''; require_once 'libraries/create_addfield.lib.php'; - // get field addition statements - $sql_statement = PMA_getFieldCreationStatements(true); + // get column addition statements + $sql_statement = PMA_getColumnCreationStatements(true); // Builds the 'create table' statement $sql_query = 'CREATE TABLE ' . PMA_Util::backquote($db) . '.' From 9746f07ae988815ff6de06f858de7d266bfe4591 Mon Sep 17 00:00:00 2001 From: Samith Dassanayake Date: Wed, 15 May 2013 19:33:30 +0530 Subject: [PATCH 4/4] Fixed typos. --- libraries/create_addfield.lib.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/libraries/create_addfield.lib.php b/libraries/create_addfield.lib.php index ffa41d7c77..bc117c7e77 100644 --- a/libraries/create_addfield.lib.php +++ b/libraries/create_addfield.lib.php @@ -13,7 +13,7 @@ if (! defined('PHPMYADMIN')) { /** * Transforms the radio button field_key into 4 arrays * - * @return array An array of arrays which represents coulmn keys for each index type + * @return array An array of arrays which represents column keys for each index type */ function PMA_getIndexedColumns() { @@ -46,10 +46,10 @@ function PMA_getIndexedColumns() } /** - * Initiate the coulmn creation statement according to the table creation or - * add coulmns to a exsiting table + * Initiate the column creation statement according to the table creation or + * add columns to a exsiting table * - * @param int $field_cnt number of coulmns + * @param int $field_cnt number of columns * @param boolean $is_create_tbl true if requirement is to get the statement * for table creation * @@ -67,7 +67,7 @@ function PMA_buildColumnCreationStatement($field_cnt ,$is_create_tbl = true) continue; } - $definition = getStatementPrefix($is_create_tbl) . + $definition = PMA_getStatementPrefix($is_create_tbl) . PMA_Table::generateFieldSpec( $_REQUEST['field_name'][$i], $_REQUEST['field_type'][$i], @@ -100,9 +100,9 @@ function PMA_buildColumnCreationStatement($field_cnt ,$is_create_tbl = true) } /** - * Set coulmn creation suffix according to requested position of the new coulmn + * Set column creation suffix according to requested position of the new column * - * @param int $current_field_num current coulmn number + * @param int $current_field_num current column number * @param boolean $is_create_tbl true if requirement is to get the statement * for table creation * @@ -136,7 +136,7 @@ function PMA_setColumnCreationStatementSuffix($current_field_num ,$is_create_tbl /** * Create relevent index statements * - * @param array $indexed_fields an array of index coulmns + * @param array $indexed_fields an array of index columns * @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 @@ -152,7 +152,7 @@ function PMA_buildIndexStatements($indexed_fields, $index_type, $is_create_tbl foreach ($indexed_fields as $field_nr) { $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]); } - $statement[] = getStatementPrefix($is_create_tbl) + $statement[] = PMA_getStatementPrefix($is_create_tbl) .' '.$index_type.' (' . implode(', ', $fields) . ') '; unset($fields); } @@ -161,14 +161,14 @@ function PMA_buildIndexStatements($indexed_fields, $index_type, $is_create_tbl } /** - * Statement prefix for the PMA_buildCoulmnCreationStatement() + * Statement prefix for the PMA_buildColumnCreationStatement() * * @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) +function PMA_getStatementPrefix($is_create_tbl = true) { $sql_prefix = " "; if ( !$is_create_tbl) { @@ -178,7 +178,7 @@ function getStatementPrefix($is_create_tbl = true) } /** - * Returns sql statement according to the coulmn and index specifications as requested + * Returns sql statement according to the column and index specifications as requested * * @param boolean $is_create_tbl true if requirement is to get the statement * for table creation