forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbl_select.php
322 lines (283 loc) · 10.7 KB
/
tbl_select.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Handles table search tab
*
* display table search form, create SQL query from form data
* and include sql.php to execute it
*
* @todo display search form again if no results from previous search
* @package PhpMyAdmin
*/
/**
* Gets some core libraries
*/
require_once 'libraries/common.inc.php';
require_once 'libraries/mysql_charsets.lib.php';
require_once 'libraries/tbl_select.lib.php';
$GLOBALS['js_include'][] = 'makegrid.js';
$GLOBALS['js_include'][] = 'sql.js';
$GLOBALS['js_include'][] = 'tbl_select.js';
$GLOBALS['js_include'][] = 'tbl_change.js';
$GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.16.custom.js';
$GLOBALS['js_include'][] = 'jquery/timepicker.js';
$GLOBALS['js_include'][] = 'gis_data_editor.js';
$titles['Browse'] = PMA_getIcon('b_browse.png', __('Browse foreign values'));
$geom_types = PMA_getGISDatatypes();
$post_params = array(
'ajax_request',
'collations',
'db',
'distinct',
'fields',
'func',
'max_number_of_fields',
'names',
'order',
'orderField',
'param',
'session_max_rows',
'table',
'types',
'where',
);
foreach ($post_params as $one_post_param) {
if (isset($_POST[$one_post_param])) {
$GLOBALS[$one_post_param] = $_POST[$one_post_param];
}
}
/**
* Not selection yet required -> displays the selection form
*/
if (! isset($param) || $param[0] == '') {
// Gets some core libraries
include_once 'libraries/tbl_common.php';
//$err_url = 'tbl_select.php' . $err_url;
$url_query .= '&goto=tbl_select.php&back=tbl_select.php';
/**
* Gets tables informations
*/
include_once 'libraries/tbl_info.inc.php';
/**
* Displays top menu links
*/
include_once 'libraries/tbl_links.inc.php';
if (! isset($goto)) {
$goto = $GLOBALS['cfg']['DefaultTabTable'];
}
// Defines the url to return to in case of error in the next sql statement
$err_url = $goto . '?' . PMA_generate_common_url($db, $table);
// Gets the list and number of fields
list($fields_list, $fields_type, $fields_collation, $fields_null, $geom_column_present) = PMA_tbl_getFields($db, $table);
$fields_cnt = count($fields_list);
// retrieve keys into foreign fields, if any
// check also foreigners even if relwork is FALSE (to get
// foreign keys from innodb)
$foreigners = PMA_getForeigners($db, $table);
?>
<fieldset id="fieldset_subtab">
<?php
$url_params = array();
$url_params['db'] = $db;
$url_params['table'] = $table;
echo PMA_generate_html_tabs(PMA_tbl_getSubTabs(), $url_params, '', 'topmenu2');
?>
<form method="post" action="tbl_select.php" name="insertForm" id="tbl_search_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax"' : ''); ?>>
<?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
<input type="hidden" name="goto" value="<?php echo $goto; ?>" />
<input type="hidden" name="back" value="tbl_select.php" />
<fieldset id="fieldset_table_search">
<fieldset id="fieldset_table_qbe">
<legend><?php echo __('Do a "query by example" (wildcard: "%")') ?></legend>
<table class="data">
<?php echo PMA_tbl_setTableHeader($geom_column_present); ?>
<tbody>
<?php
$odd_row = true;
for ($i = 0; $i < $fields_cnt; $i++) {
?>
<tr class="noclick <?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
<?php
// if 'Function' column is present
if ($geom_column_present) {
echo('<td>');
// if a geometry column
if (in_array($fields_type[$i], $geom_types)) {
echo('<select class="geom_func" name="geom_func['. $i .']">');
// get the relevant list of functions
$funcs = PMA_getGISFunctions($fields_type[$i], true, true);
foreach ($funcs as $func_name => $func) {
$name = isset($func['display']) ? $func['display'] : $func_name;
echo('<option value="' . htmlspecialchars($name) . '">'
. htmlspecialchars($name) . '</option>');
}
echo('</select>');
} else {
echo(' ');
}
echo('</td>');
}
?>
<th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
<td><?php echo htmlspecialchars($fields_type[$i]); ?></td>
<td><?php echo $fields_collation[$i]; ?></td>
<td><select name="func[]">
<?php
if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
echo "\n" . ' '
. '<option value="' . htmlspecialchars($fc) . '">'
. htmlspecialchars($fc) . '</option>';
}
} elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
echo "\n" . ' '
. '<option value="' . htmlspecialchars($fc) . '">'
. htmlspecialchars($fc) . '</option>';
}
} else {
foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
echo "\n" . ' '
. '<option value="' . htmlspecialchars($fc) . '">'
. htmlspecialchars($fc) . '</option>';
}
} // end if... else...
if ($fields_null[$i]) {
foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
echo "\n" . ' '
. '<option value="' . htmlspecialchars($fc) . '">'
. htmlspecialchars($fc) . '</option>';
}
}
?>
</select>
</td>
<td>
<?php
$field = $fields_list[$i];
$foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
echo PMA_getForeignFields_Values($foreigners, $foreignData, $field, $fields_type, $i, $db, $table, $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'], '', true);
?>
<input type="hidden" name="names[<?php echo $i; ?>]"
value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
<input type="hidden" name="types[<?php echo $i; ?>]"
value="<?php echo $fields_type[$i]; ?>" />
<input type="hidden" name="collations[<?php echo $i; ?>]"
value="<?php echo $fields_collation[$i]; ?>" />
</td>
</tr>
<?php
} // end for
?>
</tbody>
</table>
<div id="gis_editor"></div><div id="popup_background"></div>
</fieldset>
<?php
PMA_generate_slider_effect('searchoptions', __('Options'));
?>
<fieldset id="fieldset_select_fields">
<legend><?php echo __('Select columns (at least one):'); ?></legend>
<select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
multiple="multiple">
<?php
// Displays the list of the fields
foreach ($fields_list as $each_field) {
echo ' '
.'<option value="' . htmlspecialchars($each_field) . '"'
.' selected="selected">' . htmlspecialchars($each_field)
.'</option>' . "\n";
}
?>
</select>
<input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
<label for="oDistinct">DISTINCT</label>
</fieldset>
<fieldset id="fieldset_search_conditions">
<legend><?php echo '<em>' . __('Or') . '</em> ' . __('Add search conditions (body of the "where" clause):'); ?></legend>
<?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
<input type="text" name="where" class="textfield" size="64" />
</fieldset>
<fieldset id="fieldset_limit_rows">
<legend><?php echo __('Number of rows per page'); ?></legend>
<input type="text" size="4" name="session_max_rows"
value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
</fieldset>
<fieldset id="fieldset_display_order">
<legend><?php echo __('Display order:'); ?></legend>
<select name="orderField">
<option value="--nil--"></option>
<?php
foreach ($fields_list as $each_field) {
echo ' '
.'<option value="' . htmlspecialchars($each_field) . '">'
.htmlspecialchars($each_field) . '</option>' . "\n";
} // end for
?>
</select>
<?php
$choices = array(
'ASC' => __('Ascending'),
'DESC' => __('Descending')
);
PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
unset($choices);
?>
</fieldset>
<br style="clear: both;"/>
</div>
</fieldset>
<fieldset class="tblFooters">
<input type="hidden" name="max_number_of_fields"
value="<?php echo $fields_cnt; ?>" />
<input type="submit" name="submit" value="<?php echo __('Go'); ?>" />
</fieldset>
</form>
<div id="sqlqueryresults"></div>
</fieldset>
<?php
include 'libraries/footer.inc.php';
} else {
/**
* Selection criteria have been submitted -> do the work
*/
// Builds the query
$sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
// if all fields were selected to display, we do a SELECT *
// (more efficient and this helps prevent a problem in IE
// if one of the rows is edited and we come back to the Select results)
if (count($param) == $max_number_of_fields) {
$sql_query .= '* ';
} else {
$param = PMA_backquote($param);
$sql_query .= implode(', ', $param);
} // end if
// avoid a loop, for example when $cfg['DefaultTabTable'] is set
// to 'tbl_select.php'
unset($param);
$sql_query .= ' FROM ' . PMA_backquote($table);
// The where clause
if (trim($where) != '') {
$sql_query .= ' WHERE ' . $where;
} else {
$w = $charsets = array();
$cnt_func = count($func);
reset($func);
while (list($i, $func_type) = each($func)) {
list($charsets[$i]) = explode('_', $collations[$i]);
$unaryFlag = (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) ? true : false;
$tmp_geom_func = isset($geom_func[$i]) ? $geom_func[$i] : null;
$whereClause = PMA_tbl_search_getWhereClause($fields[$i], $names[$i], $types[$i], $collations[$i], $func_type, $unaryFlag, $tmp_geom_func);
if($whereClause)
$w[] = $whereClause;
} // end for
if ($w) {
$sql_query .= ' WHERE ' . implode(' AND ', $w);
}
} // end if
if ($orderField != '--nil--') {
$sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
} // end if
include 'sql.php';
}
?>