Skip to content

Commit

Permalink
Fix search for iso dates in timestamp fields
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansbv committed Sep 14, 2023
1 parent 18d8aef commit 73b1b09
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/Tpda3/Model.pm
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ sub build_sql_where {
die "Unknown 'find_type': $find_type for '$field'";
}
}

say $where if $self->debug;
return $where;
}

Expand Down
38 changes: 28 additions & 10 deletions lib/Tpda3/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ sub special_ops {
return ( $sql, @bind );
}
},

# special op for PostgreSQL syntax: extract date from timestamp
{ regex => qr/^extractdate$/i,
handler => sub {
my ( $self, $field, $op, $arg ) = @_;
$arg = [$arg] if not ref $arg;
my ($placeholder) = $self->_convert('?');
my $sql = $self->_sqlcase('date(') . $field . ')'
. " = $placeholder ";
my @bind = $self->_bindtype( $field, @$arg );
return ( $sql, @bind );
}
},

# special op for PostgreSQL syntax: field SIMILAR TO 'regex1'
{ regex => qr/^similar_to$/i,
handler => sub {
Expand Down Expand Up @@ -228,15 +242,17 @@ sub process_date_string {
sub identify_date_string {
my ( $self, $is ) = @_;

# When date format is... Type is ...
return
$is eq q{} ? 'nothing'
: $is =~ m/^(\d{4})[\.\/-](\d{2})[\.\/-](\d{2})$/ ? "dateiso:$is"
: $is =~ m/^(\d{2})[\.\/-](\d{2})[\.\/-](\d{4})$/ ? "dateamb:$is"
: $is =~ m/^(\d{4})[\.\/-](\d{1,2})$/ ? "dateym:$1:$2"
: $is =~ m/^(\d{1,2})[\.\/-](\d{4})$/ ? "datemy:$2:$1"
: $is =~ m/^(\d{4})$/ ? "datey:$1"
: "dataerr:$is";
# When date format is... Type is ...
my $rez =
$is eq q{} ? "nothing"
: $is =~ m{^(\d{4})[./-](\d{2})[\.\/-](\d{2})$} ? "dateiso:$is"
: $is =~ m{^(\d{2})[./-](\d{2})[\.\/-](\d{4})$} ? "dateamb:$is"
: $is =~ m{^(\d{4})[./-](\d{1,2})$} ? "dateym:$1:$2"
: $is =~ m{^(\d{1,2})[./-](\d{4})$} ? "datemy:$2:$1"
: $is =~ m{^(\d{4})$} ? "datey:$1"
: "dataerr:$is";
$rez =~ s{[./]}{-}g;
return $rez;
}

sub format_query {
Expand Down Expand Up @@ -267,7 +283,9 @@ sub year_month {

sub date_string {
my ($date) = @_;
return $date;
my $where = {};
$where->{-extractdate} = [$date] if ($date);
return $where;
}

sub do_error {
Expand Down

0 comments on commit 73b1b09

Please sign in to comment.