Query/drivers/sqlite/sqlite_driver.php

129 lines
2.6 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
2013-01-02 14:26:42 -05:00
* @copyright Copyright (c) 2012 - 2013
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
*/
// --------------------------------------------------------------------------
/**
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 DB_PDO {
/**
* 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
2012-03-15 09:25:18 -04:00
*/
public function __construct($dsn, $user=NULL, $pass=NULL)
{
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
{
2012-03-15 09:25:18 -04:00
$tables = array();
$sql = <<<SQL
2012-04-10 14:06:34 -04:00
SELECT "name"
FROM "sqlite_master"
2012-03-15 09:25:18 -04:00
WHERE "type"='table'
2012-04-10 14:06:34 -04:00
ORDER BY "name" DESC
2012-03-15 09:25:18 -04:00
SQL;
$res = $this->query($sql);
2012-04-10 14:06:34 -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
// --------------------------------------------------------------------------
2012-03-15 09:25:18 -04:00
/**
* List system tables for the current database
2012-04-10 14:06:34 -04:00
*
2012-03-15 09:25:18 -04:00
* @return array
*/
public function get_system_tables()
{
//SQLite only has the sqlite_master table
// that is of any importance.
return array('sqlite_master');
}
2012-04-10 14:06:34 -04:00
2012-03-15 09:25:18 -04:00
// --------------------------------------------------------------------------
/**
* Load a database for the current connection
2012-04-10 14:06:34 -04:00
*
2012-03-15 09:25:18 -04:00
* @param string $db
2012-04-10 14:06:34 -04:00
* @param string $name
2012-03-15 09:25:18 -04:00
*/
public function load_database($db, $name)
{
$sql = 'ATTACH DATABASE "'.$db.'" AS "'.$name.'"';
$this->query($sql);
}
2012-04-10 14:06:34 -04:00
2012-03-15 09:25:18 -04:00
// --------------------------------------------------------------------------
/**
* Unload a database from the current connection
2012-04-10 14:06:34 -04:00
*
2012-03-15 09:25:18 -04:00
* @param string $name
*/
public function unload_database($name)
{
$sql = 'DETACH DATABASE ":name"';
2012-04-10 14:06:34 -04:00
2012-03-15 09:25:18 -04:00
$this->prepare_query($sql, array(
':name' => $name,
));
$this->statement->execute();
}
}
2012-04-10 14:06:34 -04:00
//End of sqlite_driver.php