3 // vim: expandtab sw=4 ts=4 sts=4:
6 * query by example the whole database
12 require_once './libraries/common.lib.php';
13 require_once './libraries/Table.class.php';
14 require_once './libraries/relation.lib.php';
18 * Gets the relation settings
20 $cfgRelation = PMA_getRelationsParam();
24 * A query has been submitted -> execute it, else display the headers
26 if (isset($_REQUEST['submit_sql'])
27 && preg_match('@^SELECT@i', $_REQUEST['encoded_sql_query'])) {
28 $goto = 'db_details.php';
29 $zero_rows = htmlspecialchars($GLOBALS['strSuccess']);
30 $sql_query = urldecode($_REQUEST['encoded_sql_query']);
35 require './libraries/db_details_common.inc.php';
36 $url_query .= '&goto=db_details_qbe.php';
37 $url_params['goto'] = 'db_details_qbe.php';
38 require './libraries/db_details_db_info.inc.php';
41 if (isset($_REQUEST['submit_sql'])
42 && ! preg_match('@^SELECT@i', $_REQUEST['encoded_sql_query'])) {
43 echo '<div class="warning">' . $GLOBALS['strHaveToShow'] . '</div>';
48 * Initialize some variables
50 $col_cnt = isset($_REQUEST['col_cnt']) ?
(int) $_REQUEST['col_cnt'] : 3;
51 $add_col = isset($_REQUEST['add_col']) ?
(int) $_REQUEST['add_col'] : 0;
52 $add_row = isset($_REQUEST['add_row']) ?
(int) $_REQUEST['add_row'] : 0;
54 $rows = isset($_REQUEST['rows']) ?
(int) $_REQUEST['rows'] : 0;
55 $ins_col = isset($_REQUEST['ins_col']) ?
$_REQUEST['ins_col'] : array();
56 $del_col = isset($_REQUEST['del_col']) ?
$_REQUEST['del_col'] : array();
58 $prev_criteria = isset($_REQUEST['prev_criteria'])
59 ?
$_REQUEST['prev_criteria']
61 $criteria = isset($_REQUEST['criteria'])
62 ?
$_REQUEST['criteria']
63 : array_fill(0, $col_cnt, '');
65 $ins_row = isset($_REQUEST['ins_row'])
66 ?
$_REQUEST['ins_row']
67 : array_fill(0, $col_cnt, '');
68 $del_row = isset($_REQUEST['del_row'])
69 ?
$_REQUEST['del_row']
70 : array_fill(0, $col_cnt, '');
71 $and_or_row = isset($_REQUEST['and_or_row'])
72 ?
$_REQUEST['and_or_row']
73 : array_fill(0, $col_cnt, '');
74 $and_or_col = isset($_REQUEST['and_or_col'])
75 ?
$_REQUEST['and_or_col']
76 : array_fill(0, $col_cnt, '');
79 $form_column_width = 12;
80 $col = max($col_cnt +
$add_col, 0);
81 $row = max($rows +
$add_row, 0);
84 // The tables list sent by a previously submitted form
85 if (!empty($TableList)) {
86 $cnt_table_list = count($TableList);
87 for ($x = 0; $x < $cnt_table_list; $x++
) {
88 $tbl_names[urldecode($TableList[$x])] = ' selected="selected"';
93 $columns = PMA_DBI_get_columns_full($GLOBALS['db']);
94 $tables = PMA_DBI_get_columns_full($GLOBALS['db']);
100 $tbl_result = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE
);
101 $tbl_result_cnt = PMA_DBI_num_rows($tbl_result);
105 // The tables list gets from MySQL
106 while ($i < $tbl_result_cnt) {
107 list($tbl) = PMA_DBI_fetch_row($tbl_result);
108 $fld_results = PMA_DBI_get_fields($db, $tbl);
109 $fld_results_cnt = ($fld_results) ?
count($fld_results) : 0;
112 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
113 $tbl_names[$tbl] = '';
115 $tbl_names[$tbl] = ' selected="selected"';
118 // The fields list per selected tables
119 if ($tbl_names[$tbl] == ' selected="selected"') {
120 $fld[$k++
] = PMA_backquote($tbl) . '.*';
121 while ($j < $fld_results_cnt) {
122 $fld[$k] = PMA_convert_display_charset($fld_results[$j]['Field']);
123 $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
125 // increase the width if necessary
126 if (strlen($fld[$k]) > $form_column_width) {
127 $form_column_width = strlen($fld[$k]);
137 PMA_DBI_free_result($tbl_result);
139 // largest width found
140 $realwidth = $form_column_width . 'ex';
144 * Displays the Query by example form
147 function showColumnSelectCell($columns, $column_number, $selected = '')
151 <select name
="Field[<?php echo $column_number; ?>]" size
="1">
152 <option value
=""></option
>
154 foreach ($columns as $column) {
155 if ($column === $selected) {
156 $sel = ' selected="selected"';
161 echo '<option value="' . htmlspecialchars($column) . '"' . $sel . '>'
162 . htmlspecialchars($column) . '</option>' . "\n";
172 <form action
="db_details_qbe.php" method
="post">
173 <table
class="data" style
="width: 100%;">
174 <tr
class="odd noclick">
175 <th
><?php
echo $strField; ?
>:</th
>
178 for ($x = 0; $x < $col; $x++
) {
179 if (isset($ins_col[$x]) && $ins_col[$x] == 'on') {
180 showColumnSelectCell( $fld, $z );
184 if (! empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
189 if (isset($Field[$x])) {
190 $selected = urldecode($Field[$x]);
191 $curField[$z] = urldecode($Field[$x]);
193 showColumnSelectCell($fld, $z, $selected);
200 <tr
class="even noclick">
201 <th
><?php
echo $strSort; ?
>:</th
>
204 for ($x = 0; $x < $col; $x++
) {
205 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
208 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
209 <option value
=""></option
>
210 <option value
="ASC"><?php
echo $strAscending; ?
></option
>
211 <option value
="DESC"><?php
echo $strDescending; ?
></option
>
219 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
224 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
225 <option value
=""></option
>
229 // If they have chosen all fields using the * selector,
230 // then sorting is not available
231 // Robbat2 - Fix for Bug #570698
232 if (isset($Sort[$x]) && isset($Field[$x])
233 && substr(urldecode($Field[$x]), -2) == '.*') {
237 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
238 $curSort[$z] = $Sort[$x];
239 $sel = ' selected="selected"';
244 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
245 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
246 $curSort[$z] = $Sort[$x];
247 $sel = ' selected="selected"';
252 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
264 <tr
class="odd noclick">
265 <th
><?php
echo $strShow; ?
>:</th
>
268 for ($x = 0; $x < $col; $x++
) {
269 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
272 <input type
="checkbox" name
="Show[<?php echo $z; ?>]" />
279 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
282 if (isset($Show[$x])) {
283 $checked = ' checked="checked"';
284 $curShow[$z] = $Show[$x];
290 <input type
="checkbox" name
="Show[<?php echo $z; ?>]"<?php
echo $checked; ?
> />
299 <!-- Criteria row
-->
300 <tr
class="even noclick">
301 <th
><?php
echo $strCriteria; ?
>:</th
>
304 for ($x = 0; $x < $col; $x++
) {
305 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
308 <input type
="text" name
="criteria[<?php echo $z; ?>]" value
="" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
315 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
318 if (isset($criteria[$x])) {
319 $stripped_Criteria = $criteria[$x];
321 if ((empty($prev_criteria) ||
!isset($prev_criteria[$x]))
322 ||
urldecode($prev_criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
323 $curCriteria[$z] = $stripped_Criteria;
324 $encoded_Criteria = urlencode($stripped_Criteria);
326 $curCriteria[$z] = urldecode($prev_criteria[$x]);
327 $encoded_Criteria = $prev_criteria[$x];
331 <input type
="hidden" name
="prev_criteria[<?php echo $z; ?>]" value
="<?php echo $encoded_Criteria; ?>" />
332 <input type
="text" name
="criteria[<?php echo $z; ?>]" value
="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
341 <!-- And/Or columns
and rows
-->
345 for ($y = 0; $y <= $row; $y++
) {
346 if (isset($ins_row[$y]) && $ins_row[$y] == 'on') {
347 $chk['or'] = ' checked="checked"';
350 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
351 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
352 <!-- Row controls
-->
353 <table cellpadding
="0" cellspacing
="0" border
="0">
355 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
356 <small
><?php
echo $strQBEIns; ?
>:</small
>
357 <input type
="checkbox" name
="ins_row[<?php echo $w; ?>]" />
359 <td align
="<?php echo $cell_align_right; ?>">
360 <b
><?php
echo $strAnd; ?
>:</b
>
363 <input type
="radio" name
="and_or_row[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
368 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
369 <small
><?php
echo $strQBEDel; ?
>:</small
>
370 <input type
="checkbox" name
="del_row[<?php echo $w; ?>]" />
372 <td align
="<?php echo $cell_align_right; ?>">
373 <b
><?php
echo $strOr; ?
>:</b
>
376 <input type
="radio" name
="and_or_row[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
384 for ($x = 0; $x < $col; $x++
) {
385 if (isset($ins_col[$x]) && $ins_col[$x] == 'on') {
387 $or = 'Or' . $w . '[' . $z . ']';
390 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
395 if (isset($del_col[$x]) && $del_col[$x] == 'on') {
400 $or = 'Or' . $w . '[' . $z . ']';
403 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
413 $odd_row =! $odd_row;
416 if (isset($del_row[$y]) && $del_row[$y] == 'on') {
420 if (isset($and_or_row[$y])) {
421 $curAndOrRow[$w] = $and_or_row[$y];
423 if (isset($and_or_row[$y]) && $and_or_row[$y] == 'and') {
424 $chk['and'] = ' checked="checked"';
427 $chk['or'] = ' checked="checked"';
432 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
433 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
434 <!-- Row controls
-->
435 <table border
="0" cellpadding
="0" cellspacing
="0">
437 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
438 <small
><?php
echo $strQBEIns; ?
>:</small
>
439 <input type
="checkbox" name
="ins_row[<?php echo $w; ?>]" />
441 <td align
="<?php echo $cell_align_right; ?>">
442 <b
><?php
echo $strAnd; ?
>:</b
>
445 <input type
="radio" name
="and_or_row[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
449 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
450 <small
><?php
echo $strQBEDel; ?
>:</small
>
451 <input type
="checkbox" name
="del_row[<?php echo $w; ?>]" />
453 <td align
="<?php echo $cell_align_right; ?>">
454 <b
><?php
echo $strOr; ?
>:</b
>
457 <input type
="radio" name
="and_or_row[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
464 for ($x = 0; $x < $col; $x++
) {
465 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
467 $or = 'Or' . $w . '[' . $z . ']';
470 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
475 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
481 if (!isset($
{$or})) {
484 if (!empty($
{$or}) && isset($
{$or}[$x])) {
485 $stripped_or = $
{$or}[$x];
491 <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
>
494 if (!empty($
{$or}) && isset($
{$or}[$x])) {
495 $
{'cur' . $or}[$z] = $
{$or}[$x];
505 $odd_row =! $odd_row;
508 <!-- Modify columns
-->
509 <tr
class="even noclick">
510 <th
><?php
echo $strModify; ?
>:</th
>
513 for ($x = 0; $x < $col; $x++
) {
514 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
515 $curAndOrCol[$z] = $and_or_col[$y];
516 if ($and_or_col[$z] == 'or') {
517 $chk['or'] = ' checked="checked"';
520 $chk['and'] = ' checked="checked"';
525 <b
><?php
echo $strOr; ?
>:</b
>
526 <input type
="radio" name
="and_or_col[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
527  
; 
;<b
><?php
echo $strAnd; ?
>:</b
>
528 <input type
="radio" name
="and_or_col[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
530 <?php
echo $strQBEIns . "\n"; ?
>
531 <input type
="checkbox" name
="ins_col[<?php echo $z; ?>]" />
532  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
533 <input type
="checkbox" name
="del_col[<?php echo $z; ?>]" />
540 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
544 if (isset($and_or_col[$y])) {
545 $curAndOrCol[$z] = $and_or_col[$y];
547 if (isset($and_or_col[$z]) && $and_or_col[$z] == 'or') {
548 $chk['or'] = ' checked="checked"';
551 $chk['and'] = ' checked="checked"';
556 <b
><?php
echo $strOr; ?
>:</b
>
557 <input type
="radio" name
="and_or_col[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
558  
; 
;<b
><?php
echo $strAnd; ?
>:</b
>
559 <input type
="radio" name
="and_or_col[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
561 <?php
echo $strQBEIns . "\n"; ?
>
562 <input type
="checkbox" name
="ins_col[<?php echo $z; ?>]" />
563  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
564 <input type
="checkbox" name
="del_col[<?php echo $z; ?>]" />
574 <!-- Other controls
-->
577 $url_params['db'] = $db;
578 $url_params['col_cnt'] = $z;
579 $url_params['rows'] = $w;
580 echo PMA_generate_common_hidden_inputs($url_params);
582 <fieldset
class="tblFooters">
583 <table border
="0" cellpadding
="2" cellspacing
="1">
586 <?php
echo $strAddDeleteRow; ?
>:
587 <select size
="1" name
="add_row" style
="vertical-align: middle">
588 <option value
="-3">-3</option
>
589 <option value
="-2">-2</option
>
590 <option value
="-1">-1</option
>
591 <option value
="0" selected
="selected">0</option
>
592 <option value
="1">1</option
>
593 <option value
="2">2</option
>
594 <option value
="3">3</option
>
597 <td width
="10"> 
;</td
>
598 <td nowrap
="nowrap"><?php
echo $strAddDeleteColumn; ?
>:
599 <select size
="1" name
="add_col" style
="vertical-align: middle">
600 <option value
="-3">-3</option
>
601 <option value
="-2">-2</option
>
602 <option value
="-1">-1</option
>
603 <option value
="0" selected
="selected">0</option
>
604 <option value
="1">1</option
>
605 <option value
="2">2</option
>
606 <option value
="3">3</option
>
609 <td width
="10"> 
;</td
>
610 <!-- Generates a query
-->
611 <td
><input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" /></td
>
619 <legend
><?php
echo $strUseTables; ?
></legend
>
621 $strTableListOptions = '';
622 $numTableListOptions = 0;
623 foreach ($tbl_names AS $key => $val) {
624 $strTableListOptions .= ' ';
625 $strTableListOptions .= '<option value="' . htmlspecialchars($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
626 $numTableListOptions++
;
629 <select name
="TableList[]" multiple
="multiple" id
="listTable"
630 size
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>">
631 <?php
echo $strTableListOptions; ?
>
634 <fieldset
class="tblFooters">
635 <input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" />
638 <td width
="20"> 
;</td
>
641 <legend
><?php
echo sprintf($strQueryOnDb, PMA_getDbLink($db)); ?
>
643 <textarea cols
="30" name
="sql_query" id
="textSqlquery"
644 rows
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>"
645 dir
="<?php echo $text_dir; ?>">
650 if (!isset($qry_select)) {
653 for ($x = 0; $x < $col; $x++
) {
654 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
658 $qry_select .= $curField[$x];
662 if (!empty($qry_select)) {
663 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
664 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
669 // Create LEFT JOINS out of Relations
670 // Code originally by Mike Beck <mike.beck@ibmiller.de>
671 // If we can use Relations we could make some left joins.
672 // First find out if relations are available in this database.
674 // First we need the really needed Tables - those in TableList might still be
676 if (isset($Field) && count($Field) > 0) {
678 // Initialize some variables
684 $col_where = array();
687 // We only start this if we have fields, otherwise it would be dumb
688 foreach ($Field AS $value) {
689 $parts = explode('.', $value);
690 if (!empty($parts[0]) && !empty($parts[1])) {
691 $tab_raw = urldecode($parts[0]);
692 $tab = str_replace('`', '', $tab_raw);
693 $tab_all[$tab] = $tab;
695 $col_raw = urldecode($parts[1]);
696 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
700 // Check 'where' clauses
701 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
702 // Now we need all tables that we have in the where clause
703 $crit_cnt = count($criteria);
704 for ($x = 0; $x < $crit_cnt; $x++
) {
705 $curr_tab = explode('.', urldecode($Field[$x]));
706 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
707 $tab_raw = urldecode($curr_tab[0]);
708 $tab = str_replace('`', '', $tab_raw);
710 $col_raw = urldecode($curr_tab[1]);
711 $col1 = str_replace('`', '', $col_raw);
712 $col1 = $tab . '.' . $col1;
713 // Now we know that our array has the same numbers as $criteria
714 // we can check which of our columns has a where clause
715 if (!empty($criteria[$x])) {
716 if (substr($criteria[$x], 0, 1) == '=' ||
stristr($criteria[$x], 'is')) {
717 $col_where[$col] = $col1;
718 $tab_wher[$tab] = $tab;
724 // Cleans temp vars w/o further use
729 if (count($tab_wher) == 1) {
730 // If there is exactly one column that has a decent where-clause
731 // we will just use this
732 $master = key($tab_wher);
734 // Now let's find out which of the tables has an index
735 // ( When the control user is the same as the normal user
736 // because he is using one of his databases as pmadb,
737 // the last db selected is not always the one where we need to work)
738 PMA_DBI_select_db($db);
740 foreach ($tab_all AS $tab) {
741 $ind_rs = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tab) . ';');
742 while ($ind = PMA_DBI_fetch_assoc($ind_rs)) {
743 $col1 = $tab . '.' . $ind['Column_name'];
744 if (isset($col_all[$col1])) {
745 if ($ind['non_unique'] == 0) {
746 if (isset($col_where[$col1])) {
747 $col_unique[$col1] = 'Y';
749 $col_unique[$col1] = 'N';
752 if (isset($col_where[$col1])) {
753 $col_index[$col1] = 'Y';
755 $col_index[$col1] = 'N';
759 } // end while (each col of tab)
760 } // end while (each tab)
761 // now we want to find the best.
762 if (isset($col_unique) && count($col_unique) > 0) {
763 $col_cand = $col_unique;
765 } elseif (isset($col_index) && count($col_index) > 0) {
766 $col_cand = $col_index;
768 } elseif (isset($col_where) && count($col_where) > 0) {
769 $col_cand = $tab_wher;
772 $col_cand = $tab_all;
776 // If we came up with $col_unique (very good) or $col_index (still
777 // good) as $col_cand we want to check if we have any 'Y' there
778 // (that would mean that they were also found in the whereclauses
779 // which would be great). if yes, we take only those
780 if ($needsort == 1) {
781 foreach ($col_cand AS $col => $is_where) {
782 $tab = explode('.', $col);
784 if ($is_where == 'Y') {
792 // Candidates restricted in index+where
795 // None of the candidates where in a where-clause
799 // If our array of candidates has more than one member we'll just
800 // find the smallest table.
801 // Of course the actual query would be faster if we check for
802 // the Criteria which gives the smallest result set in its table,
803 // but it would take too much time to check this
804 if (count($col_cand) > 1) {
805 // Of course we only want to check each table once
806 $checked_tables = $col_cand;
807 foreach ($col_cand AS $tab) {
808 if ($checked_tables[$tab] != 1 ) {
809 $tsize[$tab] = PMA_Table
::countRecords($db, $tab, true, false);
810 $checked_tables[$tab] = 1;
812 $csize[$tab] = $tsize[$tab];
816 $master = key($csize); // Smallest
819 $master = current($col_cand); // Only one single candidate
821 } // end if (exactly one where clause)
824 * Removes unwanted entries from an array (PHP3 compliant)
826 * @param array the array to work with
827 * @param array the list of keys to remove
829 * @return array the cleaned up array
833 function PMA_arrayShort($array, $key)
835 foreach ($array AS $k => $v) {
845 } // end of the "PMA_arrayShort()" function
849 * Finds all related tables
851 * @param string wether to go from master to foreign or vice versa
853 * @return boolean always TRUE
855 * @global array the list of tables that we still couldn't connect
856 * @global array the list of allready connected tables
857 * @global string the current databse name
858 * @global string the super user connection id
859 * @global array the list of relation settings
863 function PMA_getRelatives($from) {
864 global $tab_left, $tab_know, $fromclause;
865 global $controllink, $db, $cfgRelation;
867 if ($from == 'master') {
872 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
873 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
875 $rel_query = 'SELECT *'
876 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
877 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
878 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
879 . ' AND ' . $from . '_table IN ' . $in_know
880 . ' AND ' . $to . '_table IN ' . $in_left;
881 PMA_DBI_select_db($cfgRelation['db'], $controllink);
882 $relations = @PMA_DBI_query
($rel_query, $controllink);
883 PMA_DBI_select_db($db, $controllink);
884 while ($row = PMA_DBI_fetch_assoc($relations)) {
885 $found_table = $row[$to . '_table'];
886 if (isset($tab_left[$found_table])) {
887 $fromclause .= "\n" . ' LEFT JOIN '
888 . PMA_backquote($row[$to . '_table']) . ' ON '
889 . PMA_backquote($row[$from . '_table']) . '.'
890 . PMA_backquote($row[$from . '_field']) . ' = '
891 . PMA_backquote($row[$to . '_table']) . '.'
892 . PMA_backquote($row[$to . '_field']) . ' ';
893 $tab_know[$found_table] = $found_table;
894 $tab_left = PMA_arrayShort($tab_left, $found_table);
899 } // end of the "PMA_getRelatives()" function
902 $tab_left = PMA_arrayShort($tab_all, $master);
903 $tab_know[$master] = $master;
907 while (count($tab_left) > 0) {
909 PMA_getRelatives('master');
911 PMA_getRelatives('foreign');
916 foreach ($tab_left AS $tab) {
917 $emerg .= ', ' . PMA_backquote($tab);
918 $tab_left = PMA_arrayShort($tab_left, $tab);
922 $qry_from = PMA_backquote($master) . $emerg . $fromclause;
923 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
925 } // end count($Field) > 0
927 // In case relations are not defined, just generate the FROM clause
928 // from the list of tables, however we don't generate any JOIN
930 if (empty($qry_from) && isset($tab_all)) {
931 $qry_from = implode(', ', $tab_all);
933 // Now let's see what we got
934 if (!empty($qry_from)) {
935 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
936 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
942 for ($x = 0; $x < $col; $x++
) {
943 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where) && isset($curAndOrCol)) {
944 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
946 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
947 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
952 if ($criteria_cnt > 1) {
953 $qry_where = '(' . $qry_where . ')';
955 // OR rows ${'cur' . $or}[$x]
956 if (!isset($curAndOrRow)) {
957 $curAndOrRow = array();
959 for ($y = 0; $y <= $row; $y++
) {
963 for ($x = 0; $x < $col; $x++
) {
964 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x]) && $x) {
965 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
967 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x])) {
968 $qry_orwhere .= '(' . $curField[$x]
970 . $
{'curOr' . $y}[$x]
976 if ($criteria_cnt > 1) {
977 $qry_orwhere = '(' . $qry_orwhere . ')';
979 if (!empty($qry_orwhere)) {
981 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
986 if (!empty($qry_where) && $qry_where != '()') {
987 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
988 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
993 if (!isset($qry_orderby)) {
996 for ($x = 0; $x < $col; $x++
) {
997 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
998 $qry_orderby .= ', ';
1000 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1001 // if they have chosen all fields using the * selector,
1002 // then sorting is not available
1003 // Robbat2 - Fix for Bug #570698
1004 if (substr($curField[$x], -2) != '.*') {
1005 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1010 if (!empty($qry_orderby)) {
1011 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1012 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1016 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
1018 <fieldset
class="tblFooters">
1019 <input type
="submit" name
="submit_sql" value
="<?php echo $strRunQuery; ?>" />
1027 * Displays the footer
1029 require_once './libraries/footer.inc.php';