d9f4bc9c2f4c22af9505e38f1bf62fa4152e14e8
[Evergreen.git] / Open-ILS / src / reporter / reporter.schema-gen.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3 use XML::LibXML;
4 use Date::Manip;
5
6 my %chunkmap =
7         (       doy => '%j',
8                 woy => '%U',
9                 month => '%m',
10                 year => '%Y',
11         );
12
13
14 my $parser = XML::LibXML->new;
15 my $doc = $parser->parse_file($ARGV[0]);
16 $parser->process_xincludes($doc);
17
18 print "BEGIN;\n\n";
19 for my $table ($doc->findnodes('/reporter/tables/table')) {
20         my $tname = $table->getElementsByTagName('tablename')->string_value;
21         
22         (my $pkey_name = $tname) =~ s/\./_/gso;
23         $pkey_name .= '_pkey';
24         warn "$tname\n";
25
26         my (@primary,@other,@indexed);
27         for my $field ($table->findnodes('fields/field')) {
28                 my $fname = $field->getAttribute('name');
29                 my $fdatatype = $field->getAttribute('create-type') || $field->getAttribute('datatype');
30                 warn "\t$fname\n";
31                 
32                 if ($field->getAttribute('indexed')) {
33                         my $itype = $field->getAttribute('index-type') || 'BTREE';
34                         push @indexed, [$fname, $itype];
35                 }
36
37                 if ($field->getAttribute('primary')) {
38                         push @primary, [$fname, $fdatatype];
39                 } else {
40                         push @other, [$fname, $fdatatype];
41                 }
42         }
43
44         warn "\n";
45         print   "DROP TABLE $tname CASCADE;\n";
46         print   "CREATE TABLE $tname (\n\t".
47                 join(",\n\t", map { join("\t", @$_) } (@primary, @other))."\n".
48                 do {
49                         @primary ?
50                                 ",\tCONSTRAINT $pkey_name PRIMARY KEY (".
51                                         join(", ", map { $$_[0] } @primary). ")\n" :
52                                 ''
53                 }.
54                 ");\n";
55
56         print "\n";
57
58         if ($table->getAttribute('partition')) {
59                 my ($part) = $table->getElementsByTagName('partition')->get_nodelist;
60                 my ($field) = $part->getElementsByTagName('field')->get_nodelist;
61                 my ($chunk) = $part->getElementsByTagName('chunk')->get_nodelist;
62                 my ($start) = $part->getElementsByTagName('start')->get_nodelist;
63                 my ($end) = $part->getElementsByTagName('end')->get_nodelist;
64
65                 $field = $field->textContent;
66                 $chunk = $chunk->textContent;
67                 $start = UnixDate(ParseDate($start->textContent),$chunkmap{$chunk});
68                 $end = UnixDate(ParseDate($end->textContent),$chunkmap{$chunk});
69
70                 for my $tpart ( $start .. $end ) {
71                         print   "CREATE TABLE ${tname}_${chunk}_$tpart () INHERITS ($tname);\n";
72                         print   "ALTER TABLE ${tname}_${chunk}_$tpart\n".
73                                 "\tADD CONSTRAINT \"${tname}_${chunk}_${tpart}_test\"\n".
74                                 "\tCHECK (EXTRACT('$chunk' FROM $field) = $tpart);\n";
75                         print   "CREATE RULE \"${tname}_${chunk}_${tpart}_ins_rule\" AS\n\tON INSERT TO ".
76                                 "$tname \n\tWHERE EXTRACT('$chunk' FROM NEW.$field) = $tpart ".
77                                 "\n\tDO INSTEAD INSERT INTO ${tname}_${chunk}_$tpart VALUES (NEW.*);\n";
78                         for my $i (@indexed) {
79                                 print   "CREATE INDEX \"${tname}_${chunk}_${tpart}_$$i[0]_idx\" ".
80                                         "ON ${tname}_${chunk}_$tpart USING $$i[1] ($$i[0]);\n";
81                         }
82                         print "\n";
83                 }
84         } else {
85                 for my $i (@indexed) {
86                         print   "CREATE INDEX \"${tname}_$$i[0]_idx\" ON $tname USING $$i[1] ($$i[0]);\n";
87                 }
88         }
89         print "\n";
90
91 }
92 print "COMMIT;\n";