Translation update done using Pootle.
[phpmyadmin/crack.git] / db_qbe.php
blob7a08f07bc517d162aa8a36ae56730b458c66733f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * query by example the whole database
6 * @package phpMyAdmin
7 */
9 /**
10 * requirements
12 require_once './libraries/common.inc.php';
13 require_once './libraries/Table.class.php';
14 require_once './libraries/relation.lib.php';
17 /**
18 * Gets the relation settings
20 $cfgRelation = PMA_getRelationsParam();
23 /**
24 * A query has been submitted -> execute it, else display the headers
26 if (isset($_REQUEST['submit_sql']) && ! empty($sql_query)) {
27 $goto = 'db_sql.php';
28 $zero_rows = htmlspecialchars(__('Your SQL query has been executed successfully'));
29 require './sql.php';
30 exit;
31 } else {
32 $sub_part = '_qbe';
33 require './libraries/db_common.inc.php';
34 $url_query .= '&amp;goto=db_qbe.php';
35 $url_params['goto'] = 'db_qbe.php';
36 require './libraries/db_info.inc.php';
39 if (isset($_REQUEST['submit_sql'])
40 && ! preg_match('@^SELECT@i', $sql_query)) {
41 PMA_Message::warning(__('You have to choose at least one column to display'))->display();
45 /**
46 * Initialize some variables
48 $col_cnt = PMA_ifSetOr($_REQUEST['col_cnt'], 3, 'numeric');
49 $add_col = PMA_ifSetOr($_REQUEST['add_col'], 0, 'numeric');
50 $add_row = PMA_ifSetOr($_REQUEST['add_row'], 0, 'numeric');
52 $rows = PMA_ifSetOr($_REQUEST['rows'], 0, 'numeric');
53 $ins_col = PMA_ifSetOr($_REQUEST['ins_col'], null, 'array');
54 $del_col = PMA_ifSetOr($_REQUEST['del_col'], null, 'array');
56 $prev_criteria = isset($_REQUEST['prev_criteria'])
57 ? $_REQUEST['prev_criteria']
58 : array();
59 $criteria = isset($_REQUEST['criteria'])
60 ? $_REQUEST['criteria']
61 : array_fill(0, $col_cnt, '');
63 $ins_row = isset($_REQUEST['ins_row'])
64 ? $_REQUEST['ins_row']
65 : array_fill(0, $col_cnt, '');
66 $del_row = isset($_REQUEST['del_row'])
67 ? $_REQUEST['del_row']
68 : array_fill(0, $col_cnt, '');
69 $and_or_row = isset($_REQUEST['and_or_row'])
70 ? $_REQUEST['and_or_row']
71 : array_fill(0, $col_cnt, '');
72 $and_or_col = isset($_REQUEST['and_or_col'])
73 ? $_REQUEST['and_or_col']
74 : array_fill(0, $col_cnt, '');
76 // minimum width
77 $form_column_width = 12;
78 $col = max($col_cnt + $add_col, 0);
79 $row = max($rows + $add_row, 0);
82 // The tables list sent by a previously submitted form
83 if (PMA_isValid($_REQUEST['TableList'], 'array')) {
84 foreach ($_REQUEST['TableList'] as $each_table) {
85 $tbl_names[$each_table] = ' selected="selected"';
87 } // end if
90 // this was a work in progress, deactivated for now
91 //$columns = PMA_DBI_get_columns_full($GLOBALS['db']);
92 //$tables = PMA_DBI_get_columns_full($GLOBALS['db']);
95 /**
96 * Prepares the form
98 $tbl_result = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
99 $tbl_result_cnt = PMA_DBI_num_rows($tbl_result);
100 if (0 == $tbl_result_cnt) {
101 PMA_Message::error(__('No tables found in database.'))->display();
102 require_once './libraries/footer.inc.php';
103 exit;
106 // The tables list gets from MySQL
107 while (list($tbl) = PMA_DBI_fetch_row($tbl_result)) {
108 $fld_results = PMA_DBI_get_fields($db, $tbl);
110 if (empty($tbl_names[$tbl]) && !empty($_REQUEST['TableList'])) {
111 $tbl_names[$tbl] = '';
112 } else {
113 $tbl_names[$tbl] = ' selected="selected"';
114 } // end if
116 // The fields list per selected tables
117 if ($tbl_names[$tbl] == ' selected="selected"') {
118 $each_table = PMA_backquote($tbl);
119 $fld[] = $each_table . '.*';
120 foreach ($fld_results as $each_field) {
121 $each_field = $each_table . '.' . PMA_backquote($each_field['Field']);
122 $fld[] = $each_field;
124 // increase the width if necessary
125 $form_column_width = max(strlen($each_field), $form_column_width);
126 } // end foreach
127 } // end if
128 } // end while
129 PMA_DBI_free_result($tbl_result);
131 // largest width found
132 $realwidth = $form_column_width . 'ex';
136 * Displays the Query by example form
140 * Enter description here...
142 * @param array $columns
143 * @param integer $column_number
144 * @param string $selected
146 function showColumnSelectCell($columns, $column_number, $selected = '')
149 <td align="center">
150 <select name="Field[<?php echo $column_number; ?>]" size="1">
151 <option value="">&nbsp;</option>
152 <?php
153 foreach ($columns as $column) {
154 if ($column === $selected) {
155 $sel = ' selected="selected"';
156 } else {
157 $sel = '';
159 echo '<option value="' . htmlspecialchars($column) . '"' . $sel . '>'
160 . str_replace(' ', '&nbsp;', htmlspecialchars($column)) . '</option>' . "\n";
163 </select>
164 </td>
165 <?php
169 <form action="db_qbe.php" method="post">
170 <fieldset>
171 <table class="data" style="width: 100%;">
172 <tr class="odd noclick">
173 <th><?php echo __('Column'); ?>:</th>
174 <?php
175 $z = 0;
176 for ($x = 0; $x < $col; $x++) {
177 if (isset($ins_col[$x]) && $ins_col[$x] == 'on') {
178 showColumnSelectCell($fld, $z);
179 $z++;
182 if (! empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
183 continue;
186 $selected = '';
187 if (isset($Field[$x])) {
188 $selected = $Field[$x];
189 $curField[$z] = $Field[$x];
191 showColumnSelectCell($fld, $z, $selected);
192 $z++;
193 } // end for
195 </tr>
197 <!-- Sort row -->
198 <tr class="even noclick">
199 <th><?php echo __('Sort'); ?>:</th>
200 <?php
201 $z = 0;
202 for ($x = 0; $x < $col; $x++) {
203 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
205 <td align="center">
206 <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
207 <option value="">&nbsp;</option>
208 <option value="ASC"><?php echo __('Ascending'); ?></option>
209 <option value="DESC"><?php echo __('Descending'); ?></option>
210 </select>
211 </td>
212 <?php
213 $z++;
214 } // end if
215 echo "\n";
217 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
218 continue;
221 <td align="center">
222 <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
223 <option value="">&nbsp;</option>
224 <?php
225 echo "\n";
227 // If they have chosen all fields using the * selector,
228 // then sorting is not available
229 // Robbat2 - Fix for Bug #570698
230 if (isset($Sort[$x]) && isset($Field[$x])
231 && substr($Field[$x], -2) == '.*') {
232 $Sort[$x] = '';
233 } //end if
235 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
236 $curSort[$z] = $Sort[$x];
237 $sel = ' selected="selected"';
238 } else {
239 $sel = '';
240 } // end if
241 echo ' ';
242 echo '<option value="ASC"' . $sel . '>' . __('Ascending') . '</option>' . "\n";
243 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
244 $curSort[$z] = $Sort[$x];
245 $sel = ' selected="selected"';
246 } else {
247 $sel = '';
248 } // end if
249 echo ' ';
250 echo '<option value="DESC"' . $sel . '>' . __('Descending') . '</option>' . "\n";
252 </select>
253 </td>
254 <?php
255 $z++;
256 echo "\n";
257 } // end for
259 </tr>
261 <!-- Show row -->
262 <tr class="odd noclick">
263 <th><?php echo __('Show'); ?>:</th>
264 <?php
265 $z = 0;
266 for ($x = 0; $x < $col; $x++) {
267 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
269 <td align="center">
270 <input type="checkbox" name="Show[<?php echo $z; ?>]" />
271 </td>
272 <?php
273 $z++;
274 } // end if
275 echo "\n";
277 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
278 continue;
280 if (isset($Show[$x])) {
281 $checked = ' checked="checked"';
282 $curShow[$z] = $Show[$x];
283 } else {
284 $checked = '';
287 <td align="center">
288 <input type="checkbox" name="Show[<?php echo $z; ?>]"<?php echo $checked; ?> />
289 </td>
290 <?php
291 $z++;
292 echo "\n";
293 } // end for
295 </tr>
297 <!-- Criteria row -->
298 <tr class="even noclick">
299 <th><?php echo __('Criteria'); ?>:</th>
300 <?php
301 $z = 0;
302 for ($x = 0; $x < $col; $x++) {
303 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
305 <td align="center">
306 <input type="text" name="criteria[<?php echo $z; ?>]" value="" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
307 </td>
308 <?php
309 $z++;
310 } // end if
311 echo "\n";
313 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
314 continue;
316 if (isset($criteria[$x])) {
317 $tmp_criteria = $criteria[$x];
319 if ((empty($prev_criteria) || !isset($prev_criteria[$x]))
320 || $prev_criteria[$x] != htmlspecialchars($tmp_criteria)) {
321 $curCriteria[$z] = $tmp_criteria;
322 } else {
323 $curCriteria[$z] = $prev_criteria[$x];
326 <td align="center">
327 <input type="hidden" name="prev_criteria[<?php echo $z; ?>]" value="<?php echo htmlspecialchars($curCriteria[$z]); ?>" />
328 <input type="text" name="criteria[<?php echo $z; ?>]" value="<?php echo htmlspecialchars($tmp_criteria); ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
329 </td>
330 <?php
331 $z++;
332 echo "\n";
333 } // end for
335 </tr>
337 <!-- And/Or columns and rows -->
338 <?php
339 $w = 0;
340 $odd_row = true;
341 for ($y = 0; $y <= $row; $y++) {
342 if (isset($ins_row[$y]) && $ins_row[$y] == 'on') {
343 $chk['or'] = ' checked="checked"';
344 $chk['and'] = '';
346 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
347 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
348 <!-- Row controls -->
349 <table cellpadding="0" cellspacing="0" border="0">
350 <tr>
351 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
352 <small><?php echo __('Ins'); ?>:</small>
353 <input type="checkbox" name="ins_row[<?php echo $w; ?>]" />
354 </td>
355 <td align="<?php echo $cell_align_right; ?>">
356 <strong><?php echo __('And'); ?>:</strong>
357 </td>
358 <td>
359 <input type="radio" name="and_or_row[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
360 &nbsp;
361 </td>
362 </tr>
363 <tr>
364 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
365 <small><?php echo __('Del'); ?>:</small>
366 <input type="checkbox" name="del_row[<?php echo $w; ?>]" />
367 </td>
368 <td align="<?php echo $cell_align_right; ?>">
369 <strong><?php echo __('Or'); ?>:</strong>
370 </td>
371 <td>
372 <input type="radio" name="and_or_row[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
373 &nbsp;
374 </td>
375 </tr>
376 </table>
377 </td>
378 <?php
379 $z = 0;
380 for ($x = 0; $x < $col; $x++) {
381 if (isset($ins_col[$x]) && $ins_col[$x] == 'on') {
382 echo "\n";
383 $or = 'Or' . $w . '[' . $z . ']';
385 <td align="center">
386 <input type="text" name="Or<?php echo $or; ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
387 </td>
388 <?php
389 $z++;
390 } // end if
391 if (isset($del_col[$x]) && $del_col[$x] == 'on') {
392 continue;
395 echo "\n";
396 $or = 'Or' . $w . '[' . $z . ']';
398 <td align="center">
399 <input type="text" name="Or<?php echo $or; ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
400 </td>
401 <?php
402 $z++;
403 } // end for
404 $w++;
405 echo "\n";
407 </tr>
408 <?php
409 $odd_row =! $odd_row;
410 } // end if
412 if (isset($del_row[$y]) && $del_row[$y] == 'on') {
413 continue;
416 if (isset($and_or_row[$y])) {
417 $curAndOrRow[$w] = $and_or_row[$y];
419 if (isset($and_or_row[$y]) && $and_or_row[$y] == 'and') {
420 $chk['and'] = ' checked="checked"';
421 $chk['or'] = '';
422 } else {
423 $chk['or'] = ' checked="checked"';
424 $chk['and'] = '';
426 echo "\n";
428 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
429 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
430 <!-- Row controls -->
431 <table border="0" cellpadding="0" cellspacing="0">
432 <tr>
433 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
434 <small><?php echo __('Ins'); ?>:</small>
435 <input type="checkbox" name="ins_row[<?php echo $w; ?>]" />
436 </td>
437 <td align="<?php echo $cell_align_right; ?>">
438 <strong><?php echo __('And'); ?>:</strong>
439 </td>
440 <td>
441 <input type="radio" name="and_or_row[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
442 </td>
443 </tr>
444 <tr>
445 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
446 <small><?php echo __('Del'); ?>:</small>
447 <input type="checkbox" name="del_row[<?php echo $w; ?>]" />
448 </td>
449 <td align="<?php echo $cell_align_right; ?>">
450 <strong><?php echo __('Or'); ?>:</strong>
451 </td>
452 <td>
453 <input type="radio" name="and_or_row[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
454 </td>
455 </tr>
456 </table>
457 </td>
458 <?php
459 $z = 0;
460 for ($x = 0; $x < $col; $x++) {
461 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
462 echo "\n";
463 $or = 'Or' . $w . '[' . $z . ']';
465 <td align="center">
466 <input type="text" name="Or<?php echo $or; ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
467 </td>
468 <?php
469 $z++;
470 } // end if
471 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
472 continue;
475 echo "\n";
476 $or = 'Or' . $y;
477 if (!isset(${$or})) {
478 ${$or} = '';
480 if (!empty(${$or}) && isset(${$or}[$x])) {
481 $tmp_or = ${$or}[$x];
482 } else {
483 $tmp_or = '';
486 <td align="center">
487 <input type="text" name="Or<?php echo $w . '[' . $z . ']'; ?>" value="<?php echo htmlspecialchars($tmp_or); ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
488 </td>
489 <?php
490 if (!empty(${$or}) && isset(${$or}[$x])) {
491 ${'cur' . $or}[$z] = ${$or}[$x];
493 $z++;
494 } // end for
495 $w++;
496 echo "\n";
498 </tr>
499 <?php
500 echo "\n";
501 $odd_row =! $odd_row;
502 } // end for
504 <!-- Modify columns -->
505 <tr class="even noclick">
506 <th><?php echo __('Modify'); ?>:</th>
507 <?php
508 $z = 0;
509 for ($x = 0; $x < $col; $x++) {
510 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
511 $curAndOrCol[$z] = $and_or_col[$y];
512 if ($and_or_col[$z] == 'or') {
513 $chk['or'] = ' checked="checked"';
514 $chk['and'] = '';
515 } else {
516 $chk['and'] = ' checked="checked"';
517 $chk['or'] = '';
520 <td align="center">
521 <strong><?php echo __('Or'); ?>:</strong>
522 <input type="radio" name="and_or_col[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
523 &nbsp;&nbsp;<strong><?php echo __('And'); ?>:</strong>
524 <input type="radio" name="and_or_col[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
525 <br />
526 <?php echo __('Ins') . "\n"; ?>
527 <input type="checkbox" name="ins_col[<?php echo $z; ?>]" />
528 &nbsp;&nbsp;<?php echo __('Del') . "\n"; ?>
529 <input type="checkbox" name="del_col[<?php echo $z; ?>]" />
530 </td>
531 <?php
532 $z++;
533 } // end if
534 echo "\n";
536 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
537 continue;
540 if (isset($and_or_col[$y])) {
541 $curAndOrCol[$z] = $and_or_col[$y];
543 if (isset($and_or_col[$z]) && $and_or_col[$z] == 'or') {
544 $chk['or'] = ' checked="checked"';
545 $chk['and'] = '';
546 } else {
547 $chk['and'] = ' checked="checked"';
548 $chk['or'] = '';
551 <td align="center">
552 <strong><?php echo __('Or'); ?>:</strong>
553 <input type="radio" name="and_or_col[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
554 &nbsp;&nbsp;<strong><?php echo __('And'); ?>:</strong>
555 <input type="radio" name="and_or_col[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
556 <br />
557 <?php echo __('Ins') . "\n"; ?>
558 <input type="checkbox" name="ins_col[<?php echo $z; ?>]" />
559 &nbsp;&nbsp;<?php echo __('Del') . "\n"; ?>
560 <input type="checkbox" name="del_col[<?php echo $z; ?>]" />
561 </td>
562 <?php
563 $z++;
564 echo "\n";
565 } // end for
567 </tr>
568 </table>
570 <!-- Other controls -->
571 <?php
572 $w--;
573 $url_params['db'] = $db;
574 $url_params['col_cnt'] = $z;
575 $url_params['rows'] = $w;
576 echo PMA_generate_common_hidden_inputs($url_params);
578 </fieldset>
579 <fieldset class="tblFooters">
580 <table border="0" cellpadding="2" cellspacing="1">
581 <tr>
582 <td nowrap="nowrap">
583 <?php echo __('Add/Delete criteria rows'); ?>:
584 <select size="1" name="add_row">
585 <option value="-3">-3</option>
586 <option value="-2">-2</option>
587 <option value="-1">-1</option>
588 <option value="0" selected="selected">0</option>
589 <option value="1">1</option>
590 <option value="2">2</option>
591 <option value="3">3</option>
592 </select>
593 </td>
594 <td width="10">&nbsp;</td>
595 <td nowrap="nowrap"><?php echo __('Add/Delete columns'); ?>:
596 <select size="1" name="add_col">
597 <option value="-3">-3</option>
598 <option value="-2">-2</option>
599 <option value="-1">-1</option>
600 <option value="0" selected="selected">0</option>
601 <option value="1">1</option>
602 <option value="2">2</option>
603 <option value="3">3</option>
604 </select>
605 </td>
606 <td width="10">&nbsp;</td>
607 <!-- Generates a query -->
608 <td><input type="submit" name="modify" value="<?php echo __('Update Query'); ?>" /></td>
609 </tr>
610 </table>
611 </fieldset>
613 <table>
614 <tr><td>
615 <fieldset>
616 <legend><?php echo __('Use Tables'); ?></legend>
617 <?php
618 $options = '';
619 $numTableListOptions = 0;
620 foreach ($tbl_names as $key => $val) {
621 $options .= ' ';
622 $options .= '<option value="' . htmlspecialchars($key) . '"' . $val . '>'
623 . str_replace(' ', '&nbsp;', htmlspecialchars($key)) . '</option>' . "\n";
624 $numTableListOptions++;
627 <select name="TableList[]" multiple="multiple" id="listTable"
628 size="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>">
629 <?php echo $options; ?>
630 </select>
631 </fieldset>
632 <fieldset class="tblFooters">
633 <input type="submit" name="modify" value="<?php echo __('Update Query'); ?>" />
634 </fieldset>
635 </td>
636 <td width="20">&nbsp;</td>
637 <td>
638 <fieldset>
639 <legend><?php echo sprintf(__('SQL query on database <b>%s</b>:'), PMA_getDbLink($db)); ?>
640 </legend>
641 <textarea cols="80" name="sql_query" id="textSqlquery"
642 rows="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>"
643 dir="<?php echo $text_dir; ?>">
644 <?php
645 // 1. SELECT
646 $last_select = 0;
647 if (!isset($qry_select)) {
648 $qry_select = '';
650 for ($x = 0; $x < $col; $x++) {
651 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
652 if ($last_select) {
653 $qry_select .= ', ';
655 $qry_select .= $curField[$x];
656 $last_select = 1;
658 } // end for
659 if (!empty($qry_select)) {
660 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
663 // 2. FROM
665 // Create LEFT JOINS out of Relations
666 // Code originally by Mike Beck <mike.beck@ibmiller.de>
667 // If we can use Relations we could make some left joins.
668 // First find out if relations are available in this database.
670 // First we need the really needed Tables - those in TableList might still be
671 // all Tables.
672 if (isset($Field) && count($Field) > 0) {
673 // Initialize some variables
674 $tab_all = array();
675 $col_all = array();
676 $tab_wher = array();
677 $tab_know = array();
678 $tab_left = array();
679 $col_where = array();
680 $fromclause = '';
682 // We only start this if we have fields, otherwise it would be dumb
683 foreach ($Field as $value) {
684 $parts = explode('.', $value);
685 if (!empty($parts[0]) && !empty($parts[1])) {
686 $tab_raw = $parts[0];
687 $tab = str_replace('`', '', $tab_raw);
688 $tab_all[$tab] = $tab;
690 $col_raw = $parts[1];
691 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
693 } // end while
695 // Check 'where' clauses
696 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
697 // Now we need all tables that we have in the where clause
698 $crit_cnt = count($criteria);
699 for ($x = 0; $x < $crit_cnt; $x++) {
700 $curr_tab = explode('.', $Field[$x]);
701 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
702 $tab_raw = $curr_tab[0];
703 $tab = str_replace('`', '', $tab_raw);
705 $col_raw = $curr_tab[1];
706 $col1 = str_replace('`', '', $col_raw);
707 $col1 = $tab . '.' . $col1;
708 // Now we know that our array has the same numbers as $criteria
709 // we can check which of our columns has a where clause
710 if (!empty($criteria[$x])) {
711 if (substr($criteria[$x], 0, 1) == '=' || stristr($criteria[$x], 'is')) {
712 $col_where[$col] = $col1;
713 $tab_wher[$tab] = $tab;
715 } // end if
716 } // end if
717 } // end for
719 // Cleans temp vars w/o further use
720 unset($tab_raw);
721 unset($col_raw);
722 unset($col1);
724 if (count($tab_wher) == 1) {
725 // If there is exactly one column that has a decent where-clause
726 // we will just use this
727 $master = key($tab_wher);
728 } else {
729 // Now let's find out which of the tables has an index
730 // (When the control user is the same as the normal user
731 // because he is using one of his databases as pmadb,
732 // the last db selected is not always the one where we need to work)
733 PMA_DBI_select_db($db);
735 foreach ($tab_all as $tab) {
736 $ind_rs = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tab) . ';');
737 while ($ind = PMA_DBI_fetch_assoc($ind_rs)) {
738 $col1 = $tab . '.' . $ind['Column_name'];
739 if (isset($col_all[$col1])) {
740 if ($ind['non_unique'] == 0) {
741 if (isset($col_where[$col1])) {
742 $col_unique[$col1] = 'Y';
743 } else {
744 $col_unique[$col1] = 'N';
746 } else {
747 if (isset($col_where[$col1])) {
748 $col_index[$col1] = 'Y';
749 } else {
750 $col_index[$col1] = 'N';
754 } // end while (each col of tab)
755 } // end while (each tab)
756 // now we want to find the best.
757 if (isset($col_unique) && count($col_unique) > 0) {
758 $col_cand = $col_unique;
759 $needsort = 1;
760 } elseif (isset($col_index) && count($col_index) > 0) {
761 $col_cand = $col_index;
762 $needsort = 1;
763 } elseif (isset($col_where) && count($col_where) > 0) {
764 $col_cand = $tab_wher;
765 $needsort = 0;
766 } else {
767 $col_cand = $tab_all;
768 $needsort = 0;
771 // If we came up with $col_unique (very good) or $col_index (still
772 // good) as $col_cand we want to check if we have any 'Y' there
773 // (that would mean that they were also found in the whereclauses
774 // which would be great). if yes, we take only those
775 if ($needsort == 1) {
776 foreach ($col_cand as $col => $is_where) {
777 $tab = explode('.', $col);
778 $tab = $tab[0];
779 if ($is_where == 'Y') {
780 $vg[$col] = $tab;
781 } else {
782 $sg[$col] = $tab;
785 if (isset($vg)) {
786 $col_cand = $vg;
787 // Candidates restricted in index+where
788 } else {
789 $col_cand = $sg;
790 // None of the candidates where in a where-clause
794 // If our array of candidates has more than one member we'll just
795 // find the smallest table.
796 // Of course the actual query would be faster if we check for
797 // the Criteria which gives the smallest result set in its table,
798 // but it would take too much time to check this
799 if (count($col_cand) > 1) {
800 // Of course we only want to check each table once
801 $checked_tables = $col_cand;
802 foreach ($col_cand as $tab) {
803 if ($checked_tables[$tab] != 1) {
804 $tsize[$tab] = PMA_Table::countRecords($db, $tab, false);
805 $checked_tables[$tab] = 1;
807 $csize[$tab] = $tsize[$tab];
809 asort($csize);
810 reset($csize);
811 $master = key($csize); // Smallest
812 } else {
813 reset($col_cand);
814 $master = current($col_cand); // Only one single candidate
816 } // end if (exactly one where clause)
818 $tab_left = $tab_all;
819 unset($tab_left[$master]);
820 $tab_know[$master] = $master;
822 $run = 0;
823 $emerg = '';
824 while (count($tab_left) > 0) {
825 if ($run % 2 == 0) {
826 PMA_getRelatives('master');
827 } else {
828 PMA_getRelatives('foreign');
830 $run++;
831 if ($run > 5) {
833 foreach ($tab_left as $tab) {
834 $emerg .= ', ' . PMA_backquote($tab);
835 unset($tab_left[$tab]);
838 } // end while
839 $qry_from = PMA_backquote($master) . $emerg . $fromclause;
840 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
842 } // end count($Field) > 0
844 // In case relations are not defined, just generate the FROM clause
845 // from the list of tables, however we don't generate any JOIN
847 if (empty($qry_from) && isset($tab_all)) {
848 $qry_from = implode(', ', $tab_all);
850 // Now let's see what we got
851 if (!empty($qry_from)) {
852 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
855 // 3. WHERE
856 $qry_where = '';
857 $criteria_cnt = 0;
858 for ($x = 0; $x < $col; $x++) {
859 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where) && isset($curAndOrCol)) {
860 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
862 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
863 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
864 $last_where = $x;
865 $criteria_cnt++;
867 } // end for
868 if ($criteria_cnt > 1) {
869 $qry_where = '(' . $qry_where . ')';
871 // OR rows ${'cur' . $or}[$x]
872 if (!isset($curAndOrRow)) {
873 $curAndOrRow = array();
875 for ($y = 0; $y <= $row; $y++) {
876 $criteria_cnt = 0;
877 $qry_orwhere = '';
878 $last_orwhere = '';
879 for ($x = 0; $x < $col; $x++) {
880 if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x]) && $x) {
881 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
883 if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x])) {
884 $qry_orwhere .= '(' . $curField[$x]
885 . ' '
886 . ${'curOr' . $y}[$x]
887 . ')';
888 $last_orwhere = $x;
889 $criteria_cnt++;
891 } // end for
892 if ($criteria_cnt > 1) {
893 $qry_orwhere = '(' . $qry_orwhere . ')';
895 if (!empty($qry_orwhere)) {
896 $qry_where .= "\n"
897 . strtoupper(isset($curAndOrRow[$y]) ? $curAndOrRow[$y] . ' ' : '')
898 . $qry_orwhere;
899 } // end if
900 } // end for
902 if (!empty($qry_where) && $qry_where != '()') {
903 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
904 } // end if
907 // 4. ORDER BY
908 $last_orderby = 0;
909 if (!isset($qry_orderby)) {
910 $qry_orderby = '';
912 for ($x = 0; $x < $col; $x++) {
913 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
914 $qry_orderby .= ', ';
916 if (!empty($curField[$x]) && !empty($curSort[$x])) {
917 // if they have chosen all fields using the * selector,
918 // then sorting is not available
919 // Robbat2 - Fix for Bug #570698
920 if (substr($curField[$x], -2) != '.*') {
921 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
922 $last_orderby = 1;
925 } // end for
926 if (!empty($qry_orderby)) {
927 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
930 </textarea>
931 </fieldset>
932 <fieldset class="tblFooters">
933 <input type="submit" name="submit_sql" value="<?php echo __('Submit Query'); ?>" />
934 </fieldset>
935 </td>
936 </tr>
937 </table>
938 </form>
939 <?php
941 * Displays the footer
943 require_once './libraries/footer.inc.php';