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