]> git.joonet.de Git - adminer.git/commitdiff
Abstract DELETE, INSERT and INSERT+UPDATE
authorJakub Vrana <jakub@vrana.cz>
Fri, 5 Jul 2013 16:04:06 +0000 (09:04 -0700)
committerJakub Vrana <jakub@vrana.cz>
Tue, 9 Jul 2013 17:34:52 +0000 (10:34 -0700)
12 files changed:
adminer/drivers/mssql.inc.php
adminer/drivers/mysql.inc.php
adminer/drivers/oracle.inc.php
adminer/drivers/pgsql.inc.php
adminer/drivers/sqlite.inc.php
adminer/dump.inc.php
adminer/edit.inc.php
adminer/include/auth.inc.php
adminer/include/bootstrap.inc.php
adminer/include/driver.inc.php [new file with mode: 0644]
adminer/select.inc.php
compile.php

index 2b541e62b1aa30edc4179f674155350b709ab8fe..85545b6923a6fc6d6209599778dcc726279603ad 100644 (file)
@@ -233,6 +233,30 @@ if (isset($_GET["mssql"])) {
                
        }
 
+
+
+       class Min_Driver extends Min_SQL {
+               
+               function insertUpdate($table, $set, $primary) {
+                       $update = array();
+                       $where = array();
+                       foreach ($set as $key => $val) {
+                               $update[] = "$key = $val";
+                               if (isset($primary[idf_unescape($key)])) {
+                                       $where[] = "$key = $val";
+                               }
+                       }
+                       // can use only one query for all rows with different API
+                       return queries("MERGE " . table($table) . " USING (VALUES(" . implode(", ", $set) . ")) AS source (c" . implode(", c", range(1, count($set))) . ") ON " . implode(" AND ", $where) //! source, c1 - possible conflict
+                               . " WHEN MATCHED THEN UPDATE SET " . implode(", ", $update)
+                               . " WHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ");" // ; is mandatory
+                       );
+               }
+               
+       }
+
+
+
        function idf_escape($idf) {
                return "[" . str_replace("]", "]]", $idf) . "]";
        }
@@ -460,26 +484,6 @@ WHERE OBJECT_NAME(i.object_id) = " . q($table)
                return queries("BEGIN TRANSACTION");
        }
        
-       function insert_into($table, $set) {
-               return queries("INSERT INTO " . table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : "DEFAULT VALUES"));
-       }
-       
-       function insert_update($table, $set, $primary) {
-               $update = array();
-               $where = array();
-               foreach ($set as $key => $val) {
-                       $update[] = "$key = $val";
-                       if (isset($primary[idf_unescape($key)])) {
-                               $where[] = "$key = $val";
-                       }
-               }
-               // can use only one query for all rows with different API
-               return queries("MERGE " . table($table) . " USING (VALUES(" . implode(", ", $set) . ")) AS source (c" . implode(", c", range(1, count($set))) . ") ON " . implode(" AND ", $where) //! source, c1 - possible conflict
-                       . " WHEN MATCHED THEN UPDATE SET " . implode(", ", $update)
-                       . " WHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ");" // ; is mandatory
-               );
-       }
-       
        function last_id() {
                global $connection;
                return $connection->result("SELECT SCOPE_IDENTITY()"); // @@IDENTITY can return trigger INSERT
index 772e3ce4a295b7fabff3bf6db01d934bdc641e26..21117f7ba5fa1e6ebaa66f369f1cd6d1501c9153 100644 (file)
@@ -229,6 +229,26 @@ if (!defined("DRIVER")) {
                
        }
 
+
+
+       class Min_Driver extends Min_SQL {
+               
+               function insert($table, $set) {
+                       return ($set ? parent::insert($table, $set) : queries("INSERT INTO " . table($table) . " ()\nVALUES ()"));
+               }
+               
+               function insertUpdate($table, $set, $primary) {
+                       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");
+               }
+               
+       }
+
+
+
        /** Escape database identifier
        * @param string
        * @return string
@@ -807,29 +827,6 @@ if (!defined("DRIVER")) {
                return queries("BEGIN");
        }
        
-       /** Insert data into table
-       * @param string
-       * @param array
-       * @return bool
-       */
-       function insert_into($table, $set) {
-               return queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")");
-       }
-       
-       /** Insert or update data in the table
-       * @param string
-       * @param array
-       * @param array columns in keys
-       * @return bool
-       */
-       function insert_update($table, $set, $primary) {
-               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
        */
index 4422e20d841ca727a337ecda9db21085c1429d62..c5a30f1e6b8dae6ac455b853714d493a14ba4249 100644 (file)
@@ -132,6 +132,16 @@ if (isset($_GET["oracle"])) {
                
        }
        
+
+
+       class Min_Driver extends Min_SQL {
+               
+               //! support empty $set in insert()
+               
+       }
+
+
+
        function idf_escape($idf) {
                return '"' . str_replace('"', '""', $idf) . '"';
        }
@@ -322,10 +332,6 @@ ORDER BY uc.constraint_type, uic.column_position", $connection2) as $row) {
                return true; // automatic start
        }
        
-       function insert_into($table, $set) {
-               return queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")"); //! no columns
-       }
-       
        function last_id() {
                return 0; //!
        }
index 2e46e28bbd94b7100d5033ac7f628caf2d4c1446..41341eacd6d81e12f7165fe12f773a44f346ee6a 100644 (file)
@@ -148,7 +148,30 @@ if (isset($_GET["pgsql"])) {
                }
                
        }
-       
+
+
+
+       class Min_Driver extends Min_SQL {
+               
+               function insertUpdate($table, $set, $primary) {
+                       global $connection;
+                       $update = array();
+                       $where = array();
+                       foreach ($set as $key => $val) {
+                               $update[] = "$key = $val";
+                               if (isset($primary[idf_unescape($key)])) {
+                                       $where[] = "$key = $val";
+                               }
+                       }
+                       return ($where && queries("UPDATE " . table($table) . " SET " . implode(", ", $update) . " WHERE " . implode(" AND ", $where)) && $connection->affected_rows)
+                               || queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")")
+                       ;
+               }
+               
+       }
+
+
+
        function idf_escape($idf) {
                return '"' . str_replace('"', '""', $idf) . '"';
        }
@@ -515,25 +538,6 @@ ORDER BY p.proname');
                return queries("BEGIN");
        }
        
-       function insert_into($table, $set) {
-               return queries("INSERT INTO " . table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : "DEFAULT VALUES"));
-       }
-       
-       function insert_update($table, $set, $primary) {
-               global $connection;
-               $update = array();
-               $where = array();
-               foreach ($set as $key => $val) {
-                       $update[] = "$key = $val";
-                       if (isset($primary[idf_unescape($key)])) {
-                               $where[] = "$key = $val";
-                       }
-               }
-               return ($where && queries("UPDATE " . table($table) . " SET " . implode(", ", $update) . " WHERE " . implode(" AND ", $where)) && $connection->affected_rows)
-                       || queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")")
-               ;
-       }
-       
        function last_id() {
                return 0; // there can be several sequences
        }
index 64b75d8d2531eaab85a878b69308e0a4975782d3..888fa9e00bf52141b282f1779205b57a26700b7b 100644 (file)
@@ -203,7 +203,19 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                        }
                }
        }
-       
+
+
+
+       class Min_Driver extends Min_SQL {
+               
+               function insertUpdate($table, $set, $primary) {
+                       return queries("REPLACE INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")");
+               }
+               
+       }
+
+
+
        function idf_escape($idf) {
                return '"' . str_replace('"', '""', $idf) . '"';
        }
@@ -587,14 +599,6 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                return queries("BEGIN");
        }
        
