Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tbl_select.php - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8212a6c0a82067216449b6d14a802f40f6a957d2 (plain)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?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/timepicker.js';
$GLOBALS['js_include'][] = 'gis_data_editor.js';

$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.inc.php';
    //$err_url   = 'tbl_select.php' . $err_url;
    $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';

    /**
     * Gets table's information
     */
    include_once 'libraries/tbl_info.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);

    // Display the table search form
    PMA_tblSearchDisplaySelectionForm(
        $goto, $fields_list, $fields_type, $fields_collation, $fields_null,
        $geom_column_present, $geom_types, $fields_cnt, $foreigners, $db, $table
    );

    include 'libraries/footer.inc.php';
} else {
    /**
     * Selection criteria have been submitted -> do the work
     */
    $is_distinct = (isset($distinct)) ? 'true' : 'false';
    $sql_query = PMA_tblSearchBuildSqlQuery(
        $table, $fields, $names, $types, $param, $max_number_of_fields, $is_distinct,
        $where, $collations, $func, $orderField, $order
    );
    unset($is_distinct);
    include 'sql.php';
}

/**
 * Builds the sql search query from the post parameters
 *
 * @param string  $table                Selected table
 * @param array   $fields               Entered values of the columns
 * @param array   $names                Names of all columns
 * @param array   $types                Types of all columns
 * @param array   $param                Columns to be displayed in search results
 * @param integer $max_number_of_fields Total number of columns in the table
 * @param bool    $is_distinct          If only distinct values are needed
 * @param string  $where                The custom where clause
 * @param array   $collations           Collations of all columns
 * @param array   $func                 Operators for given column type
 * @param string  $orderField           Column by which results are to be ordered
 * @param string  $order                Whether ASC or DESC
 *
 * @return string the generated SQL query
 */
function PMA_tblSearchBuildSqlQuery($table, $fields, $names, $types, $param,
    $max_number_of_fields, $is_distinct, $where, $collations, $func, $orderField,
    $order)
{
    $sql_query = 'SELECT ';
    if($is_distinct) {
        $sql_query .= '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);
    $whereClause = PMA_tblSearchGenerateWhereClause(
        $fields, $names, $types, $where, $collations, $func
    );
    $sql_query .= $whereClause;
 
    // if the search results are to be ordered
    if ($orderField != '--nil--') {
        $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
    } // end if
    return $sql_query;
}

/**
 * Generates the where clause for the sql search query to be executed
 *
 * @param array  $fields     Entered values of the columns
 * @param array  $names      Names of all columns
 * @param array  $types      Types of all columns
 * @param string $where      The custom where clause
 * @param array  $collations Collations of all columns
 * @param array  $func       Operators for given column type
 *
 * @return string the generated where clause
 */
function PMA_tblSearchGenerateWhereClause($fields, $names, $types, $where,
    $collations, $func)
{
    // If the custom where clause is set
    if (trim($where) != '') {
        $fullWhereClause .= ' WHERE ' . $where;
        return $fullWhereClause;
    }

    $fullWhereClause = $charsets = array();
    reset($func);
    while (list($i, $func_type) = each($func)) {
        list($charsets[$i]) = explode('_', $collations[$i]);
        $unaryFlag =  $GLOBALS['PMA_Types']->isUnaryOperator($func_type);
        $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) {
            $fullWhereClause[] = $whereClause;
        }
    } // end while

    if ($fullWhereClause) {
        $fullWhereClause = ' WHERE ' . implode(' AND ', $fullWhereClause);
    }
    return $fullWhereClause;
}

/**
 * Generates HTML for a geometrical function column to be displayed in table
 * search selection form
 *
 * @param boolean $geom_column_present whether a geometry column is present
 * @param array   $fields_type         array containing types of all columns
 *                                     in the table
 * @param array   $geom_types          array of GIS data types
 * @param integer $column_index        index of current column in $fields_type array
 *
 * @return string the generated HTML
 */
function PMA_tblSearchGetGeomFuncHtml($geom_column_present, $fields_type,
$geom_types, $column_index)
{
    $html_output = '';
    // return if geometrical column is not present
    if (!$geom_column_present) {
        return $html_output;
    }

    /**
     * Displays 'Function' column if it is present
     */    
    $html_output .= '<td>';
    // if a geometry column is present
    if (in_array($fields_type[$column_index], $geom_types)) {
        $html_output .= '<select class="geom_func" name="geom_func[' . $column_index . ']">';
        // get the relevant list of GIS functions
        $funcs = PMA_getGISFunctions($fields_type[$column_index], true, true);
        /**
         * For each function in the list of functions, add an option to select list
         */
        foreach ($funcs as $func_name => $func) {
            $name = isset($func['display']) ? $func['display'] : $func_name;
            $html_output .= '<option value="' . htmlspecialchars($name) . '">'
                    . htmlspecialchars($name) . '</option>';
        }
        $html_output .= '</select>';
    } else {
        $html_output .= '&nbsp;';
    }
    $html_output .= '</td>';
    return $html_output;
}

