sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / db_details_qbe.php
blob09fa999b9983f2aed545dd64ac0c14abfce88cfd
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Get the values of the variables posted or sent to this script and display
7 * the headers
8 */
9 require_once('./libraries/common.lib.php');
10 require_once('./libraries/relation.lib.php');
13 /**
14 * Gets the relation settings
16 $cfgRelation = PMA_getRelationsParam();
19 /**
20 * A query has been submitted -> execute it, else display the headers
22 if (isset($submit_sql) && preg_match('@^SELECT@i', $encoded_sql_query)) {
23 $goto = 'db_details.php';
24 $zero_rows = htmlspecialchars($strSuccess);
25 $sql_query = urldecode($encoded_sql_query);
26 require('./sql.php');
27 exit();
28 } else {
29 $sub_part = '_qbe';
30 require('./db_details_common.php');
31 $url_query .= '&amp;goto=db_details_qbe.php';
32 require('./db_details_db_info.php');
35 if (isset($submit_sql) && !preg_match('@^SELECT@i', $encoded_sql_query)) {
36 echo '<p class="warning">' . $strHaveToShow . '</p>';
40 /**
41 * Initialize some variables
43 if (empty($Columns)) {
44 $Columns = 3; // Initial number of columns
46 if (!isset($Add_Col)) {
47 $Add_Col = '';
49 if (!isset($Add_Row)) {
50 $Add_Row = '';
52 if (!isset($Rows)) {
53 $Rows = '';
55 if (!isset($InsCol)) {
56 $InsCol = array();
58 if (!isset($DelCol)) {
59 $DelCol = array();
61 if (!isset($prev_Criteria)) {
62 $prev_Criteria = '';
64 if (!isset($Criteria)) {
65 $Criteria = array();
66 for ($i = 0; $i < $Columns; $i++) {
67 $Criteria[$i] = '';
70 if (!isset($InsRow)) {
71 $InsRow = array();
72 for ($i = 0; $i < $Columns; $i++) {
73 $InsRow[$i] = '';
76 if (!isset($DelRow)) {
77 $DelRow = array();
78 for ($i = 0; $i < $Columns; $i++) {
79 $DelRow[$i] = '';
82 if (!isset($AndOrRow)) {
83 $AndOrRow = array();
84 for ($i = 0; $i < $Columns; $i++) {
85 $AndOrRow[$i] = '';
88 if (!isset($AndOrCol)) {
89 $AndOrCol = array();
90 for ($i = 0; $i < $Columns; $i++) {
91 $AndOrCol[$i] = '';
94 // minimum width
95 $wid = 12;
96 $col = $Columns + $Add_Col;
97 if ($col < 0) {
98 $col = 0;
100 $row = $Rows + $Add_Row;
101 if ($row < 0) {
102 $row = 0;
107 * Prepares the form
109 $tbl_result = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', NULL, PMA_DBI_QUERY_STORE);
110 $tbl_result_cnt = PMA_DBI_num_rows($tbl_result);
111 $i = 0;
112 $k = 0;
114 // The tables list sent by a previously submitted form
115 if (!empty($TableList)) {
116 $cnt_table_list = count($TableList);
117 for ($x = 0; $x < $cnt_table_list; $x++) {
118 $tbl_names[urldecode($TableList[$x])] = ' selected="selected"';
120 } // end if
122 // The tables list gets from MySQL
123 while ($i < $tbl_result_cnt) {
124 list($tbl) = PMA_DBI_fetch_row($tbl_result);
125 $fld_results = PMA_DBI_get_fields($db, $tbl);
126 $fld_results_cnt = ($fld_results) ? count($fld_results) : 0;
127 $j = 0;
129 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
130 $tbl_names[$tbl] = '';
131 } else {
132 $tbl_names[$tbl] = ' selected="selected"';
133 } // end if
135 // The fields list per selected tables
136 if ($tbl_names[$tbl] == ' selected="selected"') {
137 $fld[$k++] = PMA_backquote($tbl) . '.*';
138 while ($j < $fld_results_cnt) {
139 $fld[$k] = PMA_convert_display_charset($fld_results[$j]['Field']);
140 $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
142 // increase the width if necessary
143 if (strlen($fld[$k]) > $wid) {
144 $wid = strlen($fld[$k]);
145 } //end if
147 $k++;
148 $j++;
149 } // end while
150 } // end if
152 $i++;
153 } // end if
154 PMA_DBI_free_result($tbl_result);
156 // largest width found
157 $realwidth = $wid . 'ex';
161 * Displays the form
165 <!-- Query by example form -->
166 <form action="db_details_qbe.php" method="post">
167 <table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1">
169 <!-- Fields row -->
170 <tr>
171 <td class="tblHeaders" align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
172 <b><?php echo $strField; ?>:&nbsp;</b>
173 </td>
174 <?php
175 $z = 0;
176 for ($x = 0; $x < $col; $x++) {
177 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
179 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
180 <select style="width: <?php echo $realwidth; ?>" name="Field[<?php echo $z; ?>]" size="1">
181 <option value=""></option>
182 <?php
183 echo "\n";
184 for ($y = 0; $y < sizeof($fld); $y++) {
185 if ($fld[$y] == '') {
186 $sel = ' selected="selected"';
187 } else {
188 $sel = '';
190 echo ' ';
191 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
192 } // end for
194 </select>
195 </td>
196 <?php
197 $z++;
198 } // end if
199 echo "\n";
201 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
202 continue;
205 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
206 <select style="width: <?php echo $realwidth; ?>" name="Field[<?php echo $z; ?>]" size="1">
207 <option value=""></option>
208 <?php
209 echo "\n";
210 for ($y = 0; $y < sizeof($fld); $y++) {
211 if (isset($Field[$x]) && $fld[$y] == urldecode($Field[$x])) {
212 $curField[$z] = urldecode($Field[$x]);
213 $sel = ' selected="selected"';
214 } else {
215 $sel = '';
216 } // end if
217 echo ' ';
218 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
219 } // end for
221 </select>
222 </td>
223 <?php
224 $z++;
225 echo "\n";
226 } // end for
228 </tr>
230 <!-- Sort row -->
231 <tr>
232 <td class="tblHeaders" align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
233 <b><?php echo $strSort; ?>:&nbsp;</b>
234 </td>
235 <?php
236 $z = 0;
237 for ($x = 0; $x < $col; $x++) {
238 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
240 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
241 <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
242 <option value=""></option>
243 <option value="ASC"><?php echo $strAscending; ?></option>
244 <option value="DESC"><?php echo $strDescending; ?></option>
245 </select>
246 </td>
247 <?php
248 $z++;
249 } // end if
250 echo "\n";
252 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
253 continue;
256 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
257 <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
258 <option value=""></option>
259 <?php
260 echo "\n";
262 // If they have chosen all fields using the * selector,
263 // then sorting is not available
264 // Robbat2 - Fix for Bug #570698
265 if (isset($Sort[$x]) && isset($Field[$x]) && (substr(urldecode($Field[$x]),-2) == '.*')) {
266 $Sort[$x] = '';
267 } //end if
269 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
270 $curSort[$z] = $Sort[$x];
271 $sel = ' selected="selected"';
272 } else {
273 $sel = '';
274 } // end if
275 echo ' ';
276 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
277 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
278 $curSort[$z] = $Sort[$x];
279 $sel = ' selected="selected"';
280 } else {
281 $sel = '';
282 } // end if
283 echo ' ';
284 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
286 </select>
287 </td>
288 <?php
289 $z++;
290 echo "\n";
291 } // end for
293 </tr>
295 <!-- Show row -->
296 <tr>
297 <td class="tblHeaders" align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
298 <b><?php echo $strShow; ?>:&nbsp;</b>
299 </td>
300 <?php
301 $z = 0;
302 for ($x = 0; $x < $col; $x++) {
303 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
305 <td class="tblHeaders" align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
306 <input type="checkbox" name="Show[<?php echo $z; ?>]" />
307 </td>
308 <?php
309 $z++;
310 } // end if
311 echo "\n";
313 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
314 continue;
316 if (isset($Show[$x])) {
317 $checked = ' checked="checked"';
318 $curShow[$z] = $Show[$x];
319 } else {
320 $checked = '';
323 <td class="tblHeaders" align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
324 <input type="checkbox" name="Show[<?php echo $z; ?>]"<?php echo $checked; ?> />
325 </td>
326 <?php
327 $z++;
328 echo "\n";
329 } // end for
331 </tr>
333 <!-- Criteria row -->
334 <tr>
335 <td class="tblHeaders" align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
336 <b><?php echo $strCriteria; ?>:&nbsp;</b>
337 </td>
338 <?php
339 $z = 0;
340 for ($x = 0; $x < $col; $x++) {
341 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
343 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
344 <input type="text" name="Criteria[<?php echo $z; ?>]" value="" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
345 </td>
346 <?php
347 $z++;
348 } // end if
349 echo "\n";
351 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
352 continue;
354 if (isset($Criteria[$x])) {
355 $stripped_Criteria = $Criteria[$x];
357 if ((empty($prev_Criteria) || !isset($prev_Criteria[$x]))
358 || urldecode($prev_Criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
359 $curCriteria[$z] = $stripped_Criteria;
360 $encoded_Criteria = urlencode($stripped_Criteria);
361 } else {
362 $curCriteria[$z] = urldecode($prev_Criteria[$x]);
363 $encoded_Criteria = $prev_Criteria[$x];
366 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
367 <input type="hidden" name="prev_Criteria[<?php echo $z; ?>]" value="<?php echo $encoded_Criteria; ?>" />
368 <input type="text" name="Criteria[<?php echo $z; ?>]" value="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
369 </td>
370 <?php
371 $z++;
372 echo "\n";
373 } // end for
375 </tr>
377 <!-- And/Or columns and rows -->
378 <?php
379 $w = 0;
380 for ($y = 0; $y <= $row; $y++) {
381 $bgcolor = ($y % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
382 if (isset($InsRow[$y]) && $InsRow[$y] == 'on') {
383 $chk['or'] = ' checked="checked"';
384 $chk['and'] = '';
386 <tr>
387 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap">
388 <!-- Row controls -->
389 <table bgcolor="<?php echo $bgcolor; ?>" cellpadding="0" cellspacing="0" border="0">
390 <tr>
391 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
392 <small><?php echo $strQBEIns; ?>:</small>
393 <input type="checkbox" name="InsRow[<?php echo $w; ?>]" />
394 </td>
395 <td align="<?php echo $cell_align_right; ?>">
396 <b><?php echo $strAnd; ?>:</b>
397 </td>
398 <td>
399 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
400 &nbsp;
401 </td>
402 </tr>
403 <tr>
404 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
405 <small><?php echo $strQBEDel; ?>:</small>
406 <input type="checkbox" name="DelRow[<?php echo $w; ?>]" />
407 </td>
408 <td align="<?php echo $cell_align_right; ?>">
409 <b><?php echo $strOr; ?>:</b>
410 </td>
411 <td>
412 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
413 &nbsp;
414 </td>
415 </tr>
416 </table>
417 </td>
418 <?php
419 $z = 0;
420 for ($x = 0; $x < $col; $x++) {
421 if (isset($InsCol[$x]) && $InsCol[$x] == 'on') {
422 echo "\n";
423 $or = 'Or' . $w . '[' . $z . ']';
425 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
426 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
427 </td>
428 <?php
429 $z++;
430 } // end if
431 if (isset($DelCol[$x]) && $DelCol[$x] == 'on') {
432 continue;
435 echo "\n";
436 $or = 'Or' . $w . '[' . $z . ']';
438 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
439 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
440 </td>
441 <?php
442 $z++;
443 } // end for
444 $w++;
445 echo "\n";
447 </tr>
448 <?php
449 } // end if
451 if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
452 continue;
455 if (isset($AndOrRow[$y])) {
456 $curAndOrRow[$w] = $AndOrRow[$y];
458 if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
459 $chk['and'] = ' checked="checked"';
460 $chk['or'] = '';
461 } else {
462 $chk['or'] = ' checked="checked"';
463 $chk['and'] = '';
465 echo "\n";
467 <tr>
468 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap">
469 <!-- Row controls -->
470 <table bgcolor="<?php echo $bgcolor; ?>" border="0" cellpadding="0" cellspacing="0">
471 <tr>
472 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
473 <small><?php echo $strQBEIns; ?>:</small>
474 <input type="checkbox" name="InsRow[<?php echo $w; ?>]" />
475 </td>
476 <td align="<?php echo $cell_align_right; ?>">
477 <b><?php echo $strAnd; ?>:</b>
478 </td>
479 <td>
480 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
481 </td>
482 </tr>
483 <tr>
484 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
485 <small><?php echo $strQBEDel; ?>:</small>
486 <input type="checkbox" name="DelRow[<?php echo $w; ?>]" />
487 </td>
488 <td align="<?php echo $cell_align_right; ?>">
489 <b><?php echo $strOr; ?>:</b>
490 </td>
491 <td>
492 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
493 </td>
494 </tr>
495 </table>
496 </td>
497 <?php
498 $z = 0;
499 for ($x = 0; $x < $col; $x++) {
500 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
501 echo "\n";
502 $or = 'Or' . $w . '[' . $z . ']';
504 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
505 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
506 </td>
507 <?php
508 $z++;
509 } // end if
510 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
511 continue;
514 echo "\n";
515 $or = 'Or' . $y;
516 if (!isset(${$or})) {
517 ${$or} = '';
519 if (!empty(${$or}) && isset(${$or}[$x])) {
520 $stripped_or = ${$or}[$x];
521 } else {
522 $stripped_or = '';
525 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
526 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="Or<?php echo $w . '[' . $z . ']'; ?>" dir="<?php echo $text_dir; ?>"><?php echo htmlspecialchars($stripped_or); ?></textarea>
527 </td>
528 <?php
529 if (!empty(${$or}) && isset(${$or}[$x])) {
530 ${'cur' . $or}[$z] = ${$or}[$x];
532 $z++;
533 } // end for
534 $w++;
535 echo "\n";
537 </tr>
538 <?php
539 echo "\n";
540 } // end for
543 <!-- Modify columns -->
544 <tr>
545 <td class="tblHeaders" align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
546 <b><?php echo $strModify; ?>:&nbsp;</b>
547 </td>
548 <?php
549 $z = 0;
550 for ($x = 0; $x < $col; $x++) {
551 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
552 $curAndOrCol[$z] = $AndOrCol[$y];
553 if ($AndOrCol[$z] == 'or') {
554 $chk['or'] = ' checked="checked"';
555 $chk['and'] = '';
556 } else {
557 $chk['and'] = ' checked="checked"';
558 $chk['or'] = '';
561 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
562 <b><?php echo $strOr; ?>:</b>
563 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
564 &nbsp;&nbsp;<b><?php echo $strAnd; ?>:</b>
565 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
566 <br />
567 <?php echo $strQBEIns . "\n"; ?>
568 <input type="checkbox" name="InsCol[<?php echo $z; ?>]" />
569 &nbsp;&nbsp;<?php echo $strQBEDel . "\n"; ?>
570 <input type="checkbox" name="DelCol[<?php echo $z; ?>]" />
571 </td>
572 <?php
573 $z++;
574 } // end if
575 echo "\n";
577 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
578 continue;
581 if (isset($AndOrCol[$y])) {
582 $curAndOrCol[$z] = $AndOrCol[$y];
584 if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
585 $chk['or'] = ' checked="checked"';
586 $chk['and'] = '';
587 } else {
588 $chk['and'] = ' checked="checked"';
589 $chk['or'] = '';
592 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
593 <b><?php echo $strOr; ?>:</b>
594 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
595 &nbsp;&nbsp;<b><?php echo $strAnd; ?>:</b>
596 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
597 <br />
598 <?php echo $strQBEIns . "\n"; ?>
599 <input type="checkbox" name="InsCol[<?php echo $z; ?>]" />
600 &nbsp;&nbsp;<?php echo $strQBEDel . "\n"; ?>
601 <input type="checkbox" name="DelCol[<?php echo $z; ?>]" />
602 </td>
603 <?php
604 $z++;
605 echo "\n";
606 } // end for
608 </tr>
609 </table>
611 <!-- Other controls -->
612 <?php echo PMA_generate_common_hidden_inputs(); ?>
613 <table border="0" cellpadding="2" cellspacing="1">
614 <tr>
615 <td nowrap="nowrap"><input type="hidden" value="<?php echo htmlspecialchars($db); ?>" name="db" />
616 <input type="hidden" value="<?php echo $z; ?>" name="Columns" />
617 <?php
618 $w--;
620 <input type="hidden" value="<?php echo $w; ?>" name="Rows" />
621 <?php echo $strAddDeleteRow; ?>:
622 <select size="1" name="Add_Row" style="vertical-align: middle">
623 <option value="-3">-3</option>
624 <option value="-2">-2</option>
625 <option value="-1">-1</option>
626 <option value="0" selected="selected">0</option>
627 <option value="1">1</option>
628 <option value="2">2</option>
629 <option value="3">3</option>
630 </select>
631 </td>
632 <td width="10">&nbsp;</td>
633 <td nowrap="nowrap"><?php echo $strAddDeleteColumn; ?>:
634 <select size="1" name="Add_Col" style="vertical-align: middle">
635 <option value="-3">-3</option>
636 <option value="-2">-2</option>
637 <option value="-1">-1</option>
638 <option value="0" selected="selected">0</option>
639 <option value="1">1</option>
640 <option value="2">2</option>
641 <option value="3">3</option>
642 </select>
643 </td>
644 <td width="10">&nbsp;</td>
645 <!-- Generates a query -->
646 <td><input type="submit" name="modify" value="<?php echo $strUpdateQuery; ?>" /></td>
647 </tr>
648 </table><br />
650 <table border="0" cellpadding="2" cellspacing="1">
651 <tr>
652 <td class="tblHeaders">&nbsp;<?php echo $strUseTables; ?>:&nbsp;</td>
653 <td width="20">&nbsp;</td>
654 <td class="tblHeaders">&nbsp;<?php echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?>&nbsp;</td>
655 </tr>
656 <tr>
657 <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
658 <?php
659 $strTableListOptions = '';
660 $numTableListOptions = 0;
661 foreach($tbl_names AS $key => $val) {
662 $strTableListOptions .= ' ';
663 $strTableListOptions .= '<option value="' . htmlspecialchars($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
664 $numTableListOptions++;
667 <select name="TableList[]" size="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>" multiple="multiple" id="listTable">
668 <?php echo $strTableListOptions; ?>
669 </select>
670 </td>
671 <td>&nbsp;&nbsp;</td>
672 <!-- Displays the current query -->
673 <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
674 <textarea cols="30" rows="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>" name="sql_query" dir="<?php echo $text_dir; ?>" id="textSqlquery">
675 <?php
676 // 1. SELECT
677 $last_select = 0;
678 $encoded_qry = '';
679 if (!isset($qry_select)) {
680 $qry_select = '';
682 for ($x = 0; $x < $col; $x++) {
683 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
684 if ($last_select) {
685 $qry_select .= ', ';
687 $qry_select .= $curField[$x];
688 $last_select = 1;
690 } // end for
691 if (!empty($qry_select)) {
692 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
693 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
696 // 2. FROM
698 // Create LEFT JOINS out of Relations
699 // Code originally by Mike Beck <mike.beck@ibmiller.de>
700 // If we can use Relations we could make some left joins.
701 // First find out if relations are available in this database.
703 // First we need the really needed Tables - those in TableList might still be
704 // all Tables.
705 if (isset($Field) && count($Field) > 0) {
707 // Initialize some variables
708 $tab_all = array();
709 $col_all = array();
710 $tab_wher = array();
711 $tab_know = array();
712 $tab_left = array();
713 $col_where = array();
714 $fromclause = '';
716 // We only start this if we have fields, otherwise it would be dumb
717 foreach ($Field AS $value) {
718 $parts = explode('.', $value);
719 if (!empty($parts[0]) && !empty($parts[1])) {
720 $tab_raw = urldecode($parts[0]);
721 $tab = str_replace('`', '', $tab_raw);
722 $tab_all[$tab] = $tab;
724 $col_raw = urldecode($parts[1]);
725 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
727 } // end while
729 // Check 'where' clauses
730 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
731 // Now we need all tables that we have in the where clause
732 $crit_cnt = count($Criteria);
733 for ($x = 0; $x < $crit_cnt; $x++) {
734 $curr_tab = explode('.', urldecode($Field[$x]));
735 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
736 $tab_raw = urldecode($curr_tab[0]);
737 $tab = str_replace('`', '', $tab_raw);
739 $col_raw = urldecode($curr_tab[1]);
740 $col1 = str_replace('`', '', $col_raw);
741 $col1 = $tab . '.' . $col1;
742 // Now we know that our array has the same numbers as $Criteria
743 // we can check which of our columns has a where clause
744 if (!empty($Criteria[$x])) {
745 if (substr($Criteria[$x], 0, 1) == '=' || stristr($Criteria[$x], 'is')) {
746 $col_where[$col] = $col1;
747 $tab_wher[$tab] = $tab;
749 } // end if
750 } // end if
751 } // end for
753 // Cleans temp vars w/o further use
754 unset($tab_raw);
755 unset($col_raw);
756 unset($col1);
758 if (count($tab_wher) == 1) {
759 // If there is exactly one column that has a decent where-clause
760 // we will just use this
761 $master = key($tab_wher);
762 } else {
763 // Now let's find out which of the tables has an index
764 // ( When the control user is the same as the normal user
765 // because he is using one of his databases as pmadb,
766 // the last db selected is not always the one where we need to work)
767 PMA_DBI_select_db($db);
769 foreach ($tab_all AS $tab) {
770 $ind_rs = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tab) . ';');
771 while ($ind = PMA_DBI_fetch_assoc($ind_rs)) {
772 $col1 = $tab . '.' . $ind['Column_name'];
773 if (isset($col_all[$col1])) {
774 if ($ind['non_unique'] == 0) {
775 if (isset($col_where[$col1])) {
776 $col_unique[$col1] = 'Y';
777 } else {
778 $col_unique[$col1] = 'N';
780 } else {
781 if (isset($col_where[$col1])) {
782 $col_index[$col1] = 'Y';
783 } else {
784 $col_index[$col1] = 'N';
788 } // end while (each col of tab)
789 } // end while (each tab)
790 // now we want to find the best.
791 if (isset($col_unique) && count($col_unique) > 0) {
792 $col_cand = $col_unique;
793 $needsort = 1;
794 } else if (isset($col_index) && count($col_index) > 0) {
795 $col_cand = $col_index;
796 $needsort = 1;
797 } else if (isset($col_where) && count($col_where) > 0) {
798 $col_cand = $tab_wher;
799 $needsort = 0;
800 } else {
801 $col_cand = $tab_all;
802 $needsort = 0;
805 // If we came up with $col_unique (very good) or $col_index (still
806 // good) as $col_cand we want to check if we have any 'Y' there
807 // (that would mean that they were also found in the whereclauses
808 // which would be great). if yes, we take only those
809 if ($needsort == 1) {
810 foreach ($col_cand AS $col => $is_where) {
811 $tab = explode('.', $col);
812 $tab = $tab[0];
813 if ($is_where == 'Y') {
814 $vg[$col] = $tab;
815 } else {
816 $sg[$col] = $tab;
819 if (isset($vg)) {
820 $col_cand = $vg;
821 // Candidates restricted in index+where
822 } else {
823 $col_cand = $sg;
824 // None of the candidates where in a where-clause
828 // If our array of candidates has more than one member we'll just
829 // find the smallest table.
830 // Of course the actual query would be faster if we check for
831 // the Criteria which gives the smallest result set in its table,
832 // but it would take too much time to check this
833 if (count($col_cand) > 1) {
834 // Of course we only want to check each table once
835 $checked_tables = $col_cand;
836 foreach ($col_cand AS $tab) {
837 if ($checked_tables[$tab] != 1 ) {
838 $rows_qry = 'SELECT COUNT(1) AS anz '
839 . 'FROM ' . PMA_backquote($tab);
840 $rows_rs = PMA_DBI_query($rows_qry);
841 while ($res = PMA_DBI_fetch_assoc($rows_rs)) {
842 $tsize[$tab] = $res['anz'];
844 PMA_DBI_free_result($rows_rs);
845 unset($rows_rs);
846 $checked_tables[$tab] = 1;
848 $csize[$tab] = $tsize[$tab];
850 asort($csize);
851 reset($csize);
852 $master = key($csize); // Smallest
853 } else {
854 reset($col_cand);
855 $master = current($col_cand); // Only one single candidate
857 } // end if (exactly one where clause)
860 * Removes unwanted entries from an array (PHP3 compliant)
862 * @param array the array to work with
863 * @param array the list of keys to remove
865 * @return array the cleaned up array
867 * @access private
869 function PMA_arrayShort($array, $key)
871 foreach ($array AS $k => $v) {
872 if ($k != $key) {
873 $reta[$k] = $v;
876 if (!isset($reta)) {
877 $reta = array();
880 return $reta;
881 } // end of the "PMA_arrayShort()" function
885 * Finds all related tables
887 * @param string wether to go from master to foreign or vice versa
889 * @return boolean always TRUE
891 * @global array the list of tables that we still couldn't connect
892 * @global array the list of allready connected tables
893 * @global string the current databse name
894 * @global string the super user connection id
895 * @global array the list of relation settings
897 * @access private
899 function PMA_getRelatives($from) {
900 global $tab_left, $tab_know, $fromclause;
901 global $dbh, $db, $cfgRelation;
903 if ($from == 'master') {
904 $to = 'foreign';
905 } else {
906 $to = 'master';
908 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
909 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
911 $rel_query = 'SELECT *'
912 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
913 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
914 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
915 . ' AND ' . $from . '_table IN ' . $in_know
916 . ' AND ' . $to . '_table IN ' . $in_left;
917 PMA_DBI_select_db($cfgRelation['db'], $dbh);
918 $relations = @PMA_DBI_query($rel_query, $dbh);
919 PMA_DBI_select_db($db, $dbh);
920 while ($row = PMA_DBI_fetch_assoc($relations)) {
921 $found_table = $row[$to . '_table'];
922 if (isset($tab_left[$found_table])) {
923 $fromclause .= "\n" . ' LEFT JOIN '
924 . PMA_backquote($row[$to . '_table']) . ' ON '
925 . PMA_backquote($row[$from . '_table']) . '.'
926 . PMA_backquote($row[$from . '_field']) . ' = '
927 . PMA_backquote($row[$to . '_table']) . '.'
928 . PMA_backquote($row[$to . '_field']) . ' ';
929 $tab_know[$found_table] = $found_table;
930 $tab_left = PMA_arrayShort($tab_left, $found_table);
932 } // end while
934 return TRUE;
935 } // end of the "PMA_getRelatives()" function
938 $tab_left = PMA_arrayShort($tab_all, $master);
939 $tab_know[$master] = $master;
941 $run = 0;
942 $emerg = '';
943 while (count($tab_left) > 0) {
944 if ($run % 2 == 0) {
945 PMA_getRelatives('master');
946 } else {
947 PMA_getRelatives('foreign');
949 $run++;
950 if ($run > 5) {
952 foreach ($tab_left AS $tab) {
953 $emerg .= ', ' . PMA_backquote($tab);
954 $tab_left = PMA_arrayShort($tab_left, $tab);
957 } // end while
958 $qry_from = PMA_backquote($master) . $emerg . $fromclause;
959 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
961 } // end count($Field) > 0
963 // In case relations are not defined, just generate the FROM clause
964 // from the list of tables, however we don't generate any JOIN
966 if (empty($qry_from) && isset($tab_all)) {
967 $qry_from = implode(', ', $tab_all);
969 // Now let's see what we got
970 if (!empty($qry_from)) {
971 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
972 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
975 // 3. WHERE
976 $qry_where = '';
977 $criteria_cnt = 0;
978 for ($x = 0; $x < $col; $x++) {
979 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where) && isset($curAndOrCol)) {
980 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
982 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
983 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
984 $last_where = $x;
985 $criteria_cnt++;
987 } // end for
988 if ($criteria_cnt > 1) {
989 $qry_where = '(' . $qry_where . ')';
991 // OR rows ${'cur' . $or}[$x]
992 if (!isset($curAndOrRow)) {
993 $curAndOrRow = array();
995 for ($y = 0; $y <= $row; $y++) {
996 $criteria_cnt = 0;
997 $qry_orwhere = '';
998 $last_orwhere = '';
999 for ($x = 0; $x < $col; $x++) {
1000 if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x]) && $x) {
1001 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
1003 if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x])) {
1004 $qry_orwhere .= '(' . $curField[$x]
1005 . ' '
1006 . ${'curOr' . $y}[$x]
1007 . ')';
1008 $last_orwhere = $x;
1009 $criteria_cnt++;
1011 } // end for
1012 if ($criteria_cnt > 1) {
1013 $qry_orwhere = '(' . $qry_orwhere . ')';
1015 if (!empty($qry_orwhere)) {
1016 $qry_where .= "\n"
1017 . strtoupper(isset($curAndOrRow[$y]) ? $curAndOrRow[$y] . ' ' : '')
1018 . $qry_orwhere;
1019 } // end if
1020 } // end for
1022 if (!empty($qry_where) && $qry_where != '()') {
1023 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
1024 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1025 } // end if
1027 // 4. ORDER BY
1028 $last_orderby = 0;
1029 if (!isset($qry_orderby)) {
1030 $qry_orderby = '';
1032 for ($x = 0; $x < $col; $x++) {
1033 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1034 $qry_orderby .= ', ';
1036 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1037 // if they have chosen all fields using the * selector,
1038 // then sorting is not available
1039 // Robbat2 - Fix for Bug #570698
1040 if (substr($curField[$x], -2) != '.*') {
1041 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1042 $last_orderby = 1;
1045 } // end for
1046 if (!empty($qry_orderby)) {
1047 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1048 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1051 </textarea>
1052 <input type="hidden" name="encoded_sql_query" value="<?php echo $encoded_qry; ?>" />
1053 </td>
1054 </tr>
1055 <tr>
1056 <!-- Generates a query -->
1057 <td align="right" class="tblHeaders"><input type="submit" name="modify" value="<?php echo $strUpdateQuery; ?>" /></td>
1058 <td>&nbsp;</td>
1059 <!-- Execute a query -->
1060 <td align="right" class="tblHeaders"><input type="submit" name="submit_sql" value="<?php echo $strRunQuery; ?>" /></td>
1061 </tr>
1062 </table>
1063 </form>
1064 <?php
1066 * Displays the footer
1068 require_once('./footer.inc.php');