PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/html/cp/db/db_qbe.php

https://github.com/jmathai/photos
PHP | 1039 lines | 853 code | 79 blank | 107 comment | 224 complexity | 5cdc44b381b212b83188af857b3cd2bd MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * query by example the whole database
  5. *
  6. * @version $Id: db_qbe.php 10588 2007-09-02 19:23:59Z lem9 $
  7. */
  8. /**
  9. * requirements
  10. */
  11. require_once './libraries/common.inc.php';
  12. require_once './libraries/Table.class.php';
  13. require_once './libraries/relation.lib.php';
  14. /**
  15. * Gets the relation settings
  16. */
  17. $cfgRelation = PMA_getRelationsParam();
  18. /**
  19. * A query has been submitted -> execute it, else display the headers
  20. */
  21. if (isset($_REQUEST['submit_sql'])
  22. && preg_match('@^SELECT@i', $_REQUEST['encoded_sql_query'])) {
  23. $goto = 'db_sql.php';
  24. $zero_rows = htmlspecialchars($GLOBALS['strSuccess']);
  25. $sql_query = urldecode($_REQUEST['encoded_sql_query']);
  26. require './sql.php';
  27. exit;
  28. } else {
  29. $sub_part = '_qbe';
  30. require './libraries/db_common.inc.php';
  31. $url_query .= '&amp;goto=db_qbe.php';
  32. $url_params['goto'] = 'db_qbe.php';
  33. require './libraries/db_info.inc.php';
  34. }
  35. if (isset($_REQUEST['submit_sql'])
  36. && ! preg_match('@^SELECT@i', $_REQUEST['encoded_sql_query'])) {
  37. echo '<div class="warning">' . $GLOBALS['strHaveToShow'] . '</div>';
  38. }
  39. /**
  40. * Initialize some variables
  41. */
  42. $col_cnt = isset($_REQUEST['col_cnt']) ? (int) $_REQUEST['col_cnt'] : 3;
  43. $add_col = isset($_REQUEST['add_col']) ? (int) $_REQUEST['add_col'] : 0;
  44. $add_row = isset($_REQUEST['add_row']) ? (int) $_REQUEST['add_row'] : 0;
  45. $rows = isset($_REQUEST['rows']) ? (int) $_REQUEST['rows'] : 0;
  46. $ins_col = isset($_REQUEST['ins_col']) ? $_REQUEST['ins_col'] : array();
  47. $del_col = isset($_REQUEST['del_col']) ? $_REQUEST['del_col'] : array();
  48. $prev_criteria = isset($_REQUEST['prev_criteria'])
  49. ? $_REQUEST['prev_criteria']
  50. : array();
  51. $criteria = isset($_REQUEST['criteria'])
  52. ? $_REQUEST['criteria']
  53. : array_fill(0, $col_cnt, '');
  54. $ins_row = isset($_REQUEST['ins_row'])
  55. ? $_REQUEST['ins_row']
  56. : array_fill(0, $col_cnt, '');
  57. $del_row = isset($_REQUEST['del_row'])
  58. ? $_REQUEST['del_row']
  59. : array_fill(0, $col_cnt, '');
  60. $and_or_row = isset($_REQUEST['and_or_row'])
  61. ? $_REQUEST['and_or_row']
  62. : array_fill(0, $col_cnt, '');
  63. $and_or_col = isset($_REQUEST['and_or_col'])
  64. ? $_REQUEST['and_or_col']
  65. : array_fill(0, $col_cnt, '');
  66. // minimum width
  67. $form_column_width = 12;
  68. $col = max($col_cnt + $add_col, 0);
  69. $row = max($rows + $add_row, 0);
  70. // The tables list sent by a previously submitted form
  71. if (!empty($TableList)) {
  72. $cnt_table_list = count($TableList);
  73. for ($x = 0; $x < $cnt_table_list; $x++) {
  74. $tbl_names[urldecode($TableList[$x])] = ' selected="selected"';
  75. }
  76. } // end if
  77. // this was a work in progress, deactivated for now
  78. //$columns = PMA_DBI_get_columns_full($GLOBALS['db']);
  79. //$tables = PMA_DBI_get_columns_full($GLOBALS['db']);
  80. /**
  81. * Prepares the form
  82. */
  83. $tbl_result = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
  84. $tbl_result_cnt = PMA_DBI_num_rows($tbl_result);
  85. if (0 == $tbl_result_cnt) {
  86. echo '<div class="warning">' . $strNoTablesFound . '</div>';
  87. require_once './libraries/footer.inc.php';
  88. exit;
  89. }
  90. $i = 0;
  91. $k = 0;
  92. // The tables list gets from MySQL
  93. while ($i < $tbl_result_cnt) {
  94. list($tbl) = PMA_DBI_fetch_row($tbl_result);
  95. $fld_results = PMA_DBI_get_fields($db, $tbl);
  96. $fld_results_cnt = ($fld_results) ? count($fld_results) : 0;
  97. $j = 0;
  98. if (empty($tbl_names[$tbl]) && !empty($TableList)) {
  99. $tbl_names[$tbl] = '';
  100. } else {
  101. $tbl_names[$tbl] = ' selected="selected"';
  102. } // end if
  103. // The fields list per selected tables
  104. if ($tbl_names[$tbl] == ' selected="selected"') {
  105. $fld[$k++] = PMA_backquote($tbl) . '.*';
  106. while ($j < $fld_results_cnt) {
  107. $fld[$k] = PMA_convert_display_charset($fld_results[$j]['Field']);
  108. $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
  109. // increase the width if necessary
  110. if (strlen($fld[$k]) > $form_column_width) {
  111. $form_column_width = strlen($fld[$k]);
  112. } //end if
  113. $k++;
  114. $j++;
  115. } // end while
  116. } // end if
  117. $i++;
  118. } // end if
  119. PMA_DBI_free_result($tbl_result);
  120. // largest width found
  121. $realwidth = $form_column_width . 'ex';
  122. /**
  123. * Displays the Query by example form
  124. */
  125. function showColumnSelectCell($columns, $column_number, $selected = '')
  126. {
  127. ?>
  128. <td align="center">
  129. <select name="Field[<?php echo $column_number; ?>]" size="1">
  130. <option value="">&nbsp;</option>
  131. <?php
  132. foreach ($columns as $column) {
  133. if ($column === $selected) {
  134. $sel = ' selected="selected"';
  135. } else {
  136. $sel = '';
  137. }
  138. echo ' ';
  139. echo '<option value="' . htmlspecialchars($column) . '"' . $sel . '>'
  140. . str_replace(' ', '&nbsp;', htmlspecialchars($column)) . '</option>' . "\n";
  141. }
  142. ?>
  143. </select>
  144. </td>
  145. <?php
  146. }
  147. ?>
  148. <fieldset>
  149. <form action="db_qbe.php" method="post">
  150. <table class="data" style="width: 100%;">
  151. <tr class="odd noclick">
  152. <th><?php echo $strField; ?>:</th>
  153. <?php
  154. $z = 0;
  155. for ($x = 0; $x < $col; $x++) {
  156. if (isset($ins_col[$x]) && $ins_col[$x] == 'on') {
  157. showColumnSelectCell($fld, $z);
  158. $z++;
  159. }
  160. if (! empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
  161. continue;
  162. }
  163. $selected = '';
  164. if (isset($Field[$x])) {
  165. $selected = urldecode($Field[$x]);
  166. $curField[$z] = urldecode($Field[$x]);
  167. }
  168. showColumnSelectCell($fld, $z, $selected);
  169. $z++;
  170. } // end for
  171. ?>
  172. </tr>
  173. <!-- Sort row -->
  174. <tr class="even noclick">
  175. <th><?php echo $strSort; ?>:</th>
  176. <?php
  177. $z = 0;
  178. for ($x = 0; $x < $col; $x++) {
  179. if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
  180. ?>
  181. <td align="center">
  182. <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
  183. <option value="">&nbsp;</option>
  184. <option value="ASC"><?php echo $strAscending; ?></option>
  185. <option value="DESC"><?php echo $strDescending; ?></option>
  186. </select>
  187. </td>
  188. <?php
  189. $z++;
  190. } // end if
  191. echo "\n";
  192. if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
  193. continue;
  194. }
  195. ?>
  196. <td align="center">
  197. <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
  198. <option value="">&nbsp;</option>
  199. <?php
  200. echo "\n";
  201. // If they have chosen all fields using the * selector,
  202. // then sorting is not available
  203. // Robbat2 - Fix for Bug #570698
  204. if (isset($Sort[$x]) && isset($Field[$x])
  205. && substr(urldecode($Field[$x]), -2) == '.*') {
  206. $Sort[$x] = '';
  207. } //end if
  208. if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
  209. $curSort[$z] = $Sort[$x];
  210. $sel = ' selected="selected"';
  211. } else {
  212. $sel = '';
  213. } // end if
  214. echo ' ';
  215. echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
  216. if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
  217. $curSort[$z] = $Sort[$x];
  218. $sel = ' selected="selected"';
  219. } else {
  220. $sel = '';
  221. } // end if
  222. echo ' ';
  223. echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
  224. ?>
  225. </select>
  226. </td>
  227. <?php
  228. $z++;
  229. echo "\n";
  230. } // end for
  231. ?>
  232. </tr>
  233. <!-- Show row -->
  234. <tr class="odd noclick">
  235. <th><?php echo $strShow; ?>:</th>
  236. <?php
  237. $z = 0;
  238. for ($x = 0; $x < $col; $x++) {
  239. if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
  240. ?>
  241. <td align="center">
  242. <input type="checkbox" name="Show[<?php echo $z; ?>]" />
  243. </td>
  244. <?php
  245. $z++;
  246. } // end if
  247. echo "\n";
  248. if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
  249. continue;
  250. }
  251. if (isset($Show[$x])) {
  252. $checked = ' checked="checked"';
  253. $curShow[$z] = $Show[$x];
  254. } else {
  255. $checked = '';
  256. }
  257. ?>
  258. <td align="center">
  259. <input type="checkbox" name="Show[<?php echo $z; ?>]"<?php echo $checked; ?> />
  260. </td>
  261. <?php
  262. $z++;
  263. echo "\n";
  264. } // end for
  265. ?>
  266. </tr>
  267. <!-- Criteria row -->
  268. <tr class="even noclick">
  269. <th><?php echo $strCriteria; ?>:</th>
  270. <?php
  271. $z = 0;
  272. for ($x = 0; $x < $col; $x++) {
  273. if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
  274. ?>
  275. <td align="center">
  276. <input type="text" name="criteria[<?php echo $z; ?>]" value="" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
  277. </td>
  278. <?php
  279. $z++;
  280. } // end if
  281. echo "\n";
  282. if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
  283. continue;
  284. }
  285. if (isset($criteria[$x])) {
  286. $stripped_Criteria = $criteria[$x];
  287. }
  288. if ((empty($prev_criteria) || !isset($prev_criteria[$x]))
  289. || urldecode($prev_criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
  290. $curCriteria[$z] = $stripped_Criteria;
  291. $encoded_Criteria = urlencode($stripped_Criteria);
  292. } else {
  293. $curCriteria[$z] = urldecode($prev_criteria[$x]);
  294. $encoded_Criteria = $prev_criteria[$x];
  295. }
  296. ?>
  297. <td align="center">
  298. <input type="hidden" name="prev_criteria[<?php echo $z; ?>]" value="<?php echo $encoded_Criteria; ?>" />
  299. <input type="text" name="criteria[<?php echo $z; ?>]" value="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
  300. </td>
  301. <?php
  302. $z++;
  303. echo "\n";
  304. } // end for
  305. ?>
  306. </tr>
  307. <!-- And/Or columns and rows -->
  308. <?php
  309. $w = 0;
  310. $odd_row = true;
  311. for ($y = 0; $y <= $row; $y++) {
  312. if (isset($ins_row[$y]) && $ins_row[$y] == 'on') {
  313. $chk['or'] = ' checked="checked"';
  314. $chk['and'] = '';
  315. ?>
  316. <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
  317. <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  318. <!-- Row controls -->
  319. <table cellpadding="0" cellspacing="0" border="0">
  320. <tr>
  321. <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  322. <small><?php echo $strQBEIns; ?>:</small>
  323. <input type="checkbox" name="ins_row[<?php echo $w; ?>]" />
  324. </td>
  325. <td align="<?php echo $cell_align_right; ?>">
  326. <b><?php echo $strAnd; ?>:</b>
  327. </td>
  328. <td>
  329. <input type="radio" name="and_or_row[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
  330. &nbsp;
  331. </td>
  332. </tr>
  333. <tr>
  334. <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  335. <small><?php echo $strQBEDel; ?>:</small>
  336. <input type="checkbox" name="del_row[<?php echo $w; ?>]" />
  337. </td>
  338. <td align="<?php echo $cell_align_right; ?>">
  339. <b><?php echo $strOr; ?>:</b>
  340. </td>
  341. <td>
  342. <input type="radio" name="and_or_row[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
  343. &nbsp;
  344. </td>
  345. </tr>
  346. </table>
  347. </td>
  348. <?php
  349. $z = 0;
  350. for ($x = 0; $x < $col; $x++) {
  351. if (isset($ins_col[$x]) && $ins_col[$x] == 'on') {
  352. echo "\n";
  353. $or = 'Or' . $w . '[' . $z . ']';
  354. ?>
  355. <td align="center">
  356. <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
  357. </td>
  358. <?php
  359. $z++;
  360. } // end if
  361. if (isset($del_col[$x]) && $del_col[$x] == 'on') {
  362. continue;
  363. }
  364. echo "\n";
  365. $or = 'Or' . $w . '[' . $z . ']';
  366. ?>
  367. <td align="center">
  368. <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
  369. </td>
  370. <?php
  371. $z++;
  372. } // end for
  373. $w++;
  374. echo "\n";
  375. ?>
  376. </tr>
  377. <?php
  378. $odd_row =! $odd_row;
  379. } // end if
  380. if (isset($del_row[$y]) && $del_row[$y] == 'on') {
  381. continue;
  382. }
  383. if (isset($and_or_row[$y])) {
  384. $curAndOrRow[$w] = $and_or_row[$y];
  385. }
  386. if (isset($and_or_row[$y]) && $and_or_row[$y] == 'and') {
  387. $chk['and'] = ' checked="checked"';
  388. $chk['or'] = '';
  389. } else {
  390. $chk['or'] = ' checked="checked"';
  391. $chk['and'] = '';
  392. }
  393. echo "\n";
  394. ?>
  395. <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
  396. <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  397. <!-- Row controls -->
  398. <table border="0" cellpadding="0" cellspacing="0">
  399. <tr>
  400. <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  401. <small><?php echo $strQBEIns; ?>:</small>
  402. <input type="checkbox" name="ins_row[<?php echo $w; ?>]" />
  403. </td>
  404. <td align="<?php echo $cell_align_right; ?>">
  405. <b><?php echo $strAnd; ?>:</b>
  406. </td>
  407. <td>
  408. <input type="radio" name="and_or_row[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
  409. </td>
  410. </tr>
  411. <tr>
  412. <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
  413. <small><?php echo $strQBEDel; ?>:</small>
  414. <input type="checkbox" name="del_row[<?php echo $w; ?>]" />
  415. </td>
  416. <td align="<?php echo $cell_align_right; ?>">
  417. <b><?php echo $strOr; ?>:</b>
  418. </td>
  419. <td>
  420. <input type="radio" name="and_or_row[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
  421. </td>
  422. </tr>
  423. </table>
  424. </td>
  425. <?php
  426. $z = 0;
  427. for ($x = 0; $x < $col; $x++) {
  428. if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
  429. echo "\n";
  430. $or = 'Or' . $w . '[' . $z . ']';
  431. ?>
  432. <td align="center">
  433. <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
  434. </td>
  435. <?php
  436. $z++;
  437. } // end if
  438. if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
  439. continue;
  440. }
  441. echo "\n";
  442. $or = 'Or' . $y;
  443. if (!isset(${$or})) {
  444. ${$or} = '';
  445. }
  446. if (!empty(${$or}) && isset(${$or}[$x])) {
  447. $stripped_or = ${$or}[$x];
  448. } else {
  449. $stripped_or = '';
  450. }
  451. ?>
  452. <td align="center">
  453. <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>
  454. </td>
  455. <?php
  456. if (!empty(${$or}) && isset(${$or}[$x])) {
  457. ${'cur' . $or}[$z] = ${$or}[$x];
  458. }
  459. $z++;
  460. } // end for
  461. $w++;
  462. echo "\n";
  463. ?>
  464. </tr>
  465. <?php
  466. echo "\n";
  467. $odd_row =! $odd_row;
  468. } // end for
  469. ?>
  470. <!-- Modify columns -->
  471. <tr class="even noclick">
  472. <th><?php echo $strModify; ?>:</th>
  473. <?php
  474. $z = 0;
  475. for ($x = 0; $x < $col; $x++) {
  476. if (!empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') {
  477. $curAndOrCol[$z] = $and_or_col[$y];
  478. if ($and_or_col[$z] == 'or') {
  479. $chk['or'] = ' checked="checked"';
  480. $chk['and'] = '';
  481. } else {
  482. $chk['and'] = ' checked="checked"';
  483. $chk['or'] = '';
  484. }
  485. ?>
  486. <td align="center">
  487. <b><?php echo $strOr; ?>:</b>
  488. <input type="radio" name="and_or_col[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
  489. &nbsp;&nbsp;<b><?php echo $strAnd; ?>:</b>
  490. <input type="radio" name="and_or_col[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
  491. <br />
  492. <?php echo $strQBEIns . "\n"; ?>
  493. <input type="checkbox" name="ins_col[<?php echo $z; ?>]" />
  494. &nbsp;&nbsp;<?php echo $strQBEDel . "\n"; ?>
  495. <input type="checkbox" name="del_col[<?php echo $z; ?>]" />
  496. </td>
  497. <?php
  498. $z++;
  499. } // end if
  500. echo "\n";
  501. if (!empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') {
  502. continue;
  503. }
  504. if (isset($and_or_col[$y])) {
  505. $curAndOrCol[$z] = $and_or_col[$y];
  506. }
  507. if (isset($and_or_col[$z]) && $and_or_col[$z] == 'or') {
  508. $chk['or'] = ' checked="checked"';
  509. $chk['and'] = '';
  510. } else {
  511. $chk['and'] = ' checked="checked"';
  512. $chk['or'] = '';
  513. }
  514. ?>
  515. <td align="center">
  516. <b><?php echo $strOr; ?>:</b>
  517. <input type="radio" name="and_or_col[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
  518. &nbsp;&nbsp;<b><?php echo $strAnd; ?>:</b>
  519. <input type="radio" name="and_or_col[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
  520. <br />
  521. <?php echo $strQBEIns . "\n"; ?>
  522. <input type="checkbox" name="ins_col[<?php echo $z; ?>]" />
  523. &nbsp;&nbsp;<?php echo $strQBEDel . "\n"; ?>
  524. <input type="checkbox" name="del_col[<?php echo $z; ?>]" />
  525. </td>
  526. <?php
  527. $z++;
  528. echo "\n";
  529. } // end for
  530. ?>
  531. </tr>
  532. </table>
  533. <!-- Other controls -->
  534. <?php
  535. $w--;
  536. $url_params['db'] = $db;
  537. $url_params['col_cnt'] = $z;
  538. $url_params['rows'] = $w;
  539. echo PMA_generate_common_hidden_inputs($url_params);
  540. ?>
  541. </fieldset>
  542. <fieldset class="tblFooters">
  543. <table border="0" cellpadding="2" cellspacing="1">
  544. <tr>
  545. <td nowrap="nowrap">
  546. <?php echo $strAddDeleteRow; ?>:
  547. <select size="1" name="add_row" style="vertical-align: middle">
  548. <option value="-3">-3</option>
  549. <option value="-2">-2</option>
  550. <option value="-1">-1</option>
  551. <option value="0" selected="selected">0</option>
  552. <option value="1">1</option>
  553. <option value="2">2</option>
  554. <option value="3">3</option>
  555. </select>
  556. </td>
  557. <td width="10">&nbsp;</td>
  558. <td nowrap="nowrap"><?php echo $strAddDeleteColumn; ?>:
  559. <select size="1" name="add_col" style="vertical-align: middle">
  560. <option value="-3">-3</option>
  561. <option value="-2">-2</option>
  562. <option value="-1">-1</option>
  563. <option value="0" selected="selected">0</option>
  564. <option value="1">1</option>
  565. <option value="2">2</option>
  566. <option value="3">3</option>
  567. </select>
  568. </td>
  569. <td width="10">&nbsp;</td>
  570. <!-- Generates a query -->
  571. <td><input type="submit" name="modify" value="<?php echo $strUpdateQuery; ?>" /></td>
  572. </tr>
  573. </table>
  574. </fieldset>
  575. <table>
  576. <tr><td>
  577. <fieldset>
  578. <legend><?php echo $strUseTables; ?></legend>
  579. <?php
  580. $strTableListOptions = '';
  581. $numTableListOptions = 0;
  582. foreach ($tbl_names AS $key => $val) {
  583. $strTableListOptions .= ' ';
  584. $strTableListOptions .= '<option value="' . htmlspecialchars($key) . '"' . $val . '>'
  585. . str_replace(' ', '&nbsp;', htmlspecialchars($key)) . '</option>' . "\n";
  586. $numTableListOptions++;
  587. }
  588. ?>
  589. <select name="TableList[]" multiple="multiple" id="listTable"
  590. size="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>">
  591. <?php echo $strTableListOptions; ?>
  592. </select>
  593. </fieldset>
  594. <fieldset class="tblFooters">
  595. <input type="submit" name="modify" value="<?php echo $strUpdateQuery; ?>" />
  596. </fieldset>
  597. </td>
  598. <td width="20">&nbsp;</td>
  599. <td>
  600. <fieldset>
  601. <legend><?php echo sprintf($strQueryOnDb, PMA_getDbLink($db)); ?>
  602. </legend>
  603. <textarea cols="30" name="sql_query" id="textSqlquery"
  604. rows="<?php echo ($numTableListOptions > 30) ? '15' : '7'; ?>"
  605. dir="<?php echo $text_dir; ?>">
  606. <?php
  607. // 1. SELECT
  608. $last_select = 0;
  609. $encoded_qry = '';
  610. if (!isset($qry_select)) {
  611. $qry_select = '';
  612. }
  613. for ($x = 0; $x < $col; $x++) {
  614. if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
  615. if ($last_select) {
  616. $qry_select .= ', ';
  617. }
  618. $qry_select .= $curField[$x];
  619. $last_select = 1;
  620. }
  621. } // end for
  622. if (!empty($qry_select)) {
  623. $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
  624. echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
  625. }
  626. // 2. FROM
  627. // Create LEFT JOINS out of Relations
  628. // Code originally by Mike Beck <mike.beck@ibmiller.de>
  629. // If we can use Relations we could make some left joins.
  630. // First find out if relations are available in this database.
  631. // First we need the really needed Tables - those in TableList might still be
  632. // all Tables.
  633. if (isset($Field) && count($Field) > 0) {
  634. // Initialize some variables
  635. $tab_all = array();
  636. $col_all = array();
  637. $tab_wher = array();
  638. $tab_know = array();
  639. $tab_left = array();
  640. $col_where = array();
  641. $fromclause = '';
  642. // We only start this if we have fields, otherwise it would be dumb
  643. foreach ($Field AS $value) {
  644. $parts = explode('.', $value);
  645. if (!empty($parts[0]) && !empty($parts[1])) {
  646. $tab_raw = urldecode($parts[0]);
  647. $tab = str_replace('`', '', $tab_raw);
  648. $tab_all[$tab] = $tab;
  649. $col_raw = urldecode($parts[1]);
  650. $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
  651. }
  652. } // end while
  653. // Check 'where' clauses
  654. if ($cfgRelation['relwork'] && count($tab_all) > 0) {
  655. // Now we need all tables that we have in the where clause
  656. $crit_cnt = count($criteria);
  657. for ($x = 0; $x < $crit_cnt; $x++) {
  658. $curr_tab = explode('.', urldecode($Field[$x]));
  659. if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
  660. $tab_raw = urldecode($curr_tab[0]);
  661. $tab = str_replace('`', '', $tab_raw);
  662. $col_raw = urldecode($curr_tab[1]);
  663. $col1 = str_replace('`', '', $col_raw);
  664. $col1 = $tab . '.' . $col1;
  665. // Now we know that our array has the same numbers as $criteria
  666. // we can check which of our columns has a where clause
  667. if (!empty($criteria[$x])) {
  668. if (substr($criteria[$x], 0, 1) == '=' || stristr($criteria[$x], 'is')) {
  669. $col_where[$col] = $col1;
  670. $tab_wher[$tab] = $tab;
  671. }
  672. } // end if
  673. } // end if
  674. } // end for
  675. // Cleans temp vars w/o further use
  676. unset($tab_raw);
  677. unset($col_raw);
  678. unset($col1);
  679. if (count($tab_wher) == 1) {
  680. // If there is exactly one column that has a decent where-clause
  681. // we will just use this
  682. $master = key($tab_wher);
  683. } else {
  684. // Now let's find out which of the tables has an index
  685. // (When the control user is the same as the normal user
  686. // because he is using one of his databases as pmadb,
  687. // the last db selected is not always the one where we need to work)
  688. PMA_DBI_select_db($db);
  689. foreach ($tab_all AS $tab) {
  690. $ind_rs = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tab) . ';');
  691. while ($ind = PMA_DBI_fetch_assoc($ind_rs)) {
  692. $col1 = $tab . '.' . $ind['Column_name'];
  693. if (isset($col_all[$col1])) {
  694. if ($ind['non_unique'] == 0) {
  695. if (isset($col_where[$col1])) {
  696. $col_unique[$col1] = 'Y';
  697. } else {
  698. $col_unique[$col1] = 'N';
  699. }
  700. } else {
  701. if (isset($col_where[$col1])) {
  702. $col_index[$col1] = 'Y';
  703. } else {
  704. $col_index[$col1] = 'N';
  705. }
  706. }
  707. }
  708. } // end while (each col of tab)
  709. } // end while (each tab)
  710. // now we want to find the best.
  711. if (isset($col_unique) && count($col_unique) > 0) {
  712. $col_cand = $col_unique;
  713. $needsort = 1;
  714. } elseif (isset($col_index) && count($col_index) > 0) {
  715. $col_cand = $col_index;
  716. $needsort = 1;
  717. } elseif (isset($col_where) && count($col_where) > 0) {
  718. $col_cand = $tab_wher;
  719. $needsort = 0;
  720. } else {
  721. $col_cand = $tab_all;
  722. $needsort = 0;
  723. }
  724. // If we came up with $col_unique (very good) or $col_index (still
  725. // good) as $col_cand we want to check if we have any 'Y' there
  726. // (that would mean that they were also found in the whereclauses
  727. // which would be great). if yes, we take only those
  728. if ($needsort == 1) {
  729. foreach ($col_cand AS $col => $is_where) {
  730. $tab = explode('.', $col);
  731. $tab = $tab[0];
  732. if ($is_where == 'Y') {
  733. $vg[$col] = $tab;
  734. } else {
  735. $sg[$col] = $tab;
  736. }
  737. }
  738. if (isset($vg)) {
  739. $col_cand = $vg;
  740. // Candidates restricted in index+where
  741. } else {
  742. $col_cand = $sg;
  743. // None of the candidates where in a where-clause
  744. }
  745. }
  746. // If our array of candidates has more than one member we'll just
  747. // find the smallest table.
  748. // Of course the actual query would be faster if we check for
  749. // the Criteria which gives the smallest result set in its table,
  750. // but it would take too much time to check this
  751. if (count($col_cand) > 1) {
  752. // Of course we only want to check each table once
  753. $checked_tables = $col_cand;
  754. foreach ($col_cand AS $tab) {
  755. if ($checked_tables[$tab] != 1) {
  756. $tsize[$tab] = PMA_Table::countRecords($db, $tab, true, false);
  757. $checked_tables[$tab] = 1;
  758. }
  759. $csize[$tab] = $tsize[$tab];
  760. }
  761. asort($csize);
  762. reset($csize);
  763. $master = key($csize); // Smallest
  764. } else {
  765. reset($col_cand);
  766. $master = current($col_cand); // Only one single candidate
  767. }
  768. } // end if (exactly one where clause)
  769. /**
  770. * Removes unwanted entries from an array (PHP3 compliant)
  771. *
  772. * @param array the array to work with
  773. * @param array the list of keys to remove
  774. *
  775. * @return array the cleaned up array
  776. *
  777. * @access private
  778. */
  779. function PMA_arrayShort($array, $key)
  780. {
  781. foreach ($array AS $k => $v) {
  782. if ($k != $key) {
  783. $reta[$k] = $v;
  784. }
  785. }
  786. if (!isset($reta)) {
  787. $reta = array();
  788. }
  789. return $reta;
  790. } // end of the "PMA_arrayShort()" function
  791. /**
  792. * Finds all related tables
  793. *
  794. * @param string wether to go from master to foreign or vice versa
  795. *
  796. * @return boolean always TRUE
  797. *
  798. * @global array the list of tables that we still couldn't connect
  799. * @global array the list of allready connected tables
  800. * @global string the current databse name
  801. * @global string the super user connection id
  802. * @global array the list of relation settings
  803. *
  804. * @access private
  805. */
  806. function PMA_getRelatives($from) {
  807. global $tab_left, $tab_know, $fromclause;
  808. global $controllink, $db, $cfgRelation;
  809. if ($from == 'master') {
  810. $to = 'foreign';
  811. } else {
  812. $to = 'master';
  813. }
  814. $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
  815. $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
  816. $rel_query = 'SELECT *'
  817. . ' FROM ' . PMA_backquote($cfgRelation['relation'])
  818. . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
  819. . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
  820. . ' AND ' . $from . '_table IN ' . $in_know
  821. . ' AND ' . $to . '_table IN ' . $in_left;
  822. PMA_DBI_select_db($cfgRelation['db'], $controllink);
  823. $relations = @PMA_DBI_query($rel_query, $controllink);
  824. PMA_DBI_select_db($db, $controllink);
  825. while ($row = PMA_DBI_fetch_assoc($relations)) {
  826. $found_table = $row[$to . '_table'];
  827. if (isset($tab_left[$found_table])) {
  828. $fromclause .= "\n" . ' LEFT JOIN '
  829. . PMA_backquote($row[$to . '_table']) . ' ON '
  830. . PMA_backquote($row[$from . '_table']) . '.'
  831. . PMA_backquote($row[$from . '_field']) . ' = '
  832. . PMA_backquote($row[$to . '_table']) . '.'
  833. . PMA_backquote($row[$to . '_field']) . ' ';
  834. $tab_know[$found_table] = $found_table;
  835. $tab_left = PMA_arrayShort($tab_left, $found_table);
  836. }
  837. } // end while
  838. return TRUE;
  839. } // end of the "PMA_getRelatives()" function
  840. $tab_left = PMA_arrayShort($tab_all, $master);
  841. $tab_know[$master] = $master;
  842. $run = 0;
  843. $emerg = '';
  844. while (count($tab_left) > 0) {
  845. if ($run % 2 == 0) {
  846. PMA_getRelatives('master');
  847. } else {
  848. PMA_getRelatives('foreign');
  849. }
  850. $run++;
  851. if ($run > 5) {
  852. foreach ($tab_left AS $tab) {
  853. $emerg .= ', ' . PMA_backquote($tab);
  854. $tab_left = PMA_arrayShort($tab_left, $tab);
  855. }
  856. }
  857. } // end while
  858. $qry_from = PMA_backquote($master) . $emerg . $fromclause;
  859. } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
  860. } // end count($Field) > 0
  861. // In case relations are not defined, just generate the FROM clause
  862. // from the list of tables, however we don't generate any JOIN
  863. if (empty($qry_from) && isset($tab_all)) {
  864. $qry_from = implode(', ', $tab_all);
  865. }
  866. // Now let's see what we got
  867. if (!empty($qry_from)) {
  868. $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
  869. echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
  870. }
  871. // 3. WHERE
  872. $qry_where = '';
  873. $criteria_cnt = 0;
  874. for ($x = 0; $x < $col; $x++) {
  875. if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where) && isset($curAndOrCol)) {
  876. $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
  877. }
  878. if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
  879. $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
  880. $last_where = $x;
  881. $criteria_cnt++;
  882. }
  883. } // end for
  884. if ($criteria_cnt > 1) {
  885. $qry_where = '(' . $qry_where . ')';
  886. }
  887. // OR rows ${'cur' . $or}[$x]
  888. if (!isset($curAndOrRow)) {
  889. $curAndOrRow = array();
  890. }
  891. for ($y = 0; $y <= $row; $y++) {
  892. $criteria_cnt = 0;
  893. $qry_orwhere = '';
  894. $last_orwhere = '';
  895. for ($x = 0; $x < $col; $x++) {
  896. if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x]) && $x) {
  897. $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
  898. }
  899. if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x])) {
  900. $qry_orwhere .= '(' . $curField[$x]
  901. . ' '
  902. . ${'curOr' . $y}[$x]
  903. . ')';
  904. $last_orwhere = $x;
  905. $criteria_cnt++;
  906. }
  907. } // end for
  908. if ($criteria_cnt > 1) {
  909. $qry_orwhere = '(' . $qry_orwhere . ')';
  910. }
  911. if (!empty($qry_orwhere)) {
  912. $qry_where .= "\n"
  913. . strtoupper(isset($curAndOrRow[$y]) ? $curAndOrRow[$y] . ' ' : '')
  914. . $qry_orwhere;
  915. } // end if
  916. } // end for
  917. if (!empty($qry_where) && $qry_where != '()') {
  918. $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
  919. echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
  920. } // end if
  921. // 4. ORDER BY
  922. $last_orderby = 0;
  923. if (!isset($qry_orderby)) {
  924. $qry_orderby = '';
  925. }
  926. for ($x = 0; $x < $col; $x++) {
  927. if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
  928. $qry_orderby .= ', ';
  929. }
  930. if (!empty($curField[$x]) && !empty($curSort[$x])) {
  931. // if they have chosen all fields using the * selector,
  932. // then sorting is not available
  933. // Robbat2 - Fix for Bug #570698
  934. if (substr($curField[$x], -2) != '.*') {
  935. $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
  936. $last_orderby = 1;
  937. }
  938. }
  939. } // end for
  940. if (!empty($qry_orderby)) {
  941. $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
  942. echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
  943. }
  944. ?>
  945. </textarea>
  946. <input type="hidden" name="encoded_sql_query" value="<?php echo $encoded_qry; ?>" />
  947. </fieldset>
  948. <fieldset class="tblFooters">
  949. <input type="submit" name="submit_sql" value="<?php echo $strRunQuery; ?>" />
  950. </fieldset>
  951. </td>
  952. </tr>
  953. </table>
  954. </form>
  955. <?php
  956. /**
  957. * Displays the footer
  958. */
  959. require_once './libraries/footer.inc.php';
  960. ?>