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_once('./libraries/grab_globals.lib.php');
11 require_once('./libraries/common.lib.php');
12 require_once('./libraries/relation.lib.php');
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) && preg_match('@^SELECT@i', $encoded_sql_query)) {
25 $goto = 'db_details.php';
26 $zero_rows = htmlspecialchars($strSuccess);
27 $sql_query = urldecode($encoded_sql_query);
32 require('./db_details_common.php');
33 $url_query .= '&goto=db_details_qbe.php';
34 require('./db_details_db_info.php');
37 if (isset($submit_sql) && !preg_match('@^SELECT@i', $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 if (!isset($Criteria)) {
68 for ($i = 0; $i < $Columns; $i++
) {
72 if (!isset($InsRow)) {
74 for ($i = 0; $i < $Columns; $i++
) {
78 if (!isset($DelRow)) {
80 for ($i = 0; $i < $Columns; $i++
) {
84 if (!isset($AndOrRow)) {
86 for ($i = 0; $i < $Columns; $i++
) {
90 if (!isset($AndOrCol)) {
92 for ($i = 0; $i < $Columns; $i++
) {
98 $col = $Columns +
$Add_Col;
102 $row = $Rows +
$Add_Row;
111 $tbl_result = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', NULL, PMA_DBI_QUERY_STORE
);
112 $tbl_result_cnt = PMA_DBI_num_rows($tbl_result);
116 // The tables list sent by a previously submitted form
117 if (!empty($TableList)) {
118 $cnt_table_list = count($TableList);
119 for ($x = 0; $x < $cnt_table_list; $x++
) {
120 $tbl_names[urldecode($TableList[$x])] = ' selected="selected"';
124 // The tables list gets from MySQL
125 while ($i < $tbl_result_cnt) {
126 list($tbl) = PMA_DBI_fetch_row($tbl_result);
127 $fld_results = PMA_DBI_get_fields($db, $tbl);
128 $fld_results_cnt = ($fld_results) ?
count($fld_results) : 0;
131 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
132 $tbl_names[$tbl] = '';
134 $tbl_names[$tbl] = ' selected="selected"';
137 // The fields list per selected tables
138 if ($tbl_names[$tbl] == ' selected="selected"') {
139 $fld[$k++
] = PMA_backquote($tbl) . '.*';
140 while ($j < $fld_results_cnt) {
141 $fld[$k] = PMA_convert_display_charset($fld_results[$j]['Field']);
142 $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
144 // increase the width if necessary
145 if (strlen($fld[$k]) > $wid) {
146 $wid = strlen($fld[$k]);
156 PMA_DBI_free_result($tbl_result);
158 // largest width found
159 $realwidth = $wid . 'ex';
167 <!-- Query by example form
-->
168 <form action
="db_details_qbe.php" method
="post">
169 <table border
="<?php echo $cfg['Border']; ?>" cellpadding
="2" cellspacing
="1">
173 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
174 <b
><?php
echo $strField; ?
>: 
;</b
>
178 for ($x = 0; $x < $col; $x++
) {
179 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
181 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
182 <select style
="width: <?php echo $realwidth; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
183 <option value
=""></option
>
186 for ($y = 0; $y < sizeof($fld); $y++
) {
187 if ($fld[$y] == '') {
188 $sel = ' selected="selected"';
193 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
203 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
207 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
208 <select style
="width: <?php echo $realwidth; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
209 <option value
=""></option
>
212 for ($y = 0; $y < sizeof($fld); $y++
) {
213 if (isset($Field[$x]) && $fld[$y] == urldecode($Field[$x])) {
214 $curField[$z] = urldecode($Field[$x]);
215 $sel = ' selected="selected"';
220 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
234 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
235 <b
><?php
echo $strSort; ?
>: 
;</b
>
239 for ($x = 0; $x < $col; $x++
) {
240 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
242 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
243 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
244 <option value
=""></option
>
245 <option value
="ASC"><?php
echo $strAscending; ?
></option
>
246 <option value
="DESC"><?php
echo $strDescending; ?
></option
>
254 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
258 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
259 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
260 <option value
=""></option
>
264 // If they have chosen all fields using the * selector,
265 // then sorting is not available
266 // Robbat2 - Fix for Bug #570698
267 if (isset($Sort[$x]) && isset($Field[$x]) && (substr(urldecode($Field[$x]),-2) == '.*')) {
271 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
272 $curSort[$z] = $Sort[$x];
273 $sel = ' selected="selected"';
278 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
279 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
280 $curSort[$z] = $Sort[$x];
281 $sel = ' selected="selected"';
286 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
299 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
300 <b
><?php
echo $strShow; ?
>: 
;</b
>
304 for ($x = 0; $x < $col; $x++
) {
305 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
307 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
308 <input type
="checkbox" name
="Show[<?php echo $z; ?>]" />
315 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
318 if (isset($Show[$x])) {
319 $checked = ' checked="checked"';
320 $curShow[$z] = $Show[$x];
325 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
326 <input type
="checkbox" name
="Show[<?php echo $z; ?>]"<?php
echo $checked; ?
> />
335 <!-- Criteria row
-->
337 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
338 <b
><?php
echo $strCriteria; ?
>: 
;</b
>
342 for ($x = 0; $x < $col; $x++
) {
343 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
345 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
346 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
353 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
356 if (isset($Criteria[$x])) {
357 $stripped_Criteria = $Criteria[$x];
359 if ((empty($prev_Criteria) ||
!isset($prev_Criteria[$x]))
360 ||
urldecode($prev_Criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
361 $curCriteria[$z] = $stripped_Criteria;
362 $encoded_Criteria = urlencode($stripped_Criteria);
364 $curCriteria[$z] = urldecode($prev_Criteria[$x]);
365 $encoded_Criteria = $prev_Criteria[$x];
368 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
369 <input type
="hidden" name
="prev_Criteria[<?php echo $z; ?>]" value
="<?php echo $encoded_Criteria; ?>" />
370 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
379 <!-- And/Or columns
and rows
-->
382 for ($y = 0; $y <= $row; $y++
) {
383 $bgcolor = ($y %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
384 if (isset($InsRow[$y]) && $InsRow[$y] == 'on') {
385 $chk['or'] = ' checked="checked"';
389 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $bgcolor; ?>" nowrap
="nowrap">
390 <!-- Row controls
-->
391 <table bgcolor
="<?php echo $bgcolor; ?>" cellpadding
="0" cellspacing
="0" border
="0">
393 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
394 <small
><?php
echo $strQBEIns; ?
>:</small
>
395 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
397 <td align
="<?php echo $cell_align_right; ?>">
398 <b
><?php
echo $strAnd; ?
>:</b
>
401 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
406 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
407 <small
><?php
echo $strQBEDel; ?
>:</small
>
408 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
410 <td align
="<?php echo $cell_align_right; ?>">
411 <b
><?php
echo $strOr; ?
>:</b
>
414 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
422 for ($x = 0; $x < $col; $x++
) {
423 if (isset($InsCol[$x]) && $InsCol[$x] == 'on') {
425 $or = 'Or' . $w . '[' . $z . ']';
427 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
428 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
433 if (isset($DelCol[$x]) && $DelCol[$x] == 'on') {
438 $or = 'Or' . $w . '[' . $z . ']';
440 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
441 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
453 if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
457 if (isset($AndOrRow[$y])) {
458 $curAndOrRow[$w] = $AndOrRow[$y];
460 if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
461 $chk['and'] = ' checked="checked"';
464 $chk['or'] = ' checked="checked"';
470 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $bgcolor; ?>" nowrap
="nowrap">
471 <!-- Row controls
-->
472 <table bgcolor
="<?php echo $bgcolor; ?>" border
="0" cellpadding
="0" cellspacing
="0">
474 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
475 <small
><?php
echo $strQBEIns; ?
>:</small
>
476 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
478 <td align
="<?php echo $cell_align_right; ?>">
479 <b
><?php
echo $strAnd; ?
>:</b
>
482 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
486 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
487 <small
><?php
echo $strQBEDel; ?
>:</small
>
488 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
490 <td align
="<?php echo $cell_align_right; ?>">
491 <b
><?php
echo $strOr; ?
>:</b
>
494 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
501 for ($x = 0; $x < $col; $x++
) {
502 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
504 $or = 'Or' . $w . '[' . $z . ']';
506 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
507 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
512 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
518 if (!isset($
{$or})) {
521 if (!empty($
{$or}) && isset($
{$or}[$x])) {
522 $stripped_or = $
{$or}[$x];
527 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
528 <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
>
531 if (!empty($
{$or}) && isset($
{$or}[$x])) {
532 $
{'cur' . $or}[$z] = $
{$or}[$x];
545 <!-- Modify columns
-->
547 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
548 <b
><?php
echo $strModify; ?
>: 
;</b
>
552 for ($x = 0; $x < $col; $x++
) {
553 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
554 $curAndOrCol[$z] = $AndOrCol[$y];
555 if ($AndOrCol[$z] == 'or') {
556 $chk['or'] = ' checked="checked"';
559 $chk['and'] = ' checked="checked"';
563 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
564 <b
><?php
echo $strOr; ?
>:</b
>
565 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
566  
; 
;<b
><?php
echo $strAnd; ?
>:</b
>
567 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
569 <?php
echo $strQBEIns . "\n"; ?
>
570 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
571  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
572 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
579 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
583 if (isset($AndOrCol[$y])) {
584 $curAndOrCol[$z] = $AndOrCol[$y];
586 if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
587 $chk['or'] = ' checked="checked"';
590 $chk['and'] = ' checked="checked"';
594 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
595 <b
><?php
echo $strOr; ?
>:</b
>
596 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
597  
; 
;<b
><?php
echo $strAnd; ?
>:</b
>
598 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
600 <?php
echo $strQBEIns . "\n"; ?
>
601 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
602  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
603 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
613 <!-- Other controls
-->
614 <?php
echo PMA_generate_common_hidden_inputs(); ?
>
615 <table border
="0" cellpadding
="2" cellspacing
="1">
617 <td nowrap
="nowrap"><input type
="hidden" value
="<?php echo htmlspecialchars($db); ?>" name
="db" />
618 <input type
="hidden" value
="<?php echo $z; ?>" name
="Columns" />
622 <input type
="hidden" value
="<?php echo $w; ?>" name
="Rows" />
623 <?php
echo $strAddDeleteRow; ?
>:
624 <select size
="1" name
="Add_Row" style
="vertical-align: middle">
625 <option value
="-3">-3</option
>
626 <option value
="-2">-2</option
>
627 <option value
="-1">-1</option
>
628 <option value
="0" selected
="selected">0</option
>
629 <option value
="1">1</option
>
630 <option value
="2">2</option
>
631 <option value
="3">3</option
>
634 <td width
="10"> 
;</td
>
635 <td nowrap
="nowrap"><?php
echo $strAddDeleteColumn; ?
>:
636 <select size
="1" name
="Add_Col" style
="vertical-align: middle">
637 <option value
="-3">-3</option
>
638 <option value
="-2">-2</option
>
639 <option value
="-1">-1</option
>
640 <option value
="0" selected
="selected">0</option
>
641 <option value
="1">1</option
>
642 <option value
="2">2</option
>
643 <option value
="3">3</option
>
646 <td width
="10"> 
;</td
>
647 <!-- Generates a query
-->
648 <td
><input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" /></td
>
652 <table border
="0" cellpadding
="2" cellspacing
="1">
654 <td
class="tblHeaders"> 
;<?php
echo $strUseTables; ?
>: 
;</td
>
655 <td width
="20"> 
;</td
>
656 <td
class="tblHeaders"> 
;<?php
echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?
> 
;</td
>
659 <td bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
661 $strTableListOptions = '';
662 $numTableListOptions = 0;
663 foreach($tbl_names AS $key => $val) {
664 $strTableListOptions .= ' ';
665 $strTableListOptions .= '<option value="' . htmlspecialchars($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
666 $numTableListOptions++
;
669 <select name
="TableList[]" size
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>" multiple
="multiple" id
="listTable">
670 <?php
echo $strTableListOptions; ?
>
673 <td
> 
; 
;</td
>
674 <!-- Displays the current query
-->
675 <td bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
676 <textarea cols
="30" rows
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>" name
="sql_query" dir
="<?php echo $text_dir; ?>" id
="textSqlquery">
681 if (!isset($qry_select)) {
684 for ($x = 0; $x < $col; $x++
) {
685 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
689 $qry_select .= $curField[$x];
693 if (!empty($qry_select)) {
694 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
695 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
700 // Create LEFT JOINS out of Relations
701 // Code originally by Mike Beck <mike.beck@ibmiller.de>
702 // If we can use Relations we could make some left joins.
703 // First find out if relations are available in this database.
705 // First we need the really needed Tables - those in TableList might still be
707 if (isset($Field) && count($Field) > 0) {
709 // Initialize some variables
715 $col_where = array();
718 // We only start this if we have fields, otherwise it would be dumb
719 foreach ($Field AS $value) {
720 $parts = explode('.', $value);
721 if (!empty($parts[0]) && !empty($parts[1])) {
722 $tab_raw = urldecode($parts[0]);
723 $tab = str_replace('`', '', $tab_raw);
724 $tab_all[$tab] = $tab;
726 $col_raw = urldecode($parts[1]);
727 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
731 // Check 'where' clauses
732 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
733 // Now we need all tables that we have in the where clause
734 $crit_cnt = count($Criteria);
735 for ($x = 0; $x < $crit_cnt; $x++
) {
736 $curr_tab = explode('.', urldecode($Field[$x]));
737 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
738 $tab_raw = urldecode($curr_tab[0]);
739 $tab = str_replace('`', '', $tab_raw);
741 $col_raw = urldecode($curr_tab[1]);
742 $col1 = str_replace('`', '', $col_raw);
743 $col1 = $tab . '.' . $col1;
744 // Now we know that our array has the same numbers as $Criteria
745 // we can check which of our columns has a where clause
746 if (!empty($Criteria[$x])) {
747 if (substr($Criteria[$x], 0, 1) == '=' ||
stristr($Criteria[$x], 'is')) {
748 $col_where[$col] = $col1;
749 $tab_wher[$tab] = $tab;
755 // Cleans temp vars w/o further use
760 if (count($tab_wher) == 1) {
761 // If there is exactly one column that has a decent where-clause
762 // we will just use this
763 $master = key($tab_wher);
765 // Now let's find out which of the tables has an index
766 // ( When the control user is the same as the normal user
767 // because he is using one of his databases as pmadb,
768 // the last db selected is not always the one where we need to work)
769 PMA_DBI_select_db($db);
771 foreach ($tab_all AS $tab) {
772 $ind_rs = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tab) . ';');
773 while ($ind = PMA_DBI_fetch_assoc($ind_rs)) {
774 $col1 = $tab . '.' . $ind['Column_name'];
775 if (isset($col_all[$col1])) {
776 if ($ind['non_unique'] == 0) {
777 if (isset($col_where[$col1])) {
778 $col_unique[$col1] = 'Y';
780 $col_unique[$col1] = 'N';
783 if (isset($col_where[$col1])) {
784 $col_index[$col1] = 'Y';
786 $col_index[$col1] = 'N';
790 } // end while (each col of tab)
791 } // end while (each tab)
792 // now we want to find the best.
793 if (isset($col_unique) && count($col_unique) > 0) {
794 $col_cand = $col_unique;
796 } else if (isset($col_index) && count($col_index) > 0) {
797 $col_cand = $col_index;
799 } else if (isset($col_where) && count($col_where) > 0) {
800 $col_cand = $tab_wher;
803 $col_cand = $tab_all;
807 // If we came up with $col_unique (very good) or $col_index (still
808 // good) as $col_cand we want to check if we have any 'Y' there
809 // (that would mean that they were also found in the whereclauses
810 // which would be great). if yes, we take only those
811 if ($needsort == 1) {
812 foreach ($col_cand AS $col => $is_where) {
813 $tab = explode('.', $col);
815 if ($is_where == 'Y') {
823 // Candidates restricted in index+where
826 // None of the candidates where in a where-clause
830 // If our array of candidates has more than one member we'll just
831 // find the smallest table.
832 // Of course the actual query would be faster if we check for
833 // the Criteria which gives the smallest result set in its table,
834 // but it would take too much time to check this
835 if (count($col_cand) > 1) {
836 // Of course we only want to check each table once
837 $checked_tables = $col_cand;
838 foreach ($col_cand AS $tab) {
839 if ($checked_tables[$tab] != 1 ) {
840 $rows_qry = 'SELECT COUNT(1) AS anz '
841 . 'FROM ' . PMA_backquote($tab);
842 $rows_rs = PMA_DBI_query($rows_qry);
843 while ($res = PMA_DBI_fetch_assoc($rows_rs)) {
844 $tsize[$tab] = $res['anz'];
846 PMA_DBI_free_result($rows_rs);
848 $checked_tables[$tab] = 1;
850 $csize[$tab] = $tsize[$tab];
854 $master = key($csize); // Smallest
857 $master = current($col_cand); // Only one single candidate
859 } // end if (exactly one where clause)
862 * Removes unwanted entries from an array (PHP3 compliant)
864 * @param array the array to work with
865 * @param array the list of keys to remove
867 * @return array the cleaned up array
871 function PMA_arrayShort($array, $key)
873 foreach ($array AS $k => $v) {
883 } // end of the "PMA_arrayShort()" function
887 * Finds all related tables
889 * @param string wether to go from master to foreign or vice versa
891 * @return boolean always TRUE
893 * @global array the list of tables that we still couldn't connect
894 * @global array the list of allready connected tables
895 * @global string the current databse name
896 * @global string the super user connection id
897 * @global array the list of relation settings
901 function PMA_getRelatives($from) {
902 global $tab_left, $tab_know, $fromclause;
903 global $dbh, $db, $cfgRelation;
905 if ($from == 'master') {
910 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
911 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
913 $rel_query = 'SELECT *'
914 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
915 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
916 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
917 . ' AND ' . $from . '_table IN ' . $in_know
918 . ' AND ' . $to . '_table IN ' . $in_left;
919 PMA_DBI_select_db($cfgRelation['db'], $dbh);
920 $relations = @PMA_DBI_query
($rel_query, $dbh);
921 PMA_DBI_select_db($db, $dbh);
922 while ($row = PMA_DBI_fetch_assoc($relations)) {
923 $found_table = $row[$to . '_table'];
924 if (isset($tab_left[$found_table])) {
925 $fromclause .= "\n" . ' LEFT JOIN '
926 . PMA_backquote($row[$to . '_table']) . ' ON '
927 . PMA_backquote($row[$from . '_table']) . '.'
928 . PMA_backquote($row[$from . '_field']) . ' = '
929 . PMA_backquote($row[$to . '_table']) . '.'
930 . PMA_backquote($row[$to . '_field']) . ' ';
931 $tab_know[$found_table] = $found_table;
932 $tab_left = PMA_arrayShort($tab_left, $found_table);
937 } // end of the "PMA_getRelatives()" function
940 $tab_left = PMA_arrayShort($tab_all, $master);
941 $tab_know[$master] = $master;
945 while (count($tab_left) > 0) {
947 PMA_getRelatives('master');
949 PMA_getRelatives('foreign');
954 foreach ($tab_left AS $tab) {
955 $emerg .= ', ' . $tab;
956 $tab_left = PMA_arrayShort($tab_left, $tab);
960 $qry_from = $master . $emerg . $fromclause;
961 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
963 } // end count($Field) > 0
965 // In case relations are not defined, just generate the FROM clause
966 // from the list of tables, however we don't generate any JOIN
968 if (empty($qry_from) && isset($tab_all)) {
969 $qry_from = implode(', ', $tab_all);
971 // Now let's see what we got
972 if (!empty($qry_from)) {
973 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
974 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
980 for ($x = 0; $x < $col; $x++
) {
981 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where) && isset($curAndOrCol)) {
982 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
984 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
985 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
990 if ($criteria_cnt > 1) {
991 $qry_where = '(' . $qry_where . ')';
993 // OR rows ${'cur' . $or}[$x]
994 if (!isset($curAndOrRow)) {
995 $curAndOrRow = array();
997 for ($y = 0; $y <= $row; $y++
) {
1001 for ($x = 0; $x < $col; $x++
) {
1002 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x]) && $x) {
1003 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
1005 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x])) {
1006 $qry_orwhere .= '(' . $curField[$x]
1008 . $
{'curOr' . $y}[$x]
1014 if ($criteria_cnt > 1) {
1015 $qry_orwhere = '(' . $qry_orwhere . ')';
1017 if (!empty($qry_orwhere)) {
1019 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
1024 if (!empty($qry_where) && $qry_where != '()') {
1025 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
1026 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1031 if (!isset($qry_orderby)) {
1034 for ($x = 0; $x < $col; $x++
) {
1035 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1036 $qry_orderby .= ', ';
1038 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1039 // if they have chosen all fields using the * selector,
1040 // then sorting is not available
1041 // Robbat2 - Fix for Bug #570698
1042 if (substr($curField[$x], -2) != '.*') {
1043 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1048 if (!empty($qry_orderby)) {
1049 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1050 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1054 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
1058 <!-- Generates a query
-->
1059 <td align
="right" class="tblHeaders"><input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" /></td
>
1061 <!-- Execute a query
-->
1062 <td align
="right" class="tblHeaders"><input type
="submit" name
="submit_sql" value
="<?php echo $strRunQuery; ?>" /></td
>
1068 * Displays the footer
1070 require_once('./footer.inc.php');