diff options
author | Thilina Buddika <thilinaabeyrathna@gmail.com> | 2012-05-06 18:31:48 +0400 |
---|---|---|
committer | Thilina Buddika <thilinaabeyrathna@gmail.com> | 2012-05-06 18:31:48 +0400 |
commit | a86ca38f925a39847348fcb794974fc60f4f94c6 (patch) | |
tree | 22b0ab4cfc9353c5922acb9d82810c3b6c952b39 /tbl_change.php | |
parent | 34cf0987a2345d4cc74a8eb26145c2fe2539f2cd (diff) |
small code refactoring on tbl_change script
Diffstat (limited to 'tbl_change.php')
-rw-r--r-- | tbl_change.php | 113 |
1 files changed, 89 insertions, 24 deletions
diff --git a/tbl_change.php b/tbl_change.php index d6b46b51a4..f8bb7a0d10 100644 --- a/tbl_change.php +++ b/tbl_change.php @@ -153,52 +153,117 @@ unset($show_create_table); PMA_DBI_select_db($db); $table_fields = array_values(PMA_DBI_get_columns($db, $table)); $rows = array(); -if (isset($where_clause)) { - // when in edit mode load all selected rows from table - $insert_mode = false; + +$insertMode_whereClauses_reult = PMA_insert_mode($where_clause, $rows, $table, $db, $cfg); +$insert_mode = $insertMode_whereClauses_reult['insertMode']; +$where_clauses = $insertMode_whereClauses_reult['whereClauses']; +$result = $insertMode_whereClauses_reult['result']; +$rows = $insertMode_whereClauses_reult['rows']; + +/** + * + * @param array $where_clause + * @param array $rows + * @param srting $table + * @param sring $db + * @param array $cfg + * @return type array + */ +function PMA_insert_mode($where_clause, $rows, $table, $db, $cfg) +{ + if (isset($where_clause)) { + // when in edit mode load all selected rows from table + $where_clause_array = PMA_where_clause_array($where_clause); + $where_clauses_and_result = PMA_edit_load_all_selected_row($where_clause_array, $rows, $table, $db); + return array('insertMode' => false, 'whereClauses' => $where_clauses_and_result['where_clauses'], 'result' =>$where_clauses_and_result['result'], 'rows' => $where_clauses_and_result['rows']); + } else { + $result = PMA_edit_load_first_raw($table, $db, $rows, $cfg); + return array('insertMode' => true, 'result' => $result['result'], 'rows' => $result['rows']); + } +} + +/** + * + * @param array $where_clause + * @return array + */ +function PMA_where_clause_array($where_clause) +{ if (is_array($where_clause)) { - $where_clause_array = $where_clause; + return $where_clause; } else { - $where_clause_array = array(0 => $where_clause); + return array(0 => $where_clause); } +} +/** + * + * @param array $where_clause_array + * @param array $rows + * @param string $table + * @param string $db + * @return array + */ +function PMA_edit_load_all_selected_row($where_clause_array, $rows, $table, $db) +{ $result = array(); $found_unique_key = false; $where_clauses = array(); - foreach ($where_clause_array as $key_id => $where_clause) { $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' WHERE ' . $where_clause . ';'; $result[$key_id] = PMA_DBI_query($local_query, null, PMA_DBI_QUERY_STORE); $rows[$key_id] = PMA_DBI_fetch_assoc($result[$key_id]); $where_clauses[$key_id] = str_replace('\\', '\\\\', $where_clause); + PMA_edit_no_raw_return($rows, $key_id, $where_clause_array, $local_query, $result, $found_unique_key); + } + return array('whereClauses' => $where_clauses, 'resullt' => $result, 'rows' => $rows); +} - // No row returned - if (! $rows[$key_id]) { - unset($rows[$key_id], $where_clause_array[$key_id]); - PMA_showMessage(__('MySQL returned an empty result set (i.e. zero rows).'), $local_query); - echo "\n"; - include 'libraries/footer.inc.php'; - } else { // end if (no row returned) - $meta = PMA_DBI_get_fields_meta($result[$key_id]); - list($unique_condition, $tmp_clause_is_unique) - = PMA_getUniqueCondition($result[$key_id], count($meta), $meta, $rows[$key_id], true); - if (! empty($unique_condition)) { - $found_unique_key = true; - } - unset($unique_condition, $tmp_clause_is_unique); +/** + * + * @param array $rows + * @param string $key_id + * @param array $where_clause_array + * @param string $local_query + * @param array $result + * @param boolean $found_unique_key + */ +function PMA_edit_no_raw_return($rows, $key_id, $where_clause_array, $local_query, $result, $found_unique_key) +{ + // No row returned + if (! $rows[$key_id]) { + unset($rows[$key_id], $where_clause_array[$key_id]); + PMA_showMessage(__('MySQL returned an empty result set (i.e. zero rows).'), $local_query); + echo "\n"; + include 'libraries/footer.inc.php'; + } else {// end if (no row returned) + $meta = PMA_DBI_get_fields_meta($result[$key_id]); + list($unique_condition, $tmp_clause_is_unique) + = PMA_getUniqueCondition($result[$key_id], count($meta), $meta, $rows[$key_id], true); + if (! empty($unique_condition)) { + $found_unique_key = true; } - + unset($unique_condition, $tmp_clause_is_unique); } -} else { - // no primary key given, just load first row - but what happens if table is empty? - $insert_mode = true; +} +/** + * + * @param string $table + * @param string $db + * @param array $rows + * @param array $cfg + * @return array + */ +function PMA_edit_load_first_raw($table, $db, $rows, $cfg ) +{ $result = PMA_DBI_query( 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' LIMIT 1;', null, PMA_DBI_QUERY_STORE ); $rows = array_fill(0, $cfg['InsertRows'], false); + return array('result' => $result, 'rows' => $rows); } // Copying a row - fetched data will be inserted as a new row, therefore the where clause is needless. |