Query/drivers/sqlite/sqlite_driver.php

142 lines
2.8 KiB
PHP
Raw Normal View History

2012-03-15 09:25:18 -04:00
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
2012-04-10 14:06:34 -04:00
* SQLite specific class
2012-03-15 09:25:18 -04:00
*
* @extends DB_PDO
*/
class SQLite extends DB_PDO {
protected $statement;
/**
* Open SQLite Database
2012-04-10 14:06:34 -04:00
*
* @param string $dsn
2012-03-15 09:25:18 -04:00
*/
public function __construct($dsn, $user=NULL, $pass=NULL)
{
// DSN is simply `sqlite:/path/to/db`
parent::__construct("sqlite:{$dsn}", $user, $pass);
}
2012-04-10 14:06:34 -04:00
// --------------------------------------------------------------------------
/**
* Doesn't apply to sqlite
*/
public function switch_db($name)
{
return FALSE;
}
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
2012-03-15 09:25:18 -04:00
// --------------------------------------------------------------------------
/**
* Return the number of rows returned for a SELECT query
2012-04-10 14:06:34 -04:00
*
2012-03-15 09:25:18 -04:00
* @return int
*/
public function num_rows()
{
return (isset($this->statement)) ? $this->statement->rowCount() : FALSE;
2012-03-15 09:25:18 -04:00
}
}
2012-04-10 14:06:34 -04:00
//End of sqlite_driver.php