]> git.nbdom.net Git - nb.git/commitdiff
lib/php/db/table.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 12 Dec 2017 21:29:16 +0000 (21:29 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 12 Dec 2017 21:29:16 +0000 (21:29 +0000)
lib/php/db.php
lib/php/db/table.php
lib/php/db/types/pgsql.php
www/dbq/dbq.php

index 45acaf4e0541ee7e2f9cf8ae97dbdbb0065e1a61..4bbc159e76f76c82ee98f4256dab42b4ba659c9b 100644 (file)
@@ -472,12 +472,12 @@ class Db extends nb {
     return $this->unvar($db_type[$key]);
   }
 
-  public function type($key=null,$die=false,$type=null) {
-    if (empty($type)) $type = $this->type;
-    if (!empty($key) and $key == 'type') return $type;
-
-    return $this->conf_type($key,$die);
-  }
+# NB 12.12.17   public function type($key=null,$die=false,$type=null) {
+# NB 12.12.17     if (empty($type)) $type = $this->type;
+# NB 12.12.17     if (!empty($key) and $key == 'type') return $type;
+# NB 12.12.17 
+# NB 12.12.17     return $this->conf_type($key,$die);
+# NB 12.12.17   }
 
   public function table_exist($table) {
   # NB 02.11.16: TODO NEVEW USED 
index 4129e24c5a90717cb5915028fcac3321a26422d1..4abbeb2455363b5227378b636c21b79acafe8a5f 100644 (file)
@@ -1581,7 +1581,57 @@ Class Table extends nb {
     return $this->db()->sql_name($value === null ? $this->name : $value);
   }
 
+  private function _post2sql($post) {
+    $keys = [];
+    $keys_values = [];
+    $fields = [];
+    $fields_values = [];
+    foreach ($this->fields() as $name => $field) {
+
+      if ($field->extras) continue;
+
+      $value = $post[$this->field_preff.$name];
+
+      if ($field->key) {
+        $keys[] = $name;
+
+        if (!isset($post[$this->key_preff.$name])) {
+          if ($field->key) $this->bye("Missing `$name`!");
+          continue;
+        }
+
+        if (isset($post[$this->key_preff.$name])) {
+          $keys_values[] = $post[$this->key_preff.$name];
+
+        } else {
+          $keys_values[] = $value;
+        }
+
+      } else {
+
+        $fields[] = $name;
+        $fields_values[] = $value;
+
+      }
+               }
+
+               return [
+       'keys' => $keys,
+       'keys_values' => $keys_values,
+       'fields' => $fields,
+       'fields_values' => $fields_values,
+       'all_keys' => array_combine($keys,$keys_values),
+       'all_fields' => array_combine($fields,$fields_values),
+               ];
+  }
+
   public function replace($hvalues,&$info=[]) {
+               # NB 12.12.17: When REPLACE is not supported where call a personalized function 
+               $fct = $this->db()->conf_type('replace_insert');
+               if (!empty($fct)) {
+                       $info['sql_fct'] = $fct;
+                       return $this->insert($hvalues,$info);
+               }
     return $this->insert($hvalues,$info,'REPLACE');
   }
 
@@ -1609,6 +1659,11 @@ Class Table extends nb {
       $insert_word.' INTO '. $this->sql_name() . ' (' . join(',',array_values($sql_names)).')'
       .' VALUES (' . join(',',ar_map('":$a"',array_keys($fields))) . ')'
     ;
+
+               if (!empty($info['sql_fct'])) {
+                       $fct = $info['sql_fct'];
+                       $fct($sql,$this);
+               }
     $info['sql'] = $sql;
 
     if (!($query = $this->db()->conn->prepare($sql))) {
@@ -1761,7 +1816,8 @@ Class Table extends nb {
     }
 
                # NB 07.12.17: Add LIMIT 1. If a table as not primary key when want to delete only on record
-    $sql = 'DELETE FROM ' . $this->sql_name() . $where . ($this->db()->type == 'pgsql' ? '' : ' LIMIT 1');
+# NB 12.12.17     $sql = 'DELETE FROM ' . $this->sql_name() . $where . ($this->db()->type == 'pgsql' ? '' : ' LIMIT 1');
+    $sql = 'DELETE FROM ' . $this->sql_name() . $where . ($this->db()->conf_type('delete_no_limit') ? '' : ' LIMIT 1');
     $info['sql'] = $sql;
 
     if (self::p('debug')) {
index f267337c526292bfd24c503e33a03c5dbfbbd197..4f423aa5107afcf6ede09c58facd278e8bda404c 100644 (file)
@@ -1,5 +1,13 @@
 <?php
 $DB_TYPES['pgsql'] = array (
+'delete_no_limit' => true,
+'replace_insert' => function(&$sql,&$table) {
+       $keys = $table->fields_keys($others);
+       #if ($table->p('debug')) {
+       $sql .= ' ON CONFLICT ('.join(',',array_keys($keys)).') DO UPDATE ';
+  $sql .= ' SET '   . join(',',$table->ar_map('"$a=:$a"',array_keys($others)));
+       #$info['debug'] = [$keys,$others];
+},
 'extra_where' => 'denorm',
 'regexp' => '~',
 # NB 04.12.17: Overwrite db->quote because of bugs with INSERT INTO 
index 1da6588fde80b23706fcca08ae82dfa38317a08b..a10b7554211941ea9c1a4c4d77b067cbbcfc44ec 100644 (file)
@@ -1135,6 +1135,11 @@ EOF;
                        #header('Location: '.$this->table->base.'/');
                        $this->page($info);
 
+               } elseif ($action == 'replace' and $this->perm >= self::WRITE) {
+                       if (!$this->table->replace($_POST,$info)) $this->error('replace: '.print_r($info,true));
+                       header('Location: '.$this->table->base.'/');
+                       $this->page($info);
+
                } elseif ($action == 'rm' and $this->perm >= self::DELETE) {
                        if (!$this->table->delete($_POST,$info)) $this->error('rm: '.print_r($info,true));
                        header('Location: '.$this->table->base.'/');