From c31377389521204fb90bd9bb1004d20618eafb0c Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 12 Dec 2017 23:13:01 +0000 Subject: [PATCH] www/dbq/dbq.php --- etc/profile.d/nb.sh | 6 ++---- lib/php/db.php | 2 +- lib/php/db/field.php | 5 +++++ lib/php/db/table.php | 12 ++++++------ lib/php/db/types/pgsql.php | 7 ++++--- share/sql/host_info.sql | 7 ++++--- www/dbq/etc/nginx.conf | 4 +++- 7 files changed, 25 insertions(+), 18 deletions(-) diff --git a/etc/profile.d/nb.sh b/etc/profile.d/nb.sh index 67da5cda..4dd598c9 100644 --- a/etc/profile.d/nb.sh +++ b/etc/profile.d/nb.sh @@ -92,10 +92,8 @@ nb_api_post() { nb_post_sys_infos() { ( - sys_infos | while IFS=$'\t' read -r -a i; do - #eval "nb_api_post host_info \"key=${i[0]}\" \"val=${i[1]}\"" - nb_api_post host_info "key=${i[0]}" "val=${i[1]}" + sys_infos | while IFS=$'\t' read -r key val; do + nb_api_post host_info "key=$key" "val=$val" done ) | sed 's/^/nb_post_sys_infos: /' } - diff --git a/lib/php/db.php b/lib/php/db.php index 4bbc159e..c3e23b04 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -14,7 +14,7 @@ $DB_TYPES = []; # See db/types/*.php #$arr = ['rent'=>'Rent','nb'=>'Nb']; #if (!empty($argv) and $argv[1] == 'zaza') bye($arr); -if (defined('PRODUCTION')) { +if (defined('PRODUCTION') and PRODUCTION) { define('DB_DEFAUL_ERRMODE',PDO::ERRMODE_SILENT); } else { define('DB_DEFAUL_ERRMODE',PDO::ERRMODE_EXCEPTION); diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 4424078c..e7ed0087 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -39,6 +39,11 @@ class field extends nb { if (is_scalar($attr)) $attr = ['name'=>$attr]; foreach ($attr as $k => $v) { $this->$k = $v; } 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 = 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"); } public function autoincrement() { diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 4abbeb24..5c5b27d0 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -239,7 +239,6 @@ Class Table extends nb { // Default return $field->sql_name().' '.$field->type -# NB 27.10.16 .($field->null ? ' NULL' : ' NOT NULL') .($field->null ? '' : ' NOT NULL') .($field->default !== null ? ' DEFAULT '.$field->quote($field->default,true) : '') .($field->key ? ' PRIMARY KEY' : '') @@ -1645,6 +1644,7 @@ Class Table extends nb { $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; @@ -1662,7 +1662,7 @@ Class Table extends nb { if (!empty($info['sql_fct'])) { $fct = $info['sql_fct']; - $fct($sql,$this); + $fct($sql,$this,$fields); } $info['sql'] = $sql; @@ -1715,12 +1715,12 @@ Class Table extends nb { continue; } - $_value = isset($post[$this->key_preff.$name]) ? $post[$this->key_preff.$name] : null; +# NB 12.12.17 $_value = isset($post[$this->key_preff.$name]) ? $post[$this->key_preff.$name] : null; - if ($_value) { + if (isset($post[$this->key_preff.$name])) { $fields[] = $name; - $fields_values[] = $value; - $keys_values[] = $_value; + $fields_values[] = $post[$this->key_preff.$name]; + $keys_values[] = $post[$this->key_preff.$name]; } else { $keys_values[] = $value; diff --git a/lib/php/db/types/pgsql.php b/lib/php/db/types/pgsql.php index 4f423aa5..d9cb1508 100644 --- a/lib/php/db/types/pgsql.php +++ b/lib/php/db/types/pgsql.php @@ -1,11 +1,12 @@ true, -'replace_insert' => function(&$sql,&$table) { +'replace_insert' => function(&$sql,&$table,$fields=[]) { $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))); + if (!empty($fields)) $others = $fields; + $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', diff --git a/share/sql/host_info.sql b/share/sql/host_info.sql index 62aeb019..318e43a1 100644 --- a/share/sql/host_info.sql +++ b/share/sql/host_info.sql @@ -3,11 +3,12 @@ CREATE TABLE host_info ( host VARCHAR(200), key VARCHAR(100) NOT NULL, val VARCHAR(500) NOT NULL, - created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (host,key,created) + updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + -- PRIMARY KEY (host,key,updated) + PRIMARY KEY (host,key) ); CREATE INDEX IF NOT EXISTS host_info_host ON host_info USING btree (host); CREATE INDEX IF NOT EXISTS host_info_key ON host_info USING btree (key); CREATE INDEX IF NOT EXISTS host_info_val ON host_info USING btree (val); -CREATE INDEX IF NOT EXISTS host_info_created ON host_info USING btree (created); +CREATE INDEX IF NOT EXISTS host_info_updated ON host_info USING btree (updated); GRANT ALL ON host_info TO www; diff --git a/www/dbq/etc/nginx.conf b/www/dbq/etc/nginx.conf index 67475373..002c55bd 100644 --- a/www/dbq/etc/nginx.conf +++ b/www/dbq/etc/nginx.conf @@ -39,8 +39,10 @@ location ~ /index\.php$ { fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; - if ($DBQ_CONF_FILE = "") { set $DBQ_CONF_FILE $document_root/../../../etc/dbq/000-local.php; } fastcgi_param DBQ_TITLE $DBQ_TITLE; + if ($DBQ_CONF_FILE = "") { set $DBQ_CONF_FILE $document_root/../../../etc/dbq/000-local.php; } fastcgi_param DBQ_CONF_FILE $DBQ_CONF_FILE; fastcgi_param DBQ_PERM $DBQ_PERM; + if ($DBQ_PROD = "") { set $DBQ_PROD ""; } + fastcgi_param PRODUCTION $DBQ_PROD; } -- 2.47.3