<分区>
在 Postgres High (PSD 9.6) 中, Nerd 和欢乐合唱团处于一场奇怪的相互依赖的 war 中。 Nerd 们密谋针对合唱团的 secret 计划。欢乐合唱团以悦耳的歌曲回应。
如果 Nerd 退缩,合唱团将永远不会回应。类似地, Nerd 们只是想激怒合唱团,所以如果合唱团没有回应, Nerd 们实际上不会调用他们的 secret 计划。
作为首席程序员,我的工作是确保一次只发生一次可能的 Nerd 叛乱尝试。我还必须确保 nerds 和 glee club 都成功或都不成功。但是那些 麻烦的 Nerd ,总是尝试 sql 注入(inject)攻击。也得防止那个。正如 Postgres High 的所有优秀领导者所做的那样,我们用 SQL 编写类(class)计划。
类(class)计划:
begin;
-- stop race conditions with a hall monitor
select from semaphore where name = 'hall_monitor' for update;
prepare nerd_rebellion (
text, -- secret_mission
date -- when_we_strike
) as
update ultimate_plan
set secret_mission = $1
where when_we_strike = $2;
prepare glee_club_counterstrike (
text, -- happy_song
boolean -- kill_them_with_love
) as
insert into song_therapy (
happy_song,
kill_them_with_love
) values (
$1,
$2
)
execute nerd_rebellion(
'Nerds do stuff like this ; drop table song_therapy --f you glee club',
'2017-01-01'
);
execute glee_club_counterstrike(
'god_bless_america',
true
);
-- let us never speak of this again ...
deallocate nerd_rebellion
deallocate glee_club_counterstrike
-- all done. Release the hall monitor
commit;
天哪,校长要做的事情太多了。我们的类时间很短,我们有所有这些准备和分配。啊。真的减少了我喝咖啡和 donut 的时间。但是我需要在单个事务中包装多个语句并使用信号量进行序列化。也不能在 SQL 注入(inject)上妥协。我没有看到更简单的解决方案。你?