2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * query by example the whole database
12 require_once './libraries/common.inc.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'])) {
29 $zero_rows = htmlspecialchars($GLOBALS['strSuccess']);
30 $sql_query = urldecode($_REQUEST['encoded_sql_query']);
35 require './libraries/db_common.inc.php';
36 $url_query .= '&goto=db_qbe.php';
37 $url_params['goto'] = 'db_qbe.php';
38 require './libraries/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 // this was a work in progress, deactivated for now
94 //$columns = PMA_DBI_get_columns_full($GLOBALS['db']);
95 //$tables = PMA_DBI_get_columns_full($GLOBALS['db']);
101 $tbl_result = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE
);
102 $tbl_result_cnt = PMA_DBI_num_rows($tbl_result);
103 if (0 == $tbl_result_cnt) {
104 echo '<div class="warning">' . $strNoTablesFound . '</div>';
105 require_once './libraries/footer.inc.php';
112 // The tables list gets from MySQL
113 while ($i < $tbl_result_cnt) {
114 list($tbl) = PMA_DBI_fetch_row($tbl_result);
115 $fld_results = PMA_DBI_get_fields($db, $tbl);
116 $fld_results_cnt = ($fld_results) ?
count($fld_results) : 0;
119 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
120 $tbl_names[$tbl] = '';
122 $tbl_names[$tbl] = ' selected="selected"';
125 // The fields list per selected tables
126 if ($tbl_names[$tbl] == ' selected="selected"') {
127 $fld[$k++
] = PMA_backquote($tbl) . '.*';
128 while ($j < $fld_results_cnt) {
129 $fld[$k] = PMA_convert_display_charset($fld_results[$j]['Field']);
130 $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
132 // increase the width if necessary
133 if (strlen($fld[$k]) > $form_column_width) {
134 $form_column_width = strlen($fld[$k]);
144 PMA_DBI_free_result($tbl_result);
146 // largest width found
147 $realwidth = $form_column_width . 'ex';
151 * Displays the Query by example form
154 function showColumnSelectCell($columns, $column_number, $selected = '')
158 <select name
="Field[<?php echo $column_number; ?>]" size
="1">
159 <option value
=""> 
;</option
>
161 foreach ($columns as $column) {
162 if ($column === $selected) {
163 $sel = ' selected="selected"';
168 echo '<option value="' . htmlspecialchars($column) . '"' . $sel . '>'
169 . str_replace(' ', ' ', htmlspecialchars($column)) . '</option>' . "\n";
179 <form action
="db_qbe.php" method
="post">
180 <table
class="data" style
="width: 100%;">
181 <tr
class="odd noclick">
182 <th
><?php
echo $strField; ?
>:</th
>
185 for ($x = 0; $x < $col; $x++
) {
186 if (isset($ins_col[$x]) && $ins_col[$x] == 'on') {
187 showColumnSelectCell($fld, $z);
191 if (! empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
196 if (isset($Field[$x])) {
197 $selected = urldecode($Field[$x]);
198 $curField[$z] = urldecode($Field[$x]);
200 showColumnSelectCell($fld, $z, $selected);
207 <tr
class="even noclick">
208 <th
><?php
echo $strSort; ?
>:</th
>
211 for ($x = 0; $x < $col; $x++
) {
212 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
215 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
216 <option value
=""> 
;</option
>
217 <option value
="ASC"><?php
echo $strAscending; ?
></option
>
218 <option value
="DESC"><?php
echo $strDescending; ?
></option
>
226 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
231 <select style
="width: <?php echo $realwidth; ?>" name
="Sort[<?php echo $z; ?>]" size
="1">
232 <option value
=""> 
;</option
>
236 // If they have chosen all fields using the * selector,
237 // then sorting is not available
238 // Robbat2 - Fix for Bug #570698
239 if (isset($Sort[$x]) && isset($Field[$x])
240 && substr(urldecode($Field[$x]), -2) == '.*') {
244 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
245 $curSort[$z] = $Sort[$x];
246 $sel = ' selected="selected"';
251 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
252 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
253 $curSort[$z] = $Sort[$x];
254 $sel = ' selected="selected"';
259 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
271 <tr
class="odd noclick">
272 <th
><?php
echo $strShow; ?
>:</th
>
275 for ($x = 0; $x < $col; $x++
) {
276 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
279 <input type
="checkbox" name
="Show[<?php echo $z; ?>]" />
286 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
289 if (isset($Show[$x])) {
290 $checked = ' checked="checked"';
291 $curShow[$z] = $Show[$x];
297 <input type
="checkbox" name
="Show[<?php echo $z; ?>]"<?php
echo $checked; ?
> />
306 <!-- Criteria row
-->
307 <tr
class="even noclick">
308 <th
><?php
echo $strCriteria; ?
>:</th
>
311 for ($x = 0; $x < $col; $x++
) {
312 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
315 <input type
="text" name
="criteria[<?php echo $z; ?>]" value
="" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
322 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
325 if (isset($criteria[$x])) {
326 $stripped_Criteria = $criteria[$x];
328 if ((empty($prev_criteria) ||
!isset($prev_criteria[$x]))
329 ||
urldecode($prev_criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
330 $curCriteria[$z] = $stripped_Criteria;
331 $encoded_Criteria = urlencode($stripped_Criteria);
333 $curCriteria[$z] = urldecode($prev_criteria[$x]);
334 $encoded_Criteria = $prev_criteria[$x];
338 <input type
="hidden" name
="prev_criteria[<?php echo $z; ?>]" value
="<?php echo $encoded_Criteria; ?>" />
339 <input type
="text" name
="criteria[<?php echo $z; ?>]" value
="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style
="width: <?php echo $realwidth; ?>" size
="20" />
348 <!-- And/Or columns
and rows
-->
352 for ($y = 0; $y <= $row; $y++
) {
353 if (isset($ins_row[$y]) && $ins_row[$y] == 'on') {
354 $chk['or'] = ' checked="checked"';
357 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
358 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
359 <!-- Row controls
-->
360 <table cellpadding
="0" cellspacing
="0" border
="0">
362 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
363 <small
><?php
echo $strQBEIns; ?
>:</small
>
364 <input type
="checkbox" name
="ins_row[<?php echo $w; ?>]" />
366 <td align
="<?php echo $cell_align_right; ?>">
367 <b
><?php
echo $strAnd; ?
>:</b
>
370 <input type
="radio" name
="and_or_row[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
375 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
376 <small
><?php
echo $strQBEDel; ?
>:</small
>
377 <input type
="checkbox" name
="del_row[<?php echo $w; ?>]" />
379 <td align
="<?php echo $cell_align_right; ?>">
380 <b
><?php
echo $strOr; ?
>:</b
>
383 <input type
="radio" name
="and_or_row[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
391 for ($x = 0; $x < $col; $x++
) {
392 if (isset($ins_col[$x]) && $ins_col[$x] == 'on') {
394 $or = 'Or' . $w . '[' . $z . ']';
397 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
402 if (isset($del_col[$x]) && $del_col[$x] == 'on') {
407 $or = 'Or' . $w . '[' . $z . ']';
410 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
420 $odd_row =! $odd_row;
423 if (isset($del_row[$y]) && $del_row[$y] == 'on') {
427 if (isset($and_or_row[$y])) {
428 $curAndOrRow[$w] = $and_or_row[$y];
430 if (isset($and_or_row[$y]) && $and_or_row[$y] == 'and') {
431 $chk['and'] = ' checked="checked"';
434 $chk['or'] = ' checked="checked"';
439 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
440 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
441 <!-- Row controls
-->
442 <table border
="0" cellpadding
="0" cellspacing
="0">
444 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
445 <small
><?php
echo $strQBEIns; ?
>:</small
>
446 <input type
="checkbox" name
="ins_row[<?php echo $w; ?>]" />
448 <td align
="<?php echo $cell_align_right; ?>">
449 <b
><?php
echo $strAnd; ?
>:</b
>
452 <input type
="radio" name
="and_or_row[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
456 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
457 <small
><?php
echo $strQBEDel; ?
>:</small
>
458 <input type
="checkbox" name
="del_row[<?php echo $w; ?>]" />
460 <td align
="<?php echo $cell_align_right; ?>">
461 <b
><?php
echo $strOr; ?
>:</b
>
464 <input type
="radio" name
="and_or_row[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
471 for ($x = 0; $x < $col; $x++
) {
472 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
474 $or = 'Or' . $w . '[' . $z . ']';
477 <textarea cols
="20" rows
="2" style
="width: <?php echo $realwidth; ?>" name
="<?php echo $or; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
482 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
488 if (!isset($
{$or})) {
491 if (!empty($
{$or}) && isset($
{$or}[$x])) {
492 $stripped_or = $
{$or}[$x];
498 <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
>
501 if (!empty($
{$or}) && isset($
{$or}[$x])) {
502 $
{'cur' . $or}[$z] = $
{$or}[$x];
512 $odd_row =! $odd_row;
515 <!-- Modify columns
-->
516 <tr
class="even noclick">
517 <th
><?php
echo $strModify; ?
>:</th
>
520 for ($x = 0; $x < $col; $x++
) {
521 if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
522 $curAndOrCol[$z] = $and_or_col[$y];
523 if ($and_or_col[$z] == 'or') {
524 $chk['or'] = ' checked="checked"';
527 $chk['and'] = ' checked="checked"';
532 <b
><?php
echo $strOr; ?
>:</b
>
533 <input type
="radio" name
="and_or_col[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
534  
; 
;<b
><?php
echo $strAnd; ?
>:</b
>
535 <input type
="radio" name
="and_or_col[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
537 <?php
echo $strQBEIns . "\n"; ?
>
538 <input type
="checkbox" name
="ins_col[<?php echo $z; ?>]" />
539  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
540 <input type
="checkbox" name
="del_col[<?php echo $z; ?>]" />
547 if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
551 if (isset($and_or_col[$y])) {
552 $curAndOrCol[$z] = $and_or_col[$y];
554 if (isset($and_or_col[$z]) && $and_or_col[$z] == 'or') {
555 $chk['or'] = ' checked="checked"';
558 $chk['and'] = ' checked="checked"';
563 <b
><?php
echo $strOr; ?
>:</b
>
564 <input type
="radio" name
="and_or_col[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
565  
; 
;<b
><?php
echo $strAnd; ?
>:</b
>
566 <input type
="radio" name
="and_or_col[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
568 <?php
echo $strQBEIns . "\n"; ?
>
569 <input type
="checkbox" name
="ins_col[<?php echo $z; ?>]" />
570  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
571 <input type
="checkbox" name
="del_col[<?php echo $z; ?>]" />
581 <!-- Other controls
-->
584 $url_params['db'] = $db;
585 $url_params['col_cnt'] = $z;
586 $url_params['rows'] = $w;
587 echo PMA_generate_common_hidden_inputs($url_params);
590 <fieldset
class="tblFooters">
591 <table border
="0" cellpadding
="2" cellspacing
="1">
594 <?php
echo $strAddDeleteRow; ?
>:
595 <select size
="1" name
="add_row" style
="vertical-align: middle">
596 <option value
="-3">-3</option
>
597 <option value
="-2">-2</option
>
598 <option value
="-1">-1</option
>
599 <option value
="0" selected
="selected">0</option
>
600 <option value
="1">1</option
>
601 <option value
="2">2</option
>
602 <option value
="3">3</option
>
605 <td width
="10"> 
;</td
>
606 <td nowrap
="nowrap"><?php
echo $strAddDeleteColumn; ?
>:
607 <select size
="1" name
="add_col" style
="vertical-align: middle">
608 <option value
="-3">-3</option
>
609 <option value
="-2">-2</option
>
610 <option value
="-1">-1</option
>
611 <option value
="0" selected
="selected">0</option
>
612 <option value
="1">1</option
>
613 <option value
="2">2</option
>
614 <option value
="3">3</option
>
617 <td width
="10"> 
;</td
>
618 <!-- Generates a query
-->
619 <td
><input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" /></td
>
627 <legend
><?php
echo $strUseTables; ?
></legend
>
629 $strTableListOptions = '';
630 $numTableListOptions = 0;
631 foreach ($tbl_names AS $key => $val) {
632 $strTableListOptions .= ' ';
633 $strTableListOptions .= '<option value="' . htmlspecialchars($key) . '"' . $val . '>'
634 . str_replace(' ', ' ', htmlspecialchars($key)) . '</option>' . "\n";
635 $numTableListOptions++
;
638 <select name
="TableList[]" multiple
="multiple" id
="listTable"
639 size
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>">
640 <?php
echo $strTableListOptions; ?
>
643 <fieldset
class="tblFooters">
644 <input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" />
647 <td width
="20"> 
;</td
>
650 <legend
><?php
echo sprintf($strQueryOnDb, PMA_getDbLink($db)); ?
>
652 <textarea cols
="30" name
="sql_query" id
="textSqlquery"
653 rows
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>"
654 dir
="<?php echo $text_dir; ?>">
659 if (!isset($qry_select)) {
662 for ($x = 0; $x < $col; $x++
) {
663 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
667 $qry_select .= $curField[$x];
671 if (!empty($qry_select)) {
672 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
673 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
678 // Create LEFT JOINS out of Relations
679 // Code originally by Mike Beck <mike.beck@ibmiller.de>
680 // If we can use Relations we could make some left joins.
681 // First find out if relations are available in this database.
683 // First we need the really needed Tables - those in TableList might still be
685 if (isset($Field) && count($Field) > 0) {
687 // Initialize some variables
693 $col_where = array();
696 // We only start this if we have fields, otherwise it would be dumb
697 foreach ($Field AS $value) {
698 $parts = explode('.', $value);
699 if (!empty($parts[0]) && !empty($parts[1])) {
700 $tab_raw = urldecode($parts[0]);
701 $tab = str_replace('`', '', $tab_raw);
702 $tab_all[$tab] = $tab;
704 $col_raw = urldecode($parts[1]);
705 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
709 // Check 'where' clauses
710 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
711 // Now we need all tables that we have in the where clause
712 $crit_cnt = count($criteria);
713 for ($x = 0; $x < $crit_cnt; $x++
) {
714 $curr_tab = explode('.', urldecode($Field[$x]));
715 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
716 $tab_raw = urldecode($curr_tab[0]);
717 $tab = str_replace('`', '', $tab_raw);
719 $col_raw = urldecode($curr_tab[1]);
720 $col1 = str_replace('`', '', $col_raw);
721 $col1 = $tab . '.' . $col1;
722 // Now we know that our array has the same numbers as $criteria
723 // we can check which of our columns has a where clause
724 if (!empty($criteria[$x])) {
725 if (substr($criteria[$x], 0, 1) == '=' ||
stristr($criteria[$x], 'is')) {
726 $col_where[$col] = $col1;
727 $tab_wher[$tab] = $tab;
733 // Cleans temp vars w/o further use
738 if (count($tab_wher) == 1) {
739 // If there is exactly one column that has a decent where-clause
740 // we will just use this
741 $master = key($tab_wher);
743 // Now let's find out which of the tables has an index
744 // (When the control user is the same as the normal user
745 // because he is using one of his databases as pmadb,
746 // the last db selected is not always the one where we need to work)
747 PMA_DBI_select_db($db);
749 foreach ($tab_all AS $tab) {
750 $ind_rs = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tab) . ';');
751 while ($ind = PMA_DBI_fetch_assoc($ind_rs)) {
752 $col1 = $tab . '.' . $ind['Column_name'];
753 if (isset($col_all[$col1])) {
754 if ($ind['non_unique'] == 0) {
755 if (isset($col_where[$col1])) {
756 $col_unique[$col1] = 'Y';
758 $col_unique[$col1] = 'N';
761 if (isset($col_where[$col1])) {
762 $col_index[$col1] = 'Y';
764 $col_index[$col1] = 'N';
768 } // end while (each col of tab)
769 } // end while (each tab)
770 // now we want to find the best.
771 if (isset($col_unique) && count($col_unique) > 0) {
772 $col_cand = $col_unique;
774 } elseif (isset($col_index) && count($col_index) > 0) {
775 $col_cand = $col_index;
777 } elseif (isset($col_where) && count($col_where) > 0) {
778 $col_cand = $tab_wher;
781 $col_cand = $tab_all;
785 // If we came up with $col_unique (very good) or $col_index (still
786 // good) as $col_cand we want to check if we have any 'Y' there
787 // (that would mean that they were also found in the whereclauses
788 // which would be great). if yes, we take only those
789 if ($needsort == 1) {
790 foreach ($col_cand AS $col => $is_where) {
791 $tab = explode('.', $col);
793 if ($is_where == 'Y') {
801 // Candidates restricted in index+where
804 // None of the candidates where in a where-clause
808 // If our array of candidates has more than one member we'll just
809 // find the smallest table.
810 // Of course the actual query would be faster if we check for
811 // the Criteria which gives the smallest result set in its table,
812 // but it would take too much time to check this
813 if (count($col_cand) > 1) {
814 // Of course we only want to check each table once
815 $checked_tables = $col_cand;
816 foreach ($col_cand AS $tab) {
817 if ($checked_tables[$tab] != 1) {
818 $tsize[$tab] = PMA_Table
::countRecords($db, $tab, true, false);
819 $checked_tables[$tab] = 1;
821 $csize[$tab] = $tsize[$tab];
825 $master = key($csize); // Smallest
828 $master = current($col_cand); // Only one single candidate
830 } // end if (exactly one where clause)
833 * Removes unwanted entries from an array (PHP3 compliant)
835 * @param array the array to work with
836 * @param array the list of keys to remove
838 * @return array the cleaned up array
842 function PMA_arrayShort($array, $key)
844 foreach ($array AS $k => $v) {
854 } // end of the "PMA_arrayShort()" function
858 * Finds all related tables
860 * @param string wether to go from master to foreign or vice versa
862 * @return boolean always TRUE
864 * @global array the list of tables that we still couldn't connect
865 * @global array the list of allready connected tables
866 * @global string the current databse name
867 * @global string the super user connection id
868 * @global array the list of relation settings
872 function PMA_getRelatives($from) {
873 global $tab_left, $tab_know, $fromclause;
874 global $controllink, $db, $cfgRelation;
876 if ($from == 'master') {
881 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
882 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
884 $rel_query = 'SELECT *'
885 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
886 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
887 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
888 . ' AND ' . $from . '_table IN ' . $in_know
889 . ' AND ' . $to . '_table IN ' . $in_left;
890 PMA_DBI_select_db($cfgRelation['db'], $controllink);
891 $relations = @PMA_DBI_query
($rel_query, $controllink);
892 PMA_DBI_select_db($db, $controllink);
893 while ($row = PMA_DBI_fetch_assoc($relations)) {
894 $found_table = $row[$to . '_table'];
895 if (isset($tab_left[$found_table])) {
896 $fromclause .= "\n" . ' LEFT JOIN '
897 . PMA_backquote($row[$to . '_table']) . ' ON '
898 . PMA_backquote($row[$from . '_table']) . '.'
899 . PMA_backquote($row[$from . '_field']) . ' = '
900 . PMA_backquote($row[$to . '_table']) . '.'
901 . PMA_backquote($row[$to . '_field']) . ' ';
902 $tab_know[$found_table] = $found_table;
903 $tab_left = PMA_arrayShort($tab_left, $found_table);
908 } // end of the "PMA_getRelatives()" function
911 $tab_left = PMA_arrayShort($tab_all, $master);
912 $tab_know[$master] = $master;
916 while (count($tab_left) > 0) {
918 PMA_getRelatives('master');
920 PMA_getRelatives('foreign');
925 foreach ($tab_left AS $tab) {
926 $emerg .= ', ' . PMA_backquote($tab);
927 $tab_left = PMA_arrayShort($tab_left, $tab);
931 $qry_from = PMA_backquote($master) . $emerg . $fromclause;
932 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
934 } // end count($Field) > 0
936 // In case relations are not defined, just generate the FROM clause
937 // from the list of tables, however we don't generate any JOIN
939 if (empty($qry_from) && isset($tab_all)) {
940 $qry_from = implode(', ', $tab_all);
942 // Now let's see what we got
943 if (!empty($qry_from)) {
944 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
945 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
951 for ($x = 0; $x < $col; $x++
) {
952 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where) && isset($curAndOrCol)) {
953 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
955 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
956 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
961 if ($criteria_cnt > 1) {
962 $qry_where = '(' . $qry_where . ')';
964 // OR rows ${'cur' . $or}[$x]
965 if (!isset($curAndOrRow)) {
966 $curAndOrRow = array();
968 for ($y = 0; $y <= $row; $y++
) {
972 for ($x = 0; $x < $col; $x++
) {
973 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x]) && $x) {
974 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
976 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x])) {
977 $qry_orwhere .= '(' . $curField[$x]
979 . $
{'curOr' . $y}[$x]
985 if ($criteria_cnt > 1) {
986 $qry_orwhere = '(' . $qry_orwhere . ')';
988 if (!empty($qry_orwhere)) {
990 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
995 if (!empty($qry_where) && $qry_where != '()') {
996 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
997 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1002 if (!isset($qry_orderby)) {
1005 for ($x = 0; $x < $col; $x++
) {
1006 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1007 $qry_orderby .= ', ';
1009 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1010 // if they have chosen all fields using the * selector,
1011 // then sorting is not available
1012 // Robbat2 - Fix for Bug #570698
1013 if (substr($curField[$x], -2) != '.*') {
1014 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1019 if (!empty($qry_orderby)) {
1020 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1021 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1025 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
1027 <fieldset
class="tblFooters">
1028 <input type
="submit" name
="submit_sql" value
="<?php echo $strRunQuery; ?>" />
1036 * Displays the footer
1038 require_once './libraries/footer.inc.php';