initial "report class" xml and schema generator script
[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",
48                         map { join("\t", @$_) } (@primary, @other)
49                 ). ",\n\tCONSTRAINT $pkey_name PRIMARY KEY (".join(", ", map { $$_[0] } @primary).
50                 ")\n);\n";
51
52         for my $i (@indexed) {
53                 print   "CREATE INDEX \"${tname}_$$i[0]_idx\" ON $tname USING $$i[1] ($$i[0]);\n";
54         }
55         print "\n";
56
57         if ($table->getAttribute('partition')) {
58                 my ($part) = $table->getElementsByTagName('partition')->get_nodelist;
59                 my ($field) = $part->getElementsByTagName('field')->get_nodelist;
60                 my ($chunk) = $part->getElementsByTagName('chunk')->get_nodelist;
61                 my ($start) = $part->getElementsByTagName('start')->get_nodelist;
62                 my ($end) = $part->getElementsByTagName('end')->get_nodelist;
63
64                 $field = $field->textContent;
65                 $chunk = $chunk->textContent;
66                 $start = UnixDate(ParseDate($start->textContent),$chunkmap{$chunk});
67                 $end = UnixDate(ParseDate($end->textContent),$chunkmap{$chunk});
68
69                 for my $tpart ( $start .. $end ) {
70                         print   "CREATE TABLE ${tname}_${chunk}_$tpart () INHERITS ($tname);\n";
71                         print   "ALTER TABLE ${tname}_${chunk}_$tpart\n".
72                                 "\tADD CONSTRAINT \"${tname}_${chunk}_${tpart}_test\"\n".
73                                 "\tCHECK (EXTRACT('$chunk' FROM $field) = $tpart);\n";
74                         print   "CREATE RULE \"${tname}_${chunk}_${tpart}_ins_rule\" AS\n\tON INSERT TO ".
75                                 "$tname \n\tWHERE EXTRACT('$chunk' FROM NEW.$field) = $tpart ".
76                                 "\n\tDO INSTEAD INSERT INTO ${tname}_${chunk}_$tpart VALUES (NEW.*);\n";
77                         for my $i (@indexed) {
78                                 print   "CREATE INDEX \"${tname}_${chunk}_${tpart}_$$i[0]_idx\" ".
79                                         "ON ${tname}_${chunk}_$tpart USING $$i[1] ($$i[0]);\n";
80                         }
81                         print "\n";
82                 }
83         }
84         print "\n";
85
86 }
87 print "COMMIT;\n";