DbSelect.php 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

namespace Q2A\Database;

use Q2A\Storage\CacheFactory;

/*
	The selectspec array can contain the elements below. See db/selects.php for lots of examples.

	By default, singleSelect() and multiSelect() return the data for each selectspec as a numbered
	array of arrays, one per row. The array for each row has column names in the keys, and data in the values.
	But this can be changed using the 'arraykey', 'arrayvalue' and 'single' in the selectspec.

	Note that even if you specify ORDER BY in 'source', the final results may not be ordered. This is because
	the SELECT could be done within a UNION that (annoyingly) doesn't maintain order. Use 'sortasc' or 'sortdesc'
	to fix this. You can however rely on the combination of ORDER BY and LIMIT retrieving the appropriate records.


	'columns' => Array of names of columns to be retrieved (required)

		If a value in the columns array has an integer key, it is retrieved AS itself (in a SQL sense).
		If a value in the columns array has a non-integer key, it is retrieved AS that key.
		Values in the columns array can include table specifiers before the period.

	'source' => Any SQL after FROM, including table names, JOINs, GROUP BY, ORDER BY, WHERE, etc... (required)

43
	'arguments' => Substitutions in order for ?s in the query, applied in DbConnection->query()
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

	'arraykey' => Name of column to use for keys of the outer-level returned array, instead of numbers by default

	'arrayvalue' => Name of column to use for values of outer-level returned array, instead of arrays by default

	'single' => If true, return the array for a single row and don't embed it within an outer-level array

	'sortasc' => Sort the output ascending by this column

	'sortdesc' => Sort the output descending by this column


	Why does multiSelect() combine usually unrelated SELECT statements into a single query?

	Because if the database and web servers are on different computers, there will be latency.
	This way we ensure that every read pageview on the site requires as few DB queries as possible, so
	that we pay for this latency only one time.

	For writes we worry less, since the user is more likely to be expecting a delay.

	If QA_OPTIMIZE_DISTANT_DB is set to false in qa-config.php, we assume zero latency and go back to
	simple queries, since this will allow both MySQL and PHP to provide quicker results.
*/

class DbSelect
{
70
	/** @var DbConnection */
71 72
	protected $db;

73 74 75 76 77 78
	/** @var array */
	protected $pendingSelects = [];

	/** @var array */
	protected $pendingResults = [];

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	public function __construct(DbConnection $db)
	{
		$this->db = $db;
	}

	/**
	 * Return the data specified by a single $selectspec - see long comment above.
	 * @param $selectspec
	 * @return array|mixed
	 */
	public function singleSelect(array $selectspec)
	{
		// check for cached results
		if (isset($selectspec['caching'])) {
			$cacheDriver = CacheFactory::getCacheDriver();
			$cacheKey = 'query:' . $selectspec['caching']['key'];

			if ($cacheDriver->isEnabled()) {
				$queryData = $cacheDriver->get($cacheKey);
				if ($queryData !== null) {
					return $queryData;
				}
			}
		}

		$query = 'SELECT ';
		foreach ($selectspec['columns'] as $columnas => $columnfrom) {
			$query .= is_int($columnas) ? "$columnfrom, " : "$columnfrom AS `$columnas`, ";
		}

		$query = substr($query, 0, -2);
		if (isset($selectspec['source']) && strlen($selectspec['source']) > 0) {
			$query .= ' FROM ' . $selectspec['source'];
		}

		$params = isset($selectspec['arguments']) ? $selectspec['arguments'] : array();
		$arraykey = isset($selectspec['arraykey']) ? $selectspec['arraykey'] : null;

		$result = $this->db->query($query, $params);
		$data = $result->fetchAllAssoc($arraykey); // arrayvalue is applied in formatSelect()

		$this->formatSelect($data, $selectspec); // post-processing

		// save cached results
		if (isset($selectspec['caching'])) {
			if ($cacheDriver->isEnabled()) {
				$cacheDriver->set($cacheKey, $data, $selectspec['caching']['ttl']);
			}
		}

		return $data;
	}

