我试图编写一个PHP脚本,其中用户
可以对其他用户的“优点”进行评分:
create table pref_rep (
id varchar(32) references pref_users(id) check (id <> author),
author varchar(32) references pref_users(id),
author_ip inet,
good boolean,
last_rated timestamp default current_timestamp
);
防止我要删除的篡改
过去一小时内来自同一IP的同一id条目
(由于代理/路由器的偶然误报是可以的,因为它可以是一个评级,因为作者可以在任何时候重新提交,但是在我离开网站时,在不同的ID下注册一些白痴,破坏整个数据库是不合适的):
/* _author_ip will be $_SERVER['REMOTE_ADDR'] */
create or replace function pref_update_rep(_id varchar,
_author varchar, _author_ip inet,
_good boolean) returns void as $BODY$
begin
delete from pref_rep
where id = _id and
author_ip = _author_ip and
age(to_timestamp(last_rated)) < interval '1 hour';
update pref_rep set
author = _author,
author_ip = _author_ip,
good = _good,
last_rated = current_timestamp
where id = _id and author = _author;
if not found then
insert into pref_rep(id, author, author_ip, good)
values (_id, _author, _author_ip, _good);
end if;
end;
$BODY$ language plpgsql;
我有两个问题:
1) 如果我只想比较
IP地址而不是4,我该怎么办?
(是的,我知道IPv4网络的A、B、C类型,这里不重要…)
2) 我需要在表中添加索引吗
或者id和author已经被索引了?
谢谢您!
亚历克斯