]> de.git.xonotic.org Git - xonotic/xonstatdb.git/blob - scripts/gen_trigger.shl
1746278b84ac2444d6d8387dccb358e1bb0e13d9
[xonotic/xonstatdb.git] / scripts / gen_trigger.shl
1 #!/bin/bash
2
3 table=$1
4 start_year=$2
5 end_year=$3
6
7 printf "CREATE OR REPLACE FUNCTION %s_ins()\n" $table
8 printf "RETURNS TRIGGER AS \$\$\n"
9 printf "BEGIN\n"
10
11 for i in `seq $start_year $end_year`
12 do
13
14     if [[ start_year -eq i ]]
15     then
16         printf "\tIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
17     else
18         printf "\tELSIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
19     fi
20     printf "\t\tINSERT INTO %s_%sQ1 VALUES (NEW.*);\n" $table $i
21
22     printf "\tELSIF (NEW.create_dt >= DATE '%s-04-01' AND NEW.create_dt < DATE '%s-07-01') THEN\n" $i $i
23     printf "\t\tINSERT INTO %s_%sQ2 VALUES (NEW.*);\n" $table $i
24
25     printf "\tELSIF (NEW.create_dt >= DATE '%s-07-01' AND NEW.create_dt < DATE '%s-10-01') THEN\n" $i $i
26     printf "\t\tINSERT INTO %s_%sQ3 VALUES (NEW.*);\n" $table $i
27
28     next_year=$[i + 1]
29     printf "\tELSIF (NEW.create_dt >= DATE '%s-10-01' AND NEW.create_dt < DATE '%s-01-01') THEN\n" $i $next_year
30     printf "\t\tINSERT INTO %s_%sQ4 VALUES (NEW.*);\n" $table $i
31
32 done
33
34 printf "\tELSE\n"
35 printf "\t\tRAISE EXCEPTION 'Date out of range. Fix the %s_ins() trigger!';\n" $table
36 printf "\tEND IF;\n"
37 printf "\tRETURN NULL;\n"
38
39 printf "END\n"
40 printf "\$\$\n"
41 printf "LANGUAGE plpgsql;\n\n"
42
43 printf "DROP TRIGGER IF EXISTS %s_ins_trg ON xonstat.games;\n" $table
44 printf "CREATE TRIGGER %s_ins_trg\n" $table
45 printf "BEFORE INSERT on xonstat.%s\n" $table
46 printf "FOR EACH ROW EXECUTE PROCEDURE %s_ins();\n" $table