From 91d372f9bcca4b92e6e63d7354fcf2a81b6b8f18 Mon Sep 17 00:00:00 2001 From: Ammar Yasir Date: Sun, 29 May 2011 12:16:13 +0530 Subject: [PATCH] Added a library file for the table-search and zoom-search code Contains two functions: -> Get Field list and details -> Get where clause of query generation process --- libraries/tbl_select.lib.php | 151 +++++++++++++++++++++++++++++++++++ tbl_select.php | 135 ++++--------------------------- tbl_zoom_select.php | 135 ++++--------------------------- 3 files changed, 181 insertions(+), 240 deletions(-) create mode 100644 libraries/tbl_select.lib.php diff --git a/libraries/tbl_select.lib.php b/libraries/tbl_select.lib.php new file mode 100644 index 0000000000..59bdd6da11 --- /dev/null +++ b/libraries/tbl_select.lib.php @@ -0,0 +1,151 @@ + 1) { + $func_type = 'IN'; + $parens_open = '('; + $parens_close = ')'; + + } elseif ($func_type == '!=' && $enum_selected_count > 1) { + $func_type = 'NOT IN'; + $parens_open = '('; + $parens_close = ')'; + + } else { + $parens_open = ''; + $parens_close = ''; + } + $enum_where = '\'' . PMA_sqlAddslashes($fields[0]) . '\''; + for ($e = 1; $e < $enum_selected_count; $e++) { + $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$e]) . '\''; + } + + $w = PMA_backquote($names) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close; + } + + } elseif ($fields != '') { + // For these types we quote the value. Even if it's another type (like INT), + // for a LIKE we always quote the value. MySQL converts strings to numbers + // and numbers to strings as necessary during the comparison + if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types) || strpos(' ' . $func_type, 'LIKE')) { + $quot = '\''; + } else { + $quot = ''; + } + + // LIKE %...% + if ($func_type == 'LIKE %...%') { + $func_type = 'LIKE'; + $fields = '%' . $fields . '%'; + } + if ($func_type == 'REGEXP ^...$') { + $func_type = 'REGEXP'; + $fields = '^' . $fields . '$'; + } + + if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') { + $func_type = str_replace(' (...)', '', $func_type); + + // quote values one by one + $values = explode(',', $fields); + foreach ($values as &$value) + $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot; + + if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') + $w = PMA_backquote($names) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : ''); + else + $w = PMA_backquote($names) . ' ' . $func_type . ' (' . implode(',', $values) . ')'; + } + else { + $w = PMA_backquote($names) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields) . $quot;; + } + + } // end if + + return $w; +} + +function PMA_tbl_getSubTabs(){ + + $subtabs = array(); + + $subtabs['search']['icon'] = 'b_search.png'; + $subtabs['search']['text'] = __('Table Search'); + $subtabs['search']['link'] = 'tbl_select.php'; + $subtabs['search']['id'] = 'tbl_search_id'; + $subtabs['search']['args']['pos'] = 0; + + $subtabs['zoom']['icon'] = 'b_props.png'; + $subtabs['zoom']['link'] = 'tbl_zoom_select.php'; + $subtabs['zoom']['text'] = __('Zoom Search'); + $subtabs['zoom']['id'] = 'zoom_search_id'; + + return $subtabs; + +} + +?> diff --git a/tbl_select.php b/tbl_select.php index 8732c1c592..a2fd6194f3 100644 --- a/tbl_select.php +++ b/tbl_select.php @@ -15,6 +15,7 @@ */ require_once './libraries/common.inc.php'; require_once './libraries/mysql_charsets.lib.php'; +require_once './libraries/tbl_select.lib.php'; $GLOBALS['js_include'][] = 'sql.js'; $GLOBALS['js_include'][] = 'tbl_select.js'; @@ -59,39 +60,13 @@ if (! isset($param) || $param[0] == '') { $err_url = $goto . '?' . PMA_generate_common_url($db, $table); // Gets the list and number of fields - $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE); - $fields_cnt = PMA_DBI_num_rows($result); - $fields_list = $fields_null = $fields_type = $fields_collation = array(); - while ($row = PMA_DBI_fetch_assoc($result)) { - $fields_list[] = $row['Field']; - $type = $row['Type']; - // reformat mysql query output - if (strncasecmp($type, 'set', 3) == 0 - || strncasecmp($type, 'enum', 4) == 0) { - $type = str_replace(',', ', ', $type); - } else { - // strip the "BINARY" attribute, except if we find "BINARY(" because - // this would be a BINARY or VARBINARY field type - if (!preg_match('@BINARY[\(]@i', $type)) { - $type = preg_replace('@BINARY@i', '', $type); - } - $type = preg_replace('@ZEROFILL@i', '', $type); - $type = preg_replace('@UNSIGNED@i', '', $type); - - $type = strtolower($type); - } - if (empty($type)) { - $type = ' '; - } - $fields_null[] = $row['Null']; - $fields_type[] = $type; - $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL' - ? $row['Collation'] - : ''; - } // end while - PMA_DBI_free_result($result); - unset($result, $type); + $fields_array = PMA_tbl_getFields($table,$db); + $fields_list = $fields_array[0]; + $fields_type = $fields_array[1]; + $fields_collation = $fields_array[2]; + $fields_null = $fields_array[3]; + $fields_cnt = count($fields_list); // retrieve keys into foreign fields, if any // check also foreigners even if relwork is FALSE (to get @@ -106,21 +81,8 @@ $url_params = array(); $url_params['db'] = $db; $url_params['table'] = $table; -$subtabs = array(); +echo PMA_generate_html_tabs(PMA_tbl_getSubTabs(), $url_params); -$subtabs['search']['icon'] = 'b_search.png'; -$subtabs['search']['text'] = __('Table Search'); -$subtabs['search']['link'] = 'tbl_select.php'; -$subtabs['search']['id'] = 'tbl_search_id'; -$subtabs['search']['args']['pos'] = 0; - -$subtabs['zoom']['icon'] = 'b_props.png'; -$subtabs['zoom']['link'] = 'tbl_zoom_select.php'; -$subtabs['zoom']['text'] = __('Zoom Search'); -$subtabs['zoom']['id'] = 'zoom_search_id'; - -echo PMA_generate_html_tabs($subtabs, $url_params); -unset($subtabs); ?>
> @@ -361,79 +323,15 @@ else { $cnt_func = count($func); reset($func); while (list($i, $func_type) = each($func)) { - list($charsets[$i]) = explode('_', $collations[$i]); - if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) { - $fields[$i] = ''; - $w[] = PMA_backquote($names[$i]) . ' ' . $func_type; - - } elseif (strncasecmp($types[$i], 'enum', 4) == 0) { - if (!empty($fields[$i])) { - if (! is_array($fields[$i])) { - $fields[$i] = explode(',', $fields[$i]); - } - $enum_selected_count = count($fields[$i]); - if ($func_type == '=' && $enum_selected_count > 1) { - $func_type = $func[$i] = 'IN'; - $parens_open = '('; - $parens_close = ')'; - - } elseif ($func_type == '!=' && $enum_selected_count > 1) { - $func_type = $func[$i] = 'NOT IN'; - $parens_open = '('; - $parens_close = ')'; - - } else { - $parens_open = ''; - $parens_close = ''; - } - $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\''; - for ($e = 1; $e < $enum_selected_count; $e++) { - $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\''; - } - - $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close; - } - - } elseif ($fields[$i] != '') { - // For these types we quote the value. Even if it's another type (like INT), - // for a LIKE we always quote the value. MySQL converts strings to numbers - // and numbers to strings as necessary during the comparison - if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) { - $quot = '\''; - } else { - $quot = ''; - } - - // LIKE %...% - if ($func_type == 'LIKE %...%') { - $func_type = 'LIKE'; - $fields[$i] = '%' . $fields[$i] . '%'; - } - if ($func_type == 'REGEXP ^...$') { - $func_type = 'REGEXP'; - $fields[$i] = '^' . $fields[$i] . '$'; - } - - if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') { - $func_type = str_replace(' (...)', '', $func_type); - - // quote values one by one - $values = explode(',', $fields[$i]); - foreach ($values as &$value) - $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot; - - if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') - $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : ''); - else - $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')'; - } - else { - $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;; - } - - } // end if + + list($charsets[$i]) = explode('_', $collations[$i]); + $unaryFlag = (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) ? true : false; + $whereClause = PMA_tbl_search_getWhereClause($fields[$i],$names[$i], $types[$i], $collations[$i], $func_type, $unaryFlag); + if($whereClause) + $w[] = $whereClause; + } // end for - + //print_r($w); if ($w) { $sql_query .= ' WHERE ' . implode(' AND ', $w); } @@ -442,7 +340,6 @@ else { if ($orderField != '--nil--') { $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order; } // end if - require './sql.php'; } diff --git a/tbl_zoom_select.php b/tbl_zoom_select.php index efc8879fb4..5e59bdd8d3 100644 --- a/tbl_zoom_select.php +++ b/tbl_zoom_select.php @@ -12,6 +12,7 @@ */ require_once './libraries/common.inc.php'; require_once './libraries/mysql_charsets.lib.php'; +require_once './libraries/tbl_select.lib.php'; $GLOBALS['js_include'][] = 'sql.js'; $GLOBALS['js_include'][] = 'tbl_select.js'; @@ -55,39 +56,14 @@ if (! isset($zoom_submit)) { $err_url = $goto . '?' . PMA_generate_common_url($db, $table); // Gets the list and number of fields - $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE); - $fields_cnt = PMA_DBI_num_rows($result); - $fields_list = $fields_null = $fields_type = $fields_collation = array(); - while ($row = PMA_DBI_fetch_assoc($result)) { - $fields_list[] = $row['Field']; - $type = $row['Type']; - // reformat mysql query output - if (strncasecmp($type, 'set', 3) == 0 - || strncasecmp($type, 'enum', 4) == 0) { - $type = str_replace(',', ', ', $type); - } else { - // strip the "BINARY" attribute, except if we find "BINARY(" because - // this would be a BINARY or VARBINARY field type - if (!preg_match('@BINARY[\(]@i', $type)) { - $type = preg_replace('@BINARY@i', '', $type); - } - $type = preg_replace('@ZEROFILL@i', '', $type); - $type = preg_replace('@UNSIGNED@i', '', $type); + $fields_array = PMA_tbl_getFields($table,$db); + $fields_list = $fields_array[0]; + $fields_type = $fields_array[1]; + $fields_collation = $fields_array[2]; + $fields_null = $fields_array[3]; + $fields_cnt = count($fields_list); - $type = strtolower($type); - } - if (empty($type)) { - $type = ' '; - } - $fields_null[] = $row['Null']; - $fields_type[] = $type; - $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL' - ? $row['Collation'] - : ''; - } // end while - PMA_DBI_free_result($result); - unset($result, $type); // retrieve keys into foreign fields, if any // check also foreigners even if relwork is FALSE (to get @@ -105,21 +81,7 @@ $url_params = array(); $url_params['db'] = $db; $url_params['table'] = $table; -$subtabs = array(); - -$subtabs['search']['icon'] = 'b_search.png'; -$subtabs['search']['text'] = __('Table Search'); -$subtabs['search']['link'] = 'tbl_select.php'; -$subtabs['search']['id'] = 'tbl_search_id'; -$subtabs['search']['args']['pos'] = 0; - -$subtabs['zoom']['icon'] = 'b_props.png'; -$subtabs['zoom']['link'] = 'tbl_zoom_select.php'; -$subtabs['zoom']['text'] = __('Zoom Search'); -$subtabs['zoom']['id'] = 'zoom_search_id'; - -echo PMA_generate_html_tabs($subtabs, $url_params); -unset($subtabs); +echo PMA_generate_html_tabs(PMA_tbl_getSubTabs(), $url_params); ?> 1) { - $func_type = $func[$i] = 'IN'; - $parens_open = '('; - $parens_close = ')'; - - } elseif ($func_type == '!=' && $enum_selected_count > 1) { - $func_type = $func[$i] = 'NOT IN'; - $parens_open = '('; - $parens_close = ')'; - } else { - $parens_open = ''; - $parens_close = ''; - } - $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\''; - for ($e = 1; $e < $enum_selected_count; $e++) { - $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\''; - } - - $w[] = PMA_backquote($inputs[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close; - } - - } elseif ($fields[$i] != '') { - // For these types we quote the value. Even if it's another type (like INT), - // for a LIKE we always quote the value. MySQL converts strings to numbers - // and numbers to strings as necessary during the comparison - if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) { - $quot = '\''; - } else { - $quot = ''; - } - - // LIKE %...% - if ($func_type == 'LIKE %...%') { - $func_type = 'LIKE'; - $fields[$i] = '%' . $fields[$i] . '%'; - } - if ($func_type == 'REGEXP ^...$') { - $func_type = 'REGEXP'; - $fields[$i] = '^' . $fields[$i] . '$'; - } - - if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') { - $func_type = str_replace(' (...)', '', $func_type); - - // quote values one by one - $values = explode(',', $fields[$i]); - foreach ($values as &$value) - $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot; - - if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') - $w[] = PMA_backquote($inputs[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : ''); - else - $w[] = PMA_backquote($inputs[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')'; - } - else { - $w[] = PMA_backquote($inputs[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;; - } - - } // end if - - if ($w) { - $sql_query .= ' WHERE ' . implode(' AND ', $w); + $unaryFlag = (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) ? true : false; + $w = PMA_tbl_search_getWhereClause($fields[$i],$inputs[$i], $types[$i], $collations[$i], $func_type, $unaryFlag); + if ($w != '') { + $sql_query .= ' WHERE ' . $w; } - echo $sql_query."
"; + print $sql_query."
"; } }