[adminer-svn] SF.net SVN: adminer:[1482] trunk
Database management in a single PHP file
Brought to you by:
jakubvrana
From: <jak...@us...> - 2010-04-22 23:05:22
|
Revision: 1482 http://adminer.svn.sourceforge.net/adminer/?rev=1482&view=rev Author: jakubvrana Date: 2010-04-22 23:05:16 +0000 (Thu, 22 Apr 2010) Log Message: ----------- Improve SQLite Modified Paths: -------------- trunk/adminer/drivers/sqlite.inc.php trunk/adminer/table.inc.php trunk/todo.txt Modified: trunk/adminer/drivers/sqlite.inc.php =================================================================== --- trunk/adminer/drivers/sqlite.inc.php 2010-04-22 23:03:54 UTC (rev 1481) +++ trunk/adminer/drivers/sqlite.inc.php 2010-04-22 23:05:16 UTC (rev 1482) @@ -15,20 +15,24 @@ if (isset($_GET["sqlite2"])) { class Min_SQLite { - var $extension = "SQLite", $server_info, $affected_rows, $error, $_connection; + var $extension = "SQLite", $server_info, $affected_rows, $error, $_link; function __construct() { $this->server_info = sqlite_libversion(); - $this->_connection = new SQLiteDatabase(":memory:"); + $this->_link = new SQLiteDatabase(":memory:"); } function open($filename) { - $this->_connection = new SQLiteDatabase($filename); + $this->_link = new SQLiteDatabase($filename); } + function close() { + $this->_link = null; + } + function query($query, $unbuffered = false) { $method = ($unbuffered ? "unbufferedQuery" : "query"); - $result = @$this->_connection->$method($query, SQLITE_BOTH, $error); + $result = @$this->_link->$method($query, SQLITE_BOTH, $error); if (!$result) { $this->error = $error; return false; @@ -98,32 +102,36 @@ } else { class Min_SQLite { - var $extension = "SQLite3", $server_info, $affected_rows, $error, $_connection; + var $extension = "SQLite3", $server_info, $affected_rows, $error, $_link; function __construct() { - $this->_connection = new SQLite3(":memory:"); // required to display variables - $version = $this->_connection->version(); + $this->_link = new SQLite3(":memory:"); // required to display variables + $version = $this->_link->version(); $this->server_info = $version["versionString"]; } function open($filename) { - $this->_connection = new SQLite3($filename); + $this->_link = new SQLite3($filename); } + function close() { + $this->_link->close(); + } + function query($query) { - $result = @$this->_connection->query($query); + $result = @$this->_link->query($query); if (!$result) { - $this->error = $this->_connection->lastErrorMsg(); + $this->error = $this->_link->lastErrorMsg(); return false; } elseif ($result->numColumns()) { return new Min_Result($result); } - $this->affected_rows = $this->_connection->changes(); + $this->affected_rows = $this->_link->changes(); return true; } function quote($string) { - return "'" . $this->_connection->escapeString($string) . "'"; + return "'" . $this->_link->escapeString($string) . "'"; } function result($query, $field = 0) { @@ -169,46 +177,56 @@ } - class Min_DB extends Min_SQLite { + } elseif (extension_loaded("pdo_sqlite")) { + class Min_SQLite extends Min_PDO { + var $extension = "PDO_SQLite"; - function select_db($filename) { - set_exception_handler('connect_error'); // try/catch is not compatible with PHP 4 - $this->open($filename); - restore_exception_handler(); - return true; + function __construct() { + $this->dsn(DRIVER . "::memory:", "", ""); } - function multi_query($query) { - return $this->_result = $this->query($query); - } - - function store_result() { - return $this->_result; - } - - function next_result() { - return false; - } - } - - } elseif (extension_loaded("pdo_sqlite")) { - class Min_DB extends Min_PDO { - var $extension = "PDO_SQLite"; - - function select_db($filename) { + function open($filename) { static $connected = false; if ($connected) { return true; } $connected = true; - $this->dsn(DRIVER . ":$filename", "", "", "connect_error"); - //! $this->server_info needs to be filled in __construct() + $this->dsn(DRIVER . ":$filename", "", ""); return true; } + + function close() { + // no known way + } } } + class Min_DB extends Min_SQLite { + + function select_db($filename) { + if (!is_readable($filename)) { //! verify database format + return false; + } + set_exception_handler('connect_error'); // try/catch is not compatible with PHP 4 + $this->open($filename); + restore_exception_handler(); + return true; + } + + function multi_query($query) { + return $this->_result = $this->query($query); + } + + function store_result() { + return $this->_result; + } + + function next_result() { + return false; + } + } + function idf_escape($idf) { return '"' . str_replace('"', '""', $idf) . '"'; } @@ -235,7 +253,8 @@ } function db_collation($db, $collations) { - return null; + global $connection; + return $connection->result("PRAGMA encoding"); // there is no database list so $db == DB } function engines() { @@ -332,6 +351,7 @@ if (is_object($result)) { while ($row = $result->fetch_assoc()) { $foreign_key = &$return[$row["id"]]; + //! idf_unescape in SQLite2 if (!$foreign_key) { $foreign_key = $row; } @@ -348,7 +368,7 @@ } function collations() { - return get_vals("PRAGMA collation_list", 1); + return (isset($_GET["create"]) ? get_vals("PRAGMA collation_list", 1) : array()); } function information_schema($db) { @@ -365,9 +385,31 @@ return $connection->quote($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; + } + $connection->error = lang('File can not be created.'); + return false; + } + + function drop_databases($databases) { + global $connection; + $connection->close(); + foreach ($databases as $db) { + if (!unlink($db)) { + return false; + } + } + return true; + } + function rename_database($name, $collation) { global $connection; - $connection->close(); //! not available with all extensions + $connection->close(); return rename(DB, $name); } @@ -395,7 +437,7 @@ return false; } if ($auto_increment) { - return queries("UPDATE sqlite_sequence SET seq = $auto_increment WHERE name = " . $connection->quote($name) . ""); + queries("UPDATE sqlite_sequence SET seq = $auto_increment WHERE name = " . $connection->quote($name)); // ignores error } return true; } Modified: trunk/adminer/table.inc.php =================================================================== --- trunk/adminer/table.inc.php 2010-04-22 23:03:54 UTC (rev 1481) +++ trunk/adminer/table.inc.php 2010-04-22 23:05:16 UTC (rev 1482) @@ -48,7 +48,9 @@ echo "<th><i>" . implode("</i>, <i>", array_map('h', $foreign_key["source"])) . "</i>"; echo "<td><a href='" . h($foreign_key["db"] != "" ? preg_replace('~db=[^&]*~', "db=" . urlencode($foreign_key["db"]), ME) : ME) . "table=" . urlencode($foreign_key["table"]) . "'>$link</a>"; echo "(<em>" . implode("</em>, <em>", array_map('h', $foreign_key["target"])) . "</em>)"; - echo '<td><a href="' . h(ME . 'foreign=' . urlencode($TABLE) . '&name=' . urlencode($name)) . '">' . lang('Alter') . '</a>'; + if ($driver != "sqlite") { + echo '<td><a href="' . h(ME . 'foreign=' . urlencode($TABLE) . '&name=' . urlencode($name)) . '">' . lang('Alter') . '</a>'; + } } echo "</table>\n"; } Modified: trunk/todo.txt =================================================================== --- trunk/todo.txt 2010-04-22 23:03:54 UTC (rev 1481) +++ trunk/todo.txt 2010-04-22 23:05:16 UTC (rev 1482) @@ -26,11 +26,9 @@ Add whisperer to fields with foreign key to big table SQLite: -CREATE DATABASE - PRAGMA encoding = "UTF-8" -Detecion of non-existing database -DROP DATABASE by file operations CSV import - ON DUPLICATE KEY UPDATE -Export - views, triggers +Use ATTACH for select_db +Export - triggers, CREATE DATABASE Delimiter in export and SQL command PostgreSQL: @@ -40,8 +38,10 @@ Export - http://www.postgresql.org/docs/8.4/static/functions-info.html Table status - http://www.postgresql.org/docs/8.4/static/functions-admin.html Column rights - http://www.postgresql.org/docs/8.4/static/functions-info.html +Dollar terminated string in SQL command Move table - ALTER TABLE SET SCHEMA bool in Editor +Check PDO driver MS SQL: Rename by sp_rename This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |