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
|
|
|
*
|
2020-04-10 20:54:31 -04:00
|
|
|
* PHP version 7.4
|
2016-09-07 13:17:17 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-03-18 11:31:56 -04:00
|
|
|
* @copyright 2012 - 2020 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
|
|
|
|
* @version 3.0.0
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2015-07-16 16:56:13 -04:00
|
|
|
namespace Query\Drivers\Sqlite;
|
2014-04-02 17:08:50 -04:00
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
use function is_array;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2016-10-12 22:12:25 -04:00
|
|
|
use PDO;
|
2016-10-13 21:55:23 -04:00
|
|
|
use Query\Drivers\AbstractDriver;
|
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
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
class Driver extends AbstractDriver {
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2014-04-24 16:25:04 -04:00
|
|
|
/**
|
|
|
|
* SQLite has a truncate optimization,
|
|
|
|
* but no support for the actual keyword
|
|
|
|
*/
|
2020-04-10 20:53:52 -04:00
|
|
|
protected bool $hasTruncate = FALSE;
|
2014-04-24 16:25:04 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Open SQLite Database
|
|
|
|
*/
|
2018-01-24 13:17:00 -05:00
|
|
|
public function __construct(string $dsn, string $user=NULL, string $pass=NULL, array $driverOptions=[])
|
2014-03-26 20:49:33 -04:00
|
|
|
{
|
2022-09-29 10:18:26 -04:00
|
|
|
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
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2022-09-29 10:18:26 -04:00
|
|
|
* @return mixed[]
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getTables(): array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2020-04-17 14:57:56 -04:00
|
|
|
$sql = $this->getSql()->tableList();
|
2012-03-15 09:25:18 -04:00
|
|
|
$res = $this->query($sql);
|
2018-01-24 13:14:03 -05:00
|
|
|
return dbFilter($res->fetchAll(PDO::FETCH_ASSOC), 'name');
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
|
|
|
|
2014-04-15 16:15:08 -04:00
|
|
|
/**
|
|
|
|
* Retrieve foreign keys for the table
|
|
|
|
*
|
2022-09-29 10:18:26 -04:00
|
|
|
* @return array<int, array{child_column: mixed, parent_table: mixed, parent_column: mixed, update: mixed, delete: mixed}>
|
2014-04-15 16:15:08 -04:00
|
|
|
*/
|
2022-09-29 10:18:26 -04:00
|
|
|
public function getFks(string $table): array
|
2014-04-15 16:15:08 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$returnRows = [];
|
2014-04-15 16:15:08 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
foreach(parent::getFks($table) as $row)
|
2014-04-15 16:15:08 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$returnRows[] = [
|
2014-04-15 16:15:08 -04:00
|
|
|
'child_column' => $row['from'],
|
|
|
|
'parent_table' => $row['table'],
|
2014-04-17 16:41:12 -04:00
|
|
|
'parent_column' => $row['to'],
|
|
|
|
'update' => $row['on_update'],
|
|
|
|
'delete' => $row['on_delete']
|
2016-09-07 13:10:03 -04:00
|
|
|
];
|
2014-04-15 16:15:08 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
return $returnRows;
|
2014-04-15 16:15:08 -04:00
|
|
|
}
|
|
|
|
|
2014-03-26 20:49:33 -04:00
|
|
|
/**
|
2013-05-03 13:07:34 -04:00
|
|
|
* Create sql for batch insert
|
|
|
|
*
|
2014-04-03 16:49:01 -04:00
|
|
|
* @codeCoverageIgnore
|
2022-09-29 10:18:26 -04:00
|
|
|
* @return mixed[][]|string[]|null[]|string[]|null[]
|
2013-05-03 13:07:34 -04:00
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
public function insertBatch(string $table, array $data=[]): array
|
2013-05-03 13:07:34 -04:00
|
|
|
{
|
2014-04-03 14:44:03 -04:00
|
|
|
// If greater than version 3.7.11, supports the same syntax as
|
|
|
|
// MySQL and Postgres
|
2019-12-13 15:11:49 -05:00
|
|
|
if (version_compare($this->getVersion(), '3.7.11', '>='))
|
2014-04-03 14:44:03 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return parent::insertBatch($table, $data);
|
2014-04-03 14:44:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// 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
|
|
|
}
|
2014-04-03 14:44:03 -04:00
|
|
|
|
|
|
|
// Start the block of sql statements
|
2016-10-13 21:55:23 -04:00
|
|
|
$table = $this->quoteTable($table);
|
2014-04-03 14:44:03 -04:00
|
|
|
$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 = [];
|
2019-12-11 16:49:06 -05:00
|
|
|
foreach($first as $colName => $datum)
|
2014-04-03 14:44:03 -04:00
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
$cols[] = $this->_quote($datum) . ' AS ' . $this->quoteIdent($colName);
|
2014-04-03 14:44:03 -04:00
|
|
|
}
|
2018-01-24 15:03:41 -05:00
|
|
|
$sql .= 'SELECT ' . implode(', ', $cols) . "\n";
|
2014-04-03 14:44:03 -04:00
|
|
|
|
2014-04-03 16:49:01 -04:00
|
|
|
foreach($data as $union)
|
2014-04-03 14:44:03 -04:00
|
|
|
{
|
2016-09-07 13:10:03 -04:00
|
|
|
$vals = array_map([$this, 'quote'], $union);
|
2018-01-24 15:03:41 -05:00
|
|
|
$sql .= 'UNION SELECT ' . implode(',', $vals) . "\n";
|
2014-04-03 14:44:03 -04:00
|
|
|
}
|
|
|
|
|
2016-09-07 13:10:03 -04:00
|
|
|
return [$sql, NULL];
|
2014-04-03 14:44:03 -04:00
|
|
|
}
|
2019-12-13 15:11:49 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2016-10-13 21:55:23 -04:00
|
|
|
}
|