2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Handles table search tab
6 * display table search form, create SQL query from form data
7 * and include sql.php to execute it
9 * @todo display search form again if no results from previous search
14 * Gets some core libraries
16 require_once './libraries/common.inc.php';
17 require_once './libraries/mysql_charsets.lib.php';
19 $GLOBALS['js_include'][] = 'makegrid.js';
20 $GLOBALS['js_include'][] = 'sql.js';
21 $GLOBALS['js_include'][] = 'tbl_select.js';
22 $GLOBALS['js_include'][] = 'tbl_change.js';
23 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
24 $GLOBALS['js_include'][] = 'jquery/timepicker.js';
25 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
27 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
28 .'b_browse.png" alt="' . __('Browse foreign values') . '" title="'
29 . __('Browse foreign values') . '" />';
31 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
32 $titles['Browse'] .= __('Browse foreign values');
35 $titles['Browse'] = __('Browse foreign values');
38 $geom_types = PMA_getGISDatatypes();
40 * Not selection yet required -> displays the selection form
42 if (! isset($param) ||
$param[0] == '') {
43 // Gets some core libraries
44 require_once './libraries/tbl_common.php';
45 //$err_url = 'tbl_select.php' . $err_url;
46 $url_query .= '&goto=tbl_select.php&back=tbl_select.php';
49 * Gets tables informations
51 require_once './libraries/tbl_info.inc.php';
54 * Displays top menu links
56 require_once './libraries/tbl_links.inc.php';
59 $goto = $GLOBALS['cfg']['DefaultTabTable'];
61 // Defines the url to return to in case of error in the next sql statement
62 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
64 // Gets the list and number of fields
65 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE
);
66 $fields_cnt = PMA_DBI_num_rows($result);
67 $fields_list = $fields_null = $fields_type = $fields_collation = array();
68 $geom_column_present = false;
69 while ($row = PMA_DBI_fetch_assoc($result)) {
70 $fields_list[] = $row['Field'];
72 // check whether table contains geometric columns
73 if (in_array($type, $geom_types)) {
74 $geom_column_present = true;
76 // reformat mysql query output
77 if (strncasecmp($type, 'set', 3) == 0
78 ||
strncasecmp($type, 'enum', 4) == 0) {
79 $type = str_replace(',', ', ', $type);
82 // strip the "BINARY" attribute, except if we find "BINARY(" because
83 // this would be a BINARY or VARBINARY field type
84 if (!preg_match('@BINARY[\(]@i', $type)) {
85 $type = preg_replace('@BINARY@i', '', $type);
87 $type = preg_replace('@ZEROFILL@i', '', $type);
88 $type = preg_replace('@UNSIGNED@i', '', $type);
90 $type = strtolower($type);
95 $fields_null[] = $row['Null'];
96 $fields_type[] = $type;
97 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
101 PMA_DBI_free_result($result);
102 unset($result, $type);
104 // retrieve keys into foreign fields, if any
105 // check also foreigners even if relwork is FALSE (to get
106 // foreign keys from innodb)
107 $foreigners = PMA_getForeigners($db, $table);
109 <form method
="post" action
="tbl_select.php" name
="insertForm" id
="tbl_search_form" <?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax"' : ''); ?
>>
110 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
111 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
112 <input type
="hidden" name
="back" value
="tbl_select.php" />
114 <fieldset id
="fieldset_table_search">
116 <fieldset id
="fieldset_table_qbe">
117 <legend
><?php
echo __('Do a "query by example" (wildcard: "%")') ?
></legend
>
121 // Display the Function column only if there is alteast one geomety colum
122 if ($geom_column_present) {
123 echo('<th>'); echo __('Function'); echo('</th>');
126 <th
><?php
echo __('Column'); ?
></th
>
127 <th
><?php
echo __('Type'); ?
></th
>
128 <th
><?php
echo __('Collation'); ?
></th
>
129 <th
><?php
echo __('Operator'); ?
></th
>
130 <th
><?php
echo __('Value'); ?
></th
>
137 for ($i = 0; $i < $fields_cnt; $i++
) {
139 <tr
class="noclick <?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
141 // if 'Function' column is present
142 if ($geom_column_present) {
144 // if a geometry column
145 if (in_array($fields_type[$i], $geom_types)) {
146 echo('<select name="geom_func['. $i .']">');
147 // get the relevant list of functions
148 $funcs = PMA_getGISFunctions($fields_type[$i], false, true);
149 foreach ($funcs as $func_name => $func) {
150 $name = isset($func['display']) ?
$func['display'] : $func_name;
151 echo('<option value="' . htmlspecialchars($name) . '">'
152 . htmlspecialchars($name) . '</option>');
161 <th
><?php
echo htmlspecialchars($fields_list[$i]); ?
></th
>
162 <td
><?php
echo $fields_type[$i]; ?
></td
>
163 <td
><?php
echo $fields_collation[$i]; ?
></td
>
164 <td
><select name
="func[]">
166 // determine valid operators
167 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
173 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
207 } // end if... else...
209 // if field can be NULL, add IS NULL and IS NOT NULL
210 if ($fields_null[$i]) {
211 $operators[] = 'IS NULL';
212 $operators[] = 'IS NOT NULL';
214 foreach ($operators as $op) {
216 . '<option value="' . htmlspecialchars($op) . '">' . htmlspecialchars($op) . '</option>';
224 $field = $fields_list[$i];
226 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
228 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
229 // f o r e i g n k e y s
230 echo ' <select name="fields[' . $i . ']">' . "\n";
231 // go back to first row
233 // here, the 4th parameter is empty because there is no current
234 // value of data for the dropdown (the search page initial values
235 // are displayed empty)
236 echo PMA_foreignDropdown($foreignData['disp_row'],
237 $foreignData['foreign_field'],
238 $foreignData['foreign_display'],
239 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
240 echo ' </select>' . "\n";
241 } elseif ($foreignData['foreign_link'] == true) {
243 <input type
="text" name
="fields[<?php echo $i; ?>]"
244 id
="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
246 <script type
="text/javascript">
248 document
.writeln('<a target="_blank" onclick="window.open(this.href, \'foreigners\', \'width=640,height=240,scrollbars=yes\'); return false" href="browse_foreigners.php?<?php echo PMA_generate_common_url($db, $table); ?>&field=<?php echo urlencode($field); ?>&fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'
", $titles['Browse']); ?></a>');
252 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
254 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
255 $cnt_enum_value = count($enum_value);
256 echo ' <select name
="fields[' . $i . '][]"'
257 .' multiple
="multiple" size
="' . min(3, $cnt_enum_value) . '">' . "\n";
258 for ($j = 0; $j < $cnt_enum_value; $j++) {
259 echo ' <option value
="' . $enum_value[$j] . '">'
260 . $enum_value[$j] . '</option
>';
262 echo ' </select
>' . "\n";
264 // o t h e r c a s e s
265 $the_class = 'textfield
';
266 $type = $fields_type[$i];
267 if ($type == 'date
') {
268 $the_class .= ' datefield
';
269 } elseif ($type == 'datetime
' || substr($type, 0, 9) == 'timestamp
') {
270 $the_class .= ' datetimefield
';
272 echo ' <input type
="text" name
="fields[' . $i . ']"'
273 .' size
="40" class="' . $the_class . '" id
="field_' . $i . '" />' . "\n";
276 <input type="hidden" name="names[<?php echo $i; ?>]"
277 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
278 <input type="hidden" name="types[<?php echo $i; ?>]"
279 value="<?php echo $fields_type[$i]; ?>" />
280 <input type="hidden" name="collations[<?php echo $i; ?>]"
281 value="<?php echo $fields_collation[$i]; ?>" />
291 PMA_generate_slider_effect('searchoptions
', __('Options
'));
293 <fieldset id="fieldset_select_fields">
294 <legend><?php echo __('Select
columns (at least one
):'); ?></legend>
295 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
298 // Displays the list of the fields
299 foreach ($fields_list as $each_field) {
301 .'<option value
="' . htmlspecialchars($each_field) . '"'
302 .' selected
="selected">' . htmlspecialchars($each_field)
307 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
308 <label for="oDistinct">DISTINCT</label>
311 <fieldset id="fieldset_search_conditions">
312 <legend><?php echo '<em
>' . __('Or') . '</em
> ' . __('Add search
conditions (body of the
"where" clause
):'); ?></legend>
313 <?php echo PMA_showMySQLDocu('SQL
-Syntax
', 'Functions
'); ?>
315 <input type="text" name="where" class="textfield" size="64" />
318 <fieldset id="fieldset_limit_rows">
319 <legend><?php echo __('Number of rows per page
'); ?></legend>
320 <input type="text" size="4" name="session_max_rows"
321 value="<?php echo $GLOBALS['cfg
']['MaxRows
']; ?>" class="textfield" />
324 <fieldset id="fieldset_display_order">
325 <legend><?php echo __('Display order
:'); ?></legend>
326 <select name="orderField">
327 <option value="--nil--"></option>
329 foreach ($fields_list as $each_field) {
331 .'<option value
="' . htmlspecialchars($each_field) . '">'
332 .htmlspecialchars($each_field) . '</option
>' . "\n";
338 'ASC
' => __('Ascending
'),
339 'DESC
' => __('Descending
')
341 PMA_display_html_radio('order
', $choices, 'ASC
', false, true, "formelement");
345 <br style="clear: both;"/>
348 <fieldset class="tblFooters">
349 <input type="hidden" name="max_number_of_fields"
350 value="<?php echo $fields_cnt; ?>" />
351 <input type="submit" name="submit" value="<?php echo __('Go
'); ?>" />
354 <div id="sqlqueryresults"></div>
356 require './libraries
/footer
.inc
.php
';
361 * Selection criteria have been submitted -> do the work
366 $sql_query = 'SELECT
' . (isset($distinct) ? 'DISTINCT
' : '');
368 // if all fields were selected to display, we do a SELECT *
369 // (more efficient and this helps prevent a problem in IE
370 // if one of the rows is edited and we come back to the Select results)
372 if (count($param) == $max_number_of_fields) {
375 $param = PMA_backquote($param);
376 $sql_query .= implode(', ', $param);
379 // avoid a loop, for example when $cfg['DefaultTabTable
'] is set
380 // to 'tbl_select
.php
'
383 $sql_query .= ' FROM
' . PMA_backquote($table);
386 if (trim($where) != '') {
387 $sql_query .= ' WHERE
' . $where;
389 $w = $charsets = array();
390 $unary_operators = array(
396 $geom_unary_operators = array(
402 $cnt_func = count($func);
404 while (list($i, $func_type) = each($func)) {
405 // If geometry function is set apply it to the field name
406 if (isset($geom_func[$i]) && trim($geom_func[$i]) != '') {
407 $backquoted_name = $geom_func[$i] . '(' . PMA_backquote($names[$i]) . ')';
409 // New output type is the output type of the function being applied
410 $geom_funcs = PMA_getGISFunctions($types[$i], false, false);
411 $types[$i] = $geom_funcs[$geom_func[$i]]['type
'];
413 // If the intended where clause is something like 'IsEmpty(`spatial_col_name`
)'
414 if (isset($geom_unary_operators[$geom_func[$i]]) && trim($fields[$i]) == '') {
415 $w[] = $backquoted_name;
419 $backquoted_name = PMA_backquote($names[$i]);
422 list($charsets[$i]) = explode('_
', $collations[$i]);
423 if (isset($unary_operators[$func_type])) {
425 $w[] = $backquoted_name . ' ' . $func_type;
427 } elseif (in_array($types[$i], $geom_types)) {
428 $w[] = $backquoted_name . ' ' . $func_type . " GeomFromText('" . $fields[$i] . "')";
429 } elseif (strncasecmp($types[$i], 'enum
', 4) == 0) {
430 if (!empty($fields[$i])) {
431 if (! is_array($fields[$i])) {
432 $fields[$i] = explode(',', $fields[$i]);
434 $enum_selected_count = count($fields[$i]);
435 if ($func_type == '=' && $enum_selected_count > 1) {
436 $func_type = $func[$i] = 'IN
';
440 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
441 $func_type = $func[$i] = 'NOT IN
';
449 $enum_where = '\'
' . PMA_sqlAddSlashes($fields[$i][0]) . '\'
';
450 for ($e = 1; $e < $enum_selected_count; $e++) {
451 $enum_where .= ', \'' . PMA_sqlAddSlashes($fields[$i][$e]) . '\'
';
454 $w[] = $backquoted_name . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
457 } elseif ($fields[$i] != '') {
458 // For these types we quote the value. Even if it's another
type (like
INT),
459 // for a LIKE we always quote the value. MySQL converts strings to numbers
460 // and numbers to strings as necessary during the comparison
461 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) ||
strpos(' ' . $func_type, 'LIKE')) {
468 if ($func_type == 'LIKE %...%') {
470 $fields[$i] = '%' . $fields[$i] . '%';
472 if ($func_type == 'REGEXP ^...$') {
473 $func_type = 'REGEXP';
474 $fields[$i] = '^' . $fields[$i] . '$';
477 if ($func_type == 'IN (...)' ||
$func_type == 'NOT IN (...)' ||
$func_type == 'BETWEEN' ||
$func_type == 'NOT BETWEEN') {
478 $func_type = str_replace(' (...)', '', $func_type);
480 // quote values one by one
481 $values = explode(',', $fields[$i]);
482 foreach ($values as &$value) {
483 $value = $quot . PMA_sqlAddSlashes(trim($value)) . $quot;
486 if ($func_type == 'BETWEEN' ||
$func_type == 'NOT BETWEEN') {
487 $w[] = $backquoted_name . ' ' . $func_type . ' ' . (isset($values[0]) ?
$values[0] : '') . ' AND ' . (isset($values[1]) ?
$values[1] : '');
489 $w[] = $backquoted_name . ' ' . $func_type . ' (' . implode(',', $values) . ')';
493 $w[] = $backquoted_name . ' ' . $func_type . ' ' . $quot . PMA_sqlAddSlashes($fields[$i]) . $quot;;
500 $sql_query .= ' WHERE ' . implode(' AND ', $w);
504 if ($orderField != '--nil--') {
505 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;