]> git.nbdom.net Git - nb.git/commitdiff
json header
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 23 Jul 2015 10:52:46 +0000 (11:52 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 23 Jul 2015 10:52:46 +0000 (11:52 +0100)
1  2 
lib/php/db.php
lib/php/db/postgres-create-functions.sql
lib/php/db/table.php

diff --cc lib/php/db.php
index 631ed66e39de4c4f6eb39b6652c5460f1174cae3,de32a92909b15ea549168bae90f8ae4b6bb22938..9e6f1a3a87a49e4cb8b753baeeb697612a3c5c0d
@@@ -189,6 -202,6 +202,9 @@@ EOF
      } elseif ($this->p('format')=='xml') {
        header('Content-type: text/xml');
  
++    } elseif ($this->p('format')=='json') {
++      header('Content-type: application/json');
++
      } else {
  
        return false;
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..ccdf3803514d6efe63a2535c35b021f4e30bac25
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,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;
index b1f19224c6e9b1bb8318660fa30d6be644e77aa3,28e7500d017924d5c19dbc22df9063e1a1f679f7..cbcee08d375d7bf8675f32a8b4f5595abf9874e2
@@@ -2,14 -2,16 +2,16 @@@
  require_once(dirname(__FILE__).'/../functions.php');
  require_once(dirname(__FILE__).'/../db.php');
  
- if (empty($_SERVER['DOCUMENT_ROOT'])) {
-   $_REQUEST['db'] = 'rent'; $name = 'test';
-   #$_REQUEST['db'] = 'rt'; $name = 'classes';
+ if (false and empty($_SERVER['DOCUMENT_ROOT'])) {
+   $_REQUEST['db'] = 'rent'; $name = 'tenant';
+   $_REQUEST['db'] = 'rt'; $name = 'classes';
+   $_REQUEST['db'] = 'mysql'; $name = 'addr';
+   $_REQUEST['db'] = 'izi'; $name = 'zone';
    require_once('/opt/rent/www/config/db.php');
 -  #$db = new db('mysql:host=big;port=3306;dbname=rent;user=nico;password=');
    print_r($Db->tables());
    $Table = $Db->table($name);
 -  print_r($Table->sql());
 +  print_r($Table->fields());
 +  #print_r($Table->sql());
  }
  
  if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');