Query/src/Drivers/Sqlite/Driver.php

161 lines
3.6 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
*
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
*/
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
/**
* SQLite has a truncate optimization,
* but no support for the actual keyword
2016-09-07 13:10:03 -04:00
* @var boolean
*/
protected bool $hasTruncate = FALSE;
2012-03-15 09:25:18 -04:00
/**
* Open SQLite Database
2012-04-10 14:06:34 -04:00
*
* @param string $dsn
* @param string $user
* @param string $pass
2016-10-13 21:55:23 -04:00
* @param array $driverOptions
2012-03-15 09:25:18 -04:00
*/
2018-01-24 13:17:00 -05:00
public function __construct(string $dsn, string $user=NULL, string $pass=NULL, array $driverOptions=[])
{
2015-11-11 09:25:21 -05:00
if (strpos($dsn, 'sqlite:') === FALSE)
{
$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.
*
* @return array | null
*/
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
*
2012-03-15 09:25:18 -04:00
* @return mixed
*/
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);
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
*
* @param string $table
* @return array
*/
2018-01-22 15:43:56 -05:00
public function getFks($table): array
{
2016-10-13 21:55:23 -04:00
$returnRows = [];
2016-10-13 21:55:23 -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'],
'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
*
* @codeCoverageIgnore
* @param string $table
* @param array $data
2018-01-24 13:14:03 -05:00
* @return array
*/
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 = [];
2019-12-11 16:49:06 -05: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";
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
*
* @param string $query
* @param string $select
* @return string
*/
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
}