Qbe.php 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Handles DB QBE search
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin\Database;
  10. use PhpMyAdmin\Core;
  11. use PhpMyAdmin\DatabaseInterface;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Relation;
  14. use PhpMyAdmin\SavedSearches;
  15. use PhpMyAdmin\Table;
  16. use PhpMyAdmin\Template;
  17. use PhpMyAdmin\Url;
  18. use PhpMyAdmin\Util;
  19. /**
  20. * Class to handle database QBE search
  21. *
  22. * @package PhpMyAdmin
  23. */
  24. class Qbe
  25. {
  26. /**
  27. * Database name
  28. *
  29. * @access private
  30. * @var string
  31. */
  32. private $_db;
  33. /**
  34. * Table Names (selected/non-selected)
  35. *
  36. * @access private
  37. * @var array
  38. */
  39. private $_criteriaTables;
  40. /**
  41. * Column Names
  42. *
  43. * @access private
  44. * @var array
  45. */
  46. private $_columnNames;
  47. /**
  48. * Number of columns
  49. *
  50. * @access private
  51. * @var integer
  52. */
  53. private $_criteria_column_count;
  54. /**
  55. * Number of Rows
  56. *
  57. * @access private
  58. * @var integer
  59. */
  60. private $_criteria_row_count;
  61. /**
  62. * Whether to insert a new column
  63. *
  64. * @access private
  65. * @var array
  66. */
  67. private $_criteriaColumnInsert;
  68. /**
  69. * Whether to delete a column
  70. *
  71. * @access private
  72. * @var array
  73. */
  74. private $_criteriaColumnDelete;
  75. /**
  76. * Whether to insert a new row
  77. *
  78. * @access private
  79. * @var array
  80. */
  81. private $_criteriaRowInsert;
  82. /**
  83. * Whether to delete a row
  84. *
  85. * @access private
  86. * @var array
  87. */
  88. private $_criteriaRowDelete;
  89. /**
  90. * Already set criteria values
  91. *
  92. * @access private
  93. * @var array
  94. */
  95. private $_criteria;
  96. /**
  97. * Previously set criteria values
  98. *
  99. * @access private
  100. * @var array
  101. */
  102. private $_prev_criteria;
  103. /**
  104. * AND/OR relation b/w criteria columns
  105. *
  106. * @access private
  107. * @var array
  108. */
  109. private $_criteriaAndOrColumn;
  110. /**
  111. * AND/OR relation b/w criteria rows
  112. *
  113. * @access private
  114. * @var array
  115. */
  116. private $_criteriaAndOrRow;
  117. /**
  118. * Large width of a column
  119. *
  120. * @access private
  121. * @var string
  122. */
  123. private $_realwidth;
  124. /**
  125. * Minimum width of a column
  126. *
  127. * @access private
  128. * @var int
  129. */
  130. private $_form_column_width;
  131. /**
  132. * Selected columns in the form
  133. *
  134. * @access private
  135. * @var array
  136. */
  137. private $_formColumns;
  138. /**
  139. * Entered aliases in the form
  140. *
  141. * @access private
  142. * @var array
  143. */
  144. private $_formAliases;
  145. /**
  146. * Chosen sort options in the form
  147. *
  148. * @access private
  149. * @var array
  150. */
  151. private $_formSorts;
  152. /**
  153. * Chosen sort orders in the form
  154. *
  155. * @access private
  156. * @var array
  157. */
  158. private $_formSortOrders;
  159. /**
  160. * Show checkboxes in the form
  161. *
  162. * @access private
  163. * @var array
  164. */
  165. private $_formShows;
  166. /**
  167. * Entered criteria values in the form
  168. *
  169. * @access private
  170. * @var array
  171. */
  172. private $_formCriterions;
  173. /**
  174. * AND/OR column radio buttons in the form
  175. *
  176. * @access private
  177. * @var array
  178. */
  179. private $_formAndOrCols;
  180. /**
  181. * AND/OR row radio buttons in the form
  182. *
  183. * @access private
  184. * @var array
  185. */
  186. private $_formAndOrRows;
  187. /**
  188. * New column count in case of add/delete
  189. *
  190. * @access private
  191. * @var integer
  192. */
  193. private $_new_column_count;
  194. /**
  195. * New row count in case of add/delete
  196. *
  197. * @access private
  198. * @var integer
  199. */
  200. private $_new_row_count;
  201. /**
  202. * List of saved searches
  203. *
  204. * @access private
  205. * @var array
  206. */
  207. private $_savedSearchList = null;
  208. /**
  209. * Current search
  210. *
  211. * @access private
  212. * @var SavedSearches
  213. */
  214. private $_currentSearch = null;
  215. /**
  216. * @var Relation
  217. */
  218. private $relation;
  219. /**
  220. * @var DatabaseInterface
  221. */
  222. public $dbi;
  223. /**
  224. * @var Template
  225. */
  226. public $template;
  227. /**
  228. * Public Constructor
  229. *
  230. * @param Relation $relation Relation object
  231. * @param Template $template Template object
  232. * @param DatabaseInterface $dbi DatabaseInterface object
  233. * @param string $dbname Database name
  234. * @param array $savedSearchList List of saved searches
  235. * @param SavedSearches $currentSearch Current search id
  236. */
  237. public function __construct(
  238. Relation $relation,
  239. Template $template,
  240. $dbi,
  241. $dbname,
  242. array $savedSearchList = [],
  243. $currentSearch = null
  244. ) {
  245. $this->_db = $dbname;
  246. $this->_savedSearchList = $savedSearchList;
  247. $this->_currentSearch = $currentSearch;
  248. $this->dbi = $dbi;
  249. $this->relation = $relation;
  250. $this->template = $template;
  251. $this->_loadCriterias();
  252. // Sets criteria parameters
  253. $this->_setSearchParams();
  254. $this->_setCriteriaTablesAndColumns();
  255. }
  256. /**
  257. * Initialize criterias
  258. *
  259. * @return static
  260. */
  261. private function _loadCriterias()
  262. {
  263. if (null === $this->_currentSearch
  264. || null === $this->_currentSearch->getCriterias()
  265. ) {
  266. return $this;
  267. }
  268. $criterias = $this->_currentSearch->getCriterias();
  269. $_POST = $criterias + $_POST;
  270. return $this;
  271. }
  272. /**
  273. * Getter for current search
  274. *
  275. * @return SavedSearches
  276. */
  277. private function _getCurrentSearch()
  278. {
  279. return $this->_currentSearch;
  280. }
  281. /**
  282. * Sets search parameters
  283. *
  284. * @return void
  285. */
  286. private function _setSearchParams()
  287. {
  288. $criteriaColumnCount = $this->_initializeCriteriasCount();
  289. $this->_criteriaColumnInsert = Core::ifSetOr(
  290. $_POST['criteriaColumnInsert'],
  291. null,
  292. 'array'
  293. );
  294. $this->_criteriaColumnDelete = Core::ifSetOr(
  295. $_POST['criteriaColumnDelete'],
  296. null,
  297. 'array'
  298. );
  299. $this->_prev_criteria = isset($_POST['prev_criteria'])
  300. ? $_POST['prev_criteria']
  301. : [];
  302. $this->_criteria = isset($_POST['criteria'])
  303. ? $_POST['criteria']
  304. : array_fill(0, $criteriaColumnCount, '');
  305. $this->_criteriaRowInsert = isset($_POST['criteriaRowInsert'])
  306. ? $_POST['criteriaRowInsert']
  307. : array_fill(0, $criteriaColumnCount, '');
  308. $this->_criteriaRowDelete = isset($_POST['criteriaRowDelete'])
  309. ? $_POST['criteriaRowDelete']
  310. : array_fill(0, $criteriaColumnCount, '');
  311. $this->_criteriaAndOrRow = isset($_POST['criteriaAndOrRow'])
  312. ? $_POST['criteriaAndOrRow']
  313. : array_fill(0, $criteriaColumnCount, '');
  314. $this->_criteriaAndOrColumn = isset($_POST['criteriaAndOrColumn'])
  315. ? $_POST['criteriaAndOrColumn']
  316. : array_fill(0, $criteriaColumnCount, '');
  317. // sets minimum width
  318. $this->_form_column_width = 12;
  319. $this->_formColumns = [];
  320. $this->_formSorts = [];
  321. $this->_formShows = [];
  322. $this->_formCriterions = [];
  323. $this->_formAndOrRows = [];
  324. $this->_formAndOrCols = [];
  325. }
  326. /**
  327. * Sets criteria tables and columns
  328. *
  329. * @return void
  330. */
  331. private function _setCriteriaTablesAndColumns()
  332. {
  333. // The tables list sent by a previously submitted form
  334. if (Core::isValid($_POST['TableList'], 'array')) {
  335. foreach ($_POST['TableList'] as $each_table) {
  336. $this->_criteriaTables[$each_table] = ' selected="selected"';
  337. }
  338. } // end if
  339. $all_tables = $this->dbi->query(
  340. 'SHOW TABLES FROM ' . Util::backquote($this->_db) . ';',
  341. DatabaseInterface::CONNECT_USER,
  342. DatabaseInterface::QUERY_STORE
  343. );
  344. $all_tables_count = $this->dbi->numRows($all_tables);
  345. if (0 == $all_tables_count) {
  346. Message::error(__('No tables found in database.'))->display();
  347. exit;
  348. }
  349. // The tables list gets from MySQL
  350. while (list($table) = $this->dbi->fetchRow($all_tables)) {
  351. $columns = $this->dbi->getColumns($this->_db, $table);
  352. if (empty($this->_criteriaTables[$table])
  353. && ! empty($_POST['TableList'])
  354. ) {
  355. $this->_criteriaTables[$table] = '';
  356. } else {
  357. $this->_criteriaTables[$table] = ' selected="selected"';
  358. } // end if
  359. // The fields list per selected tables
  360. if ($this->_criteriaTables[$table] == ' selected="selected"') {
  361. $each_table = Util::backquote($table);
  362. $this->_columnNames[] = $each_table . '.*';
  363. foreach ($columns as $each_column) {
  364. $each_column = $each_table . '.'
  365. . Util::backquote($each_column['Field']);
  366. $this->_columnNames[] = $each_column;
  367. // increase the width if necessary
  368. $this->_form_column_width = max(
  369. mb_strlen($each_column),
  370. $this->_form_column_width
  371. );
  372. } // end foreach
  373. } // end if
  374. } // end while
  375. $this->dbi->freeResult($all_tables);
  376. // sets the largest width found
  377. $this->_realwidth = $this->_form_column_width . 'ex';
  378. }
  379. /**
  380. * Provides select options list containing column names
  381. *
  382. * @param integer $column_number Column Number (0,1,2) or more
  383. * @param string $selected Selected criteria column name
  384. *
  385. * @return string HTML for select options
  386. */
  387. private function _showColumnSelectCell($column_number, $selected = '')
  388. {
  389. return $this->template->render('database/qbe/column_select_cell', [
  390. 'column_number' => $column_number,
  391. 'column_names' => $this->_columnNames,
  392. 'selected' => $selected,
  393. ]);
  394. }
  395. /**
  396. * Provides select options list containing sort options (ASC/DESC)
  397. *
  398. * @param integer $columnNumber Column Number (0,1,2) or more
  399. * @param string $selected Selected criteria 'ASC' or 'DESC'
  400. *
  401. * @return string HTML for select options
  402. */
  403. private function _getSortSelectCell(
  404. $columnNumber,
  405. $selected = ''
  406. ) {
  407. return $this->template->render('database/qbe/sort_select_cell', [
  408. 'real_width' => $this->_realwidth,
  409. 'column_number' => $columnNumber,
  410. 'selected' => $selected,
  411. ]);
  412. }
  413. /**
  414. * Provides select options list containing sort order
  415. *
  416. * @param integer $columnNumber Column Number (0,1,2) or more
  417. * @param integer $sortOrder Sort order
  418. *
  419. * @return string HTML for select options
  420. */
  421. private function _getSortOrderSelectCell($columnNumber, $sortOrder)
  422. {
  423. $totalColumnCount = $this->_getNewColumnCount();
  424. return $this->template->render('database/qbe/sort_order_select_cell', [
  425. 'total_column_count' => $totalColumnCount,
  426. 'column_number' => $columnNumber,
  427. 'sort_order' => $sortOrder,
  428. ]);
  429. }
  430. /**
  431. * Returns the new column count after adding and removing columns as instructed
  432. *
  433. * @return int new column count
  434. */
  435. private function _getNewColumnCount()
  436. {
  437. $totalColumnCount = $this->_criteria_column_count;
  438. if (! empty($this->_criteriaColumnInsert)) {
  439. $totalColumnCount += count($this->_criteriaColumnInsert);
  440. }
  441. if (! empty($this->_criteriaColumnDelete)) {
  442. $totalColumnCount -= count($this->_criteriaColumnDelete);
  443. }
  444. return $totalColumnCount;
  445. }
  446. /**
  447. * Provides search form's row containing column select options
  448. *
  449. * @return string HTML for search table's row
  450. */
  451. private function _getColumnNamesRow()
  452. {
  453. $html_output = '<tr class="noclick">';
  454. $html_output .= '<th>' . __('Column:') . '</th>';
  455. $new_column_count = 0;
  456. for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) {
  457. if (isset($this->_criteriaColumnInsert[$column_index])
  458. && $this->_criteriaColumnInsert[$column_index] == 'on'
  459. ) {
  460. $html_output .= $this->_showColumnSelectCell(
  461. $new_column_count
  462. );
  463. $new_column_count++;
  464. }
  465. if (! empty($this->_criteriaColumnDelete)
  466. && isset($this->_criteriaColumnDelete[$column_index])
  467. && $this->_criteriaColumnDelete[$column_index] == 'on'
  468. ) {
  469. continue;
  470. }
  471. $selected = '';
  472. if (isset($_POST['criteriaColumn'][$column_index])) {
  473. $selected = $_POST['criteriaColumn'][$column_index];
  474. $this->_formColumns[$new_column_count]
  475. = $_POST['criteriaColumn'][$column_index];
  476. }
  477. $html_output .= $this->_showColumnSelectCell(
  478. $new_column_count,
  479. $selected
  480. );
  481. $new_column_count++;
  482. } // end for
  483. $this->_new_column_count = $new_column_count;
  484. $html_output .= '</tr>';
  485. return $html_output;
  486. }
  487. /**
  488. * Provides search form's row containing column aliases
  489. *
  490. * @return string HTML for search table's row
  491. */
  492. private function _getColumnAliasRow()
  493. {
  494. $html_output = '<tr class="noclick">';
  495. $html_output .= '<th>' . __('Alias:') . '</th>';
  496. $new_column_count = 0;
  497. for ($colInd = 0; $colInd < $this->_criteria_column_count; $colInd++) {
  498. if (! empty($this->_criteriaColumnInsert)
  499. && isset($this->_criteriaColumnInsert[$colInd])
  500. && $this->_criteriaColumnInsert[$colInd] == 'on'
  501. ) {
  502. $html_output .= '<td class="center">';
  503. $html_output .= '<input type="text"'
  504. . ' name="criteriaAlias[' . $new_column_count . ']">';
  505. $html_output .= '</td>';
  506. $new_column_count++;
  507. } // end if
  508. if (! empty($this->_criteriaColumnDelete)
  509. && isset($this->_criteriaColumnDelete[$colInd])
  510. && $this->_criteriaColumnDelete[$colInd] == 'on'
  511. ) {
  512. continue;
  513. }
  514. $tmp_alias = '';
  515. if (! empty($_POST['criteriaAlias'][$colInd])) {
  516. $tmp_alias
  517. = $this->_formAliases[$new_column_count]
  518. = $_POST['criteriaAlias'][$colInd];
  519. }// end if
  520. $html_output .= '<td class="center">';
  521. $html_output .= '<input type="text"'
  522. . ' name="criteriaAlias[' . $new_column_count . ']"'
  523. . ' value="' . htmlspecialchars($tmp_alias) . '">';
  524. $html_output .= '</td>';
  525. $new_column_count++;
  526. } // end for
  527. $html_output .= '</tr>';
  528. return $html_output;
  529. }
  530. /**
  531. * Provides search form's row containing sort(ASC/DESC) select options
  532. *
  533. * @return string HTML for search table's row
  534. */
  535. private function _getSortRow()
  536. {
  537. $html_output = '<tr class="noclick">';
  538. $html_output .= '<th>' . __('Sort:') . '</th>';
  539. $new_column_count = 0;
  540. for ($colInd = 0; $colInd < $this->_criteria_column_count; $colInd++) {
  541. if (! empty($this->_criteriaColumnInsert)
  542. && isset($this->_criteriaColumnInsert[$colInd])
  543. && $this->_criteriaColumnInsert[$colInd] == 'on'
  544. ) {
  545. $html_output .= $this->_getSortSelectCell($new_column_count);
  546. $new_column_count++;
  547. } // end if
  548. if (! empty($this->_criteriaColumnDelete)
  549. && isset($this->_criteriaColumnDelete[$colInd])
  550. && $this->_criteriaColumnDelete[$colInd] == 'on'
  551. ) {
  552. continue;
  553. }
  554. // If they have chosen all fields using the * selector,
  555. // then sorting is not available, Fix for Bug #570698
  556. if (isset($_POST['criteriaSort'][$colInd])
  557. && isset($_POST['criteriaColumn'][$colInd])
  558. && mb_substr($_POST['criteriaColumn'][$colInd], -2) == '.*'
  559. ) {
  560. $_POST['criteriaSort'][$colInd] = '';
  561. } //end if
  562. $selected = '';
  563. if (isset($_POST['criteriaSort'][$colInd])) {
  564. $this->_formSorts[$new_column_count]
  565. = $_POST['criteriaSort'][$colInd];
  566. if ($_POST['criteriaSort'][$colInd] == 'ASC') {
  567. $selected = 'ASC';
  568. } elseif ($_POST['criteriaSort'][$colInd] == 'DESC') {
  569. $selected = 'DESC';
  570. }
  571. } else {
  572. $this->_formSorts[$new_column_count] = '';
  573. }
  574. $html_output .= $this->_getSortSelectCell(
  575. $new_column_count,
  576. $selected
  577. );
  578. $new_column_count++;
  579. } // end for
  580. $html_output .= '</tr>';
  581. return $html_output;
  582. }
  583. /**
  584. * Provides search form's row containing sort order
  585. *
  586. * @return string HTML for search table's row
  587. */
  588. private function _getSortOrder()
  589. {
  590. $html_output = '<tr class="noclick">';
  591. $html_output .= '<th>' . __('Sort order:') . '</th>';
  592. $new_column_count = 0;
  593. for ($colInd = 0; $colInd < $this->_criteria_column_count; $colInd++) {
  594. if (! empty($this->_criteriaColumnInsert)
  595. && isset($this->_criteriaColumnInsert[$colInd])
  596. && $this->_criteriaColumnInsert[$colInd] == 'on'
  597. ) {
  598. $html_output .= $this->_getSortOrderSelectCell(
  599. $new_column_count,
  600. null
  601. );
  602. $new_column_count++;
  603. } // end if
  604. if (! empty($this->_criteriaColumnDelete)
  605. && isset($this->_criteriaColumnDelete[$colInd])
  606. && $this->_criteriaColumnDelete[$colInd] == 'on'
  607. ) {
  608. continue;
  609. }
  610. $sortOrder = null;
  611. if (! empty($_POST['criteriaSortOrder'][$colInd])) {
  612. $sortOrder
  613. = $this->_formSortOrders[$new_column_count]
  614. = $_POST['criteriaSortOrder'][$colInd];
  615. }
  616. $html_output .= $this->_getSortOrderSelectCell(
  617. $new_column_count,
  618. $sortOrder
  619. );
  620. $new_column_count++;
  621. } // end for
  622. $html_output .= '</tr>';
  623. return $html_output;
  624. }
  625. /**
  626. * Provides search form's row containing SHOW checkboxes
  627. *
  628. * @return string HTML for search table's row
  629. */
  630. private function _getShowRow()
  631. {
  632. $html_output = '<tr class="noclick">';
  633. $html_output .= '<th>' . __('Show:') . '</th>';
  634. $new_column_count = 0;
  635. for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) {
  636. if (! empty($this->_criteriaColumnInsert)
  637. && isset($this->_criteriaColumnInsert[$column_index])
  638. && $this->_criteriaColumnInsert[$column_index] == 'on'
  639. ) {
  640. $html_output .= '<td class="center">';
  641. $html_output .= '<input type="checkbox"'
  642. . ' name="criteriaShow[' . $new_column_count . ']">';
  643. $html_output .= '</td>';
  644. $new_column_count++;
  645. } // end if
  646. if (! empty($this->_criteriaColumnDelete)
  647. && isset($this->_criteriaColumnDelete[$column_index])
  648. && $this->_criteriaColumnDelete[$column_index] == 'on'
  649. ) {
  650. continue;
  651. }
  652. if (isset($_POST['criteriaShow'][$column_index])) {
  653. $checked_options = ' checked="checked"';
  654. $this->_formShows[$new_column_count]
  655. = $_POST['criteriaShow'][$column_index];
  656. } else {
  657. $checked_options = '';
  658. }
  659. $html_output .= '<td class="center">';
  660. $html_output .= '<input type="checkbox"'
  661. . ' name="criteriaShow[' . $new_column_count . ']"'
  662. . $checked_options . '>';
  663. $html_output .= '</td>';
  664. $new_column_count++;
  665. } // end for
  666. $html_output .= '</tr>';
  667. return $html_output;
  668. }
  669. /**
  670. * Provides search form's row containing criteria Inputboxes
  671. *
  672. * @return string HTML for search table's row
  673. */
  674. private function _getCriteriaInputboxRow()
  675. {
  676. $html_output = '<tr class="noclick">';
  677. $html_output .= '<th>' . __('Criteria:') . '</th>';
  678. $new_column_count = 0;
  679. for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) {
  680. if (! empty($this->_criteriaColumnInsert)
  681. && isset($this->_criteriaColumnInsert[$column_index])
  682. && $this->_criteriaColumnInsert[$column_index] == 'on'
  683. ) {
  684. $html_output .= '<td class="center">';
  685. $html_output .= '<input type="text"'
  686. . ' name="criteria[' . $new_column_count . ']"'
  687. . ' class="textfield"'
  688. . ' style="width: ' . $this->_realwidth . '"'
  689. . ' size="20">';
  690. $html_output .= '</td>';
  691. $new_column_count++;
  692. } // end if
  693. if (! empty($this->_criteriaColumnDelete)
  694. && isset($this->_criteriaColumnDelete[$column_index])
  695. && $this->_criteriaColumnDelete[$column_index] == 'on'
  696. ) {
  697. continue;
  698. }
  699. $tmp_criteria = '';
  700. if (isset($this->_criteria[$column_index])) {
  701. $tmp_criteria = $this->_criteria[$column_index];
  702. }
  703. if ((empty($this->_prev_criteria)
  704. || ! isset($this->_prev_criteria[$column_index]))
  705. || $this->_prev_criteria[$column_index] != htmlspecialchars($tmp_criteria)
  706. ) {
  707. $this->_formCriterions[$new_column_count] = $tmp_criteria;
  708. } else {
  709. $this->_formCriterions[$new_column_count]
  710. = $this->_prev_criteria[$column_index];
  711. }
  712. $html_output .= '<td class="center">';
  713. $html_output .= '<input type="hidden"'
  714. . ' name="prev_criteria[' . $new_column_count . ']"'
  715. . ' value="'
  716. . htmlspecialchars($this->_formCriterions[$new_column_count])
  717. . '">';
  718. $html_output .= '<input type="text"'
  719. . ' name="criteria[' . $new_column_count . ']"'
  720. . ' value="' . htmlspecialchars($tmp_criteria) . '"'
  721. . ' class="textfield"'
  722. . ' style="width: ' . $this->_realwidth . '"'
  723. . ' size="20">';
  724. $html_output .= '</td>';
  725. $new_column_count++;
  726. } // end for
  727. $html_output .= '</tr>';
  728. return $html_output;
  729. }
  730. /**
  731. * Provides footer options for adding/deleting row/columns
  732. *
  733. * @param string $type Whether row or column
  734. *
  735. * @return string HTML for footer options
  736. */
  737. private function _getFootersOptions($type)
  738. {
  739. return $this->template->render('database/qbe/footer_options', [
  740. 'type' => $type,
  741. ]);
  742. }
  743. /**
  744. * Provides search form table's footer options
  745. *
  746. * @return string HTML for table footer
  747. */
  748. private function _getTableFooters()
  749. {
  750. $html_output = '<fieldset class="tblFooters">';
  751. $html_output .= $this->_getFootersOptions("row");
  752. $html_output .= $this->_getFootersOptions("column");
  753. $html_output .= '<div class="floatleft">';
  754. $html_output .= '<input class="btn btn-secondary" type="submit" name="modify"'
  755. . ' value="' . __('Update Query') . '">';
  756. $html_output .= '</div>';
  757. $html_output .= '</fieldset>';
  758. return $html_output;
  759. }
  760. /**
  761. * Provides a select list of database tables
  762. *
  763. * @return string HTML for table select list
  764. */
  765. private function _getTablesList()
  766. {
  767. $html_output = '<div class="floatleft width100">';
  768. $html_output .= '<fieldset>';
  769. $html_output .= '<legend>' . __('Use Tables') . '</legend>';
  770. // Build the options list for each table name
  771. $options = '';
  772. $numTableListOptions = 0;
  773. foreach ($this->_criteriaTables as $key => $val) {
  774. $options .= '<option value="' . htmlspecialchars($key) . '"' . $val . '>'
  775. . str_replace(' ', '&nbsp;', htmlspecialchars($key)) . '</option>';
  776. $numTableListOptions++;
  777. }
  778. $html_output .= '<select name="TableList[]"'
  779. . ' multiple="multiple" id="listTable"'
  780. . ' size="' . ($numTableListOptions > 30 ? '15' : '7') . '">';
  781. $html_output .= $options;
  782. $html_output .= '</select>';
  783. $html_output .= '</fieldset>';
  784. $html_output .= '<fieldset class="tblFooters">';
  785. $html_output .= '<input class="btn btn-secondary" type="submit" name="modify" value="'
  786. . __('Update Query') . '">';
  787. $html_output .= '</fieldset>';
  788. $html_output .= '</div>';
  789. return $html_output;
  790. }
  791. /**
  792. * Provides And/Or modification cell along with Insert/Delete options
  793. * (For modifying search form's table columns)
  794. *
  795. * @param integer $column_number Column Number (0,1,2) or more
  796. * @param array|null $selected Selected criteria column name
  797. * @param bool $last_column Whether this is the last column
  798. *
  799. * @return string HTML for modification cell
  800. */
  801. private function _getAndOrColCell(
  802. $column_number,
  803. $selected = null,
  804. $last_column = false
  805. ) {
  806. $html_output = '<td class="center">';
  807. if (! $last_column) {
  808. $html_output .= '<strong>' . __('Or:') . '</strong>';
  809. $html_output .= '<input type="radio"'
  810. . ' name="criteriaAndOrColumn[' . $column_number . ']"'
  811. . ' value="or"' . ($selected['or'] ?? '') . '>';
  812. $html_output .= '&nbsp;&nbsp;<strong>' . __('And:') . '</strong>';
  813. $html_output .= '<input type="radio"'
  814. . ' name="criteriaAndOrColumn[' . $column_number . ']"'
  815. . ' value="and"' . ($selected['and'] ?? '') . '>';
  816. }
  817. $html_output .= '<br>' . __('Ins');
  818. $html_output .= '<input type="checkbox"'
  819. . ' name="criteriaColumnInsert[' . $column_number . ']">';
  820. $html_output .= '&nbsp;&nbsp;' . __('Del');
  821. $html_output .= '<input type="checkbox"'
  822. . ' name="criteriaColumnDelete[' . $column_number . ']">';
  823. $html_output .= '</td>';
  824. return $html_output;
  825. }
  826. /**
  827. * Provides search form's row containing column modifications options
  828. * (For modifying search form's table columns)
  829. *
  830. * @return string HTML for search table's row
  831. */
  832. private function _getModifyColumnsRow()
  833. {
  834. $html_output = '<tr class="noclick">';
  835. $html_output .= '<th>' . __('Modify:') . '</th>';
  836. $new_column_count = 0;
  837. for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) {
  838. if (! empty($this->_criteriaColumnInsert)
  839. && isset($this->_criteriaColumnInsert[$column_index])
  840. && $this->_criteriaColumnInsert[$column_index] == 'on'
  841. ) {
  842. $html_output .= $this->_getAndOrColCell($new_column_count);
  843. $new_column_count++;
  844. } // end if
  845. if (! empty($this->_criteriaColumnDelete)
  846. && isset($this->_criteriaColumnDelete[$column_index])
  847. && $this->_criteriaColumnDelete[$column_index] == 'on'
  848. ) {
  849. continue;
  850. }
  851. if (isset($this->_criteriaAndOrColumn[$column_index])) {
  852. $this->_formAndOrCols[$new_column_count]
  853. = $this->_criteriaAndOrColumn[$column_index];
  854. }
  855. $checked_options = [];
  856. if (isset($this->_criteriaAndOrColumn[$column_index])
  857. && $this->_criteriaAndOrColumn[$column_index] == 'or'
  858. ) {
  859. $checked_options['or'] = ' checked="checked"';
  860. $checked_options['and'] = '';
  861. } else {
  862. $checked_options['and'] = ' checked="checked"';
  863. $checked_options['or'] = '';
  864. }
  865. $html_output .= $this->_getAndOrColCell(
  866. $new_column_count,
  867. $checked_options,
  868. $column_index + 1 == $this->_criteria_column_count
  869. );
  870. $new_column_count++;
  871. } // end for
  872. $html_output .= '</tr>';
  873. return $html_output;
  874. }
  875. /**
  876. * Provides Insert/Delete options for criteria inputbox
  877. * with AND/OR relationship modification options
  878. *
  879. * @param integer $row_index Number of criteria row
  880. * @param array $checked_options If checked
  881. *
  882. * @return string HTML
  883. */
  884. private function _getInsDelAndOrCell($row_index, array $checked_options)
  885. {
  886. $html_output = '<td class="value nowrap">';
  887. $html_output .= '<!-- Row controls -->';
  888. $html_output .= '<table class="nospacing nopadding">';
  889. $html_output .= '<tr>';
  890. $html_output .= '<td class="value nowrap">';
  891. $html_output .= '<small>' . __('Ins:') . '</small>';
  892. $html_output .= '<input type="checkbox"'
  893. . ' name="criteriaRowInsert[' . $row_index . ']">';
  894. $html_output .= '</td>';
  895. $html_output .= '<td class="value">';
  896. $html_output .= '<strong>' . __('And:') . '</strong>';
  897. $html_output .= '</td>';
  898. $html_output .= '<td>';
  899. $html_output .= '<input type="radio"'
  900. . ' name="criteriaAndOrRow[' . $row_index . ']" value="and"'
  901. . $checked_options['and'] . '>';
  902. $html_output .= '</td>';
  903. $html_output .= '</tr>';
  904. $html_output .= '<tr>';
  905. $html_output .= '<td class="value nowrap">';
  906. $html_output .= '<small>' . __('Del:') . '</small>';
  907. $html_output .= '<input type="checkbox"'
  908. . ' name="criteriaRowDelete[' . $row_index . ']">';
  909. $html_output .= '</td>';
  910. $html_output .= '<td class="value">';
  911. $html_output .= '<strong>' . __('Or:') . '</strong>';
  912. $html_output .= '</td>';
  913. $html_output .= '<td>';
  914. $html_output .= '<input type="radio"'
  915. . ' name="criteriaAndOrRow[' . $row_index . ']"'
  916. . ' value="or"' . $checked_options['or'] . '>';
  917. $html_output .= '</td>';
  918. $html_output .= '</tr>';
  919. $html_output .= '</table>';
  920. $html_output .= '</td>';
  921. return $html_output;
  922. }
  923. /**
  924. * Provides rows for criteria inputbox Insert/Delete options
  925. * with AND/OR relationship modification options
  926. *
  927. * @param integer $new_row_index New row index if rows are added/deleted
  928. *
  929. * @return string HTML table rows
  930. */
  931. private function _getInputboxRow($new_row_index)
  932. {
  933. $html_output = '';
  934. $new_column_count = 0;
  935. for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) {
  936. if (! empty($this->_criteriaColumnInsert)
  937. && isset($this->_criteriaColumnInsert[$column_index])
  938. && $this->_criteriaColumnInsert[$column_index] == 'on'
  939. ) {
  940. $orFieldName = 'Or' . $new_row_index . '[' . $new_column_count . ']';
  941. $html_output .= '<td class="center">';
  942. $html_output .= '<input type="text"'
  943. . ' name="Or' . $orFieldName . '" class="textfield"'
  944. . ' style="width: ' . $this->_realwidth . '" size="20">';
  945. $html_output .= '</td>';
  946. $new_column_count++;
  947. } // end if
  948. if (! empty($this->_criteriaColumnDelete)
  949. && isset($this->_criteriaColumnDelete[$column_index])
  950. && $this->_criteriaColumnDelete[$column_index] == 'on'
  951. ) {
  952. continue;
  953. }
  954. $or = 'Or' . $new_row_index;
  955. if (! empty($_POST[$or]) && isset($_POST[$or][$column_index])) {
  956. $tmp_or = $_POST[$or][$column_index];
  957. } else {
  958. $tmp_or = '';
  959. }
  960. $html_output .= '<td class="center">';
  961. $html_output .= '<input type="text"'
  962. . ' name="Or' . $new_row_index . '[' . $new_column_count . ']"'
  963. . ' value="' . htmlspecialchars($tmp_or) . '" class="textfield"'
  964. . ' style="width: ' . $this->_realwidth . '" size="20">';
  965. $html_output .= '</td>';
  966. if (! empty(${$or}) && isset(${$or}[$column_index])) {
  967. $GLOBALS[${'cur' . $or}][$new_column_count]
  968. = ${$or}[$column_index];
  969. }
  970. $new_column_count++;
  971. } // end for
  972. return $html_output;
  973. }
  974. /**
  975. * Provides rows for criteria inputbox Insert/Delete options
  976. * with AND/OR relationship modification options
  977. *
  978. * @return string HTML table rows
  979. */
  980. private function _getInsDelAndOrCriteriaRows()
  981. {
  982. $html_output = '';
  983. $new_row_count = 0;
  984. $checked_options = [];
  985. for ($row_index = 0; $row_index <= $this->_criteria_row_count; $row_index++) {
  986. if (isset($this->_criteriaRowInsert[$row_index])
  987. && $this->_criteriaRowInsert[$row_index] == 'on'
  988. ) {
  989. $checked_options['or'] = ' checked="checked"';
  990. $checked_options['and'] = '';
  991. $html_output .= '<tr class="noclick">';
  992. $html_output .= $this->_getInsDelAndOrCell(
  993. $new_row_count,
  994. $checked_options
  995. );
  996. $html_output .= $this->_getInputboxRow(
  997. $new_row_count
  998. );
  999. $new_row_count++;
  1000. $html_output .= '</tr>';
  1001. } // end if
  1002. if (isset($this->_criteriaRowDelete[$row_index])
  1003. && $this->_criteriaRowDelete[$row_index] == 'on'
  1004. ) {
  1005. continue;
  1006. }
  1007. if (isset($this->_criteriaAndOrRow[$row_index])) {
  1008. $this->_formAndOrRows[$new_row_count]
  1009. = $this->_criteriaAndOrRow[$row_index];
  1010. }
  1011. if (isset($this->_criteriaAndOrRow[$row_index])
  1012. && $this->_criteriaAndOrRow[$row_index] == 'and'
  1013. ) {
  1014. $checked_options['and'] = ' checked="checked"';
  1015. $checked_options['or'] = '';
  1016. } else {
  1017. $checked_options['or'] = ' checked="checked"';
  1018. $checked_options['and'] = '';
  1019. }
  1020. $html_output .= '<tr class="noclick">';
  1021. $html_output .= $this->_getInsDelAndOrCell(
  1022. $new_row_count,
  1023. $checked_options
  1024. );
  1025. $html_output .= $this->_getInputboxRow(
  1026. $new_row_count
  1027. );
  1028. $new_row_count++;
  1029. $html_output .= '</tr>';
  1030. } // end for
  1031. $this->_new_row_count = $new_row_count;
  1032. return $html_output;
  1033. }
  1034. /**
  1035. * Provides SELECT clause for building SQL query
  1036. *
  1037. * @return string Select clause
  1038. */
  1039. private function _getSelectClause()
  1040. {
  1041. $select_clause = '';
  1042. $select_clauses = [];
  1043. for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) {
  1044. if (! empty($this->_formColumns[$column_index])
  1045. && isset($this->_formShows[$column_index])
  1046. && $this->_formShows[$column_index] == 'on'
  1047. ) {
  1048. $select = $this->_formColumns[$column_index];
  1049. if (! empty($this->_formAliases[$column_index])) {
  1050. $select .= " AS "
  1051. . Util::backquote($this->_formAliases[$column_index]);
  1052. }
  1053. $select_clauses[] = $select;
  1054. }
  1055. } // end for
  1056. if (! empty($select_clauses)) {
  1057. $select_clause = 'SELECT '
  1058. . htmlspecialchars(implode(", ", $select_clauses)) . "\n";
  1059. }
  1060. return $select_clause;
  1061. }
  1062. /**
  1063. * Provides WHERE clause for building SQL query
  1064. *
  1065. * @return string Where clause
  1066. */
  1067. private function _getWhereClause()
  1068. {
  1069. $where_clause = '';
  1070. $criteria_cnt = 0;
  1071. for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) {
  1072. if (! empty($this->_formColumns[$column_index])
  1073. && ! empty($this->_formCriterions[$column_index])
  1074. && $column_index
  1075. && isset($last_where)
  1076. && isset($this->_formAndOrCols)
  1077. ) {
  1078. $where_clause .= ' '
  1079. . mb_strtoupper($this->_formAndOrCols[$last_where])
  1080. . ' ';
  1081. }
  1082. if (! empty($this->_formColumns[$column_index])
  1083. && ! empty($this->_formCriterions[$column_index])
  1084. ) {
  1085. $where_clause .= '(' . $this->_formColumns[$column_index] . ' '
  1086. . $this->_formCriterions[$column_index] . ')';
  1087. $last_where = $column_index;
  1088. $criteria_cnt++;
  1089. }
  1090. } // end for
  1091. if ($criteria_cnt > 1) {
  1092. $where_clause = '(' . $where_clause . ')';
  1093. }
  1094. // OR rows ${'cur' . $or}[$column_index]
  1095. if (! isset($this->_formAndOrRows)) {
  1096. $this->_formAndOrRows = [];
  1097. }
  1098. for ($row_index = 0; $row_index <= $this->_criteria_row_count; $row_index++) {
  1099. $criteria_cnt = 0;
  1100. $qry_orwhere = '';
  1101. $last_orwhere = '';
  1102. for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) {
  1103. if (! empty($this->_formColumns[$column_index])
  1104. && ! empty($_POST['Or' . $row_index][$column_index])
  1105. && $column_index
  1106. ) {
  1107. $qry_orwhere .= ' '
  1108. . mb_strtoupper(
  1109. $this->_formAndOrCols[$last_orwhere]
  1110. )
  1111. . ' ';
  1112. }
  1113. if (! empty($this->_formColumns[$column_index])
  1114. && ! empty($_POST['Or' . $row_index][$column_index])
  1115. ) {
  1116. $qry_orwhere .= '(' . $this->_formColumns[$column_index]
  1117. . ' '
  1118. . $_POST['Or' . $row_index][$column_index]
  1119. . ')';
  1120. $last_orwhere = $column_index;
  1121. $criteria_cnt++;
  1122. }
  1123. } // end for
  1124. if ($criteria_cnt > 1) {
  1125. $qry_orwhere = '(' . $qry_orwhere . ')';
  1126. }
  1127. if (! empty($qry_orwhere)) {
  1128. $where_clause .= "\n"
  1129. . mb_strtoupper(
  1130. isset($this->_formAndOrRows[$row_index])
  1131. ? $this->_formAndOrRows[$row_index] . ' '
  1132. : ''
  1133. )
  1134. . $qry_orwhere;
  1135. } // end if
  1136. } // end for
  1137. if (! empty($where_clause) && $where_clause != '()') {
  1138. $where_clause = 'WHERE ' . htmlspecialchars($where_clause) . "\n";
  1139. } // end if
  1140. return $where_clause;
  1141. }
  1142. /**
  1143. * Provides ORDER BY clause for building SQL query
  1144. *
  1145. * @return string Order By clause
  1146. */
  1147. private function _getOrderByClause()
  1148. {
  1149. $orderby_clause = '';
  1150. $orderby_clauses = [];
  1151. // Create copy of instance variables
  1152. $columns = $this->_formColumns;
  1153. $sort = $this->_formSorts;
  1154. $sortOrder = $this->_formSortOrders;
  1155. if (! empty($sortOrder)
  1156. && count($sortOrder) == count($sort)
  1157. && count($sortOrder) == count($columns)
  1158. ) {
  1159. // Sort all three arrays based on sort order
  1160. array_multisort($sortOrder, $sort, $columns);
  1161. }
  1162. for ($column_index = 0; $column_index < $this->_criteria_column_count; $column_index++) {
  1163. // if all columns are chosen with * selector,
  1164. // then sorting isn't available
  1165. // Fix for Bug #570698
  1166. if (empty($columns[$column_index])
  1167. && empty($sort[$column_index])
  1168. ) {
  1169. continue;
  1170. }
  1171. if (mb_substr($columns[$column_index], -2) == '.*') {
  1172. continue;
  1173. }
  1174. if (! empty($sort[$column_index])) {
  1175. $orderby_clauses[] = $columns[$column_index] . ' '
  1176. . $sort[$column_index];
  1177. }
  1178. } // end for
  1179. if (! empty($orderby_clauses)) {
  1180. $orderby_clause = 'ORDER BY '
  1181. . htmlspecialchars(implode(", ", $orderby_clauses)) . "\n";
  1182. }
  1183. return $orderby_clause;
  1184. }
  1185. /**
  1186. * Provides UNIQUE columns and INDEX columns present in criteria tables
  1187. *
  1188. * @param array $search_tables Tables involved in the search
  1189. * @param array $search_columns Columns involved in the search
  1190. * @param array $where_clause_columns Columns having criteria where clause
  1191. *
  1192. * @return array having UNIQUE and INDEX columns
  1193. */
  1194. private function _getIndexes(
  1195. array $search_tables,
  1196. array $search_columns,
  1197. array $where_clause_columns
  1198. ) {
  1199. $unique_columns = [];
  1200. $index_columns = [];
  1201. foreach ($search_tables as $table) {
  1202. $indexes = $this->dbi->getTableIndexes($this->_db, $table);
  1203. foreach ($indexes as $index) {
  1204. $column = $table . '.' . $index['Column_name'];
  1205. if (isset($search_columns[$column])) {
  1206. if ($index['Non_unique'] == 0) {
  1207. if (isset($where_clause_columns[$column])) {
  1208. $unique_columns[$column] = 'Y';
  1209. } else {
  1210. $unique_columns[$column] = 'N';
  1211. }
  1212. } else {
  1213. if (isset($where_clause_columns[$column])) {
  1214. $index_columns[$column] = 'Y';
  1215. } else {
  1216. $index_columns[$column] = 'N';
  1217. }
  1218. }
  1219. }
  1220. } // end while (each index of a table)
  1221. } // end while (each table)
  1222. return [
  1223. 'unique' => $unique_columns,
  1224. 'index' => $index_columns,
  1225. ];
  1226. }
  1227. /**
  1228. * Provides UNIQUE columns and INDEX columns present in criteria tables
  1229. *
  1230. * @param array $search_tables Tables involved in the search
  1231. * @param array $search_columns Columns involved in the search
  1232. * @param array $where_clause_columns Columns having criteria where clause
  1233. *
  1234. * @return array having UNIQUE and INDEX columns
  1235. */
  1236. private function _getLeftJoinColumnCandidates(
  1237. array $search_tables,
  1238. array $search_columns,
  1239. array $where_clause_columns
  1240. ) {
  1241. $this->dbi->selectDb($this->_db);
  1242. // Get unique columns and index columns
  1243. $indexes = $this->_getIndexes(
  1244. $search_tables,
  1245. $search_columns,
  1246. $where_clause_columns
  1247. );
  1248. $unique_columns = $indexes['unique'];
  1249. $index_columns = $indexes['index'];
  1250. list($candidate_columns, $needsort)
  1251. = $this->_getLeftJoinColumnCandidatesBest(
  1252. $search_tables,
  1253. $where_clause_columns,
  1254. $unique_columns,
  1255. $index_columns
  1256. );
  1257. // If we came up with $unique_columns (very good) or $index_columns (still
  1258. // good) as $candidate_columns we want to check if we have any 'Y' there
  1259. // (that would mean that they were also found in the whereclauses
  1260. // which would be great). if yes, we take only those
  1261. if ($needsort != 1) {
  1262. return $candidate_columns;
  1263. }
  1264. $very_good = [];
  1265. $still_good = [];
  1266. foreach ($candidate_columns as $column => $is_where) {
  1267. $table = explode('.', $column);
  1268. $table = $table[0];
  1269. if ($is_where == 'Y') {
  1270. $very_good[$column] = $table;
  1271. } else {
  1272. $still_good[$column] = $table;
  1273. }
  1274. }
  1275. if (count($very_good) > 0) {
  1276. $candidate_columns = $very_good;
  1277. // Candidates restricted in index+where
  1278. } else {
  1279. $candidate_columns = $still_good;
  1280. // None of the candidates where in a where-clause
  1281. }
  1282. return $candidate_columns;
  1283. }
  1284. /**
  1285. * Provides the main table to form the LEFT JOIN clause
  1286. *
  1287. * @param array $search_tables Tables involved in the search
  1288. * @param array $search_columns Columns involved in the search
  1289. * @param array $where_clause_columns Columns having criteria where clause
  1290. * @param array $where_clause_tables Tables having criteria where clause
  1291. *
  1292. * @return string table name
  1293. */
  1294. private function _getMasterTable(
  1295. array $search_tables,
  1296. array $search_columns,
  1297. array $where_clause_columns,
  1298. array $where_clause_tables
  1299. ) {
  1300. if (count($where_clause_tables) === 1) {
  1301. // If there is exactly one column that has a decent where-clause
  1302. // we will just use this
  1303. return key($where_clause_tables);
  1304. }
  1305. // Now let's find out which of the tables has an index
  1306. // (When the control user is the same as the normal user
  1307. // because he is using one of his databases as pmadb,
  1308. // the last db selected is not always the one where we need to work)
  1309. $candidate_columns = $this->_getLeftJoinColumnCandidates(
  1310. $search_tables,
  1311. $search_columns,
  1312. $where_clause_columns
  1313. );
  1314. // Generally, we need to display all the rows of foreign (referenced)
  1315. // table, whether they have any matching row in child table or not.
  1316. // So we select candidate tables which are foreign tables.
  1317. $foreign_tables = [];
  1318. foreach ($candidate_columns as $one_table) {
  1319. $foreigners = $this->relation->getForeigners($this->_db, $one_table);
  1320. foreach ($foreigners as $key => $foreigner) {
  1321. if ($key != 'foreign_keys_data') {
  1322. if (in_array($foreigner['foreign_table'], $candidate_columns)) {
  1323. $foreign_tables[$foreigner['foreign_table']]
  1324. = $foreigner['foreign_table'];
  1325. }
  1326. continue;
  1327. }
  1328. foreach ($foreigner as $one_key) {
  1329. if (in_array($one_key['ref_table_name'], $candidate_columns)) {
  1330. $foreign_tables[$one_key['ref_table_name']]
  1331. = $one_key['ref_table_name'];
  1332. }
  1333. }
  1334. }
  1335. }
  1336. if (count($foreign_tables)) {
  1337. $candidate_columns = $foreign_tables;
  1338. }
  1339. // If our array of candidates has more than one member we'll just
  1340. // find the smallest table.
  1341. // Of course the actual query would be faster if we check for
  1342. // the Criteria which gives the smallest result set in its table,
  1343. // but it would take too much time to check this
  1344. if (! (count($candidate_columns) > 1)) {
  1345. // Only one single candidate
  1346. return reset($candidate_columns);
  1347. }
  1348. // Of course we only want to check each table once
  1349. $checked_tables = $candidate_columns;
  1350. $tsize = [];
  1351. $maxsize = -1;
  1352. $result = '';
  1353. foreach ($candidate_columns as $table) {
  1354. if ($checked_tables[$table] != 1) {
  1355. $_table = new Table($table, $this->_db);
  1356. $tsize[$table] = $_table->countRecords();
  1357. $checked_tables[$table] = 1;
  1358. }
  1359. if ($tsize[$table] > $maxsize) {
  1360. $maxsize = $tsize[$table];
  1361. $result = $table;
  1362. }
  1363. }
  1364. // Return largest table
  1365. return $result;
  1366. }
  1367. /**
  1368. * Provides columns and tables that have valid where clause criteria
  1369. *
  1370. * @return array
  1371. */
  1372. private function _getWhereClauseTablesAndColumns()
  1373. {
  1374. $where_clause_columns = [];
  1375. $where_clause_tables = [];
  1376. // Now we need all tables that we have in the where clause
  1377. for ($column_index = 0, $nb = count($this->_criteria); $column_index < $nb; $column_index++) {
  1378. $current_table = explode('.', $_POST['criteriaColumn'][$column_index]);
  1379. if (empty($current_table[0]) || empty($current_table[1])) {
  1380. continue;
  1381. } // end if
  1382. $table = str_replace('`', '', $current_table[0]);
  1383. $column = str_replace('`', '', $current_table[1]);
  1384. $column = $table . '.' . $column;
  1385. // Now we know that our array has the same numbers as $criteria
  1386. // we can check which of our columns has a where clause
  1387. if (! empty($this->_criteria[$column_index])) {
  1388. if (mb_substr($this->_criteria[$column_index], 0, 1) == '='
  1389. || false !== stripos($this->_criteria[$column_index], 'is')
  1390. ) {
  1391. $where_clause_columns[$column] = $column;
  1392. $where_clause_tables[$table] = $table;
  1393. }
  1394. } // end if
  1395. } // end for
  1396. return [
  1397. 'where_clause_tables' => $where_clause_tables,
  1398. 'where_clause_columns' => $where_clause_columns,
  1399. ];
  1400. }
  1401. /**
  1402. * Provides FROM clause for building SQL query
  1403. *
  1404. * @param array $formColumns List of selected columns in the form
  1405. *
  1406. * @return string FROM clause
  1407. */
  1408. private function _getFromClause(array $formColumns)
  1409. {
  1410. $from_clause = '';
  1411. if (empty($formColumns)) {
  1412. return $from_clause;
  1413. }
  1414. // Initialize some variables
  1415. $search_tables = $search_columns = [];
  1416. // We only start this if we have fields, otherwise it would be dumb
  1417. foreach ($formColumns as $value) {
  1418. $parts = explode('.', $value);
  1419. if (! empty($parts[0]) && ! empty($parts[1])) {
  1420. $table = str_replace('`', '', $parts[0]);
  1421. $search_tables[$table] = $table;
  1422. $search_columns[] = $table . '.' . str_replace(
  1423. '`',
  1424. '',
  1425. $parts[1]
  1426. );
  1427. }
  1428. } // end while
  1429. // Create LEFT JOINS out of Relations
  1430. $from_clause = $this->_getJoinForFromClause(
  1431. $search_tables,
  1432. $search_columns
  1433. );
  1434. // In case relations are not defined, just generate the FROM clause
  1435. // from the list of tables, however we don't generate any JOIN
  1436. if (empty($from_clause)) {
  1437. // Create cartesian product
  1438. $from_clause = implode(
  1439. ', ',
  1440. array_map([Util::class, 'backquote'], $search_tables)
  1441. );
  1442. }
  1443. return $from_clause;
  1444. }
  1445. /**
  1446. * Formulates the WHERE clause by JOINing tables
  1447. *
  1448. * @param array $searchTables Tables involved in the search
  1449. * @param array $searchColumns Columns involved in the search
  1450. *
  1451. * @return string table name
  1452. */
  1453. private function _getJoinForFromClause(array $searchTables, array $searchColumns)
  1454. {
  1455. // $relations[master_table][foreign_table] => clause
  1456. $relations = [];
  1457. // Fill $relations with inter table relationship data
  1458. foreach ($searchTables as $oneTable) {
  1459. $this->_loadRelationsForTable($relations, $oneTable);
  1460. }
  1461. // Get tables and columns with valid where clauses
  1462. $validWhereClauses = $this->_getWhereClauseTablesAndColumns();
  1463. $whereClauseTables = $validWhereClauses['where_clause_tables'];
  1464. $whereClauseColumns = $validWhereClauses['where_clause_columns'];
  1465. // Get master table
  1466. $master = $this->_getMasterTable(
  1467. $searchTables,
  1468. $searchColumns,
  1469. $whereClauseColumns,
  1470. $whereClauseTables
  1471. );
  1472. // Will include master tables and all tables that can be combined into
  1473. // a cluster by their relation
  1474. $finalized = [];
  1475. if (strlen($master) > 0) {
  1476. // Add master tables
  1477. $finalized[$master] = '';
  1478. }
  1479. // Fill the $finalized array with JOIN clauses for each table
  1480. $this->_fillJoinClauses($finalized, $relations, $searchTables);
  1481. // JOIN clause
  1482. $join = '';
  1483. // Tables that can not be combined with the table cluster
  1484. // which includes master table
  1485. $unfinalized = array_diff($searchTables, array_keys($finalized));
  1486. if (count($unfinalized) > 0) {
  1487. // We need to look for intermediary tables to JOIN unfinalized tables
  1488. // Heuristic to chose intermediary tables is to look for tables
  1489. // having relationships with unfinalized tables
  1490. foreach ($unfinalized as $oneTable) {
  1491. $references = $this->relation->getChildReferences($this->_db, $oneTable);
  1492. foreach ($references as $column => $columnReferences) {
  1493. foreach ($columnReferences as $reference) {
  1494. // Only from this schema
  1495. if ($reference['table_schema'] != $this->_db) {
  1496. continue;
  1497. }
  1498. $table = $reference['table_name'];
  1499. $this->_loadRelationsForTable($relations, $table);
  1500. // Make copies
  1501. $tempFinalized = $finalized;
  1502. $tempSearchTables = $searchTables;
  1503. $tempSearchTables[] = $table;
  1504. // Try joining with the added table
  1505. $this->_fillJoinClauses(
  1506. $tempFinalized,
  1507. $relations,
  1508. $tempSearchTables
  1509. );
  1510. $tempUnfinalized = array_diff(
  1511. $tempSearchTables,
  1512. array_keys($tempFinalized)
  1513. );
  1514. // Take greedy approach.
  1515. // If the unfinalized count drops we keep the new table
  1516. // and switch temporary varibles with the original ones
  1517. if (count($tempUnfinalized) < count($unfinalized)) {
  1518. $finalized = $tempFinalized;
  1519. $searchTables = $tempSearchTables;
  1520. }
  1521. // We are done if no unfinalized tables anymore
  1522. if (count($tempUnfinalized) === 0) {
  1523. break 3;
  1524. }
  1525. }
  1526. }
  1527. }
  1528. $unfinalized = array_diff($searchTables, array_keys($finalized));
  1529. // If there are still unfinalized tables
  1530. if (count($unfinalized) > 0) {
  1531. // Add these tables as cartesian product before joined tables
  1532. $join .= implode(
  1533. ', ',
  1534. array_map([Util::class, 'backquote'], $unfinalized)
  1535. );
  1536. }
  1537. }
  1538. $first = true;
  1539. // Add joined tables
  1540. foreach ($finalized as $table => $clause) {
  1541. if ($first) {
  1542. if (! empty($join)) {
  1543. $join .= ", ";
  1544. }
  1545. $join .= Util::backquote($table);
  1546. $first = false;
  1547. } else {
  1548. $join .= "\n LEFT JOIN " . Util::backquote(
  1549. $table
  1550. ) . " ON " . $clause;
  1551. }
  1552. }
  1553. return $join;
  1554. }
  1555. /**
  1556. * Loads relations for a given table into the $relations array
  1557. *
  1558. * @param array $relations array of relations
  1559. * @param string $oneTable the table
  1560. *
  1561. * @return void
  1562. */
  1563. private function _loadRelationsForTable(array &$relations, $oneTable)
  1564. {
  1565. $relations[$oneTable] = [];
  1566. $foreigners = $this->relation->getForeigners($GLOBALS['db'], $oneTable);
  1567. foreach ($foreigners as $field => $foreigner) {
  1568. // Foreign keys data
  1569. if ($field == 'foreign_keys_data') {
  1570. foreach ($foreigner as $oneKey) {
  1571. $clauses = [];
  1572. // There may be multiple column relations
  1573. foreach ($oneKey['index_list'] as $index => $oneField) {
  1574. $clauses[]
  1575. = Util::backquote($oneTable) . "."
  1576. . Util::backquote($oneField) . " = "
  1577. . Util::backquote($oneKey['ref_table_name']) . "."
  1578. . Util::backquote($oneKey['ref_index_list'][$index]);
  1579. }
  1580. // Combine multiple column relations with AND
  1581. $relations[$oneTable][$oneKey['ref_table_name']]
  1582. = implode(" AND ", $clauses);
  1583. }
  1584. } else { // Internal relations
  1585. $relations[$oneTable][$foreigner['foreign_table']]
  1586. = Util::backquote($oneTable) . "."
  1587. . Util::backquote($field) . " = "
  1588. . Util::backquote($foreigner['foreign_table']) . "."
  1589. . Util::backquote($foreigner['foreign_field']);
  1590. }
  1591. }
  1592. }
  1593. /**
  1594. * Fills the $finalized arrays with JOIN clauses for each of the tables
  1595. *
  1596. * @param array $finalized JOIN clauses for each table
  1597. * @param array $relations Relations among tables
  1598. * @param array $searchTables Tables involved in the search
  1599. *
  1600. * @return void
  1601. */
  1602. private function _fillJoinClauses(array &$finalized, array $relations, array $searchTables)
  1603. {
  1604. while (true) {
  1605. $added = false;
  1606. foreach ($searchTables as $masterTable) {
  1607. $foreignData = $relations[$masterTable];
  1608. foreach ($foreignData as $foreignTable => $clause) {
  1609. if (! isset($finalized[$masterTable])
  1610. && isset($finalized[$foreignTable])
  1611. ) {
  1612. $finalized[$masterTable] = $clause;
  1613. $added = true;
  1614. } elseif (! isset($finalized[$foreignTable])
  1615. && isset($finalized[$masterTable])
  1616. && in_array($foreignTable, $searchTables)
  1617. ) {
  1618. $finalized[$foreignTable] = $clause;
  1619. $added = true;
  1620. }
  1621. if ($added) {
  1622. // We are done if all tables are in $finalized
  1623. if (count($finalized) == count($searchTables)) {
  1624. return;
  1625. }
  1626. }
  1627. }
  1628. }
  1629. // If no new tables were added during this iteration, break;
  1630. if (! $added) {
  1631. return;
  1632. }
  1633. }
  1634. }
  1635. /**
  1636. * Provides the generated SQL query
  1637. *
  1638. * @param array $formColumns List of selected columns in the form
  1639. *
  1640. * @return string SQL query
  1641. */
  1642. private function _getSQLQuery(array $formColumns)
  1643. {
  1644. $sql_query = '';
  1645. // get SELECT clause
  1646. $sql_query .= $this->_getSelectClause();
  1647. // get FROM clause
  1648. $from_clause = $this->_getFromClause($formColumns);
  1649. if (! empty($from_clause)) {
  1650. $sql_query .= 'FROM ' . htmlspecialchars($from_clause) . "\n";
  1651. }
  1652. // get WHERE clause
  1653. $sql_query .= $this->_getWhereClause();
  1654. // get ORDER BY clause
  1655. $sql_query .= $this->_getOrderByClause();
  1656. return $sql_query;
  1657. }
  1658. /**
  1659. * Provides the generated QBE form
  1660. *
  1661. * @return string QBE form
  1662. */
  1663. public function getSelectionForm()
  1664. {
  1665. $html_output = '<form action="db_qbe.php" method="post" id="formQBE" '
  1666. . 'class="lock-page">';
  1667. $html_output .= '<div class="width100">';
  1668. $html_output .= '<fieldset>';
  1669. if ($GLOBALS['cfgRelation']['savedsearcheswork']) {
  1670. $html_output .= $this->_getSavedSearchesField();
  1671. }
  1672. $html_output .= '<div class="responsivetable jsresponsive">';
  1673. $html_output .= '<table class="data" style="width: 100%;">';
  1674. // Get table's <tr> elements
  1675. $html_output .= $this->_getColumnNamesRow();
  1676. $html_output .= $this->_getColumnAliasRow();
  1677. $html_output .= $this->_getShowRow();
  1678. $html_output .= $this->_getSortRow();
  1679. $html_output .= $this->_getSortOrder();
  1680. $html_output .= $this->_getCriteriaInputboxRow();
  1681. $html_output .= $this->_getInsDelAndOrCriteriaRows();
  1682. $html_output .= $this->_getModifyColumnsRow();
  1683. $html_output .= '</table>';
  1684. $this->_new_row_count--;
  1685. $url_params = [];
  1686. $url_params['db'] = $this->_db;
  1687. $url_params['criteriaColumnCount'] = $this->_new_column_count;
  1688. $url_params['rows'] = $this->_new_row_count;
  1689. $html_output .= Url::getHiddenInputs($url_params);
  1690. $html_output .= '</div>';
  1691. $html_output .= '</fieldset>';
  1692. $html_output .= '</div>';
  1693. // get footers
  1694. $html_output .= $this->_getTableFooters();
  1695. // get tables select list
  1696. $html_output .= $this->_getTablesList();
  1697. $html_output .= '</form>';
  1698. $html_output .= '<form action="db_qbe.php" method="post" class="lock-page">';
  1699. $html_output .= Url::getHiddenInputs(['db' => $this->_db]);
  1700. // get SQL query
  1701. $html_output .= '<div class="floatleft desktop50">';
  1702. $html_output .= '<fieldset id="tblQbe">';
  1703. $html_output .= '<legend>'
  1704. . sprintf(
  1705. __('SQL query on database <b>%s</b>:'),
  1706. Util::getDbLink($this->_db)
  1707. );
  1708. $html_output .= '</legend>';
  1709. $text_dir = 'ltr';
  1710. $html_output .= '<textarea cols="80" name="sql_query" id="textSqlquery"'
  1711. . ' rows="' . (count($this->_criteriaTables) > 30 ? '15' : '7') . '"'
  1712. . ' dir="' . $text_dir . '">';
  1713. if (empty($this->_formColumns)) {
  1714. $this->_formColumns = [];
  1715. }
  1716. $html_output .= $this->_getSQLQuery($this->_formColumns);
  1717. $html_output .= '</textarea>';
  1718. $html_output .= '</fieldset>';
  1719. // displays form's footers
  1720. $html_output .= '<fieldset class="tblFooters" id="tblQbeFooters">';
  1721. $html_output .= '<input type="hidden" name="submit_sql" value="1">';
  1722. $html_output .= '<input class="btn btn-primary" type="submit" value="' . __('Submit Query') . '">';
  1723. $html_output .= '</fieldset>';
  1724. $html_output .= '</div>';
  1725. $html_output .= '</form>';
  1726. return $html_output;
  1727. }
  1728. /**
  1729. * Get fields to display
  1730. *
  1731. * @return string
  1732. */
  1733. private function _getSavedSearchesField()
  1734. {
  1735. $html_output = __('Saved bookmarked search:');
  1736. $html_output .= ' <select name="searchId" id="searchId">';
  1737. $html_output .= '<option value="">' . __('New bookmark') . '</option>';
  1738. $currentSearch = $this->_getCurrentSearch();
  1739. $currentSearchId = null;
  1740. $currentSearchName = null;
  1741. if (null != $currentSearch) {
  1742. $currentSearchId = $currentSearch->getId();
  1743. $currentSearchName = $currentSearch->getSearchName();
  1744. }
  1745. foreach ($this->_savedSearchList as $id => $name) {
  1746. $html_output .= '<option value="' . htmlspecialchars($id)
  1747. . '" ' . (
  1748. $id == $currentSearchId
  1749. ? 'selected="selected" '
  1750. : ''
  1751. )
  1752. . '>'
  1753. . htmlspecialchars($name)
  1754. . '</option>';
  1755. }
  1756. $html_output .= '</select>';
  1757. $html_output .= '<input type="text" name="searchName" id="searchName" '
  1758. . 'value="' . htmlspecialchars((string) $currentSearchName) . '">';
  1759. $html_output .= '<input type="hidden" name="action" id="action" value="">';
  1760. $html_output .= '<input class="btn btn-secondary" type="submit" name="saveSearch" id="saveSearch" '
  1761. . 'value="' . __('Create bookmark') . '">';
  1762. if (null !== $currentSearchId) {
  1763. $html_output .= '<input class="btn btn-secondary" type="submit" name="updateSearch" '
  1764. . 'id="updateSearch" value="' . __('Update bookmark') . '">';
  1765. $html_output .= '<input class="btn btn-secondary" type="submit" name="deleteSearch" '
  1766. . 'id="deleteSearch" value="' . __('Delete bookmark') . '">';
  1767. }
  1768. return $html_output;
  1769. }
  1770. /**
  1771. * Initialize _criteria_column_count
  1772. *
  1773. * @return int Previous number of columns
  1774. */
  1775. private function _initializeCriteriasCount(): int
  1776. {
  1777. // sets column count
  1778. $criteriaColumnCount = Core::ifSetOr(
  1779. $_POST['criteriaColumnCount'],
  1780. 3,
  1781. 'numeric'
  1782. );
  1783. $criteriaColumnAdd = Core::ifSetOr(
  1784. $_POST['criteriaColumnAdd'],
  1785. 0,
  1786. 'numeric'
  1787. );
  1788. $this->_criteria_column_count = max(
  1789. $criteriaColumnCount + $criteriaColumnAdd,
  1790. 0
  1791. );
  1792. // sets row count
  1793. $rows = Core::ifSetOr($_POST['rows'], 0, 'numeric');
  1794. $criteriaRowAdd = Core::ifSetOr($_POST['criteriaRowAdd'], 0, 'numeric');
  1795. $this->_criteria_row_count = min(
  1796. 100,
  1797. max($rows + $criteriaRowAdd, 0)
  1798. );
  1799. return (int) $criteriaColumnCount;
  1800. }
  1801. /**
  1802. * Get best
  1803. *
  1804. * @param array $search_tables Tables involved in the search
  1805. * @param array|null $where_clause_columns Columns with where clause
  1806. * @param array|null $unique_columns Unique columns
  1807. * @param array|null $index_columns Indexed columns
  1808. *
  1809. * @return array
  1810. */
  1811. private function _getLeftJoinColumnCandidatesBest(
  1812. array $search_tables,
  1813. ?array $where_clause_columns,
  1814. ?array $unique_columns,
  1815. ?array $index_columns
  1816. ) {
  1817. // now we want to find the best.
  1818. if (isset($unique_columns) && count($unique_columns) > 0) {
  1819. $candidate_columns = $unique_columns;
  1820. $needsort = 1;
  1821. return [
  1822. $candidate_columns,
  1823. $needsort,
  1824. ];
  1825. } elseif (isset($index_columns) && count($index_columns) > 0) {
  1826. $candidate_columns = $index_columns;
  1827. $needsort = 1;
  1828. return [
  1829. $candidate_columns,
  1830. $needsort,
  1831. ];
  1832. } elseif (isset($where_clause_columns) && count($where_clause_columns) > 0) {
  1833. $candidate_columns = $where_clause_columns;
  1834. $needsort = 0;
  1835. return [
  1836. $candidate_columns,
  1837. $needsort,
  1838. ];
  1839. }
  1840. $candidate_columns = $search_tables;
  1841. $needsort = 0;
  1842. return [
  1843. $candidate_columns,
  1844. $needsort,
  1845. ];
  1846. }
  1847. }