| 1 | # | ||
| 2 | # $Id: OracleSQL.pm,v 1.1 2005/06/30 02:01:39 rsandberg Exp $ | ||
| 3 | # | ||
| 4 | |||
| 5 | |||
| 6 | package DBIx::Knowledge::OracleSQL; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | |||
| 10 | use vars qw( $REPORT_TOTAL_KEY ); | ||
| 11 | |||
| 12 | use DBIx::Knowledge qw( $REPORT_TOTAL_KEY ); | ||
| 13 | use DBIx::Knowledge::SQL; | ||
| 14 | |||
| 15 | @DBIx::Knowledge::OracleSQL::ISA = qw( DBIx::Knowledge::SQL ); | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | DBIx::Knowledge::OracleSQL - Oracle-specific SQL generator | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | ##at add functionality for cross tab reports using Oracle CUBE() and Math::BaseCalc to convert binary -> decimal | ||
| 24 | |||
| 25 | # return sql string or undef if error | ||
| 26 | sub sql_rollup | ||
| 27 | { | ||
| 28 | my ($self,$table_name,$select,$where_clause,$aggr_summary_fields,$order,$want_aggr_grouping_ids) = @_; | ||
| 29 | $want_aggr_grouping_ids ||= {}; | ||
| 30 | |||
| 31 | (($self->{errstr} = "No fields to select for report"),return undef) unless ref($select) && @$select; | ||
| 32 | |||
| 33 | my $sql = 'SELECT ' . join(',',map($_->{select_sql},@$select)) . ' '; | ||
| 34 | |||
| 35 | my %select_position = (); | ||
| 36 | my $sel_idx = 0; | ||
| 37 | foreach my $s (@$select) | ||
| 38 | { | ||
| 39 | $select_position{$s->{ID}} = $sel_idx++; | ||
| 40 | } | ||
| 41 | |||
| 42 | my @group_by = (); | ||
| 43 | my %group_by = (); | ||
| 44 | map { (push(@group_by,$_),$group_by{lc($_->{select_sql})} = 1) if !$_->{group_by_expr} && !exists($group_by{lc($_->{select_sql})}) } @$select; | ||
| 45 | |||
| 46 | my $aggr_summary_fields_count_less_report_total = @$aggr_summary_fields - scalar($aggr_summary_fields->[$#$aggr_summary_fields] eq $REPORT_TOTAL_KEY) | ||
| 47 | if @$aggr_summary_fields; | ||
| 48 | |||
| 49 | # If $aggr_summary_fields is given then @group_by must have at least one value | ||
| 50 | # Should simply be a list of ID values | ||
| 51 | ref($aggr_summary_fields) or $aggr_summary_fields = []; | ||
| 52 | |||
| 53 | # Should be in order of decreasing record characterization specificity (normally the reverse order of the sort order (or select order)) | ||
| 54 | # if $REPORT_TOTAL_KEY is used it should always be the last in the list | ||
| 55 | my $invalid_groupings = 0; | ||
| 56 | for (my $i = 0; $i < @$aggr_summary_fields; $i++) | ||
| 57 | { | ||
| 58 | my $id = $aggr_summary_fields->[$i]; | ||
| 59 | |||
| 60 | my $grouping_id = 0; | ||
| 61 | if ($id eq $REPORT_TOTAL_KEY) | ||
| 62 | { | ||
| 63 | $grouping_id = (1 << @group_by) - 1; | ||
| 64 | } | ||
| 65 | else | ||
| 66 | { | ||
| 67 | my $j = 0; | ||
| 68 | foreach my $f (reverse @group_by) | ||
| 69 | { | ||
| 70 | if ($f->{ID} eq $id) | ||
| 71 | { | ||
| 72 | $grouping_id = (1 << $j) - 1 if $j; | ||
| 73 | last; | ||
| 74 | } | ||
| 75 | $j++; | ||
| 76 | } | ||
| 77 | ($self->{errstr} = "Unable to summarize by $id in this report") unless $grouping_id; | ||
| 78 | } | ||
| 79 | |||
| 80 | # SUBTOTAL_RANK could get screwed up here if there is an invalid entry in $aggr_summary_fields ($grouping_id is false) despite $invalid_groupings | ||
| 81 | # Perhaps best to return undef after setting {errstr} above. | ||
| 82 | # POSITION of undef is the REPORT TOTAL field (equivalent to 0 if need be) | ||
| 83 | $want_aggr_grouping_ids->{$grouping_id} = { FIELD_ID => $id, SUBTOTAL_RANK => | ||
| 84 | abs(scalar(keys(%$want_aggr_grouping_ids)) - ($aggr_summary_fields_count_less_report_total - $invalid_groupings)), | ||
| 85 | POSITION => $select_position{$id} } if $grouping_id; | ||
| 86 | |||
| 87 | $invalid_groupings++ unless $grouping_id; | ||
| 88 | } | ||
| 89 | |||
| 90 | my $init_group_by = ''; | ||
| 91 | $init_group_by = (%$want_aggr_grouping_ids ? "ROLLUP(" : '') . join(',',map($_->{select_sql},@group_by)) . (%$want_aggr_grouping_ids ? ")" : '') if @group_by; | ||
| 92 | |||
| 93 | my $grouping_id_sql = ''; | ||
| 94 | $sql .= "," . ($grouping_id_sql = "GROUPING_ID(" . join(',',map($_->{select_sql},@group_by)) . ")") . ' ' if @group_by && %$want_aggr_grouping_ids; | ||
| 95 | my $default_order_by_sql = ''; | ||
| 96 | my @default_order_expr = (); | ||
| 97 | |||
| 98 | # If the column has a {group_by_sql} it is added after GROUP BY ROLLUP(...), below and screws up the ROLLUP summary columns | ||
| 99 | map { push(@default_order_expr,length($_->{order_by_sql}) ? $_->{order_by_sql} : $_->{select_sql}) . ($_->{descending_order} ? ' DESC' : '') | ||
| 100 | unless $_->{no_default_order_by} } @group_by; | ||
| 101 | $default_order_by_sql = join(',',@default_order_expr); | ||
| 102 | |||
| 103 | $sql .= "FROM $table_name "; | ||
| 104 | $sql .= "$where_clause " if $where_clause; | ||
| 105 | |||
| 106 | my $add_group_by = ''; | ||
| 107 | foreach my $select_field (@$select) | ||
| 108 | { | ||
| 109 | # {group_by_sql} attr must be an array ref if specified, {group_by_expr} must be true | ||
| 110 | my $gr = $select_field->{group_by_sql} or next; | ||
| 111 | (($self->{errstr} = "{group_by_expr} or {order_by_sql} must be true for $select_field->{ID} because {group_by_sql} is specified"),return undef) | ||
| 112 | unless $select_field->{group_by_expr} || length($select_field->{order_by_sql}); | ||
| 113 | |||
| 114 | foreach my $g (@$gr) | ||
| 115 | { | ||
| 116 | $add_group_by .= "$g," unless $group_by{lc($g)}; | ||
| 117 | $group_by{lc($g)} = 1; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | chop($add_group_by); | ||
| 121 | |||
| 122 | # order of terms that appear in ROLLUP() is important! Must be same order as select list | ||
| 123 | $sql .= "GROUP BY " . ($init_group_by ? "$init_group_by ," : '') . ($add_group_by ? "$add_group_by " : '') if $add_group_by || $init_group_by; | ||
| 124 | chop($sql) if $add_group_by || $init_group_by; | ||
| 125 | |||
| 126 | # Could optimize away if all groups are wanted | ||
| 127 | $sql .= "HAVING $grouping_id_sql IN (" . join(',',keys(%$want_aggr_grouping_ids),0) . ") " if %$want_aggr_grouping_ids; | ||
| 128 | |||
| 129 | # $order should be an array ref to field hashes | ||
| 130 | if (ref($order) && @$order) | ||
| 131 | { | ||
| 132 | $sql .= "ORDER BY " . join(',',map((length($_->{order_by_sql}) ? $_->{order_by_sql} : $_->{select_sql}) | ||
| 133 | . ($_->{descending_order} ? ' DESC' : ''),@$order)) | ||
| 134 | . ($grouping_id_sql ? ",$grouping_id_sql" : ''); | ||
| 135 | } | ||
| 136 | elsif ($default_order_by_sql) | ||
| 137 | { | ||
| 138 | $sql .= "ORDER BY $default_order_by_sql" . ($grouping_id_sql ? ",$grouping_id_sql" : ''); | ||
| 139 | } | ||
| 140 | |||
| 141 | return $sql; | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; | ||
| 145 | |||
| 146 | __END__ |