]> git.nbdom.net Git - nb.git/commitdiff
lib/php/nb.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 13 Dec 2017 00:43:34 +0000 (00:43 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 13 Dec 2017 00:43:34 +0000 (00:43 +0000)
lib/php/db/field.php
lib/php/db/table.php
lib/php/nb.php

index e7ed0087124a3c47914ea95510cac995058b0e8a..5695a2cf77453f348b09f2b8ced78d5e8920f718 100644 (file)
@@ -41,6 +41,8 @@ class field extends nb {
     if ($this->default == "''") $this->default = '';
 # NB 12.12.17     if (strtoupper($this->default) == "NULL") $this->default = null;
     #if (strtoupper($this->default) == "NOW()") $this->default = null;
+    if (strtoupper($this->default) == "NOW()") $this->default = $this->date_microtime();
+    if (strtoupper($this->default) == "CURRENT_TIMESTAMP)") $this->default = $this->date_microtime();
     if (strtoupper($this->default) == "NOW()") $this->default = date("Y-m-d H:i:s.u");
     if (strtoupper($this->default) == "CURRENT_TIMESTAMP)") $this->default = date("Y-m-d H:i:s.u");
     if (strtoupper($this->default) == "CURRENT_DATE)") $this->default = date("Y-m-d");
index 5c5b27d03965d916862bbb3f40fc049e25c808fc..f9538d1a1de5100b1b6cfc5d75f5bf977d8e03da 100644 (file)
@@ -1625,10 +1625,10 @@ Class Table extends nb {
   }
 
   public function replace($hvalues,&$info=[]) {
-               # NB 12.12.17: When REPLACE is not supported where call a personalized function 
+               # NB 12.12.17: When REPLACE is not supported where call a personalized function to modify $sql
                $fct = $this->db()->conf_type('replace_insert');
                if (!empty($fct)) {
-                       $info['sql_fct'] = $fct;
+                       $info['fct'] = $fct;
                        return $this->insert($hvalues,$info);
                }
     return $this->insert($hvalues,$info,'REPLACE');
@@ -1639,15 +1639,22 @@ Class Table extends nb {
     if (empty($info['values'])) $info['values'] = [];
     if (empty($post)) $this->bye('insert(): No values');
 
-    $info['values'] = $post;
-
     $sql_names = $fields = $values = [];
 
     foreach ($this->fields() as $name => $field) {
-      #if (!isset($post[$this->field_preff.$name]) and isset($field->default)) 
-      if (!isset($post[$this->field_preff.$name])) continue;
       if ($field->key and $field->autoincrement()) continue;
 
+      if (!isset($post[$this->field_preff.$name])) {
+
+                               # For postgres and replace mode, we want to update timestamp for example
+                               if (!empty($info['fct']) and isset($field->default)) {
+                                       $post[$this->field_preff.$name] = $field->default;
+                               } else {
+                                       continue;
+                               }
+
+                       }
+
       $fields[$name] = $field;
       $sql_names[$name] = $field->sql_name();
       $values[] = $post[$this->field_preff.$name];
@@ -1660,8 +1667,9 @@ Class Table extends nb {
       .' VALUES (' . join(',',ar_map('":$a"',array_keys($fields))) . ')'
     ;
 
-               if (!empty($info['sql_fct'])) {
-                       $fct = $info['sql_fct'];
+               if (!empty($info['fct'])) {
+                       $fct = $info['fct'];
+                       unset($info['fct']);
                        $fct($sql,$this,$fields);
                }
     $info['sql'] = $sql;
@@ -1671,6 +1679,8 @@ Class Table extends nb {
       return false;
     }
 
+    $info['values'] = $post;
+
     foreach ($fields as $name => $field) {
       $row = [ $name => $post[$name] ];
       $this->db()->table_row_encrypt($this,$row);
index fdcb3ee91b1746570b73568f0ab44afbb7f569c5..dd4017f29a7657855fb10b9fea118654ca42723d 100644 (file)
@@ -1111,6 +1111,17 @@ class NB {
 
        } # < ldap_search
 
+       public static function date_microtime($format='Y-m-d H:i:s') {
+               #return date("Y-m-d H:i:s.u");
+               $micro_date = microtime();
+               $date_array = explode(" ",$micro_date);
+
+               $date = date($format,$date_array[1]);
+
+               $date_array[0] = preg_replace('/^.*?\D/','',$date_array[0]);
+
+               return $date . '.' . $date_array[0];
+       }
 } # < Class NB
 
 /*