From f3fd67378f45a860b6ddb91cadfe50092c4e09e4 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 19 Mar 2008 19:35:43 +0000 Subject: [PATCH] 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. --- lib/WebGUI/SQL.pm | 40 ++++++++++++++++++++++++++++++++++ lib/WebGUI/Shop/Tax.pm | 7 +----- lib/WebGUI/Shop/Transaction.pm | 7 +----- t/SQL.t | 20 ++++++++++++++++- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/lib/WebGUI/SQL.pm b/lib/WebGUI/SQL.pm index 1f067775e..6292f0038 100644 --- a/lib/WebGUI/SQL.pm +++ b/lib/WebGUI/SQL.pm @@ -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 ( ) diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index 07249ddcf..50f42dd34 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -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 ?,?'; diff --git a/lib/WebGUI/Shop/Transaction.pm b/lib/WebGUI/Shop/Transaction.pm index 19ae91884..422233a32 100644 --- a/lib/WebGUI/Shop/Transaction.pm +++ b/lib/WebGUI/Shop/Transaction.pm @@ -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 ?,?'; diff --git a/t/SQL.t b/t/SQL.t index e780696d4..f381bd684 100644 --- a/t/SQL.t +++ b/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'); }