/**
 * Displays formatted HTML for extra search options (slider) in table search form
 *
 * @param array   $fields_list array containing types of all columns
 *                             in the table
 * @param integer $fields_cnt  number of fields in the table
 *
 * @return void
 */
function PMA_tblSearchDisplaySliderOptions($fields_list, $fields_cnt)
{
    PMA_generateSliderEffect('searchoptions', __('Options'));
    $html_output = '';

    /**
     * Displays columns select list for selecting distinct columns in the search
     */
    $html_output = '<fieldset id="fieldset_select_fields">'
        . '<legend>' . __('Select columns (at least one):') . '</legend>'
        . '<select name="param[]" size="' . min($fields_cnt, 10)
        . '" multiple="multiple">';
    // Displays the list of the fields
    foreach ($fields_list as $each_field) {
        $html_output .= '        '
            . '<option value="' . htmlspecialchars($each_field) . '"'
            . ' selected="selected">' . htmlspecialchars($each_field)
            . '</option>' . "\n";
    } // end for
    $html_output .= '</select>'
        . '<input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />'
        . '<label for="oDistinct">DISTINCT</label></fieldset>';

    /**
     * Displays input box for custom 'Where' clause to be used in the search
     */
    $html_output .= '<fieldset id="fieldset_search_conditions">'
        . '<legend>' . '<em>' . __('Or') . '</em> '
        . __('Add search conditions (body of the "where" clause):') . '</legend>';
    $html_output .= PMA_showMySQLDocu('SQL-Syntax', 'Functions');
    $html_output .= '<input type="text" name="where" class="textfield" size="64" />'
        . '</fieldset>';

    /**
     * Displays option of changing default number of rows displayed per page
     */
    $html_output .= '<fieldset id="fieldset_limit_rows">'
        . '<legend>' . __('Number of rows per page') . '</legend>'
        . '<input type="text" size="4" name="session_max_rows" '
        . 'value="' . $GLOBALS['cfg']['MaxRows'] . '" class="textfield" />'
        . '</fieldset>';

    /**
     * Displays option for ordering search results by a column value (Asc or Desc)
     */
    $html_output .= '<fieldset id="fieldset_display_order">'
        . '<legend>' . __('Display order:') . '</legend>'
        . '<select name="orderField"><option value="--nil--"></option>';
    foreach ($fields_list as $each_field) {
        $html_output .= '        '
            . '<option value="' . htmlspecialchars($each_field) . '">'
            . htmlspecialchars($each_field) . '</option>' . "\n";
    } // end for
    $html_output .= '</select>';
    $choices = array(
        'ASC' => __('Ascending'),
        'DESC' => __('Descending')
    );
    echo $html_output;
    PMA_displayHtmlRadio('order', $choices, 'ASC', false, true, "formelement");
    unset($choices);    
}

/**
 * Generates HTML for displaying fields table in search form
 *
 * @param array   $fields_list         Names of columns in the table
 * @param array   $fields_type         Types of columns in the table
 * @param array   $fields_collation    Collation of all columns
 * @param array   $fields_null         Null information of columns
 * @param boolean $geom_column_present Whether a geometry column is present
 * @param array   $geom_types          array of GIS data types
 * @param integer $fields_cnt          Number of columns in the table
 * @param array   $foreigners          Array of foreign keys
 * @param string  $db                  Selected database
 * @param string  $table               Selected table
 *
 * @return string the generated HTML
 */
