cvsuser     06/02/05 20:21:06

  Modified:    App-Repository/lib/App/Repository DBI.pm
  Log:
  add support for literal aggregation functions. rename summarykeys to group_by
  
  Revision  Changes    Path
  1.35      +37 -23    p5ee/App-Repository/lib/App/Repository/DBI.pm
  
  Index: DBI.pm
  ===================================================================
  RCS file: /cvs/public/p5ee/App-Repository/lib/App/Repository/DBI.pm,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- DBI.pm    9 Dec 2005 22:16:32 -0000       1.34
  +++ DBI.pm    6 Feb 2006 04:21:01 -0000       1.35
  @@ -833,20 +833,18 @@
           $param_order = [ (keys %$params) ];
       }
   
  -    my ($startrow, $endrow, $auto_extend, $keycolidx, $writeref, 
$reptyperef, $summarykeys);
  -    $startrow    = $options->{startrow}    || 0;
  -    $endrow      = $options->{endrow}      || 0;
  -    $auto_extend = $options->{auto_extend} || 0;
  -    $keycolidx   = $options->{keycolidx};
  -    $writeref    = $options->{writeref};
  -    $reptyperef  = $options->{reptyperef};
  -    $summarykeys = $options->{summarykeys};
  +    my $startrow    = $options->{startrow}    || 0;
  +    my $endrow      = $options->{endrow}      || 0;
  +    my $auto_extend = $options->{auto_extend} || 0;
  +    my $keycolidx   = $options->{keycolidx};
  +    my $writeref    = $options->{writeref};
  +    my $reptyperef  = $options->{reptyperef};
  +    my $group_by    = $options->{group_by} || $options->{summarykeys};
   
       my ($table_def, $tablealiases, $tablealiashref);
   
       $table_def = $self->{table}{$table};
  -    return undef if (!$table_def);
  -    $self->_load_table_metadata($table) if (!defined $table_def->{loaded});
  +    die "Table $table not defined" if (!$table_def);
   
       $tablealiases   = $table_def->{tablealiases};
       $tablealiashref = $table_def->{tablealias};
  @@ -854,7 +852,7 @@
       ############################################################
       # Record indexes of all requested columns
       ############################################################
  -    my ($idx, $column, %columnidx, @write, @reptype);
  +    my ($idx, $column, %columnidx, $column_def, @write, @reptype);
   
       for ($idx = 0; $idx <= $#$cols; $idx++) {
           $column = $cols->[$idx];
  @@ -863,21 +861,32 @@
           }
           $write[$idx] = 1;            # assume every field writable
           $reptype[$idx] = "string";   # assume every field a string (most 
general type)
  +        $column_def = $table_def->{column}{$column};
  +        if ((!defined $column_def || !%$column_def) && $column =~ 
/[^_a-zA-Z0-9]/) {
  +            my $alias = $column;
  +            $alias =~ s/[^_a-zA-Z0-9.]+/_/g;
  +            $alias =~ s/^([0-9])/_$1/;
  +            $column_def = {
  +                dbexpr => $column,
  +                alias => $alias,
  +            };
  +            $table_def->{column}{$column} = $column_def;
  +        }
       }
   
       ############################################################
       # ensure that the primary key and sort keys are included
       ############################################################
  -    my ($dbexpr, $columnalias, $columntype, $colidx, $column_def, $quoted);
  +    my ($dbexpr, $columnalias, $columntype, $colidx, $quoted);
       my (%dbexpr, @select_phrase, $group_reqd, @group_dbexpr, %reqd_tables);
       my (@keycolidx, $primary_key, $primary_table);
       my ($is_summary, %is_summary_key, $summaryexpr, @group_summarykeys);
   
  -    $is_summary = (defined $summarykeys && $#$summarykeys >= 0);
  +    $is_summary = (defined $group_by && $#$group_by >= 0);
   
       $primary_table = "";
       if ($is_summary) {
  -        foreach $column (@$summarykeys) {         # primary key is list of 
summary keys
  +        foreach $column (@$group_by) {         # primary key is list of 
summary keys
               $is_summary_key{$column} = 1;
               $colidx = $columnidx{$column};
               if (! defined $colidx && $auto_extend) {
  @@ -956,12 +965,17 @@
               else {
                   $summaryexpr = $column_def->{summary};
                   if (!defined $summaryexpr || $summaryexpr eq "") {
  -                    $columntype = $column_def->{type};
  -                    if ($columntype eq "integer" || $columntype eq "number") 
{
  -                        $summaryexpr = "avg(\$)";
  +                    if ($dbexpr && $dbexpr =~ /^(count|sum|avg|min|max)\(/) {
  +                        $summaryexpr = $dbexpr;
                       }
                       else {
  -                        $summaryexpr = "count(distinct(\$))";
  +                        $columntype = $column_def->{type};
  +                        if ($columntype eq "integer" || $columntype eq 
"number") {
  +                            $summaryexpr = "avg(\$)";
  +                        }
  +                        else {
  +                            $summaryexpr = "count(distinct(\$))";
  +                        }
                       }
                   }
                   if (defined $dbexpr) {
  @@ -1902,7 +1916,7 @@
   sub _select_rows {
       &App::sub_entry if ($App::trace);
       my ($self, $table, $cols, $params, $paramvalues, $order_by, $startrow, 
$endrow,
  -        $sortdircol, $keycolidx, $writeable, $columntype, $summarykeys) = @_;
  +        $sortdircol, $keycolidx, $writeable, $columntype, $group_by) = @_;
       my ($sql, $param, @params, %paramvalues, @paramvalues);
   
       $self->{error} = "";
  @@ -1919,11 +1933,11 @@
   
       if ($self->{table}{$table}{rawaccess}) {
           $sql = $self->_mk_select_sql($table, $cols, [EMAIL PROTECTED], 
\%paramvalues, $order_by,
  -            $startrow, $endrow, $sortdircol, $keycolidx, $writeable, 
$columntype, $summarykeys);
  +            $startrow, $endrow, $sortdircol, $keycolidx, $writeable, 
$columntype, $group_by);
       }
       else {
           $sql = $self->_mk_select_rows_sql($table, $cols, [EMAIL PROTECTED], 
\%paramvalues, $order_by,
  -            $startrow, $endrow, $sortdircol, $keycolidx, $writeable, 
$columntype, $summarykeys);
  +            $startrow, $endrow, $sortdircol, $keycolidx, $writeable, 
$columntype, $group_by);
       }
       $self->{sql} = $sql;
       my $retval = $self->_selectrange_arrayref($sql, $startrow, $endrow, 
undef, @paramvalues);
  
  
  

Reply via email to