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 ?,?';
|
||||
|
|
|
|||
20
t/SQL.t
20
t/SQL.t
|
|
@ -17,7 +17,7 @@ use WebGUI::Session;
|
|||
use Data::Dumper;
|
||||
use Test::Deep;
|
||||
|
||||
use Test::More tests => 53; # increment this value for each test you create
|
||||
use Test::More tests => 57; # increment this value for each test you create
|
||||
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
|
|
@ -267,6 +267,8 @@ cmp_deeply($hrefHref, \%expected, 'buildHashRefOfHashRefs, 2 columns, 1 param');
|
|||
#
|
||||
# buildDataTableStructure
|
||||
#
|
||||
# Uses the testTable data from the preceeding *RefOf*Ref tests above
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my %tableStruct = $session->db->buildDataTableStructure('select * from testTable');
|
||||
|
|
@ -283,6 +285,22 @@ cmp_deeply(
|
|||
'Check table structure',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# buildSearchQuery
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my ($searchQuery, @placeHolders) = $session->db->buildSearchQuery('select * from users', 'Admin', [qw{username alias}]);
|
||||
like($searchQuery, qr{^select \* from users}, 'returned query begins with original query');
|
||||
cmp_deeply(
|
||||
\@placeHolders,
|
||||
[('%Admin%') x 2],
|
||||
'placeholders are the keyword repeated 2 times, one for each column, sandwiched in %'
|
||||
);
|
||||
like($searchQuery, qr{where username like \?}, 'returned query searches username column, first column uses where');
|
||||
like($searchQuery, qr{or alias like \?}, 'returned query searches alias column, second column uses or');
|
||||
|
||||
END: {
|
||||
$session->db->dbh->do('DROP TABLE IF EXISTS testTable');
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue