PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/pma/db_qbe.php

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