	/**
	 * Return the data specified by each element of $selectspecs, where the keys of the
	 * returned array match the keys of the supplied $selectspecs array. See long comment above.
	 * @param array $selectspecs
	 * @return array
	 */
	public function multiSelect(array $selectspecs)
	{
		// Perform simple queries if the database is local or there are only 0-1 selectspecs

		if (!QA_OPTIMIZE_DISTANT_DB || count($selectspecs) <= 1) {
			$outresults = array();

			foreach ($selectspecs as $selectkey => $selectspec) {
				$outresults[$selectkey] = $this->singleSelect($selectspec);
			}

			return $outresults;
		}

		// Otherwise, parse columns for each spec to deal with columns without an 'AS' specification

		foreach ($selectspecs as $selectkey => $selectspec) {
			$selectspecs[$selectkey]['outcolumns'] = array();
			$selectspecs[$selectkey]['autocolumn'] = array();

			foreach ($selectspec['columns'] as $columnas => $columnfrom) {
				if (is_int($columnas)) {
					// fetch column name if not already provided in the array key
					$periodpos = strpos($columnfrom, '.');
					$columnas = is_numeric($periodpos) ? substr($columnfrom, $periodpos + 1) : $columnfrom;
					$selectspecs[$selectkey]['autocolumn'][$columnas] = true;
				}

				if (isset($selectspecs[$selectkey]['outcolumns'][$columnas])) {
					qa_fatal_error('Duplicate column name in DbConnection::multiSelect()');
				}

				$selectspecs[$selectkey]['outcolumns'][$columnas] = $columnfrom;
			}

			if (isset($selectspec['arraykey']) && !isset($selectspecs[$selectkey]['outcolumns'][$selectspec['arraykey']])) {
				qa_fatal_error('Used arraykey not in columns in DbConnection::multiSelect()');
			}
			if (isset($selectspec['arrayvalue']) && !isset($selectspecs[$selectkey]['outcolumns'][$selectspec['arrayvalue']])) {
				qa_fatal_error('Used arrayvalue not in columns in DbConnection::multiSelect()');
			}
		}

		// Work out the full list of columns used

		$outcolumns = array();
		foreach ($selectspecs as $selectspec) {
			$outcolumns = array_unique(array_merge($outcolumns, array_keys($selectspec['outcolumns'])));
		}

		// Build the query based on this full list

		$query = '';
		$queryParams = array();
		foreach ($selectspecs as $selectkey => $selectspec) {
			$subquery = "(SELECT " . $this->db->getPDO()->quote($selectkey) . ' AS selectkey';

			foreach ($outcolumns as $columnas) {
				$subquery .= ', ' . (isset($selectspec['outcolumns'][$columnas]) ? $selectspec['outcolumns'][$columnas] : 'NULL');

				if (empty($query) && !isset($selectspec['autocolumn'][$columnas])) {
					$subquery .= ' AS ' . $columnas;
				}
			}

			if (isset($selectspec['source']) && strlen($selectspec['source']) > 0) {
				$subquery .= ' FROM ' . $selectspec['source'];
			}

			$subquery .= ')';

			if (strlen($query)) {
				$query .= ' UNION ALL ';
			}

			$query .= $subquery;
			if (isset($selectspec['arguments'])) {
				$queryParams = array_merge($queryParams, array_values($selectspec['arguments']));
			}
		}

		// Perform query and extract results

		$stmt = $this->db->query($query, $queryParams);
		$rawresults = $stmt->fetchAllAssoc();

		$outresults = array();
		foreach ($selectspecs as $selectkey => $selectspec) {
			$outresults[$selectkey] = array();
		}

		foreach ($rawresults as $rawresult) {
			$selectkey = $rawresult['selectkey'];
			$selectspec = $selectspecs[$selectkey];

			$keepresult = array();
			foreach ($selectspec['outcolumns'] as $columnas => $columnfrom) {
				$keepresult[$columnas] = $rawresult[$columnas];
			}

			if (isset($selectspec['arraykey'])) {
				$outresults[$selectkey][$keepresult[$selectspec['arraykey']]] = $keepresult;
			} else {
				$outresults[$selectkey][] = $keepresult;
			}
		}

		// Post-processing to apply various stuff include sorting request, since we can't rely on ORDER BY due to UNION

		foreach ($selectspecs as $selectkey => $selectspec) {
			$this->formatSelect($outresults[$selectkey], $selectspec);
		}

		return $outresults;
	}

