Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify SQLite

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1483 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information...
commit e780f9bb05adeee6410aa6d6cdb5776a299fdb6c 1 parent b649373
jakubvrana authored
Showing with 28 additions and 57 deletions.
  1. +28 −56 adminer/drivers/sqlite.inc.php
  2. +0 −1  todo.txt
View
84 adminer/drivers/sqlite.inc.php
@@ -17,19 +17,11 @@
class Min_SQLite {
var $extension = "SQLite", $server_info, $affected_rows, $error, $_link;
- function __construct() {
+ function Min_SQLite($filename) {
$this->server_info = sqlite_libversion();
- $this->_link = new SQLiteDatabase(":memory:");
- }
-
- function open($filename) {
$this->_link = new SQLiteDatabase($filename);
}
- function close() {
- $this->_link = null;
- }
-
function query($query, $unbuffered = false) {
$method = ($unbuffered ? "unbufferedQuery" : "query");
$result = @$this->_link->$method($query, SQLITE_BOTH, $error);
@@ -60,7 +52,7 @@ function result($query, $field = 0) {
class Min_Result {
var $_result, $_offset = 0, $num_rows;
- function __construct($result) {
+ function Min_Result($result) {
$this->_result = $result;
if (method_exists($result, 'numRows')) { // not available in unbuffered query
$this->num_rows = $result->numRows();
@@ -104,20 +96,12 @@ function fetch_field() {
class Min_SQLite {
var $extension = "SQLite3", $server_info, $affected_rows, $error, $_link;
- function __construct() {
- $this->_link = new SQLite3(":memory:"); // required to display variables
+ function Min_SQLite($filename) {
+ $this->_link = new SQLite3($filename);
$version = $this->_link->version();
$this->server_info = $version["versionString"];
}
- function open($filename) {
- $this->_link = new SQLite3($filename);
- }
-
- function close() {
- $this->_link->close();
- }
-
function query($query) {
$result = @$this->_link->query($query);
if (!$result) {
@@ -147,7 +131,7 @@ function result($query, $field = 0) {
class Min_Result {
var $_result, $_offset = 0, $num_rows;
- function __construct($result) {
+ function Min_Result($result) {
$this->_result = $result;
$this->num_rows = 1; //!
}
@@ -181,22 +165,8 @@ function __desctruct() {
class Min_SQLite extends Min_PDO {
var $extension = "PDO_SQLite";
- function __construct() {
- $this->dsn(DRIVER . "::memory:", "", "");
- }
-
- function open($filename) {
- static $connected = false;
- if ($connected) {
- return true;
- }
- $connected = true;
+ function Min_SQLite($filename) {
$this->dsn(DRIVER . ":$filename", "", "");
- return true;
- }
-
- function close() {
- // no known way
}
}
@@ -204,14 +174,16 @@ function close() {
class Min_DB extends Min_SQLite {
+ function Min_DB() {
+ $this->Min_SQLite(":memory:");
+ }
+
function select_db($filename) {
- if (!is_readable($filename)) { //! verify database format
- return false;
+ if (is_readable($filename) && $this->query("ATTACH " . $this->quote(ereg("(^[/\\]|:)", $filename) ? $filename : dirname($_SERVER["SCRIPT_FILENAME"]) . "/$filename") . " AS a")) { // is_readable - SQLite 3
+ $this->Min_SQLite($filename);
+ return true;
}
- set_exception_handler('connect_error'); // try/catch is not compatible with PHP 4
- $this->open($filename);
- restore_exception_handler();
- return true;
+ return false;
}
function multi_query($query) {
@@ -232,10 +204,6 @@ function idf_escape($idf) {
}
function connect() {
- global $connection;
- if ($connection) {
- return $connection; // can connect only once, function to get number of rows doesn't exist anyway
- }
return new Min_DB;
}
@@ -387,20 +355,23 @@ function exact_value($val) {
function create_database($db, $collation) {
global $connection;
- // SQLITE3_OPEN_CREATE is not respected
- // PRAGMA encoding = "UTF-8" is not respected
- if (!file_exists($db) && touch($db)) {
- return true;
+ if (file_exists($db)) {
+ $connection->error = lang('File exists.');
+ return false;
}
- $connection->error = lang('File can not be created.');
- return false;
+ $link = new Min_SQLite($db); //! exception handler
+ $link->query('PRAGMA encoding = "UTF-8"');
+ $link->query('CREATE TABLE adminer (i)'); // otherwise creates empty file
+ $link->query('DROP TABLE adminer');
+ return true;
}
function drop_databases($databases) {
global $connection;
- $connection->close();
+ $connection->Min_SQLite(":memory:"); // to unlock file, doesn't work in PDO on Windows
foreach ($databases as $db) {
- if (!unlink($db)) {
+ if (!@unlink($db)) {
+ $connection->error = lang('File exists.');
return false;
}
}
@@ -409,8 +380,9 @@ function drop_databases($databases) {
function rename_database($name, $collation) {
global $connection;
- $connection->close();
- return rename(DB, $name);
+ $connection->Min_SQLite(":memory:");
+ $connection->error = lang('File exists.');
+ return @rename(DB, $name);
}
function auto_increment() {
View
1  todo.txt
@@ -27,7 +27,6 @@ Add whisperer to fields with foreign key to big table
SQLite:
CSV import - ON DUPLICATE KEY UPDATE
-Use ATTACH for select_db
Export - triggers, CREATE DATABASE
Delimiter in export and SQL command
Please sign in to comment.
Something went wrong with that request. Please try again.