| 1 | package JQuery::DataTables::Heavy::DBI; | ||
| 2 | use Moo; | ||
| 3 | use MooX::Types::MooseLike::Base qw(:all); | ||
| 4 | use Carp; | ||
| 5 | with 'JQuery::DataTables::Heavy::Base'; | ||
| 6 | use SQL::Abstract::Limit; | ||
| 7 | use namespace::clean; | ||
| 8 | |||
| 9 | sub _get_table_content { | ||
| 10 | my ($self) = @_; | ||
| 11 | my $limit = $self->limit; | ||
| 12 | |||
| 13 | if ( $limit == -1 ) { $limit = undef; } | ||
| 14 | |||
| 15 | my $dbh = $self->dbh; | ||
| 16 | |||
| 17 | my $sql = SQL::Abstract::Limit->new( limit_dialect => $dbh ); | ||
| 18 | my @cols = grep { $_ ne '' } ${ $self->table_cols }; | ||
| 19 | my ( $stmt, @bind ) | ||
| 20 | = $sql->select( $self->table, \@cols, $self->where_clause, $self->order_clause, $self->limit, $self->offset); | ||
| 21 | |||
| 22 | my $sth = $dbh->prepare($stmt) | ||
| 23 | or croak "Error preparing sql: " . $dbh->errstr() . "\nSQL: $sql\n"; | ||
| 24 | my $rv = $sth->execute(@bind) | ||
| 25 | or croak "Error executing sql: " . $dbh->errstr() . "\nSQL: $sql\nBind: @bind"; | ||
| 26 | |||
| 27 | my $aaData = $sth->fetchall_arrayref( +{} ); | ||
| 28 | |||
| 29 | $sth->finish(); # clean up | ||
| 30 | |||
| 31 | return $aaData; | ||
| 32 | } | ||
| 33 | |||
| 34 | # _get_total_record_count() | ||
| 35 | # | ||
| 36 | # Get the number of records in the table, regardless of restrictions of the | ||
| 37 | # where clause or the limit clause. Used to display the total number of records | ||
| 38 | # without applied filters. | ||
| 39 | |||
| 40 | sub _get_total_record_count { | ||
| 41 | my ($self) = @_; | ||
| 42 | |||
| 43 | my $dbh = $self->dbh; | ||
| 44 | |||
| 45 | my $sql = SQL::Abstract::Limit->new( limit_dialect => $dbh ); | ||
| 46 | my ( $stmt, @bind ) = $sql->select( $self->table, ['count(*) AS count'] ); | ||
| 47 | |||
| 48 | my $sth = $dbh->prepare($stmt) | ||
| 49 | or croak "Error preparing sql: " . $dbh->errstr() . "\nSQL: $sql\n"; | ||
| 50 | my $rv = $sth->execute() or croak "Error executing sql: " . $dbh->errstr() . "\nSQL: $sql\n"; | ||
| 51 | |||
| 52 | return $sth->fetchrow_hashref()->{count}; | ||
| 53 | } | ||
| 54 | |||
| 55 | # _get_filtered_total() | ||
| 56 | # | ||
| 57 | # Get the total number of filtered records (in resprect of filters by the where | ||
| 58 | # clause, without limit). This accounts for the "search" field of data tables. | ||
| 59 | |||
| 60 | sub _get_filtered_total { | ||
| 61 | my ( $self ) = @_; | ||
| 62 | |||
| 63 | my $dbh = $self->dbh; | ||
| 64 | my $sql = SQL::Abstract::Limit->new( limit_dialect => $dbh ); | ||
| 65 | my ( $stmt, @bind ) = $sql->select( $self->table, ['count(*) AS count'], $self->where_clause); | ||
| 66 | |||
| 67 | my $sth = $dbh->prepare($stmt) | ||
| 68 | or croak "Error preparing sql: " . $dbh->errstr() . "\nSQL: $sql\n"; | ||
| 69 | my $rv = $sth->execute(@bind) | ||
| 70 | or croak "Error executing sql: " . $dbh->errstr() . "\nSQL: $sql\nBind: @bind"; | ||
| 71 | return $sth->fetchrow_hashref()->{count}; | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; | ||
| 75 | |||
| 76 | __END__ |