There are no triggers in Drizzle
[phpmyadmin.git] / tbl_select.php
blob056b9e4de78f9d39a2c49c8244dc2d36f817f300
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
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
10 * @package phpMyAdmin
13 /**
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'][] = 'jquery/jquery-ui-1.8.custom.js';
23 $GLOBALS['js_include'][] = 'jquery/timepicker.js';
24 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
25 $titles['Browse'] =
26 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
27 .'b_browse.png" alt="' . __('Browse foreign values') . '" title="'
28 . __('Browse foreign values') . '" />';
30 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
31 $titles['Browse'] .= __('Browse foreign values');
33 } else {
34 $titles['Browse'] = __('Browse foreign values');
37 /**
38 * Not selection yet required -> displays the selection form
40 if (! isset($param) || $param[0] == '') {
41 // Gets some core libraries
42 require_once './libraries/tbl_common.php';
43 //$err_url = 'tbl_select.php' . $err_url;
44 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
46 /**
47 * Gets tables informations
49 require_once './libraries/tbl_info.inc.php';
51 /**
52 * Displays top menu links
54 require_once './libraries/tbl_links.inc.php';
56 if (! isset($goto)) {
57 $goto = $GLOBALS['cfg']['DefaultTabTable'];
59 // Defines the url to return to in case of error in the next sql statement
60 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
62 // Gets the list and number of fields
63 $result = PMA_DBI_query(PMA_DBI_get_columns_sql($db, $table, null, true), null, PMA_DBI_QUERY_STORE);
64 $fields_cnt = PMA_DBI_num_rows($result);
65 $fields_list = $fields_null = $fields_type = $fields_collation = array();
66 while ($row = PMA_DBI_fetch_assoc($result)) {
67 $fields_list[] = $row['Field'];
68 $type = $row['Type'];
69 // reformat mysql query output
70 if (strncasecmp($type, 'set', 3) == 0
71 || strncasecmp($type, 'enum', 4) == 0) {
72 $type = str_replace(',', ', ', $type);
73 } else {
75 // strip the "BINARY" attribute, except if we find "BINARY(" because
76 // this would be a BINARY or VARBINARY field type
77 if (!preg_match('@BINARY[\(]@i', $type)) {
78 $type = preg_replace('@BINARY@i', '', $type);
80 $type = preg_replace('@ZEROFILL@i', '', $type);
81 $type = preg_replace('@UNSIGNED@i', '', $type);
83 $type = strtolower($type);
85 if (empty($type)) {
86 $type = '&nbsp;';
88 $fields_null[] = $row['Null'];
89 $fields_type[] = $type;
90 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
91 ? $row['Collation']
92 : '';
93 } // end while
94 PMA_DBI_free_result($result);
95 unset($result, $type);
97 // retrieve keys into foreign fields, if any
98 // check also foreigners even if relwork is FALSE (to get
99 // foreign keys from innodb)
100 $foreigners = PMA_getForeigners($db, $table);
102 <form method="post" action="tbl_select.php" name="insertForm" id="tbl_search_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax"' : ''); ?>>
103 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
104 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
105 <input type="hidden" name="back" value="tbl_select.php" />
107 <fieldset id="fieldset_table_search">
109 <fieldset id="fieldset_table_qbe">
110 <legend><?php echo __('Do a "query by example" (wildcard: "%")') ?></legend>
111 <table class="data">
112 <thead>
113 <tr><th><?php echo __('Column'); ?></th>
114 <th><?php echo __('Type'); ?></th>
115 <th><?php echo __('Collation'); ?></th>
116 <th><?php echo __('Operator'); ?></th>
117 <th><?php echo __('Value'); ?></th>
118 </tr>
119 </thead>
120 <tbody>
121 <?php
122 $odd_row = true;
124 for ($i = 0; $i < $fields_cnt; $i++) {
126 <tr class="noclick <?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
127 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
128 <td><?php echo $fields_type[$i]; ?></td>
129 <td><?php echo $fields_collation[$i]; ?></td>
130 <td><select name="func[]">
131 <?php
132 // determine valid operators
133 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
134 // enum operators
135 $operators = array(
136 '=',
137 '!=',
139 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
140 // text operators
141 $operators = array(
142 'LIKE',
143 'LIKE %...%',
144 'NOT LIKE',
145 '=',
146 '!=',
147 'REGEXP',
148 'REGEXP ^...$',
149 'NOT REGEXP',
150 "= ''",
151 "!= ''",
152 'IN (...)',
153 'NOT IN (...)',
154 'BETWEEN',
155 'NOT BETWEEN',
157 } else {
158 // numeric operators
159 $operators = array(
160 '=',
161 '>',
162 '>=',
163 '<',
164 '<=',
165 '!=',
166 'LIKE',
167 'NOT LIKE',
168 'IN (...)',
169 'NOT IN (...)',
170 'BETWEEN',
171 'NOT BETWEEN',
173 } // end if... else...
175 // if field can be NULL, add IS NULL and IS NOT NULL
176 if ($fields_null[$i]) {
177 $operators[] = 'IS NULL';
178 $operators[] = 'IS NOT NULL';
180 foreach ($operators as $op) {
181 echo "\n" . ' '
182 . '<option value="' . htmlspecialchars($op) . '">' . htmlspecialchars($op) . '</option>';
186 </select>
187 </td>
188 <td>
189 <?php
190 $field = $fields_list[$i];
192 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
194 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
195 // f o r e i g n k e y s
196 echo ' <select name="fields[' . $i . ']">' . "\n";
197 // go back to first row
199 // here, the 4th parameter is empty because there is no current
200 // value of data for the dropdown (the search page initial values
201 // are displayed empty)
202 echo PMA_foreignDropdown($foreignData['disp_row'],
203 $foreignData['foreign_field'],
204 $foreignData['foreign_display'],
205 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
206 echo ' </select>' . "\n";
207 } elseif ($foreignData['foreign_link'] == true) {
209 <input type="text" name="fields[<?php echo $i; ?>]"
210 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
211 class="textfield" />
212 <script type="text/javascript">
213 // <![CDATA[
214 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); ?>&amp;field=<?php echo urlencode($field); ?>&amp;fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
215 // ]]>
216 </script>
217 <?php
218 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
219 // e n u m s
220 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
221 $cnt_enum_value = count($enum_value);
222 echo ' <select name="fields[' . $i . '][]"'
223 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
224 for ($j = 0; $j < $cnt_enum_value; $j++) {
225 echo ' <option value="' . $enum_value[$j] . '">'
226 . $enum_value[$j] . '</option>';
227 } // end for
228 echo ' </select>' . "\n";
229 } else {
230 // o t h e r c a s e s
231 $the_class = 'textfield';
232 $type = $fields_type[$i];
233 if ($type == 'date') {
234 $the_class .= ' datefield';
235 } elseif ($type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
236 $the_class .= ' datetimefield';
238 echo ' <input type="text" name="fields[' . $i . ']"'
239 .' size="40" class="' . $the_class . '" id="field_' . $i . '" />' . "\n";
242 <input type="hidden" name="names[<?php echo $i; ?>]"
243 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
244 <input type="hidden" name="types[<?php echo $i; ?>]"
245 value="<?php echo $fields_type[$i]; ?>" />
246 <input type="hidden" name="collations[<?php echo $i; ?>]"
247 value="<?php echo $fields_collation[$i]; ?>" />
248 </td>
249 </tr>
250 <?php
251 } // end for
253 </tbody>
254 </table>
255 </fieldset>
256 <?php
257 PMA_generate_slider_effect('searchoptions', __('Options'));
259 <fieldset id="fieldset_select_fields">
260 <legend><?php echo __('Select columns (at least one):'); ?></legend>
261 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
262 multiple="multiple">
263 <?php
264 // Displays the list of the fields
265 foreach ($fields_list as $each_field) {
266 echo ' '
267 .'<option value="' . htmlspecialchars($each_field) . '"'
268 .' selected="selected">' . htmlspecialchars($each_field)
269 .'</option>' . "\n";
272 </select>
273 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
274 <label for="oDistinct">DISTINCT</label>
275 </fieldset>
277 <fieldset id="fieldset_search_conditions">
278 <legend><?php echo '<em>' . __('Or') . '</em> ' . __('Add search conditions (body of the "where" clause):'); ?></legend>
279 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
281 <input type="text" name="where" class="textfield" size="64" />
282 </fieldset>
284 <fieldset id="fieldset_limit_rows">
285 <legend><?php echo __('Number of rows per page'); ?></legend>
286 <input type="text" size="4" name="session_max_rows"
287 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
288 </fieldset>
290 <fieldset id="fieldset_display_order">
291 <legend><?php echo __('Display order:'); ?></legend>
292 <select name="orderField">
293 <option value="--nil--"></option>
294 <?php
295 foreach ($fields_list as $each_field) {
296 echo ' '
297 .'<option value="' . htmlspecialchars($each_field) . '">'
298 .htmlspecialchars($each_field) . '</option>' . "\n";
299 } // end for
301 </select>
302 <?php
303 $choices = array(
304 'ASC' => __('Ascending'),
305 'DESC' => __('Descending')
307 PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
308 unset($choices);
310 </fieldset>
311 <br style="clear: both;"/>
312 </div>
313 </fieldset>
314 <fieldset class="tblFooters">
315 <input type="hidden" name="max_number_of_fields"
316 value="<?php echo $fields_cnt; ?>" />
317 <input type="submit" name="submit" value="<?php echo __('Go'); ?>" />
318 </fieldset>
319 </form>
320 <div id="sqlqueryresults"></div>
321 <?php
322 require './libraries/footer.inc.php';
327 * Selection criteria have been submitted -> do the work
329 else {
330 // Builds the query
332 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
334 // if all fields were selected to display, we do a SELECT *
335 // (more efficient and this helps prevent a problem in IE
336 // if one of the rows is edited and we come back to the Select results)
338 if (count($param) == $max_number_of_fields) {
339 $sql_query .= '* ';
340 } else {
341 $param = PMA_backquote($param);
342 $sql_query .= implode(', ', $param);
343 } // end if
345 // avoid a loop, for example when $cfg['DefaultTabTable'] is set
346 // to 'tbl_select.php'
347 unset($param);
349 $sql_query .= ' FROM ' . PMA_backquote($table);
351 // The where clause
352 if (trim($where) != '') {
353 $sql_query .= ' WHERE ' . $where;
354 } else {
355 $w = $charsets = array();
356 $unary_operators = array(
357 'IS NULL' => 1,
358 'IS NOT NULL' => 1,
359 "= ''" => 1,
360 "!= ''" => 1
362 $cnt_func = count($func);
363 reset($func);
364 while (list($i, $func_type) = each($func)) {
365 list($charsets[$i]) = explode('_', $collations[$i]);
366 if (isset($unary_operators[$func_type])) {
367 $fields[$i] = '';
368 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
370 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
371 if (!empty($fields[$i])) {
372 if (! is_array($fields[$i])) {
373 $fields[$i] = explode(',', $fields[$i]);
375 $enum_selected_count = count($fields[$i]);
376 if ($func_type == '=' && $enum_selected_count > 1) {
377 $func_type = $func[$i] = 'IN';
378 $parens_open = '(';
379 $parens_close = ')';
381 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
382 $func_type = $func[$i] = 'NOT IN';
383 $parens_open = '(';
384 $parens_close = ')';
386 } else {
387 $parens_open = '';
388 $parens_close = '';
390 $enum_where = '\'' . PMA_sqlAddSlashes($fields[$i][0]) . '\'';
391 for ($e = 1; $e < $enum_selected_count; $e++) {
392 $enum_where .= ', \'' . PMA_sqlAddSlashes($fields[$i][$e]) . '\'';
395 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
398 } elseif ($fields[$i] != '') {
399 // For these types we quote the value. Even if it's another type (like INT),
400 // for a LIKE we always quote the value. MySQL converts strings to numbers
401 // and numbers to strings as necessary during the comparison
402 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
403 $quot = '\'';
404 } else {
405 $quot = '';
408 // LIKE %...%
409 if ($func_type == 'LIKE %...%') {
410 $func_type = 'LIKE';
411 $fields[$i] = '%' . $fields[$i] . '%';
413 if ($func_type == 'REGEXP ^...$') {
414 $func_type = 'REGEXP';
415 $fields[$i] = '^' . $fields[$i] . '$';
418 if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
419 $func_type = str_replace(' (...)', '', $func_type);
421 // quote values one by one
422 $values = explode(',', $fields[$i]);
423 foreach ($values as &$value)
424 $value = $quot . PMA_sqlAddSlashes(trim($value)) . $quot;
426 if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN')
427 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : '');
428 else
429 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
431 else {
432 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddSlashes($fields[$i]) . $quot;;
435 } // end if
436 } // end for
438 if ($w) {
439 $sql_query .= ' WHERE ' . implode(' AND ', $w);
441 } // end if
443 if ($orderField != '--nil--') {
444 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
445 } // end if
447 require './sql.php';