datatype_for_encrypt_function_in_postgresql

ALTER TABLE SEC.TEST ADD V_smallint smallint NOT NULL DEFAULT 3;
ALTER TABLE SEC.TEST ADD V_integer integer NOT NULL DEFAULT 3;
ALTER TABLE SEC.TEST ADD V_bigint bigint NOT NULL DEFAULT 3;
ALTER TABLE SEC.TEST ADD V_decimal decimal(10,4) NOT NULL DEFAULT 3.111;
ALTER TABLE SEC.TEST ADD V_numeric numeric(10,4) NOT NULL DEFAULT 3.112;
ALTER TABLE SEC.TEST ADD V_real real NOT NULL DEFAULT 3;
ALTER TABLE SEC.TEST ADD V_double_precision double precision NOT NULL DEFAULT 3;
ALTER TABLE SEC.TEST ADD V_serial serial;
ALTER TABLE SEC.TEST ADD V_bigserial bigserial NOT NULL;

ALTER TABLE SEC.TEST ADD V_character_varying character varying(200) NOT NULL DEFAULT 'a3';
ALTER TABLE SEC.TEST ADD V_varchar varchar(20) NOT NULL DEFAULT 'a3';

ALTER TABLE SEC.TEST ADD V_character character(200) NOT NULL DEFAULT 'a3';
ALTER TABLE SEC.TEST ADD V_char char(20) NOT NULL DEFAULT 'a3';
ALTER TABLE SEC.TEST ADD V_text text NOT NULL DEFAULT 'a3';

ALTER TABLE SEC.TEST ADD V_bytea bytea NOT NULL DEFAULT '//134'::bytea;

ALTER TABLE SEC.TEST ADD V_timestamp_without_time_zone timestamp(4) without time zone NOT NULL DEFAULT '1999-01-08 04:05:06';
ALTER TABLE SEC.TEST ADD V_timestamp_with_time_zone timestamp(4) with time zone NOT NULL DEFAULT '1999-01-08 04:05:06 -8:00';
--ALTER TABLE SEC.TEST ADD V_interval interval NOT NULL DEFAULT 3::interval;--cannot cast type integer to interval
ALTER TABLE SEC.TEST ADD V_datea date NOT NULL DEFAULT now();
ALTER TABLE SEC.TEST ADD V_time_without_time_zone time(4) without time zone NOT NULL DEFAULT '1999-01-08 04:05:06';
ALTER TABLE SEC.TEST ADD V_time_with_time_zone time(4) with time zone NOT NULL DEFAULT '1999-01-08 04:05:06 -8:00';

-----------------------------------------------------------------------------------------
--确定加解密函数,加密时数值类型需要先转换为varchar类型然后转换为bytea类型;解密时需要转换为对应的数据类型
---解密后类型转换;decimal精度待定;解密后用::bytea转换等价于原值
SELECT t.COLUMN_NAME,t.data_type,t.data_length,t.data_default
,'SELECT '||T.COLUMN_NAME||',convert_from(DECRYPT'||'(ENCRYPT('||T.COLUMN_NAME||CASE WHEN T.DATA_TYPE IN ('int2','int4','int8','numeric','float4','float8','timestamp','timestamptz','date','time','timetz') THEN '::VARCHAR' ELSE '' END||'::BYTEA,''key'',''aes''),''key'',''aes''),''SQL_ASCII'') FROM sec.test t 
where '||
  CASE WHEN T.DATA_TYPE IN ('int2','int4','int8','numeric','float4','float8','bpchar','bytea','timestamp','timestamptz','date','time','timetz') THEN 
    T.COLUMN_NAME||'=convert_from(DECRYPT'||'(ENCRYPT('||T.COLUMN_NAME||CASE WHEN T.DATA_TYPE IN ('int2','int4','int8','numeric','float4','float8','timestamp','timestamptz','date','time','timetz') THEN '::VARCHAR' ELSE '' END||'::BYTEA,''key'',''aes''),''key'',''aes''),''SQL_ASCII'')'||'::'||t.data_type
  else 
    T.COLUMN_NAME||'=convert_from(DECRYPT'||'(ENCRYPT('||T.COLUMN_NAME||CASE WHEN T.DATA_TYPE IN ('int2','int4','int8','numeric','float4','float8','timestamp','timestamptz','date','time','timetz') THEN '::VARCHAR' ELSE '' END||'::BYTEA,''key'',''aes''),''key'',''aes''),''SQL_ASCII'')'
  end
  ||';'
 from sbdc.user_table_cols t where t.schema_name='sec' and t."table_name"='test';

 

posted @ 2020-03-13 14:53  virtual_daemon  阅读(184)  评论(0编辑  收藏  举报