| 1 | package JQuery::DataTables::Heavy::DBIC; | ||
| 2 | use Moo; | ||
| 3 | use MooX::Types::MooseLike::Base qw(:all); | ||
| 4 | with 'JQuery::DataTables::Heavy::Base'; | ||
| 5 | use Carp; | ||
| 6 | use Scalar::Util qw(blessed); | ||
| 7 | use Hash::Merge; | ||
| 8 | use namespace::clean; | ||
| 9 | |||
| 10 | has '+dbh' => ( is => 'rw', isa => InstanceOf ['DBIx::Class::Schema'] ); | ||
| 11 | has has_many => ( is => 'rw', isa => Any, default => sub { []; } ); | ||
| 12 | has rs => ( is => 'lazy', isa => InstanceOf ['DBIx::Class::ResultSet'] ); | ||
| 13 | has _hashed_has_many => ( is => 'lazy', isa => HashRef ); | ||
| 14 | has _related_has_many => ( is => 'lazy', isa => HashRef ); | ||
| 15 | has _hash_merge_prefetch => ( is => 'lazy', isa => InstanceOf ['Hash::Merge'] ); | ||
| 16 | |||
| 17 | sub _build_rs { | ||
| 18 | my ($self) = @_; | ||
| 19 | if ( blessed $self->table and $self->table->isa('DBIx::Class::ResultSet') ) { | ||
| 20 | return $self->table; | ||
| 21 | } | ||
| 22 | else { | ||
| 23 | return $self->dbh->resultset( $self->table ); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | sub _build__hashed_has_many { | ||
| 28 | my ($self) = @_; | ||
| 29 | return $self->_generate_hashed_has_many( $self->has_many ); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _build__related_has_many { | ||
| 33 | my ($self) = @_; | ||
| 34 | return $self->_generate_related_has_many( $self->_hashed_has_many ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _build__hash_merge_prefetch { | ||
| 38 | my $merge = Hash::Merge->new; | ||
| 39 | $merge->set_behavior('RETAINMENT_PRECEDENT'); | ||
| 40 | return $merge; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _get_table_content { | ||
| 44 | my ($self) = @_; | ||
| 45 | my $limit = $self->limit; | ||
| 46 | |||
| 47 | if ( $limit == -1 ) { $limit = undef; } | ||
| 48 | |||
| 49 | my $attr = { | ||
| 50 | order_by => $self->order_clause, | ||
| 51 | rows => $limit, | ||
| 52 | offset => $self->offset, | ||
| 53 | alias => $self->rs->result_source->name, | ||
| 54 | }; | ||
| 55 | |||
| 56 | if ( my $prefetch = $self->_generate_prefetch_clause_for_where_clause ) { | ||
| 57 | $attr->{prefetch} = $prefetch; | ||
| 58 | } | ||
| 59 | |||
| 60 | my $rs = $self->rs->search_rs( $self->where_clause, $attr ); | ||
| 61 | my $rs_hashed = $self->rs->search_rs( $self->where_clause, $attr ); | ||
| 62 | my %rel_rs; | ||
| 63 | |||
| 64 | my $aoData = []; | ||
| 65 | if ( $self->return_hashref ) { | ||
| 66 | $rs_hashed->result_class('DBIx::Class::ResultClass::HashRefInflator'); | ||
| 67 | while ( my $row = $rs->next ) { | ||
| 68 | my $h = $rs_hashed->next; | ||
| 69 | while ( my ( $rel_name, $v ) = each %{ $self->_related_has_many } ) { | ||
| 70 | next if exists $h->{$rel_name}; | ||
| 71 | my $attr = {}; | ||
| 72 | if ( ref $v or $rel_name ne $v ) { | ||
| 73 | $attr->{prefetch} = $v; | ||
| 74 | } | ||
| 75 | my $rel_rs = $row->related_resultset($rel_name)->search_rs( {}, $attr ); | ||
| 76 | $rel_rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); | ||
| 77 | $h->{$rel_name} = [ $rel_rs->all ]; | ||
| 78 | } | ||
| 79 | push @$aoData, $self->_merge_data_default($h); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | $aoData = $rs; | ||
| 84 | } | ||
| 85 | return $aoData; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _generate_related_has_many { | ||
| 89 | my ( $self, $data ) = @_; | ||
| 90 | my $h; | ||
| 91 | foreach my $v ( values %$data ) { | ||
| 92 | if ( not defined $h ) { | ||
| 93 | $h = $v; | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | $h = $self->_hash_merge_prefetch->merge( $h, $v ); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | foreach my $key ( keys %$h ) { | ||
| 100 | $h->{$key} = $self->_extract_hash( $h->{$key} ); | ||
| 101 | } | ||
| 102 | return $h; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _generate_hashed_has_many { | ||
| 106 | my ( $self, $ref ) = @_; | ||
| 107 | my %h = (); | ||
| 108 | if ( ref $ref eq 'HASH' ) { | ||
| 109 | while ( my ( $k, $v ) = each %{$ref} ) { # hash shoud have 1 key-value. | ||
| 110 | my $hash_tmp = $self->_generate_hashed_has_many($v); | ||
| 111 | foreach my $k2 ( keys %$hash_tmp ) { | ||
| 112 | $h{"$k.$k2"} = { $k => $hash_tmp->{$k2} }; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | elsif ( ref $ref eq 'ARRAY' ) { | ||
| 117 | foreach my $item (@$ref) { | ||
| 118 | my $hash_tmp = $self->_generate_hashed_has_many($item); | ||
| 119 | @h{ keys %$hash_tmp } = values %$hash_tmp; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | else { | ||
| 123 | $h{$ref} = $ref; | ||
| 124 | } | ||
| 125 | return \%h; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _generate_prefetch_clause_for_where_clause { | ||
| 129 | my ($self) = @_; | ||
| 130 | $self->where_clause; # setup where_fields | ||
| 131 | my $prefetch; | ||
| 132 | foreach my $field ( @{ $self->where_fields } ) { | ||
| 133 | ( my $key = $field ) =~ s/\.[^\.]+\z//msx; | ||
| 134 | if ( my $v = $self->_hashed_has_many->{$key} ) { | ||
| 135 | if ( not defined $prefetch ) { | ||
| 136 | $prefetch = $v; | ||
| 137 | } | ||
| 138 | else { | ||
| 139 | $prefetch = $self->_hash_merge_prefetch->merge( $prefetch, $v ); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | return $self->_extract_hash($prefetch); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _extract_hash { | ||
| 147 | my ( $self, $data ) = @_; | ||
| 148 | my $ref = ref $data; | ||
| 149 | if ( $ref eq 'ARRAY' ) { | ||
| 150 | foreach my $item (@$data) { | ||
| 151 | $item = $self->_extract_hash($item); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | elsif ( $ref eq 'HASH' ) { | ||
| 155 | while ( my ( $k, $v ) = each %$data ) { | ||
| 156 | $data->{$k} = $self->_extract_hash($v); | ||
| 157 | } | ||
| 158 | if ( keys %$data > 1 ) { | ||
| 159 | return [ | ||
| 160 | map { ( not ref $data->{$_} and $_ eq $data->{$_} ) ? $_ : +{ $_ => $data->{$_} } } | ||
| 161 | sort keys %$data | ||
| 162 | ]; | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | my ( $k, $v ) = each %$data; | ||
| 166 | if ( not ref $v and $k eq $v ) { | ||
| 167 | return $k; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | return $data; | ||
| 172 | } | ||
| 173 | |||
| 174 | # _get_total_record_count() | ||
| 175 | # | ||
| 176 | # Get the number of records in the table, regardless of restrictions of the | ||
| 177 | # where clause or the limit clause. Used to display the total number of records | ||
| 178 | # without applied filters. | ||
| 179 | |||
| 180 | sub _get_total_record_count { | ||
| 181 | my ($self) = @_; | ||
| 182 | return $self->rs->count(); | ||
| 183 | } | ||
| 184 | |||
| 185 | # _get_filtered_total() | ||
| 186 | # | ||
| 187 | # Get the total number of filtered records (in resprect of filters by the where | ||
| 188 | # clause, without limit). This accounts for the "search" field of data tables. | ||
| 189 | |||
| 190 | sub _get_filtered_total { | ||
| 191 | my ($self) = @_; | ||
| 192 | my $attr = {}; | ||
| 193 | if ( my $prefetch = $self->_generate_prefetch_clause_for_where_clause ) { | ||
| 194 | $attr->{prefetch} = $prefetch; | ||
| 195 | } | ||
| 196 | return $self->rs->count( $self->where_clause, $attr ); | ||
| 197 | } | ||
| 198 | |||
| 199 | 1; | ||
| 200 | |||
| 201 | __END__ |