psql(9.2.23) 我使用用PGsql编写的函数从数据库中获取用户凭据。 这是数据库存储的内容。
directory=# select * from entusercredentials;
lastUpdate | userName | nAttribute | tag | pAttribute |
uri
----------------------------+----------+------------+------+------------------+-
----------------------
2018-12-10 09:18:35.103+00 | admin | admin | test | 71A2E03E670C3799 |
admin@acmepacket.com
2018-12-10 09:18:35.103+00 | apple | apple | test | A489BB760335F6F5 |
2018-12-10 09:18:35.103+00 | user01 | user01 | test | 71A2E03E670C3799 |
2018-12-10 09:18:35.103+00 | user02 | user02 | test | 71A2E03E670C3799 |
user02@acmepacket.com
2018-12-10 09:18:35.103+00 | user03 | user03 | test | 71A2E03E670C3799 |
user03@acmepacket.com
(5 rows)
我使用了一个函数来获取数据。 当“ uri”不为空时,它可以正常工作,但是我注意到,如果“ uri”为空,则“ nAttribute”和“ pAttribute”将丢失。
directory=# SELECT * FROM sp_fetch_user_credentials('admin', 'test');
userName | groups | sipAddress | nAttribute | pAttribute |
uri
----------+---------+--------------------------+------------+------------------+
----------------------
admin | group01 | sip:admin@acmepacket.com | admin | 71A2E03E670C3799 |
admin@acmepacket.com
(1 row)
directory=# SELECT * FROM sp_fetch_user_credentials('apple', 'test');
userName | groups | sipAddress | nAttribute | pAttribute | uri
----------+---------+--------------------------+------------+------------+-----
apple | group01 | sip:apple@acmepacket.com | | |
(1 row)
directory=#
这是我的功能:
CREATE OR REPLACE FUNCTION sp_fetch_user_credentials(text, text)
RETURNS SETOF type_usercredentials AS
$BODY$
DECLARE
sName ALIAS FOR $1;
sTag ALIAS FOR $2;
iCnt int;
sGroups text;
select_statement text;
tmp_grp_rec record;
tmp_cred_rec record;
user_cred type_usercredentials;
BEGIN
SET ENABLE_SEQSCAN = OFF;
SELECT into tmp_cred_rec * FROM entusercredentials WHERE "nAttribute"= sName AND "tag" = sTag;
IF (tmp_cred_rec IS NOT NULL) THEN
IF (tmp_cred_rec."nAttribute" IS NOT NULL) THEN
user_cred."nAttribute" = tmp_cred_rec."nAttribute";
END IF;
IF (tmp_cred_rec."pAttribute" IS NOT NULL) THEN
user_cred."pAttribute" = tmp_cred_rec."pAttribute";
END IF;
IF (tmp_cred_rec."uri" IS NOT NULL) THEN
user_cred."uri" = tmp_cred_rec."uri";
END IF;
END IF;
user_cred."userName" = sName;
IF (tmp_cred_rec."userName" IS NOT NULL) THEN
SELECT into user_cred."sipAddress" "sipAddress" FROM entusername WHERE "userName"= tmp_cred_rec."userName" AND "tag"= sTag;
select_statement = 'SELECT "groupName" FROM entusergroup WHERE
"userName"=$q$' || tmp_cred_rec."userName" || '$q$ AND "tag"=''' || sTag || ''' ORDER BY "groupName"';
iCnt := 0;
FOR tmp_grp_rec IN EXECUTE select_statement
LOOP
IF (iCnt = 0) THEN
sGroups := tmp_grp_rec."groupName";
ELSE
sGroups := sGroups || '\t' || tmp_grp_rec."groupName";
END IF;
iCnt := iCnt + 1;
END LOOP;
user_cred."groups" := sGroups;
END IF;
RETURN NEXT user_cred;
END;
$BODY$
希望有人可以帮助我。预先感谢。