Query/src/Drivers/Sqlite/Driver.php

134 lines
3.0 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
*
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
*/
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
/**
* SQLite has a truncate optimization,
* but no support for the actual keyword
2016-09-07 13:10:03 -04:00
* @var boolean
*/
2016-10-13 21:55:23 -04:00
protected $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
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
}
/**
* 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
2016-10-12 22:12:25 -04:00
if (version_compare($this->getAttribute(PDO::ATTR_SERVER_VERSION), '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
2018-01-24 13:14:03 -05:00
if ( ! \is_array(current($data)))
2015-11-11 09:25:21 -05:00
{
return NULL;
}
// 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 = [];
foreach($first as $colname => $datum)
{
2016-10-13 21:55:23 -04: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];
}
2016-10-13 21:55:23 -04:00
}