]> git.joonet.de Git - adminer.git/commitdiff
Improve SQLite
authorjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Thu, 22 Apr 2010 23:05:16 +0000 (23:05 +0000)
committerjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Thu, 22 Apr 2010 23:05:16 +0000 (23:05 +0000)
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1482 7c3ca157-0c34-0410-bff1-cbf682f78f5c

adminer/drivers/sqlite.inc.php
adminer/table.inc.php
todo.txt

index 7e1493113c6645644f63e50b84d73895b794270d..fe971fe1adbe7e582899a6e831f2986c8bf2d358 100644 (file)
@@ -15,20 +15,24 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                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 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                } 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 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                        
                }
                
-               class Min_DB extends Min_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 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 {
+               class Min_SQLite extends Min_PDO {
                        var $extension = "PDO_SQLite";
                        
-                       function select_db($filename) {
+                       function __construct() {
+                               $this->dsn(DRIVER . "::memory:", "", "");
+                       }
+                       
+                       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 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
        }
 
        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 (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                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 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
        }
 
        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 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                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 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                        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;
        }
index 6f039c1e9fcc478a37acaff8228c6e04a5c71280..f6c4fbc070364a8d8e63668b9230c3492983f7dd 100644 (file)
@@ -48,7 +48,9 @@ if ($fields) {
                                        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";
                        }
index 6141fbf59b97727c45758c0ae9fd38472af00707..df6d6439d53ca573c38d505495096c9bcc3a7919 100644 (file)
--- a/todo.txt
+++ b/todo.txt
@@ -26,11 +26,9 @@ Rank, Tree structure
 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 @@ Table schema
 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