7 for year in `seq $start_year $end_year`
9 for qtr in "q1" "q2" "q3" "q4"
11 printf "CREATE TABLE IF NOT EXISTS xonstat.%s_%s%s ( \n" $table $year $qtr
14 printf "\tCHECK ( create_dt >= DATE '%s-01-01' AND create_dt < DATE '%s-04-01' ) \n" $year $year
15 elif [[ $qtr = "q2" ]]
17 printf "\tCHECK ( create_dt >= DATE '%s-04-01' AND create_dt < DATE '%s-07-01' ) \n" $year $year
18 elif [[ $qtr = "q3" ]]
20 printf "\tCHECK ( create_dt >= DATE '%s-07-01' AND create_dt < DATE '%s-10-01' ) \n" $year $year
21 elif [[ $qtr = "q4" ]]
24 printf "\tCHECK ( create_dt >= DATE '%s-10-01' AND create_dt < DATE '%s-01-01' ) \n" $year $next_year
27 printf ") INHERITS (%s);\n\n" $table
30 printf "CREATE INDEX %s_%s%s_ix001 on %s_%s%s(create_dt);\n" $table $year $qtr $table $year $qtr
32 # conditional indexes that depend on the table
33 if [[ $table = "player_game_stats" || $table = "player_weapon_stats" ]]
35 printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
36 printf "CREATE INDEX %s_%s%s_ix003 on %s_%s%s(player_id);\n" $table $year $qtr $table $year $qtr
39 if [[ $table = "team_game_stats" ]]
41 printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
48 printf "CREATE OR REPLACE FUNCTION %s_ins()\n" $table
49 printf "RETURNS TRIGGER AS \$\$\n"
52 for i in `seq $start_year $end_year`
55 if [[ start_year -eq i ]]
57 printf "\tIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
59 printf "\tELSIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
61 printf "\t\tINSERT INTO %s_%sQ1 VALUES (NEW.*);\n" $table $i
63 printf "\tELSIF (NEW.create_dt >= DATE '%s-04-01' AND NEW.create_dt < DATE '%s-07-01') THEN\n" $i $i
64 printf "\t\tINSERT INTO %s_%sQ2 VALUES (NEW.*);\n" $table $i
66 printf "\tELSIF (NEW.create_dt >= DATE '%s-07-01' AND NEW.create_dt < DATE '%s-10-01') THEN\n" $i $i
67 printf "\t\tINSERT INTO %s_%sQ3 VALUES (NEW.*);\n" $table $i
70 printf "\tELSIF (NEW.create_dt >= DATE '%s-10-01' AND NEW.create_dt < DATE '%s-01-01') THEN\n" $i $next_year
71 printf "\t\tINSERT INTO %s_%sQ4 VALUES (NEW.*);\n" $table $i
76 printf "\t\tRAISE EXCEPTION 'Date out of range. Fix the %s_ins() trigger!';\n" $table
78 printf "\tRETURN NULL;\n"
82 printf "LANGUAGE plpgsql;\n\n"
84 printf "DROP TRIGGER IF EXISTS %s_ins_trg ON xonstat.%s;\n" $table $table
85 printf "CREATE TRIGGER %s_ins_trg\n" $table
86 printf "BEFORE INSERT on xonstat.%s\n" $table
87 printf "FOR EACH ROW EXECUTE PROCEDURE %s_ins();\n" $table