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
($db, $tbl) or PMA_mysqlDie(PMA_mysql_error(), 'PMA_mysql_list_fields(' . $db . ', ' . $tbl . ')', FALSE, $err_url);
133 $fld_results_cnt = ($fld_results) ?
mysql_num_fields($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_mysql_field_name($fld_results, $j);
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]);
159 mysql_free_result($fld_results);
164 mysql_free_result($tbl_result);
166 // largest width found
167 $realwidth = $wid . 'ex';
175 <!-- Query by example form
-->
176 <form action
="db_details_qbe.php3" method
="post">
177 <table border
="<?php echo $cfg['Border']; ?>">
181 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
182 <b
><?php
echo $strField; ?
> 
;: 
;</b
>
186 for ($x = 0; $x < $col; $x++
) {
187 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
189 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
190 <select style
="width: <?php echo $realwidth; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
191 <option value
=""></option
>
194 for ($y = 0; $y < sizeof($fld); $y++
) {
195 if ($fld[$y] == '') {
196 $sel = ' selected="selected"';
201 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
211 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
215 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
216 <select style
="width: <?php echo $realwidth; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
217 <option value
=""></option
>
220 for ($y = 0; $y < sizeof($fld); $y++
) {
221 if (isset($Field[$x]) && $fld[$y] == urldecode($Field[$x])) {
222 $curField[$z] = urldecode($Field[$x]);
223 $sel = ' selected="selected"';
228 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
242 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
243 <b
><?php
echo $strSort; ?
> 
;: 
;</b
>
247 for ($x = 0; $x < $col; $x++
) {
248 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
250 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
251 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
252 <option value
=""></option
>
253 <option value
="ASC"><?php
echo $strAscending; ?
></option
>
254 <option value
="DESC"><?php
echo $strDescending; ?
></option
>
262 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
266 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
267 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
268 <option value
=""></option
>
272 // If they have chosen all fields using the * selector,
273 // then sorting is not available
274 // Robbat2 - Fix for Bug #570698
275 if (isset($Sort[$x]) && isset($Field[$x]) && (substr(urldecode($Field[$x]),-2) == '.*')) {
279 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
280 $curSort[$z] = $Sort[$x];
281 $sel = ' selected="selected"';
286 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
287 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
288 $curSort[$z] = $Sort[$x];
289 $sel = ' selected="selected"';
294 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
307 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
308 <b
><?php
echo $strShow; ?
> 
;: 
;</b
>
312 for ($x = 0; $x < $col; $x++
) {
313 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
315 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
316 <input type
="checkbox" name
="Show[<?php echo $z; ?>]" />
323 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
326 if (isset($Show[$x])) {
327 $checked = ' checked="checked"';
328 $curShow[$z] = $Show[$x];
333 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
334 <input type
="checkbox" name
="Show[<?php echo $z; ?>]"<?php
echo $checked; ?
> />
343 <!-- Criteria row
-->
345 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
346 <b
><?php
echo $strCriteria; ?
> 
;: 
;</b
>
350 for ($x = 0; $x < $col; $x++
) {
351 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
353 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
354 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
361 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
364 if (isset($Criteria[$x])) {
365 $stripped_Criteria = $Criteria[$x];
367 if ((empty($prev_Criteria) ||
!isset($prev_Criteria[$x]))
368 ||
urldecode($prev_Criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
369 $curCriteria[$z] = $stripped_Criteria;
370 $encoded_Criteria = urlencode($stripped_Criteria);
372 $curCriteria[$z] = urldecode($prev_Criteria[$x]);
373 $encoded_Criteria = $prev_Criteria[$x];
376 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
377 <input type
="hidden" name
="prev_Criteria[<?php echo $z; ?>]" value
="<?php echo $encoded_Criteria; ?>" />
378 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
387 <!-- And/Or columns
and rows
-->
390 for ($y = 0; $y <= $row; $y++
) {
391 $bgcolor = ($y %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
392 if (isset($InsRow[$y]) && $InsRow[$y] == 'on') {
393 $chk['or'] = ' checked="checked"';
397 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $bgcolor; ?>" nowrap
="nowrap">
398 <!-- Row controls
-->
399 <table bgcolor
="<?php echo $bgcolor; ?>">
401 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
402 <small
><?php
echo $strQBEIns; ?
> 
;:</small
>
403 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
405 <td align
="<?php echo $cell_align_right; ?>">
406 <b
><?php
echo $strAnd; ?
> 
;:</b
>
409 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
414 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
415 <small
><?php
echo $strQBEDel; ?
> 
;:</small
>
416 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
418 <td align
="<?php echo $cell_align_right; ?>">
419 <b
><?php
echo $strOr; ?
> 
;:</b
>
422 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
430 for ($x = 0; $x < $col; $x++
) {
431 if ($InsCol[$x] == 'on') {
433 $or = 'Or' . $w . '[' . $z . ']';
435 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
436 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
441 if ($DelCol[$x] == 'on') {
446 $or = 'Or' . $w . '[' . $z . ']';
448 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
449 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
461 if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
465 if (isset($AndOrRow[$y])) {
466 $curAndOrRow[$w] = $AndOrRow[$y];
468 if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
469 $chk['and'] = ' checked="checked"';
472 $chk['or'] = ' checked="checked"';
478 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
479 <!-- Row controls
-->
480 <table bgcolor
="<?php echo $bgcolor; ?>">
482 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
483 <small
><?php
echo $strQBEIns; ?
> 
;:</small
>
484 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
486 <td align
="<?php echo $cell_align_right; ?>">
487 <b
><?php
echo $strAnd; ?
> 
;:</b
>
490 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
494 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
495 <small
><?php
echo $strQBEDel; ?
> 
;:</small
>
496 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
498 <td align
="<?php echo $cell_align_right; ?>">
499 <b
><?php
echo $strOr; ?
> 
;:</b
>
502 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
509 for ($x = 0; $x < $col; $x++
) {
510 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
512 $or = 'Or' . $w . '[' . $z . ']';
514 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
515 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
520 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
526 if (!isset($
{$or})) {
529 if (!empty($
{$or}) && isset($
{$or}[$x])) {
530 $stripped_or = $
{$or}[$x];
535 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
536 <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
>
539 if (!empty($
{$or}) && isset($
{$or}[$x])) {
540 $
{'cur' . $or}[$z] = $
{$or}[$x];
553 <!-- Modify columns
-->
555 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
556 <b
><?php
echo $strModify; ?
> 
;: 
;</b
>
560 for ($x = 0; $x < $col; $x++
) {
561 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
562 $curAndOrCol[$z] = $AndOrCol[$y];
563 if ($AndOrCol[$z] == 'or') {
564 $chk['or'] = ' checked="checked"';
567 $chk['and'] = ' checked="checked"';
571 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
572 <b
><?php
echo $strOr; ?
> 
;:</b
>
573 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
574  
; 
;<b
><?php
echo $strAnd; ?
> 
;:</b
>
575 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
577 <?php
echo $strQBEIns . "\n"; ?
>
578 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
579  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
580 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
587 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
591 if (isset($AndOrCol[$y])) {
592 $curAndOrCol[$z] = $AndOrCol[$y];
594 if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
595 $chk['or'] = ' checked="checked"';
598 $chk['and'] = ' checked="checked"';
602 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
603 <b
><?php
echo $strOr; ?
> 
;:</b
>
604 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
605  
; 
;<b
><?php
echo $strAnd; ?
> 
;:</b
>
606 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
608 <?php
echo $strQBEIns . "\n"; ?
>
609 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
610  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
611 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
622 <!-- Other controls
-->
626 <table border
="0" align
="<?php echo $cell_align_left; ?>">
628 <td rowspan
="4" valign
="top">
629 <?php
echo $strUseTables; ?
> 
;:
631 <select name
="TableList[]" size
="7" multiple
="multiple">
633 while (list($key, $val) = each($tbl_names)) {
635 echo '<option value="' . htmlspecialchars($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
640 <td align
="<?php echo $cell_align_right; ?>" valign
="bottom">
641 <input type
="hidden" value
="<?php echo htmlspecialchars($db); ?>" name
="db" />
642 <input type
="hidden" value
="<?php echo $z; ?>" name
="Columns" />
646 <input type
="hidden" value
="<?php echo $w; ?>" name
="Rows" />
647 <?php
echo $strAddDeleteRow; ?
> 
;:
648 <select size
="1" name
="Add_Row">
649 <option value
="-3">-3</option
>
650 <option value
="-2">-2</option
>
651 <option value
="-1">-1</option
>
652 <option value
="0" selected
="selected">0</option
>
653 <option value
="1">1</option
>
654 <option value
="2">2</option
>
655 <option value
="3">3</option
>
660 <td align
="<?php echo $cell_align_right; ?>" valign
="bottom">
661 <?php
echo $strAddDeleteColumn; ?
> 
;:
662 <select size
="1" name
="Add_Col">
663 <option value
="-3">-3</option
>
664 <option value
="-2">-2</option
>
665 <option value
="-1">-1</option
>
666 <option value
="0" selected
="selected">0</option
>
667 <option value
="1">1</option
>
668 <option value
="2">2</option
>
669 <option value
="3">3</option
>
673 <!-- Generates a query
-->
674 <tr align
="center" valign
="top">
676 <input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" />
677 <?php
echo PMA_generate_common_hidden_inputs(); ?
>
680 <!-- Executes a query
-->
681 <tr align
="center" valign
="top">
683 <input type
="submit" name
="submit_sql" value
="<?php echo $strRunQuery; ?>" />
689 <!-- Displays the current query
-->
690 <?php
echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?
><br
/>
691 <textarea cols
="30" rows
="7" name
="sql_query" dir
="<?php echo $text_dir; ?>">
696 if (!isset($qry_select)) {
699 for ($x = 0; $x < $col; $x++
) {
700 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
704 $qry_select .= $curField[$x];
708 if (!empty($qry_select)) {
709 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
710 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
715 // Create LEFT JOINS out of Relations
716 // Code originally by Mike Beck <mike.beck@ibmiller.de>
717 // If we can use Relations we could make some left joins.
718 // First find out if relations are available in this database.
721 //echo '</textarea><pre style="background-color: white;">';
722 // First we need the really needed Tables - those in TableList might still be
724 if (isset($Field) && count($Field) > 0) {
726 // Initialize some variables
732 $col_where = array();
735 // We only start this if we have fields, otherwise it would be dumb
736 // echo "get everything\n";
737 while (list(, $value) = each($Field)) {
738 $parts = explode('.', $value);
739 if (!empty($parts[0]) && !empty($parts[1])) {
740 $tab_raw = urldecode($parts[0]);
741 $tab = str_replace('`', '', $tab_raw);
742 // echo 'new Tab: ' . $tab . "\n";
743 $tab_all[$tab] = $tab;
744 $col_raw = urldecode($parts[1]);
745 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
746 // echo 'new col: ' . $tab . '.' . str_replace('`', '', $col_raw) . "\n";
750 // echo "check where clauses\n";
751 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
752 // Now we need all tables that we have in the where clause
753 $crit_cnt = count($Criteria);
754 for ($x = 0; $x < $crit_cnt; $x++
) {
755 $curr_tab = explode('.', urldecode($Field[$x]));
756 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
757 $tab_raw = urldecode($curr_tab[0]);
758 $tab = str_replace('`', '', $tab_raw);
760 $col_raw = urldecode($curr_tab[1]);
761 $col1 = str_replace('`', '', $col_raw);
762 $col1 = $tab . '.' . $col1;
763 // Now we know that our array has the same numbers as $Criteria
764 // we can check which of our columns has a where clause
765 if (!empty($Criteria[$x])) {
766 if (substr($Criteria[$x], 0, 1) == '=' ||
eregi('is', $Criteria[$x])) {
767 $col_where[$col] = $col1;
768 $tab_wher[$tab] = $tab;
769 // echo 'new where clause: ' . $tab_wher[$tab] . "||\n";
775 // Cleans temp vars w/o further use
780 if (count($tab_wher) == 1) {
781 // If there is exactly one column that has a decent where-clause
782 // we will just use this
783 $master = key($tab_wher);
784 // echo 'nur ein where: master = ' .$master . "||\n";
786 // Now let's find out which of the tables has an index
787 // echo "prüfe indexe:\n";
788 while (list(, $tab) = each($tab_all)) {
789 $ind_qry = 'SHOW INDEX FROM ' . PMA_backquote($tab);
790 $ind_rs = PMA_mysql_query($ind_qry);
791 while ($ind = PMA_mysql_fetch_array($ind_rs)) {
792 $col1 = $tab . '.' . $ind['Column_name'];
793 if (isset($col_all[$col1])) {
794 if ($ind['non_unique'] == 0) {
795 if (isset($col_where[$col1])) {
796 $col_unique[$col1] = 'Y';
798 $col_unique[$col1] = 'N';
800 //echo 'neuen unique index gefunden: ' . $col . "\n";
802 if (isset($col_where[$col1])) {
803 $col_index[$col1] = 'Y';
805 $col_index[$col1] = 'N';
807 //echo 'neuen index gefunden: ' . $col . "\n";
810 } // end while (each col of tab)
811 } // end while (each tab)
812 // now we want to find the best.
813 if (isset($col_unique) && count($col_unique) > 0) {
814 $col_cand = $col_unique;
815 //echo "Kandidaten sind jetzt alle mit unique index\n";
817 } else if (isset($col_index) && count($col_index) > 0) {
818 $col_cand = $col_index;
820 //echo "Kandidaten sind jetzt alle mit index\n";
821 } else if (isset($col_where) && count($col_where) > 0) {
822 $col_cand = $tab_wher;
823 //echo "Kandidaten sind jetzt alle im whereclause\n";
826 $col_cand = $tab_all;
828 //echo "Kandidaten sind jetzt alle \n";
831 // If we came up with $col_unique (very good) or $col_index (still
832 // good) as $col_cand we want to check if we have any 'Y' there
833 // (that would mean that they were also found in the whereclauses
834 // which would be great). if yes, we take only those
835 if ($needsort == 1) {
836 while (list($col, $is_where) = each($col_cand)) {
837 $tab = explode('.', $col);
839 if ($is_where == 'Y') {
847 //echo "Kandidaten konnten auf index+where beschränkt werden\n";
850 //echo "keiner der Kandidaten mit Index ist im wherclause\n";
854 // If our array of candidates has more than one member we'll just
855 // find the smallest table.
856 // Of course the actual query would be faster if we check for
857 // the Criteria which gives the smallest result set in its table,
858 // but it would take too much time to check this
859 if (count($col_cand) > 1) {
860 //echo "wir haben immer noch mehr als einen Kandidaten. Prüfe Größe\n";
861 // Of course we only want to check each table once
862 $checked_tables = $col_cand;
863 while (list(, $tab) = each($col_cand)) {
864 if ($checked_tables[$tab] != 1 ) {
865 //echo 'prüfe jetzt: Tabelle ' . $tab . "\n";
866 $rows_qry = 'SELECT COUNT(1) AS anz '
867 . 'FROM ' . PMA_backquote($tab);
868 $rows_rs = PMA_mysql_query($rows_qry);
869 while ($res = PMA_mysql_fetch_array($rows_rs)) {
870 $tsize[$tab] = $res['anz'];
871 //echo "$tab hat: " . $tsize[$tab] . "\n";
873 $checked_tables[$tab] = 1;
875 $csize[$tab] = $tsize[$tab];
876 //echo 'erster csize: ' . $csize[$tab] . "\n";
880 $master = key($csize);
881 //echo 'kleinste Datei: ' . $master . "\n";
883 //$master = $col_cand[0];
885 $master = current($col_cand);
886 //echo 'master ist der einzige Kandidat: ' . $master . "\n";
888 } // end if (exactly one where clause)
889 //echo 'ich habe mich entschieden: ' . $master;
894 * Removes unwanted entries from an array (PHP3 compliant)
896 * @param array the array to work with
897 * @param array the list of keys to remove
899 * @return array the cleaned up array
903 function PMA_arrayShort($array, $key)
905 while (list($k, $v) = each($array)) {
915 } // end of the "PMA_arrayShort()" function
919 * Finds all related tables
921 * @param string wether to go from master to foreign or vice versa
923 * @return boolean always TRUE
925 * @global array the list of tables that we still couldn't connect
926 * @global array the list of allready connected tables
927 * @global string the current databse name
928 * @global string the super user connection id
929 * @global array the list of relation settings
933 function PMA_getRelatives($from) {
934 global $tab_left, $tab_know, $fromclause;
935 global $dbh, $db, $cfgRelation;
937 if ($from == 'master') {
942 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
943 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
945 $rel_query = 'SELECT *'
946 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
947 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
948 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
949 . ' AND ' . $from . '_table IN ' . $in_know
950 . ' AND ' . $to . '_table IN ' . $in_left;
952 PMA_mysql_select_db($cfgRelation['db'], $dbh);
953 $relations = @PMA_mysql_query
($rel_query, $dbh) or PMA_mysqlDie(PMA_mysql_error($dbh), $rel_query, '', $err_url_0);
954 PMA_mysql_select_db($db, $dbh);
956 PMA_mysql_select_db($cfgRelation['db']);
957 $relations = @PMA_mysql_query
($rel_query) or PMA_mysqlDie('', $rel_query, '', $err_url_0);
958 PMA_mysql_select_db($db);
960 while ($row = PMA_mysql_fetch_array($relations)) {
961 $found_table = $row[$to . '_table'];
962 if (isset($tab_left[$found_table])) {
963 $fromclause .= "\n" . ' LEFT JOIN '
964 . PMA_backquote($row[$to . '_table']) . ' ON '
965 . PMA_backquote($row[$from . '_table']) . '.'
966 . PMA_backquote($row[$from . '_field']) . ' = '
967 . PMA_backquote($row[$to . '_table']) . '.'
968 . PMA_backquote($row[$to . '_field']) . ' ';
969 $tab_know[$found_table] = $found_table;
970 $tab_left = PMA_arrayShort($tab_left, $found_table);
975 } // end of the "PMA_getRelatives()" function
978 $tab_left = PMA_arrayShort($tab_all, $master);
979 $tab_know[$master] = $master;
983 while (count($tab_left) > 0) {
985 PMA_getRelatives('master');
987 PMA_getRelatives('foreign');
992 while (list(, $tab) = each($tab_left)) {
993 $emerg .= ', ' . $tab;
994 $tab_left = PMA_arrayShort($tab_left, $tab);
998 $qry_from = $master . $emerg . $fromclause;
999 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
1001 } // end count($Field) > 0
1003 // Now let's see what we got
1004 if (!empty($qry_from)) {
1005 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
1006 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
1012 for ($x = 0; $x < $col; $x++
) {
1013 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where)) {
1014 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
1016 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
1017 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
1022 if ($criteria_cnt > 1) {
1023 $qry_where = '(' . $qry_where . ')';
1025 // OR rows ${'cur' . $or}[$x]
1026 if (!isset($curAndOrRow)) {
1027 $curAndOrRow = array();
1029 for ($y = 0; $y <= $row; $y++
) {
1033 for ($x = 0; $x < $col; $x++
) {
1034 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x]) && $x) {
1035 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
1037 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x])) {
1038 $qry_orwhere .= '(' . $curField[$x]
1040 . $
{'curOr' . $y}[$x]
1046 if ($criteria_cnt > 1) {
1047 $qry_orwhere = '(' . $qry_orwhere . ')';
1049 if (!empty($qry_orwhere)) {
1051 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
1056 if (!empty($qry_where) && $qry_where != '()') {
1057 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
1058 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1063 if (!isset($qry_orderby)) {
1066 for ($x = 0; $x < $col; $x++
) {
1067 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1068 $qry_orderby .= ', ';
1070 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1071 // if they have chosen all fields using the * selector,
1072 // then sorting is not available
1073 // Robbat2 - Fix for Bug #570698
1074 if (substr($curField[$x], -2) != '.*') {
1075 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1080 if (!empty($qry_orderby)) {
1081 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1082 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1086 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
1096 * Displays the footer
1098 require('./footer.inc.php3');