7 printf "CREATE OR REPLACE FUNCTION %s_ins()\n" $table
8 printf "RETURNS TRIGGER AS \$\$\n"
11 for i in `seq $start_year $end_year`
14 if [[ start_year -eq i ]]
16 printf "\tIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
18 printf "\tELSIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
20 printf "\t\tINSERT INTO %s_%sQ1 VALUES (NEW.*);\n" $table $i
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
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
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
35 printf "\t\tRAISE EXCEPTION 'Date out of range. Fix the %s_ins() trigger!';\n" $table
37 printf "\tRETURN NULL;\n"
41 printf "LANGUAGE plpgsql;\n\n"
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