From 78129140fd3d49510089c2d6311d01ffbbef5f7c Mon Sep 17 00:00:00 2001 From: Chanaka Indrajith Date: Sun, 5 Aug 2012 09:53:16 +0530 Subject: [PATCH] Code improvements related to VIEW transformations --- libraries/tbl_views.lib.php | 161 ++++++++++++++++++++++++++++++++++++ view_create.php | 105 +++-------------------- 2 files changed, 174 insertions(+), 92 deletions(-) create mode 100644 libraries/tbl_views.lib.php diff --git a/libraries/tbl_views.lib.php b/libraries/tbl_views.lib.php new file mode 100644 index 0000000000..4b94439e2e --- /dev/null +++ b/libraries/tbl_views.lib.php @@ -0,0 +1,161 @@ + 0) { + + for ($i=0; $itable; + $map['refering_column'] = $real_source_fields_meta[$i]->name; + + if (count($view_columns) > 1) { + $map['real_column'] = $view_columns[$i]; + } + + $column_map[] = $map; + + } + + } + + } + unset($real_source_result); + + return $column_map; + +} + + +/** + * Get existing data on tranformations applyed for + * columns in a particular table + * + * @param string $db Database name looking for + * + * @return mysqli_result Result of executed SQL query + */ +function PMA_getExistingTranformationData($db) +{ + + $common_functions = PMA_CommonFunctions::getInstance(); + $cfgRelation = PMA_getRelationsParam(); + + // Get the existing transformation details of the same database + // from pma_column_info table + $pma_transformation_sql = 'SELECT * FROM ' + . $common_functions->backquote($cfgRelation['db']) . '.' + . $common_functions->backquote($cfgRelation['column_info']) + . ' WHERE `db_name` = \'' + . $common_functions->sqlAddSlashes($db) . '\''; + + return PMA_DBI_try_query($pma_transformation_sql); + +} + + +/** + * Get SQL query for store new transformation details of a VIEW + * + * @param mysqli_result $pma_tranformation_data Result set of SQL execution + * @param array $column_map Details of VIEW columns + * @param string $view_name Name of the VIEW + * @param string $db Database name of the VIEW + * + * @return string $new_transformations_sql SQL query for new tranformations + */ +function PMA_getNewTransformationDataSql( + $pma_tranformation_data, $column_map, $view_name, $db +) { + + $common_functions = PMA_CommonFunctions::getInstance(); + $cfgRelation = PMA_getRelationsParam(); + + // Need to store new transformation details for VIEW + $new_transformations_sql = 'INSERT INTO ' + . $common_functions->backquote($cfgRelation['db']) . '.' + . $common_functions->backquote($cfgRelation['column_info']) + . ' (`db_name`, `table_name`, `column_name`, `comment`, ' + . '`mimetype`, `transformation`, `transformation_options`)' + . ' VALUES '; + + $column_count = 0; + $add_comma = false; + + while ($data_row = PMA_DBI_fetch_assoc($pma_tranformation_data)) { + + foreach ($column_map as $column) { + + if ($data_row['table_name'] == $column['table_name'] + && $data_row['column_name'] == $column['refering_column'] + ) { + + $new_transformations_sql .= $add_comma ? ', ' : ''; + + $new_transformations_sql .= '(' + . '\'' . $db . '\', ' + . '\'' . $view_name . '\', ' + . '\''; + + $new_transformations_sql .= (isset($column['real_column'])) + ? $column['real_column'] + : $column['refering_column']; + + $new_transformations_sql .= '\', ' + . '\'' . $data_row['comment'] . '\', ' + . '\'' . $data_row['mimetype'] . '\', ' + . '\'' . $data_row['transformation'] . '\', ' + . '\'' + . $common_functions->sqlAddSlashes( + $data_row['transformation_options'] + ) + . '\')'; + + $add_comma = true; + $column_count++; + break; + + } + + } + + if ($column_count == count($column_map)) { + break; + } + + } + + return ($column_count > 0) ? $new_transformations_sql : ''; + +} + + +?> diff --git a/view_create.php b/view_create.php index 76e4d88c1d..d5644fb275 100644 --- a/view_create.php +++ b/view_create.php @@ -64,111 +64,32 @@ if (isset($_REQUEST['createview'])) { if (PMA_DBI_try_query($sql_query)) { + require_once './libraries/tbl_views.lib.php'; + // If different column names defined for VIEW $view_columns = array(); if (isset($_REQUEST['view']['column_names'])) { $view_columns = explode(',', $_REQUEST['view']['column_names']); } - $column_map = array(); - // Select query which give results for VIEW - $real_source_result = PMA_DBI_try_query($_REQUEST['view']['as']); - - if ($real_source_result !== false) { - - $real_source_fields_meta = PMA_DBI_get_fields_meta($real_source_result); - - if (count($real_source_fields_meta) > 0) { - - for ($i=0; $itable; - $map['refering_column'] = $real_source_fields_meta[$i]->name; - - if (count($view_columns) > 1) { - $map['real_column'] = $view_columns[$i]; - } - - $column_map[] = $map; - - } - - } - - } - unset($real_source_result); - - // Get the existing transformation details of the same database - // from pma_column_info table - $pma_transformation_sql = 'SELECT * FROM ' - . $common_functions->backquote($cfgRelation['db']) . '.' - . $common_functions->backquote($cfgRelation['column_info']) - . ' WHERE `db_name` = \'' - . $common_functions->sqlAddSlashes($GLOBALS['db']) . '\''; - - $pma_tranformation_data = PMA_DBI_try_query($pma_transformation_sql); + $column_map = PMA_getColumnMap($_REQUEST['view']['as'], $view_columns); + $pma_tranformation_data = PMA_getExistingTranformationData($GLOBALS['db']); if ($pma_tranformation_data !== false) { - // Need to store new transformation details for VIEW - $new_transformations_sql = 'INSERT INTO ' - . $common_functions->backquote($cfgRelation['db']) . '.' - . $common_functions->backquote($cfgRelation['column_info']) - . ' (`db_name`, `table_name`, `column_name`, `comment`, ' - . '`mimetype`, `transformation`, `transformation_options`)' - . ' VALUES '; - - $column_count = 0; - $add_comma = false; - - while ($data_row = PMA_DBI_fetch_assoc($pma_tranformation_data)) { - - foreach ($column_map as $column) { - - if ($data_row['table_name'] == $column['table_name'] - && $data_row['column_name'] == $column['refering_column'] - ) { - - $new_transformations_sql .= $add_comma ? ', ' : ''; - - $new_transformations_sql .= '(' - . '\'' . $GLOBALS['db'] . '\', ' - . '\'' . $_REQUEST['view']['name'] . '\', ' - . '\''; - - $new_transformations_sql .= (isset($column['real_column'])) - ? $column['real_column'] - : $column['refering_column']; - - $new_transformations_sql .= '\', ' - . '\'' . $data_row['comment'] . '\', ' - . '\'' . $data_row['mimetype'] . '\', ' - . '\'' . $data_row['transformation'] . '\', ' - . '\'' - . $common_functions->sqlAddSlashes( - $data_row['transformation_options'] - ) - . '\')'; - - $add_comma = true; - $column_count++; - break; - - } - - } - - if ($column_count == count($column_map)) { - break; - } - - } + // SQL for store new transformation details of VIEW + $new_transformations_sql = PMA_getNewTransformationDataSql( + $pma_tranformation_data, $column_map, $_REQUEST['view']['name'], + $GLOBALS['db'] + ); // Store new transformations - PMA_DBI_try_query($new_transformations_sql); + if ($new_transformations_sql != '') { + PMA_DBI_try_query($new_transformations_sql); + } } + unset($pma_tranformation_data); if ($GLOBALS['is_ajax_request'] != true) { $message = PMA_Message::success();