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);
589 <fieldset
class="tblFooters">
590 <table border
="0" cellpadding
="2" cellspacing
="1">
593 <?php
echo $strAddDeleteRow; ?
>:
594 <select size
="1" name
="add_row" style
="vertical-align: middle">
595 <option value
="-3">-3</option
>
596 <option value
="-2">-2</option
>
597 <option value
="-1">-1</option
>
598 <option value
="0" selected
="selected">0</option
>
599 <option value
="1">1</option
>
600 <option value
="2">2</option
>
601 <option value
="3">3</option
>
604 <td width
="10"> 
;</td
>
605 <td nowrap
="nowrap"><?php
echo $strAddDeleteColumn; ?
>:
606 <select size
="1" name
="add_col" style
="vertical-align: middle">
607 <option value
="-3">-3</option
>
608 <option value
="-2">-2</option
>
609 <option value
="-1">-1</option
>
610 <option value
="0" selected
="selected">0</option
>
611 <option value
="1">1</option
>
612 <option value
="2">2</option
>
613 <option value
="3">3</option
>
616 <td width
="10"> 
;</td
>
617 <!-- Generates a query
-->
618 <td
><input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" /></td
>
626 <legend
><?php
echo $strUseTables; ?
></legend
>
628 $strTableListOptions = '';
629 $numTableListOptions = 0;
630 foreach ($tbl_names AS $key => $val) {
631 $strTableListOptions .= ' ';
632 $strTableListOptions .= '<option value="' . htmlspecialchars($key) . '"' . $val . '>'
633 . str_replace(' ', ' ', htmlspecialchars($key)) . '</option>' . "\n";
634 $numTableListOptions++
;
637 <select name
="TableList[]" multiple
="multiple" id
="listTable"
638 size
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>">
639 <?php
echo $strTableListOptions; ?
>
642 <fieldset
class="tblFooters">
643 <input type
="submit" name
="modify" value
="<?php echo $strUpdateQuery; ?>" />
646 <td width
="20"> 
;</td
>
649 <legend
><?php
echo sprintf($strQueryOnDb, PMA_getDbLink($db)); ?
>
651 <textarea cols
="30" name
="sql_query" id
="textSqlquery"
652 rows
="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>"
653 dir
="<?php echo $text_dir; ?>">
658 if (!isset($qry_select)) {
661 for ($x = 0; $x < $col; $x++
) {
662 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
666 $qry_select .= $curField[$x];
670 if (!empty($qry_select)) {
671 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
672 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
677 // Create LEFT JOINS out of Relations
678 // Code originally by Mike Beck <mike.beck@ibmiller.de>
679 // If we can use Relations we could make some left joins.
680 // First find out if relations are available in this database.
682 // First we need the really needed Tables - those in TableList might still be
684 if (isset($Field) && count($Field) > 0) {
686 // Initialize some variables
692 $col_where = array();
695 // We only start this if we have fields, otherwise it would be dumb
696 foreach ($Field AS $value) {
697 $parts = explode('.', $value);
698 if (!empty($parts[0]) && !empty($parts[1])) {
699 $tab_raw = urldecode($parts[0]);
700 $tab = str_replace('`', '', $tab_raw);
701 $tab_all[$tab] = $tab;
703 $col_raw = urldecode($parts[1]);
704 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
708 // Check 'where' clauses
709 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
710 // Now we need all tables that we have in the where clause
711 $crit_cnt = count($criteria);
712 for ($x = 0; $x < $crit_cnt; $x++
) {
713 $curr_tab = explode('.', urldecode($Field[$x]));
714 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
715 $tab_raw = urldecode($curr_tab[0]);
716 $tab = str_replace('`', '', $tab_raw);
718 $col_raw = urldecode($curr_tab[1]);
719 $col1 = str_replace('`', '', $col_raw);
720 $col1 = $tab . '.' . $col1;
721 // Now we know that our array has the same numbers as $criteria
722 // we can check which of our columns has a where clause
723 if (!empty($criteria[$x])) {
724 if (substr($criteria[$x], 0, 1) == '=' ||
stristr($criteria[$x], 'is')) {
725 $col_where[$col] = $col1;
726 $tab_wher[$tab] = $tab;
732 // Cleans temp vars w/o further use
737 if (count($tab_wher) == 1) {
738 // If there is exactly one column that has a decent where-clause
739 // we will just use this
740 $master = key($tab_wher);
742 // Now let's find out which of the tables has an index
743 // (When the control user is the same as the normal user
744 // because he is using one of his databases as pmadb,
745 // the last db selected is not always the one where we need to work)
746 PMA_DBI_select_db($db);
748 foreach ($tab_all AS $tab) {
749 $ind_rs = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tab) . ';');
750 while ($ind = PMA_DBI_fetch_assoc($ind_rs)) {
751 $col1 = $tab . '.' . $ind['Column_name'];
752 if (isset($col_all[$col1])) {
753 if ($ind['non_unique'] == 0) {
754 if (isset($col_where[$col1])) {
755 $col_unique[$col1] = 'Y';
757 $col_unique[$col1] = 'N';
760 if (isset($col_where[$col1])) {
761 $col_index[$col1] = 'Y';
763 $col_index[$col1] = 'N';
767 } // end while (each col of tab)
768 } // end while (each tab)
769 // now we want to find the best.
770 if (isset($col_unique) && count($col_unique) > 0) {
771 $col_cand = $col_unique;
773 } elseif (isset($col_index) && count($col_index) > 0) {
774 $col_cand = $col_index;
776 } elseif (isset($col_where) && count($col_where) > 0) {
777 $col_cand = $tab_wher;
780 $col_cand = $tab_all;
784 // If we came up with $col_unique (very good) or $col_index (still
785 // good) as $col_cand we want to check if we have any 'Y' there
786 // (that would mean that they were also found in the whereclauses
787 // which would be great). if yes, we take only those
788 if ($needsort == 1) {
789 foreach ($col_cand AS $col => $is_where) {
790 $tab = explode('.', $col);
792 if ($is_where == 'Y') {
800 // Candidates restricted in index+where
803 // None of the candidates where in a where-clause
807 // If our array of candidates has more than one member we'll just
808 // find the smallest table.
809 // Of course the actual query would be faster if we check for
810 // the Criteria which gives the smallest result set in its table,
811 // but it would take too much time to check this
812 if (count($col_cand) > 1) {
813 // Of course we only want to check each table once
814 $checked_tables = $col_cand;
815 foreach ($col_cand AS $tab) {
816 if ($checked_tables[$tab] != 1) {
817 $tsize[$tab] = PMA_Table
::countRecords($db, $tab, true, false);
818 $checked_tables[$tab] = 1;
820 $csize[$tab] = $tsize[$tab];
824 $master = key($csize); // Smallest
827 $master = current($col_cand); // Only one single candidate
829 } // end if (exactly one where clause)
832 * Removes unwanted entries from an array (PHP3 compliant)
834 * @param array the array to work with
835 * @param array the list of keys to remove
837 * @return array the cleaned up array
841 function PMA_arrayShort($array, $key)
843 foreach ($array AS $k => $v) {
853 } // end of the "PMA_arrayShort()" function
857 * Finds all related tables
859 * @param string wether to go from master to foreign or vice versa
861 * @return boolean always TRUE
863 * @global array the list of tables that we still couldn't connect
864 * @global array the list of allready connected tables
865 * @global string the current databse name
866 * @global string the super user connection id
867 * @global array the list of relation settings
871 function PMA_getRelatives($from) {
872 global $tab_left, $tab_know, $fromclause;
873 global $controllink, $db, $cfgRelation;
875 if ($from == 'master') {
880 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
881 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
883 $rel_query = 'SELECT *'
884 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
885 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
886 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
887 . ' AND ' . $from . '_table IN ' . $in_know
888 . ' AND ' . $to . '_table IN ' . $in_left;
889 PMA_DBI_select_db($cfgRelation['db'], $controllink);
890 $relations = @PMA_DBI_query
($rel_query, $controllink);
891 PMA_DBI_select_db($db, $controllink);
892 while ($row = PMA_DBI_fetch_assoc($relations)) {
893 $found_table = $row[$to . '_table'];
894 if (isset($tab_left[$found_table])) {
895 $fromclause .= "\n" . ' LEFT JOIN '
896 . PMA_backquote($row[$to . '_table']) . ' ON '
897 . PMA_backquote($row[$from . '_table']) . '.'
898 . PMA_backquote($row[$from . '_field']) . ' = '
899 . PMA_backquote($row[$to . '_table']) . '.'
900 . PMA_backquote($row[$to . '_field']) . ' ';
901 $tab_know[$found_table] = $found_table;
902 $tab_left = PMA_arrayShort($tab_left, $found_table);
907 } // end of the "PMA_getRelatives()" function
910 $tab_left = PMA_arrayShort($tab_all, $master);
911 $tab_know[$master] = $master;
915 while (count($tab_left) > 0) {
917 PMA_getRelatives('master');
919 PMA_getRelatives('foreign');
924 foreach ($tab_left AS $tab) {
925 $emerg .= ', ' . PMA_backquote($tab);
926 $tab_left = PMA_arrayShort($tab_left, $tab);
930 $qry_from = PMA_backquote($master) . $emerg . $fromclause;
931 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
933 } // end count($Field) > 0
935 // In case relations are not defined, just generate the FROM clause
936 // from the list of tables, however we don't generate any JOIN
938 if (empty($qry_from) && isset($tab_all)) {
939 $qry_from = implode(', ', $tab_all);
941 // Now let's see what we got
942 if (!empty($qry_from)) {
943 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
944 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
950 for ($x = 0; $x < $col; $x++
) {
951 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where) && isset($curAndOrCol)) {
952 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
954 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
955 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
960 if ($criteria_cnt > 1) {
961 $qry_where = '(' . $qry_where . ')';
963 // OR rows ${'cur' . $or}[$x]
964 if (!isset($curAndOrRow)) {
965 $curAndOrRow = array();
967 for ($y = 0; $y <= $row; $y++
) {
971 for ($x = 0; $x < $col; $x++
) {
972 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x]) && $x) {
973 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
975 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x])) {
976 $qry_orwhere .= '(' . $curField[$x]
978 . $
{'curOr' . $y}[$x]
984 if ($criteria_cnt > 1) {
985 $qry_orwhere = '(' . $qry_orwhere . ')';
987 if (!empty($qry_orwhere)) {
989 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
994 if (!empty($qry_where) && $qry_where != '()') {
995 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
996 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1001 if (!isset($qry_orderby)) {
1004 for ($x = 0; $x < $col; $x++
) {
1005 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1006 $qry_orderby .= ', ';
1008 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1009 // if they have chosen all fields using the * selector,
1010 // then sorting is not available
1011 // Robbat2 - Fix for Bug #570698
1012 if (substr($curField[$x], -2) != '.*') {
1013 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1018 if (!empty($qry_orderby)) {
1019 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1020 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1024 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
1026 <fieldset
class="tblFooters">
1027 <input type
="submit" name
="submit_sql" value
="<?php echo $strRunQuery; ?>" />
1035 * Displays the footer
1037 require_once './libraries/footer.inc.php';