| 1 | package QBit::QueryData; | ||
| 2 | $QBit::QueryData::VERSION = '0.005'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::Class); | ||
| 6 | |||
| 7 | __PACKAGE__->mk_accessors(qw(definition)); | ||
| 8 | |||
| 9 | my $FILTER_OPERATIONS = { | ||
| 10 | number => { | ||
| 11 | '=' => '==', | ||
| 12 | '!=' => '==', | ||
| 13 | '<>' => '==', | ||
| 14 | '>' => '>', | ||
| 15 | '>=' => '>=', | ||
| 16 | '<' => '<', | ||
| 17 | '<=' => '<=', | ||
| 18 | 'IN' => '==', | ||
| 19 | 'NOT IN' => '==', | ||
| 20 | 'IS' => '==', | ||
| 21 | 'IS NOT' => '==', | ||
| 22 | }, | ||
| 23 | string => { | ||
| 24 | '=' => 'eq', | ||
| 25 | '!=' => 'eq', | ||
| 26 | '<>' => 'eq', | ||
| 27 | '>' => 'gt', | ||
| 28 | '>=' => 'ge', | ||
| 29 | '<' => 'lt', | ||
| 30 | '<=' => 'le', | ||
| 31 | 'IN' => 'eq', | ||
| 32 | 'NOT IN' => 'eq', | ||
| 33 | 'IS' => 'eq', | ||
| 34 | 'IS NOT' => 'eq', | ||
| 35 | 'LIKE' => '=~', | ||
| 36 | 'NOT LIKE' => '=~', | ||
| 37 | }, | ||
| 38 | }; | ||
| 39 | |||
| 40 | my $ORDER_OPERATIONS = { | ||
| 41 | number => '<=>', | ||
| 42 | string => 'cmp', | ||
| 43 | }; | ||
| 44 | |||
| 45 | sub init { | ||
| 46 | my ($self) = @_; | ||
| 47 | |||
| 48 | $self->definition({}) unless defined($self->definition); | ||
| 49 | |||
| 50 | $self->data($self->{'data'} // []); | ||
| 51 | |||
| 52 | $self->fields($self->get_fields()); | ||
| 53 | |||
| 54 | $self->filter($self->{'filter'}); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub data { | ||
| 58 | my ($self, $data) = @_; | ||
| 59 | |||
| 60 | if (defined($data)) { | ||
| 61 | delete($self->{'__EXISTS_FIELDS__'}); | ||
| 62 | delete($self->{'__ALL_FIELDS__'}); | ||
| 63 | |||
| 64 | foreach my $field (sort keys(%{$data->[0] // {}})) { | ||
| 65 | $self->{'__EXISTS_FIELDS__'}{$field} = TRUE; | ||
| 66 | push(@{$self->{'__ALL_FIELDS__'}}, $field); | ||
| 67 | } | ||
| 68 | |||
| 69 | $self->{'data'} = $data; | ||
| 70 | } | ||
| 71 | |||
| 72 | return $self->{'data'}; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub fields { | ||
| 76 | my ($self, $fields) = @_; | ||
| 77 | |||
| 78 | if (defined($fields)) { | ||
| 79 | $fields = [keys(%$fields)] if ref($fields) eq 'HASH'; | ||
| 80 | |||
| 81 | if (@$fields == 0) { | ||
| 82 | #default | ||
| 83 | delete($self->{'__FIELDS__'}); | ||
| 84 | } else { | ||
| 85 | #set fields | ||
| 86 | if (exists($self->{'__EXISTS_FIELDS__'})) { | ||
| 87 | my @not_exists = grep {!$self->{'__EXISTS_FIELDS__'}{$_}} @$fields; | ||
| 88 | throw gettext('Unknown fields: %s', join(', ', @not_exists)) if @not_exists; | ||
| 89 | } | ||
| 90 | |||
| 91 | $self->{'__FIELDS__'} = $fields; | ||
| 92 | } | ||
| 93 | } else { | ||
| 94 | #all fields | ||
| 95 | delete($self->{'__FIELDS__'}); | ||
| 96 | delete($self->{'fields'}); | ||
| 97 | } | ||
| 98 | |||
| 99 | return $self; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub get_fields { | ||
| 103 | my ($self) = @_; | ||
| 104 | |||
| 105 | return $self->{'__FIELDS__'} // $self->{'fields'} // $self->{'__ALL_FIELDS__'}; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub filter { | ||
| 109 | my ($self, $filter) = @_; | ||
| 110 | |||
| 111 | if (defined($filter)) { | ||
| 112 | $self->{'__FILTER__'} = eval($self->_get_filter($filter)); | ||
| 113 | } else { | ||
| 114 | delete($self->{'__FILTER__'}); | ||
| 115 | } | ||
| 116 | |||
| 117 | return $self; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub all_langs { | ||
| 121 | my ($self, $value) = @_; | ||
| 122 | |||
| 123 | $self->{'__ALL_LANGS__'} = $value // TRUE; | ||
| 124 | |||
| 125 | return $self; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub distinct { | ||
| 129 | my ($self, $value) = @_; | ||
| 130 | |||
| 131 | $self->{'__DISTINCT__'} = $value // TRUE; | ||
| 132 | |||
| 133 | return $self; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub insensitive { | ||
| 137 | my ($self, $value) = @_; | ||
| 138 | |||
| 139 | $self->{'__INSENSITIVE__'} = $value // TRUE; | ||
| 140 | |||
| 141 | return $self; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub for_update { } | ||
| 145 | |||
| 146 | sub order_by { | ||
| 147 | my ($self, @order_by) = @_; | ||
| 148 | |||
| 149 | unless (@order_by) { | ||
| 150 | delete($self->{'__ORDER_BY__'}); | ||
| 151 | |||
| 152 | return $self; | ||
| 153 | } | ||
| 154 | |||
| 155 | @order_by = map {[ref($_) ? ($_->[0], $_->[1]) : ($_, 0)]} @order_by; | ||
| 156 | |||
| 157 | $self->{'__ORDER_BY__'} = eval($self->_get_order(@order_by)); | ||
| 158 | |||
| 159 | return $self; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub limit { | ||
| 163 | my ($self, $offset, $limit) = @_; | ||
| 164 | |||
| 165 | if (defined($limit)) { | ||
| 166 | $self->{'__OFFSET__'} = $offset // 0; | ||
| 167 | |||
| 168 | $self->{'__LIMIT__'} = $limit; | ||
| 169 | } else { | ||
| 170 | delete($self->{'__OFFSET__'}); | ||
| 171 | delete($self->{'__LIMIT__'}); | ||
| 172 | } | ||
| 173 | |||
| 174 | return $self; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub calc_rows { | ||
| 178 | my ($self) = @_; | ||
| 179 | |||
| 180 | $self->{'__CALC_ROWS__'} = TRUE; | ||
| 181 | |||
| 182 | return $self; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub found_rows { | ||
| 186 | my ($self) = @_; | ||
| 187 | |||
| 188 | return scalar(@{$self->data}); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub get_all { | ||
| 192 | my ($self, %opts) = @_; | ||
| 193 | |||
| 194 | my @data = defined($self->{'__FILTER__'}) ? grep {$self->{'__FILTER__'}->($_)} @{$self->data} : @{$self->data}; | ||
| 195 | |||
| 196 | if (defined($self->{'__ORDER_BY__'})) { | ||
| 197 | @data = sort {$self->{'__ORDER_BY__'}->($a, $b)} @data; | ||
| 198 | } | ||
| 199 | |||
| 200 | if (defined($self->{'__LIMIT__'})) { | ||
| 201 | $self->{'__OFFSET__'} //= 0; | ||
| 202 | |||
| 203 | return [] if $self->{'__OFFSET__'} >= @data; | ||
| 204 | |||
| 205 | my $high = $self->{'__OFFSET__'} + $self->{'__LIMIT__'} - 1; | ||
| 206 | |||
| 207 | $high = $#data if $high > $#data; | ||
| 208 | |||
| 209 | @data = @data[$self->{'__OFFSET__'} .. $high]; | ||
| 210 | } | ||
| 211 | |||
| 212 | my @result = (); | ||
| 213 | |||
| 214 | my @fields = @{$self->get_fields() // []}; | ||
| 215 | if ($self->{'__DISTINCT__'}) { | ||
| 216 | my %uniq = (); | ||
| 217 | |||
| 218 | foreach my $row (@data) { | ||
| 219 | my $str = ''; | ||
| 220 | |||
| 221 | my $new_row = {}; | ||
| 222 | foreach (@fields) { | ||
| 223 | $str .= $row->{$_} // 'UNDEF'; | ||
| 224 | |||
| 225 | $new_row->{$_} = $row->{$_}; | ||
| 226 | } | ||
| 227 | |||
| 228 | unless ($uniq{$str}) { | ||
| 229 | push(@result, $new_row); | ||
| 230 | |||
| 231 | $uniq{$str} = TRUE; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } else { | ||
| 235 | foreach my $row (@data) { | ||
| 236 | push(@result, {map {$_ => $row->{$_}} @fields}); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | return \@result; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub _get_filter { | ||
| 244 | my ($self, $filter) = @_; | ||
| 245 | |||
| 246 | my $body = ''; | ||
| 247 | |||
| 248 | $self->_filter(\$body, $filter); | ||
| 249 | |||
| 250 | return $self->_get_sub($body); | ||
| 251 | } | ||
| 252 | |||
| 253 | sub _get_sub { | ||
| 254 | my ($self, $body) = @_; | ||
| 255 | |||
| 256 | return "sub {\n no warnings;\n\n return " . $body . ";\n}"; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub _filter { | ||
| 260 | my ($self, $body, $filter) = @_; | ||
| 261 | |||
| 262 | my $operation = ' && '; | ||
| 263 | |||
| 264 | $$body .= '('; | ||
| 265 | |||
| 266 | my @part = (); | ||
| 267 | if (ref($filter) eq 'HASH') { | ||
| 268 | foreach my $field (keys(%$filter)) { | ||
| 269 | throw gettext('Unknown field "%s"', $field) | ||
| 270 | if exists($self->{'__EXISTS_FIELDS__'}) && !$self->{'__EXISTS_FIELDS__'}{$field}; | ||
| 271 | |||
| 272 | my $type_operation = $self->_get_filter_operation($field, '='); | ||
| 273 | |||
| 274 | if (ref($filter->{$field}) eq 'ARRAY') { | ||
| 275 | push(@part, | ||
| 276 | "(grep {\$_[0]->{$field} $type_operation \$_} (" | ||
| 277 | . join(', ', map {$self->_get_value($field, $_)} @{$filter->{$field}}) | ||
| 278 | . "))"); | ||
| 279 | } else { | ||
| 280 | my $value = $self->_get_value($field, $filter->{$field}); | ||
| 281 | |||
| 282 | push(@part, "(\$_[0]->{$field} $type_operation $value)"); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | } elsif (ref($filter) eq 'ARRAY' && @$filter == 2) { | ||
| 286 | $operation = ' || ' if uc($filter->[0]) eq 'OR'; | ||
| 287 | |||
| 288 | foreach my $sub_filter (@{$filter->[1]}) { | ||
| 289 | my $sub_body = ''; | ||
| 290 | $self->_filter(\$sub_body, $sub_filter); | ||
| 291 | push(@part, $sub_body); | ||
| 292 | } | ||
| 293 | } elsif (ref($filter) eq 'ARRAY' && @$filter == 3) { | ||
| 294 | my ($field, $op, $value) = @$filter; | ||
| 295 | |||
| 296 | throw gettext('Unknown field "%s"', $field) | ||
| 297 | if exists($self->{'__EXISTS_FIELDS__'}) && !$self->{'__EXISTS_FIELDS__'}{$field}; | ||
| 298 | |||
| 299 | $op = uc($op); | ||
| 300 | $value = $$value; | ||
| 301 | |||
| 302 | my $type_operation = $self->_get_filter_operation($field, $op); | ||
| 303 | |||
| 304 | if (ref($value) eq 'ARRAY') { | ||
| 305 | throw gettext('Operation "%s" is not applied to the array', $op) | ||
| 306 | if grep {$op eq $_} ('>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IS', 'IS NOT'); | ||
| 307 | |||
| 308 | push(@part, | ||
| 309 | "(" | ||
| 310 | . ($op eq '<>' || $op eq '!=' || $op eq 'NOT IN' ? '!' : '') | ||
| 311 | . "grep {\$_[0]->{$field} $type_operation \$_} (" | ||
| 312 | . join(', ', map {$self->_get_value($field, $_)} @$value) | ||
| 313 | . "))"); | ||
| 314 | } else { | ||
| 315 | $value = $self->_get_value($field, $value, $op); | ||
| 316 | |||
| 317 | push(@part, | ||
| 318 | ($op eq '<>' || $op eq '!=' || $op =~ /^NOT\s|\sNOT$/i ? '!' : '') | ||
| 319 | . "(\$_[0]->{$field} $type_operation $value)"); | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | $$body .= join($operation, @part); | ||
| 324 | |||
| 325 | $$body .= ')'; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub _get_order { | ||
| 329 | my ($self, @order_by) = @_; | ||
| 330 | |||
| 331 | my @part = (); | ||
| 332 | foreach my $order (@order_by) { | ||
| 333 | my @path = split(/\./, $order->[0]); | ||
| 334 | |||
| 335 | throw gettext('Unknown field "%s"', $path[0]) | ||
| 336 | if exists($self->{'__EXISTS_FIELDS__'}) && !$self->{'__EXISTS_FIELDS__'}{$path[0]}; | ||
| 337 | |||
| 338 | my $type_operation = $self->_get_order_operation($order->[0]); | ||
| 339 | |||
| 340 | my $value = '$_[%s]'; | ||
| 341 | $value .= "->{$_}" foreach @path; | ||
| 342 | |||
| 343 | if ($order->[1]) { | ||
| 344 | push(@part, sprintf("($value %s $value)", 1, $type_operation, 0)); | ||
| 345 | } else { | ||
| 346 | push(@part, sprintf("($value %s $value)", 0, $type_operation, 1)); | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | my $body = join(' || ', @part); | ||
| 351 | |||
| 352 | return $self->_get_sub($body); | ||
| 353 | } | ||
| 354 | |||
| 355 | sub _get_filter_operation { | ||
| 356 | my ($self, $field, $op) = @_; | ||
| 357 | |||
| 358 | my $type = $self->definition->{$field}{'type'} // 'string'; | ||
| 359 | |||
| 360 | return $FILTER_OPERATIONS->{$type}{$op} // throw gettext('Unknow operation "%s"', $op); | ||
| 361 | } | ||
| 362 | |||
| 363 | sub _get_order_operation { | ||
| 364 | my ($self, $field) = @_; | ||
| 365 | |||
| 366 | my $type = $self->definition->{$field}{'type'} // 'string'; | ||
| 367 | |||
| 368 | return $ORDER_OPERATIONS->{$type}; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub _get_value { | ||
| 372 | my ($self, $field, $value, $op) = @_; | ||
| 373 | |||
| 374 | return 'undef' unless defined($value); | ||
| 375 | |||
| 376 | #TODO: REGEXP | ||
| 377 | if (defined($op) && $op =~ /LIKE/i) { | ||
| 378 | return 'm/' . quotemeta($value) . '/' . ($self->{'__INSENSITIVE__'} ? 'i' : ''); | ||
| 379 | } | ||
| 380 | |||
| 381 | my $type = $self->definition->{$field}{'type'} // 'string'; | ||
| 382 | |||
| 383 | if ($type eq 'string') { | ||
| 384 | $value =~ s/\\/\\\\/g; | ||
| 385 | $value =~ s/'/\\'/g; | ||
| 386 | $value = "'$value'"; | ||
| 387 | } else { | ||
| 388 | throw gettext('%s - not number', $value) unless looks_like_number($value); | ||
| 389 | } | ||
| 390 | |||
| 391 | return $value; | ||
| 392 | } | ||
| 393 | |||
| 394 | TRUE; | ||
| 395 | |||
| 396 | __END__ |