From: Nicolas Boisselier Date: Wed, 13 Dec 2017 00:43:34 +0000 (+0000) Subject: lib/php/nb.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=e9a885c5e47b9021fc1000658a97088fc04c2afe;p=nb.git lib/php/nb.php --- diff --git a/lib/php/db/field.php b/lib/php/db/field.php index e7ed0087..5695a2cf 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -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"); diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 5c5b27d0..f9538d1a 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -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); diff --git a/lib/php/nb.php b/lib/php/nb.php index fdcb3ee9..dd4017f2 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -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 /*