3 // vim: expandtab sw=4 ts=4 sts=4:
7 * Get the values of the variables posted or sent to this script and display
10 require('./libraries/grab_globals.lib.php3');
11 require('./libraries/common.lib.php3');
12 require('./libraries/relation.lib.php3');
16 * Gets the relation settings
18 $cfgRelation = PMA_getRelationsParam();
22 * A query has been submitted -> execute it, else display the headers
24 if (isset($submit_sql) && eregi('^SELECT', $encoded_sql_query)) {
25 $goto = 'db_details.php3';
26 $zero_rows = htmlspecialchars($strSuccess);
27 $sql_query = urldecode($encoded_sql_query);
28 include('./sql.php3');
32 include('./db_details_common.php3');
33 $url_query .= '&goto=db_details_qbe.php3';
34 include('./db_details_db_info.php3');
37 if (isset($submit_sql) && !eregi('^SELECT', $encoded_sql_query)) {
38 echo '<p class="warning">' . $strHaveToShow . '</p>';
43 * Initialize some variables
45 if (empty($Columns)) {
46 $Columns = 3; // Initial number of columns
48 if (!isset($Add_Col)) {
51 if (!isset($Add_Row)) {
57 if (!isset($InsCol)) {
60 if (!isset($DelCol)) {
63 if (!isset($prev_Criteria)) {
66 // workaround for a PHP3 problem
67 if (!isset($Criteria)) {
70 for ($i = 0; $i < $Columns; $i++
) {
74 if (!isset($InsRow)) {
77 for ($i = 0; $i < $Columns; $i++
) {
81 if (!isset($DelRow)) {
84 for ($i = 0; $i < $Columns; $i++
) {
88 if (!isset($AndOrRow)) {
91 for ($i = 0; $i < $Columns; $i++
) {
95 if (!isset($AndOrCol)) {
98 for ($i = 0; $i < $Columns; $i++
) {
104 $col = $Columns +
$Add_Col;
108 $row = $Rows +
$Add_Row;
117 $tbl_result = PMA_mysql_list_tables($db);
118 $tbl_result_cnt = mysql_num_rows($tbl_result);
122 // The tables list sent by a previously submitted form
123 if (!empty($TableList)) {
124 for ($x = 0; $x < count($TableList); $x++
) {
125 $tbl_names[urldecode($TableList[$x])] = ' selected="selected"';
129 // The tables list gets from MySQL
130 while ($i < $tbl_result_cnt) {
131 $tbl = PMA_mysql_tablename($tbl_result, $i);
132 $fld_results = @PMA_mysql_list_fields_alternate
($db, $tbl) or PMA_mysqlDie(PMA_mysql_error(), 'PMA_mysql_list_fields_alternate(' . $db . ', ' . $tbl . ')', FALSE, $err_url);
133 $fld_results_cnt = ($fld_results) ?
count($fld_results) : 0;
136 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
137 $tbl_names[$tbl] = '';
139 $tbl_names[$tbl] = ' selected="selected"';
142 // The fields list per selected tables
143 if ($tbl_names[$tbl] == ' selected="selected"') {
144 $fld[$k++
] = PMA_backquote($tbl) . '.*';
145 while ($j < $fld_results_cnt) {
146 $fld[$k] = PMA_convert_display_charset($fld_results[$j]['Field']);
147 $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
149 // increase the width if necessary
150 if (strlen($fld[$k]) > $wid) {
151 $wid = strlen($fld[$k]);
161 mysql_free_result($tbl_result);
163 // largest width found
164 $realwidth = $wid . 'ex';
172 <!-- Query by example form
-->
173 <form action
="db_details_qbe.php3" method
="post">
174 <table border
="<?php echo $cfg['Border']; ?>">
178 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
179 <b
><?php
echo $strField; ?
> 
;: 
;</b
>
183 for ($x = 0; $x < $col; $x++
) {
184 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
186 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
187 <select style
="width: <?php echo $realwidth; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
188 <option value
=""></option
>
191 for ($y = 0; $y < sizeof($fld); $y++
) {
192 if ($fld[$y] == '') {
193 $sel = ' selected="selected"';
198 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
208 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
212 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
213 <select style
="width: <?php echo $realwidth; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
214 <option value
=""></option
>
217 for ($y = 0; $y < sizeof($fld); $y++
) {
218 if (isset($Field[$x]) && $fld[$y] == urldecode($Field[$x])) {
219 $curField[$z] = urldecode($Field[$x]);
220 $sel = ' selected="selected"';
225 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
239 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
240 <b
><?php
echo $strSort; ?
> 
;: 
;</b
>
244 for ($x = 0; $x < $col; $x++
) {
245 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
247 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
248 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
249 <option value
=""></option
>
250 <option value
="ASC"><?php
echo $strAscending; ?
></option
>
251 <option value
="DESC"><?php
echo $strDescending; ?
></option
>
259 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
263 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
264 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
265 <option value
=""></option
>
269 // If they have chosen all fields using the * selector,
270 // then sorting is not available
271 // Robbat2 - Fix for Bug #570698
272 if (isset($Sort[$x]) && isset($Field[$x]) && (substr(urldecode($Field[$x]),-2) == '.*')) {
276 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
277 $curSort[$z] = $Sort[$x];
278 $sel = ' selected="selected"';
283 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
284 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
285 $curSort[$z] = $Sort[$x];
286 $sel = ' selected="selected"';
291 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
304 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
305 <b
><?php
echo $strShow; ?
> 
;: 
;</b
>
309 for ($x = 0; $x < $col; $x++
) {
310 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
312 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
313 <input type
="checkbox" name
="Show[<?php echo $z; ?>]" />
320 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
323 if (isset($Show[$x])) {
324 $checked = ' checked="checked"';
325 $curShow[$z] = $Show[$x];
330 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
331 <input type
="checkbox" name
="Show[<?php echo $z; ?>]"<?php
echo $checked; ?
> />
340 <!-- Criteria row
-->
342 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
343 <b
><?php
echo $strCriteria; ?
> 
;: 
;</b
>
347 for ($x = 0; $x < $col; $x++
) {
348 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
350 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
351 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
358 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
361 if (isset($Criteria[$x])) {
362 $stripped_Criteria = $Criteria[$x];
364 if ((empty($prev_Criteria) ||
!isset($prev_Criteria[$x]))
365 ||
urldecode($prev_Criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
366 $curCriteria[$z] = $stripped_Criteria;
367 $encoded_Criteria = urlencode($stripped_Criteria);
369 $curCriteria[$z] = urldecode($prev_Criteria[$x]);
370 $encoded_Criteria = $prev_Criteria[$x];
373 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
374 <input type
="hidden" name
="prev_Criteria[<?php echo $z; ?>]" value
="<?php echo $encoded_Criteria; ?>" />
375 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
384 <!-- And/Or columns
and rows
-->
387 for ($y = 0; $y <= $row; $y++
) {
388 $bgcolor = ($y %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
389 if (isset($InsRow[$y]) && $InsRow[$y] == 'on') {
390 $chk['or'] = ' checked="checked"';
394 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $bgcolor; ?>" nowrap
="nowrap">
395 <!-- Row controls
-->
396 <table bgcolor
="<?php echo $bgcolor; ?>">
398 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
399 <small
><?php
echo $strQBEIns; ?
> 
;:</small
>
400 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
402 <td align
="<?php echo $cell_align_right; ?>">
403 <b
><?php
echo $strAnd; ?
> 
;:</b
>
406 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
411 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
412 <small
><?php
echo $strQBEDel; ?
> 
;:</small
>
413 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
415 <td align
="<?php echo $cell_align_right; ?>">
416 <b
><?php
echo $strOr; ?
> 
;:</b
>
419 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
427 for ($x = 0; $x < $col; $x++
) {
428 if ($InsCol[$x] == 'on') {
430 $or = 'Or' . $w . '[' . $z . ']';
432 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
433 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
438 if ($DelCol[$x] == 'on') {
443 $or = 'Or' . $w . '[' . $z . ']';
445 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
446 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
458 if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
462 if (isset($AndOrRow[$y])) {
463 $curAndOrRow[$w] = $AndOrRow[$y];
465 if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
466 $chk['and'] = ' checked="checked"';
469 $chk['or'] = ' checked="checked"';
475 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
476 <!-- Row controls
-->
477 <table bgcolor
="<?php echo $bgcolor; ?>">
479 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
480 <small
><?php
echo $strQBEIns; ?
> 
;:</small
>
481 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
483 <td align
="<?php echo $cell_align_right; ?>">
484 <b
><?php
echo $strAnd; ?
> 
;:</b
>
487 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
491 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
492 <small
><?php
echo $strQBEDel; ?
> 
;:</small
>
493 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
495 <td align
="<?php echo $cell_align_right; ?>">
496 <b
><?php
echo $strOr; ?
> 
;:</b
>
499 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
506 for ($x = 0; $x < $col; $x++
) {
507 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
509 $or = 'Or' . $w . '[' . $z . ']';
511 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
512 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
517 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
523 if (!isset($
{$or})) {
526 if (!empty($
{$or}) && isset($
{$or}[$x])) {
527 $stripped_or = $
{$or}[$x];
532 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
533 <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
>
536 if (!empty($
{$or}) && isset($
{$or}[$x])) {
537 $
{'cur' . $or}[$z] = $
{$or}[$x];
550 <!-- Modify columns
-->
552 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
553 <b
><?php
echo $strModify; ?
> 
;: 
;</b
>
557 for ($x = 0; $x < $col; $x++
) {
558 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
559 $curAndOrCol[$z] = $AndOrCol[$y];
560 if ($AndOrCol[$z] == 'or') {
561 $chk['or'] = ' checked="checked"';
564 $chk['and'] = ' checked="checked"';
568 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
569 <b
><?php
echo $strOr; ?
> 
;:</b
>
570 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
571  
; 
;<b
><?php
echo $strAnd; ?
> 
;:</b
>
572 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
574 <?php
echo $strQBEIns . "\n"; ?
>
575 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
576  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
577 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
584 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
588 if (isset($AndOrCol[$y])) {
589 $curAndOrCol[$z] = $AndOrCol[$y];
591 if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
592 $chk['or'] = ' checked="checked"';
595 $chk['and'] = ' checked="checked"';
599 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
600 <b
><?php
echo $strOr; ?
> 
;:</b
>
601 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
602  
; 
;<b
><?php
echo $strAnd; ?
> 
;:</b
>
603 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
605 <?php
echo $strQBEIns . "\n"; ?
>
606 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
607  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
608 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
619 <!-- Other controls
-->
623 <table border
="0" align
="<?php echo $cell_align_left; ?>">
625 <td rowspan
="4" valign
="top">
626 <?php
echo $strUseTables; ?
> 
;:
628 <select name
="TableList[]" size
="7" multiple
="multiple">
630 while (list($key, $val) = each($tbl_names)) {
632 echo '<option value="' . htmlspecialchars($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
637 <td align
="<?php echo $cell_align_right; ?>" valign
="bottom">
638 <input type
="hidden" value
="<?php echo htmlspecialchars($db); ?>" name
="db" />
639 <input type
="hidden" value
="<?php echo $z; ?>" name
="Columns" />
643 <input type
="hidden" value
="<?php echo $w; ?>" name
="Rows" />
644 <?php
echo $strAddDeleteRow; ?
> 
;:
645 <select size
="1" name
="Add_Row">
646 <option value
="-3">-3</option
>
647 <option value
="-2">-2</option
>
648 <option value
="-1">-1</option
>
649 <option value
="0" selected
="selected">0</option
>
650 <option value
="1">1</option
>
651 <option value
="2">2</option
>
652 <option value
="3">3</option
>
657 <td align
="<?php echo $cell_align_right; ?>" valign
="bottom">
658 <?php
echo $strAddDeleteColumn; ?
> 
;:
659 <select size
="1" name
="Add_Col">
660 <option value
="-3">-3</option
>
661 <option value
="-2">-2</option
>
662 <option value
="-1">-1</option
>
663 <option value
="0" selected
="selected">0</option
>
664 <option value
="1">1</option
>
665 <option value
="2">2</option
>
666 <option value
="3">3</option
>
670 <!-- Generates a query
-->
671 <tr align
="center" valign
="top">
673 <input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" />
674 <?php
echo PMA_generate_common_hidden_inputs(); ?
>
677 <!-- Executes a query
-->
678 <tr align
="center" valign
="top">
680 <input type
="submit" name
="submit_sql" value
="<?php echo $strRunQuery; ?>" />
686 <!-- Displays the current query
-->
687 <?php
echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?
><br
/>
688 <textarea cols
="30" rows
="7" name
="sql_query" dir
="<?php echo $text_dir; ?>">
693 if (!isset($qry_select)) {
696 for ($x = 0; $x < $col; $x++
) {
697 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
701 $qry_select .= $curField[$x];
705 if (!empty($qry_select)) {
706 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
707 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
712 // Create LEFT JOINS out of Relations
713 // Code originally by Mike Beck <mike.beck@ibmiller.de>
714 // If we can use Relations we could make some left joins.
715 // First find out if relations are available in this database.
718 //echo '</textarea><pre style="background-color: white;">';
719 // First we need the really needed Tables - those in TableList might still be
721 if (isset($Field) && count($Field) > 0) {
723 // Initialize some variables
729 $col_where = array();
732 // We only start this if we have fields, otherwise it would be dumb
733 // echo "get everything\n";
734 while (list(, $value) = each($Field)) {
735 $parts = explode('.', $value);
736 if (!empty($parts[0]) && !empty($parts[1])) {
737 $tab_raw = urldecode($parts[0]);
738 $tab = str_replace('`', '', $tab_raw);
739 // echo 'new Tab: ' . $tab . "\n";
740 $tab_all[$tab] = $tab;
741 $col_raw = urldecode($parts[1]);
742 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
743 // echo 'new col: ' . $tab . '.' . str_replace('`', '', $col_raw) . "\n";
747 // echo "check where clauses\n";
748 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
749 // Now we need all tables that we have in the where clause
750 $crit_cnt = count($Criteria);
751 for ($x = 0; $x < $crit_cnt; $x++
) {
752 $curr_tab = explode('.', urldecode($Field[$x]));
753 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
754 $tab_raw = urldecode($curr_tab[0]);
755 $tab = str_replace('`', '', $tab_raw);
757 $col_raw = urldecode($curr_tab[1]);
758 $col1 = str_replace('`', '', $col_raw);
759 $col1 = $tab . '.' . $col1;
760 // Now we know that our array has the same numbers as $Criteria
761 // we can check which of our columns has a where clause
762 if (!empty($Criteria[$x])) {
763 if (substr($Criteria[$x], 0, 1) == '=' ||
eregi('is', $Criteria[$x])) {
764 $col_where[$col] = $col1;
765 $tab_wher[$tab] = $tab;
766 // echo 'new where clause: ' . $tab_wher[$tab] . "||\n";
772 // Cleans temp vars w/o further use
777 if (count($tab_wher) == 1) {
778 // If there is exactly one column that has a decent where-clause
779 // we will just use this
780 $master = key($tab_wher);
781 // echo 'nur ein where: master = ' .$master . "||\n";
783 // Now let's find out which of the tables has an index
784 // echo "prüfe indexe:\n";
785 while (list(, $tab) = each($tab_all)) {
786 $ind_qry = 'SHOW INDEX FROM ' . PMA_backquote($tab);
787 $ind_rs = PMA_mysql_query($ind_qry);
788 while ($ind = PMA_mysql_fetch_array($ind_rs)) {
789 $col1 = $tab . '.' . $ind['Column_name'];
790 if (isset($col_all[$col1])) {
791 if ($ind['non_unique'] == 0) {
792 if (isset($col_where[$col1])) {
793 $col_unique[$col1] = 'Y';
795 $col_unique[$col1] = 'N';
797 //echo 'neuen unique index gefunden: ' . $col . "\n";
799 if (isset($col_where[$col1])) {
800 $col_index[$col1] = 'Y';
802 $col_index[$col1] = 'N';
804 //echo 'neuen index gefunden: ' . $col . "\n";
807 } // end while (each col of tab)
808 } // end while (each tab)
809 // now we want to find the best.
810 if (isset($col_unique) && count($col_unique) > 0) {
811 $col_cand = $col_unique;
812 //echo "Kandidaten sind jetzt alle mit unique index\n";
814 } else if (isset($col_index) && count($col_index) > 0) {
815 $col_cand = $col_index;
817 //echo "Kandidaten sind jetzt alle mit index\n";
818 } else if (isset($col_where) && count($col_where) > 0) {
819 $col_cand = $tab_wher;
820 //echo "Kandidaten sind jetzt alle im whereclause\n";
823 $col_cand = $tab_all;
825 //echo "Kandidaten sind jetzt alle \n";
828 // If we came up with $col_unique (very good) or $col_index (still
829 // good) as $col_cand we want to check if we have any 'Y' there
830 // (that would mean that they were also found in the whereclauses
831 // which would be great). if yes, we take only those
832 if ($needsort == 1) {
833 while (list($col, $is_where) = each($col_cand)) {
834 $tab = explode('.', $col);
836 if ($is_where == 'Y') {
844 //echo "Kandidaten konnten auf index+where beschränkt werden\n";
847 //echo "keiner der Kandidaten mit Index ist im wherclause\n";
851 // If our array of candidates has more than one member we'll just
852 // find the smallest table.
853 // Of course the actual query would be faster if we check for
854 // the Criteria which gives the smallest result set in its table,
855 // but it would take too much time to check this
856 if (count($col_cand) > 1) {
857 //echo "wir haben immer noch mehr als einen Kandidaten. Prüfe Größe\n";
858 // Of course we only want to check each table once
859 $checked_tables = $col_cand;
860 while (list(, $tab) = each($col_cand)) {
861 if ($checked_tables[$tab] != 1 ) {
862 //echo 'prüfe jetzt: Tabelle ' . $tab . "\n";
863 $rows_qry = 'SELECT COUNT(1) AS anz '
864 . 'FROM ' . PMA_backquote($tab);
865 $rows_rs = PMA_mysql_query($rows_qry);
866 while ($res = PMA_mysql_fetch_array($rows_rs)) {
867 $tsize[$tab] = $res['anz'];
868 //echo "$tab hat: " . $tsize[$tab] . "\n";
870 $checked_tables[$tab] = 1;
872 $csize[$tab] = $tsize[$tab];
873 //echo 'erster csize: ' . $csize[$tab] . "\n";
877 $master = key($csize);
878 //echo 'kleinste Datei: ' . $master . "\n";
880 //$master = $col_cand[0];
882 $master = current($col_cand);
883 //echo 'master ist der einzige Kandidat: ' . $master . "\n";
885 } // end if (exactly one where clause)
886 //echo 'ich habe mich entschieden: ' . $master;
891 * Removes unwanted entries from an array (PHP3 compliant)
893 * @param array the array to work with
894 * @param array the list of keys to remove
896 * @return array the cleaned up array
900 function PMA_arrayShort($array, $key)
902 while (list($k, $v) = each($array)) {
912 } // end of the "PMA_arrayShort()" function
916 * Finds all related tables
918 * @param string wether to go from master to foreign or vice versa
920 * @return boolean always TRUE
922 * @global array the list of tables that we still couldn't connect
923 * @global array the list of allready connected tables
924 * @global string the current databse name
925 * @global string the super user connection id
926 * @global array the list of relation settings
930 function PMA_getRelatives($from) {
931 global $tab_left, $tab_know, $fromclause;
932 global $dbh, $db, $cfgRelation;
934 if ($from == 'master') {
939 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
940 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
942 $rel_query = 'SELECT *'
943 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
944 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
945 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
946 . ' AND ' . $from . '_table IN ' . $in_know
947 . ' AND ' . $to . '_table IN ' . $in_left;
949 PMA_mysql_select_db($cfgRelation['db'], $dbh);
950 $relations = @PMA_mysql_query
($rel_query, $dbh) or PMA_mysqlDie(PMA_mysql_error($dbh), $rel_query, '', $err_url_0);
951 PMA_mysql_select_db($db, $dbh);
953 PMA_mysql_select_db($cfgRelation['db']);
954 $relations = @PMA_mysql_query
($rel_query) or PMA_mysqlDie('', $rel_query, '', $err_url_0);
955 PMA_mysql_select_db($db);
957 while ($row = PMA_mysql_fetch_array($relations)) {
958 $found_table = $row[$to . '_table'];
959 if (isset($tab_left[$found_table])) {
960 $fromclause .= "\n" . ' LEFT JOIN '
961 . PMA_backquote($row[$to . '_table']) . ' ON '
962 . PMA_backquote($row[$from . '_table']) . '.'
963 . PMA_backquote($row[$from . '_field']) . ' = '
964 . PMA_backquote($row[$to . '_table']) . '.'
965 . PMA_backquote($row[$to . '_field']) . ' ';
966 $tab_know[$found_table] = $found_table;
967 $tab_left = PMA_arrayShort($tab_left, $found_table);
972 } // end of the "PMA_getRelatives()" function
975 $tab_left = PMA_arrayShort($tab_all, $master);
976 $tab_know[$master] = $master;
980 while (count($tab_left) > 0) {
982 PMA_getRelatives('master');
984 PMA_getRelatives('foreign');
989 while (list(, $tab) = each($tab_left)) {
990 $emerg .= ', ' . $tab;
991 $tab_left = PMA_arrayShort($tab_left, $tab);
995 $qry_from = $master . $emerg . $fromclause;
996 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
998 } // end count($Field) > 0
1000 // In case relations are not defined, just generate the FROM clause
1001 // from the list of tables, however we don't generate any JOIN
1003 if (empty($qry_from) && isset($tab_all)) {
1004 $qry_from = implode(', ', $tab_all);
1006 // Now let's see what we got
1007 if (!empty($qry_from)) {
1008 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
1009 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
1015 for ($x = 0; $x < $col; $x++
) {
1016 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where)) {
1017 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
1019 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
1020 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
1025 if ($criteria_cnt > 1) {
1026 $qry_where = '(' . $qry_where . ')';
1028 // OR rows ${'cur' . $or}[$x]
1029 if (!isset($curAndOrRow)) {
1030 $curAndOrRow = array();
1032 for ($y = 0; $y <= $row; $y++
) {
1036 for ($x = 0; $x < $col; $x++
) {
1037 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x]) && $x) {
1038 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
1040 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x])) {
1041 $qry_orwhere .= '(' . $curField[$x]
1043 . $
{'curOr' . $y}[$x]
1049 if ($criteria_cnt > 1) {
1050 $qry_orwhere = '(' . $qry_orwhere . ')';
1052 if (!empty($qry_orwhere)) {
1054 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
1059 if (!empty($qry_where) && $qry_where != '()') {
1060 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
1061 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1066 if (!isset($qry_orderby)) {
1069 for ($x = 0; $x < $col; $x++
) {
1070 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1071 $qry_orderby .= ', ';
1073 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1074 // if they have chosen all fields using the * selector,
1075 // then sorting is not available
1076 // Robbat2 - Fix for Bug #570698
1077 if (substr($curField[$x], -2) != '.*') {
1078 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1083 if (!empty($qry_orderby)) {
1084 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1085 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1089 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
1099 * Displays the footer
1101 require('./footer.inc.php3');