]> git.joonet.de Git - adminer.git/commitdiff
Finish SQLite
authorJakub Vrana <jakub@vrana.cz>
Thu, 27 May 2010 14:10:17 +0000 (16:10 +0200)
committerJakub Vrana <jakub@vrana.cz>
Thu, 27 May 2010 14:10:17 +0000 (16:10 +0200)
Don't require num_rows

12 files changed:
adminer/create.inc.php
adminer/drivers/mssql.inc.php
adminer/drivers/mysql.inc.php
adminer/drivers/oracle.inc.php
adminer/drivers/sqlite.inc.php
adminer/dump.inc.php
adminer/include/adminer.inc.php
adminer/include/editing.inc.php
adminer/include/export.inc.php
adminer/include/functions.inc.php
adminer/select.inc.php
todo.txt

index 479e5b9bdf1e85eb607d39e229273571a816b2dd..e219c00e0487e5338dbd35cb853be08110923b8c 100644 (file)
@@ -151,7 +151,7 @@ foreach ($engines as $engine) {
 <p>
 <?php echo lang('Table name'); ?>: <input name="name" maxlength="64" value="<?php echo h($row["name"]); ?>">
 <?php echo ($engines ? html_select("Engine", array("" => "(" . lang('engine') . ")") + $engines, $row["Engine"]) : ""); ?>
- <?php echo ($collations && $jush != "mssql" ? html_select("Collation", array("" => "(" . lang('collation') . ")") + $collations, $row["Collation"]) : ""); ?>
+ <?php echo ($collations && !ereg("sqlite|mssql", $jush) ? html_select("Collation", array("" => "(" . lang('collation') . ")") + $collations, $row["Collation"]) : ""); ?>
  <input type="submit" value="<?php echo lang('Save'); ?>">
 <table cellspacing="0" id="edit-fields" class="nowrap">
 <?php $comments = edit_fields($row["fields"], $collations, "TABLE", $suhosin, $foreign_keys, $row["Comment"] != ""); ?>
index d14f5d3701e6b37f1d4c5b4015de4253c15fad97..789185329b9ae33e544c28b68f53a7f191846e48 100644 (file)
@@ -92,7 +92,7 @@ if (isset($_GET["mssql"])) {
 
                        function Min_Result($result) {
                                $this->_result = $result;
-                               $this->num_rows = sqlsrv_has_rows($result); //! sqlsrv_num_rows($result)
+                               // $this->num_rows = sqlsrv_num_rows($result); // available only in scrollable results
                        }
 
                        function _convert($row) {
index 1ca1cca3abe77b6136f0850c5eff27d6c93f48b7..037440f479ea6eb6d5a00171894e92f1f481858e 100644 (file)
@@ -767,6 +767,19 @@ if (!defined("DRIVER")) {
                return queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")");
        }
        
+       /** Insert or update data in the table
+       * @param string
+       * @param array
+       * @return bool
+       */
+       function insert_update($table, $set) {
+               foreach ($set as $key => $val) {
+                       $set[$key] = "$key = $val";
+               }
+               $update = implode(", ", $set);
+               return queries("INSERT INTO " . table($table) . " SET $update ON DUPLICATE KEY UPDATE $update");
+       }
+       
        /** Get last auto increment ID
        * @return string
        */
@@ -822,6 +835,14 @@ if (!defined("DRIVER")) {
                return $connection->result("SHOW CREATE TABLE " . table($table), 1);
        }
        
+       /** Get SQL command to truncate table
+       * @param string
+       * @return string
+       */
+       function truncate_sql($table) {
+               return "TRUNCATE " . table($table);
+       }
+       
        /** Get SQL command to change database
        * @param string
        * @return string
index 3635ef98764cbd5e4b22c0d994e4abacd5680618..b66dfc7d028a2e9cf4d15cb82d580af21282e9b6 100644 (file)
@@ -83,7 +83,6 @@ if (isset($_GET["oracle"])) {
 
                        function Min_Result($result) {
                                $this->_result = $result;
-                               $this->num_rows = -1; // all results unbuffered
                        }
 
                        function _convert($row) {
index ddc989b6c42da90c17115e94b0df42168b8903a2..f684957628efe8966ed3dc12f7423d9f726ed79b 100644 (file)
@@ -39,6 +39,10 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                                        return "'" . sqlite_escape_string($string) . "'";
                                }
                                
+                               function store_result() {
+                                       return $this->_result;
+                               }
+                               
                                function result($query, $field = 0) {
                                        $result = $this->query($query);
                                        if (!is_object($result)) {
@@ -118,6 +122,10 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                                        return "'" . $this->_link->escapeString($string) . "'";
                                }
                                
+                               function store_result() {
+                                       return $this->_result;
+                               }
+                               
                                function result($query, $field = 0) {
                                        $result = $this->query($query);
                                        if (!is_object($result)) {
@@ -133,7 +141,6 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                                
                                function Min_Result($result) {
                                        $this->_result = $result;
-                                       $this->num_rows = 1; //!
                                }
                                
                                function fetch_assoc() {
@@ -190,10 +197,6 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                        return $this->_result = $this->query($query);
                }
                
-               function store_result() {
-                       return $this->_result;
-               }
-               
                function next_result() {
                        return false;
                }
@@ -238,7 +241,7 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
        }
 
        function tables_list() {
-               return get_key_vals("SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view')", 1);
+               return get_key_vals("SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') ORDER BY (name = 'sqlite_sequence'), name", 1);
        }
 
        function count_tables($databases) {
@@ -278,14 +281,14 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                if (is_object($result)) {
                        while ($row = $result->fetch_assoc()) {
                                $type = strtolower($row["type"]);
+                               $default = $row["dflt_value"];
                                $return[$row["name"]] = array(
                                        "field" => $row["name"],
                                        "type" => (eregi("int", $type) ? "integer" : (eregi("char|clob|text", $type) ? "text" : (eregi("blob", $type) ? "blob" : (eregi("real|floa|doub", $type) ? "real" : "numeric")))),
                                        "full_type" => $type,
-                                       "default" => $row["dflt_value"],
+                                       "default" => (ereg("'(.*)'", $default, $match) ? str_replace("''", "'", $match[1]) : ($default == "NULL" ? null : $default)),
                                        "null" => !$row["notnull"],
                                        "auto_increment" => eregi('^integer$', $type) && $row["pk"], //! possible false positive
-                                       "collation" => null, //!
                                        "privileges" => array("select" => 1, "insert" => 1, "update" => 1),
                                        "primary" => $row["pk"],
                                );
@@ -489,6 +492,10 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                return queries("INSERT INTO " . table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : "DEFAULT VALUES"));
        }
        
+       function insert_update($table, $set) {
+               return queries("REPLACE INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")");
+       }
+       
        function last_id() {
                global $connection;
                return $connection->result("SELECT LAST_INSERT_ROWID()");
@@ -498,6 +505,10 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                return $connection->query("EXPLAIN $query");
        }
        
+       function types() {
+               return array();
+       }
+       
        function schemas() {
                return array();
        }
@@ -515,9 +526,11 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                return $connection->result("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . $connection->quote($table));
        }
        
+       function truncate_sql($table) {
+               return "DELETE FROM " . table($table);
+       }
+       
        function use_sql($database) {
-               global $connection;
-               return "ATTACH " . $connection->quote($database) . " AS " . idf_escape($database);
        }
        
        function trigger_sql($table, $style) {
index 46bd535767bae6868146d87fb32ce5154ed45c78..75c094b7b91df048d1072f16eb4dce5a21915613 100644 (file)
@@ -157,10 +157,11 @@ page_header(lang('Export'), "", ($_GET["export"] != "" ? array("table" => $_GET[
 <?php
 $db_style = array('', 'USE', 'DROP+CREATE', 'CREATE');
 $table_style = array('', 'DROP+CREATE', 'CREATE');
-$data_style = array('', 'TRUNCATE+INSERT', 'INSERT', 'INSERT+UPDATE');
+$data_style = array('', 'TRUNCATE+INSERT', 'INSERT');
 if ($jush == "sql") {
        $db_style[] = 'CREATE+ALTER';
        $table_style[] = 'CREATE+ALTER';
+       $data_style[] = 'INSERT+UPDATE';
 }
 parse_str($_COOKIE["adminer_export"], $row);
 if (!$row) {
@@ -169,10 +170,10 @@ if (!$row) {
 $checked = ($_GET["dump"] == "");
 echo "<tr><th>" . lang('Output') . "<td>" . $adminer->dumpOutput(0, $row["output"]) . "\n";
 echo "<tr><th>" . lang('Format') . "<td>" . $adminer->dumpFormat(0, $row["format"]) . "\n";
-echo "<tr><th>" . lang('Database') . "<td>" . html_select('db_style', $db_style, $row["db_style"])
+echo ($jush == "sqlite" ? "" : "<tr><th>" . lang('Database') . "<td>" . html_select('db_style', $db_style, $row["db_style"])
        . (support("routine") ? checkbox("routines", 1, $checked, lang('Routines')) : "")
        . (support("event") ? checkbox("events", 1, $checked, lang('Events')) : "")
-;
+);
 echo "<tr><th>" . lang('Tables') . "<td>" . html_select('table_style', $table_style, $row["table_style"])
        . (support("trigger") ? checkbox("triggers", 1, $row["table_style"], lang('Triggers')) : "")
 ;
index d52d4b829ae6476c6314bb5820596853df3df21e..db6028fad1440eaa016c568f3a3f033ac97d605b 100644 (file)
@@ -304,7 +304,7 @@ document.getElementById('username').focus();
        * @return array expressions to join by AND
        */
        function selectSearchProcess($fields, $indexes) {
-               global $connection;
+               global $connection, $jush;
                $return = array();
                foreach ($indexes as $i => $index) {
                        if ($index["type"] == "FULLTEXT" && $_GET["fulltext"][$i] != "") {
@@ -330,7 +330,7 @@ document.getElementById('username').focus();
                                        foreach ($fields as $name => $field) {
                                                if (is_numeric($val["val"]) || !ereg('int|float|double|decimal', $field["type"])) {
                                                        $name = idf_escape($name);
-                                                       $cols[] = (ereg('char|text|enum|set', $field["type"]) && !ereg('^utf8', $field["collation"]) ? "CONVERT($name USING utf8)" : $name);
+                                                       $cols[] = ($jush == "sql" && ereg('char|text|enum|set', $field["type"]) && !ereg('^utf8', $field["collation"]) ? "CONVERT($name USING utf8)" : $name);
                                                }
                                        }
                                        $return[] = ($cols ? "(" . implode("$cond OR ", $cols) . "$cond)" : "0");
index 1ebdb422066c256c54f5fc1d8e36d1dc717319f5..70ac314d9d3e8813031436a8d8511bae36d3620d 100644 (file)
@@ -5,77 +5,73 @@
 * @return null
 */
 function select($result, $connection2 = null) {
-       if (!$result->num_rows) {
-               echo "<p class='message'>" . lang('No rows.') . "\n";
-       } else {
-               echo "<table cellspacing='0' class='nowrap'>\n";
-               $links = array(); // colno => orgtable - create links from these columns
-               $indexes = array(); // orgtable => array(column => colno) - primary keys
-               $columns = array(); // orgtable => array(column => ) - not selected columns in primary key
-               $blobs = array(); // colno => bool - display bytes for blobs
-               $types = array(); // colno => type - display char in <code>
-               odd(''); // reset odd for each result
-               for ($i=0; $row = $result->fetch_row(); $i++) {
-                       if (!$i) {
-                               echo "<thead><tr>";
-                               for ($j=0; $j < count($row); $j++) {
-                                       $field = $result->fetch_field();
-                                       $orgtable = $field->orgtable;
-                                       $orgname = $field->orgname;
-                                       if ($orgtable != "") {
-                                               if (!isset($indexes[$orgtable])) {
-                                                       // find primary key in each table
-                                                       $indexes[$orgtable] = array();
-                                                       foreach (indexes($orgtable, $connection2) as $index) {
-                                                               if ($index["type"] == "PRIMARY") {
-                                                                       $indexes[$orgtable] = array_flip($index["columns"]);
-                                                                       break;
-                                                               }
+       $links = array(); // colno => orgtable - create links from these columns
+       $indexes = array(); // orgtable => array(column => colno) - primary keys
+       $columns = array(); // orgtable => array(column => ) - not selected columns in primary key
+       $blobs = array(); // colno => bool - display bytes for blobs
+       $types = array(); // colno => type - display char in <code>
+       odd(''); // reset odd for each result
+       for ($i=0; $row = $result->fetch_row(); $i++) {
+               if (!$i) {
+                       echo "<table cellspacing='0' class='nowrap'>\n";
+                       echo "<thead><tr>";
+                       for ($j=0; $j < count($row); $j++) {
+                               $field = $result->fetch_field();
+                               $orgtable = $field->orgtable;
+                               $orgname = $field->orgname;
+                               if ($orgtable != "") {
+                                       if (!isset($indexes[$orgtable])) {
+                                               // find primary key in each table
+                                               $indexes[$orgtable] = array();
+                                               foreach (indexes($orgtable, $connection2) as $index) {
+                                                       if ($index["type"] == "PRIMARY") {
+                                                               $indexes[$orgtable] = array_flip($index["columns"]);
+                                                               break;
                                                        }
-                                                       $columns[$orgtable] = $indexes[$orgtable];
-                                               }
-                                               if (isset($columns[$orgtable][$orgname])) {
-                                                       unset($columns[$orgtable][$orgname]);
-                                                       $indexes[$orgtable][$orgname] = $j;
-                                                       $links[$j] = $orgtable;
                                                }
+                                               $columns[$orgtable] = $indexes[$orgtable];
                                        }
-                                       if ($field->charsetnr == 63) { // 63 - binary
-                                               $blobs[$j] = true;
+                                       if (isset($columns[$orgtable][$orgname])) {
+                                               unset($columns[$orgtable][$orgname]);
+                                               $indexes[$orgtable][$orgname] = $j;
+                                               $links[$j] = $orgtable;
                                        }
-                                       $types[$j] = $field->type;
-                                       echo "<th" . ($orgtable != "" || $field->name != $orgname ? " title='" . h(($orgtable != "" ? "$orgtable." : "") . $orgname) . "'" : "") . ">" . h($field->name);
                                }
-                               echo "</thead>\n";
+                               if ($field->charsetnr == 63) { // 63 - binary
+                                       $blobs[$j] = true;
+                               }
+                               $types[$j] = $field->type;
+                               echo "<th" . ($orgtable != "" || $field->name != $orgname ? " title='" . h(($orgtable != "" ? "$orgtable." : "") . $orgname) . "'" : "") . ">" . h($field->name);
                        }
-                       echo "<tr" . odd() . ">";
-                       foreach ($row as $key => $val) {
-                               if (!isset($val)) {
-                                       $val = "<i>NULL</i>";
+                       echo "</thead>\n";
+               }
+               echo "<tr" . odd() . ">";
+               foreach ($row as $key => $val) {
+                       if (!isset($val)) {
+                               $val = "<i>NULL</i>";
+                       } else {
+                               if ($blobs[$key] && !is_utf8($val)) {
+                                       $val = "<i>" . lang('%d byte(s)', strlen($val)) . "</i>"; //! link to download
+                               } elseif (!strlen($val)) { // strlen - SQLite can return int
+                                       $val = "&nbsp;"; // some content to print a border
                                } else {
-                                       if ($blobs[$key] && !is_utf8($val)) {
-                                               $val = "<i>" . lang('%d byte(s)', strlen($val)) . "</i>"; //! link to download
-                                       } elseif (!strlen($val)) { // strlen - SQLite can return int
-                                               $val = "&nbsp;"; // some content to print a border
-                                       } else {
-                                               $val = h($val);
-                                               if ($types[$key] == 254) { // 254 - char
-                                                       $val = "<code>$val</code>";
-                                               }
+                                       $val = h($val);
+                                       if ($types[$key] == 254) { // 254 - char
+                                               $val = "<code>$val</code>";
                                        }
-                                       if (isset($links[$key]) && !$columns[$links[$key]]) {
-                                               $link = "edit=" . urlencode($links[$key]);
-                                               foreach ($indexes[$links[$key]] as $col => $j) {
-                                                       $link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
-                                               }
-                                               $val = "<a href='" . h(ME . $link) . "'>$val</a>";
+                               }
+                               if (isset($links[$key]) && !$columns[$links[$key]]) {
+                                       $link = "edit=" . urlencode($links[$key]);
+                                       foreach ($indexes[$links[$key]] as $col => $j) {
+                                               $link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
                                        }
+                                       $val = "<a href='" . h(ME . $link) . "'>$val</a>";
                                }
-                               echo "<td>$val";
                        }
+                       echo "<td>$val";
                }
-               echo "</table>\n";
        }
+       echo ($i ? "</table>" : "<p class='message'>" . lang('No rows.')) . "\n";
 }
 
 /** Get referencable tables with single column primary key except self
index 892dce53882b931b25c182140b74d9aaa2ecdcd8..7c4495290946c8ca9b11161e13aeb913940aaf37 100644 (file)
@@ -20,7 +20,7 @@ function dump_table($table, $style, $is_view = false) {
                $create = create_sql($table);
                if ($create) {
                        if ($style == "DROP+CREATE") {
-                               echo "DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " . idf_escape($table) . ";\n";
+                               echo "DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " . table($table) . ";\n";
                        }
                        echo ($style != "CREATE+ALTER" ? $create : ($is_view ? substr_replace($create, " OR REPLACE", 6, 0) : substr_replace($create, " IF NOT EXISTS", 12, 0))) . ";\n\n";
                }
@@ -86,7 +86,7 @@ CREATE PROCEDURE adminer_alter (INOUT alter_command text) BEGIN
        UNTIL done END REPEAT;
        CLOSE columns;
        IF @alter_table != '' OR add_columns != '' THEN
-               SET alter_command = CONCAT(alter_command, 'ALTER TABLE " . idf_escape($table) . "', SUBSTR(CONCAT(add_columns, @alter_table), 2), ';\\n');
+               SET alter_command = CONCAT(alter_command, 'ALTER TABLE " . table($table) . "', SUBSTR(CONCAT(add_columns, @alter_table), 2), ';\\n');
        END IF;
 END;;
 DELIMITER ;
@@ -104,10 +104,10 @@ function dump_data($table, $style, $select = "") {
        $max_packet = ($jush == "sqlite" ? 0 : 1048576); // default, minimum is 1024
        if ($style) {
                if ($_POST["format"] == "sql" && $style == "TRUNCATE+INSERT") {
-                       echo "TRUNCATE " . idf_escape($table) . ";\n";
+                       echo truncate_sql($table) . ";\n";
                }
                $fields = fields($table);
-               $result = $connection->query(($select ? $select : "SELECT * FROM " . idf_escape($table)), 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers, microtime
+               $result = $connection->query(($select ? $select : "SELECT * FROM " . table($table)), 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers, microtime
                if ($result) {
                        $insert = "";
                        $buffer = "";
@@ -116,7 +116,7 @@ function dump_data($table, $style, $select = "") {
                                        dump_csv($row);
                                } else {
                                        if (!$insert) {
-                                               $insert = "INSERT INTO " . idf_escape($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES";
+                                               $insert = "INSERT INTO " . table($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES";
                                        }
                                        foreach ($row as $key => $val) {
                                                $row[$key] = (isset($val) ? (ereg('int|float|double|decimal', $fields[$key]["type"]) ? $val : $connection->quote($val)) : "NULL"); //! columns looking like functions
index 743ecfd0c2ed7d46b9663de74a73c4cb00ff22f8..21733338201d0c1d968f0cc2a312858543a668f4 100644 (file)
@@ -626,7 +626,7 @@ function search_tables() {
                $name = $adminer->tableName($table_status);
                if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) {
                        $result = $connection->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1));
-                       if ($result->num_rows) {
+                       if ($result->fetch_row()) {
                                if (!$found) {
                                        echo "<ul>\n";
                                        $found = true;
index df2eed787d9048dfc178c4a6ebd34034f82e73f0..e65684730edbe91a84ca899cf63de412d392df5b 100644 (file)
@@ -144,12 +144,11 @@ if ($_POST && !$error) {
                                        $cols = $matches2[1];
                                        $affected--;
                                } else {
-                                       $set = "";
+                                       $set = array();
                                        foreach ($matches2[1] as $i => $col) {
-                                               $set .= ", " . idf_escape($cols[$i]) . " = " . ($col == "" && $fields[$cols[$i]]["null"] ? "NULL" : $connection->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
+                                               $set[idf_escape($cols[$i])] = ($col == "" && $fields[$cols[$i]]["null"] ? "NULL" : $connection->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
                                        }
-                                       $set = substr($set, 1);
-                                       $result = queries("INSERT INTO " . table($TABLE) . " SET$set ON DUPLICATE KEY UPDATE$set");
+                                       $result = insert_update($TABLE, $set);
                                        if (!$result) {
                                                break;
                                        }
@@ -159,7 +158,7 @@ if ($_POST && !$error) {
                                queries("COMMIT");
                        }
                        queries_redirect(remove_from_uri("page"), lang('%d row(s) have been imported.', $affected), $result);
-                       queries("ROLLBACK");
+                       queries("ROLLBACK"); // after queries_redirect() to not overwrite error
                } else {
                        $error = upload_error($file);
                }
index e2b159b5fd5a5c169a6a482d364d97009b465ec0..5d68a1de3be985bf292943af9fa65c5a4050f341 100644 (file)
--- a/todo.txt
+++ b/todo.txt
@@ -26,8 +26,6 @@ Saving of MySQL 5 BIT data type - don't use quote()
 ? Geometry support
 
 SQLite:
-CSV import - ON DUPLICATE KEY UPDATE
-Export - CREATE DATABASE
 Delimiter in export and SQL command
 Backward keys in Editor