	/**
	 * Return the results of all the SELECT operations specified by the supplied selectspec parameters, while also
	 * performing all pending selects that have not yet been executed. If only one parameter is supplied, return its
	 * result, otherwise return an array of results indexed as per the parameters.
	 * @return mixed
	 */
	public function selectWithPending() // any number of parameters read via func_get_args()
	{
		require_once QA_INCLUDE_DIR . 'app/options.php';

		$selectspecs = func_get_args();
		$singleresult = (count($selectspecs) == 1);
		$outresults = array();

		foreach ($selectspecs as $key => $selectspec) { // can pass null parameters
			if (empty($selectspec)) {
				unset($selectspecs[$key]);
				$outresults[$key] = null;
			}
		}

275 276 277
		foreach ($this->pendingSelects as $pendingid => $selectspec) {
			if (!isset($this->pendingResults[$pendingid])) {
				$selectspecs['pending_' . $pendingid] = $selectspec;
278 279 280 281 282
			}
		}

		$outresults = $outresults + $this->multiSelect($selectspecs);

283 284 285 286
		foreach ($this->pendingSelects as $pendingid => $selectspec) {
			if (!isset($this->pendingResults[$pendingid])) {
				$this->pendingResults[$pendingid] = $outresults['pending_' . $pendingid];
				unset($outresults['pending_' . $pendingid]);
287 288 289 290 291 292
			}
		}

		return $singleresult ? $outresults[0] : $outresults;
	}

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	/**
	 * Queue a $selectspec for running later, with $pendingid (used for retrieval)
	 * @param string $pendingid
	 * @param array $selectspec
	 */
	public function queuePending($pendingid, $selectspec)
	{
		$this->pendingSelects[$pendingid] = $selectspec;
	}

	/**
	 * Get the result of the queued SELECT query identified by $pendingid. Run the query if it hasn't run already. If
	 * $selectspec is supplied, it doesn't matter if this hasn't been queued before - it will be queued and run now.
	 * @param string $pendingid
	 * @param array|null $selectspec
	 * @return mixed
	 */
	public function getPendingResult($pendingid, $selectspec = null)
	{
		if (isset($selectspec)) {
			$this->queuePending($pendingid, $selectspec);
		} elseif (!isset($this->pendingSelects[$pendingid])) {
			qa_fatal_error('Pending query was never set up: ' . $pendingid);
		}

		if (!isset($this->pendingResults[$pendingid])) {
			$this->selectWithPending();
		}

		return $this->pendingResults[$pendingid];
	}

	/**
	 * Remove the results of queued SELECT query identified by $pendingid if it has already been run. This means it will
327
	 * run again if its results are requested via getPendingResult()
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	 * @param string $pendingid
	 */
	public function flushPendingResult($pendingid)
	{
		unset($this->pendingResults[$pendingid]);
	}

	/**
	 * Modify a selectspec to count the number of items. This assumes the original selectspec does not have a LIMIT clause.
	 * Currently works with message inbox/outbox functions and user-flags function.
	 * @param array $selectspec
	 * @return array
	 */
	public function selectspecWithCount($selectspec)
	{
		$selectspec['columns'] = array('count' => 'COUNT(*)');
		$selectspec['single'] = true;
		unset($selectspec['arraykey']);

		return $selectspec;
	}


351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	/**
	 * Post-process $outresult according to $selectspec, applying 'sortasc', 'sortdesc', 'arrayvalue' and 'single'.
	 * @param array $outresult
	 * @param array $selectspec
	 */
	private function formatSelect(array &$outresult, array $selectspec)
	{
		// PHP's sorting algorithm is not 'stable', so we use '_order_' element to keep stability.
		// By contrast, MySQL's ORDER BY does seem to give the results in a reliable order.

		if (isset($selectspec['sortasc'])) {
			require_once QA_INCLUDE_DIR . 'util/sort.php';

			$index = 0;
			foreach ($outresult as $key => $value) {
				$outresult[$key]['_order_'] = $index++;
			}

			qa_sort_by($outresult, $selectspec['sortasc'], '_order_');
		} elseif (isset($selectspec['sortdesc'])) {
			require_once QA_INCLUDE_DIR . 'util/sort.php';

			if (isset($selectspec['sortdesc_2'])) {
				qa_sort_by($outresult, $selectspec['sortdesc'], $selectspec['sortdesc_2']);
			} else {
				$index = count($outresult);
				foreach ($outresult as $key => $value) {
					$outresult[$key]['_order_'] = $index--;
				}

				qa_sort_by($outresult, $selectspec['sortdesc'], '_order_');
			}

			$outresult = array_reverse($outresult, true);
		}

		if (isset($selectspec['arrayvalue'])) {
			foreach ($outresult as $key => $value) {
				$outresult[$key] = $value[$selectspec['arrayvalue']];
			}
		}

		if (@$selectspec['single']) {
			$outresult = count($outresult) ? reset($outresult) : null;
		}
	}
397
}