]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 13 Sep 2016 22:44:23 +0000 (23:44 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 13 Sep 2016 22:44:23 +0000 (23:44 +0100)
lib/php/db/index.php
lib/php/db/postgres-create-functions.sql [deleted file]
lib/php/db/shell.php
lib/php/functions.php
lib/php/nb.php
share/postgres/postgres-create-functions.sql [new file with mode: 0644]

index beaf177e9195718d338da296af08c8c472ee59e6..0664afc12d5c30540c731c92bf8f0edaee8f7021 100755 (executable)
@@ -7,15 +7,6 @@
 * lib/php/db/index.php
 */
 require_once(realpath(dirname(__FILE__).'/config.php'));
-# NB 13.09.16 require_once(realpath(dirname(__FILE__).'/../config.php'));
-# NB 13.09.16 require_once(realpath(dirname(__FILE__).'/../db.php'));
-# NB 13.09.16 
-# NB 13.09.16 Db::pinit();
-# NB 13.09.16 
-# NB 13.09.16 if(defined('DB_NO_INIT') and DB_NO_INIT) return true;
-# NB 13.09.16 #bye(Db::conf_load(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )));
-# NB 13.09.16 #$conf = Db::conf_load(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )); Db::init($conf);
-# NB 13.09.16 Db::init([Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml']);
 
 if(defined('DB_NO_ACTION') and DB_NO_ACTION) return true;
 
diff --git a/lib/php/db/postgres-create-functions.sql b/lib/php/db/postgres-create-functions.sql
deleted file mode 100644 (file)
index ccdf380..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
--- CREATE OR REPLACE FUNCTION generate_create_table_statement(p_table_name varchar)
-CREATE OR REPLACE FUNCTION show_create_table(p_table_name varchar)
-  RETURNS text AS
-$BODY$
-DECLARE
-    v_table_ddl   text;
-    column_record record;
-BEGIN
-    FOR column_record IN 
-        SELECT 
-(SELECT 1 FROM pg_index i WHERE a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) AND i.indrelid = b.relname::regclass AND i.indisprimary) as pk,
-            b.nspname as schema_name,
-            b.relname as table_name,
-            a.attname as column_name,
-            pg_catalog.format_type(a.atttypid, a.atttypmod) as column_type,
-            CASE WHEN 
-                (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
-                 FROM pg_catalog.pg_attrdef d
-                 WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) IS NOT NULL THEN
-                'DEFAULT '|| (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
-                              FROM pg_catalog.pg_attrdef d
-                              WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
-            ELSE
-                ''
-            END as column_default_value,
-            CASE WHEN a.attnotnull = true THEN 
-                'NOT NULL'
-            ELSE
-                'NULL'
-            END as column_not_null,
-            a.attnum as attnum,
-            e.max_attnum as max_attnum
-        FROM 
-            pg_catalog.pg_attribute a
-            INNER JOIN 
-             (SELECT c.oid,
-                n.nspname,
-                c.relname
-              FROM pg_catalog.pg_class c
-                   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
-              WHERE c.relname ~ ('^('||p_table_name||')$')
-                AND pg_catalog.pg_table_is_visible(c.oid)
-              ORDER BY 2, 3) b
-            ON a.attrelid = b.oid
-            INNER JOIN 
-             (SELECT 
-                  a.attrelid,
-                  max(a.attnum) as max_attnum
-              FROM pg_catalog.pg_attribute a
-              WHERE a.attnum > 0 
-                AND NOT a.attisdropped
-              GROUP BY a.attrelid) e
-            ON a.attrelid=e.attrelid
-        WHERE a.attnum > 0 
-          AND NOT a.attisdropped
-        ORDER BY a.attnum
-    LOOP
-        IF column_record.attnum = 1 THEN
-            v_table_ddl:='CREATE TABLE '||column_record.schema_name||'.'||column_record.table_name||' (';
-        ELSE
-            v_table_ddl:=v_table_ddl||',';
-        END IF;
-
-        IF column_record.attnum <= column_record.max_attnum THEN
-            v_table_ddl:=v_table_ddl||chr(10)||
-                     '  '||column_record.column_name||' '||column_record.column_type||' '||column_record.column_default_value||' '||column_record.column_not_null;
-        END IF;
-        IF column_record.pk = 1 THEN
-          v_table_ddl:=v_table_ddl||' PRIMARY KEY';
-        END IF;
-    END LOOP;
-
-    v_table_ddl:=v_table_ddl||chr(10)||');';
-    RETURN v_table_ddl;
-END;
-$BODY$
-  LANGUAGE 'plpgsql' COST 100.0 SECURITY INVOKER;
index b0959f7c16c736e3aa23be95c96848b3d6c576b2..fd07f729923c95c1f802a7139171124a4188341c 100755 (executable)
@@ -1,23 +1,20 @@
 #!/usr/bin/env php
 <?php
-define('DB_NO_ACTION',true);
-#require_once(dirname(__FILE__).'/index.php');
 require_once(dirname(__FILE__).'/../config.php');
 require_once(dirname(__FILE__).'/../db.php');
 
 #
 # Default params
 #
-argv2request();
 
 # Set format from client Accept if != html
 #bye(out::client_type());
-Db::pdef('format',out::client_type());
+Db::pdef('format','human');
 
 #
 # Confs
 #
-$Confs = Db::conf_load(array( Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml' ));
+$Confs = Db::conf_load([ Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml' ]);
 
 #
 # Parse stdin
index 7366c378c1ce397141fea41dc7b5e2adb525033a..6a2c44828db60e89672cb9abea4a4eb664bc1aab 100644 (file)
@@ -62,7 +62,10 @@ function txt2md($txt) {
 
 function bye($msg='__bye__',$backtrace_deep=0) {
   #if ($msg) err($msg,'bye',$backtrace_deep === 0 ? 1 : $backtrace_deep);
-  if ($msg!=='__bye__') err($msg,'bye',( $backtrace_deep !== false ? (1+-1+$backtrace_deep) : $backtrace_deep ));
+  if ($msg!=='__bye__') {
+    err($msg,'bye',( $backtrace_deep !== false ? (1+-1+$backtrace_deep) : $backtrace_deep ));
+    exit(1);
+  }
   #if ($msg) err($msg,'bye',1+$backtrace_deep);
   exit;
   #die("Can't exit from BYE!!!");
index d71a3fa8d27e96f0f8e364c32e6a443cbd4b5c14..0b446678648def65ee00495b3a0b522d25fd53cf 100644 (file)
@@ -14,6 +14,7 @@ if (class_exists('NB')) return;
 class NB {
 
   const ROOT_DIR = NB_ROOT;
+  const NB_ROOT = NB_ROOT;
 
   # Sql: elect type,ext from mime_type where ext in ('json','csv','txt','yaml','xml','html','doc','xls');
   protected static $content_types_aliases = array(
diff --git a/share/postgres/postgres-create-functions.sql b/share/postgres/postgres-create-functions.sql
new file mode 100644 (file)
index 0000000..ccdf380
--- /dev/null
@@ -0,0 +1,77 @@
+-- CREATE OR REPLACE FUNCTION generate_create_table_statement(p_table_name varchar)
+CREATE OR REPLACE FUNCTION show_create_table(p_table_name varchar)
+  RETURNS text AS
+$BODY$
+DECLARE
+    v_table_ddl   text;
+    column_record record;
+BEGIN
+    FOR column_record IN 
+        SELECT 
+(SELECT 1 FROM pg_index i WHERE a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) AND i.indrelid = b.relname::regclass AND i.indisprimary) as pk,
+            b.nspname as schema_name,
+            b.relname as table_name,
+            a.attname as column_name,
+            pg_catalog.format_type(a.atttypid, a.atttypmod) as column_type,
+            CASE WHEN 
+                (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
+                 FROM pg_catalog.pg_attrdef d
+                 WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) IS NOT NULL THEN
+                'DEFAULT '|| (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
+                              FROM pg_catalog.pg_attrdef d
+                              WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
+            ELSE
+                ''
+            END as column_default_value,
+            CASE WHEN a.attnotnull = true THEN 
+                'NOT NULL'
+            ELSE
+                'NULL'
+            END as column_not_null,
+            a.attnum as attnum,
+            e.max_attnum as max_attnum
+        FROM 
+            pg_catalog.pg_attribute a
+            INNER JOIN 
+             (SELECT c.oid,
+                n.nspname,
+                c.relname
+              FROM pg_catalog.pg_class c
+                   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
+              WHERE c.relname ~ ('^('||p_table_name||')$')
+                AND pg_catalog.pg_table_is_visible(c.oid)
+              ORDER BY 2, 3) b
+            ON a.attrelid = b.oid
+            INNER JOIN 
+             (SELECT 
+                  a.attrelid,
+                  max(a.attnum) as max_attnum
+              FROM pg_catalog.pg_attribute a
+              WHERE a.attnum > 0 
+                AND NOT a.attisdropped
+              GROUP BY a.attrelid) e
+            ON a.attrelid=e.attrelid
+        WHERE a.attnum > 0 
+          AND NOT a.attisdropped
+        ORDER BY a.attnum
+    LOOP
+        IF column_record.attnum = 1 THEN
+            v_table_ddl:='CREATE TABLE '||column_record.schema_name||'.'||column_record.table_name||' (';
+        ELSE
+            v_table_ddl:=v_table_ddl||',';
+        END IF;
+
+        IF column_record.attnum <= column_record.max_attnum THEN
+            v_table_ddl:=v_table_ddl||chr(10)||
+                     '  '||column_record.column_name||' '||column_record.column_type||' '||column_record.column_default_value||' '||column_record.column_not_null;
+        END IF;
+        IF column_record.pk = 1 THEN
+          v_table_ddl:=v_table_ddl||' PRIMARY KEY';
+        END IF;
+    END LOOP;
+
+    v_table_ddl:=v_table_ddl||chr(10)||');';
+    RETURN v_table_ddl;
+END;
+$BODY$
+  LANGUAGE 'plpgsql' COST 100.0 SECURITY INVOKER;