Query/src/Drivers/Sqlite/Driver.php

147 lines
3.3 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
2012-03-15 09:25:18 -04:00
/**
* Query
*
2016-09-07 13:17:17 -04:00
* SQL Query Builder / Database Abstraction Layer
2012-03-15 09:25:18 -04:00
*
2022-09-29 11:33:08 -04:00
* PHP version 8.1
2016-09-07 13:17:17 -04:00
*
* @package Query
2023-01-20 11:30:51 -05:00
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 Timothy J. Warren
2016-09-07 13:17:17 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-11 16:49:42 -05:00
* @link https://git.timshomepage.net/aviat/Query
2023-03-17 16:34:21 -04:00
* @version 4.1.0
2012-03-15 09:25:18 -04:00
*/
2014-04-02 17:08:50 -04:00
2023-03-17 15:18:33 -04:00
namespace Query\Drivers\Sqlite;
2019-12-11 16:49:06 -05:00
use InvalidArgumentException;
2016-10-12 22:12:25 -04:00
use PDO;
use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
2016-10-13 21:55:23 -04:00
use Query\Drivers\AbstractDriver;
2023-03-17 15:18:33 -04:00
use function is_array;
2016-09-07 17:39:19 -04:00
2012-03-15 09:25:18 -04:00
/**
2012-04-10 14:06:34 -04:00
* SQLite specific class
2012-03-15 09:25:18 -04:00
*/
2023-03-17 15:18:33 -04:00
class Driver extends AbstractDriver
{
/**
* SQLite has a truncate optimization,
* but no support for the actual keyword
*/
protected bool $hasTruncate = FALSE;
2012-03-15 09:25:18 -04:00
/**
* Open SQLite Database
*/
2023-03-17 15:18:33 -04:00
public function __construct(string $dsn, ?string $user=NULL, ?string $pass=NULL, array $driverOptions=[])
{
if ( ! str_contains($dsn, 'sqlite:'))
2015-11-11 09:25:21 -05:00
{
$dsn = "sqlite:{$dsn}";
}
2015-07-20 15:24:21 -04:00
parent::__construct($dsn, $user, $pass);
2012-03-15 09:25:18 -04:00
}
2012-04-10 14:06:34 -04:00
2019-12-11 16:49:06 -05:00
/**
* Return list of dbs for the current connection, if possible. Meaningless for SQLite.
*/
public function getDbs(): ?array
{
return NULL;
}
2012-03-15 09:25:18 -04:00
/**
* List tables for the current database
*/
2018-01-22 15:43:56 -05:00
public function getTables(): array
2012-04-10 14:06:34 -04:00
{
$sql = $this->getSql()->tableList();
2012-03-15 09:25:18 -04:00
$res = $this->query($sql);
2023-03-17 15:18:33 -04:00
2018-01-24 13:14:03 -05:00
return dbFilter($res->fetchAll(PDO::FETCH_ASSOC), 'name');
2012-03-15 09:25:18 -04:00
}
/**
* Retrieve foreign keys for the table
*/
public function getFks(string $table): array
{
2016-10-13 21:55:23 -04:00
$returnRows = [];
2023-03-17 15:18:33 -04:00
foreach (parent::getFks($table) as $row)
{
2016-10-13 21:55:23 -04:00
$returnRows[] = [
'child_column' => $row['from'],
'parent_table' => $row['table'],
'parent_column' => $row['to'],
'update' => $row['on_update'],
2023-03-17 15:18:33 -04:00
'delete' => $row['on_delete'],
2016-09-07 13:10:03 -04:00
];
}
2016-10-13 21:55:23 -04:00
return $returnRows;
}
/**
* Create sql for batch insert
*
2023-03-17 15:18:33 -04:00
* @return array[]|null[]|string[]
*/
#[CodeCoverageIgnore]
2018-01-24 13:14:03 -05:00
public function insertBatch(string $table, array $data=[]): array
{
// If greater than version 3.7.11, supports the same syntax as
// MySQL and Postgres
if (version_compare($this->getVersion(), '3.7.11', '>='))
{
2016-10-13 21:55:23 -04:00
return parent::insertBatch($table, $data);
}
// --------------------------------------------------------------------------
// Otherwise, do a union query as an analogue to a 'proper' batch insert
// --------------------------------------------------------------------------
// Each member of the data array needs to be an array
2019-12-11 16:49:06 -05:00
if ( ! is_array(current($data)))
2015-11-11 09:25:21 -05:00
{
2019-12-11 16:49:06 -05:00
throw new InvalidArgumentException('$data must be an array of arrays');
2015-11-11 09:25:21 -05:00
}
// Start the block of sql statements
2016-10-13 21:55:23 -04:00
$table = $this->quoteTable($table);
$sql = "INSERT INTO {$table} \n";
// Create a key-value mapping for each field
$first = array_shift($data);
2016-09-07 13:10:03 -04:00
$cols = [];
2023-03-17 15:18:33 -04:00
foreach ($first as $colName => $datum)
{
2019-12-11 16:49:06 -05:00
$cols[] = $this->_quote($datum) . ' AS ' . $this->quoteIdent($colName);
}
$sql .= 'SELECT ' . implode(', ', $cols) . "\n";
2023-03-17 15:18:33 -04:00
foreach ($data as $union)
{
2016-09-07 13:10:03 -04:00
$vals = array_map([$this, 'quote'], $union);
$sql .= 'UNION SELECT ' . implode(',', $vals) . "\n";
}
2016-09-07 13:10:03 -04:00
return [$sql, NULL];
}
/**
* Generate the returning clause for the current database
*/
public function returning(string $query, string $select): string
{
// Return the same query, as the returning clause is not supported
return $query;
}
2023-03-17 15:18:33 -04:00
}