2012-03-15 09:25:18 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
|
|
|
* Free Query Builder / Database Abstraction Layer
|
|
|
|
*
|
2012-04-20 13:17:39 -04:00
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren
|
2014-01-02 12:36:50 -05:00
|
|
|
* @copyright Copyright (c) 2012 - 2014
|
2012-03-15 09:25:18 -04:00
|
|
|
* @link https://github.com/aviat4ion/Query
|
2012-04-20 13:17:39 -04:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
namespace Query\Driver;
|
|
|
|
|
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
|
|
|
*
|
2012-04-20 13:17:39 -04:00
|
|
|
* @package Query
|
|
|
|
* @subpackage Drivers
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2014-03-26 20:49:33 -04:00
|
|
|
class SQLite extends Abstract_Driver {
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2012-04-19 11:42:50 -04:00
|
|
|
/**
|
|
|
|
* Reference to the last executed sql query
|
2012-04-23 13:28:49 -04:00
|
|
|
*
|
|
|
|
* @var PDOStatement
|
2012-04-19 11:42:50 -04:00
|
|
|
*/
|
2012-03-15 09:25:18 -04:00
|
|
|
protected $statement;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2014-03-26 20:49:33 -04:00
|
|
|
* @param array $driver_options
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2014-03-26 20:49:33 -04:00
|
|
|
public function __construct($dsn, $user=NULL, $pass=NULL, array $driver_options=array())
|
|
|
|
{
|
2012-03-15 09:25:18 -04:00
|
|
|
// DSN is simply `sqlite:/path/to/db`
|
|
|
|
parent::__construct("sqlite:{$dsn}", $user, $pass);
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Empty a table
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
*/
|
|
|
|
public function truncate($table)
|
|
|
|
{
|
|
|
|
// SQLite has a TRUNCATE optimization,
|
|
|
|
// but no support for the actual command.
|
|
|
|
$sql = 'DELETE FROM "'.$table.'"';
|
|
|
|
|
|
|
|
$this->statement = $this->query($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
return $this->statement;
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
public function get_tables()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2013-12-09 20:11:49 -05:00
|
|
|
$sql = $this->sql->table_list();
|
2012-03-15 09:25:18 -04:00
|
|
|
|
|
|
|
$res = $this->query($sql);
|
2014-04-02 17:08:50 -04:00
|
|
|
return db_filter($res->fetchAll(\PDO::FETCH_ASSOC), 'name');
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
|
|
|
|
2012-04-03 16:54:33 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-15 16:15:08 -04:00
|
|
|
/**
|
|
|
|
* Retrieve foreign keys for the table
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_fks($table)
|
|
|
|
{
|
|
|
|
$return_rows = array();
|
|
|
|
$rows = parent::get_fks($table);
|
|
|
|
|
|
|
|
foreach($rows as $row)
|
|
|
|
{
|
|
|
|
$return_rows[] = array(
|
|
|
|
'child_column' => $row['from'],
|
|
|
|
'parent_table' => $row['table'],
|
|
|
|
'parent_column' => $row['to']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return_rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-04-03 14:44:03 -04:00
|
|
|
public function insert_batch($table, $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
|
|
|
|
if (version_compare($this->getAttribute(\PDO::ATTR_SERVER_VERSION), '3.7.11', '>='))
|
|
|
|
{
|
|
|
|
return parent::insert_batch($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
|
|
|
|
if ( ! is_array(current($data))) return NULL;
|
|
|
|
|
|
|
|
// Start the block of sql statements
|
|
|
|
$table = $this->quote_table($table);
|
|
|
|
$sql = "INSERT INTO {$table} \n";
|
|
|
|
|
|
|
|
// Create a key-value mapping for each field
|
|
|
|
$first = array_shift($data);
|
|
|
|
$cols = array();
|
|
|
|
foreach($first as $colname => $datum)
|
|
|
|
{
|
|
|
|
$cols[] = $this->_quote($datum) . ' AS ' . $this->quote_ident($colname);
|
|
|
|
}
|
|
|
|
$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
|
|
|
{
|
2014-04-03 16:49:01 -04:00
|
|
|
$vals = array_map(array($this, 'quote'), $union);
|
2014-04-03 14:44:03 -04:00
|
|
|
$sql .= "UNION SELECT " . implode(',', $vals) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($sql, NULL);
|
|
|
|
}
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2014-02-07 14:16:39 -05:00
|
|
|
//End of sqlite_driver.php
|