From: Nicolas Boisselier Date: Thu, 23 Jul 2015 10:52:46 +0000 (+0100) Subject: json header X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=958e52abd9d574a3de0b926c73798d983523e404;p=nb.git json header --- 958e52abd9d574a3de0b926c73798d983523e404 diff --cc lib/php/db.php index 631ed66e,de32a929..9e6f1a3a --- a/lib/php/db.php +++ b/lib/php/db.php @@@ -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; diff --cc lib/php/db/postgres-create-functions.sql index 00000000,00000000..ccdf3803 new file mode 100644 --- /dev/null +++ b/lib/php/db/postgres-create-functions.sql @@@ -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; diff --cc lib/php/db/table.php index b1f19224,28e7500d..cbcee08d --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@@ -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');