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
|
|
|
*
|
2018-01-19 13:43:19 -05:00
|
|
|
* PHP version 7.1
|
2016-09-07 13:17:17 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2018-01-19 13:43:19 -05:00
|
|
|
* @copyright 2012 - 2018 Timothy J. Warren
|
2016-09-07 13:17:17 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link https://git.timshomepage.net/aviat4ion/Query
|
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
|
|
|
|
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
|
2016-09-07 13:10:03 -04:00
|
|
|
* @var boolean
|
2014-04-24 16:25:04 -04:00
|
|
|
*/
|
2016-10-13 21:55:23 -04:00
|
|
|
protected $hasTruncate = FALSE;
|
2014-04-24 16:25:04 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Open SQLite Database
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
|
|
|
* @param string $dsn
|
2012-04-19 11:42:50 -04:00
|
|
|
* @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=[])
|
2014-03-26 20:49:33 -04:00
|
|
|
{
|
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
|
|
|
|
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
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$sql = $this->sql->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
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getFks($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
|
2013-05-03 13:07:34 -04:00
|
|
|
* @param string $table
|
|
|
|
* @param array $data
|
2018-01-24 13:14:03 -05:00
|
|
|
* @return array
|
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
|
2016-10-12 22:12:25 -04:00
|
|
|
if (version_compare($this->getAttribute(PDO::ATTR_SERVER_VERSION), '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
|
2018-01-24 13:14:03 -05:00
|
|
|
if ( ! \is_array(current($data)))
|
2015-11-11 09:25:21 -05:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
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 = [];
|
2014-04-03 14:44:03 -04:00
|
|
|
foreach($first as $colname => $datum)
|
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$cols[] = $this->_quote($datum) . ' AS ' . $this->quoteIdent($colname);
|
2014-04-03 14:44:03 -04:00
|
|
|
}
|
|
|
|
$sql .= "SELECT " . implode(', ', $cols) . "\n";
|
|
|
|
|
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);
|
2014-04-03 14:44:03 -04:00
|
|
|
$sql .= "UNION SELECT " . implode(',', $vals) . "\n";
|
|
|
|
}
|
|
|
|
|
2016-09-07 13:10:03 -04:00
|
|
|
return [$sql, NULL];
|
2014-04-03 14:44:03 -04:00
|
|
|
}
|
2016-10-13 21:55:23 -04:00
|
|
|
}
|