]> git.nbdom.net Git - nb.git/commitdiff
lib/php/db.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 4 Jan 2018 23:22:44 +0000 (23:22 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 4 Jan 2018 23:22:44 +0000 (23:22 +0000)
lib/php/db.php
lib/php/db/config.php
lib/postgres/function/CIDR_RANGE.sql [new file with mode: 0644]
lib/postgres/function/DATE.sql [new file with mode: 0644]
lib/postgres/function/GROUP_CONCAT.sql [new file with mode: 0644]
lib/postgres/function/STRFTIME.sql [new file with mode: 0644]

index db307cf39c1578b1f76fcfbec4539f55162cf87f..7f5e9f294b0fcc1f60cdf2d2375e0e590cbe538a 100644 (file)
@@ -685,6 +685,7 @@ class Db extends nb {
     # Load all files into a $dbs if #files is not a hash
     #
 
+    $DBQ = [];
     $dbs = [];
     $done = [];
     foreach ((array)$files as $file) {
index 3ac6562a671e524dd49d53a46c4bd3e60ff46d1d..df5dd19599de1467db173e557360d7e44f0c819d 100644 (file)
@@ -2,7 +2,6 @@
 require_once(realpath(dirname(__FILE__).'/../config.php'));
 require_once(NB_ROOT.'/lib/php/db.php');
 if (empty($_SERVER['HOME'])) $_SERVER['HOME'] = '';
-if (empty($DBQ)) $DBQ = [];
 $DB_CONFS = array_merge([],
 
   # ::ROOT_DIR
diff --git a/lib/postgres/function/CIDR_RANGE.sql b/lib/postgres/function/CIDR_RANGE.sql
new file mode 100644 (file)
index 0000000..988ce60
--- /dev/null
@@ -0,0 +1,5 @@
+DROP FUNCTION IF EXISTS CIDR_RANGE(cidr text, ip text);
+CREATE OR REPLACE FUNCTION CIDR_RANGE(cidr text, ip text)
+RETURNS bool AS $$
+SELECT ip::inet << cidr::cidr
+$$ LANGUAGE sql;
diff --git a/lib/postgres/function/DATE.sql b/lib/postgres/function/DATE.sql
new file mode 100644 (file)
index 0000000..8ab3e51
--- /dev/null
@@ -0,0 +1,5 @@
+DROP FUNCTION IF EXISTS DATE(sdate text, sinterval text);
+CREATE OR REPLACE FUNCTION DATE(sdate text DEFAULT 'now', sinterval text DEFAULT '0 seconds')
+RETURNS date AS $$
+SELECT to_char(sdate::date + sinterval::interval,'YYYY-MM-DD')::date;
+$$ LANGUAGE sql;
diff --git a/lib/postgres/function/GROUP_CONCAT.sql b/lib/postgres/function/GROUP_CONCAT.sql
new file mode 100644 (file)
index 0000000..d632712
--- /dev/null
@@ -0,0 +1,16 @@
+-- NB 22.06.16 CREATE OR REPLACE FUNCTION GROUP_CONCAT(anyarray, sep text DEFAULT ',', nullstr text DEFAULT '')
+-- NB 22.06.16 RETURNS text AS $$
+-- NB 22.06.16 SELECT array_to_string( ARRAY( SELECT coalesce(v::text, $3) FROM unnest($1) g(v) ), $2 )
+-- NB 22.06.16 $$ LANGUAGE sql;
+
+CREATE OR REPLACE FUNCTION GROUP_CONCAT(str text,sep text DEFAULT ',', nullstr text DEFAULT '')
+RETURNS text AS $$
+SELECT array_to_string(array_agg(coalesce(str,nullstr)),sep)
+$$ LANGUAGE sql;
+
+-- NB 21.06.16 CREATE AGGREGATE GROUP_CONCAT(field,sep=',')
+-- NB 21.06.16   sfunc = array_append,
+-- NB 21.06.16   stype = anyarray,
+-- NB 21.06.16   initcond = '{}'
+-- NB 21.06.16   -- return array_to_string(array_agg(field),sep)
+-- NB 21.06.16 }
diff --git a/lib/postgres/function/STRFTIME.sql b/lib/postgres/function/STRFTIME.sql
new file mode 100644 (file)
index 0000000..7d6f9ce
--- /dev/null
@@ -0,0 +1,5 @@
+DROP FUNCTION IF EXISTS STRFTIME(format text,sdate text);
+CREATE OR REPLACE FUNCTION STRFTIME(format text,sdate text)
+RETURNS text AS $$
+SELECT to_char(sdate::date,format)
+$$ LANGUAGE sql;