DBIx-Class-Helper-SimpleStats

 view release on metacpan or  search on metacpan

lib/DBIx/Class/Helper/SimpleStats.pm  view on Meta::CPAN

package DBIx::Class::Helper::SimpleStats;

# ABSTRACT: Simple grouping and aggregate functions for DBIx::Class

use v5.10.1;

use strict;
use warnings;

use base qw( DBIx::Class );

use Carp;
use List::Util 1.45 qw/ uniqstr /;
use Ref::Util qw/ is_plain_hashref is_ref /;

# RECOMMEND PREREQ: Ref::Util::XS

use namespace::clean;

our $VERSION = 'v0.1.3';



sub simple_stats {
    my ( $self, @args ) = @_;

    croak "No columns" unless @args;

    my @cols;
    my @funcs;

    my $me = $self->current_source_alias;

    my $alias = sub {
      my $ident = shift;
      $ident = "$me.$ident" unless $ident =~ /^(\w+)\./;
      return $ident;
    };

    foreach my $arg (@args) {

        if ( is_ref($arg) ) {

            if ( is_plain_hashref($arg) ) {

                my $as = delete $arg->{'-as'};
                my ( $func, $col ) = each %{$arg};

                $as //= "${col}_${func}";

                push @cols, $alias->($col);

                push @funcs, { $func => $alias->($col), -as => $as };

            }
            else {

                croak "Unsupported reference type: " . ref($arg);

            }

        }
        else {

            push @cols, $alias->($arg);

        }

    }

    unless (@funcs) {

        my $func = "count";
        my $col  = $cols[0];
        $col =~ s/^\w+\.// ;



( run in 1.941 second using v1.01-cache-2.11-cpan-39bf76dae61 )