Query/drivers/sqlite/sqlite_driver.php

153 lines
3.5 KiB
PHP
Raw Normal View History

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
*/
class SQLite extends Abstract_Driver {
2012-03-15 09:25:18 -04:00
/**
* Reference to the last executed sql query
2012-04-23 13:28:49 -04:00
*
* @var PDOStatement
*/
2012-03-15 09:25:18 -04:00
protected $statement;
/**
* Open SQLite Database
2012-04-10 14:06:34 -04:00
*
* @param string $dsn
* @param string $user
* @param string $pass
* @param array $driver_options
2012-03-15 09:25:18 -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
// --------------------------------------------------------------------------
/**
* 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;
}
// --------------------------------------------------------------------------
/**
* Create sql for batch insert
*
* @codeCoverageIgnore
* @param string $table
* @param array $data
* @return string
*/
public function insert_batch($table, $data=array())
{
// 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";
foreach($data as $union)
{
$vals = array_map(array($this, 'quote'), $union);
$sql .= "UNION SELECT " . implode(',', $vals) . "\n";
}
return array($sql, NULL);
}
2012-03-15 09:25:18 -04:00
}
//End of sqlite_driver.php