From dbe87e52a4f6c957f807893bb2188aac0f17f5da Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Mon, 9 Jul 2012 23:54:18 +0530 Subject: [PATCH 01/45] Form function for generating SELECT clause --- db_qbe.php | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 7ed278204b..1c0b75d3a0 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -568,6 +568,34 @@ function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $and_or_col, return $html_output; } +/** + * Provides SELECT clause for building SQL query + * + * @param array $criteria_column_count Number of criteria columns + * + * @return Select clause + */ +function PMA_dbQbeGetSelectClause($criteria_column_count){ + $last_select = 0; + $select_clause = ''; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if (! empty($GLOBALS['curField'][$column_index]) + && isset($GLOBALS['curShow'][$column_index]) + && $GLOBALS['curShow'][$column_index] == 'on') + { + if ($last_select) { + $select_clause .= ', '; + } + $select_clause .= $GLOBALS['curField'][$column_index]; + $last_select = 1; + } + } // end for + if (! empty($select_clause)) { + $select_clause = 'SELECT ' . htmlspecialchars($select_clause) . "\n"; + } + return $select_clause; +} + if ($cfgRelation['designerwork']) { $url = 'pmd_general.php' . PMA_generate_common_url( array_merge( @@ -799,26 +827,7 @@ echo PMA_dbQbeGetTablesList($tbl_names); dir=""> Date: Tue, 10 Jul 2012 08:18:07 +0530 Subject: [PATCH 02/45] Form function for generating where clause --- db_qbe.php | 120 +++++++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 55 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 1c0b75d3a0..cd92136cf0 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -596,6 +596,70 @@ function PMA_dbQbeGetSelectClause($criteria_column_count){ return $select_clause; } +/** + * Provides WHERE clause for building SQL query + * + * @param array $criteria_column_count Number of criteria columns + * + * @return Where clause + */ +function PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count) { + $where_clause = ''; + $criteria_cnt = 0; + for ($x = 0; $x < $criteria_column_count; $x++) { + if (! empty($GLOBALS['curField'][$x]) + && ! empty($GLOBALS['curCriteria'][$x]) + && $x + && isset($last_where) + && isset($GLOBALS['curAndOrCol'])) { + $where_clause .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_where]) . ' '; + } + if (! empty($GLOBALS['curField'][$x]) && ! empty($GLOBALS['curCriteria'][$x])) { + $where_clause .= '(' . $GLOBALS['curField'][$x] . ' ' + . $GLOBALS['curCriteria'][$x] . ')'; + $last_where = $x; + $criteria_cnt++; + } + } // end for + if ($criteria_cnt > 1) { + $where_clause = '(' . $where_clause . ')'; + } + // OR rows ${'cur' . $or}[$x] + if (! isset($GLOBALS['curAndOrRow'])) { + $GLOBALS['curAndOrRow'] = array(); + } + for ($y = 0; $y <= $criteria_row_count; $y++) { + $criteria_cnt = 0; + $qry_orwhere = ''; + $last_orwhere = ''; + for ($x = 0; $x < $criteria_column_count; $x++) { + if (! empty($GLOBALS['curField'][$x]) && ! empty(${'curOr' . $y}[$x]) && $x) { + $qry_orwhere .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_orwhere]) . ' '; + } + if (! empty($GLOBALS['curField'][$x]) && ! empty(${'curOr' . $y}[$x])) { + $qry_orwhere .= '(' . $GLOBALS['curField'][$x] + . ' ' + . ${'curOr' . $y}[$x] + . ')'; + $last_orwhere = $x; + $criteria_cnt++; + } + } // end for + if ($criteria_cnt > 1) { + $qry_orwhere = '(' . $qry_orwhere . ')'; + } + if (! empty($qry_orwhere)) { + $where_clause .= "\n" + . strtoupper(isset($GLOBALS['curAndOrRow'][$y]) ? $GLOBALS['curAndOrRow'][$y] . ' ' : '') + . $qry_orwhere; + } // end if + } // end for + + if (! empty($where_clause) && $where_clause != '()') { + echo 'WHERE ' . htmlspecialchars($where_clause) . "\n"; + } // end if +} + if ($cfgRelation['designerwork']) { $url = 'pmd_general.php' . PMA_generate_common_url( array_merge( @@ -1020,61 +1084,7 @@ if (! empty($qry_from)) { } // 3. WHERE -$qry_where = ''; -$criteria_cnt = 0; -for ($x = 0; $x < $col; $x++) { - if (! empty($GLOBALS['curField'][$x]) - && ! empty($GLOBALS['curCriteria'][$x]) - && $x - && isset($last_where) - && isset($GLOBALS['curAndOrCol'])) { - $qry_where .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_where]) . ' '; - } - if (! empty($GLOBALS['curField'][$x]) && ! empty($GLOBALS['curCriteria'][$x])) { - $qry_where .= '(' . $GLOBALS['curField'][$x] . ' ' - . $GLOBALS['curCriteria'][$x] . ')'; - $last_where = $x; - $criteria_cnt++; - } -} // end for -if ($criteria_cnt > 1) { - $qry_where = '(' . $qry_where . ')'; -} -// OR rows ${'cur' . $or}[$x] -if (! isset($GLOBALS['curAndOrRow'])) { - $GLOBALS['curAndOrRow'] = array(); -} -for ($y = 0; $y <= $row; $y++) { - $criteria_cnt = 0; - $qry_orwhere = ''; - $last_orwhere = ''; - for ($x = 0; $x < $col; $x++) { - if (! empty($GLOBALS['curField'][$x]) && ! empty(${'curOr' . $y}[$x]) && $x) { - $qry_orwhere .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_orwhere]) . ' '; - } - if (! empty($GLOBALS['curField'][$x]) && ! empty(${'curOr' . $y}[$x])) { - $qry_orwhere .= '(' . $GLOBALS['curField'][$x] - . ' ' - . ${'curOr' . $y}[$x] - . ')'; - $last_orwhere = $x; - $criteria_cnt++; - } - } // end for - if ($criteria_cnt > 1) { - $qry_orwhere = '(' . $qry_orwhere . ')'; - } - if (! empty($qry_orwhere)) { - $qry_where .= "\n" - . strtoupper(isset($GLOBALS['curAndOrRow'][$y]) ? $GLOBALS['curAndOrRow'][$y] . ' ' : '') - . $qry_orwhere; - } // end if -} // end for - -if (! empty($qry_where) && $qry_where != '()') { - echo 'WHERE ' . htmlspecialchars($qry_where) . "\n"; -} // end if - +echo PMA_dbQbeGetWhereClause($col, $row); // 4. ORDER BY $last_orderby = 0; From 664e2d46a62360f85f71bb7994c72e33f075cde1 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 10 Jul 2012 20:55:21 +0530 Subject: [PATCH 03/45] Add missing return statement --- db_qbe.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/db_qbe.php b/db_qbe.php index cd92136cf0..55d674208c 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -656,8 +656,9 @@ function PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count) { } // end for if (! empty($where_clause) && $where_clause != '()') { - echo 'WHERE ' . htmlspecialchars($where_clause) . "\n"; + $where_clause = 'WHERE ' . htmlspecialchars($where_clause) . "\n"; } // end if + return $where_clause; } if ($cfgRelation['designerwork']) { From 398588a16de39ce39c8d6112167e41c245d32095 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 10 Jul 2012 21:10:24 +0530 Subject: [PATCH 04/45] Form function to generate order by clause --- db_qbe.php | 60 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 55d674208c..51c3531c77 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -661,6 +661,44 @@ function PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count) { return $where_clause; } +/** + * Provides ORDER BY clause for building SQL query + * + * @param array $criteria_column_count Number of criteria columns + * + * @return Order By clause + */ +function PMA_dbQbeGetOrderByClause($criteria_column_count) +{ + $last_orderby = 0; + $orderby_clause = ''; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if ($last_orderby + && $column_index + && ! empty($GLOBALS['curField'][$column_index]) + && ! empty($GLOBALS['curSort'][$column_index]) + ) { + $orderby_clause .= ', '; + } + if (! empty($GLOBALS['curField'][$column_index]) + && ! empty($GLOBALS['curSort'][$column_index]) + ) { + // if they have chosen all fields using the * selector, + // then sorting is not available + // Fix for Bug #570698 + if (substr($GLOBALS['curField'][$column_index], -2) != '.*') { + $orderby_clause .= $GLOBALS['curField'][$column_index] . ' ' + . $GLOBALS['curSort'][$column_index]; + $last_orderby = 1; + } + } + } // end for + if (! empty($orderby_clause)) { + $orderby_clause = 'ORDER BY ' . htmlspecialchars($orderby_clause) . "\n"; + } + return $orderby_clause; +} + if ($cfgRelation['designerwork']) { $url = 'pmd_general.php' . PMA_generate_common_url( array_merge( @@ -1088,27 +1126,7 @@ if (! empty($qry_from)) { echo PMA_dbQbeGetWhereClause($col, $row); // 4. ORDER BY -$last_orderby = 0; -if (! isset($qry_orderby)) { - $qry_orderby = ''; -} -for ($x = 0; $x < $col; $x++) { - if ($last_orderby && $x && ! empty($GLOBALS['curField'][$x]) && ! empty($GLOBALS['curSort'][$x])) { - $qry_orderby .= ', '; - } - if (! empty($GLOBALS['curField'][$x]) && ! empty($GLOBALS['curSort'][$x])) { - // if they have chosen all fields using the * selector, - // then sorting is not available - // Fix for Bug #570698 - if (substr($GLOBALS['curField'][$x], -2) != '.*') { - $qry_orderby .= $GLOBALS['curField'][$x] . ' ' . $GLOBALS['curSort'][$x]; - $last_orderby = 1; - } - } -} // end for -if (! empty($qry_orderby)) { - echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n"; -} +echo PMA_dbQbeGetOrderByClause($col); ?> From 7024c4df9b5cd97ebe1c37bfb7045280a9bb3312 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 10 Jul 2012 21:11:31 +0530 Subject: [PATCH 05/45] Fix doc comment --- db_qbe.php | 1 + 1 file changed, 1 insertion(+) diff --git a/db_qbe.php b/db_qbe.php index 51c3531c77..36437ce145 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -600,6 +600,7 @@ function PMA_dbQbeGetSelectClause($criteria_column_count){ * Provides WHERE clause for building SQL query * * @param array $criteria_column_count Number of criteria columns + * @param array $criteria_row_count Number of criteria rows * * @return Where clause */ From 9fe3a570f097267367dbd2f9dc2935d2a0e548c2 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 10 Jul 2012 21:26:45 +0530 Subject: [PATCH 06/45] Logical improvement in PMA_dbQbeGetOrderByClause --- db_qbe.php | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 36437ce145..2d0b0149f1 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -673,25 +673,22 @@ function PMA_dbQbeGetOrderByClause($criteria_column_count) { $last_orderby = 0; $orderby_clause = ''; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if ($last_orderby - && $column_index - && ! empty($GLOBALS['curField'][$column_index]) - && ! empty($GLOBALS['curSort'][$column_index]) - ) { - $orderby_clause .= ', '; - } + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + // if all columns are chosen with * selector, then sorting isn't available + // Fix for Bug #570698 if (! empty($GLOBALS['curField'][$column_index]) && ! empty($GLOBALS['curSort'][$column_index]) ) { - // if they have chosen all fields using the * selector, - // then sorting is not available - // Fix for Bug #570698 - if (substr($GLOBALS['curField'][$column_index], -2) != '.*') { - $orderby_clause .= $GLOBALS['curField'][$column_index] . ' ' - . $GLOBALS['curSort'][$column_index]; - $last_orderby = 1; + if (substr($GLOBALS['curField'][$column_index], -2) == '.*') { + continue; } + if ($last_orderby && $column_index) { + $orderby_clause .= ', '; + } + $orderby_clause .= $GLOBALS['curField'][$column_index] . ' ' + . $GLOBALS['curSort'][$column_index]; + $last_orderby = 1; } } // end for if (! empty($orderby_clause)) { From fc7bfa8cc332d88a3ec95babf54769e869dcbc0d Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 10 Jul 2012 21:51:49 +0530 Subject: [PATCH 07/45] Use implode in place of last_select and last_orderby --- db_qbe.php | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 2d0b0149f1..8d1a6e8f60 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -576,22 +576,19 @@ function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $and_or_col, * @return Select clause */ function PMA_dbQbeGetSelectClause($criteria_column_count){ - $last_select = 0; $select_clause = ''; + $select_clauses = array(); for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { if (! empty($GLOBALS['curField'][$column_index]) && isset($GLOBALS['curShow'][$column_index]) && $GLOBALS['curShow'][$column_index] == 'on') { - if ($last_select) { - $select_clause .= ', '; - } - $select_clause .= $GLOBALS['curField'][$column_index]; - $last_select = 1; + $select_clauses[] = $GLOBALS['curField'][$column_index]; } } // end for - if (! empty($select_clause)) { - $select_clause = 'SELECT ' . htmlspecialchars($select_clause) . "\n"; + if ($select_clauses) { + $select_clause = 'SELECT ' + . htmlspecialchars(implode(", ", $select_clauses)) . "\n"; } return $select_clause; } @@ -671,8 +668,8 @@ function PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count) { */ function PMA_dbQbeGetOrderByClause($criteria_column_count) { - $last_orderby = 0; $orderby_clause = ''; + $orderby_clauses = array(); for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { // if all columns are chosen with * selector, then sorting isn't available @@ -683,16 +680,13 @@ function PMA_dbQbeGetOrderByClause($criteria_column_count) if (substr($GLOBALS['curField'][$column_index], -2) == '.*') { continue; } - if ($last_orderby && $column_index) { - $orderby_clause .= ', '; - } - $orderby_clause .= $GLOBALS['curField'][$column_index] . ' ' + $orderby_clauses[] = $GLOBALS['curField'][$column_index] . ' ' . $GLOBALS['curSort'][$column_index]; - $last_orderby = 1; } } // end for - if (! empty($orderby_clause)) { - $orderby_clause = 'ORDER BY ' . htmlspecialchars($orderby_clause) . "\n"; + if ($orderby_clauses) { + $orderby_clause = 'ORDER BY ' + . htmlspecialchars(implode(", ", $orderby_clauses)) . "\n"; } return $orderby_clause; } From 48ae6db4cfc2e8f89a7e0920c5e866dd054b6ce8 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Wed, 11 Jul 2012 10:13:29 +0000 Subject: [PATCH 08/45] Some variable name improvements --- db_qbe.php | 201 +++++++++++++++++++++++++++-------------------------- 1 file changed, 101 insertions(+), 100 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 8d1a6e8f60..69ea8437a1 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -15,9 +15,9 @@ require_once 'libraries/common.inc.php'; * Sets globals from $_POST */ $post_params = array( - 'Field', - 'Show', - 'Sort' + 'criteriaColumn', + 'criteriaShow', + 'criteriaSort' ); foreach ($post_params as $one_post_param) { if (isset($_POST[$one_post_param])) { @@ -85,38 +85,38 @@ unset($message_to_display); /** * Initialize some variables */ -$col_cnt = PMA_ifSetOr($_REQUEST['col_cnt'], 3, 'numeric'); -$add_col = PMA_ifSetOr($_REQUEST['add_col'], 0, 'numeric'); -$add_row = PMA_ifSetOr($_REQUEST['add_row'], 0, 'numeric'); +$criteriaColumnCount = PMA_ifSetOr($_REQUEST['criteriaColumnCount'], 3, 'numeric'); +$criteriaColumnAdd = PMA_ifSetOr($_REQUEST['criteriaColumnAdd'], 0, 'numeric'); +$criteriaRowAdd = PMA_ifSetOr($_REQUEST['criteriaRowAdd'], 0, 'numeric'); $rows = PMA_ifSetOr($_REQUEST['rows'], 0, 'numeric'); -$ins_col = PMA_ifSetOr($_REQUEST['ins_col'], null, 'array'); -$del_col = PMA_ifSetOr($_REQUEST['del_col'], null, 'array'); +$criteriaColumnInsert = PMA_ifSetOr($_REQUEST['criteriaColumnInsert'], null, 'array'); +$criteriaColumnDelete = PMA_ifSetOr($_REQUEST['criteriaColumnDelete'], null, 'array'); $prev_criteria = isset($_REQUEST['prev_criteria']) ? $_REQUEST['prev_criteria'] : array(); $criteria = isset($_REQUEST['criteria']) ? $_REQUEST['criteria'] - : array_fill(0, $col_cnt, ''); + : array_fill(0, $criteriaColumnCount, ''); -$ins_row = isset($_REQUEST['ins_row']) - ? $_REQUEST['ins_row'] - : array_fill(0, $col_cnt, ''); -$del_row = isset($_REQUEST['del_row']) - ? $_REQUEST['del_row'] - : array_fill(0, $col_cnt, ''); -$and_or_row = isset($_REQUEST['and_or_row']) - ? $_REQUEST['and_or_row'] - : array_fill(0, $col_cnt, ''); -$and_or_col = isset($_REQUEST['and_or_col']) - ? $_REQUEST['and_or_col'] - : array_fill(0, $col_cnt, ''); +$criteriaRowInsert = isset($_REQUEST['criteriaRowInsert']) + ? $_REQUEST['criteriaRowInsert'] + : array_fill(0, $criteriaColumnCount, ''); +$criteriaRowDelete = isset($_REQUEST['criteriaRowDelete']) + ? $_REQUEST['criteriaRowDelete'] + : array_fill(0, $criteriaColumnCount, ''); +$criteriaAndOrRow = isset($_REQUEST['criteriaAndOrRow']) + ? $_REQUEST['criteriaAndOrRow'] + : array_fill(0, $criteriaColumnCount, ''); +$criteriaAndOrColumn = isset($_REQUEST['criteriaAndOrColumn']) + ? $_REQUEST['criteriaAndOrColumn'] + : array_fill(0, $criteriaColumnCount, ''); // minimum width $form_column_width = 12; -$col = max($col_cnt + $add_col, 0); -$row = max($rows + $add_row, 0); +$col = max($criteriaColumnCount + $criteriaColumnAdd, 0); +$row = max($rows + $criteriaRowAdd, 0); // The tables list sent by a previously submitted form @@ -191,7 +191,7 @@ function showColumnSelectCell($columns, $column_number, $selected = '') { $html_output = ''; $html_output .= ''; - $html_output .= ''; $html_output .= ''; foreach ($columns as $column) { $html_output .= ''; $html_output .= ''; @@ -234,30 +234,30 @@ function getSortSelectCell($column_number, $realwidth, $asc_selected = '', * * @param array $criteria_column_count Number of criteria columns * @param integer $columns All column names - * @param string $ins_col If a new criteria column is needed - * @param string $del_col If a criteria column is to be deleted + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted * * @return HTML for search table's row */ function PMA_dbQbegetColumnNamesRow( - $criteria_column_count, $columns, $ins_col = null, $del_col = null + $criteria_column_count, $columns, $criteriaColumnInsert = null, $criteriaColumnDelete = null ) { $html_output = ''; $html_output .= '' . __('Column') . ':'; $z = 0; for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (isset($ins_col[$column_index]) && $ins_col[$column_index] == 'on') { + if (isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { $html_output .= showColumnSelectCell($columns, $z); $z++; } - if (! empty($del_col) && isset($del_col[$column_index]) && $del_col[$column_index] == 'on') { + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { continue; } $selected = ''; - if (isset($_REQUEST['Field'][$column_index])) { - $selected = $_REQUEST['Field'][$column_index]; - $GLOBALS['curField'][$z] = $_REQUEST['Field'][$column_index]; + if (isset($_REQUEST['criteriaColumn'][$column_index])) { + $selected = $_REQUEST['criteriaColumn'][$column_index]; + $GLOBALS['curField'][$z] = $_REQUEST['criteriaColumn'][$column_index]; } $html_output .= showColumnSelectCell($columns, $z, $selected); $z++; @@ -271,44 +271,44 @@ function PMA_dbQbegetColumnNamesRow( * * @param array $criteria_column_count Number of criteria columns * @param string $realwidth Largest column width found - * @param string $ins_col If a new criteria column is needed - * @param string $del_col If a criteria column is to be deleted + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted * * @return HTML for search table's row */ function PMA_dbQbegetSortRow( - $criteria_column_count, $realwidth, $ins_col = null, $del_col = null + $criteria_column_count, $realwidth, $criteriaColumnInsert = null, $criteriaColumnDelete = null ) { $html_output = ''; $html_output .= '' . __('Sort') . ':'; $z = 0; for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($ins_col) && isset($ins_col[$column_index]) && $ins_col[$column_index] == 'on') { + if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { $html_output .= getSortSelectCell($z, $realwidth); $z++; } // end if - if (! empty($del_col) && isset($del_col[$column_index]) && $del_col[$column_index] == 'on') { + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { continue; } // If they have chosen all fields using the * selector, // then sorting is not available, Fix for Bug #570698 - if (isset($_REQUEST['Sort'][$column_index]) && isset($_REQUEST['Field'][$column_index]) - && substr($_REQUEST['Field'][$column_index], -2) == '.*' + if (isset($_REQUEST['criteriaSort'][$column_index]) && isset($_REQUEST['criteriaColumn'][$column_index]) + && substr($_REQUEST['criteriaColumn'][$column_index], -2) == '.*' ) { - $_REQUEST['Sort'][$column_index] = ''; + $_REQUEST['criteriaSort'][$column_index] = ''; } //end if // Set asc_selected - if (isset($_REQUEST['Sort'][$column_index]) && $_REQUEST['Sort'][$column_index] == 'ASC') { - $GLOBALS['curSort'][$z] = $_REQUEST['Sort'][$column_index]; + if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'ASC') { + $GLOBALS['curSort'][$z] = $_REQUEST['criteriaSort'][$column_index]; $asc_selected = ' selected="selected"'; } else { $asc_selected = ''; } // end if // Set desc selected - if (isset($_REQUEST['Sort'][$column_index]) && $_REQUEST['Sort'][$column_index] == 'DESC') { - $GLOBALS['curSort'][$z] = $_REQUEST['Sort'][$column_index]; + if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'DESC') { + $GLOBALS['curSort'][$z] = $_REQUEST['criteriaSort'][$column_index]; $desc_selected = ' selected="selected"'; } else { $desc_selected = ''; @@ -326,36 +326,36 @@ function PMA_dbQbegetSortRow( * Provides search form's row containing SHOW checkboxes * * @param array $criteria_column_count Number of criteria columns - * @param string $ins_col If a new criteria column is needed - * @param string $del_col If a criteria column is to be deleted + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted * * @return HTML for search table's row */ function PMA_dbQbegetShowRow( - $criteria_column_count, $ins_col = null, $del_col = null + $criteria_column_count, $criteriaColumnInsert = null, $criteriaColumnDelete = null ) { $html_output = ''; $html_output .= '' . __('Show') . ':'; $z = 0; for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($ins_col) && isset($ins_col[$column_index]) && $ins_col[$column_index] == 'on') { + if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { $html_output .= ''; - $html_output .= ''; + $html_output .= ''; $html_output .= ''; $z++; } // end if - if (! empty($del_col) && isset($del_col[$column_index]) && $del_col[$column_index] == 'on') { + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { continue; } - if (isset($_REQUEST['Show'][$column_index])) { + if (isset($_REQUEST['criteriaShow'][$column_index])) { $checked = ' checked="checked"'; - $GLOBALS['curShow'][$z] = $_REQUEST['Show'][$column_index]; + $GLOBALS['curShow'][$z] = $_REQUEST['criteriaShow'][$column_index]; } else { $checked = ''; } $html_output .= ''; - $html_output .= ''; $html_output .= ''; $z++; @@ -371,21 +371,21 @@ function PMA_dbQbegetShowRow( * @param string $realwidth Largest column width found * @param string $criteria Already Filled criteria * @param string $prev_criteria Previously filled criteria(hidden form field) - * @param string $ins_col If a new criteria column is needed - * @param string $del_col If a criteria column is to be deleted + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted * * @return HTML for search table's row */ function PMA_dbQbegetCriteriaInputboxRow( $criteria_column_count, $realwidth, $criteria, $prev_criteria, - $ins_col = null, $del_col = null + $criteriaColumnInsert = null, $criteriaColumnDelete = null ) { $html_output = ''; $html_output .= '' . __('Criteria') . ':'; $z = 0; for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($ins_col) && isset($ins_col[$column_index]) && $ins_col[$column_index] == 'on') { + if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { $html_output .= ''; $html_output .= ''; + . (($type == 'row') ? 'criteriaRowAdd' : 'criteriaColumnAdd') . '">'; $html_output .= ''; $html_output .= ''; $html_output .= ''; @@ -509,15 +509,15 @@ function PMA_dbQbeGetAndOrColCell($column_number, $selected = null) { $html_output = ''; $html_output .= '' . __('Or') . ':'; - $html_output .= ''; $html_output .= '  ' . __('And') . ':'; - $html_output .= ''; $html_output .= '
' . __('Ins'); - $html_output .= ''; + $html_output .= ''; $html_output .= '  ' . __('Del'); - $html_output .= ''; + $html_output .= ''; $html_output .= ''; return $html_output; } @@ -530,31 +530,31 @@ function PMA_dbQbeGetAndOrColCell($column_number, $selected = null) * @param string $realwidth Largest column width found * @param string $criteria Already Filled criteria * @param string $prev_criteria Previously filled criteria(hidden form field) - * @param string $ins_col If a new criteria column is needed - * @param string $del_col If a criteria column is to be deleted + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted * * @return HTML for search table's row */ -function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $and_or_col, - $ins_col = null, $del_col = null +function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $criteriaAndOrColumn, + $criteriaColumnInsert = null, $criteriaColumnDelete = null ) { $html_output = ''; $html_output .= '' . __('Modify') . ':'; $z = 0; for ($x = 0; $x < $criteria_column_count; $x++) { - if (! empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') { + if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$x]) && $criteriaColumnInsert[$x] == 'on') { $html_output .= PMA_dbQbeGetAndOrColCell($z); $z++; } // end if - if (! empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') { + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$x]) && $criteriaColumnDelete[$x] == 'on') { continue; } - if (isset($and_or_col[$x])) { - $GLOBALS['curAndOrCol'][$z] = $and_or_col[$x]; + if (isset($criteriaAndOrColumn[$x])) { + $GLOBALS['curAndOrCol'][$z] = $criteriaAndOrColumn[$x]; } - if (isset($and_or_col[$x]) && $and_or_col[$x] == 'or') { + if (isset($criteriaAndOrColumn[$x]) && $criteriaAndOrColumn[$x] == 'or') { $chk['or'] = ' checked="checked"'; $chk['and'] = ''; } else { @@ -712,16 +712,16 @@ if ($cfgRelation['designerwork']) { @@ -730,7 +730,7 @@ echo PMA_dbQbegetCriteriaInputboxRow( $w = 0; $odd_row = true; for ($y = 0; $y <= $row; $y++) { - if (isset($ins_row[$y]) && $ins_row[$y] == 'on') { + if (isset($criteriaRowInsert[$y]) && $criteriaRowInsert[$y] == 'on') { $chk['or'] = ' checked="checked"'; $chk['and'] = ''; ?> @@ -741,26 +741,26 @@ for ($y = 0; $y <= $row; $y++) { @@ -769,7 +769,7 @@ for ($y = 0; $y <= $row; $y++) { @@ -779,7 +779,7 @@ for ($y = 0; $y <= $row; $y++) {
: - + : - /> + />  
: - + : - /> + />  
: - + : - /> + />
: - + : - /> + />
@@ -849,7 +849,7 @@ for ($y = 0; $y <= $row; $y++) { @@ -859,7 +859,7 @@ for ($y = 0; $y <= $row; $y++) { @@ -931,7 +931,7 @@ echo PMA_dbQbeGetSelectClause($col); // First we need the really needed Tables - those in TableList might still be // all Tables. -if (isset($Field) && count($Field) > 0) { +if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // Initialize some variables $tab_all = array(); $col_all = array(); @@ -942,7 +942,7 @@ if (isset($Field) && count($Field) > 0) { $fromclause = ''; // We only start this if we have fields, otherwise it would be dumb - foreach ($Field as $value) { + foreach ($criteriaColumn as $value) { $parts = explode('.', $value); if (! empty($parts[0]) && ! empty($parts[1])) { $tab_raw = $parts[0]; @@ -959,7 +959,7 @@ if (isset($Field) && count($Field) > 0) { // Now we need all tables that we have in the where clause $crit_cnt = count($criteria); for ($x = 0; $x < $crit_cnt; $x++) { - $curr_tab = explode('.', $Field[$x]); + $curr_tab = explode('.', $criteriaColumn[$x]); if (! empty($curr_tab[0]) && ! empty($curr_tab[1])) { $tab_raw = $curr_tab[0]; $tab = str_replace('`', '', $tab_raw); @@ -1101,7 +1101,7 @@ if (isset($Field) && count($Field) > 0) { $qry_from = $common_functions->backquote($master) . $emerg . $fromclause; } // end if ($cfgRelation['relwork'] && count($tab_all) > 0) -} // end count($Field) > 0 +} // end count($criteriaColumn) > 0 // In case relations are not defined, just generate the FROM clause // from the list of tables, however we don't generate any JOIN @@ -1127,3 +1127,4 @@ echo PMA_dbQbeGetOrderByClause($col); + From cc3fbae9407dd75fa85179d9473b4503291a55a3 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Wed, 11 Jul 2012 22:38:44 +0530 Subject: [PATCH 09/45] Form function to get Ins/Del and AND/OR row cell --- db_qbe.php | 113 ++++++++++++++++++++++++----------------------------- 1 file changed, 52 insertions(+), 61 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 69ea8437a1..32337f267d 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -568,6 +568,53 @@ function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $criteriaAndOrColu return $html_output; } +/** + * Provides Insert/Delete options for criteria inputbox + * with AND/OR relationship modification options + * + * @param integer $row_index Number of criteria row + * @param string $checked If checked + * + * @return HTML + */ +function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked) { + $html_output = ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '' . __('Ins') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '' . __('And') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '' . __('Del') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '' . __('Or') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= ''; + return $html_output; +} + /** * Provides SELECT clause for building SQL query * @@ -735,37 +782,9 @@ for ($y = 0; $y <= $row; $y++) { $chk['and'] = ''; ?> - - - - - - - - - - - - - -
- : - - - : - - /> -   -
- : - - - : - - /> -   -
- + - - - - - - - - - - - - - -
- : - - - : - - /> -
- : - - - : - - /> -
- - Date: Thu, 12 Jul 2012 07:56:53 +0530 Subject: [PATCH 10/45] Form function to get more criteria inputboxes --- db_qbe.php | 125 +++++++++++++++++++++++------------------------------ 1 file changed, 55 insertions(+), 70 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 32337f267d..a9f128e6e5 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -615,6 +615,48 @@ function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked) { return $html_output; } +function PMA_dbQbeGetInputboxRow($col, $new_row_index, $row_index, + $criteriaColumnInsert, $criteriaColumnDelete, $realwidth +) { + $html_output = ''; + $z = 0; + for ($x = 0; $x < $col; $x++) { + if (! empty($criteriaColumnInsert) + && isset($criteriaColumnInsert[$x]) + && $criteriaColumnInsert[$x] == 'on' + ) { + $or = 'Or' . $new_row_index . '[' . $z . ']'; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $z++; + } // end if + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$x]) && $criteriaColumnDelete[$x] == 'on') { + continue; + } + $or = 'Or' . $row_index; + if (! isset(${$or})) { + ${$or} = ''; + } + if (! empty(${$or}) && isset(${$or}[$x])) { + $tmp_or = ${$or}[$x]; + } else { + $tmp_or = ''; + } + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + if (! empty(${$or}) && isset(${$or}[$x])) { + $GLOBALS[${'cur' . $or}][$z] = ${$or}[$x]; + } + $z++; + } // end for + return $html_output; +} + /** * Provides SELECT clause for building SQL query * @@ -781,40 +823,16 @@ for ($y = 0; $y <= $row; $y++) { $chk['or'] = ' checked="checked"'; $chk['and'] = ''; ?> - - - - - - - - - - - - + + + noclick"> - - - - - - - - From 91f04e5660a69d4ed39e279947bd23cbbb11b1b2 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Thu, 12 Jul 2012 08:18:15 +0530 Subject: [PATCH 11/45] Form function for generating AND/OR, INS/DEL criteria inbutbox rows --- db_qbe.php | 123 ++++++++++++++++++++++++++++------------------------- 1 file changed, 64 insertions(+), 59 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index a9f128e6e5..89e273d5c2 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -531,7 +531,7 @@ function PMA_dbQbeGetAndOrColCell($column_number, $selected = null) * @param string $criteria Already Filled criteria * @param string $prev_criteria Previously filled criteria(hidden form field) * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted + * @param string $criteriaColumnDelete If a criteria column is to be deleted * * @return HTML for search table's row */ @@ -657,6 +657,66 @@ function PMA_dbQbeGetInputboxRow($col, $new_row_index, $row_index, return $html_output; } +/** + * Provides rows for criteria inputbox Insert/Delete options + * with AND/OR relationship modification options + * + * @param integer $criteria_row_count Number of criteria rows + * @param array $criteria_column_count Number of criteria columns + * @param string $realwidth Largest column width found + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * @param string $criteriaAndOrRow If AND or OR is to be checked + * + * @return HTML table rows + */ +function PMA_dbQbeGetInsDelAndOrCriteriaRows($criteria_row_count, + $criteria_column_count, $realwidth, $criteriaColumnInsert, $criteriaColumnDelete, + $criteriaAndOrRow +) { + $html_output = ''; + $w = 0; + $odd_row = true; + for ($y = 0; $y <= $criteria_row_count; $y++) { + if (isset($criteriaRowInsert[$y]) && $criteriaRowInsert[$y] == 'on') { + $chk['or'] = ' checked="checked"'; + $chk['and'] = ''; + $html_output .= ''; + $html_output .= PMA_dbQbeGetInsDelAndOrCell($w, $chk); + $html_output .= PMA_dbQbeGetInputboxRow( + $criteria_column_count, $w, $y, $criteriaColumnInsert, + $criteriaColumnDelete, $realwidth + ); + $w++; + $html_output .= ''; + $odd_row =! $odd_row; + } // end if + if (isset($criteriaRowDelete[$y]) && $criteriaRowDelete[$y] == 'on') { + continue; + } + if (isset($criteriaAndOrRow[$y])) { + $GLOBALS['curAndOrRow'][$w] = $criteriaAndOrRow[$y]; + } + if (isset($criteriaAndOrRow[$y]) && $criteriaAndOrRow[$y] == 'and') { + $chk['and'] = ' checked="checked"'; + $chk['or'] = ''; + } else { + $chk['or'] = ' checked="checked"'; + $chk['and'] = ''; + } + $html_output .= ''; + $html_output .= PMA_dbQbeGetInsDelAndOrCell($w, $chk); + $html_output .= PMA_dbQbeGetInputboxRow( + $criteria_column_count, $w, $y, $criteriaColumnInsert, + $criteriaColumnDelete, $realwidth + ); + $w++; + $html_output .= ''; + $odd_row =! $odd_row; + } // end for + return $html_output; +} + /** * Provides SELECT clause for building SQL query * @@ -812,64 +872,9 @@ echo PMA_dbQbegetShowRow( echo PMA_dbQbegetCriteriaInputboxRow( $col, $realwidth, $criteria, $prev_criteria, $criteriaColumnInsert, $criteriaColumnDelete ); -?> - - - - - - - - - - - - - Date: Fri, 13 Jul 2012 07:03:40 +0530 Subject: [PATCH 12/45] Some more variable name improvements --- db_qbe.php | 254 +++++++++++++++++++++++++++-------------------------- 1 file changed, 130 insertions(+), 124 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 89e273d5c2..966f4de61a 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -115,8 +115,8 @@ $criteriaAndOrColumn = isset($_REQUEST['criteriaAndOrColumn']) // minimum width $form_column_width = 12; -$col = max($criteriaColumnCount + $criteriaColumnAdd, 0); -$row = max($rows + $criteriaRowAdd, 0); +$criteria_column_count = max($criteriaColumnCount + $criteriaColumnAdd, 0); +$criteria_row_count = max($rows + $criteriaRowAdd, 0); // The tables list sent by a previously submitted form @@ -244,12 +244,12 @@ function PMA_dbQbegetColumnNamesRow( ) { $html_output = ''; $html_output .= '' . __('Column') . ':'; - $z = 0; + $new_column_count = 0; for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { if (isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= showColumnSelectCell($columns, $z); - $z++; + $html_output .= showColumnSelectCell($columns, $new_column_count); + $new_column_count++; } if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { continue; @@ -257,10 +257,10 @@ function PMA_dbQbegetColumnNamesRow( $selected = ''; if (isset($_REQUEST['criteriaColumn'][$column_index])) { $selected = $_REQUEST['criteriaColumn'][$column_index]; - $GLOBALS['curField'][$z] = $_REQUEST['criteriaColumn'][$column_index]; + $GLOBALS['curField'][$new_column_count] = $_REQUEST['criteriaColumn'][$column_index]; } - $html_output .= showColumnSelectCell($columns, $z, $selected); - $z++; + $html_output .= showColumnSelectCell($columns, $new_column_count, $selected); + $new_column_count++; } // end for $html_output .= ''; return $html_output; @@ -281,12 +281,12 @@ function PMA_dbQbegetSortRow( ) { $html_output = ''; $html_output .= '' . __('Sort') . ':'; - $z = 0; + $new_column_count = 0; for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= getSortSelectCell($z, $realwidth); - $z++; + $html_output .= getSortSelectCell($new_column_count, $realwidth); + $new_column_count++; } // end if if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { @@ -301,22 +301,22 @@ function PMA_dbQbegetSortRow( } //end if // Set asc_selected if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'ASC') { - $GLOBALS['curSort'][$z] = $_REQUEST['criteriaSort'][$column_index]; + $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; $asc_selected = ' selected="selected"'; } else { $asc_selected = ''; } // end if // Set desc selected if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'DESC') { - $GLOBALS['curSort'][$z] = $_REQUEST['criteriaSort'][$column_index]; + $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; $desc_selected = ' selected="selected"'; } else { $desc_selected = ''; } // end if $html_output .= getSortSelectCell( - $z, $realwidth, $asc_selected, $desc_selected + $new_column_count, $realwidth, $asc_selected, $desc_selected ); - $z++; + $new_column_count++; } // end for $html_output .= ''; return $html_output; @@ -336,29 +336,29 @@ function PMA_dbQbegetShowRow( ) { $html_output = ''; $html_output .= '' . __('Show') . ':'; - $z = 0; + $new_column_count = 0; for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { $html_output .= ''; - $html_output .= ''; + $html_output .= ''; $html_output .= ''; - $z++; + $new_column_count++; } // end if if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { continue; } if (isset($_REQUEST['criteriaShow'][$column_index])) { - $checked = ' checked="checked"'; - $GLOBALS['curShow'][$z] = $_REQUEST['criteriaShow'][$column_index]; + $checked_options = ' checked="checked"'; + $GLOBALS['curShow'][$new_column_count] = $_REQUEST['criteriaShow'][$column_index]; } else { - $checked = ''; + $checked_options = ''; } $html_output .= ''; - $html_output .= ''; + $html_output .= ''; $html_output .= ''; - $z++; + $new_column_count++; } // end for $html_output .= ''; return $html_output; @@ -382,16 +382,16 @@ function PMA_dbQbegetCriteriaInputboxRow( ) { $html_output = ''; $html_output .= '' . __('Criteria') . ':'; - $z = 0; + $new_column_count = 0; for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { $html_output .= ''; - $html_output .= ''; $html_output .= ''; - $z++; + $new_column_count++; } // end if if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { continue; @@ -402,18 +402,18 @@ function PMA_dbQbegetCriteriaInputboxRow( if ((empty($prev_criteria) || ! isset($prev_criteria[$column_index])) || $prev_criteria[$column_index] != htmlspecialchars($tmp_criteria) ) { - $GLOBALS['curCriteria'][$z] = $tmp_criteria; + $GLOBALS['curCriteria'][$new_column_count] = $tmp_criteria; } else { - $GLOBALS['curCriteria'][$z] = $prev_criteria[$column_index]; + $GLOBALS['curCriteria'][$new_column_count] = $prev_criteria[$column_index]; } $html_output .= ''; - $html_output .= ''; - $html_output .= ''; + $html_output .= ''; $html_output .= ''; - $z++; + $new_column_count++; } // end for $html_output .= ''; return $html_output; @@ -540,29 +540,32 @@ function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $criteriaAndOrColu ) { $html_output = ''; $html_output .= '' . __('Modify') . ':'; - $z = 0; - for ($x = 0; $x < $criteria_column_count; $x++) { - if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$x]) && $criteriaColumnInsert[$x] == 'on') { - $html_output .= PMA_dbQbeGetAndOrColCell($z); - $z++; + $new_column_count = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if (! empty($criteriaColumnInsert) + && isset($criteriaColumnInsert[$column_index]) + && $criteriaColumnInsert[$column_index] == 'on' + ) { + $html_output .= PMA_dbQbeGetAndOrColCell($new_column_count); + $new_column_count++; } // end if - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$x]) && $criteriaColumnDelete[$x] == 'on') { + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { continue; } - if (isset($criteriaAndOrColumn[$x])) { - $GLOBALS['curAndOrCol'][$z] = $criteriaAndOrColumn[$x]; + if (isset($criteriaAndOrColumn[$column_index])) { + $GLOBALS['curAndOrCol'][$new_column_count] = $criteriaAndOrColumn[$column_index]; } - if (isset($criteriaAndOrColumn[$x]) && $criteriaAndOrColumn[$x] == 'or') { - $chk['or'] = ' checked="checked"'; - $chk['and'] = ''; + if (isset($criteriaAndOrColumn[$column_index]) && $criteriaAndOrColumn[$column_index] == 'or') { + $checked_options['or'] = ' checked="checked"'; + $checked_options['and'] = ''; } else { - $chk['and'] = ' checked="checked"'; - $chk['or'] = ''; + $checked_options['and'] = ' checked="checked"'; + $checked_options['or'] = ''; } - $html_output .= PMA_dbQbeGetAndOrColCell($z, $chk); - $z++; + $html_output .= PMA_dbQbeGetAndOrColCell($new_column_count, $checked_options); + $new_column_count++; } // end for $html_output .= ''; return $html_output; @@ -572,12 +575,12 @@ function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $criteriaAndOrColu * Provides Insert/Delete options for criteria inputbox * with AND/OR relationship modification options * - * @param integer $row_index Number of criteria row - * @param string $checked If checked + * @param integer $row_index Number of criteria row + * @param string $checked_options If checked * * @return HTML */ -function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked) { +function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked_options) { $html_output = ''; $html_output .= ''; $html_output .= ''; @@ -593,7 +596,7 @@ function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked) { $html_output .= ''; $html_output .= ''; $html_output .= ''; @@ -607,7 +610,7 @@ function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked) { $html_output .= ''; $html_output .= ''; $html_output .= ''; $html_output .= '
'; $html_output .= ''; + . $checked_options['and'] . ' />'; $html_output .= '
'; $html_output .= ''; + . ' value="or"' . $checked_options['or'] . ' />'; $html_output .= '
'; @@ -615,44 +618,47 @@ function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked) { return $html_output; } -function PMA_dbQbeGetInputboxRow($col, $new_row_index, $row_index, +function PMA_dbQbeGetInputboxRow($criteria_column_count, $new_row_index, $row_index, $criteriaColumnInsert, $criteriaColumnDelete, $realwidth ) { $html_output = ''; - $z = 0; - for ($x = 0; $x < $col; $x++) { + $new_column_count = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { if (! empty($criteriaColumnInsert) - && isset($criteriaColumnInsert[$x]) - && $criteriaColumnInsert[$x] == 'on' + && isset($criteriaColumnInsert[$column_index]) + && $criteriaColumnInsert[$column_index] == 'on' ) { - $or = 'Or' . $new_row_index . '[' . $z . ']'; + $or = 'Or' . $new_row_index . '[' . $new_column_count . ']'; $html_output .= ''; $html_output .= ''; $html_output .= ''; - $z++; + $new_column_count++; } // end if - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$x]) && $criteriaColumnDelete[$x] == 'on') { + if (! empty($criteriaColumnDelete) + && isset($criteriaColumnDelete[$column_index]) + && $criteriaColumnDelete[$column_index] == 'on' + ) { continue; } $or = 'Or' . $row_index; if (! isset(${$or})) { ${$or} = ''; } - if (! empty(${$or}) && isset(${$or}[$x])) { - $tmp_or = ${$or}[$x]; + if (! empty(${$or}) && isset(${$or}[$column_index])) { + $tmp_or = ${$or}[$column_index]; } else { $tmp_or = ''; } $html_output .= ''; - $html_output .= ''; $html_output .= ''; - if (! empty(${$or}) && isset(${$or}[$x])) { - $GLOBALS[${'cur' . $or}][$z] = ${$or}[$x]; + if (! empty(${$or}) && isset(${$or}[$column_index])) { + $GLOBALS[${'cur' . $or}][$new_column_count] = ${$or}[$column_index]; } - $z++; + $new_column_count++; } // end for return $html_output; } @@ -675,42 +681,42 @@ function PMA_dbQbeGetInsDelAndOrCriteriaRows($criteria_row_count, $criteriaAndOrRow ) { $html_output = ''; - $w = 0; + $new_row_count = 0; $odd_row = true; - for ($y = 0; $y <= $criteria_row_count; $y++) { - if (isset($criteriaRowInsert[$y]) && $criteriaRowInsert[$y] == 'on') { - $chk['or'] = ' checked="checked"'; - $chk['and'] = ''; + for ($row_index = 0; $row_index <= $criteria_row_count; $row_index++) { + if (isset($criteriaRowInsert[$row_index]) && $criteriaRowInsert[$row_index] == 'on') { + $checked_options['or'] = ' checked="checked"'; + $checked_options['and'] = ''; $html_output .= ''; - $html_output .= PMA_dbQbeGetInsDelAndOrCell($w, $chk); + $html_output .= PMA_dbQbeGetInsDelAndOrCell($new_row_count, $checked_options); $html_output .= PMA_dbQbeGetInputboxRow( - $criteria_column_count, $w, $y, $criteriaColumnInsert, + $criteria_column_count, $new_row_count, $row_index, $criteriaColumnInsert, $criteriaColumnDelete, $realwidth ); - $w++; + $new_row_count++; $html_output .= ''; $odd_row =! $odd_row; } // end if - if (isset($criteriaRowDelete[$y]) && $criteriaRowDelete[$y] == 'on') { + if (isset($criteriaRowDelete[$row_index]) && $criteriaRowDelete[$row_index] == 'on') { continue; } - if (isset($criteriaAndOrRow[$y])) { - $GLOBALS['curAndOrRow'][$w] = $criteriaAndOrRow[$y]; + if (isset($criteriaAndOrRow[$row_index])) { + $GLOBALS['curAndOrRow'][$new_row_count] = $criteriaAndOrRow[$row_index]; } - if (isset($criteriaAndOrRow[$y]) && $criteriaAndOrRow[$y] == 'and') { - $chk['and'] = ' checked="checked"'; - $chk['or'] = ''; + if (isset($criteriaAndOrRow[$row_index]) && $criteriaAndOrRow[$row_index] == 'and') { + $checked_options['and'] = ' checked="checked"'; + $checked_options['or'] = ''; } else { - $chk['or'] = ' checked="checked"'; - $chk['and'] = ''; + $checked_options['or'] = ' checked="checked"'; + $checked_options['and'] = ''; } $html_output .= ''; - $html_output .= PMA_dbQbeGetInsDelAndOrCell($w, $chk); + $html_output .= PMA_dbQbeGetInsDelAndOrCell($new_row_count, $checked_options); $html_output .= PMA_dbQbeGetInputboxRow( - $criteria_column_count, $w, $y, $criteriaColumnInsert, + $criteria_column_count, $new_row_count, $row_index, $criteriaColumnInsert, $criteriaColumnDelete, $realwidth ); - $w++; + $new_row_count++; $html_output .= ''; $odd_row =! $odd_row; } // end for @@ -753,42 +759,42 @@ function PMA_dbQbeGetSelectClause($criteria_column_count){ function PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count) { $where_clause = ''; $criteria_cnt = 0; - for ($x = 0; $x < $criteria_column_count; $x++) { - if (! empty($GLOBALS['curField'][$x]) - && ! empty($GLOBALS['curCriteria'][$x]) - && $x + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if (! empty($GLOBALS['curField'][$column_index]) + && ! empty($GLOBALS['curCriteria'][$column_index]) + && $column_index && isset($last_where) && isset($GLOBALS['curAndOrCol'])) { $where_clause .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_where]) . ' '; } - if (! empty($GLOBALS['curField'][$x]) && ! empty($GLOBALS['curCriteria'][$x])) { - $where_clause .= '(' . $GLOBALS['curField'][$x] . ' ' - . $GLOBALS['curCriteria'][$x] . ')'; - $last_where = $x; + if (! empty($GLOBALS['curField'][$column_index]) && ! empty($GLOBALS['curCriteria'][$column_index])) { + $where_clause .= '(' . $GLOBALS['curField'][$column_index] . ' ' + . $GLOBALS['curCriteria'][$column_index] . ')'; + $last_where = $column_index; $criteria_cnt++; } } // end for if ($criteria_cnt > 1) { $where_clause = '(' . $where_clause . ')'; } - // OR rows ${'cur' . $or}[$x] + // OR rows ${'cur' . $or}[$column_index] if (! isset($GLOBALS['curAndOrRow'])) { $GLOBALS['curAndOrRow'] = array(); } - for ($y = 0; $y <= $criteria_row_count; $y++) { + for ($row_index = 0; $row_index <= $criteria_row_count; $row_index++) { $criteria_cnt = 0; $qry_orwhere = ''; $last_orwhere = ''; - for ($x = 0; $x < $criteria_column_count; $x++) { - if (! empty($GLOBALS['curField'][$x]) && ! empty(${'curOr' . $y}[$x]) && $x) { + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if (! empty($GLOBALS['curField'][$column_index]) && ! empty(${'curOr' . $row_index}[$column_index]) && $column_index) { $qry_orwhere .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_orwhere]) . ' '; } - if (! empty($GLOBALS['curField'][$x]) && ! empty(${'curOr' . $y}[$x])) { - $qry_orwhere .= '(' . $GLOBALS['curField'][$x] + if (! empty($GLOBALS['curField'][$column_index]) && ! empty(${'curOr' . $row_index}[$column_index])) { + $qry_orwhere .= '(' . $GLOBALS['curField'][$column_index] . ' ' - . ${'curOr' . $y}[$x] + . ${'curOr' . $row_index}[$column_index] . ')'; - $last_orwhere = $x; + $last_orwhere = $column_index; $criteria_cnt++; } } // end for @@ -797,7 +803,7 @@ function PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count) { } if (! empty($qry_orwhere)) { $where_clause .= "\n" - . strtoupper(isset($GLOBALS['curAndOrRow'][$y]) ? $GLOBALS['curAndOrRow'][$y] . ' ' : '') + . strtoupper(isset($GLOBALS['curAndOrRow'][$row_index]) ? $GLOBALS['curAndOrRow'][$row_index] . ' ' : '') . $qry_orwhere; } // end if } // end for @@ -861,30 +867,30 @@ if ($cfgRelation['designerwork']) {
@@ -903,7 +909,7 @@ echo PMA_dbQbeGetTablesList($tbl_names); dir=""> 0) { if ($cfgRelation['relwork'] && count($tab_all) > 0) { // Now we need all tables that we have in the where clause $crit_cnt = count($criteria); - for ($x = 0; $x < $crit_cnt; $x++) { - $curr_tab = explode('.', $criteriaColumn[$x]); + for ($column_index = 0; $column_index < $crit_cnt; $column_index++) { + $curr_tab = explode('.', $criteriaColumn[$column_index]); if (! empty($curr_tab[0]) && ! empty($curr_tab[1])) { $tab_raw = $curr_tab[0]; $tab = str_replace('`', '', $tab_raw); @@ -950,9 +956,9 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { $col1 = $tab . '.' . $col1; // Now we know that our array has the same numbers as $criteria // we can check which of our columns has a where clause - if (! empty($criteria[$x])) { - if (substr($criteria[$x], 0, 1) == '=' || stristr($criteria[$x], 'is')) { - $col_where[$col] = $col1; + if (! empty($criteria[$column_index])) { + if (substr($criteria[$column_index], 0, 1) == '=' || stristr($criteria[$column_index], 'is')) { + $col_where[$criteria_column_count] = $col1; $tab_wher[$tab] = $tab; } } // end if @@ -1016,13 +1022,13 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // (that would mean that they were also found in the whereclauses // which would be great). if yes, we take only those if ($needsort == 1) { - foreach ($col_cand as $col => $is_where) { - $tab = explode('.', $col); + foreach ($col_cand as $criteria_column_count => $is_where) { + $tab = explode('.', $criteria_column_count); $tab = $tab[0]; if ($is_where == 'Y') { - $vg[$col] = $tab; + $vg[$criteria_column_count] = $tab; } else { - $sg[$col] = $tab; + $sg[$criteria_column_count] = $tab; } } if (isset($vg)) { @@ -1096,10 +1102,10 @@ if (! empty($qry_from)) { } // 3. WHERE -echo PMA_dbQbeGetWhereClause($col, $row); +echo PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count); // 4. ORDER BY -echo PMA_dbQbeGetOrderByClause($col); +echo PMA_dbQbeGetOrderByClause($criteria_column_count); ?> From a83c4c943020dc8117e821c50b07134d98bd9f29 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Fri, 13 Jul 2012 07:12:46 +0530 Subject: [PATCH 13/45] Add missing doc block --- db_qbe.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/db_qbe.php b/db_qbe.php index 966f4de61a..0d2653e3e0 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -618,6 +618,19 @@ function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked_options) { return $html_output; } +/** + * Provides rows for criteria inputbox Insert/Delete options + * with AND/OR relationship modification options + * + * @param array $criteria_column_count Number of criteria columns + * @param integer $new_row_index New row index if rows are added/deleted + * @param integer $row_index Row index + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * @param string $realwidth Largest column width found + * + * @return HTML table rows + */ function PMA_dbQbeGetInputboxRow($criteria_column_count, $new_row_index, $row_index, $criteriaColumnInsert, $criteriaColumnDelete, $realwidth ) { From 5f61e9cc461870b3ff8d1daef2740c9f512c7e51 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Fri, 13 Jul 2012 07:28:09 +0530 Subject: [PATCH 14/45] New library file for db_qbe.php --- db_qbe.php | 684 +------------------------------------- libraries/db_qbe.lib.php | 693 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 694 insertions(+), 683 deletions(-) create mode 100644 libraries/db_qbe.lib.php diff --git a/db_qbe.php b/db_qbe.php index 0d2653e3e0..416a338d67 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -10,7 +10,7 @@ * requirements */ require_once 'libraries/common.inc.php'; - +require_once 'libraries/db_qbe.lib.php'; /** * Sets globals from $_POST */ @@ -173,692 +173,10 @@ PMA_DBI_free_result($tbl_result); // largest width found $realwidth = $form_column_width . 'ex'; - /** * Displays the Query by example form */ -/** - * Provides select options list containing column names - * - * @param array $columns All Column Names - * @param integer $column_number Column Number (0,1,2) or more - * @param string $selected Selected criteria column name - * - * @return HTML for select options - */ -function showColumnSelectCell($columns, $column_number, $selected = '') -{ - $html_output = ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - return $html_output; -} - -/** - * Provides select options list containing sort options (ASC/DESC) - * - * @param integer $column_number Column Number (0,1,2) or more - * @param string $realwidth Largest column width found - * @param string $asc_selected Selected criteria 'Ascending' - * @param string $desc_selected Selected criteria 'Descending' - * - * @return HTML for select options - */ -function getSortSelectCell($column_number, $realwidth, $asc_selected = '', - $desc_selected = '') -{ - $html_output = ''; - $html_output .= ''; - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing column select options - * - * @param array $criteria_column_count Number of criteria columns - * @param integer $columns All column names - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbegetColumnNamesRow( - $criteria_column_count, $columns, $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Column') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - if (isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= showColumnSelectCell($columns, $new_column_count); - $new_column_count++; - } - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - $selected = ''; - if (isset($_REQUEST['criteriaColumn'][$column_index])) { - $selected = $_REQUEST['criteriaColumn'][$column_index]; - $GLOBALS['curField'][$new_column_count] = $_REQUEST['criteriaColumn'][$column_index]; - } - $html_output .= showColumnSelectCell($columns, $new_column_count, $selected); - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing sort(ASC/DESC) select options - * - * @param array $criteria_column_count Number of criteria columns - * @param string $realwidth Largest column width found - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbegetSortRow( - $criteria_column_count, $realwidth, $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Sort') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= getSortSelectCell($new_column_count, $realwidth); - $new_column_count++; - } // end if - - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - // If they have chosen all fields using the * selector, - // then sorting is not available, Fix for Bug #570698 - if (isset($_REQUEST['criteriaSort'][$column_index]) && isset($_REQUEST['criteriaColumn'][$column_index]) - && substr($_REQUEST['criteriaColumn'][$column_index], -2) == '.*' - ) { - $_REQUEST['criteriaSort'][$column_index] = ''; - } //end if - // Set asc_selected - if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'ASC') { - $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; - $asc_selected = ' selected="selected"'; - } else { - $asc_selected = ''; - } // end if - // Set desc selected - if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'DESC') { - $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; - $desc_selected = ' selected="selected"'; - } else { - $desc_selected = ''; - } // end if - $html_output .= getSortSelectCell( - $new_column_count, $realwidth, $asc_selected, $desc_selected - ); - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing SHOW checkboxes - * - * @param array $criteria_column_count Number of criteria columns - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbegetShowRow( - $criteria_column_count, $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Show') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end if - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - if (isset($_REQUEST['criteriaShow'][$column_index])) { - $checked_options = ' checked="checked"'; - $GLOBALS['curShow'][$new_column_count] = $_REQUEST['criteriaShow'][$column_index]; - } else { - $checked_options = ''; - } - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing criteria Inputboxes - * - * @param array $criteria_column_count Number of criteria columns - * @param string $realwidth Largest column width found - * @param string $criteria Already Filled criteria - * @param string $prev_criteria Previously filled criteria(hidden form field) - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbegetCriteriaInputboxRow( - $criteria_column_count, $realwidth, $criteria, $prev_criteria, - $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Criteria') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end if - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - if (isset($criteria[$column_index])) { - $tmp_criteria = $criteria[$column_index]; - } - if ((empty($prev_criteria) || ! isset($prev_criteria[$column_index])) - || $prev_criteria[$column_index] != htmlspecialchars($tmp_criteria) - ) { - $GLOBALS['curCriteria'][$new_column_count] = $tmp_criteria; - } else { - $GLOBALS['curCriteria'][$new_column_count] = $prev_criteria[$column_index]; - } - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides footer options for adding/deleting row/columns - * - * @param string $type Whether row or column - * - * @return HTML for footer options - */ -function PMA_dbQbeGetFootersOptions($type) -{ - $html_output = '
'; - $html_output .= (($type == 'row') - ? __('Add/Delete criteria rows') : __('Add/Delete columns')); - $html_output .= ':'; - $html_output .= '
'; - return $html_output; -} - -/** - * Provides search form table's footer options - * - * @return HTML for table footer - */ -function PMA_dbQbeGetTableFooters() -{ - $html_output = '
'; - $html_output .= PMA_dbQbeGetFootersOptions("row"); - $html_output .= PMA_dbQbeGetFootersOptions("column"); - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '
'; - return $html_output; -} - -/** - * Provides a select list of database tables - * - * @param array $table_names Names of all the tables - * - * @return HTML for table select list - */ -function PMA_dbQbeGetTablesList($table_names) -{ - $html_output = '
'; - $html_output .= '
'; - $html_output .= '' . __('Use Tables') . ''; - // Build the options list for each table name - $options = ''; - $numTableListOptions = 0; - foreach ($table_names as $key => $val) { - $options .= ''; - $numTableListOptions++; - } - $html_output .= ''; - $html_output .= '
'; - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '
'; - return $html_output; -} - -/** - * Provides And/Or modification cell along with Insert/Delete options - * (For modifying search form's table columns) - * - * @param integer $column_number Column Number (0,1,2) or more - * @param array $selected Selected criteria column name - * - * @return HTML for modification cell - */ -function PMA_dbQbeGetAndOrColCell($column_number, $selected = null) -{ - $html_output = ''; - $html_output .= '' . __('Or') . ':'; - $html_output .= ''; - $html_output .= '  ' . __('And') . ':'; - $html_output .= ''; - $html_output .= '
' . __('Ins'); - $html_output .= ''; - $html_output .= '  ' . __('Del'); - $html_output .= ''; - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing column modifications options - * (For modifying search form's table columns) - * - * @param array $criteria_column_count Number of criteria columns - * @param string $realwidth Largest column width found - * @param string $criteria Already Filled criteria - * @param string $prev_criteria Previously filled criteria(hidden form field) - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $criteriaAndOrColumn, - $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Modify') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($criteriaColumnInsert) - && isset($criteriaColumnInsert[$column_index]) - && $criteriaColumnInsert[$column_index] == 'on' - ) { - $html_output .= PMA_dbQbeGetAndOrColCell($new_column_count); - $new_column_count++; - } // end if - - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - - if (isset($criteriaAndOrColumn[$column_index])) { - $GLOBALS['curAndOrCol'][$new_column_count] = $criteriaAndOrColumn[$column_index]; - } - if (isset($criteriaAndOrColumn[$column_index]) && $criteriaAndOrColumn[$column_index] == 'or') { - $checked_options['or'] = ' checked="checked"'; - $checked_options['and'] = ''; - } else { - $checked_options['and'] = ' checked="checked"'; - $checked_options['or'] = ''; - } - $html_output .= PMA_dbQbeGetAndOrColCell($new_column_count, $checked_options); - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides Insert/Delete options for criteria inputbox - * with AND/OR relationship modification options - * - * @param integer $row_index Number of criteria row - * @param string $checked_options If checked - * - * @return HTML - */ -function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked_options) { - $html_output = ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '' . __('Ins') . ':'; - $html_output .= ''; - $html_output .= ''; - $html_output .= '' . __('And') . ':'; - $html_output .= ''; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '' . __('Del') . ':'; - $html_output .= ''; - $html_output .= ''; - $html_output .= '' . __('Or') . ':'; - $html_output .= ''; - $html_output .= ''; - $html_output .= '
'; - $html_output .= ''; - return $html_output; -} - -/** - * Provides rows for criteria inputbox Insert/Delete options - * with AND/OR relationship modification options - * - * @param array $criteria_column_count Number of criteria columns - * @param integer $new_row_index New row index if rows are added/deleted - * @param integer $row_index Row index - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * @param string $realwidth Largest column width found - * - * @return HTML table rows - */ -function PMA_dbQbeGetInputboxRow($criteria_column_count, $new_row_index, $row_index, - $criteriaColumnInsert, $criteriaColumnDelete, $realwidth -) { - $html_output = ''; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($criteriaColumnInsert) - && isset($criteriaColumnInsert[$column_index]) - && $criteriaColumnInsert[$column_index] == 'on' - ) { - $or = 'Or' . $new_row_index . '[' . $new_column_count . ']'; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end if - if (! empty($criteriaColumnDelete) - && isset($criteriaColumnDelete[$column_index]) - && $criteriaColumnDelete[$column_index] == 'on' - ) { - continue; - } - $or = 'Or' . $row_index; - if (! isset(${$or})) { - ${$or} = ''; - } - if (! empty(${$or}) && isset(${$or}[$column_index])) { - $tmp_or = ${$or}[$column_index]; - } else { - $tmp_or = ''; - } - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - if (! empty(${$or}) && isset(${$or}[$column_index])) { - $GLOBALS[${'cur' . $or}][$new_column_count] = ${$or}[$column_index]; - } - $new_column_count++; - } // end for - return $html_output; -} - -/** - * Provides rows for criteria inputbox Insert/Delete options - * with AND/OR relationship modification options - * - * @param integer $criteria_row_count Number of criteria rows - * @param array $criteria_column_count Number of criteria columns - * @param string $realwidth Largest column width found - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * @param string $criteriaAndOrRow If AND or OR is to be checked - * - * @return HTML table rows - */ -function PMA_dbQbeGetInsDelAndOrCriteriaRows($criteria_row_count, - $criteria_column_count, $realwidth, $criteriaColumnInsert, $criteriaColumnDelete, - $criteriaAndOrRow -) { - $html_output = ''; - $new_row_count = 0; - $odd_row = true; - for ($row_index = 0; $row_index <= $criteria_row_count; $row_index++) { - if (isset($criteriaRowInsert[$row_index]) && $criteriaRowInsert[$row_index] == 'on') { - $checked_options['or'] = ' checked="checked"'; - $checked_options['and'] = ''; - $html_output .= ''; - $html_output .= PMA_dbQbeGetInsDelAndOrCell($new_row_count, $checked_options); - $html_output .= PMA_dbQbeGetInputboxRow( - $criteria_column_count, $new_row_count, $row_index, $criteriaColumnInsert, - $criteriaColumnDelete, $realwidth - ); - $new_row_count++; - $html_output .= ''; - $odd_row =! $odd_row; - } // end if - if (isset($criteriaRowDelete[$row_index]) && $criteriaRowDelete[$row_index] == 'on') { - continue; - } - if (isset($criteriaAndOrRow[$row_index])) { - $GLOBALS['curAndOrRow'][$new_row_count] = $criteriaAndOrRow[$row_index]; - } - if (isset($criteriaAndOrRow[$row_index]) && $criteriaAndOrRow[$row_index] == 'and') { - $checked_options['and'] = ' checked="checked"'; - $checked_options['or'] = ''; - } else { - $checked_options['or'] = ' checked="checked"'; - $checked_options['and'] = ''; - } - $html_output .= ''; - $html_output .= PMA_dbQbeGetInsDelAndOrCell($new_row_count, $checked_options); - $html_output .= PMA_dbQbeGetInputboxRow( - $criteria_column_count, $new_row_count, $row_index, $criteriaColumnInsert, - $criteriaColumnDelete, $realwidth - ); - $new_row_count++; - $html_output .= ''; - $odd_row =! $odd_row; - } // end for - return $html_output; -} - -/** - * Provides SELECT clause for building SQL query - * - * @param array $criteria_column_count Number of criteria columns - * - * @return Select clause - */ -function PMA_dbQbeGetSelectClause($criteria_column_count){ - $select_clause = ''; - $select_clauses = array(); - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($GLOBALS['curField'][$column_index]) - && isset($GLOBALS['curShow'][$column_index]) - && $GLOBALS['curShow'][$column_index] == 'on') - { - $select_clauses[] = $GLOBALS['curField'][$column_index]; - } - } // end for - if ($select_clauses) { - $select_clause = 'SELECT ' - . htmlspecialchars(implode(", ", $select_clauses)) . "\n"; - } - return $select_clause; -} - -/** - * Provides WHERE clause for building SQL query - * - * @param array $criteria_column_count Number of criteria columns - * @param array $criteria_row_count Number of criteria rows - * - * @return Where clause - */ -function PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count) { - $where_clause = ''; - $criteria_cnt = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($GLOBALS['curField'][$column_index]) - && ! empty($GLOBALS['curCriteria'][$column_index]) - && $column_index - && isset($last_where) - && isset($GLOBALS['curAndOrCol'])) { - $where_clause .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_where]) . ' '; - } - if (! empty($GLOBALS['curField'][$column_index]) && ! empty($GLOBALS['curCriteria'][$column_index])) { - $where_clause .= '(' . $GLOBALS['curField'][$column_index] . ' ' - . $GLOBALS['curCriteria'][$column_index] . ')'; - $last_where = $column_index; - $criteria_cnt++; - } - } // end for - if ($criteria_cnt > 1) { - $where_clause = '(' . $where_clause . ')'; - } - // OR rows ${'cur' . $or}[$column_index] - if (! isset($GLOBALS['curAndOrRow'])) { - $GLOBALS['curAndOrRow'] = array(); - } - for ($row_index = 0; $row_index <= $criteria_row_count; $row_index++) { - $criteria_cnt = 0; - $qry_orwhere = ''; - $last_orwhere = ''; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($GLOBALS['curField'][$column_index]) && ! empty(${'curOr' . $row_index}[$column_index]) && $column_index) { - $qry_orwhere .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_orwhere]) . ' '; - } - if (! empty($GLOBALS['curField'][$column_index]) && ! empty(${'curOr' . $row_index}[$column_index])) { - $qry_orwhere .= '(' . $GLOBALS['curField'][$column_index] - . ' ' - . ${'curOr' . $row_index}[$column_index] - . ')'; - $last_orwhere = $column_index; - $criteria_cnt++; - } - } // end for - if ($criteria_cnt > 1) { - $qry_orwhere = '(' . $qry_orwhere . ')'; - } - if (! empty($qry_orwhere)) { - $where_clause .= "\n" - . strtoupper(isset($GLOBALS['curAndOrRow'][$row_index]) ? $GLOBALS['curAndOrRow'][$row_index] . ' ' : '') - . $qry_orwhere; - } // end if - } // end for - - if (! empty($where_clause) && $where_clause != '()') { - $where_clause = 'WHERE ' . htmlspecialchars($where_clause) . "\n"; - } // end if - return $where_clause; -} - -/** - * Provides ORDER BY clause for building SQL query - * - * @param array $criteria_column_count Number of criteria columns - * - * @return Order By clause - */ -function PMA_dbQbeGetOrderByClause($criteria_column_count) -{ - $orderby_clause = ''; - $orderby_clauses = array(); - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - // if all columns are chosen with * selector, then sorting isn't available - // Fix for Bug #570698 - if (! empty($GLOBALS['curField'][$column_index]) - && ! empty($GLOBALS['curSort'][$column_index]) - ) { - if (substr($GLOBALS['curField'][$column_index], -2) == '.*') { - continue; - } - $orderby_clauses[] = $GLOBALS['curField'][$column_index] . ' ' - . $GLOBALS['curSort'][$column_index]; - } - } // end for - if ($orderby_clauses) { - $orderby_clause = 'ORDER BY ' - . htmlspecialchars(implode(", ", $orderby_clauses)) . "\n"; - } - return $orderby_clause; -} - if ($cfgRelation['designerwork']) { $url = 'pmd_general.php' . PMA_generate_common_url( array_merge( diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php new file mode 100644 index 0000000000..1e842b8139 --- /dev/null +++ b/libraries/db_qbe.lib.php @@ -0,0 +1,693 @@ +'; + $html_output .= ''; + $html_output .= ''; + return $html_output; +} + +/** + * Provides select options list containing sort options (ASC/DESC) + * + * @param integer $column_number Column Number (0,1,2) or more + * @param string $realwidth Largest column width found + * @param string $asc_selected Selected criteria 'Ascending' + * @param string $desc_selected Selected criteria 'Descending' + * + * @return HTML for select options + */ +function getSortSelectCell($column_number, $realwidth, $asc_selected = '', + $desc_selected = '') +{ + $html_output = ''; + $html_output .= ''; + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing column select options + * + * @param array $criteria_column_count Number of criteria columns + * @param integer $columns All column names + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbegetColumnNamesRow( + $criteria_column_count, $columns, $criteriaColumnInsert = null, $criteriaColumnDelete = null +) { + $html_output = ''; + $html_output .= '' . __('Column') . ':'; + $new_column_count = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + if (isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { + $html_output .= showColumnSelectCell($columns, $new_column_count); + $new_column_count++; + } + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { + continue; + } + $selected = ''; + if (isset($_REQUEST['criteriaColumn'][$column_index])) { + $selected = $_REQUEST['criteriaColumn'][$column_index]; + $GLOBALS['curField'][$new_column_count] = $_REQUEST['criteriaColumn'][$column_index]; + } + $html_output .= showColumnSelectCell($columns, $new_column_count, $selected); + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing sort(ASC/DESC) select options + * + * @param array $criteria_column_count Number of criteria columns + * @param string $realwidth Largest column width found + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbegetSortRow( + $criteria_column_count, $realwidth, $criteriaColumnInsert = null, $criteriaColumnDelete = null +) { + $html_output = ''; + $html_output .= '' . __('Sort') . ':'; + $new_column_count = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { + $html_output .= getSortSelectCell($new_column_count, $realwidth); + $new_column_count++; + } // end if + + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { + continue; + } + // If they have chosen all fields using the * selector, + // then sorting is not available, Fix for Bug #570698 + if (isset($_REQUEST['criteriaSort'][$column_index]) && isset($_REQUEST['criteriaColumn'][$column_index]) + && substr($_REQUEST['criteriaColumn'][$column_index], -2) == '.*' + ) { + $_REQUEST['criteriaSort'][$column_index] = ''; + } //end if + // Set asc_selected + if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'ASC') { + $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; + $asc_selected = ' selected="selected"'; + } else { + $asc_selected = ''; + } // end if + // Set desc selected + if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'DESC') { + $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; + $desc_selected = ' selected="selected"'; + } else { + $desc_selected = ''; + } // end if + $html_output .= getSortSelectCell( + $new_column_count, $realwidth, $asc_selected, $desc_selected + ); + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing SHOW checkboxes + * + * @param array $criteria_column_count Number of criteria columns + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbegetShowRow( + $criteria_column_count, $criteriaColumnInsert = null, $criteriaColumnDelete = null +) { + $html_output = ''; + $html_output .= '' . __('Show') . ':'; + $new_column_count = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $new_column_count++; + } // end if + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { + continue; + } + if (isset($_REQUEST['criteriaShow'][$column_index])) { + $checked_options = ' checked="checked"'; + $GLOBALS['curShow'][$new_column_count] = $_REQUEST['criteriaShow'][$column_index]; + } else { + $checked_options = ''; + } + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing criteria Inputboxes + * + * @param array $criteria_column_count Number of criteria columns + * @param string $realwidth Largest column width found + * @param string $criteria Already Filled criteria + * @param string $prev_criteria Previously filled criteria(hidden form field) + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbegetCriteriaInputboxRow( + $criteria_column_count, $realwidth, $criteria, $prev_criteria, + $criteriaColumnInsert = null, $criteriaColumnDelete = null +) { + $html_output = ''; + $html_output .= '' . __('Criteria') . ':'; + $new_column_count = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $new_column_count++; + } // end if + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { + continue; + } + if (isset($criteria[$column_index])) { + $tmp_criteria = $criteria[$column_index]; + } + if ((empty($prev_criteria) || ! isset($prev_criteria[$column_index])) + || $prev_criteria[$column_index] != htmlspecialchars($tmp_criteria) + ) { + $GLOBALS['curCriteria'][$new_column_count] = $tmp_criteria; + } else { + $GLOBALS['curCriteria'][$new_column_count] = $prev_criteria[$column_index]; + } + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; +} + +/** + * Provides footer options for adding/deleting row/columns + * + * @param string $type Whether row or column + * + * @return HTML for footer options + */ +function PMA_dbQbeGetFootersOptions($type) +{ + $html_output = '
'; + $html_output .= (($type == 'row') + ? __('Add/Delete criteria rows') : __('Add/Delete columns')); + $html_output .= ':'; + $html_output .= '
'; + return $html_output; +} + +/** + * Provides search form table's footer options + * + * @return HTML for table footer + */ +function PMA_dbQbeGetTableFooters() +{ + $html_output = '
'; + $html_output .= PMA_dbQbeGetFootersOptions("row"); + $html_output .= PMA_dbQbeGetFootersOptions("column"); + $html_output .= '
'; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + return $html_output; +} + +/** + * Provides a select list of database tables + * + * @param array $table_names Names of all the tables + * + * @return HTML for table select list + */ +function PMA_dbQbeGetTablesList($table_names) +{ + $html_output = '
'; + $html_output .= '
'; + $html_output .= '' . __('Use Tables') . ''; + // Build the options list for each table name + $options = ''; + $numTableListOptions = 0; + foreach ($table_names as $key => $val) { + $options .= ''; + $numTableListOptions++; + } + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + return $html_output; +} + +/** + * Provides And/Or modification cell along with Insert/Delete options + * (For modifying search form's table columns) + * + * @param integer $column_number Column Number (0,1,2) or more + * @param array $selected Selected criteria column name + * + * @return HTML for modification cell + */ +function PMA_dbQbeGetAndOrColCell($column_number, $selected = null) +{ + $html_output = ''; + $html_output .= '' . __('Or') . ':'; + $html_output .= ''; + $html_output .= '  ' . __('And') . ':'; + $html_output .= ''; + $html_output .= '
' . __('Ins'); + $html_output .= ''; + $html_output .= '  ' . __('Del'); + $html_output .= ''; + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing column modifications options + * (For modifying search form's table columns) + * + * @param array $criteria_column_count Number of criteria columns + * @param string $realwidth Largest column width found + * @param string $criteria Already Filled criteria + * @param string $prev_criteria Previously filled criteria(hidden form field) + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $criteriaAndOrColumn, + $criteriaColumnInsert = null, $criteriaColumnDelete = null +) { + $html_output = ''; + $html_output .= '' . __('Modify') . ':'; + $new_column_count = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if (! empty($criteriaColumnInsert) + && isset($criteriaColumnInsert[$column_index]) + && $criteriaColumnInsert[$column_index] == 'on' + ) { + $html_output .= PMA_dbQbeGetAndOrColCell($new_column_count); + $new_column_count++; + } // end if + + if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { + continue; + } + + if (isset($criteriaAndOrColumn[$column_index])) { + $GLOBALS['curAndOrCol'][$new_column_count] = $criteriaAndOrColumn[$column_index]; + } + if (isset($criteriaAndOrColumn[$column_index]) && $criteriaAndOrColumn[$column_index] == 'or') { + $checked_options['or'] = ' checked="checked"'; + $checked_options['and'] = ''; + } else { + $checked_options['and'] = ' checked="checked"'; + $checked_options['or'] = ''; + } + $html_output .= PMA_dbQbeGetAndOrColCell($new_column_count, $checked_options); + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; +} + +/** + * Provides Insert/Delete options for criteria inputbox + * with AND/OR relationship modification options + * + * @param integer $row_index Number of criteria row + * @param string $checked_options If checked + * + * @return HTML + */ +function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked_options) { + $html_output = ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '' . __('Ins') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '' . __('And') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '' . __('Del') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '' . __('Or') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= ''; + return $html_output; +} + +/** + * Provides rows for criteria inputbox Insert/Delete options + * with AND/OR relationship modification options + * + * @param array $criteria_column_count Number of criteria columns + * @param integer $new_row_index New row index if rows are added/deleted + * @param integer $row_index Row index + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * @param string $realwidth Largest column width found + * + * @return HTML table rows + */ +function PMA_dbQbeGetInputboxRow($criteria_column_count, $new_row_index, $row_index, + $criteriaColumnInsert, $criteriaColumnDelete, $realwidth +) { + $html_output = ''; + $new_column_count = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if (! empty($criteriaColumnInsert) + && isset($criteriaColumnInsert[$column_index]) + && $criteriaColumnInsert[$column_index] == 'on' + ) { + $or = 'Or' . $new_row_index . '[' . $new_column_count . ']'; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $new_column_count++; + } // end if + if (! empty($criteriaColumnDelete) + && isset($criteriaColumnDelete[$column_index]) + && $criteriaColumnDelete[$column_index] == 'on' + ) { + continue; + } + $or = 'Or' . $row_index; + if (! isset(${$or})) { + ${$or} = ''; + } + if (! empty(${$or}) && isset(${$or}[$column_index])) { + $tmp_or = ${$or}[$column_index]; + } else { + $tmp_or = ''; + } + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + if (! empty(${$or}) && isset(${$or}[$column_index])) { + $GLOBALS[${'cur' . $or}][$new_column_count] = ${$or}[$column_index]; + } + $new_column_count++; + } // end for + return $html_output; +} + +/** + * Provides rows for criteria inputbox Insert/Delete options + * with AND/OR relationship modification options + * + * @param integer $criteria_row_count Number of criteria rows + * @param array $criteria_column_count Number of criteria columns + * @param string $realwidth Largest column width found + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * @param string $criteriaAndOrRow If AND or OR is to be checked + * + * @return HTML table rows + */ +function PMA_dbQbeGetInsDelAndOrCriteriaRows($criteria_row_count, + $criteria_column_count, $realwidth, $criteriaColumnInsert, $criteriaColumnDelete, + $criteriaAndOrRow +) { + $html_output = ''; + $new_row_count = 0; + $odd_row = true; + for ($row_index = 0; $row_index <= $criteria_row_count; $row_index++) { + if (isset($criteriaRowInsert[$row_index]) && $criteriaRowInsert[$row_index] == 'on') { + $checked_options['or'] = ' checked="checked"'; + $checked_options['and'] = ''; + $html_output .= ''; + $html_output .= PMA_dbQbeGetInsDelAndOrCell($new_row_count, $checked_options); + $html_output .= PMA_dbQbeGetInputboxRow( + $criteria_column_count, $new_row_count, $row_index, $criteriaColumnInsert, + $criteriaColumnDelete, $realwidth + ); + $new_row_count++; + $html_output .= ''; + $odd_row =! $odd_row; + } // end if + if (isset($criteriaRowDelete[$row_index]) && $criteriaRowDelete[$row_index] == 'on') { + continue; + } + if (isset($criteriaAndOrRow[$row_index])) { + $GLOBALS['curAndOrRow'][$new_row_count] = $criteriaAndOrRow[$row_index]; + } + if (isset($criteriaAndOrRow[$row_index]) && $criteriaAndOrRow[$row_index] == 'and') { + $checked_options['and'] = ' checked="checked"'; + $checked_options['or'] = ''; + } else { + $checked_options['or'] = ' checked="checked"'; + $checked_options['and'] = ''; + } + $html_output .= ''; + $html_output .= PMA_dbQbeGetInsDelAndOrCell($new_row_count, $checked_options); + $html_output .= PMA_dbQbeGetInputboxRow( + $criteria_column_count, $new_row_count, $row_index, $criteriaColumnInsert, + $criteriaColumnDelete, $realwidth + ); + $new_row_count++; + $html_output .= ''; + $odd_row =! $odd_row; + } // end for + return $html_output; +} + +/** + * Provides SELECT clause for building SQL query + * + * @param array $criteria_column_count Number of criteria columns + * + * @return Select clause + */ +function PMA_dbQbeGetSelectClause($criteria_column_count){ + $select_clause = ''; + $select_clauses = array(); + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if (! empty($GLOBALS['curField'][$column_index]) + && isset($GLOBALS['curShow'][$column_index]) + && $GLOBALS['curShow'][$column_index] == 'on') + { + $select_clauses[] = $GLOBALS['curField'][$column_index]; + } + } // end for + if ($select_clauses) { + $select_clause = 'SELECT ' + . htmlspecialchars(implode(", ", $select_clauses)) . "\n"; + } + return $select_clause; +} + +/** + * Provides WHERE clause for building SQL query + * + * @param array $criteria_column_count Number of criteria columns + * @param array $criteria_row_count Number of criteria rows + * + * @return Where clause + */ +function PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count) { + $where_clause = ''; + $criteria_cnt = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if (! empty($GLOBALS['curField'][$column_index]) + && ! empty($GLOBALS['curCriteria'][$column_index]) + && $column_index + && isset($last_where) + && isset($GLOBALS['curAndOrCol'])) { + $where_clause .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_where]) . ' '; + } + if (! empty($GLOBALS['curField'][$column_index]) && ! empty($GLOBALS['curCriteria'][$column_index])) { + $where_clause .= '(' . $GLOBALS['curField'][$column_index] . ' ' + . $GLOBALS['curCriteria'][$column_index] . ')'; + $last_where = $column_index; + $criteria_cnt++; + } + } // end for + if ($criteria_cnt > 1) { + $where_clause = '(' . $where_clause . ')'; + } + // OR rows ${'cur' . $or}[$column_index] + if (! isset($GLOBALS['curAndOrRow'])) { + $GLOBALS['curAndOrRow'] = array(); + } + for ($row_index = 0; $row_index <= $criteria_row_count; $row_index++) { + $criteria_cnt = 0; + $qry_orwhere = ''; + $last_orwhere = ''; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { + if (! empty($GLOBALS['curField'][$column_index]) && ! empty(${'curOr' . $row_index}[$column_index]) && $column_index) { + $qry_orwhere .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_orwhere]) . ' '; + } + if (! empty($GLOBALS['curField'][$column_index]) && ! empty(${'curOr' . $row_index}[$column_index])) { + $qry_orwhere .= '(' . $GLOBALS['curField'][$column_index] + . ' ' + . ${'curOr' . $row_index}[$column_index] + . ')'; + $last_orwhere = $column_index; + $criteria_cnt++; + } + } // end for + if ($criteria_cnt > 1) { + $qry_orwhere = '(' . $qry_orwhere . ')'; + } + if (! empty($qry_orwhere)) { + $where_clause .= "\n" + . strtoupper(isset($GLOBALS['curAndOrRow'][$row_index]) ? $GLOBALS['curAndOrRow'][$row_index] . ' ' : '') + . $qry_orwhere; + } // end if + } // end for + + if (! empty($where_clause) && $where_clause != '()') { + $where_clause = 'WHERE ' . htmlspecialchars($where_clause) . "\n"; + } // end if + return $where_clause; +} + +/** + * Provides ORDER BY clause for building SQL query + * + * @param array $criteria_column_count Number of criteria columns + * + * @return Order By clause + */ +function PMA_dbQbeGetOrderByClause($criteria_column_count) +{ + $orderby_clause = ''; + $orderby_clauses = array(); + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + // if all columns are chosen with * selector, then sorting isn't available + // Fix for Bug #570698 + if (! empty($GLOBALS['curField'][$column_index]) + && ! empty($GLOBALS['curSort'][$column_index]) + ) { + if (substr($GLOBALS['curField'][$column_index], -2) == '.*') { + continue; + } + $orderby_clauses[] = $GLOBALS['curField'][$column_index] . ' ' + . $GLOBALS['curSort'][$column_index]; + } + } // end for + if ($orderby_clauses) { + $orderby_clause = 'ORDER BY ' + . htmlspecialchars(implode(", ", $orderby_clauses)) . "\n"; + } + return $orderby_clause; +} +?> From dc1728f99a50340856b32a6ac1ba2974df2ba715 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sat, 14 Jul 2012 18:54:11 +0530 Subject: [PATCH 15/45] Fix typo introduced before variable name changes --- db_qbe.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 416a338d67..07082b62a2 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -289,7 +289,7 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // we can check which of our columns has a where clause if (! empty($criteria[$column_index])) { if (substr($criteria[$column_index], 0, 1) == '=' || stristr($criteria[$column_index], 'is')) { - $col_where[$criteria_column_count] = $col1; + $col_where[$col1] = $col1; $tab_wher[$tab] = $tab; } } // end if @@ -353,13 +353,13 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // (that would mean that they were also found in the whereclauses // which would be great). if yes, we take only those if ($needsort == 1) { - foreach ($col_cand as $criteria_column_count => $is_where) { - $tab = explode('.', $criteria_column_count); + foreach ($col_cand as $col1 => $is_where) { + $tab = explode('.', $col1); $tab = $tab[0]; if ($is_where == 'Y') { - $vg[$criteria_column_count] = $tab; + $vg[$col1] = $tab; } else { - $sg[$criteria_column_count] = $tab; + $sg[$col1] = $tab; } } if (isset($vg)) { From de318fce3b0578d58399097db8c3e596b641db7b Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sat, 14 Jul 2012 19:56:29 +0530 Subject: [PATCH 16/45] Form functions to get LEFT JOIN column candidates --- db_qbe.php | 63 ++--------------------- libraries/db_qbe.lib.php | 107 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 60 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 07082b62a2..fed91dd01b 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -310,66 +310,9 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // (When the control user is the same as the normal user // because he is using one of his databases as pmadb, // the last db selected is not always the one where we need to work) - PMA_DBI_select_db($db); - - foreach ($tab_all as $tab) { - $indexes = PMA_DBI_get_table_indexes($db, $tab); - foreach ($indexes as $ind) { - $col1 = $tab . '.' . $ind['Column_name']; - if (isset($col_all[$col1])) { - if ($ind['Non_unique'] == 0) { - if (isset($col_where[$col1])) { - $col_unique[$col1] = 'Y'; - } else { - $col_unique[$col1] = 'N'; - } - } else { - if (isset($col_where[$col1])) { - $col_index[$col1] = 'Y'; - } else { - $col_index[$col1] = 'N'; - } - } - } - } // end while (each col of tab) - } // end while (each tab) - // now we want to find the best. - if (isset($col_unique) && count($col_unique) > 0) { - $col_cand = $col_unique; - $needsort = 1; - } elseif (isset($col_index) && count($col_index) > 0) { - $col_cand = $col_index; - $needsort = 1; - } elseif (isset($col_where) && count($col_where) > 0) { - $col_cand = $tab_wher; - $needsort = 0; - } else { - $col_cand = $tab_all; - $needsort = 0; - } - - // If we came up with $col_unique (very good) or $col_index (still - // good) as $col_cand we want to check if we have any 'Y' there - // (that would mean that they were also found in the whereclauses - // which would be great). if yes, we take only those - if ($needsort == 1) { - foreach ($col_cand as $col1 => $is_where) { - $tab = explode('.', $col1); - $tab = $tab[0]; - if ($is_where == 'Y') { - $vg[$col1] = $tab; - } else { - $sg[$col1] = $tab; - } - } - if (isset($vg)) { - $col_cand = $vg; - // Candidates restricted in index+where - } else { - $col_cand = $sg; - // None of the candidates where in a where-clause - } - } + $col_cand = PMA_dbQbeGetLeftJoinColumnCandidates( + $db, $tab_all, $col_all, $col_where + ); // If our array of candidates has more than one member we'll just // find the smallest table. diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index 1e842b8139..90813ccf8a 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -690,4 +690,111 @@ function PMA_dbQbeGetOrderByClause($criteria_column_count) } return $orderby_clause; } + +/** + * Provides UNIQUE columns and INDEX columns present in criteria tables + * + * @param array $db Selected database + * @param array $all_tables Tables involved in the search + * @param array $all_columns Columns involved in the search + * @param array $where_clause_columns Columns having criteria where clause + * + * @return array having UNIQUE and INDEX columns + */ +function PMA_dbQbeGetIndexes($db, $all_tables, $all_columns, $where_clause_columns +) { + $unique_columns = array(); + $index_columns = array(); + + foreach ($all_tables as $table) { + $indexes = PMA_DBI_get_table_indexes($db, $table); + foreach ($indexes as $index) { + $column = $table . '.' . $index['Column_name']; + if (isset($all_columns[$column])) { + if ($index['Non_unique'] == 0) { + if (isset($where_clause_columns[$column])) { + $unique_columns[$column] = 'Y'; + } else { + $unique_columns[$column] = 'N'; + } + } else { + if (isset($where_clause_columns[$column])) { + $index_columns[$column] = 'Y'; + } else { + $index_columns[$column] = 'N'; + } + } + } + } // end while (each index of a table) + } // end while (each table) + + return array( + 'unique' => $unique_columns, + 'index' => $index_columns + ); +} + +/** + * Provides UNIQUE columns and INDEX columns present in criteria tables + * + * @param array $db Selected database + * @param array $all_tables Tables involved in the search + * @param array $all_columns Columns involved in the search + * @param array $where_clause_columns Columns having criteria where clause + * + * @return array having UNIQUE and INDEX columns + */ +function PMA_dbQbeGetLeftJoinColumnCandidates($db, $all_tables, $all_columns, + $where_clause_columns +) { + PMA_DBI_select_db($db); + $candidate_columns = array(); + + // Get unique columns and index columns + $indexes = PMA_dbQbeGetIndexes( + $db, $all_tables, $all_columns, $where_clause_columns + ); + $unique_columns = $indexes['unique']; + $index_columns = $indexes['index']; + + // now we want to find the best. + if (isset($unique_columns) && count($unique_columns) > 0) { + $candidate_columns = $unique_columns; + $needsort = 1; + } elseif (isset($index_columns) && count($index_columns) > 0) { + $candidate_columns = $index_columns; + $needsort = 1; + } elseif (isset($where_clause_columns) && count($where_clause_columns) > 0) { + $candidate_columns = $tab_wher; + $needsort = 0; + } else { + $candidate_columns = $all_tables; + $needsort = 0; + } + + // If we came up with $unique_columns (very good) or $index_columns (still + // good) as $candidate_columns we want to check if we have any 'Y' there + // (that would mean that they were also found in the whereclauses + // which would be great). if yes, we take only those + if ($needsort == 1) { + foreach ($candidate_columns as $column => $is_where) { + $table = explode('.', $column); + $table = $table[0]; + if ($is_where == 'Y') { + $vg[$column] = $table; + } else { + $sg[$column] = $table; + } + } + if (isset($vg)) { + $candidate_columns = $vg; + // Candidates restricted in index+where + } else { + $candidate_columns = $sg; + // None of the candidates where in a where-clause + } + } + + return $candidate_columns; +} ?> From 5668e7f90808318f48b1cc305cebafcd79945850 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 15 Jul 2012 13:26:36 +0530 Subject: [PATCH 17/45] Form function to get the master table --- db_qbe.php | 41 +++---------------------------- libraries/db_qbe.lib.php | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 38 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index fed91dd01b..def2d03b6c 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -300,44 +300,9 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { unset($tab_raw); unset($col_raw); unset($col1); - - if (count($tab_wher) == 1) { - // If there is exactly one column that has a decent where-clause - // we will just use this - $master = key($tab_wher); - } else { - // Now let's find out which of the tables has an index - // (When the control user is the same as the normal user - // because he is using one of his databases as pmadb, - // the last db selected is not always the one where we need to work) - $col_cand = PMA_dbQbeGetLeftJoinColumnCandidates( - $db, $tab_all, $col_all, $col_where - ); - - // If our array of candidates has more than one member we'll just - // find the smallest table. - // Of course the actual query would be faster if we check for - // the Criteria which gives the smallest result set in its table, - // but it would take too much time to check this - if (count($col_cand) > 1) { - // Of course we only want to check each table once - $checked_tables = $col_cand; - foreach ($col_cand as $tab) { - if ($checked_tables[$tab] != 1) { - $tsize[$tab] = PMA_Table::countRecords($db, $tab, false); - $checked_tables[$tab] = 1; - } - $csize[$tab] = $tsize[$tab]; - } - asort($csize); - reset($csize); - $master = key($csize); // Smallest - } else { - reset($col_cand); - $master = current($col_cand); // Only one single candidate - } - } // end if (exactly one where clause) - + $master = PMA_dbQbeGetMasterTable( + $db, $tab_all, $col_all, $col_where, $tab_wher + ); $tab_left = $tab_all; unset($tab_left[$master]); $tab_know[$master] = $master; diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index 90813ccf8a..676e90b96d 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -797,4 +797,57 @@ function PMA_dbQbeGetLeftJoinColumnCandidates($db, $all_tables, $all_columns, return $candidate_columns; } + +/** + * Provides the main table to form the LEFT JOIN clause + * + * @param array $db Selected database + * @param array $all_tables Tables involved in the search + * @param array $all_columns Columns involved in the search + * @param array $where_clause_columns Columns having criteria where clause + * @param array $where_clause_tables Tables having criteria where clause + * + * @return string table name + */ +function PMA_dbQbeGetMasterTable($db, $all_tables, $all_columns, + $where_clause_columns, $where_clause_tables +) { + $master = ''; + if (count($where_clause_tables) == 1) { + // If there is exactly one column that has a decent where-clause + // we will just use this + $master = key($where_clause_tables); + } else { + // Now let's find out which of the tables has an index + // (When the control user is the same as the normal user + // because he is using one of his databases as pmadb, + // the last db selected is not always the one where we need to work) + $candidate_columns = PMA_dbQbeGetLeftJoinColumnCandidates( + $db, $all_tables, $all_columns, $where_clause_columns + ); + // If our array of candidates has more than one member we'll just + // find the smallest table. + // Of course the actual query would be faster if we check for + // the Criteria which gives the smallest result set in its table, + // but it would take too much time to check this + if (count($candidate_columns) > 1) { + // Of course we only want to check each table once + $checked_tables = $candidate_columns; + foreach ($candidate_columns as $table) { + if ($checked_tables[$table] != 1) { + $tsize[$table] = PMA_Table::countRecords($db, $table, false); + $checked_tables[$table] = 1; + } + $csize[$table] = $tsize[$table]; + } + asort($csize); + reset($csize); + $master = key($csize); // Smallest + } else { + reset($candidate_columns); + $master = current($candidate_columns); // Only one single candidate + } + } // end if (exactly one where clause) + return $master; +} ?> From 26c77f3a90cfe066ff4af932420a79b66fe17a45 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 15 Jul 2012 14:47:33 +0530 Subject: [PATCH 18/45] Form function to get valid where clauses --- db_qbe.php | 41 ++++++++++++---------------------------- libraries/db_qbe.lib.php | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index def2d03b6c..771fc6d3e7 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -253,10 +253,8 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // Initialize some variables $tab_all = array(); $col_all = array(); - $tab_wher = array(); $tab_know = array(); $tab_left = array(); - $col_where = array(); $fromclause = ''; // We only start this if we have fields, otherwise it would be dumb @@ -271,37 +269,22 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { $col_all[] = $tab . '.' . str_replace('`', '', $col_raw); } } // end while + // Cleans temp vars w/o further use + unset($tab_raw); + unset($col_raw); + unset($col1); // Check 'where' clauses if ($cfgRelation['relwork'] && count($tab_all) > 0) { - // Now we need all tables that we have in the where clause - $crit_cnt = count($criteria); - for ($column_index = 0; $column_index < $crit_cnt; $column_index++) { - $curr_tab = explode('.', $criteriaColumn[$column_index]); - if (! empty($curr_tab[0]) && ! empty($curr_tab[1])) { - $tab_raw = $curr_tab[0]; - $tab = str_replace('`', '', $tab_raw); - - $col_raw = $curr_tab[1]; - $col1 = str_replace('`', '', $col_raw); - $col1 = $tab . '.' . $col1; - // Now we know that our array has the same numbers as $criteria - // we can check which of our columns has a where clause - if (! empty($criteria[$column_index])) { - if (substr($criteria[$column_index], 0, 1) == '=' || stristr($criteria[$column_index], 'is')) { - $col_where[$col1] = $col1; - $tab_wher[$tab] = $tab; - } - } // end if - } // end if - } // end for - - // Cleans temp vars w/o further use - unset($tab_raw); - unset($col_raw); - unset($col1); + // Get tables and columns with valid where clauses + $valid_where_clauses = getWhereClauseTablesAndColumns( + $criteriaColumn, $criteria + ); + $where_clause_tables = $valid_where_clauses['where_clause_tables']; + $where_clause_columns = $valid_where_clauses['where_clause_columns']; + // Get master table $master = PMA_dbQbeGetMasterTable( - $db, $tab_all, $col_all, $col_where, $tab_wher + $db, $tab_all, $col_all, $where_clause_columns, $where_clause_tables ); $tab_left = $tab_all; unset($tab_left[$master]); diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index 676e90b96d..eeb67426c0 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -850,4 +850,41 @@ function PMA_dbQbeGetMasterTable($db, $all_tables, $all_columns, } // end if (exactly one where clause) return $master; } + +/** + * Provides columns and tables that have valid where clause criteria + * + * @param string $criteriaColumn Selected table.columns + * @param string $criteria Already Filled criteria + * + * @return array + */ +function getWhereClauseTablesAndColumns($criteriaColumn, $criteria) { + $where_clause_columns = array(); + $where_clause_tables = array(); + // Now we need all tables that we have in the where clause + for ($column_index = 0; $column_index < count($criteria); $column_index++) { + $current_table = explode('.', $criteriaColumn[$column_index]); + if (empty($current_table[0]) || empty($current_table[1])) { + continue; + } // end if + $table = str_replace('`', '', $current_table[0]); + $column = str_replace('`', '', $current_table[1]); + $column = $table . '.' . $column; + // Now we know that our array has the same numbers as $criteria + // we can check which of our columns has a where clause + if (! empty($criteria[$column_index])) { + if (substr($criteria[$column_index], 0, 1) == '=' + || stristr($criteria[$column_index], 'is') + ) { + $where_clause_columns[$column] = $column; + $where_clause_tables[$table] = $table; + } + } // end if + } // end for + return array( + 'where_clause_tables' => $where_clause_tables, + 'where_clause_columns' => $where_clause_columns + ); +} ?> From 8a901c4e73246dd63ce0d873ea931c2b80fb0ffa Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 15 Jul 2012 15:17:29 +0530 Subject: [PATCH 19/45] Some variable name improvements --- db_qbe.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 771fc6d3e7..f058f4e55c 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -251,10 +251,10 @@ echo PMA_dbQbeGetSelectClause($criteria_column_count); // all Tables. if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // Initialize some variables - $tab_all = array(); - $col_all = array(); + $all_tables = array(); + $all_columns = array(); $tab_know = array(); - $tab_left = array(); + $left_tables = array(); $fromclause = ''; // We only start this if we have fields, otherwise it would be dumb @@ -262,11 +262,11 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { $parts = explode('.', $value); if (! empty($parts[0]) && ! empty($parts[1])) { $tab_raw = $parts[0]; - $tab = str_replace('`', '', $tab_raw); - $tab_all[$tab] = $tab; + $table = str_replace('`', '', $tab_raw); + $all_tables[$table] = $table; $col_raw = $parts[1]; - $col_all[] = $tab . '.' . str_replace('`', '', $col_raw); + $all_columns[] = $table . '.' . str_replace('`', '', $col_raw); } } // end while // Cleans temp vars w/o further use @@ -275,7 +275,7 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { unset($col1); // Check 'where' clauses - if ($cfgRelation['relwork'] && count($tab_all) > 0) { + if ($cfgRelation['relwork'] && count($all_tables) > 0) { // Get tables and columns with valid where clauses $valid_where_clauses = getWhereClauseTablesAndColumns( $criteriaColumn, $criteria @@ -284,15 +284,15 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { $where_clause_columns = $valid_where_clauses['where_clause_columns']; // Get master table $master = PMA_dbQbeGetMasterTable( - $db, $tab_all, $col_all, $where_clause_columns, $where_clause_tables + $db, $all_tables, $all_columns, $where_clause_columns, $where_clause_tables ); - $tab_left = $tab_all; - unset($tab_left[$master]); + $left_tables = $all_tables; + unset($left_tables[$master]); $tab_know[$master] = $master; $run = 0; $emerg = ''; - while (count($tab_left) > 0) { + while (count($left_tables) > 0) { if ($run % 2 == 0) { PMA_getRelatives('master'); } else { @@ -301,22 +301,22 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { $run++; if ($run > 5) { - foreach ($tab_left as $tab) { - $emerg .= ', ' . $common_functions->backquote($tab); - unset($tab_left[$tab]); + foreach ($left_tables as $table) { + $emerg .= ', ' . $common_functions->backquote($table); + unset($left_tables[$table]); } } } // end while $qry_from = $common_functions->backquote($master) . $emerg . $fromclause; - } // end if ($cfgRelation['relwork'] && count($tab_all) > 0) + } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) } // end count($criteriaColumn) > 0 // In case relations are not defined, just generate the FROM clause // from the list of tables, however we don't generate any JOIN -if (empty($qry_from) && isset($tab_all)) { - $qry_from = implode(', ', $tab_all); +if (empty($qry_from) && isset($all_tables)) { + $qry_from = implode(', ', $all_tables); } // Now let's see what we got if (! empty($qry_from)) { From aea70f57439a3e8b987103ebdfd1635f665e140d Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 15 Jul 2012 15:37:01 +0530 Subject: [PATCH 20/45] Remove some unuseful variables --- db_qbe.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index f058f4e55c..d45f060a57 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -259,20 +259,13 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // We only start this if we have fields, otherwise it would be dumb foreach ($criteriaColumn as $value) { - $parts = explode('.', $value); + $parts = explode('.', $value); if (! empty($parts[0]) && ! empty($parts[1])) { - $tab_raw = $parts[0]; - $table = str_replace('`', '', $tab_raw); + $table = str_replace('`', '', $parts[0]); $all_tables[$table] = $table; - - $col_raw = $parts[1]; - $all_columns[] = $table . '.' . str_replace('`', '', $col_raw); + $all_columns[] = $table . '.' . str_replace('`', '', $parts[1]); } } // end while - // Cleans temp vars w/o further use - unset($tab_raw); - unset($col_raw); - unset($col1); // Check 'where' clauses if ($cfgRelation['relwork'] && count($all_tables) > 0) { From 1623e5581d73c34430d40666d6070ea8f41ea0c8 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 15 Jul 2012 16:12:46 +0530 Subject: [PATCH 21/45] Remove some global variables --- db_qbe.php | 9 ++++----- libraries/relation.lib.php | 24 ++++++++++-------------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index d45f060a57..f11639834e 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -255,7 +255,7 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { $all_columns = array(); $tab_know = array(); $left_tables = array(); - $fromclause = ''; + $left_join = ''; // We only start this if we have fields, otherwise it would be dumb foreach ($criteriaColumn as $value) { @@ -287,20 +287,19 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { $emerg = ''; while (count($left_tables) > 0) { if ($run % 2 == 0) { - PMA_getRelatives('master'); + $left_join .= PMA_getRelatives('master', $left_tables, $tab_know); } else { - PMA_getRelatives('foreign'); + $left_join .= PMA_getRelatives('foreign', $left_tables, $tab_know); } $run++; if ($run > 5) { - foreach ($left_tables as $table) { $emerg .= ', ' . $common_functions->backquote($table); unset($left_tables[$table]); } } } // end while - $qry_from = $common_functions->backquote($master) . $emerg . $fromclause; + $qry_from = $common_functions->backquote($master) . $emerg . $left_join; } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) } // end count($criteriaColumn) > 0 diff --git a/libraries/relation.lib.php b/libraries/relation.lib.php index e1e0975675..64461490b6 100644 --- a/libraries/relation.lib.php +++ b/libraries/relation.lib.php @@ -1161,20 +1161,16 @@ function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filte /** * Finds all related tables * - * @param string $from whether to go from master to foreign or vice versa - * - * @return boolean always true - * - * @global array $tab_left the list of tables that we still couldn't connect - * @global array $tab_know the list of allready connected tables - * @global string $fromclause + * @param string $from Whether to go from master to foreign or vice versa + * @param array $left_tables The list of tables that we still couldn't connect + * @param array $tab_know The list of allready connected tables * + * @return string LEFT JOIN * @access private */ -function PMA_getRelatives($from) +function PMA_getRelatives($from, $left_tables, $tab_know) { - global $tab_left, $tab_know, $fromclause; - + $fromclause = ''; $common_functions = PMA_CommonFunctions::getInstance(); if ($from == 'master') { @@ -1183,7 +1179,7 @@ function PMA_getRelatives($from) $to = 'master'; } $in_know = '(\'' . implode('\', \'', $tab_know) . '\')'; - $in_left = '(\'' . implode('\', \'', $tab_left) . '\')'; + $in_left = '(\'' . implode('\', \'', $left_tables) . '\')'; $rel_query = 'SELECT *' . ' FROM ' . $common_functions->backquote($GLOBALS['cfgRelation']['db']) @@ -1195,7 +1191,7 @@ function PMA_getRelatives($from) $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']); while ($row = PMA_DBI_fetch_assoc($relations)) { $found_table = $row[$to . '_table']; - if (isset($tab_left[$found_table])) { + if (isset($left_tables[$found_table])) { $fromclause .= "\n" . ' LEFT JOIN ' . $common_functions->backquote($GLOBALS['db']) . '.' . $common_functions->backquote($row[$to . '_table']) . ' ON ' @@ -1204,11 +1200,11 @@ function PMA_getRelatives($from) . $common_functions->backquote($row[$to . '_table']) . '.' . $common_functions->backquote($row[$to . '_field']) . ' '; $tab_know[$found_table] = $found_table; - unset($tab_left[$found_table]); + unset($left_tables[$found_table]); } } // end while - return true; + return $fromclause; } // end of the "PMA_getRelatives()" function /** From fd2e73e989bed088e4fda56c73d37520932062dd Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 15 Jul 2012 16:26:34 +0530 Subject: [PATCH 22/45] Form function to get FROM clause --- db_qbe.php | 76 ++-------------------------------------- libraries/db_qbe.lib.php | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index f11639834e..83fdcfff54 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -242,82 +242,12 @@ echo PMA_dbQbeGetTablesList($tbl_names); // 1. SELECT echo PMA_dbQbeGetSelectClause($criteria_column_count); // 2. FROM - -// Create LEFT JOINS out of Relations -// If we can use Relations we could make some left joins. -// First find out if relations are available in this database. - -// First we need the really needed Tables - those in TableList might still be -// all Tables. -if (isset($criteriaColumn) && count($criteriaColumn) > 0) { - // Initialize some variables - $all_tables = array(); - $all_columns = array(); - $tab_know = array(); - $left_tables = array(); - $left_join = ''; - - // We only start this if we have fields, otherwise it would be dumb - foreach ($criteriaColumn as $value) { - $parts = explode('.', $value); - if (! empty($parts[0]) && ! empty($parts[1])) { - $table = str_replace('`', '', $parts[0]); - $all_tables[$table] = $table; - $all_columns[] = $table . '.' . str_replace('`', '', $parts[1]); - } - } // end while - - // Check 'where' clauses - if ($cfgRelation['relwork'] && count($all_tables) > 0) { - // Get tables and columns with valid where clauses - $valid_where_clauses = getWhereClauseTablesAndColumns( - $criteriaColumn, $criteria - ); - $where_clause_tables = $valid_where_clauses['where_clause_tables']; - $where_clause_columns = $valid_where_clauses['where_clause_columns']; - // Get master table - $master = PMA_dbQbeGetMasterTable( - $db, $all_tables, $all_columns, $where_clause_columns, $where_clause_tables - ); - $left_tables = $all_tables; - unset($left_tables[$master]); - $tab_know[$master] = $master; - - $run = 0; - $emerg = ''; - while (count($left_tables) > 0) { - if ($run % 2 == 0) { - $left_join .= PMA_getRelatives('master', $left_tables, $tab_know); - } else { - $left_join .= PMA_getRelatives('foreign', $left_tables, $tab_know); - } - $run++; - if ($run > 5) { - foreach ($left_tables as $table) { - $emerg .= ', ' . $common_functions->backquote($table); - unset($left_tables[$table]); - } - } - } // end while - $qry_from = $common_functions->backquote($master) . $emerg . $left_join; - } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) - -} // end count($criteriaColumn) > 0 - -// In case relations are not defined, just generate the FROM clause -// from the list of tables, however we don't generate any JOIN - -if (empty($qry_from) && isset($all_tables)) { - $qry_from = implode(', ', $all_tables); +$from_clause = PMA_dbQbeGetFromClause($criteriaColumn, $criteria, $cfgRelation); +if (! empty($from_clause)) { + echo 'FROM ' . htmlspecialchars($from_clause) . "\n"; } -// Now let's see what we got -if (! empty($qry_from)) { - echo 'FROM ' . htmlspecialchars($qry_from) . "\n"; -} - // 3. WHERE echo PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count); - // 4. ORDER BY echo PMA_dbQbeGetOrderByClause($criteria_column_count); ?> diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index eeb67426c0..0e5e0f1c9e 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -887,4 +887,74 @@ function getWhereClauseTablesAndColumns($criteriaColumn, $criteria) { 'where_clause_columns' => $where_clause_columns ); } + +/** + * Provides FROM clause for building SQL query + * + * @param string $criteriaColumn Selected table.columns + * @param string $criteria Already Filled criteria + * + * @return FROM clause + */ +function PMA_dbQbeGetFromClause($criteriaColumn, $criteria, $cfgRelation) +{ + $from_clause = ''; + if (isset($criteriaColumn) && count($criteriaColumn) > 0) { + // Initialize some variables + $all_tables = $all_columns = $tab_know = $left_tables = array(); + $left_join = ''; + + // We only start this if we have fields, otherwise it would be dumb + foreach ($criteriaColumn as $value) { + $parts = explode('.', $value); + if (! empty($parts[0]) && ! empty($parts[1])) { + $table = str_replace('`', '', $parts[0]); + $all_tables[$table] = $table; + $all_columns[] = $table . '.' . str_replace('`', '', $parts[1]); + } + } // end while + + // Create LEFT JOINS out of Relations + if ($cfgRelation['relwork'] && count($all_tables) > 0) { + // Get tables and columns with valid where clauses + $valid_where_clauses = getWhereClauseTablesAndColumns( + $criteriaColumn, $criteria + ); + $where_clause_tables = $valid_where_clauses['where_clause_tables']; + $where_clause_columns = $valid_where_clauses['where_clause_columns']; + // Get master table + $master = PMA_dbQbeGetMasterTable( + $db, $all_tables, $all_columns, $where_clause_columns, $where_clause_tables + ); + $left_tables = $all_tables; + unset($left_tables[$master]); + $tab_know[$master] = $master; + + $run = 0; + $emerg = ''; + while (count($left_tables) > 0) { + if ($run % 2 == 0) { + $left_join .= PMA_getRelatives('master', $left_tables, $tab_know); + } else { + $left_join .= PMA_getRelatives('foreign', $left_tables, $tab_know); + } + $run++; + if ($run > 5) { + foreach ($left_tables as $table) { + $emerg .= ', ' . $common_functions->backquote($table); + unset($left_tables[$table]); + } + } + } // end while + $from_clause = $common_functions->backquote($master) . $emerg . $left_join; + } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) + } // end count($criteriaColumn) > 0 + + // In case relations are not defined, just generate the FROM clause + // from the list of tables, however we don't generate any JOIN + if (empty($from_clause) && isset($all_tables)) { + $from_clause = implode(', ', $all_tables); + } + return $from_clause; +} ?> From 867356b0723ced2d316645042353435f0468d702 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 17 Jul 2012 06:49:10 +0530 Subject: [PATCH 23/45] Fix typo --- libraries/db_qbe.lib.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index 0e5e0f1c9e..e8d7109f23 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -941,12 +941,13 @@ function PMA_dbQbeGetFromClause($criteriaColumn, $criteria, $cfgRelation) $run++; if ($run > 5) { foreach ($left_tables as $table) { - $emerg .= ', ' . $common_functions->backquote($table); + $emerg .= ', ' . PMA_CommonFunctions::getInstance()->backquote($table); unset($left_tables[$table]); } } } // end while - $from_clause = $common_functions->backquote($master) . $emerg . $left_join; + $from_clause = PMA_CommonFunctions::getInstance()->backquote($master) + . $emerg . $left_join; } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) } // end count($criteriaColumn) > 0 From ec6ac79775f3930afd99536ff40e43df07ecdae9 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 17 Jul 2012 06:54:14 +0530 Subject: [PATCH 24/45] Some more variable name improvements --- libraries/db_qbe.lib.php | 18 +++++++++--------- libraries/relation.lib.php | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index e8d7109f23..9aaceb64cf 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -901,7 +901,7 @@ function PMA_dbQbeGetFromClause($criteriaColumn, $criteria, $cfgRelation) $from_clause = ''; if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // Initialize some variables - $all_tables = $all_columns = $tab_know = $left_tables = array(); + $all_tables = $all_columns = $known_tables = $remaining_tables = array(); $left_join = ''; // We only start this if we have fields, otherwise it would be dumb @@ -926,23 +926,23 @@ function PMA_dbQbeGetFromClause($criteriaColumn, $criteria, $cfgRelation) $master = PMA_dbQbeGetMasterTable( $db, $all_tables, $all_columns, $where_clause_columns, $where_clause_tables ); - $left_tables = $all_tables; - unset($left_tables[$master]); - $tab_know[$master] = $master; + $remaining_tables = $all_tables; + unset($remaining_tables[$master]); + $known_tables[$master] = $master; $run = 0; $emerg = ''; - while (count($left_tables) > 0) { + while (count($remaining_tables) > 0) { if ($run % 2 == 0) { - $left_join .= PMA_getRelatives('master', $left_tables, $tab_know); + $left_join .= PMA_getRelatives('master', $remaining_tables, $known_tables); } else { - $left_join .= PMA_getRelatives('foreign', $left_tables, $tab_know); + $left_join .= PMA_getRelatives('foreign', $remaining_tables, $known_tables); } $run++; if ($run > 5) { - foreach ($left_tables as $table) { + foreach ($remaining_tables as $table) { $emerg .= ', ' . PMA_CommonFunctions::getInstance()->backquote($table); - unset($left_tables[$table]); + unset($remaining_tables[$table]); } } } // end while diff --git a/libraries/relation.lib.php b/libraries/relation.lib.php index 64461490b6..22f8d7dfd4 100644 --- a/libraries/relation.lib.php +++ b/libraries/relation.lib.php @@ -1161,14 +1161,14 @@ function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filte /** * Finds all related tables * - * @param string $from Whether to go from master to foreign or vice versa - * @param array $left_tables The list of tables that we still couldn't connect - * @param array $tab_know The list of allready connected tables + * @param string $from Whether to go from master to foreign or vice versa + * @param array $remaining_tables The list of tables that we still couldn't connect + * @param array $known_tables The list of allready connected tables * * @return string LEFT JOIN * @access private */ -function PMA_getRelatives($from, $left_tables, $tab_know) +function PMA_getRelatives($from, $remaining_tables, $known_tables) { $fromclause = ''; $common_functions = PMA_CommonFunctions::getInstance(); @@ -1178,8 +1178,8 @@ function PMA_getRelatives($from, $left_tables, $tab_know) } else { $to = 'master'; } - $in_know = '(\'' . implode('\', \'', $tab_know) . '\')'; - $in_left = '(\'' . implode('\', \'', $left_tables) . '\')'; + $in_know = '(\'' . implode('\', \'', $known_tables) . '\')'; + $in_left = '(\'' . implode('\', \'', $remaining_tables) . '\')'; $rel_query = 'SELECT *' . ' FROM ' . $common_functions->backquote($GLOBALS['cfgRelation']['db']) @@ -1191,7 +1191,7 @@ function PMA_getRelatives($from, $left_tables, $tab_know) $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']); while ($row = PMA_DBI_fetch_assoc($relations)) { $found_table = $row[$to . '_table']; - if (isset($left_tables[$found_table])) { + if (isset($remaining_tables[$found_table])) { $fromclause .= "\n" . ' LEFT JOIN ' . $common_functions->backquote($GLOBALS['db']) . '.' . $common_functions->backquote($row[$to . '_table']) . ' ON ' @@ -1199,8 +1199,8 @@ function PMA_getRelatives($from, $left_tables, $tab_know) . $common_functions->backquote($row[$from . '_field']) . ' = ' . $common_functions->backquote($row[$to . '_table']) . '.' . $common_functions->backquote($row[$to . '_field']) . ' '; - $tab_know[$found_table] = $found_table; - unset($left_tables[$found_table]); + $known_tables[$found_table] = $found_table; + unset($remaining_tables[$found_table]); } } // end while From 6f63f54ca94bc2b05063cf34c4b4c951509379c6 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 17 Jul 2012 07:06:57 +0530 Subject: [PATCH 25/45] Remove post_params --- db_qbe.php | 15 +-------------- libraries/db_qbe.lib.php | 18 +++++++----------- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 83fdcfff54..df32aebe3a 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -11,19 +11,6 @@ */ require_once 'libraries/common.inc.php'; require_once 'libraries/db_qbe.lib.php'; -/** - * Sets globals from $_POST - */ -$post_params = array( - 'criteriaColumn', - 'criteriaShow', - 'criteriaSort' -); -foreach ($post_params as $one_post_param) { - if (isset($_POST[$one_post_param])) { - $GLOBALS[$one_post_param] = $_POST[$one_post_param]; - } -} /** * Sets globals from $_POST patterns, for Or* variables @@ -242,7 +229,7 @@ echo PMA_dbQbeGetTablesList($tbl_names); // 1. SELECT echo PMA_dbQbeGetSelectClause($criteria_column_count); // 2. FROM -$from_clause = PMA_dbQbeGetFromClause($criteriaColumn, $criteria, $cfgRelation); +$from_clause = PMA_dbQbeGetFromClause($criteria, $cfgRelation); if (! empty($from_clause)) { echo 'FROM ' . htmlspecialchars($from_clause) . "\n"; } diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index 9aaceb64cf..c5c55cabba 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -854,17 +854,16 @@ function PMA_dbQbeGetMasterTable($db, $all_tables, $all_columns, /** * Provides columns and tables that have valid where clause criteria * - * @param string $criteriaColumn Selected table.columns * @param string $criteria Already Filled criteria * * @return array */ -function getWhereClauseTablesAndColumns($criteriaColumn, $criteria) { +function getWhereClauseTablesAndColumns($criteria) { $where_clause_columns = array(); $where_clause_tables = array(); // Now we need all tables that we have in the where clause for ($column_index = 0; $column_index < count($criteria); $column_index++) { - $current_table = explode('.', $criteriaColumn[$column_index]); + $current_table = explode('.', $_POST['criteriaColumn'][$column_index]); if (empty($current_table[0]) || empty($current_table[1])) { continue; } // end if @@ -891,21 +890,20 @@ function getWhereClauseTablesAndColumns($criteriaColumn, $criteria) { /** * Provides FROM clause for building SQL query * - * @param string $criteriaColumn Selected table.columns * @param string $criteria Already Filled criteria * * @return FROM clause */ -function PMA_dbQbeGetFromClause($criteriaColumn, $criteria, $cfgRelation) +function PMA_dbQbeGetFromClause($criteria, $cfgRelation) { $from_clause = ''; - if (isset($criteriaColumn) && count($criteriaColumn) > 0) { + if (isset($_POST['criteriaColumn']) && count($_POST['criteriaColumn']) > 0) { // Initialize some variables $all_tables = $all_columns = $known_tables = $remaining_tables = array(); $left_join = ''; // We only start this if we have fields, otherwise it would be dumb - foreach ($criteriaColumn as $value) { + foreach ($_POST['criteriaColumn'] as $value) { $parts = explode('.', $value); if (! empty($parts[0]) && ! empty($parts[1])) { $table = str_replace('`', '', $parts[0]); @@ -917,9 +915,7 @@ function PMA_dbQbeGetFromClause($criteriaColumn, $criteria, $cfgRelation) // Create LEFT JOINS out of Relations if ($cfgRelation['relwork'] && count($all_tables) > 0) { // Get tables and columns with valid where clauses - $valid_where_clauses = getWhereClauseTablesAndColumns( - $criteriaColumn, $criteria - ); + $valid_where_clauses = getWhereClauseTablesAndColumns($criteria); $where_clause_tables = $valid_where_clauses['where_clause_tables']; $where_clause_columns = $valid_where_clauses['where_clause_columns']; // Get master table @@ -949,7 +945,7 @@ function PMA_dbQbeGetFromClause($criteriaColumn, $criteria, $cfgRelation) $from_clause = PMA_CommonFunctions::getInstance()->backquote($master) . $emerg . $left_join; } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) - } // end count($criteriaColumn) > 0 + } // end count($_POST['criteriaColumn']) > 0 // In case relations are not defined, just generate the FROM clause // from the list of tables, however we don't generate any JOIN From fdd851c19caac185ebd6df06b507a3a4c0a06f6b Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 17 Jul 2012 07:17:53 +0530 Subject: [PATCH 26/45] Form funcion to get SQL query --- db_qbe.php | 14 +++----------- libraries/db_qbe.lib.php | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index df32aebe3a..cf8287e53d 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -226,17 +226,9 @@ echo PMA_dbQbeGetTablesList($tbl_names); rows=" 30) ? '15' : '7'; ?>" dir=""> diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index c5c55cabba..47b6a48f2e 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -954,4 +954,29 @@ function PMA_dbQbeGetFromClause($criteria, $cfgRelation) } return $from_clause; } + +/** + * Provides the generated SQL query + * + * @param string $criteria Already Filled criteria + * + * @return string SQL query + */ +function PMA_dbQbeGetSQLQuery($criteria_column_count, $criteria_row_count, $criteria, + $cfgRelation +) { + $sql_query = ''; + // get SELECT clause + $sql_query .= PMA_dbQbeGetSelectClause($criteria_column_count); + // get FROM clause + $from_clause = PMA_dbQbeGetFromClause($criteria, $cfgRelation); + if (! empty($from_clause)) { + $sql_query .= 'FROM ' . htmlspecialchars($from_clause) . "\n"; + } + // get WHERE clause + $sql_query .= PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count); + // get ORDER BY clause + $sql_query .= PMA_dbQbeGetOrderByClause($criteria_column_count); + return $sql_query; +} ?> From 748d2e9144610c40a30c67aa466bc9eaa81f2325 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 17 Jul 2012 08:00:49 +0530 Subject: [PATCH 27/45] Form function to generate QBE form --- db_qbe.php | 60 ++--------------------------- libraries/db_qbe.lib.php | 83 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 57 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index cf8287e53d..655e623119 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -179,62 +179,8 @@ if ($cfgRelation['designerwork']) { ) )->display(); } -?> -
-
- - -
- -
- - - -
-
- %s:'), $common_functions->getDbLink($db)); ?> - - -
-
- -
-
-
- diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index 47b6a48f2e..ba890727bd 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -979,4 +979,87 @@ function PMA_dbQbeGetSQLQuery($criteria_column_count, $criteria_row_count, $crit $sql_query .= PMA_dbQbeGetOrderByClause($criteria_column_count); return $sql_query; } + +/** + * Provides the generated QBE form + * + * @param array $db Selected Database + * @param array $tbl_names Number of tables + * @param array $criteria_column_count Number of criteria columns + * @param integer $criteria_row_count Number of criteria rows + * @param string $criteriaColumnInsert If a new criteria column is needed + * @param string $criteriaColumnDelete If a criteria column is to be deleted + * @param string $realwidth Largest column width found + * @param string $criteria Already Filled criteria + * @param string $prev_criteria Previously filled criteria(hidden form field) + * @param string $criteriaAndOrRow If AND or OR is to be checked among rows + * @param string $criteriaAndOrColumn If AND or OR is to be checked among columns + * + * @return string QBE form + */ +function PMA_dbQbeGetSelectionForm($db, $tbl_names, $fld, $criteria_column_count, $criteria_row_count, + $criteriaColumnInsert, $criteriaColumnDelete, $realwidth, $criteria, $prev_criteria, + $criteriaAndOrRow, $criteriaAndOrColumn, $cfgRelation +) { + $html_output = '
'; + $html_output .= '
'; + $html_output .= ''; + // Get table's elements + $html_output .= PMA_dbQbegetColumnNamesRow( + $criteria_column_count, $fld, $criteriaColumnInsert, $criteriaColumnDelete + ); + $html_output .= PMA_dbQbegetSortRow( + $criteria_column_count, $realwidth, $criteriaColumnInsert, $criteriaColumnDelete + ); + $html_output .= PMA_dbQbegetShowRow( + $criteria_column_count, $criteriaColumnInsert, $criteriaColumnDelete + ); + $html_output .= PMA_dbQbegetCriteriaInputboxRow( + $criteria_column_count, $realwidth, $criteria, $prev_criteria, + $criteriaColumnInsert, $criteriaColumnDelete + ); + $html_output .= PMA_dbQbeGetInsDelAndOrCriteriaRows($criteria_row_count, + $criteria_column_count, $realwidth, $criteriaColumnInsert, + $criteriaColumnDelete, $criteriaAndOrRow + ); + $html_output .= PMA_dbQbeGetModifyColumnsRow( + $criteria_column_count, $criteriaAndOrColumn, $criteriaColumnInsert, + $criteriaColumnDelete + ); + $html_output .= '
'; + $new_row_count--; + $url_params['db'] = $db; + $url_params['criteriaColumnCount'] = $new_column_count; + $url_params['rows'] = $new_row_count; + $html_output .= PMA_generate_common_hidden_inputs($url_params); + $html_output .= '
'; + // get footers + $html_output .= PMA_dbQbeGetTableFooters(); + // get tables select list + $html_output .= PMA_dbQbeGetTablesList($tbl_names); + // get SQL query + $html_output .= '
'; + $html_output .= '
'; + $html_output .= '' + . sprintf( + __('SQL query on database %s:'), PMA_CommonFunctions::getInstance()->getDbLink($db) + ); + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + // displays form's footers + $html_output .= '
'; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + $html_output .= '
'; + return $html_output; +} ?> From 0bf05d6b72c5f832e4d30b1dbef5b4244aa65a67 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Wed, 18 Jul 2012 12:58:47 +0530 Subject: [PATCH 28/45] Some remaining variable name improvements --- db_qbe.php | 36 ++++++++++++++++++------------------ libraries/db_qbe.lib.php | 4 ++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 655e623119..b84f3676b7 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -109,7 +109,7 @@ $criteria_row_count = max($rows + $criteriaRowAdd, 0); // The tables list sent by a previously submitted form if (PMA_isValid($_REQUEST['TableList'], 'array')) { foreach ($_REQUEST['TableList'] as $each_table) { - $tbl_names[$each_table] = ' selected="selected"'; + $criteriaTables[$each_table] = ' selected="selected"'; } } // end if @@ -122,40 +122,40 @@ if (PMA_isValid($_REQUEST['TableList'], 'array')) { /** * Prepares the form */ -$tbl_result = PMA_DBI_query( +$all_tables = PMA_DBI_query( 'SHOW TABLES FROM ' . $common_functions->backquote($db) . ';', null, PMA_DBI_QUERY_STORE ); -$tbl_result_cnt = PMA_DBI_num_rows($tbl_result); -if (0 == $tbl_result_cnt) { +$all_tables_count = PMA_DBI_num_rows($all_tables); +if (0 == $all_tables_count) { PMA_Message::error(__('No tables found in database.'))->display(); exit; } // The tables list gets from MySQL -while (list($tbl) = PMA_DBI_fetch_row($tbl_result)) { - $fld_results = PMA_DBI_get_columns($db, $tbl); +while (list($table) = PMA_DBI_fetch_row($all_tables)) { + $columns = PMA_DBI_get_columns($db, $table); - if (empty($tbl_names[$tbl]) && ! empty($_REQUEST['TableList'])) { - $tbl_names[$tbl] = ''; + if (empty($criteriaTables[$table]) && ! empty($_REQUEST['TableList'])) { + $criteriaTables[$table] = ''; } else { - $tbl_names[$tbl] = ' selected="selected"'; + $criteriaTables[$table] = ' selected="selected"'; } // end if // The fields list per selected tables - if ($tbl_names[$tbl] == ' selected="selected"') { - $each_table = $common_functions->backquote($tbl); - $fld[] = $each_table . '.*'; - foreach ($fld_results as $each_field) { - $each_field = $each_table . '.' . $common_functions->backquote($each_field['Field']); - $fld[] = $each_field; + if ($criteriaTables[$table] == ' selected="selected"') { + $each_table = $common_functions->backquote($table); + $columnNames[] = $each_table . '.*'; + foreach ($columns as $each_column) { + $each_column = $each_table . '.' . $common_functions->backquote($each_column['Field']); + $columnNames[] = $each_column; // increase the width if necessary - $form_column_width = max(strlen($each_field), $form_column_width); + $form_column_width = max(strlen($each_column), $form_column_width); } // end foreach } // end if } // end while -PMA_DBI_free_result($tbl_result); +PMA_DBI_free_result($all_tables); // largest width found $realwidth = $form_column_width . 'ex'; @@ -179,7 +179,7 @@ if ($cfgRelation['designerwork']) { ) )->display(); } -echo PMA_dbQbeGetSelectionForm($db, $tbl_names, $fld, $criteria_column_count, $criteria_row_count, +echo PMA_dbQbeGetSelectionForm($db, $criteriaTables, $columnNames, $criteria_column_count, $criteria_row_count, $criteriaColumnInsert, $criteriaColumnDelete, $realwidth, $criteria, $prev_criteria, $criteriaAndOrRow, $criteriaAndOrColumn, $cfgRelation ); diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index ba890727bd..f314ec701f 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -997,7 +997,7 @@ function PMA_dbQbeGetSQLQuery($criteria_column_count, $criteria_row_count, $crit * * @return string QBE form */ -function PMA_dbQbeGetSelectionForm($db, $tbl_names, $fld, $criteria_column_count, $criteria_row_count, +function PMA_dbQbeGetSelectionForm($db, $tbl_names, $columnNames, $criteria_column_count, $criteria_row_count, $criteriaColumnInsert, $criteriaColumnDelete, $realwidth, $criteria, $prev_criteria, $criteriaAndOrRow, $criteriaAndOrColumn, $cfgRelation ) { @@ -1006,7 +1006,7 @@ function PMA_dbQbeGetSelectionForm($db, $tbl_names, $fld, $criteria_column_count $html_output .= ''; // Get table's elements $html_output .= PMA_dbQbegetColumnNamesRow( - $criteria_column_count, $fld, $criteriaColumnInsert, $criteriaColumnDelete + $criteria_column_count, $columnNames, $criteriaColumnInsert, $criteriaColumnDelete ); $html_output .= PMA_dbQbegetSortRow( $criteria_column_count, $realwidth, $criteriaColumnInsert, $criteriaColumnDelete From d4c9646430e4be9b30e92169d634748a470c8f5b Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Wed, 18 Jul 2012 18:09:52 +0000 Subject: [PATCH 29/45] Initiate db_qbe's conversion to OOP --- libraries/DBQbe.class.php | 236 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 libraries/DBQbe.class.php diff --git a/libraries/DBQbe.class.php b/libraries/DBQbe.class.php new file mode 100644 index 0000000000..d90cf577d3 --- /dev/null +++ b/libraries/DBQbe.class.php @@ -0,0 +1,236 @@ +_db = $db; + // Sets criteria parameters + $this->_setSearchParams(); + $this->_setCriteriaTablesAndColumns(); + } + + /** + * Get CommmonFunctions + * + * @return CommonFunctions object + */ + public function getCommonFunctions() + { + if (is_null($this->_common_functions)) { + $this->_common_functions = PMA_CommonFunctions::getInstance(); + } + return $this->_common_functions; + } + + /** + * Sets search parameters + * + */ + private function _setSearchParams() + { + // sets column count + $criteriaColumnCount = PMA_ifSetOr($_REQUEST['criteriaColumnCount'], 3, 'numeric'); + $criteriaColumnAdd = PMA_ifSetOr($_REQUEST['criteriaColumnAdd'], 0, 'numeric'); + $this->_criteria_column_count = max($criteriaColumnCount + $criteriaColumnAdd, 0); + + // sets row count + $rows = PMA_ifSetOr($_REQUEST['rows'], 0, 'numeric'); + $criteriaRowAdd = PMA_ifSetOr($_REQUEST['criteriaRowAdd'], 0, 'numeric'); + $this->_criteria_row_count = max($rows + $criteriaRowAdd, 0); + + $this->_criteriaColumnInsert = PMA_ifSetOr($_REQUEST['criteriaColumnInsert'], null, 'array'); + $this->_criteriaColumnDelete = PMA_ifSetOr($_REQUEST['criteriaColumnDelete'], null, 'array'); + + $this->_prev_criteria = isset($_REQUEST['prev_criteria']) + ? $_REQUEST['prev_criteria'] + : array(); + $this->_criteria = isset($_REQUEST['criteria']) + ? $_REQUEST['criteria'] + : array_fill(0, $criteriaColumnCount, ''); + + $this->_criteriaRowInsert = isset($_REQUEST['criteriaRowInsert']) + ? $_REQUEST['criteriaRowInsert'] + : array_fill(0, $criteriaColumnCount, ''); + $this->_criteriaRowDelete = isset($_REQUEST['criteriaRowDelete']) + ? $_REQUEST['criteriaRowDelete'] + : array_fill(0, $criteriaColumnCount, ''); + $this->_criteriaAndOrRow = isset($_REQUEST['criteriaAndOrRow']) + ? $_REQUEST['criteriaAndOrRow'] + : array_fill(0, $criteriaColumnCount, ''); + $this->_criteriaAndOrColumn = isset($_REQUEST['criteriaAndOrColumn']) + ? $_REQUEST['criteriaAndOrColumn'] + : array_fill(0, $criteriaColumnCount, ''); + // sets minimum width + $this->_form_column_width = 12; + } + + /** + * Sets criteria tables and columns + * + */ + private function _setCriteriaTablesAndColumns() + { + // The tables list sent by a previously submitted form + if (PMA_isValid($_REQUEST['TableList'], 'array')) { + foreach ($_REQUEST['TableList'] as $each_table) { + $this->_criteriaTables[$each_table] = ' selected="selected"'; + } + } // end if + $all_tables = PMA_DBI_query( + 'SHOW TABLES FROM ' . $common_functions->backquote($db) . ';', + null, PMA_DBI_QUERY_STORE + ); + $all_tables_count = PMA_DBI_num_rows($all_tables); + if (0 == $all_tables_count) { + PMA_Message::error(__('No tables found in database.'))->display(); + exit; + } + // The tables list gets from MySQL + while (list($table) = PMA_DBI_fetch_row($all_tables)) { + $columns = PMA_DBI_get_columns($this->_db, $table); + + if (empty($this->_criteriaTables[$table]) && ! empty($_REQUEST['TableList'])) { + $this->_criteriaTables[$table] = ''; + } else { + $this->_criteriaTables[$table] = ' selected="selected"'; + } // end if + + // The fields list per selected tables + if ($this->_criteriaTables[$table] == ' selected="selected"') { + $each_table = $common_functions->backquote($table); + $this->_columnNames[] = $each_table . '.*'; + foreach ($columns as $each_column) { + $each_column = $each_table . '.' . $common_functions->backquote($each_column['Field']); + $this->_columnNames[] = $each_column; + // increase the width if necessary + $this->_form_column_width = max(strlen($each_column), $this->_form_column_width); + } // end foreach + } // end if + } // end while + PMA_DBI_free_result($all_tables); + + // sets the largest width found + $this->_realwidth = $this->_form_column_width . 'ex'; + } +} +?> From f1933b05c76f72b2b2ee0258fb524da50e163d7c Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Thu, 19 Jul 2012 02:09:24 +0000 Subject: [PATCH 30/45] Add existing functions to PMA_DBQbe with required modifications --- libraries/DBQbe.class.php | 1002 +++++++++++++++++++++++++++++++++++++ 1 file changed, 1002 insertions(+) diff --git a/libraries/DBQbe.class.php b/libraries/DBQbe.class.php index d90cf577d3..66910a3823 100644 --- a/libraries/DBQbe.class.php +++ b/libraries/DBQbe.class.php @@ -232,5 +232,1007 @@ class PMA_DbQbe // sets the largest width found $this->_realwidth = $this->_form_column_width . 'ex'; } + /** + * Provides select options list containing column names + * + * @param array $columns All Column Names + * @param integer $column_number Column Number (0,1,2) or more + * @param string $selected Selected criteria column name + * + * @return HTML for select options + */ + private function _showColumnSelectCell($columns, $column_number, $selected = '') + { + $html_output = ''; + $html_output .= ''; + return $html_output; + } + + /** + * Provides select options list containing sort options (ASC/DESC) + * + * @param integer $column_number Column Number (0,1,2) or more + * @param string $asc_selected Selected criteria 'Ascending' + * @param string $desc_selected Selected criteria 'Descending' + * + * @return HTML for select options + */ + private function _getSortSelectCell($column_number, $asc_selected = '', + $desc_selected = '') + { + $html_output = ''; + return $html_output; + } + + /** + * Provides search form's row containing column select options + * + * @param integer $columns All column names + * + * @return HTML for search table's row + */ + private function _getColumnNamesRow($columns) + { + $html_output = ''; + $html_output .= ''; + $new_column_count = 0; + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) + { + if (isset($this->_criteriaColumnInsert[$column_index]) + && $this->_criteriaColumnInsert[$column_index] == 'on' + ) { + $html_output .= showColumnSelectCell($columns, $new_column_count); + $new_column_count++; + } + if (! empty($this->_criteriaColumnDelete) + && isset($this->_criteriaColumnDelete[$column_index]) + && $this->_criteriaColumnDelete[$column_index] == 'on' + ) { + continue; + } + $selected = ''; + if (isset($_REQUEST['criteriaColumn'][$column_index])) { + $selected = $_REQUEST['criteriaColumn'][$column_index]; + $GLOBALS['curField'][$new_column_count] = $_REQUEST['criteriaColumn'][$column_index]; + } + $html_output .= $this->_showColumnSelectCell($columns, $new_column_count, $selected); + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; + } + + /** + * Provides search form's row containing sort(ASC/DESC) select options + * + * @return HTML for search table's row + */ + private function _getSortRow() + { + $html_output = ''; + $html_output .= ''; + $new_column_count = 0; + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) + { + if (! empty($this->_criteriaColumnInsert) + && isset($this->_criteriaColumnInsert[$column_index]) + && $this->_criteriaColumnInsert[$column_index] == 'on' + ) { + $html_output .= $this->_getSortSelectCell($new_column_count); + $new_column_count++; + } // end if + + if (! empty($this->_criteriaColumnDelete) + && isset($this->_criteriaColumnDelete[$column_index]) + && $this->_criteriaColumnDelete[$column_index] == 'on' + ) { + continue; + } + // If they have chosen all fields using the * selector, + // then sorting is not available, Fix for Bug #570698 + if (isset($_REQUEST['criteriaSort'][$column_index]) + && isset($_REQUEST['criteriaColumn'][$column_index]) + && substr($_REQUEST['criteriaColumn'][$column_index], -2) == '.*' + ) { + $_REQUEST['criteriaSort'][$column_index] = ''; + } //end if + // Set asc_selected + if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'ASC') { + $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; + $asc_selected = ' selected="selected"'; + } else { + $asc_selected = ''; + } // end if + // Set desc selected + if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'DESC') { + $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; + $desc_selected = ' selected="selected"'; + } else { + $desc_selected = ''; + } // end if + $html_output .= $this->_getSortSelectCell( + $new_column_count, $asc_selected, $desc_selected + ); + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; + } + + /** + * Provides search form's row containing SHOW checkboxes + * + * @return HTML for search table's row + */ + private function _getShowRow() + { + $html_output = ''; + $html_output .= ''; + $new_column_count = 0; + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) + { + if (! empty($this->_criteriaColumnInsert) + && isset($this->_criteriaColumnInsert[$column_index]) + && $this->_criteriaColumnInsert[$column_index] == 'on' + ) { + $html_output .= ''; + $new_column_count++; + } // end if + if (! empty($this->_criteriaColumnDelete) + && isset($this->_criteriaColumnDelete[$column_index]) + && $this->_criteriaColumnDelete[$column_index] == 'on' + ) { + continue; + } + if (isset($_REQUEST['criteriaShow'][$column_index])) { + $checked_options = ' checked="checked"'; + $GLOBALS['curShow'][$new_column_count] = $_REQUEST['criteriaShow'][$column_index]; + } else { + $checked_options = ''; + } + $html_output .= ''; + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; + } + + /** + * Provides search form's row containing criteria Inputboxes + * + * @return HTML for search table's row + */ + private function _getCriteriaInputboxRow() + { + $html_output = ''; + $html_output .= ''; + $new_column_count = 0; + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) + { + if (! empty($this->_criteriaColumnInsert) + && isset($this->_criteriaColumnInsert[$column_index]) + && $this->_criteriaColumnInsert[$column_index] == 'on' + ) { + $html_output .= ''; + $new_column_count++; + } // end if + if (! empty($this->_criteriaColumnDelete) + && isset($this->_criteriaColumnDelete[$column_index]) + && $this->_criteriaColumnDelete[$column_index] == 'on' + ) { + continue; + } + if (isset($this->_criteria[$column_index])) { + $tmp_criteria = $this->_criteria[$column_index]; + } + if ((empty($this->_prev_criteria) + || ! isset($this->_prev_criteria[$column_index])) + || $this->_prev_criteria[$column_index] != htmlspecialchars($tmp_criteria) + ) { + $GLOBALS['curCriteria'][$new_column_count] = $tmp_criteria; + } else { + $GLOBALS['curCriteria'][$new_column_count] = $this->_prev_criteria[$column_index]; + } + $html_output .= ''; + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; + } + + /** + * Provides footer options for adding/deleting row/columns + * + * @param string $type Whether row or column + * + * @return HTML for footer options + */ + private function _getFootersOptions($type) + { + $html_output = '
'; + $html_output .= (($type == 'row') + ? __('Add/Delete criteria rows') : __('Add/Delete columns')); + $html_output .= ':'; + $html_output .= '
'; + return $html_output; + } + + /** + * Provides search form table's footer options + * + * @return HTML for table footer + */ + private function _getTableFooters() + { + $html_output = '
'; + $html_output .= $this->_getFootersOptions("row"); + $html_output .= $this->_getFootersOptions("column"); + $html_output .= '
'; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + return $html_output; + } + + /** + * Provides a select list of database tables + * + * @return HTML for table select list + */ + private function _getTablesList() + { + $html_output = '
'; + $html_output .= '
'; + $html_output .= '' . __('Use Tables') . ''; + // Build the options list for each table name + $options = ''; + $numTableListOptions = 0; + foreach ($this->_criteriaTables as $key => $val) { + $options .= ''; + $numTableListOptions++; + } + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + return $html_output; + } + + /** + * Provides And/Or modification cell along with Insert/Delete options + * (For modifying search form's table columns) + * + * @param integer $column_number Column Number (0,1,2) or more + * @param array $selected Selected criteria column name + * + * @return HTML for modification cell + */ + private function _getAndOrColCell($column_number, $selected = null) + { + $html_output = ''; + return $html_output; + } + + /** + * Provides search form's row containing column modifications options + * (For modifying search form's table columns) + * + * @return HTML for search table's row + */ + private function _getModifyColumnsRow() + { + $html_output = ''; + $html_output .= ''; + $new_column_count = 0; + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) { + if (! empty($this->_criteriaColumnInsert) + && isset($this->_criteriaColumnInsert[$column_index]) + && $this->_criteriaColumnInsert[$column_index] == 'on' + ) { + $html_output .= $this->_getAndOrColCell($new_column_count); + $new_column_count++; + } // end if + + if (! empty($this->_criteriaColumnDelete) + && isset($this->_criteriaColumnDelete[$column_index]) + && $this->_criteriaColumnDelete[$column_index] == 'on' + ) { + continue; + } + + if (isset($this->_criteriaAndOrColumn[$column_index])) { + $GLOBALS['curAndOrCol'][$new_column_count] = $this->_criteriaAndOrColumn[$column_index]; + } + if (isset($this->_criteriaAndOrColumn[$column_index]) + && $this->_criteriaAndOrColumn[$column_index] == 'or' + ) { + $checked_options['or'] = ' checked="checked"'; + $checked_options['and'] = ''; + } else { + $checked_options['and'] = ' checked="checked"'; + $checked_options['or'] = ''; + } + $html_output .= $this->_getAndOrColCell($new_column_count, $checked_options); + $new_column_count++; + } // end for + $html_output .= ''; + return $html_output; + } + + /** + * Provides Insert/Delete options for criteria inputbox + * with AND/OR relationship modification options + * + * @param integer $row_index Number of criteria row + * @param string $checked_options If checked + * + * @return HTML + */ + private function _getInsDelAndOrCell($row_index, $checked_options) { + $html_output = ''; + return $html_output; + } + + /** + * Provides rows for criteria inputbox Insert/Delete options + * with AND/OR relationship modification options + * + * @param integer $new_row_index New row index if rows are added/deleted + * @param integer $row_index Row index + * + * @return HTML table rows + */ + private function _getInputboxRow($new_row_index, $row_index) + { + $html_output = ''; + $new_column_count = 0; + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) { + if (! empty($this->_criteriaColumnInsert) + && isset($this->_criteriaColumnInsert[$column_index]) + && $this->_criteriaColumnInsert[$column_index] == 'on' + ) { + $or = 'Or' . $new_row_index . '[' . $new_column_count . ']'; + $html_output .= ''; + $new_column_count++; + } // end if + if (! empty($this->_criteriaColumnDelete) + && isset($this->_criteriaColumnDelete[$column_index]) + && $this->_criteriaColumnDelete[$column_index] == 'on' + ) { + continue; + } + $or = 'Or' . $row_index; + if (! isset(${$or})) { + ${$or} = ''; + } + if (! empty(${$or}) && isset(${$or}[$column_index])) { + $tmp_or = ${$or}[$column_index]; + } else { + $tmp_or = ''; + } + $html_output .= ''; + if (! empty(${$or}) && isset(${$or}[$column_index])) { + $GLOBALS[${'cur' . $or}][$new_column_count] = ${$or}[$column_index]; + } + $new_column_count++; + } // end for + return $html_output; + } + + /** + * Provides rows for criteria inputbox Insert/Delete options + * with AND/OR relationship modification options + * + * @return HTML table rows + */ + private function _getInsDelAndOrCriteriaRows() + { + $html_output = ''; + $new_row_count = 0; + $odd_row = true; + for ($row_index = 0; $row_index <= $this->_criteria_row_count; $row_index++) { + if (isset($this->_criteriaRowInsert[$row_index]) + && $this->_criteriaRowInsert[$row_index] == 'on' + ) { + $checked_options['or'] = ' checked="checked"'; + $checked_options['and'] = ''; + $html_output .= ''; + $html_output .= $this->_getInsDelAndOrCell( + $new_row_count, $checked_options + ); + $html_output .= $this->_getInputboxRow( + $new_row_count, $row_index + ); + $new_row_count++; + $html_output .= ''; + $odd_row =! $odd_row; + } // end if + if (isset($this->_criteriaRowDelete[$row_index]) + && $this->_criteriaRowDelete[$row_index] == 'on' + ) { + continue; + } + if (isset($this->_criteriaAndOrRow[$row_index])) { + $GLOBALS['curAndOrRow'][$new_row_count] = $this->_criteriaAndOrRow[$row_index]; + } + if (isset($this->_criteriaAndOrRow[$row_index]) + && $this->_criteriaAndOrRow[$row_index] == 'and' + ) { + $checked_options['and'] = ' checked="checked"'; + $checked_options['or'] = ''; + } else { + $checked_options['or'] = ' checked="checked"'; + $checked_options['and'] = ''; + } + $html_output .= ''; + $html_output .= $this->_getInsDelAndOrCell( + $new_row_count, $checked_options + ); + $html_output .= $this->_getInputboxRow( + $new_row_count, $row_index + ); + $new_row_count++; + $html_output .= ''; + $odd_row =! $odd_row; + } // end for + return $html_output; + } + + /** + * Provides SELECT clause for building SQL query + * + * @return Select clause + */ + private function _getSelectClause(){ + $select_clause = ''; + $select_clauses = array(); + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) { + if (! empty($GLOBALS['curField'][$column_index]) + && isset($GLOBALS['curShow'][$column_index]) + && $GLOBALS['curShow'][$column_index] == 'on') + { + $select_clauses[] = $GLOBALS['curField'][$column_index]; + } + } // end for + if ($select_clauses) { + $select_clause = 'SELECT ' + . htmlspecialchars(implode(", ", $select_clauses)) . "\n"; + } + return $select_clause; + } + + /** + * Provides WHERE clause for building SQL query + * + * @return Where clause + */ + private function _getWhereClause() { + $where_clause = ''; + $criteria_cnt = 0; + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) { + if (! empty($GLOBALS['curField'][$column_index]) + && ! empty($GLOBALS['curCriteria'][$column_index]) + && $column_index + && isset($last_where) + && isset($GLOBALS['curAndOrCol'])) { + $where_clause .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_where]) . ' '; + } + if (! empty($GLOBALS['curField'][$column_index]) && ! empty($GLOBALS['curCriteria'][$column_index])) { + $where_clause .= '(' . $GLOBALS['curField'][$column_index] . ' ' + . $GLOBALS['curCriteria'][$column_index] . ')'; + $last_where = $column_index; + $criteria_cnt++; + } + } // end for + if ($criteria_cnt > 1) { + $where_clause = '(' . $where_clause . ')'; + } + // OR rows ${'cur' . $or}[$column_index] + if (! isset($GLOBALS['curAndOrRow'])) { + $GLOBALS['curAndOrRow'] = array(); + } + for ($row_index = 0; $row_index <= $this->_criteria_row_count; $row_index++) { + $criteria_cnt = 0; + $qry_orwhere = ''; + $last_orwhere = ''; + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) { + if (! empty($GLOBALS['curField'][$column_index]) + && ! empty(${'curOr' . $row_index}[$column_index]) + && $column_index + ) { + $qry_orwhere .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_orwhere]) . ' '; + } + if (! empty($GLOBALS['curField'][$column_index]) + && ! empty(${'curOr' . $row_index}[$column_index]) + ) { + $qry_orwhere .= '(' . $GLOBALS['curField'][$column_index] + . ' ' + . ${'curOr' . $row_index}[$column_index] + . ')'; + $last_orwhere = $column_index; + $criteria_cnt++; + } + } // end for + if ($criteria_cnt > 1) { + $qry_orwhere = '(' . $qry_orwhere . ')'; + } + if (! empty($qry_orwhere)) { + $where_clause .= "\n" + . strtoupper(isset($GLOBALS['curAndOrRow'][$row_index]) ? $GLOBALS['curAndOrRow'][$row_index] . ' ' : '') + . $qry_orwhere; + } // end if + } // end for + + if (! empty($where_clause) && $where_clause != '()') { + $where_clause = 'WHERE ' . htmlspecialchars($where_clause) . "\n"; + } // end if + return $where_clause; + } + + /** + * Provides ORDER BY clause for building SQL query + * + * @return Order By clause + */ + private function _getOrderByClause() + { + $orderby_clause = ''; + $orderby_clauses = array(); + for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) + { + // if all columns are chosen with * selector, then sorting isn't available + // Fix for Bug #570698 + if (! empty($GLOBALS['curField'][$column_index]) + && ! empty($GLOBALS['curSort'][$column_index]) + ) { + if (substr($GLOBALS['curField'][$column_index], -2) == '.*') { + continue; + } + $orderby_clauses[] = $GLOBALS['curField'][$column_index] . ' ' + . $GLOBALS['curSort'][$column_index]; + } + } // end for + if ($orderby_clauses) { + $orderby_clause = 'ORDER BY ' + . htmlspecialchars(implode(", ", $orderby_clauses)) . "\n"; + } + return $orderby_clause; + } + + /** + * Provides UNIQUE columns and INDEX columns present in criteria tables + * + * @param array $all_tables Tables involved in the search + * @param array $all_columns Columns involved in the search + * @param array $where_clause_columns Columns having criteria where clause + * + * @return array having UNIQUE and INDEX columns + */ + private function _getIndexes($all_tables, $all_columns, + $where_clause_columns + ) { + $unique_columns = array(); + $index_columns = array(); + + foreach ($all_tables as $table) { + $indexes = PMA_DBI_get_table_indexes($this->_db, $table); + foreach ($indexes as $index) { + $column = $table . '.' . $index['Column_name']; + if (isset($all_columns[$column])) { + if ($index['Non_unique'] == 0) { + if (isset($where_clause_columns[$column])) { + $unique_columns[$column] = 'Y'; + } else { + $unique_columns[$column] = 'N'; + } + } else { + if (isset($where_clause_columns[$column])) { + $index_columns[$column] = 'Y'; + } else { + $index_columns[$column] = 'N'; + } + } + } + } // end while (each index of a table) + } // end while (each table) + + return array( + 'unique' => $unique_columns, + 'index' => $index_columns + ); + } + + /** + * Provides UNIQUE columns and INDEX columns present in criteria tables + * + * @param array $all_tables Tables involved in the search + * @param array $all_columns Columns involved in the search + * @param array $where_clause_columns Columns having criteria where clause + * + * @return array having UNIQUE and INDEX columns + */ + private function _getLeftJoinColumnCandidates($all_tables, $all_columns, + $where_clause_columns + ) { + PMA_DBI_select_db($this->_db); + $candidate_columns = array(); + + // Get unique columns and index columns + $indexes = $this->_getIndexes( + $all_tables, $all_columns, $where_clause_columns + ); + $unique_columns = $indexes['unique']; + $index_columns = $indexes['index']; + + // now we want to find the best. + if (isset($unique_columns) && count($unique_columns) > 0) { + $candidate_columns = $unique_columns; + $needsort = 1; + } elseif (isset($index_columns) && count($index_columns) > 0) { + $candidate_columns = $index_columns; + $needsort = 1; + } elseif (isset($where_clause_columns) && count($where_clause_columns) > 0) { + $candidate_columns = $tab_wher; + $needsort = 0; + } else { + $candidate_columns = $all_tables; + $needsort = 0; + } + + // If we came up with $unique_columns (very good) or $index_columns (still + // good) as $candidate_columns we want to check if we have any 'Y' there + // (that would mean that they were also found in the whereclauses + // which would be great). if yes, we take only those + if ($needsort == 1) { + foreach ($candidate_columns as $column => $is_where) { + $table = explode('.', $column); + $table = $table[0]; + if ($is_where == 'Y') { + $vg[$column] = $table; + } else { + $sg[$column] = $table; + } + } + if (isset($vg)) { + $candidate_columns = $vg; + // Candidates restricted in index+where + } else { + $candidate_columns = $sg; + // None of the candidates where in a where-clause + } + } + + return $candidate_columns; + } + + /** + * Provides the main table to form the LEFT JOIN clause + * + * @param array $all_tables Tables involved in the search + * @param array $all_columns Columns involved in the search + * @param array $where_clause_columns Columns having criteria where clause + * @param array $where_clause_tables Tables having criteria where clause + * + * @return string table name + */ + private function _getMasterTable($all_tables, $all_columns, + $where_clause_columns, $where_clause_tables + ) { + $master = ''; + if (count($where_clause_tables) == 1) { + // If there is exactly one column that has a decent where-clause + // we will just use this + $master = key($where_clause_tables); + } else { + // Now let's find out which of the tables has an index + // (When the control user is the same as the normal user + // because he is using one of his databases as pmadb, + // the last db selected is not always the one where we need to work) + $candidate_columns = $this->_getLeftJoinColumnCandidates( + $all_tables, $all_columns, $where_clause_columns + ); + // If our array of candidates has more than one member we'll just + // find the smallest table. + // Of course the actual query would be faster if we check for + // the Criteria which gives the smallest result set in its table, + // but it would take too much time to check this + if (count($candidate_columns) > 1) { + // Of course we only want to check each table once + $checked_tables = $candidate_columns; + foreach ($candidate_columns as $table) { + if ($checked_tables[$table] != 1) { + $tsize[$table] = PMA_Table::countRecords($db, $table, false); + $checked_tables[$table] = 1; + } + $csize[$table] = $tsize[$table]; + } + asort($csize); + reset($csize); + $master = key($csize); // Smallest + } else { + reset($candidate_columns); + $master = current($candidate_columns); // Only one single candidate + } + } // end if (exactly one where clause) + return $master; + } + + /** + * Provides columns and tables that have valid where clause criteria + * + * @return array + */ + private function _getWhereClauseTablesAndColumns() + { + $where_clause_columns = array(); + $where_clause_tables = array(); + // Now we need all tables that we have in the where clause + for ($column_index = 0; $column_index < count($this->_criteria); $column_index++) { + $current_table = explode('.', $_POST['criteriaColumn'][$column_index]); + if (empty($current_table[0]) || empty($current_table[1])) { + continue; + } // end if + $table = str_replace('`', '', $current_table[0]); + $column = str_replace('`', '', $current_table[1]); + $column = $table . '.' . $column; + // Now we know that our array has the same numbers as $criteria + // we can check which of our columns has a where clause + if (! empty($this->_criteria[$column_index])) { + if (substr($this->_criteria[$column_index], 0, 1) == '=' + || stristr($this->_criteria[$column_index], 'is') + ) { + $where_clause_columns[$column] = $column; + $where_clause_tables[$table] = $table; + } + } // end if + } // end for + return array( + 'where_clause_tables' => $where_clause_tables, + 'where_clause_columns' => $where_clause_columns + ); + } + + /** + * Provides FROM clause for building SQL query + * + * @param string $cfgRelation Relation Settings + * + * @return FROM clause + */ + private function _getFromClause($cfgRelation) + { + $from_clause = ''; + if (isset($_POST['criteriaColumn']) && count($_POST['criteriaColumn']) > 0) { + // Initialize some variables + $all_tables = $all_columns = $known_tables = $remaining_tables = array(); + $left_join = ''; + + // We only start this if we have fields, otherwise it would be dumb + foreach ($_POST['criteriaColumn'] as $value) { + $parts = explode('.', $value); + if (! empty($parts[0]) && ! empty($parts[1])) { + $table = str_replace('`', '', $parts[0]); + $all_tables[$table] = $table; + $all_columns[] = $table . '.' . str_replace('`', '', $parts[1]); + } + } // end while + + // Create LEFT JOINS out of Relations + if ($cfgRelation['relwork'] && count($all_tables) > 0) { + // Get tables and columns with valid where clauses + $valid_where_clauses = $this->_getWhereClauseTablesAndColumns($this->_criteria); + $where_clause_tables = $valid_where_clauses['where_clause_tables']; + $where_clause_columns = $valid_where_clauses['where_clause_columns']; + // Get master table + $master = $this->_getMasterTable( + $all_tables, $all_columns, $where_clause_columns, $where_clause_tables + ); + $remaining_tables = $all_tables; + unset($remaining_tables[$master]); + $known_tables[$master] = $master; + + $run = 0; + $emerg = ''; + while (count($remaining_tables) > 0) { + if ($run % 2 == 0) { + $left_join .= PMA_getRelatives('master', $remaining_tables, $known_tables); + } else { + $left_join .= PMA_getRelatives('foreign', $remaining_tables, $known_tables); + } + $run++; + if ($run > 5) { + foreach ($remaining_tables as $table) { + $emerg .= ', ' . PMA_CommonFunctions::getInstance()->backquote($table); + unset($remaining_tables[$table]); + } + } + } // end while + $from_clause = PMA_CommonFunctions::getInstance()->backquote($master) + . $emerg . $left_join; + } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) + } // end count($_POST['criteriaColumn']) > 0 + + // In case relations are not defined, just generate the FROM clause + // from the list of tables, however we don't generate any JOIN + if (empty($from_clause) && isset($all_tables)) { + $from_clause = implode(', ', $all_tables); + } + return $from_clause; + } + + /** + * Provides the generated SQL query + * + * @return string SQL query + */ + private function _getSQLQuery($cfgRelation) + { + $sql_query = ''; + // get SELECT clause + $sql_query .= $this->_getSelectClause(); + // get FROM clause + $from_clause = $this->_getFromClause($cfgRelation); + if (! empty($from_clause)) { + $sql_query .= 'FROM ' . htmlspecialchars($from_clause) . "\n"; + } + // get WHERE clause + $sql_query .= $this->_getWhereClause(); + // get ORDER BY clause + $sql_query .= $this->_getOrderByClause(); + return $sql_query; + } + + /** + * Provides the generated QBE form + * + * @return string QBE form + */ + public function getSelectionForm() + { + $html_output = ''; + $html_output .= '
'; + $html_output .= '
'; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
' . __('Column') . ':
' . __('Sort') . ':
' . __('Show') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
' . __('Criteria') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '' . __('Or') . ':'; + $html_output .= ''; + $html_output .= '  ' . __('And') . ':'; + $html_output .= ''; + $html_output .= '
' . __('Ins'); + $html_output .= ''; + $html_output .= '  ' . __('Del'); + $html_output .= ''; + $html_output .= '
' . __('Modify') . ':
'; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '' . __('Ins') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '' . __('And') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '' . __('Del') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '' . __('Or') . ':'; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + // Get table's elements + $html_output .= $this->_getColumnNamesRow(); + $html_output .= $this->_getSortRow(); + $html_output .= $this->_getShowRow(); + $html_output .= $this->_getCriteriaInputboxRow(); + $html_output .= $this->_getInsDelAndOrCriteriaRows(); + $html_output .= $this->_getModifyColumnsRow(); + $html_output .= '
'; + $new_row_count--; + $url_params['db'] = $this->_db; + $url_params['criteriaColumnCount'] = $new_column_count; + $url_params['rows'] = $new_row_count; + $html_output .= PMA_generate_common_hidden_inputs($url_params); + $html_output .= ''; + // get footers + $html_output .= $this->_getTableFooters(); + // get tables select list + $html_output .= $this->_getTablesList(); + // get SQL query + $html_output .= '
'; + $html_output .= '
'; + $html_output .= '' + . sprintf( + __('SQL query on database %s:'), PMA_CommonFunctions::getInstance()->getDbLink($db) + ); + $html_output .= ''; + $html_output .= ''; + $html_output .= '
'; + // displays form's footers + $html_output .= '
'; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + $html_output .= ''; + return $html_output; + } } ?> From 08b51a1e3baa8ddbbf81a2a753a30899ddf0623e Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Thu, 19 Jul 2012 02:52:18 +0000 Subject: [PATCH 31/45] Add some more variables to PMA_DBQbe --- libraries/DBQbe.class.php | 105 ++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 32 deletions(-) diff --git a/libraries/DBQbe.class.php b/libraries/DBQbe.class.php index 66910a3823..1763479465 100644 --- a/libraries/DBQbe.class.php +++ b/libraries/DBQbe.class.php @@ -114,6 +114,41 @@ class PMA_DbQbe * @var string */ private $_form_column_width; + /** + * Current criteria field + * + * @access private + * @var array + */ + private $_curField; + /** + * Current criteria Sort options + * + * @access private + * @var array + */ + private $_curSort; + /** + * Current criteria Show options + * + * @access private + * @var array + */ + private $_curShow; + /** + * Current criteria values + * + * @access private + * @var array + */ + private $_curCriteria; + /** + * Current criteria AND/OR column realtions + * + * @access private + * @var array + */ + private $_curAndOrCol; /** * Public Constructor @@ -182,6 +217,12 @@ class PMA_DbQbe : array_fill(0, $criteriaColumnCount, ''); // sets minimum width $this->_form_column_width = 12; + $this->_curField = array(); + $this->_curSort = array(); + $this->_curShow = array(); + $this->_curCriteria = array(); + $this->_curAndOrRow = array(); + $this->_curAndOrCol = array(); } /** @@ -311,7 +352,7 @@ class PMA_DbQbe $selected = ''; if (isset($_REQUEST['criteriaColumn'][$column_index])) { $selected = $_REQUEST['criteriaColumn'][$column_index]; - $GLOBALS['curField'][$new_column_count] = $_REQUEST['criteriaColumn'][$column_index]; + $this->_curField[$new_column_count] = $_REQUEST['criteriaColumn'][$column_index]; } $html_output .= $this->_showColumnSelectCell($columns, $new_column_count, $selected); $new_column_count++; @@ -356,14 +397,14 @@ class PMA_DbQbe } //end if // Set asc_selected if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'ASC') { - $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; + $this->_curSort[$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; $asc_selected = ' selected="selected"'; } else { $asc_selected = ''; } // end if // Set desc selected if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'DESC') { - $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; + $this->_curSort[$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; $desc_selected = ' selected="selected"'; } else { $desc_selected = ''; @@ -406,7 +447,7 @@ class PMA_DbQbe } if (isset($_REQUEST['criteriaShow'][$column_index])) { $checked_options = ' checked="checked"'; - $GLOBALS['curShow'][$new_column_count] = $_REQUEST['criteriaShow'][$column_index]; + $this->_curShow[$new_column_count] = $_REQUEST['criteriaShow'][$column_index]; } else { $checked_options = ''; } @@ -456,13 +497,13 @@ class PMA_DbQbe || ! isset($this->_prev_criteria[$column_index])) || $this->_prev_criteria[$column_index] != htmlspecialchars($tmp_criteria) ) { - $GLOBALS['curCriteria'][$new_column_count] = $tmp_criteria; + $this->_curCriteria[$new_column_count] = $tmp_criteria; } else { - $GLOBALS['curCriteria'][$new_column_count] = $this->_prev_criteria[$column_index]; + $this->_curCriteria[$new_column_count] = $this->_prev_criteria[$column_index]; } $html_output .= ''; $html_output .= ''; + . ' value="' . htmlspecialchars($this->_curCriteria[$new_column_count]) . '" />'; $html_output .= ''; @@ -602,7 +643,7 @@ class PMA_DbQbe } if (isset($this->_criteriaAndOrColumn[$column_index])) { - $GLOBALS['curAndOrCol'][$new_column_count] = $this->_criteriaAndOrColumn[$column_index]; + $this->_curAndOrCol[$new_column_count] = $this->_criteriaAndOrColumn[$column_index]; } if (isset($this->_criteriaAndOrColumn[$column_index]) && $this->_criteriaAndOrColumn[$column_index] == 'or' @@ -754,7 +795,7 @@ class PMA_DbQbe continue; } if (isset($this->_criteriaAndOrRow[$row_index])) { - $GLOBALS['curAndOrRow'][$new_row_count] = $this->_criteriaAndOrRow[$row_index]; + $this->_curAndOrRow[$new_row_count] = $this->_criteriaAndOrRow[$row_index]; } if (isset($this->_criteriaAndOrRow[$row_index]) && $this->_criteriaAndOrRow[$row_index] == 'and' @@ -788,11 +829,11 @@ class PMA_DbQbe $select_clause = ''; $select_clauses = array(); for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) { - if (! empty($GLOBALS['curField'][$column_index]) - && isset($GLOBALS['curShow'][$column_index]) - && $GLOBALS['curShow'][$column_index] == 'on') + if (! empty($this->_curField[$column_index]) + && isset($this->_curShow[$column_index]) + && $this->_curShow[$column_index] == 'on') { - $select_clauses[] = $GLOBALS['curField'][$column_index]; + $select_clauses[] = $this->_curField[$column_index]; } } // end for if ($select_clauses) { @@ -811,16 +852,16 @@ class PMA_DbQbe $where_clause = ''; $criteria_cnt = 0; for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) { - if (! empty($GLOBALS['curField'][$column_index]) - && ! empty($GLOBALS['curCriteria'][$column_index]) + if (! empty($this->_curField[$column_index]) + && ! empty($this->_curCriteria[$column_index]) && $column_index && isset($last_where) - && isset($GLOBALS['curAndOrCol'])) { - $where_clause .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_where]) . ' '; + && isset($this->_curAndOrCol)) { + $where_clause .= ' ' . strtoupper($this->_curAndOrCol[$last_where]) . ' '; } - if (! empty($GLOBALS['curField'][$column_index]) && ! empty($GLOBALS['curCriteria'][$column_index])) { - $where_clause .= '(' . $GLOBALS['curField'][$column_index] . ' ' - . $GLOBALS['curCriteria'][$column_index] . ')'; + if (! empty($this->_curField[$column_index]) && ! empty($this->_curCriteria[$column_index])) { + $where_clause .= '(' . $this->_curField[$column_index] . ' ' + . $this->_curCriteria[$column_index] . ')'; $last_where = $column_index; $criteria_cnt++; } @@ -829,24 +870,24 @@ class PMA_DbQbe $where_clause = '(' . $where_clause . ')'; } // OR rows ${'cur' . $or}[$column_index] - if (! isset($GLOBALS['curAndOrRow'])) { - $GLOBALS['curAndOrRow'] = array(); + if (! isset($this->_curAndOrRow)) { + $this->_curAndOrRow = array(); } for ($row_index = 0; $row_index <= $this->_criteria_row_count; $row_index++) { $criteria_cnt = 0; $qry_orwhere = ''; $last_orwhere = ''; for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) { - if (! empty($GLOBALS['curField'][$column_index]) + if (! empty($this->_curField[$column_index]) && ! empty(${'curOr' . $row_index}[$column_index]) && $column_index ) { - $qry_orwhere .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_orwhere]) . ' '; + $qry_orwhere .= ' ' . strtoupper($this->_curAndOrCol[$last_orwhere]) . ' '; } - if (! empty($GLOBALS['curField'][$column_index]) + if (! empty($this->_curField[$column_index]) && ! empty(${'curOr' . $row_index}[$column_index]) ) { - $qry_orwhere .= '(' . $GLOBALS['curField'][$column_index] + $qry_orwhere .= '(' . $this->_curField[$column_index] . ' ' . ${'curOr' . $row_index}[$column_index] . ')'; @@ -859,7 +900,7 @@ class PMA_DbQbe } if (! empty($qry_orwhere)) { $where_clause .= "\n" - . strtoupper(isset($GLOBALS['curAndOrRow'][$row_index]) ? $GLOBALS['curAndOrRow'][$row_index] . ' ' : '') + . strtoupper(isset($this->_curAndOrRow[$row_index]) ? $this->_curAndOrRow[$row_index] . ' ' : '') . $qry_orwhere; } // end if } // end for @@ -883,14 +924,14 @@ class PMA_DbQbe { // if all columns are chosen with * selector, then sorting isn't available // Fix for Bug #570698 - if (! empty($GLOBALS['curField'][$column_index]) - && ! empty($GLOBALS['curSort'][$column_index]) + if (! empty($this->_curField[$column_index]) + && ! empty($this->_curSort[$column_index]) ) { - if (substr($GLOBALS['curField'][$column_index], -2) == '.*') { + if (substr($this->_curField[$column_index], -2) == '.*') { continue; } - $orderby_clauses[] = $GLOBALS['curField'][$column_index] . ' ' - . $GLOBALS['curSort'][$column_index]; + $orderby_clauses[] = $this->_curField[$column_index] . ' ' + . $this->_curSort[$column_index]; } } // end for if ($orderby_clauses) { From a62c91c804b4437d6badf2d8a483716488629591 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Thu, 19 Jul 2012 16:23:52 +0000 Subject: [PATCH 32/45] Use local function to get PMA_CommonFunctions instance --- libraries/DBQbe.class.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/libraries/DBQbe.class.php b/libraries/DBQbe.class.php index 1763479465..1631856bd9 100644 --- a/libraries/DBQbe.class.php +++ b/libraries/DBQbe.class.php @@ -149,6 +149,13 @@ class PMA_DbQbe * @var array */ private $_curAndOrCol; + /** + * PMA_CommonFunctions object + * + * @access private + * @var object + */ + private $_common_functions; /** * Public Constructor @@ -238,7 +245,7 @@ class PMA_DbQbe } } // end if $all_tables = PMA_DBI_query( - 'SHOW TABLES FROM ' . $common_functions->backquote($db) . ';', + 'SHOW TABLES FROM ' . $this->_getCommonFunctions->backquote($db) . ';', null, PMA_DBI_QUERY_STORE ); $all_tables_count = PMA_DBI_num_rows($all_tables); @@ -258,10 +265,10 @@ class PMA_DbQbe // The fields list per selected tables if ($this->_criteriaTables[$table] == ' selected="selected"') { - $each_table = $common_functions->backquote($table); + $each_table = $this->_getCommonFunctions->backquote($table); $this->_columnNames[] = $each_table . '.*'; foreach ($columns as $each_column) { - $each_column = $each_table . '.' . $common_functions->backquote($each_column['Field']); + $each_column = $each_table . '.' . $this->_getCommonFunctions->backquote($each_column['Field']); $this->_columnNames[] = $each_column; // increase the width if necessary $this->_form_column_width = max(strlen($each_column), $this->_form_column_width); @@ -1184,12 +1191,12 @@ class PMA_DbQbe $run++; if ($run > 5) { foreach ($remaining_tables as $table) { - $emerg .= ', ' . PMA_CommonFunctions::getInstance()->backquote($table); + $emerg .= ', ' . $this->_getCommonFunctions->backquote($table); unset($remaining_tables[$table]); } } } // end while - $from_clause = PMA_CommonFunctions::getInstance()->backquote($master) + $from_clause = $this->_getCommonFunctions->backquote($master) . $emerg . $left_join; } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) } // end count($_POST['criteriaColumn']) > 0 @@ -1257,7 +1264,7 @@ class PMA_DbQbe $html_output .= '
'; $html_output .= '' . sprintf( - __('SQL query on database %s:'), PMA_CommonFunctions::getInstance()->getDbLink($db) + __('SQL query on database %s:'), $this->_getCommonFunctions->getDbLink($db) ); $html_output .= ''; $html_output .= ''; From 6132bb129945cc330173c77a62a2fb3ef65344c4 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sat, 21 Jul 2012 12:19:42 +0000 Subject: [PATCH 37/45] Fix wrong require_once --- db_qbe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db_qbe.php b/db_qbe.php index 62ff739094..6dfe01f5a6 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -10,7 +10,7 @@ * requirements */ require_once 'libraries/common.inc.php'; -require_once 'libraries/DbQbe.class.php'; +require_once 'libraries/DBQbe.class.php'; $response = PMA_Response::getInstance(); $header = $response->getHeader(); $scripts = $header->getScripts(); From c97ee5fd4b39c0d48c4777e9c018e9c7656003d8 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sat, 21 Jul 2012 13:35:25 +0000 Subject: [PATCH 38/45] Remove some more global variables --- db_qbe.php | 15 --------------- libraries/DBQbe.class.php | 9 +++------ 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 6dfe01f5a6..09312eb249 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -15,21 +15,6 @@ $response = PMA_Response::getInstance(); $header = $response->getHeader(); $scripts = $header->getScripts(); -/** - * Sets globals from $_POST patterns, for Or* variables - * (additional criteria lines) - */ - -$post_patterns = array( - '/^Or/i' -); -foreach (array_keys($_POST) as $post_key) { - foreach ($post_patterns as $one_post_pattern) { - if (preg_match($one_post_pattern, $post_key)) { - $GLOBALS[$post_key] = $_POST[$post_key]; - } - } -} // create new qbe search instance $db_qbe = new PMA_DBQbe($GLOBALS['db']); diff --git a/libraries/DBQbe.class.php b/libraries/DBQbe.class.php index dd6b62c3be..7288b2b173 100644 --- a/libraries/DBQbe.class.php +++ b/libraries/DBQbe.class.php @@ -758,12 +758,9 @@ class PMA_DbQbe ) { continue; } - $or = 'Or' . $row_index; - if (! isset(${$or})) { - ${$or} = ''; - } - if (! empty(${$or}) && isset(${$or}[$column_index])) { - $tmp_or = ${$or}[$column_index]; + $or = 'Or' . $new_row_index; + if (! empty($_POST[$or]) && isset($_POST[$or][$column_index])) { + $tmp_or = $_POST[$or][$column_index]; } else { $tmp_or = ''; } From fadeff76e31d77984bea7aab2f91ec35d850bc78 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sat, 21 Jul 2012 13:43:50 +0000 Subject: [PATCH 39/45] Deleted libraries/db_qbe.lib.php --- libraries/db_qbe.lib.php | 1065 -------------------------------------- 1 file changed, 1065 deletions(-) delete mode 100644 libraries/db_qbe.lib.php diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php deleted file mode 100644 index f314ec701f..0000000000 --- a/libraries/db_qbe.lib.php +++ /dev/null @@ -1,1065 +0,0 @@ -'; - $html_output .= ''; - $html_output .= ''; - return $html_output; -} - -/** - * Provides select options list containing sort options (ASC/DESC) - * - * @param integer $column_number Column Number (0,1,2) or more - * @param string $realwidth Largest column width found - * @param string $asc_selected Selected criteria 'Ascending' - * @param string $desc_selected Selected criteria 'Descending' - * - * @return HTML for select options - */ -function getSortSelectCell($column_number, $realwidth, $asc_selected = '', - $desc_selected = '') -{ - $html_output = ''; - $html_output .= ''; - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing column select options - * - * @param array $criteria_column_count Number of criteria columns - * @param integer $columns All column names - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbegetColumnNamesRow( - $criteria_column_count, $columns, $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Column') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - if (isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= showColumnSelectCell($columns, $new_column_count); - $new_column_count++; - } - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - $selected = ''; - if (isset($_REQUEST['criteriaColumn'][$column_index])) { - $selected = $_REQUEST['criteriaColumn'][$column_index]; - $GLOBALS['curField'][$new_column_count] = $_REQUEST['criteriaColumn'][$column_index]; - } - $html_output .= showColumnSelectCell($columns, $new_column_count, $selected); - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing sort(ASC/DESC) select options - * - * @param array $criteria_column_count Number of criteria columns - * @param string $realwidth Largest column width found - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbegetSortRow( - $criteria_column_count, $realwidth, $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Sort') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= getSortSelectCell($new_column_count, $realwidth); - $new_column_count++; - } // end if - - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - // If they have chosen all fields using the * selector, - // then sorting is not available, Fix for Bug #570698 - if (isset($_REQUEST['criteriaSort'][$column_index]) && isset($_REQUEST['criteriaColumn'][$column_index]) - && substr($_REQUEST['criteriaColumn'][$column_index], -2) == '.*' - ) { - $_REQUEST['criteriaSort'][$column_index] = ''; - } //end if - // Set asc_selected - if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'ASC') { - $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; - $asc_selected = ' selected="selected"'; - } else { - $asc_selected = ''; - } // end if - // Set desc selected - if (isset($_REQUEST['criteriaSort'][$column_index]) && $_REQUEST['criteriaSort'][$column_index] == 'DESC') { - $GLOBALS['curSort'][$new_column_count] = $_REQUEST['criteriaSort'][$column_index]; - $desc_selected = ' selected="selected"'; - } else { - $desc_selected = ''; - } // end if - $html_output .= getSortSelectCell( - $new_column_count, $realwidth, $asc_selected, $desc_selected - ); - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing SHOW checkboxes - * - * @param array $criteria_column_count Number of criteria columns - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbegetShowRow( - $criteria_column_count, $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Show') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end if - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - if (isset($_REQUEST['criteriaShow'][$column_index])) { - $checked_options = ' checked="checked"'; - $GLOBALS['curShow'][$new_column_count] = $_REQUEST['criteriaShow'][$column_index]; - } else { - $checked_options = ''; - } - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing criteria Inputboxes - * - * @param array $criteria_column_count Number of criteria columns - * @param string $realwidth Largest column width found - * @param string $criteria Already Filled criteria - * @param string $prev_criteria Previously filled criteria(hidden form field) - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbegetCriteriaInputboxRow( - $criteria_column_count, $realwidth, $criteria, $prev_criteria, - $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Criteria') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - if (! empty($criteriaColumnInsert) && isset($criteriaColumnInsert[$column_index]) && $criteriaColumnInsert[$column_index] == 'on') { - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end if - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - if (isset($criteria[$column_index])) { - $tmp_criteria = $criteria[$column_index]; - } - if ((empty($prev_criteria) || ! isset($prev_criteria[$column_index])) - || $prev_criteria[$column_index] != htmlspecialchars($tmp_criteria) - ) { - $GLOBALS['curCriteria'][$new_column_count] = $tmp_criteria; - } else { - $GLOBALS['curCriteria'][$new_column_count] = $prev_criteria[$column_index]; - } - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides footer options for adding/deleting row/columns - * - * @param string $type Whether row or column - * - * @return HTML for footer options - */ -function PMA_dbQbeGetFootersOptions($type) -{ - $html_output = '
'; - $html_output .= (($type == 'row') - ? __('Add/Delete criteria rows') : __('Add/Delete columns')); - $html_output .= ':'; - $html_output .= '
'; - return $html_output; -} - -/** - * Provides search form table's footer options - * - * @return HTML for table footer - */ -function PMA_dbQbeGetTableFooters() -{ - $html_output = '
'; - $html_output .= PMA_dbQbeGetFootersOptions("row"); - $html_output .= PMA_dbQbeGetFootersOptions("column"); - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '
'; - return $html_output; -} - -/** - * Provides a select list of database tables - * - * @param array $table_names Names of all the tables - * - * @return HTML for table select list - */ -function PMA_dbQbeGetTablesList($table_names) -{ - $html_output = '
'; - $html_output .= '
'; - $html_output .= '' . __('Use Tables') . ''; - // Build the options list for each table name - $options = ''; - $numTableListOptions = 0; - foreach ($table_names as $key => $val) { - $options .= ''; - $numTableListOptions++; - } - $html_output .= ''; - $html_output .= '
'; - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '
'; - return $html_output; -} - -/** - * Provides And/Or modification cell along with Insert/Delete options - * (For modifying search form's table columns) - * - * @param integer $column_number Column Number (0,1,2) or more - * @param array $selected Selected criteria column name - * - * @return HTML for modification cell - */ -function PMA_dbQbeGetAndOrColCell($column_number, $selected = null) -{ - $html_output = ''; - $html_output .= '' . __('Or') . ':'; - $html_output .= ''; - $html_output .= '  ' . __('And') . ':'; - $html_output .= ''; - $html_output .= '
' . __('Ins'); - $html_output .= ''; - $html_output .= '  ' . __('Del'); - $html_output .= ''; - $html_output .= ''; - return $html_output; -} - -/** - * Provides search form's row containing column modifications options - * (For modifying search form's table columns) - * - * @param array $criteria_column_count Number of criteria columns - * @param string $realwidth Largest column width found - * @param string $criteria Already Filled criteria - * @param string $prev_criteria Previously filled criteria(hidden form field) - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * - * @return HTML for search table's row - */ -function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $criteriaAndOrColumn, - $criteriaColumnInsert = null, $criteriaColumnDelete = null -) { - $html_output = ''; - $html_output .= '' . __('Modify') . ':'; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($criteriaColumnInsert) - && isset($criteriaColumnInsert[$column_index]) - && $criteriaColumnInsert[$column_index] == 'on' - ) { - $html_output .= PMA_dbQbeGetAndOrColCell($new_column_count); - $new_column_count++; - } // end if - - if (! empty($criteriaColumnDelete) && isset($criteriaColumnDelete[$column_index]) && $criteriaColumnDelete[$column_index] == 'on') { - continue; - } - - if (isset($criteriaAndOrColumn[$column_index])) { - $GLOBALS['curAndOrCol'][$new_column_count] = $criteriaAndOrColumn[$column_index]; - } - if (isset($criteriaAndOrColumn[$column_index]) && $criteriaAndOrColumn[$column_index] == 'or') { - $checked_options['or'] = ' checked="checked"'; - $checked_options['and'] = ''; - } else { - $checked_options['and'] = ' checked="checked"'; - $checked_options['or'] = ''; - } - $html_output .= PMA_dbQbeGetAndOrColCell($new_column_count, $checked_options); - $new_column_count++; - } // end for - $html_output .= ''; - return $html_output; -} - -/** - * Provides Insert/Delete options for criteria inputbox - * with AND/OR relationship modification options - * - * @param integer $row_index Number of criteria row - * @param string $checked_options If checked - * - * @return HTML - */ -function PMA_dbQbeGetInsDelAndOrCell($row_index, $checked_options) { - $html_output = ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '' . __('Ins') . ':'; - $html_output .= ''; - $html_output .= ''; - $html_output .= '' . __('And') . ':'; - $html_output .= ''; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '' . __('Del') . ':'; - $html_output .= ''; - $html_output .= ''; - $html_output .= '' . __('Or') . ':'; - $html_output .= ''; - $html_output .= ''; - $html_output .= '
'; - $html_output .= ''; - return $html_output; -} - -/** - * Provides rows for criteria inputbox Insert/Delete options - * with AND/OR relationship modification options - * - * @param array $criteria_column_count Number of criteria columns - * @param integer $new_row_index New row index if rows are added/deleted - * @param integer $row_index Row index - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * @param string $realwidth Largest column width found - * - * @return HTML table rows - */ -function PMA_dbQbeGetInputboxRow($criteria_column_count, $new_row_index, $row_index, - $criteriaColumnInsert, $criteriaColumnDelete, $realwidth -) { - $html_output = ''; - $new_column_count = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($criteriaColumnInsert) - && isset($criteriaColumnInsert[$column_index]) - && $criteriaColumnInsert[$column_index] == 'on' - ) { - $or = 'Or' . $new_row_index . '[' . $new_column_count . ']'; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $new_column_count++; - } // end if - if (! empty($criteriaColumnDelete) - && isset($criteriaColumnDelete[$column_index]) - && $criteriaColumnDelete[$column_index] == 'on' - ) { - continue; - } - $or = 'Or' . $row_index; - if (! isset(${$or})) { - ${$or} = ''; - } - if (! empty(${$or}) && isset(${$or}[$column_index])) { - $tmp_or = ${$or}[$column_index]; - } else { - $tmp_or = ''; - } - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - if (! empty(${$or}) && isset(${$or}[$column_index])) { - $GLOBALS[${'cur' . $or}][$new_column_count] = ${$or}[$column_index]; - } - $new_column_count++; - } // end for - return $html_output; -} - -/** - * Provides rows for criteria inputbox Insert/Delete options - * with AND/OR relationship modification options - * - * @param integer $criteria_row_count Number of criteria rows - * @param array $criteria_column_count Number of criteria columns - * @param string $realwidth Largest column width found - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * @param string $criteriaAndOrRow If AND or OR is to be checked - * - * @return HTML table rows - */ -function PMA_dbQbeGetInsDelAndOrCriteriaRows($criteria_row_count, - $criteria_column_count, $realwidth, $criteriaColumnInsert, $criteriaColumnDelete, - $criteriaAndOrRow -) { - $html_output = ''; - $new_row_count = 0; - $odd_row = true; - for ($row_index = 0; $row_index <= $criteria_row_count; $row_index++) { - if (isset($criteriaRowInsert[$row_index]) && $criteriaRowInsert[$row_index] == 'on') { - $checked_options['or'] = ' checked="checked"'; - $checked_options['and'] = ''; - $html_output .= ''; - $html_output .= PMA_dbQbeGetInsDelAndOrCell($new_row_count, $checked_options); - $html_output .= PMA_dbQbeGetInputboxRow( - $criteria_column_count, $new_row_count, $row_index, $criteriaColumnInsert, - $criteriaColumnDelete, $realwidth - ); - $new_row_count++; - $html_output .= ''; - $odd_row =! $odd_row; - } // end if - if (isset($criteriaRowDelete[$row_index]) && $criteriaRowDelete[$row_index] == 'on') { - continue; - } - if (isset($criteriaAndOrRow[$row_index])) { - $GLOBALS['curAndOrRow'][$new_row_count] = $criteriaAndOrRow[$row_index]; - } - if (isset($criteriaAndOrRow[$row_index]) && $criteriaAndOrRow[$row_index] == 'and') { - $checked_options['and'] = ' checked="checked"'; - $checked_options['or'] = ''; - } else { - $checked_options['or'] = ' checked="checked"'; - $checked_options['and'] = ''; - } - $html_output .= ''; - $html_output .= PMA_dbQbeGetInsDelAndOrCell($new_row_count, $checked_options); - $html_output .= PMA_dbQbeGetInputboxRow( - $criteria_column_count, $new_row_count, $row_index, $criteriaColumnInsert, - $criteriaColumnDelete, $realwidth - ); - $new_row_count++; - $html_output .= ''; - $odd_row =! $odd_row; - } // end for - return $html_output; -} - -/** - * Provides SELECT clause for building SQL query - * - * @param array $criteria_column_count Number of criteria columns - * - * @return Select clause - */ -function PMA_dbQbeGetSelectClause($criteria_column_count){ - $select_clause = ''; - $select_clauses = array(); - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($GLOBALS['curField'][$column_index]) - && isset($GLOBALS['curShow'][$column_index]) - && $GLOBALS['curShow'][$column_index] == 'on') - { - $select_clauses[] = $GLOBALS['curField'][$column_index]; - } - } // end for - if ($select_clauses) { - $select_clause = 'SELECT ' - . htmlspecialchars(implode(", ", $select_clauses)) . "\n"; - } - return $select_clause; -} - -/** - * Provides WHERE clause for building SQL query - * - * @param array $criteria_column_count Number of criteria columns - * @param array $criteria_row_count Number of criteria rows - * - * @return Where clause - */ -function PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count) { - $where_clause = ''; - $criteria_cnt = 0; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($GLOBALS['curField'][$column_index]) - && ! empty($GLOBALS['curCriteria'][$column_index]) - && $column_index - && isset($last_where) - && isset($GLOBALS['curAndOrCol'])) { - $where_clause .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_where]) . ' '; - } - if (! empty($GLOBALS['curField'][$column_index]) && ! empty($GLOBALS['curCriteria'][$column_index])) { - $where_clause .= '(' . $GLOBALS['curField'][$column_index] . ' ' - . $GLOBALS['curCriteria'][$column_index] . ')'; - $last_where = $column_index; - $criteria_cnt++; - } - } // end for - if ($criteria_cnt > 1) { - $where_clause = '(' . $where_clause . ')'; - } - // OR rows ${'cur' . $or}[$column_index] - if (! isset($GLOBALS['curAndOrRow'])) { - $GLOBALS['curAndOrRow'] = array(); - } - for ($row_index = 0; $row_index <= $criteria_row_count; $row_index++) { - $criteria_cnt = 0; - $qry_orwhere = ''; - $last_orwhere = ''; - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) { - if (! empty($GLOBALS['curField'][$column_index]) && ! empty(${'curOr' . $row_index}[$column_index]) && $column_index) { - $qry_orwhere .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_orwhere]) . ' '; - } - if (! empty($GLOBALS['curField'][$column_index]) && ! empty(${'curOr' . $row_index}[$column_index])) { - $qry_orwhere .= '(' . $GLOBALS['curField'][$column_index] - . ' ' - . ${'curOr' . $row_index}[$column_index] - . ')'; - $last_orwhere = $column_index; - $criteria_cnt++; - } - } // end for - if ($criteria_cnt > 1) { - $qry_orwhere = '(' . $qry_orwhere . ')'; - } - if (! empty($qry_orwhere)) { - $where_clause .= "\n" - . strtoupper(isset($GLOBALS['curAndOrRow'][$row_index]) ? $GLOBALS['curAndOrRow'][$row_index] . ' ' : '') - . $qry_orwhere; - } // end if - } // end for - - if (! empty($where_clause) && $where_clause != '()') { - $where_clause = 'WHERE ' . htmlspecialchars($where_clause) . "\n"; - } // end if - return $where_clause; -} - -/** - * Provides ORDER BY clause for building SQL query - * - * @param array $criteria_column_count Number of criteria columns - * - * @return Order By clause - */ -function PMA_dbQbeGetOrderByClause($criteria_column_count) -{ - $orderby_clause = ''; - $orderby_clauses = array(); - for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) - { - // if all columns are chosen with * selector, then sorting isn't available - // Fix for Bug #570698 - if (! empty($GLOBALS['curField'][$column_index]) - && ! empty($GLOBALS['curSort'][$column_index]) - ) { - if (substr($GLOBALS['curField'][$column_index], -2) == '.*') { - continue; - } - $orderby_clauses[] = $GLOBALS['curField'][$column_index] . ' ' - . $GLOBALS['curSort'][$column_index]; - } - } // end for - if ($orderby_clauses) { - $orderby_clause = 'ORDER BY ' - . htmlspecialchars(implode(", ", $orderby_clauses)) . "\n"; - } - return $orderby_clause; -} - -/** - * Provides UNIQUE columns and INDEX columns present in criteria tables - * - * @param array $db Selected database - * @param array $all_tables Tables involved in the search - * @param array $all_columns Columns involved in the search - * @param array $where_clause_columns Columns having criteria where clause - * - * @return array having UNIQUE and INDEX columns - */ -function PMA_dbQbeGetIndexes($db, $all_tables, $all_columns, $where_clause_columns -) { - $unique_columns = array(); - $index_columns = array(); - - foreach ($all_tables as $table) { - $indexes = PMA_DBI_get_table_indexes($db, $table); - foreach ($indexes as $index) { - $column = $table . '.' . $index['Column_name']; - if (isset($all_columns[$column])) { - if ($index['Non_unique'] == 0) { - if (isset($where_clause_columns[$column])) { - $unique_columns[$column] = 'Y'; - } else { - $unique_columns[$column] = 'N'; - } - } else { - if (isset($where_clause_columns[$column])) { - $index_columns[$column] = 'Y'; - } else { - $index_columns[$column] = 'N'; - } - } - } - } // end while (each index of a table) - } // end while (each table) - - return array( - 'unique' => $unique_columns, - 'index' => $index_columns - ); -} - -/** - * Provides UNIQUE columns and INDEX columns present in criteria tables - * - * @param array $db Selected database - * @param array $all_tables Tables involved in the search - * @param array $all_columns Columns involved in the search - * @param array $where_clause_columns Columns having criteria where clause - * - * @return array having UNIQUE and INDEX columns - */ -function PMA_dbQbeGetLeftJoinColumnCandidates($db, $all_tables, $all_columns, - $where_clause_columns -) { - PMA_DBI_select_db($db); - $candidate_columns = array(); - - // Get unique columns and index columns - $indexes = PMA_dbQbeGetIndexes( - $db, $all_tables, $all_columns, $where_clause_columns - ); - $unique_columns = $indexes['unique']; - $index_columns = $indexes['index']; - - // now we want to find the best. - if (isset($unique_columns) && count($unique_columns) > 0) { - $candidate_columns = $unique_columns; - $needsort = 1; - } elseif (isset($index_columns) && count($index_columns) > 0) { - $candidate_columns = $index_columns; - $needsort = 1; - } elseif (isset($where_clause_columns) && count($where_clause_columns) > 0) { - $candidate_columns = $tab_wher; - $needsort = 0; - } else { - $candidate_columns = $all_tables; - $needsort = 0; - } - - // If we came up with $unique_columns (very good) or $index_columns (still - // good) as $candidate_columns we want to check if we have any 'Y' there - // (that would mean that they were also found in the whereclauses - // which would be great). if yes, we take only those - if ($needsort == 1) { - foreach ($candidate_columns as $column => $is_where) { - $table = explode('.', $column); - $table = $table[0]; - if ($is_where == 'Y') { - $vg[$column] = $table; - } else { - $sg[$column] = $table; - } - } - if (isset($vg)) { - $candidate_columns = $vg; - // Candidates restricted in index+where - } else { - $candidate_columns = $sg; - // None of the candidates where in a where-clause - } - } - - return $candidate_columns; -} - -/** - * Provides the main table to form the LEFT JOIN clause - * - * @param array $db Selected database - * @param array $all_tables Tables involved in the search - * @param array $all_columns Columns involved in the search - * @param array $where_clause_columns Columns having criteria where clause - * @param array $where_clause_tables Tables having criteria where clause - * - * @return string table name - */ -function PMA_dbQbeGetMasterTable($db, $all_tables, $all_columns, - $where_clause_columns, $where_clause_tables -) { - $master = ''; - if (count($where_clause_tables) == 1) { - // If there is exactly one column that has a decent where-clause - // we will just use this - $master = key($where_clause_tables); - } else { - // Now let's find out which of the tables has an index - // (When the control user is the same as the normal user - // because he is using one of his databases as pmadb, - // the last db selected is not always the one where we need to work) - $candidate_columns = PMA_dbQbeGetLeftJoinColumnCandidates( - $db, $all_tables, $all_columns, $where_clause_columns - ); - // If our array of candidates has more than one member we'll just - // find the smallest table. - // Of course the actual query would be faster if we check for - // the Criteria which gives the smallest result set in its table, - // but it would take too much time to check this - if (count($candidate_columns) > 1) { - // Of course we only want to check each table once - $checked_tables = $candidate_columns; - foreach ($candidate_columns as $table) { - if ($checked_tables[$table] != 1) { - $tsize[$table] = PMA_Table::countRecords($db, $table, false); - $checked_tables[$table] = 1; - } - $csize[$table] = $tsize[$table]; - } - asort($csize); - reset($csize); - $master = key($csize); // Smallest - } else { - reset($candidate_columns); - $master = current($candidate_columns); // Only one single candidate - } - } // end if (exactly one where clause) - return $master; -} - -/** - * Provides columns and tables that have valid where clause criteria - * - * @param string $criteria Already Filled criteria - * - * @return array - */ -function getWhereClauseTablesAndColumns($criteria) { - $where_clause_columns = array(); - $where_clause_tables = array(); - // Now we need all tables that we have in the where clause - for ($column_index = 0; $column_index < count($criteria); $column_index++) { - $current_table = explode('.', $_POST['criteriaColumn'][$column_index]); - if (empty($current_table[0]) || empty($current_table[1])) { - continue; - } // end if - $table = str_replace('`', '', $current_table[0]); - $column = str_replace('`', '', $current_table[1]); - $column = $table . '.' . $column; - // Now we know that our array has the same numbers as $criteria - // we can check which of our columns has a where clause - if (! empty($criteria[$column_index])) { - if (substr($criteria[$column_index], 0, 1) == '=' - || stristr($criteria[$column_index], 'is') - ) { - $where_clause_columns[$column] = $column; - $where_clause_tables[$table] = $table; - } - } // end if - } // end for - return array( - 'where_clause_tables' => $where_clause_tables, - 'where_clause_columns' => $where_clause_columns - ); -} - -/** - * Provides FROM clause for building SQL query - * - * @param string $criteria Already Filled criteria - * - * @return FROM clause - */ -function PMA_dbQbeGetFromClause($criteria, $cfgRelation) -{ - $from_clause = ''; - if (isset($_POST['criteriaColumn']) && count($_POST['criteriaColumn']) > 0) { - // Initialize some variables - $all_tables = $all_columns = $known_tables = $remaining_tables = array(); - $left_join = ''; - - // We only start this if we have fields, otherwise it would be dumb - foreach ($_POST['criteriaColumn'] as $value) { - $parts = explode('.', $value); - if (! empty($parts[0]) && ! empty($parts[1])) { - $table = str_replace('`', '', $parts[0]); - $all_tables[$table] = $table; - $all_columns[] = $table . '.' . str_replace('`', '', $parts[1]); - } - } // end while - - // Create LEFT JOINS out of Relations - if ($cfgRelation['relwork'] && count($all_tables) > 0) { - // Get tables and columns with valid where clauses - $valid_where_clauses = getWhereClauseTablesAndColumns($criteria); - $where_clause_tables = $valid_where_clauses['where_clause_tables']; - $where_clause_columns = $valid_where_clauses['where_clause_columns']; - // Get master table - $master = PMA_dbQbeGetMasterTable( - $db, $all_tables, $all_columns, $where_clause_columns, $where_clause_tables - ); - $remaining_tables = $all_tables; - unset($remaining_tables[$master]); - $known_tables[$master] = $master; - - $run = 0; - $emerg = ''; - while (count($remaining_tables) > 0) { - if ($run % 2 == 0) { - $left_join .= PMA_getRelatives('master', $remaining_tables, $known_tables); - } else { - $left_join .= PMA_getRelatives('foreign', $remaining_tables, $known_tables); - } - $run++; - if ($run > 5) { - foreach ($remaining_tables as $table) { - $emerg .= ', ' . PMA_CommonFunctions::getInstance()->backquote($table); - unset($remaining_tables[$table]); - } - } - } // end while - $from_clause = PMA_CommonFunctions::getInstance()->backquote($master) - . $emerg . $left_join; - } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) - } // end count($_POST['criteriaColumn']) > 0 - - // In case relations are not defined, just generate the FROM clause - // from the list of tables, however we don't generate any JOIN - if (empty($from_clause) && isset($all_tables)) { - $from_clause = implode(', ', $all_tables); - } - return $from_clause; -} - -/** - * Provides the generated SQL query - * - * @param string $criteria Already Filled criteria - * - * @return string SQL query - */ -function PMA_dbQbeGetSQLQuery($criteria_column_count, $criteria_row_count, $criteria, - $cfgRelation -) { - $sql_query = ''; - // get SELECT clause - $sql_query .= PMA_dbQbeGetSelectClause($criteria_column_count); - // get FROM clause - $from_clause = PMA_dbQbeGetFromClause($criteria, $cfgRelation); - if (! empty($from_clause)) { - $sql_query .= 'FROM ' . htmlspecialchars($from_clause) . "\n"; - } - // get WHERE clause - $sql_query .= PMA_dbQbeGetWhereClause($criteria_column_count, $criteria_row_count); - // get ORDER BY clause - $sql_query .= PMA_dbQbeGetOrderByClause($criteria_column_count); - return $sql_query; -} - -/** - * Provides the generated QBE form - * - * @param array $db Selected Database - * @param array $tbl_names Number of tables - * @param array $criteria_column_count Number of criteria columns - * @param integer $criteria_row_count Number of criteria rows - * @param string $criteriaColumnInsert If a new criteria column is needed - * @param string $criteriaColumnDelete If a criteria column is to be deleted - * @param string $realwidth Largest column width found - * @param string $criteria Already Filled criteria - * @param string $prev_criteria Previously filled criteria(hidden form field) - * @param string $criteriaAndOrRow If AND or OR is to be checked among rows - * @param string $criteriaAndOrColumn If AND or OR is to be checked among columns - * - * @return string QBE form - */ -function PMA_dbQbeGetSelectionForm($db, $tbl_names, $columnNames, $criteria_column_count, $criteria_row_count, - $criteriaColumnInsert, $criteriaColumnDelete, $realwidth, $criteria, $prev_criteria, - $criteriaAndOrRow, $criteriaAndOrColumn, $cfgRelation -) { - $html_output = '
'; - $html_output .= '
'; - $html_output .= ''; - // Get table's elements - $html_output .= PMA_dbQbegetColumnNamesRow( - $criteria_column_count, $columnNames, $criteriaColumnInsert, $criteriaColumnDelete - ); - $html_output .= PMA_dbQbegetSortRow( - $criteria_column_count, $realwidth, $criteriaColumnInsert, $criteriaColumnDelete - ); - $html_output .= PMA_dbQbegetShowRow( - $criteria_column_count, $criteriaColumnInsert, $criteriaColumnDelete - ); - $html_output .= PMA_dbQbegetCriteriaInputboxRow( - $criteria_column_count, $realwidth, $criteria, $prev_criteria, - $criteriaColumnInsert, $criteriaColumnDelete - ); - $html_output .= PMA_dbQbeGetInsDelAndOrCriteriaRows($criteria_row_count, - $criteria_column_count, $realwidth, $criteriaColumnInsert, - $criteriaColumnDelete, $criteriaAndOrRow - ); - $html_output .= PMA_dbQbeGetModifyColumnsRow( - $criteria_column_count, $criteriaAndOrColumn, $criteriaColumnInsert, - $criteriaColumnDelete - ); - $html_output .= '
'; - $new_row_count--; - $url_params['db'] = $db; - $url_params['criteriaColumnCount'] = $new_column_count; - $url_params['rows'] = $new_row_count; - $html_output .= PMA_generate_common_hidden_inputs($url_params); - $html_output .= '
'; - // get footers - $html_output .= PMA_dbQbeGetTableFooters(); - // get tables select list - $html_output .= PMA_dbQbeGetTablesList($tbl_names); - // get SQL query - $html_output .= '
'; - $html_output .= '
'; - $html_output .= '' - . sprintf( - __('SQL query on database %s:'), PMA_CommonFunctions::getInstance()->getDbLink($db) - ); - $html_output .= ''; - $html_output .= ''; - $html_output .= '
'; - // displays form's footers - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '
'; - $html_output .= '
'; - return $html_output; -} -?> From 80e13b0aca9119aead05aca83926bdc718b6b7be Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 22 Jul 2012 16:16:45 +0530 Subject: [PATCH 40/45] Add missing cfgRelation declaration --- db_qbe.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 09312eb249..42715426be 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -12,9 +12,9 @@ require_once 'libraries/common.inc.php'; require_once 'libraries/DBQbe.class.php'; $response = PMA_Response::getInstance(); -$header = $response->getHeader(); -$scripts = $header->getScripts(); +// Gets the relation settings +$cfgRelation = PMA_getRelationsParam(); // create new qbe search instance $db_qbe = new PMA_DBQbe($GLOBALS['db']); @@ -22,7 +22,6 @@ $db_qbe = new PMA_DBQbe($GLOBALS['db']); /** * Displays the Query by example form */ - if ($cfgRelation['designerwork']) { $url = 'pmd_general.php' . PMA_generate_common_url( array_merge( From 9ac38c49de981621458f9c6cfaf221d3d358a599 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 22 Jul 2012 16:24:17 +0530 Subject: [PATCH 41/45] Fix visual buider link for now --- db_qbe.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 42715426be..2bb543a279 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -29,13 +29,13 @@ if ($cfgRelation['designerwork']) { array('query' => 1) ) ); - PMA_Message::notice( + $response->addHTML(PMA_Message::notice( sprintf( __('Switch to %svisual builder%s'), '', '' ) - )->display(); + ));//->display(); } $response->addHTML($db_qbe->getSelectionForm($cfgRelation)); ?> From 916143e27fd7338c945fc4e5b0cb9ec4982e807a Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 22 Jul 2012 18:32:43 +0530 Subject: [PATCH 42/45] Fix multiple LEFT JOIN generation problem --- libraries/DBQbe.class.php | 25 ++---------- libraries/relation.lib.php | 84 +++++++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 56 deletions(-) diff --git a/libraries/DBQbe.class.php b/libraries/DBQbe.class.php index 7288b2b173..f936f25b60 100644 --- a/libraries/DBQbe.class.php +++ b/libraries/DBQbe.class.php @@ -1100,7 +1100,7 @@ class PMA_DbQbe $checked_tables = $candidate_columns; foreach ($candidate_columns as $table) { if ($checked_tables[$table] != 1) { - $tsize[$table] = PMA_Table::countRecords($db, $table, false); + $tsize[$table] = PMA_Table::countRecords($this->_db, $table, false); $checked_tables[$table] = 1; } $csize[$table] = $tsize[$table]; @@ -1186,28 +1186,9 @@ class PMA_DbQbe $master = $this->_getMasterTable( $all_tables, $all_columns, $where_clause_columns, $where_clause_tables ); - $remaining_tables = $all_tables; - unset($remaining_tables[$master]); - $known_tables[$master] = $master; - - $run = 0; - $emerg = ''; - while (count($remaining_tables) > 0) { - if ($run % 2 == 0) { - $left_join .= PMA_getRelatives('master', $remaining_tables, $known_tables); - } else { - $left_join .= PMA_getRelatives('foreign', $remaining_tables, $known_tables); - } - $run++; - if ($run > 5) { - foreach ($remaining_tables as $table) { - $emerg .= ', ' . $this->getCommonFunctions()->backquote($table); - unset($remaining_tables[$table]); - } - } - } // end while $from_clause = $this->getCommonFunctions()->backquote($master) - . $emerg . $left_join; + . PMA_getRelatives($all_tables, $master); + } // end if ($cfgRelation['relwork'] && count($all_tables) > 0) } // end count($_POST['criteriaColumn']) > 0 diff --git a/libraries/relation.lib.php b/libraries/relation.lib.php index 22f8d7dfd4..84884ecb3d 100644 --- a/libraries/relation.lib.php +++ b/libraries/relation.lib.php @@ -1161,49 +1161,65 @@ function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filte /** * Finds all related tables * - * @param string $from Whether to go from master to foreign or vice versa - * @param array $remaining_tables The list of tables that we still couldn't connect - * @param array $known_tables The list of allready connected tables + * @param array $all_tables All the involved tables + * @param string $master The master table to form the LEFT JOIN clause * * @return string LEFT JOIN * @access private */ -function PMA_getRelatives($from, $remaining_tables, $known_tables) +function PMA_getRelatives($all_tables, $master) { $fromclause = ''; + $emerg = ''; $common_functions = PMA_CommonFunctions::getInstance(); - - if ($from == 'master') { - $to = 'foreign'; - } else { - $to = 'master'; - } - $in_know = '(\'' . implode('\', \'', $known_tables) . '\')'; - $in_left = '(\'' . implode('\', \'', $remaining_tables) . '\')'; - - $rel_query = 'SELECT *' - . ' FROM ' . $common_functions->backquote($GLOBALS['cfgRelation']['db']) - . '.' . $common_functions->backquote($GLOBALS['cfgRelation']['relation']) - . ' WHERE ' . $from . '_db = \'' . $common_functions->sqlAddSlashes($GLOBALS['db']) . '\'' - . ' AND ' . $to . '_db = \'' . $common_functions->sqlAddSlashes($GLOBALS['db']) . '\'' - . ' AND ' . $from . '_table IN ' . $in_know - . ' AND ' . $to . '_table IN ' . $in_left; - $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']); - while ($row = PMA_DBI_fetch_assoc($relations)) { - $found_table = $row[$to . '_table']; - if (isset($remaining_tables[$found_table])) { - $fromclause - .= "\n" . ' LEFT JOIN ' - . $common_functions->backquote($GLOBALS['db']) . '.' . $common_functions->backquote($row[$to . '_table']) . ' ON ' - . $common_functions->backquote($row[$from . '_table']) . '.' - . $common_functions->backquote($row[$from . '_field']) . ' = ' - . $common_functions->backquote($row[$to . '_table']) . '.' - . $common_functions->backquote($row[$to . '_field']) . ' '; - $known_tables[$found_table] = $found_table; - unset($remaining_tables[$found_table]); + // The list of tables that we still couldn't connect + $remaining_tables = $all_tables; + unset($remaining_tables[$master]); + // The list of allready connected tables + $known_tables[$master] = $master; + $run = 0; + while (count($remaining_tables) > 0) { + // Whether to go from master to foreign or vice versa + if ($run % 2 == 0) { + $from = 'master'; + $to = 'foreign'; + } else { + $from = 'foreign'; + $to = 'master'; + } + $in_know = '(\'' . implode('\', \'', $known_tables) . '\')'; + $in_left = '(\'' . implode('\', \'', $remaining_tables) . '\')'; + $rel_query = 'SELECT *' + . ' FROM ' . $common_functions->backquote($GLOBALS['cfgRelation']['db']) + . '.' . $common_functions->backquote($GLOBALS['cfgRelation']['relation']) + . ' WHERE ' . $from . '_db = \'' . $common_functions->sqlAddSlashes($GLOBALS['db']) . '\'' + . ' AND ' . $to . '_db = \'' . $common_functions->sqlAddSlashes($GLOBALS['db']) . '\'' + . ' AND ' . $from . '_table IN ' . $in_know + . ' AND ' . $to . '_table IN ' . $in_left; + $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']); + while ($row = PMA_DBI_fetch_assoc($relations)) { + $found_table = $row[$to . '_table']; + if (isset($remaining_tables[$found_table])) { + $fromclause + .= "\n" . ' LEFT JOIN ' + . $common_functions->backquote($GLOBALS['db']) . '.' . $common_functions->backquote($row[$to . '_table']) . ' ON ' + . $common_functions->backquote($row[$from . '_table']) . '.' + . $common_functions->backquote($row[$from . '_field']) . ' = ' + . $common_functions->backquote($row[$to . '_table']) . '.' + . $common_functions->backquote($row[$to . '_field']) . ' '; + $known_tables[$found_table] = $found_table; + unset($remaining_tables[$found_table]); + } + } // end while + $run++; + if ($run > 5) { + foreach ($remaining_tables as $table) { + $emerg .= ', ' . $this->getCommonFunctions()->backquote($table); + unset($remaining_tables[$table]); + } } } // end while - + $fromclause = $emerg . $fromclause; return $fromclause; } // end of the "PMA_getRelatives()" function From 9dece4fbef60c31bde2ab21aeb445e6d3c569b96 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 22 Jul 2012 18:37:30 +0530 Subject: [PATCH 43/45] Add wrongly removed query execution part --- db_qbe.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/db_qbe.php b/db_qbe.php index 2bb543a279..f7628c5fc0 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -16,6 +16,31 @@ $response = PMA_Response::getInstance(); // Gets the relation settings $cfgRelation = PMA_getRelationsParam(); +/** + * A query has been submitted -> (maybe) execute it + */ +$message_to_display = false; +if (isset($_REQUEST['submit_sql']) && ! empty($sql_query)) { + if (! preg_match('@^SELECT@i', $sql_query)) { + $message_to_display = true; + } else { + $goto = 'db_sql.php'; + include 'sql.php'; + exit; + } +} + +$sub_part = '_qbe'; +require 'libraries/db_common.inc.php'; +$url_query .= '&goto=db_qbe.php'; +$url_params['goto'] = 'db_qbe.php'; +require 'libraries/db_info.inc.php'; + +if ($message_to_display) { + PMA_Message::error(__('You have to choose at least one column to display'))->display(); +} +unset($message_to_display); + // create new qbe search instance $db_qbe = new PMA_DBQbe($GLOBALS['db']); From 6c0e9e5391ba8f74ec7e3152ba08a1f429c18742 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 22 Jul 2012 19:53:51 +0530 Subject: [PATCH 44/45] Fix typo in 916143e27f --- libraries/relation.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/relation.lib.php b/libraries/relation.lib.php index 84884ecb3d..62a09809ee 100644 --- a/libraries/relation.lib.php +++ b/libraries/relation.lib.php @@ -1214,7 +1214,7 @@ function PMA_getRelatives($all_tables, $master) $run++; if ($run > 5) { foreach ($remaining_tables as $table) { - $emerg .= ', ' . $this->getCommonFunctions()->backquote($table); + $emerg .= ', ' . $common_functions->backquote($table); unset($remaining_tables[$table]); } } From c59aeba53d6b71a9c99233e196e36e1657702ace Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Tue, 24 Jul 2012 23:32:48 +0530 Subject: [PATCH 45/45] Fix typo --- libraries/DBQbe.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/DBQbe.class.php b/libraries/DBQbe.class.php index f936f25b60..355282c270 100644 --- a/libraries/DBQbe.class.php +++ b/libraries/DBQbe.class.php @@ -1031,7 +1031,7 @@ class PMA_DbQbe $candidate_columns = $index_columns; $needsort = 1; } elseif (isset($where_clause_columns) && count($where_clause_columns) > 0) { - $candidate_columns = $tab_wher; + $candidate_columns = $where_clause_columns; $needsort = 0; } else { $candidate_columns = $all_tables;