Add a SQL method for building searchable queries.
Add tests for that method. Convert Shop/Tax and Shop/Transaction to use that for their JSON generating methods.
This commit is contained in:
parent
d11899cf19
commit
f3fd67378f
4 changed files with 61 additions and 13 deletions
|
|
@ -312,6 +312,46 @@ sub buildHashRefOfHashRefs {
|
|||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 buildSearchQuery ( $sql, $keywords, $columns )
|
||||
|
||||
Append information to an existing SQL statement for implementing
|
||||
basic search functions. The ammended SQL and an array of placeholder
|
||||
variables will be returned.
|
||||
|
||||
=head3 $sql
|
||||
|
||||
An SQL query. The clauses to add search-like capabilities will be
|
||||
appended to the end of the query.
|
||||
|
||||
=head3 $keywords
|
||||
|
||||
This is the data that will be searched for in columns. An SQL wildcard '%' will
|
||||
be added to the beginning and end of $keywords.
|
||||
|
||||
=head3 $columns
|
||||
|
||||
An arrayref of column names that should be searched for $keywords.
|
||||
|
||||
=cut
|
||||
|
||||
sub buildSearchQuery {
|
||||
my $self = shift;
|
||||
my $sql = shift;
|
||||
my $keywords = shift;
|
||||
my $columns = shift || [];
|
||||
my @placeholders;
|
||||
$sql .= ' where';
|
||||
$keywords = '%'.$keywords.'%';
|
||||
foreach my $field (@{ $columns }) {
|
||||
$sql .= ' or' if (scalar @placeholders > 0);
|
||||
$sql .= qq{ $field like ?};
|
||||
push(@placeholders, $keywords);
|
||||
}
|
||||
return ($sql, @placeholders);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 commit ( )
|
||||
|
|
|
|||
|
|
@ -359,12 +359,7 @@ sub www_getTaxesAsJson {
|
|||
my $sql = 'select SQL_CALC_FOUND_ROWS * from tax';
|
||||
my $keywords = $form->get("keywords");
|
||||
if ($keywords ne "") {
|
||||
$sql .= ' where';
|
||||
foreach my $field (qw(country state city code)) {
|
||||
$sql .= ' or' if (scalar @placeholders > 0);
|
||||
$sql .= qq{ $field like ?};
|
||||
push(@placeholders, '%'.$keywords.'%');
|
||||
}
|
||||
($sql, @placeholders) = $db->buildSearchQuery($sql, $keywords, [qw{country state city code}])
|
||||
}
|
||||
push(@placeholders, $startIndex, $numberOfResults);
|
||||
$sql .= ' order by country desc limit ?,?';
|
||||
|
|
|
|||
|
|
@ -352,12 +352,7 @@ sub www_getTransactionsAsJson {
|
|||
from transaction';
|
||||
my $keywords = $form->get("keywords");
|
||||
if ($keywords ne "") {
|
||||
$sql .= ' where';
|
||||
foreach my $field (qw(amount username orderNumber shippingAddressName shippingAddress1 paymentAddressName paymentAddress1)) {
|
||||
$sql .= ' or' if (scalar @placeholders > 0);
|
||||
$sql .= qq{ $field like ?};
|
||||
push(@placeholders, '%'.$keywords.'%');
|
||||
}
|
||||
($sql, @placeholders) = $db->buildSearchQuery($sql, $keywords, [qw{amount username orderNumber shippingAddressName shippingAddress1 paymentAddressName paymentAddress1}])
|
||||
}
|
||||
push(@placeholders, $startIndex, $numberOfResults);
|
||||
$sql .= ' order by dateOfPurchase desc limit ?,?';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue