1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-14 02:23:59 +02:00

Abstract DELETE, INSERT and INSERT+UPDATE

This commit is contained in:
Jakub Vrana
2013-07-05 09:04:06 -07:00
parent 5557adf289
commit 1f7fa44923
12 changed files with 155 additions and 90 deletions

View 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

View 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
*/

View 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; //!
}

View 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
}

View 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()");