| 1 | package JQuery::DataTables::Heavy::Base; | ||
| 2 | use Moo::Role; | ||
| 3 | use MooX::Types::MooseLike::Base qw(:all); | ||
| 4 | use Hash::Merge; | ||
| 5 | use Clone; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | has dbh => ( is => 'rw', isa => Any, required => 1 ); | ||
| 9 | has table => ( is => 'rw', isa => Any, required => 1 ); | ||
| 10 | has param => ( is => 'rw', isa => HashRef, required => 1 ); | ||
| 11 | has fields => ( is => 'lazy', isa => ArrayRef[Str] ); | ||
| 12 | has table_cols => ( is => 'lazy', isa => ArrayRef[Str]); | ||
| 13 | has where_fields => ( is => 'rw', isa => ArrayRef[Str], default => sub { [] } ); | ||
| 14 | has return_hashref => ( is => 'rw', isa => Bool, default => sub { 1 } ); | ||
| 15 | has table_data_default => ( is => 'lazy', isa => HashRef); | ||
| 16 | has hash_merge => ( is => 'lazy', isa => InstanceOf['Hash::Merge'] ); | ||
| 17 | has order_clause => ( is => 'lazy', isa => ArrayRef); | ||
| 18 | has where_clause => ( is => 'lazy', isa => HashRef); | ||
| 19 | has limit => ( is => 'lazy', isa => Int); | ||
| 20 | has offset => ( is => 'lazy', isa => Int); | ||
| 21 | |||
| 22 | requires qw(_get_filtered_total _get_table_content _get_total_record_count); | ||
| 23 | |||
| 24 | sub _build_fields { | ||
| 25 | my ($self) = @_; | ||
| 26 | my @field_names; | ||
| 27 | for ( my $i = 0; $i < ( $self->param->{iColumns} || 0 ); $i++ ) { | ||
| 28 | push @field_names, $self->param->{"mDataProp_$i"}; | ||
| 29 | } | ||
| 30 | return \@field_names; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _build_table_cols { | ||
| 34 | my ($self) = @_; | ||
| 35 | my @cols = map {(my $s = $_) =~ s/\A.*\.(?=[^\.]+\.[^\.]+\z)//smx; $s} @{ $self->fields }; | ||
| 36 | return \@cols; | ||
| 37 | } | ||
| 38 | |||
| 39 | # suppress json warning on { table_name => undef } | ||
| 40 | sub _build_table_data_default { | ||
| 41 | my ($self) = @_; | ||
| 42 | my $base = {}; | ||
| 43 | foreach my $k ( @{ $self->fields } ) { | ||
| 44 | next if $k eq ''; | ||
| 45 | my $hash = undef; | ||
| 46 | foreach $k ( reverse split /\./, $k ) { | ||
| 47 | $hash = { $k => $hash }; | ||
| 48 | } | ||
| 49 | $base = Hash::Merge::merge( $hash, $base ); | ||
| 50 | } | ||
| 51 | return $base; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _build_hash_merge { | ||
| 55 | my $merge = Hash::Merge->new; | ||
| 56 | $merge->specify_behavior( | ||
| 57 | { SCALAR => { | ||
| 58 | SCALAR => sub { $_[0] }, | ||
| 59 | ARRAY => sub { Carp::croak 'SCALAR and ARRAY cannot merge.' }, | ||
| 60 | HASH => sub { defined $_[0] ? $_[0] : $_[1] }, | ||
| 61 | }, | ||
| 62 | ARRAY => { | ||
| 63 | SCALAR => sub { $_[0] }, | ||
| 64 | ARRAY => sub { $_[0] }, | ||
| 65 | HASH => sub { $_[1]->{_ARRAY} = $_[0]; return $_[1] }, # for has_many Relation | ||
| 66 | }, | ||
| 67 | HASH => { | ||
| 68 | SCALAR => sub { Carp::croak 'HASH and SCALAR cannot merge.' }, | ||
| 69 | ARRAY => sub { Carp::croak 'HASH and ARRAY cannot merge.' }, | ||
| 70 | HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) }, | ||
| 71 | }, | ||
| 72 | }, | ||
| 73 | 'MY_DATA_MERGE' | ||
| 74 | ); | ||
| 75 | return $merge; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _build_where_clause { | ||
| 79 | my ($self) = @_; | ||
| 80 | return $self->_generate_where_clause; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _build_order_clause { | ||
| 84 | my ($self) = @_; | ||
| 85 | return $self->_generate_order_clause; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _build_limit { | ||
| 89 | my ($self) = @_; | ||
| 90 | return $self->param->{'iDisplayLength'} || 10; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _build_offset { | ||
| 94 | my ($self) = @_; | ||
| 95 | my $offset = 0; | ||
| 96 | if ( $self->param->{'iDisplayStart'} ) { | ||
| 97 | $offset = $self->param->{'iDisplayStart'}; | ||
| 98 | } | ||
| 99 | return $offset; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub table_data { | ||
| 103 | my ($self) = @_; | ||
| 104 | |||
| 105 | if (not defined $self->table) { | ||
| 106 | return $self->_create_output( [] , 0, 0); | ||
| 107 | } | ||
| 108 | |||
| 109 | # -- get table contents | ||
| 110 | my $aaData = $self->_get_table_content; | ||
| 111 | |||
| 112 | # -- get meta information about the resultset | ||
| 113 | my $iFilteredTotal = $self->_get_filtered_total; | ||
| 114 | my $iTotal = $self->_get_total_record_count; | ||
| 115 | |||
| 116 | # -- build final data structure | ||
| 117 | return $self->_create_output( $aaData, $iTotal, $iFilteredTotal); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _create_output { | ||
| 121 | my ($self, $aaData, $iTotal, $iFilteredTotal) = @_; | ||
| 122 | return { | ||
| 123 | sEcho => int( $self->param->{'sEcho'} || 0 ), | ||
| 124 | iTotalRecords => int($iTotal), | ||
| 125 | iTotalDisplayRecords => int($iFilteredTotal), | ||
| 126 | aaData => $aaData, | ||
| 127 | }; | ||
| 128 | } | ||
| 129 | |||
| 130 | # _get_table_content | ||
| 131 | # empty array. If there exist results, return value will be a two-dimensonal | ||
| 132 | # array. | ||
| 133 | # Basically, this method builds the SQL and fetches the results. | ||
| 134 | |||
| 135 | # _generate_order_clause | ||
| 136 | # | ||
| 137 | # Evaluate query for odering information. If present, generate order clause, if | ||
| 138 | # not, returns empty order clause. | ||
| 139 | |||
| 140 | sub _generate_order_clause { | ||
| 141 | my ($self) = @_; | ||
| 142 | my @order = (); | ||
| 143 | |||
| 144 | if ( defined $self->param->{'iSortCol_0'} ) { | ||
| 145 | for ( my $i = 0; $i < $self->param->{'iSortingCols'}; $i++ ) { | ||
| 146 | |||
| 147 | # build direction, must be '-asc' or '-desc' (cf. SQL::Abstract) | ||
| 148 | # we only get 'asc' or 'desc', so they have to be prefixed with '-' | ||
| 149 | my $direction = '-' . $self->param->{ 'sSortDir_' . $i }; | ||
| 150 | |||
| 151 | # We only get the column index (starting from 0), so we have to | ||
| 152 | # translate the index into a column name. | ||
| 153 | my $column_name = $self->table_cols->[ $self->param->{ 'iSortCol_' . $i } ]; | ||
| 154 | push @order, { $direction => $column_name }; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | return \@order; | ||
| 159 | } | ||
| 160 | |||
| 161 | # _generate_where_clause | ||
| 162 | # | ||
| 163 | # Evaluate global search information, that is, information by which each field | ||
| 164 | # has to be restricted. If present, generate matching conditions for each | ||
| 165 | # searchable column (searchability indicated by query parameters) and combine | ||
| 166 | # as disjunction (OR). | ||
| 167 | # | ||
| 168 | # NOTE this does not match the built-in DataTables filtering which does it | ||
| 169 | # word by word on any field. It's possible to do here, but concerned about | ||
| 170 | # efficiency on very large tables, and MySQL's regex functionality is very | ||
| 171 | # limited. | ||
| 172 | |||
| 173 | sub _generate_where_clause { | ||
| 174 | my ($self) = @_; | ||
| 175 | my %where = (); | ||
| 176 | my $search_string = $self->param->{'sSearch'}; | ||
| 177 | |||
| 178 | for ( my $i = 0; $i < ( $self->param->{'iColumns'} || 0 ); $i++ ) { | ||
| 179 | |||
| 180 | # Iterate over each column and check if it is searchable. | ||
| 181 | # If so, add a constraint to the where clause restricting the given | ||
| 182 | # column. In the query, the column is identified by it's index, we | ||
| 183 | # need to translates the index to the column name. | ||
| 184 | my $searchable_ident = 'bSearchable_' . $i; | ||
| 185 | if ( $self->param->{$searchable_ident} and $self->param->{$searchable_ident} eq 'true' ) { | ||
| 186 | my $column = $self->table_cols->[$i]; | ||
| 187 | my $field = $self->fields->[$i]; | ||
| 188 | |||
| 189 | # global search | ||
| 190 | if ( defined $search_string and $search_string ne "" ) { | ||
| 191 | if ( $self->param->{'bRegex'} eq 'true' ) { | ||
| 192 | push @{ $where{'-or'} }, { $column => { -regexp => $search_string } }; | ||
| 193 | } | ||
| 194 | else { | ||
| 195 | push @{ $where{'-or'} }, { $column => { -like => '%' . $search_string . '%' } }; | ||
| 196 | } | ||
| 197 | push @{$self->where_fields}, $field; | ||
| 198 | } | ||
| 199 | |||
| 200 | # each column search | ||
| 201 | my $column_search_string = $self->param->{ 'sSearch_' . $i }; | ||
| 202 | if ( defined $column_search_string and $column_search_string ne "" ) { | ||
| 203 | if ( $self->param->{ 'bRegex_' . $i } eq 'true' ) { | ||
| 204 | push @{ $where{'-and'} }, { $column => { -regexp => $column_search_string } }; | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | push @{ $where{'-and'} }, | ||
| 208 | { $column => { -like => '%' . $column_search_string . '%' } }; | ||
| 209 | } | ||
| 210 | push @{$self->where_fields}, $field; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | } | ||
| 214 | return \%where; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub _get_table_data_default { | ||
| 218 | my ( $self, $data ) = @_; | ||
| 219 | return Clone::clone( $self->table_data_default ); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub _merge_data_default { | ||
| 223 | my ( $self, $data ) = @_; | ||
| 224 | return $self->hash_merge->merge( $data, $self->_get_table_data_default ); | ||
| 225 | } | ||
| 226 | |||
| 227 | 1; | ||
| 228 | |||
| 229 | __END__ |