REPLACE INTO nahrazeno vyrazem UPDATE - ON DUPLICATE KEY (kvuli referencni integrite)
authorThomas Mudrunka <tomas@mudrunka.cz>
Sat, 6 Aug 2011 05:22:04 +0000 (07:22 +0200)
committerThomas Mudrunka <tomas@mudrunka.cz>
Sat, 6 Aug 2011 05:22:04 +0000 (07:22 +0200)
index.php

index 7d500bac22b08ee934ba60baff196e3a4a967d50..0614a8d491015f5d5d198233910452148604f8d3 100755 (executable)
--- a/index.php
+++ b/index.php
@@ -349,7 +349,8 @@ class Sklad_DB extends PDO {
                $table = $this->escape($table);
 
                //Get list of POSTed columns
-               $columns = implode(',',array_map(array($this,'escape'), array_keys($values[0])));
+               $columns_array = array_map(array($this,'escape'), array_keys($values[0]));
+               $columns = implode(',',$columns_array);
 
                //Build query
                $sql = '';
@@ -372,8 +373,7 @@ class Sklad_DB extends PDO {
                }
 
                //Insert into table (columns)
-               $sql .= $replace ? 'REPLACE' : 'INSERT';
-               $sql .= " INTO $table ($columns) VALUES ";
+               $sql .= "INSERT INTO $table ($columns) VALUES ";
 
                //Values (a,b,c),(d,e,f)
                $comma='';
@@ -399,6 +399,15 @@ class Sklad_DB extends PDO {
                        $comma = ',';
                }
 
+               //On duplicate key
+               if($replace) {
+                       foreach($columns_array as $col) {
+                               if($col == $table.'_id' || $col == $table.'_valid_till') continue;
+                               $on_duplicate[] = "$col=VALUES($col)";
+                       }
+                       $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate);
+               }
+
                //Terminate
                $sql .= ';';
                return $sql;
This page took 0.145169 seconds and 4 git commands to generate.