-       function insert_into($table, $set) {
-               return queries("INSERT INTO " . table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : "DEFAULT VALUES"));
-       }
-       
-       function insert_update($table, $set, $primary) {
-               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()");
index 4ad663a3e8f837ba4ed270a088a9748147dd151a..c5c3f47cda7ec4bcf4d99d157f8a886e94b04316 100644 (file)
@@ -126,7 +126,7 @@ page_header(lang('Export'), $error, ($_GET["export"] != "" ? array("table" => $_
 $db_style = array('', 'USE', 'DROP+CREATE', 'CREATE');
 $table_style = array('', 'DROP+CREATE', 'CREATE');
 $data_style = array('', 'TRUNCATE+INSERT', 'INSERT');
-if ($jush == "sql") { //! use insert_update() in all drivers
+if ($jush == "sql") { //! use insertUpdate() in all drivers
        $data_style[] = 'INSERT+UPDATE';
 }
 parse_str($_COOKIE["adminer_export"], $row);
index d63e153457a6f4c7bdbe7bbf681c2660266b92fd..4bcb240a6cd2a768b8735673214bfab167035ad0 100644 (file)
@@ -22,12 +22,12 @@ if ($_POST && !$error && !isset($_GET["select"])) {
        $query_where = "\nWHERE $where";
        
        if (isset($_POST["delete"])) {
-               $query = "FROM " . table($TABLE);
-               query_redirect(
-                       "DELETE" . ($unique_array ? " $query$query_where" : limit1($query, $query_where)),
+               queries_redirect(
                        $location,
-                       lang('Item has been deleted.')
+                       lang('Item has been deleted.'),
+                       $driver->delete($TABLE, $query_where, !$unique_array)
                );
+               
        } else {
                $set = array();
                foreach ($fields as $name => $field) {
@@ -53,7 +53,7 @@ if ($_POST && !$error && !isset($_GET["select"])) {
                                exit;
                        }
                } else {
-                       $result = insert_into($TABLE, $set);
+                       $result = $driver->insert($TABLE, $set);
                        $last_id = ($result ? last_id() : 0);
                        queries_redirect($location, lang('Item%s has been inserted.', ($last_id ? " $last_id" : "")), $result); //! link
                }
index 14070be45d3f3a79dbcef3a2f1f3065f837c6daa..260b377cababb3e6dee64727dd9cac78b46d90e2 100644 (file)
@@ -116,6 +116,8 @@ if (is_string($connection) || !$adminer->login($_GET["username"], get_session("p
        exit;
 }
 
+$driver = new Min_Driver($connection);
+
 $token = $_SESSION["token"]; ///< @var string CSRF protection
 if ($auth && $_POST["token"]) {
        $_POST["token"] = $token; // reset token after explicit login
index c71a851a10f94c1aebe43dc9fe15ed02c6bda26d..3bb2f2e910f92dd8cf985330c5c9e783df130da5 100644 (file)
@@ -58,6 +58,7 @@ if (function_exists("set_magic_quotes_runtime")) { // removed in PHP 6
 include "../adminer/include/lang.inc.php";
 include "../adminer/lang/$LANG.inc.php";
 include "../adminer/include/pdo.inc.php";
+include "../adminer/include/driver.inc.php";
 include "../adminer/drivers/sqlite.inc.php";
 include "../adminer/drivers/pgsql.inc.php";
 include "../adminer/drivers/oracle.inc.php";
diff --git a/adminer/include/driver.inc.php b/adminer/include/driver.inc.php
new file mode 100644 (file)
index 0000000..5321a06
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+/*abstract*/ class Min_SQL {
+       var $_conn;
+       
+       /** Create object for performing database operations
+       * @param Min_DB
+       */
+       function Min_SQL($connection) {
+               $this->_conn = $connection;
+       }
+       
+       /** Delete data from table
+       * @param string
+       * @param string " WHERE ..."
+       * @param int 0 or 1
+       * @return bool
+       */
+       function delete($table, $queryWhere, $limit = 0) {
+               $query = "FROM " . table($table);
+               return queries("DELETE" . ($limit ? limit1($query, $queryWhere) : " $query$queryWhere"));
+       }
+       
+       /** Insert data into table
+       * @param string
+       * @param array
+       * @return bool
+       */
+       function insert($table, $set) {
+               return queries("INSERT INTO " . table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : "DEFAULT VALUES"));
+       }
+       
+       /** Insert or update data in table
+       * @param string
+       * @param array
+       * @param array columns in keys
+       * @return bool
+       */
+       /*abstract*/ function insertUpdate($table, $set, $primary) {
+               return false;
+       }
+       
+}
index c8dc5ec718b314b15150aa5e3df3332beec780e2..84ea1e32797aa268e763d32564b97515071fabdc 100644 (file)
@@ -103,21 +103,24 @@ if ($_POST && !$error) {
                        }
                        if ($_POST["delete"] || $set) {
                                $command = "UPDATE";
-                               if ($_POST["delete"]) {
-                                       $command = "DELETE";
-                                       $query = "FROM $query";
-                               }
                                if ($_POST["clone"]) {
                                        $command = "INSERT";
                                        $query = "INTO $query";
                                }
                                if ($_POST["all"] || ($unselected === array() && is_array($_POST["check"])) || $is_group) {
-                                       $result = queries("$command $query$where_check");
+                                       $result = ($_POST["delete"]
+                                               ? $driver->delete($TABLE, $where_check)
+                                               : queries("$command $query$where_check")
+                                       );
                                        $affected = $connection->affected_rows;
                                } else {
                                        foreach ((array) $_POST["check"] as $val) {
                                                // where is not unique so OR can't be used
-                                               $result = queries($command . limit1($query, "\nWHERE " . ($where ? implode(" AND ", $where) . " AND " : "") . where_check($val, $fields)));
+                                               $where2 = "\nWHERE " . ($where ? implode(" AND ", $where) . " AND " : "") . where_check($val, $fields);
+                                               $result = ($_POST["delete"]
+                                                       ? $driver->delete($TABLE, $where2, 1)
+                                                       : queries($command . limit1($query, $where2))
+                                               );
                                                if (!$result) {
                                                        break;
                                                }
@@ -181,7 +184,7 @@ if ($_POST && !$error) {
                                        foreach ($matches2[1] as $i => $col) {
                                                $set[idf_escape($cols[$i])] = ($col == "" && $fields[$cols[$i]]["null"] ? "NULL" : q(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
                                        }
-                                       $result = insert_update($TABLE, $set, $primary);
+                                       $result = $driver->insertUpdate($TABLE, $set, $primary);
                                        if (!$result) {
                                                break;
                                        }
index 71d2792fd97913de0dd05be7b51ccb06a3624c5f..9471c11a236e4fa1c9483361e80af44b54b64fa5 100755 (executable)
@@ -327,7 +327,7 @@ if ($_SERVER["argv"][1]) {
 $filename = dirname(__FILE__) . "/adminer/drivers/mysql.inc.php";
 preg_match_all('~\\bfunction ([^(]+)~', file_get_contents($filename), $matches); //! respect context (extension, class)
 $functions = array_combine($matches[1], $matches[0]);
-unset($functions["__destruct"], $functions["Min_DB"], $functions["Min_Result"]);
+unset($functions["__destruct"], $functions["Min_DB"], $functions["Min_Result"], $functions["Min_Driver"]);
 foreach (glob(dirname(__FILE__) . "/adminer/drivers/" . ($driver ? $driver : "*") . ".inc.php") as $filename) {
        if ($filename != "mysql.inc.php") {
                $file = file_get_contents($filename);
@@ -340,6 +340,7 @@ foreach (glob(dirname(__FILE__) . "/adminer/drivers/" . ($driver ? $driver : "*"
 }
 
 include dirname(__FILE__) . "/adminer/include/pdo.inc.php";
+include dirname(__FILE__) . "/adminer/include/driver.inc.php";
 $features = array("call" => "routine", "dump", "event", "privileges", "procedure" => "routine", "processlist", "routine", "scheme", "sequence", "status", "trigger", "type", "user" => "privileges", "variables", "view");
 $lang_ids = array(); // global variable simplifies usage in a callback function
 $file = file_get_contents(dirname(__FILE__) . "/$project/index.php");