tp3.2 版本的bug --->查看 http://www.thinkphp.cn/bug/3413.html
1.修改 ThinkPHP\Library\Think\Db\Driver\Pgsql.class.PHP 里的 getFields() 方法,通过转义符把单引号添加修改为
$result = $this->query('select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\''.$tableName.'\');');
2.为postgresql添加 函数 table_msg --> http://lovejuan1314.iteye.com/blog/1167680
postgresql的sql管理器执行 如下代码(代码在tp5的\thinkphp\library\think\db\connector\pgsql.sql)目录
1 CREATE OR REPLACE FUNCTION pgsql_type(a_type varchar) RETURNS varchar AS 2 $BODY$ 3 DECLARE 4 v_type varchar; 5 BEGIN 6 IF a_type='int8' THEN 7 v_type:='bigint'; 8 ELSIF a_type='int4' THEN 9 v_type:='integer'; 10 ELSIF a_type='int2' THEN 11 v_type:='smallint'; 12 ELSIF a_type='bpchar' THEN 13 v_type:='char'; 14 ELSE 15 v_type:=a_type; 16 END IF; 17 RETURN v_type; 18 END; 19 $BODY$ 20 LANGUAGE PLPGSQL; 21 22 CREATE TYPE "public"."tablestruct" AS ( 23 "fields_key_name" varchar(100), 24 "fields_name" VARCHAR(200), 25 "fields_type" VARCHAR(20), 26 "fields_length" BIGINT, 27 "fields_not_null" VARCHAR(10), 28 "fields_default" VARCHAR(500), 29 "fields_comment" VARCHAR(1000) 30 ); 31 32 CREATE OR REPLACE FUNCTION "public"."table_msg" (a_schema_name varchar, a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS 33 $body$ 34 DECLARE 35 v_ret tablestruct; 36 v_oid oid; 37 v_sql varchar; 38 v_rec RECORD; 39 v_key varchar; 40 BEGIN 41 SELECT 42 pg_class.oid INTO v_oid 43 FROM 44 pg_class 45 INNER JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid AND lower(pg_namespace.nspname) = a_schema_name) 46 WHERE 47 pg_class.relname=a_table_name; 48 IF NOT FOUND THEN 49 RETURN; 50 END IF; 51 52 v_sql=' 53 SELECT 54 pg_attribute.attname AS fields_name, 55 pg_attribute.attnum AS fields_index, 56 pgsql_type(pg_type.typname::varchar) AS fields_type, 57 pg_attribute.atttypmod-4 as fields_length, 58 CASE WHEN pg_attribute.attnotnull THEN ''not null'' 59 ELSE '''' 60 END AS fields_not_null, 61 pg_attrdef.adsrc AS fields_default, 62 pg_description.description AS fields_comment 63 FROM 64 pg_attribute 65 INNER JOIN pg_class ON pg_attribute.attrelid = pg_class.oid 66 INNER JOIN pg_type ON pg_attribute.atttypid = pg_type.oid 67 LEFT OUTER JOIN pg_attrdef ON pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = pg_attribute.attnum 68 LEFT OUTER JOIN pg_description ON pg_description.objoid = pg_class.oid AND pg_description.objsubid = pg_attribute.attnum 69 WHERE 70 pg_attribute.attnum > 0 71 AND attisdropped <> ''t'' 72 AND pg_class.oid = ' || v_oid || ' 73 ORDER BY pg_attribute.attnum' ; 74 FOR v_rec IN EXECUTE v_sql LOOP 75 v_ret.fields_name=v_rec.fields_name; 76 v_ret.fields_type=v_rec.fields_type; 77 IF v_rec.fields_length > 0 THEN 78 v_ret.fields_length:=v_rec.fields_length; 79 ELSE 80 v_ret.fields_length:=NULL; 81 END IF; 82 v_ret.fields_not_null=v_rec.fields_not_null; 83 v_ret.fields_default=v_rec.fields_default; 84 v_ret.fields_comment=v_rec.fields_comment; 85 SELECT constraint_name INTO v_key FROM information_schema.key_column_usage WHERE table_schema=a_schema_name AND table_name=a_table_name AND column_name=v_rec.fields_name; 86 IF FOUND THEN 87 v_ret.fields_key_name=v_key; 88 ELSE 89 v_ret.fields_key_name=''; 90 END IF; 91 RETURN NEXT v_ret; 92 END LOOP; 93 RETURN ; 94 END; 95 $body$ 96 LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER; 97 COMMENT ON FUNCTION "public"."table_msg"(a_schema_name varchar, a_table_name varchar) 98 IS '获得表信息'; 99 ---重载一个函数 100 CREATE OR REPLACE FUNCTION "public"."table_msg" (a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS 101 $body$ 102 DECLARE 103 v_ret tablestruct; 104 BEGIN 105 FOR v_ret IN SELECT * FROM table_msg('public',a_table_name) LOOP 106 RETURN NEXT v_ret; 107 END LOOP; 108 RETURN; 109 END; 110 $body$ 111 LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER; 112 COMMENT ON FUNCTION "public"."table_msg"(a_table_name varchar) 113 IS '获得表信息';