KD2 Framework  Check-in [0feb48c08d]

Overview
Comment:SQLite3 : use variadic params
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 7.3
Files: files | file ages | folders
SHA1: 0feb48c08daea80b9f23f3ee7888bba8161ccc28
User & Date: bohwaz on 2020-06-21 22:59:02
Other Links: branch diff | manifest | tags
Context
2020-06-21
22:59
Mail_Message: add method to get unsubscribe URL from headers check-in: bab2e4909f user: bohwaz tags: 7.3
22:59
SQLite3 : use variadic params check-in: 0feb48c08d user: bohwaz tags: 7.3
22:58
DB: Use variadic params check-in: 1ee358f0d0 user: bohwaz tags: 7.3
Changes

Modified src/lib/KD2/DB/SQLite3.php from [4a70ff3d3f] to [5e3cad3584].

216
217
218
219
220
221
222
223
224
225
226
227
228

229
230
231
232
233
234
235
	 * @return \SQLite3Stmt|boolean Returns a boolean if the query is writing
	 * to the database, or a statement if it's a read-only query.
	 *
	 * The fact that this method returns a boolean is voluntary, to avoid a bug
	 * in SQLite3/PHP where you can re-run a query by calling fetchResult
	 * on a statement. This could cause double writing.
	 */
	public function preparedQuery(string $query, array $args = [])
	{
		assert(is_string($query));
		assert(is_array($args) || is_object($args));

		// Forcer en tableau

		$args = (array) $args;

		$this->connect();

		$query = $this->applyTablePrefix($query);
		$statement = $this->db->prepare($query);








|





>







216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
	 * @return \SQLite3Stmt|boolean Returns a boolean if the query is writing
	 * to the database, or a statement if it's a read-only query.
	 *
	 * The fact that this method returns a boolean is voluntary, to avoid a bug
	 * in SQLite3/PHP where you can re-run a query by calling fetchResult
	 * on a statement. This could cause double writing.
	 */
	public function preparedQuery(string $query, ...$args)
	{
		assert(is_string($query));
		assert(is_array($args) || is_object($args));

		// Forcer en tableau
		if (!is_array($args)) { var_dump($args); exit;}
		$args = (array) $args;

		$this->connect();

		$query = $this->applyTablePrefix($query);
		$statement = $this->db->prepare($query);

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
397
398
399
400
401
402
403
404
405
406
407
		}

		return $out;
	}

	public function iterate(string $statement, ...$args): iterable
	{
		$res = $this->preparedQuery($statement, $args);

		while ($row = $res->fetchArray(\SQLITE3_ASSOC))
		{
			yield (object) $row;
		}

		unset($res);

		return;
	}

	public function get(string $statement, ...$args): array
	{
		$res = $this->preparedQuery($statement, $args);
		$out = [];

		while ($row = $res->fetchArray(\SQLITE3_ASSOC))
		{
			$out[] = (object) $row;
		}

		return $out;
	}

	public function getAssoc(string $statement, ...$args): array
	{
		$res = $this->preparedQuery($statement, $args);
		$out = [];

		while ($row = $res->fetchArray(\SQLITE3_NUM))
		{
			$out[$row[0]] = $row[1];
		}

		return $out;
	}

	public function getGrouped(string $statement, ...$args): array
	{
		$res = $this->preparedQuery($statement, $args);
		$out = [];

		while ($row = $res->fetchArray(\SQLITE3_ASSOC))
		{
			$out[current($row)] = (object) $row;
		}








|













|












|












|







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
397
398
399
400
401
402
403
404
405
406
407
408
		}

		return $out;
	}

	public function iterate(string $statement, ...$args): iterable
	{
		$res = $this->preparedQuery($statement, ...$args);

		while ($row = $res->fetchArray(\SQLITE3_ASSOC))
		{
			yield (object) $row;
		}

		unset($res);

		return;
	}

	public function get(string $statement, ...$args): array
	{
		$res = $this->preparedQuery($statement, ...$args);
		$out = [];

		while ($row = $res->fetchArray(\SQLITE3_ASSOC))
		{
			$out[] = (object) $row;
		}

		return $out;
	}

	public function getAssoc(string $statement, ...$args): array
	{
		$res = $this->preparedQuery($statement, ...$args);
		$out = [];

		while ($row = $res->fetchArray(\SQLITE3_NUM))
		{
			$out[$row[0]] = $row[1];
		}

		return $out;
	}

	public function getGrouped(string $statement, ...$args): array
	{
		$res = $this->preparedQuery($statement, ...$args);
		$out = [];

		while ($row = $res->fetchArray(\SQLITE3_ASSOC))
		{
			$out[current($row)] = (object) $row;
		}

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
	 * @param  string $query
	 * @return object|bool
	 *
	 * Accepts one or more arguments for the prepared query
	 */
	public function first(string $query, ...$args)
	{
		$res = $this->preparedQuery($query, $args);

		$row = $res->fetchArray(\SQLITE3_ASSOC);
		$res->finalize();

		return is_array($row) ? (object) $row : false;
	}

	/**
	 * Runs a query and returns the first column of the first row of the result
	 * @param  string $query
	 * @return object
	 *
	 * Accepts one or more arguments for the prepared query
	 */
	public function firstColumn(string $query, ...$args)
	{
		$res = $this->preparedQuery($query, $args);

		$row = $res->fetchArray(\SQLITE3_NUM);

		return (is_array($row) && count($row) > 0) ? $row[0] : false;
	}

	public function countRows(\SQLite3Result $result): int







|
















|







441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
	 * @param  string $query
	 * @return object|bool
	 *
	 * Accepts one or more arguments for the prepared query
	 */
	public function first(string $query, ...$args)
	{
		$res = $this->preparedQuery($query, ...$args);

		$row = $res->fetchArray(\SQLITE3_ASSOC);
		$res->finalize();

		return is_array($row) ? (object) $row : false;
	}

	/**
	 * Runs a query and returns the first column of the first row of the result
	 * @param  string $query
	 * @return object
	 *
	 * Accepts one or more arguments for the prepared query
	 */
	public function firstColumn(string $query, ...$args)
	{
		$res = $this->preparedQuery($query, ...$args);

		$row = $res->fetchArray(\SQLITE3_NUM);

		return (is_array($row) && count($row) > 0) ? $row[0] : false;
	}

	public function countRows(\SQLite3Result $result): int