function PMA_tblSearchGetFieldsTableHtml($fields_list, $fields_type,
$fields_collation, $fields_null, $geom_column_present, $geom_types, $fields_cnt,
$foreigners, $db, $table)
{
    $html_output = '';
    $html_output .= '<table class="data">';
    $html_output .= PMA_tbl_setTableHeader($geom_column_present) . '<tbody>';
    $odd_row = true;
    $titles['Browse'] = PMA_getIcon('b_browse.png', __('Browse foreign values'));

    // for every column present in table
    for ($i = 0; $i < $fields_cnt; $i++) {
        $html_output .= '<tr class="noclick ' . ($odd_row ? 'odd' : 'even') . '">';
        $odd_row = !$odd_row;

        /**
         * If 'Function' column is present
         */
        $html_output .= PMA_tblSearchGetGeomFuncHtml(
            $geom_column_present, $fields_type, $geom_types, $i
        );
        /**
         * Displays column's name, type, collation
         */
        $html_output .= '<th>' . htmlspecialchars($fields_list[$i]) . '</th>';
        $html_output .= '<td>' . htmlspecialchars($fields_type[$i]) . '</td>';
        $html_output .= '<td>' . $fields_collation[$i] . '</td>';
        /**
         * Displays column's comparison operators depending on column type
         */
        $html_output .= '<td><select name="func[]">';
        $html_output .= $GLOBALS['PMA_Types']->getTypeOperatorsHtml(
            $fields_type[$i], $fields_null[$i]
        );
        $html_output .= '</select></td><td>';
        /**
         * Displays column's foreign relations if any
         */
        $field = $fields_list[$i];
        $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
        $html_output .= PMA_getForeignFields_Values(
            $foreigners, $foreignData, $field, $fields_type, $i, $db, $table,
            $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'], '', true
        );

        $html_output .= '<input type="hidden" name="names[' . $i . ']" value="' 
            . htmlspecialchars($fields_list[$i]) . '" /><input type="hidden" '
            . 'name="types[' . $i . ']" value="' . $fields_type[$i] . '" />'
            . '<input type="hidden" name="collations[' . $i . ']" value="'
            . $fields_collation[$i] . '" /></td></tr>';
    } // end for

    $html_output .= '</tbody></table>';
    return $html_output;
}

/**
 * Displays the table search form under table search tab
 *
 * @param string  $goto                
 * @param array   $fields_list         Names of columns in the table
 * @param array   $fields_type         Types of columns in the table
 * @param array   $fields_collation    Collation of all columns
 * @param array   $fields_null         Null information of columns
 * @param boolean $geom_column_present Whether a geometry column is present
 * @param array   $geom_types          array of GIS data types
 * @param integer $fields_cnt          Number of columns in the table
 * @param array   $foreigners          Array of foreign keys
 * @param string  $db                  Selected database
 * @param string  $table               Selected table
 *
 * @return void
 */
function PMA_tblSearchDisplaySelectionForm($goto, $fields_list, $fields_type,
$fields_collation, $fields_null, $geom_column_present, $geom_types, $fields_cnt,
$foreigners, $db, $table)
{
    $html_output = '';
    $html_output .= '<fieldset id="fieldset_subtab">';
    $url_params = array();
    $url_params['db'] = $db;
    $url_params['table'] = $table;

    $html_output .= PMA_generateHtmlTabs(PMA_tbl_getSubTabs(), $url_params, 'topmenu2');
    $html_output .= '<form method="post" action="tbl_select.php" name="insertForm"'
        . ' id="tbl_search_form" ' . ($GLOBALS['cfg']['AjaxEnable'] ? 'class="ajax"' : '')
        . '>';
    $html_output .= PMA_generate_common_hidden_inputs($db, $table);
    $html_output .= '<input type="hidden" name="goto" value="' . $goto . '" />';
    $html_output .= '<input type="hidden" name="back" value="tbl_select.php" />'
        . '<fieldset id="fieldset_table_search"><fieldset id="fieldset_table_qbe">'
        . '<legend>' . __('Do a "query by example" (wildcard: "%")') . '</legend>';

    /**
     * Displays table fields
     */
    $html_output .= PMA_tblSearchGetFieldsTableHtml(
        $fields_list, $fields_type, $fields_collation, $fields_null,
        $geom_column_present, $geom_types, $fields_cnt, $foreigners, $db, $table    
    );

    $html_output .= '<div id="gis_editor"></div><div id="popup_background"></div>'
        . '</fieldset>';
    echo $html_output;

    /**
     * Displays slider options form
     */
    PMA_tblSearchDisplaySliderOptions($fields_list, $fields_cnt);
    $html_output = '</fieldset><br style="clear: both;"/></div></fieldset>';

    /**
     * Displays selection form's footer elements
     */
    $html_output .= '<fieldset class="tblFooters"><input type="hidden" '
        . 'name="max_number_of_fields" value="' . $fields_cnt . '" />'
        . '<input type="submit" name="submit" value="' . __('Go') . '" />'
        . '</fieldset></form><div id="sqlqueryresults"></div></fieldset>';
    echo $html_output;
}
?>