3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Get the values of the variables posted or sent to this script and display
9 require_once('./libraries/common.lib.php');
10 require_once('./libraries/relation.lib.php');
14 * Gets the relation settings
16 $cfgRelation = PMA_getRelationsParam();
20 * A query has been submitted -> execute it, else display the headers
22 if (isset($submit_sql) && preg_match('@^SELECT@i', $encoded_sql_query)) {
23 $goto = 'db_details.php';
24 $zero_rows = htmlspecialchars($strSuccess);
25 $sql_query = urldecode($encoded_sql_query);
30 require('./db_details_common.php');
31 $url_query .= '&goto=db_details_qbe.php';
32 require('./db_details_db_info.php');
35 if (isset($submit_sql) && !preg_match('@^SELECT@i', $encoded_sql_query)) {
36 echo '<p class="warning">' . $strHaveToShow . '</p>';
41 * Initialize some variables
43 if (empty($Columns)) {
44 $Columns = 3; // Initial number of columns
46 if (!isset($Add_Col)) {
49 if (!isset($Add_Row)) {
55 if (!isset($InsCol)) {
58 if (!isset($DelCol)) {
61 if (!isset($prev_Criteria)) {
64 if (!isset($Criteria)) {
66 for ($i = 0; $i < $Columns; $i++
) {
70 if (!isset($InsRow)) {
72 for ($i = 0; $i < $Columns; $i++
) {
76 if (!isset($DelRow)) {
78 for ($i = 0; $i < $Columns; $i++
) {
82 if (!isset($AndOrRow)) {
84 for ($i = 0; $i < $Columns; $i++
) {
88 if (!isset($AndOrCol)) {
90 for ($i = 0; $i < $Columns; $i++
) {
96 $col = $Columns +
$Add_Col;
100 $row = $Rows +
$Add_Row;
109 $tbl_result = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', NULL, PMA_DBI_QUERY_STORE
);
110 $tbl_result_cnt = PMA_DBI_num_rows($tbl_result);
114 // The tables list sent by a previously submitted form
115 if (!empty($TableList)) {
116 $cnt_table_list = count($TableList);
117 for ($x = 0; $x < $cnt_table_list; $x++
) {
118 $tbl_names[urldecode($TableList[$x])] = ' selected="selected"';
122 // The tables list gets from MySQL
123 while ($i < $tbl_result_cnt) {
124 list($tbl) = PMA_DBI_fetch_row($tbl_result);
125 $fld_results = PMA_DBI_get_fields($db, $tbl);
126 $fld_results_cnt = ($fld_results) ?
count($fld_results) : 0;
129 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
130 $tbl_names[$tbl] = '';
132 $tbl_names[$tbl] = ' selected="selected"';
135 // The fields list per selected tables
136 if ($tbl_names[$tbl] == ' selected="selected"') {
137 $fld[$k++
] = PMA_backquote($tbl) . '.*';
138 while ($j < $fld_results_cnt) {
139 $fld[$k] = PMA_convert_display_charset($fld_results[$j]['Field']);
140 $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
142 // increase the width if necessary
143 if (strlen($fld[$k]) > $wid) {
144 $wid = strlen($fld[$k]);
154 PMA_DBI_free_result($tbl_result);
156 // largest width found
157 $realwidth = $wid . 'ex';
165 <!-- Query by example form
-->
166 <form action
="db_details_qbe.php" method
="post">
167 <table border
="<?php echo $cfg['Border']; ?>" cellpadding
="2" cellspacing
="1">
171 <td
class="tblHeaders" align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
172 <b
><?php
echo $strField; ?
>: 
;</b
>
176 for ($x = 0; $x < $col; $x++
) {
177 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
179 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
180 <select style
="width: <?php echo $realwidth; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
181 <option value
=""></option
>
184 for ($y = 0; $y < sizeof($fld); $y++
) {
185 if ($fld[$y] == '') {
186 $sel = ' selected="selected"';
191 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
201 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
205 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
206 <select style
="width: <?php echo $realwidth; ?>" name
="Field[<?php echo $z; ?>]" size
="1">
207 <option value
=""></option
>
210 for ($y = 0; $y < sizeof($fld); $y++
) {
211 if (isset($Field[$x]) && $fld[$y] == urldecode($Field[$x])) {
212 $curField[$z] = urldecode($Field[$x]);
213 $sel = ' selected="selected"';
218 echo '<option value="' . htmlspecialchars($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
232 <td
class="tblHeaders" align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
233 <b
><?php
echo $strSort; ?
>: 
;</b
>
237 for ($x = 0; $x < $col; $x++
) {
238 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
240 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
241 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
242 <option value
=""></option
>
243 <option value
="ASC"><?php
echo $strAscending; ?
></option
>
244 <option value
="DESC"><?php
echo $strDescending; ?
></option
>
252 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
256 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
257 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
258 <option value
=""></option
>
262 // If they have chosen all fields using the * selector,
263 // then sorting is not available
264 // Robbat2 - Fix for Bug #570698
265 if (isset($Sort[$x]) && isset($Field[$x]) && (substr(urldecode($Field[$x]),-2) == '.*')) {
269 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
270 $curSort[$z] = $Sort[$x];
271 $sel = ' selected="selected"';
276 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
277 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
278 $curSort[$z] = $Sort[$x];
279 $sel = ' selected="selected"';
284 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
297 <td
class="tblHeaders" align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
298 <b
><?php
echo $strShow; ?
>: 
;</b
>
302 for ($x = 0; $x < $col; $x++
) {
303 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
305 <td
class="tblHeaders" align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
306 <input type
="checkbox" name
="Show[<?php echo $z; ?>]" />
313 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
316 if (isset($Show[$x])) {
317 $checked = ' checked="checked"';
318 $curShow[$z] = $Show[$x];
323 <td
class="tblHeaders" align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
324 <input type
="checkbox" name
="Show[<?php echo $z; ?>]"<?php
echo $checked; ?
> />
333 <!-- Criteria row
-->
335 <td
class="tblHeaders" align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
336 <b
><?php
echo $strCriteria; ?
>: 
;</b
>
340 for ($x = 0; $x < $col; $x++
) {
341 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
343 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
344 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
351 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
354 if (isset($Criteria[$x])) {
355 $stripped_Criteria = $Criteria[$x];
357 if ((empty($prev_Criteria) ||
!isset($prev_Criteria[$x]))
358 ||
urldecode($prev_Criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
359 $curCriteria[$z] = $stripped_Criteria;
360 $encoded_Criteria = urlencode($stripped_Criteria);
362 $curCriteria[$z] = urldecode($prev_Criteria[$x]);
363 $encoded_Criteria = $prev_Criteria[$x];
366 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
367 <input type
="hidden" name
="prev_Criteria[<?php echo $z; ?>]" value
="<?php echo $encoded_Criteria; ?>" />
368 <input type
="text" name
="Criteria[<?php echo $z; ?>]" value
="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
377 <!-- And/Or columns
and rows
-->
380 for ($y = 0; $y <= $row; $y++
) {
381 $bgcolor = ($y %
2) ?
$cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
382 if (isset($InsRow[$y]) && $InsRow[$y] == 'on') {
383 $chk['or'] = ' checked="checked"';
387 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $bgcolor; ?>" nowrap
="nowrap">
388 <!-- Row controls
-->
389 <table bgcolor
="<?php echo $bgcolor; ?>" cellpadding
="0" cellspacing
="0" border
="0">
391 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
392 <small
><?php
echo $strQBEIns; ?
>:</small
>
393 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
395 <td align
="<?php echo $cell_align_right; ?>">
396 <b
><?php
echo $strAnd; ?
>:</b
>
399 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
404 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
405 <small
><?php
echo $strQBEDel; ?
>:</small
>
406 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
408 <td align
="<?php echo $cell_align_right; ?>">
409 <b
><?php
echo $strOr; ?
>:</b
>
412 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
420 for ($x = 0; $x < $col; $x++
) {
421 if (isset($InsCol[$x]) && $InsCol[$x] == 'on') {
423 $or = 'Or' . $w . '[' . $z . ']';
425 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
426 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
431 if (isset($DelCol[$x]) && $DelCol[$x] == 'on') {
436 $or = 'Or' . $w . '[' . $z . ']';
438 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
439 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
451 if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
455 if (isset($AndOrRow[$y])) {
456 $curAndOrRow[$w] = $AndOrRow[$y];
458 if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
459 $chk['and'] = ' checked="checked"';
462 $chk['or'] = ' checked="checked"';
468 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $bgcolor; ?>" nowrap
="nowrap">
469 <!-- Row controls
-->
470 <table bgcolor
="<?php echo $bgcolor; ?>" border
="0" cellpadding
="0" cellspacing
="0">
472 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
473 <small
><?php
echo $strQBEIns; ?
>:</small
>
474 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
476 <td align
="<?php echo $cell_align_right; ?>">
477 <b
><?php
echo $strAnd; ?
>:</b
>
480 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
484 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
485 <small
><?php
echo $strQBEDel; ?
>:</small
>
486 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
488 <td align
="<?php echo $cell_align_right; ?>">
489 <b
><?php
echo $strOr; ?
>:</b
>
492 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
499 for ($x = 0; $x < $col; $x++
) {
500 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
502 $or = 'Or' . $w . '[' . $z . ']';
504 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
505 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
510 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
516 if (!isset($
{$or})) {
519 if (!empty($
{$or}) && isset($
{$or}[$x])) {
520 $stripped_or = $
{$or}[$x];
525 <td align
="center" bgcolor
="<?php echo $bgcolor; ?>">
526 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="Or<?php echo $w . '[' . $z . ']'; ?>" dir
="<?php echo $text_dir; ?>"><?php
echo htmlspecialchars($stripped_or); ?
></textarea
>
529 if (!empty($
{$or}) && isset($
{$or}[$x])) {
530 $
{'cur' . $or}[$z] = $
{$or}[$x];
543 <!-- Modify columns
-->
545 <td
class="tblHeaders" align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
546 <b
><?php
echo $strModify; ?
>: 
;</b
>
550 for ($x = 0; $x < $col; $x++
) {
551 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
552 $curAndOrCol[$z] = $AndOrCol[$y];
553 if ($AndOrCol[$z] == 'or') {
554 $chk['or'] = ' checked="checked"';
557 $chk['and'] = ' checked="checked"';
561 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
562 <b
><?php
echo $strOr; ?
>:</b
>
563 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
564  
; 
;<b
><?php
echo $strAnd; ?
>:</b
>
565 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
567 <?php
echo $strQBEIns . "\n"; ?
>
568 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
569  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
570 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
577 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
581 if (isset($AndOrCol[$y])) {
582 $curAndOrCol[$z] = $AndOrCol[$y];
584 if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
585 $chk['or'] = ' checked="checked"';
588 $chk['and'] = ' checked="checked"';
592 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
593 <b
><?php
echo $strOr; ?
>:</b
>
594 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
595  
; 
;<b
><?php
echo $strAnd; ?
>:</b
>
596 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
598 <?php
echo $strQBEIns . "\n"; ?
>
599 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
600  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
601 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
611 <!-- Other controls
-->
612 <?php
echo PMA_generate_common_hidden_inputs(); ?
>
613 <table border
="0" cellpadding
="2" cellspacing
="1">
615 <td nowrap
="nowrap"><input type
="hidden" value
="<?php echo htmlspecialchars($db); ?>" name
="db" />
616 <input type
="hidden" value
="<?php echo $z; ?>" name
="Columns" />
620 <input type
="hidden" value
="<?php echo $w; ?>" name
="Rows" />
621 <?php
echo $strAddDeleteRow; ?
>:
622 <select size
="1" name
="Add_Row" style
="vertical-align: middle">
623 <option value
="-3">-3</option
>
624 <option value
="-2">-2</option
>
625 <option value
="-1">-1</option
>
626 <option value
="0" selected
="selected">0</option
>
627 <option value
="1">1</option
>
628 <option value
="2">2</option
>
629 <option value
="3">3</option
>
632 <td width
="10"> 
;</td
>
633 <td nowrap
="nowrap"><?php
echo $strAddDeleteColumn; ?
>:
634 <select size
="1" name
="Add_Col" style
="vertical-align: middle">
635 <option value
="-3">-3</option
>
636 <option value
="-2">-2</option
>
637 <option value
="-1">-1</option
>
638 <option value
="0" selected
="selected">0</option
>
639 <option value
="1">1</option
>
640 <option value
="2">2</option
>
641 <option value
="3">3</option
>
644 <td width
="10"> 
;</td
>
645 <!-- Generates a query
-->
646 <td
><input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" /></td
>
650 <table border
="0" cellpadding
="2" cellspacing
="1">
652 <td
class="tblHeaders"> 
;<?php
echo $strUseTables; ?
>: 
;</td
>
653 <td width
="20"> 
;</td
>
654 <td
class="tblHeaders"> 
;<?php
echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?
> 
;</td
>
657 <td bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
659 $strTableListOptions = '';
660 $numTableListOptions = 0;
661 foreach($tbl_names AS $key => $val) {
662 $strTableListOptions .= ' ';
663 $strTableListOptions .= '<option value="' . htmlspecialchars($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
664 $numTableListOptions++
;
667 <select name
="TableList[]" size
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>" multiple
="multiple" id
="listTable">
668 <?php
echo $strTableListOptions; ?
>
671 <td
> 
; 
;</td
>
672 <!-- Displays the current query
-->
673 <td bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
674 <textarea cols
="30" rows
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>" name
="sql_query" dir
="<?php echo $text_dir; ?>" id
="textSqlquery">
679 if (!isset($qry_select)) {
682 for ($x = 0; $x < $col; $x++
) {
683 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
687 $qry_select .= $curField[$x];
691 if (!empty($qry_select)) {
692 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
693 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
698 // Create LEFT JOINS out of Relations
699 // Code originally by Mike Beck <mike.beck@ibmiller.de>
700 // If we can use Relations we could make some left joins.
701 // First find out if relations are available in this database.
703 // First we need the really needed Tables - those in TableList might still be
705 if (isset($Field) && count($Field) > 0) {
707 // Initialize some variables
713 $col_where = array();
716 // We only start this if we have fields, otherwise it would be dumb
717 foreach ($Field AS $value) {
718 $parts = explode('.', $value);
719 if (!empty($parts[0]) && !empty($parts[1])) {
720 $tab_raw = urldecode($parts[0]);
721 $tab = str_replace('`', '', $tab_raw);
722 $tab_all[$tab] = $tab;
724 $col_raw = urldecode($parts[1]);
725 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
729 // Check 'where' clauses
730 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
731 // Now we need all tables that we have in the where clause
732 $crit_cnt = count($Criteria);
733 for ($x = 0; $x < $crit_cnt; $x++
) {
734 $curr_tab = explode('.', urldecode($Field[$x]));
735 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
736 $tab_raw = urldecode($curr_tab[0]);
737 $tab = str_replace('`', '', $tab_raw);
739 $col_raw = urldecode($curr_tab[1]);
740 $col1 = str_replace('`', '', $col_raw);
741 $col1 = $tab . '.' . $col1;
742 // Now we know that our array has the same numbers as $Criteria
743 // we can check which of our columns has a where clause
744 if (!empty($Criteria[$x])) {
745 if (substr($Criteria[$x], 0, 1) == '=' ||
stristr($Criteria[$x], 'is')) {
746 $col_where[$col] = $col1;
747 $tab_wher[$tab] = $tab;
753 // Cleans temp vars w/o further use
758 if (count($tab_wher) == 1) {
759 // If there is exactly one column that has a decent where-clause
760 // we will just use this
761 $master = key($tab_wher);
763 // Now let's find out which of the tables has an index
764 // ( When the control user is the same as the normal user
765 // because he is using one of his databases as pmadb,
766 // the last db selected is not always the one where we need to work)
767 PMA_DBI_select_db($db);
769 foreach ($tab_all AS $tab) {
770 $ind_rs = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tab) . ';');
771 while ($ind = PMA_DBI_fetch_assoc($ind_rs)) {
772 $col1 = $tab . '.' . $ind['Column_name'];
773 if (isset($col_all[$col1])) {
774 if ($ind['non_unique'] == 0) {
775 if (isset($col_where[$col1])) {
776 $col_unique[$col1] = 'Y';
778 $col_unique[$col1] = 'N';
781 if (isset($col_where[$col1])) {
782 $col_index[$col1] = 'Y';
784 $col_index[$col1] = 'N';
788 } // end while (each col of tab)
789 } // end while (each tab)
790 // now we want to find the best.
791 if (isset($col_unique) && count($col_unique) > 0) {
792 $col_cand = $col_unique;
794 } else if (isset($col_index) && count($col_index) > 0) {
795 $col_cand = $col_index;
797 } else if (isset($col_where) && count($col_where) > 0) {
798 $col_cand = $tab_wher;
801 $col_cand = $tab_all;
805 // If we came up with $col_unique (very good) or $col_index (still
806 // good) as $col_cand we want to check if we have any 'Y' there
807 // (that would mean that they were also found in the whereclauses
808 // which would be great). if yes, we take only those
809 if ($needsort == 1) {
810 foreach ($col_cand AS $col => $is_where) {
811 $tab = explode('.', $col);
813 if ($is_where == 'Y') {
821 // Candidates restricted in index+where
824 // None of the candidates where in a where-clause
828 // If our array of candidates has more than one member we'll just
829 // find the smallest table.
830 // Of course the actual query would be faster if we check for
831 // the Criteria which gives the smallest result set in its table,
832 // but it would take too much time to check this
833 if (count($col_cand) > 1) {
834 // Of course we only want to check each table once
835 $checked_tables = $col_cand;
836 foreach ($col_cand AS $tab) {
837 if ($checked_tables[$tab] != 1 ) {
838 $rows_qry = 'SELECT COUNT(1) AS anz '
839 . 'FROM ' . PMA_backquote($tab);
840 $rows_rs = PMA_DBI_query($rows_qry);
841 while ($res = PMA_DBI_fetch_assoc($rows_rs)) {
842 $tsize[$tab] = $res['anz'];
844 PMA_DBI_free_result($rows_rs);
846 $checked_tables[$tab] = 1;
848 $csize[$tab] = $tsize[$tab];
852 $master = key($csize); // Smallest
855 $master = current($col_cand); // Only one single candidate
857 } // end if (exactly one where clause)
860 * Removes unwanted entries from an array (PHP3 compliant)
862 * @param array the array to work with
863 * @param array the list of keys to remove
865 * @return array the cleaned up array
869 function PMA_arrayShort($array, $key)
871 foreach ($array AS $k => $v) {
881 } // end of the "PMA_arrayShort()" function
885 * Finds all related tables
887 * @param string wether to go from master to foreign or vice versa
889 * @return boolean always TRUE
891 * @global array the list of tables that we still couldn't connect
892 * @global array the list of allready connected tables
893 * @global string the current databse name
894 * @global string the super user connection id
895 * @global array the list of relation settings
899 function PMA_getRelatives($from) {
900 global $tab_left, $tab_know, $fromclause;
901 global $dbh, $db, $cfgRelation;
903 if ($from == 'master') {
908 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
909 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
911 $rel_query = 'SELECT *'
912 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
913 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
914 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
915 . ' AND ' . $from . '_table IN ' . $in_know
916 . ' AND ' . $to . '_table IN ' . $in_left;
917 PMA_DBI_select_db($cfgRelation['db'], $dbh);
918 $relations = @PMA_DBI_query
($rel_query, $dbh);
919 PMA_DBI_select_db($db, $dbh);
920 while ($row = PMA_DBI_fetch_assoc($relations)) {
921 $found_table = $row[$to . '_table'];
922 if (isset($tab_left[$found_table])) {
923 $fromclause .= "\n" . ' LEFT JOIN '
924 . PMA_backquote($row[$to . '_table']) . ' ON '
925 . PMA_backquote($row[$from . '_table']) . '.'
926 . PMA_backquote($row[$from . '_field']) . ' = '
927 . PMA_backquote($row[$to . '_table']) . '.'
928 . PMA_backquote($row[$to . '_field']) . ' ';
929 $tab_know[$found_table] = $found_table;
930 $tab_left = PMA_arrayShort($tab_left, $found_table);
935 } // end of the "PMA_getRelatives()" function
938 $tab_left = PMA_arrayShort($tab_all, $master);
939 $tab_know[$master] = $master;
943 while (count($tab_left) > 0) {
945 PMA_getRelatives('master');
947 PMA_getRelatives('foreign');
952 foreach ($tab_left AS $tab) {
953 $emerg .= ', ' . PMA_backquote($tab);
954 $tab_left = PMA_arrayShort($tab_left, $tab);
958 $qry_from = PMA_backquote($master) . $emerg . $fromclause;
959 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
961 } // end count($Field) > 0
963 // In case relations are not defined, just generate the FROM clause
964 // from the list of tables, however we don't generate any JOIN
966 if (empty($qry_from) && isset($tab_all)) {
967 $qry_from = implode(', ', $tab_all);
969 // Now let's see what we got
970 if (!empty($qry_from)) {
971 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
972 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
978 for ($x = 0; $x < $col; $x++
) {
979 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where) && isset($curAndOrCol)) {
980 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
982 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
983 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
988 if ($criteria_cnt > 1) {
989 $qry_where = '(' . $qry_where . ')';
991 // OR rows ${'cur' . $or}[$x]
992 if (!isset($curAndOrRow)) {
993 $curAndOrRow = array();
995 for ($y = 0; $y <= $row; $y++
) {
999 for ($x = 0; $x < $col; $x++
) {
1000 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x]) && $x) {
1001 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
1003 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x])) {
1004 $qry_orwhere .= '(' . $curField[$x]
1006 . $
{'curOr' . $y}[$x]
1012 if ($criteria_cnt > 1) {
1013 $qry_orwhere = '(' . $qry_orwhere . ')';
1015 if (!empty($qry_orwhere)) {
1017 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
1022 if (!empty($qry_where) && $qry_where != '()') {
1023 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
1024 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1029 if (!isset($qry_orderby)) {
1032 for ($x = 0; $x < $col; $x++
) {
1033 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1034 $qry_orderby .= ', ';
1036 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1037 // if they have chosen all fields using the * selector,
1038 // then sorting is not available
1039 // Robbat2 - Fix for Bug #570698
1040 if (substr($curField[$x], -2) != '.*') {
1041 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1046 if (!empty($qry_orderby)) {
1047 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1048 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1052 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
1056 <!-- Generates a query
-->
1057 <td align
="right" class="tblHeaders"><input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" /></td
>
1059 <!-- Execute a query
-->
1060 <td align
="right" class="tblHeaders"><input type
="submit" name
="submit_sql" value
="<?php echo $strRunQuery; ?>" /></td
>
1066 * Displays the footer
1068 require_once('./footer.inc.php');