vip_until
在一个文字游戏的 PostgreSQL 表中,我通过列或grand_until
具有未来有效日期的列来跟踪付费玩家:
create table users (
uid serial primary key,
vip_until timestamp null, -- date in future indicates paying customer
grand_until timestamp null -- date in future indicates paying customer
);
我写了一个简短的存储过程来检查:
create or replace function is_vip(
IN in_uid integer,
OUT out_vip boolean
) as $BODY$
BEGIN
out_vip := exists(select 1 from users
where uid = in_uid and
greatest(vip_until, grand_until) > current_timestamp);
END;
$BODY$ language plpgsql;
然后我尝试在另一个存储过程中使用上述函数:
create or replace function join_new_game(
IN in_uid integer,
IN in_letters varchar(130),
IN in_style integer,
OUT out_gid integer
) as $BODY$
BEGIN
/* maybe there is a new game already, just waiting for the player's 1st move*/
select gid into out_gid from games
where (player1 = in_uid and stamp1 is null)
or (player2 = in_uid and stamp2 is null) limit 1;
IF not found THEN
/* try to find games having just 1 player (with different uid) */
select gid into out_gid from games
where (player1 != in_uid and stamp1 is not null
and player2 is null) limit 1;
IF not found THEN
/* only allow board style 1 for non-paying customers */
IF not select is_vip(in_uid) THEN
in_style := 1; -- the above line fails
END IF;
/* create new game with player1 = uid and stamp1 = null */
insert into games (
created,
player1,
stamp1,
stamp2,
letters1,
letters2,
letters,
board,
style
) values (
current_timestamp,
in_uid,
null,
null,
substring(in_letters, 1, 7),
substring(in_letters, 8, 7),
substring(in_letters, 15),
rpad('', 225), -- fill 15x15 board
in_style
) returning gid into out_gid;
ELSE
update games set player2 = in_uid where gid = out_gid;
END IF;
END IF;
END;
$BODY$ language plpgsql;
但我得到这个语法错误:
ERROR: syntax error at or near "select" LINE 21: IF not select is_vip(in_uid) TH... ^
如何is_vip()
正确使用该功能?