上期我们介绍了密态查询和使用jdbc连接密态数据库的操作。本期来介绍密态支持函数/存储过程。openGauss 3.0.0版本只支持sql和PL/pgsql两种语言。由于密态支持存储过程中创建和执行函数/存储过程对用户是无感知的,因此使用时语法和非密态无区别。
密态等值查询支持函数存储过程特性新增了系统表gs_encrypted_proc,用于存储参数返回的原始数据类型。下面来看下一些示例。
创建并执行涉及加密列的函数/存储过程
-
创建密钥,详细步骤请参考 使用gsql操作密态数据库和使用JDBC操作密态数据库 。
-
创建加密表。
openGauss=# CREATE TABLE creditcard_info ( id_number int, name text, credit_card varchar(19) encrypted with (column_encryption_key = ImgCEK1, encryption_type = DETERMINISTIC) ) with (orientation=row); CREATE TABLE
-
插入数据。
openGauss=# insert into creditcard_info values(1, 'Avi', '1234567890123456'); INSERT 0 1 openGauss=# insert into creditcard_info values(2, 'Eli', '2345678901234567'); INSERT 0 1
-
创建函数支持密态等值查询。
openGauss=# CREATE FUNCTION f_encrypt_in_sql(val1 text, val2 varchar(19)) RETURNS text AS 'SELECT name from creditcard_info where name=$1 or credit_card=$2 LIMIT 1' LANGUAGE SQL; CREATE FUNCTION openGauss=# CREATE FUNCTION f_encrypt_in_plpgsql (val1 text, val2 varchar(19)) RETURNS text AS $$ DECLARE c text; BEGIN SELECT into c name from creditcard_info where name=$1 or credit_card =$2 LIMIT 1; RETURN c; END; $$ LANGUAGE plpgsql; CREATE FUNCTION
-
执行函数。
openGauss=# SELECT f_encrypt_in_sql('Avi','1234567890123456'); f_encrypt_in_sql ------------------ (1 row) openGauss=# SELECT f_encrypt_in_plpgsql('Avi', val2=>'1234567890123456'); f_encrypt_in_plpgsql