diff --git a/lib/WebGUI/SQL.pm b/lib/WebGUI/SQL.pm index 5ab46ff76..1f067775e 100644 --- a/lib/WebGUI/SQL.pm +++ b/lib/WebGUI/SQL.pm @@ -241,7 +241,7 @@ Builds a data structure that can be converted to JSON and sent to a YUI Data Table. This is basically a hash of information about the results, with one of the keys being an array ref of hashrefs. It also calculates the total records that could have been matched without a limit -statement, as well as how many were actually matched. +statement, as well as how many were actually matched. It returns a hash. =head3 sql @@ -270,7 +270,7 @@ sub buildDataTableStructure { $hash{totalRecords} = $self->quickScalar('select found_rows()') + 0; ##Convert to numeric $hash{recordsReturned} = $sth->rows(); $sth->finish; - return \%hash; + return %hash; } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index 07d877157..07249ddcf 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -368,21 +368,10 @@ sub www_getTaxesAsJson { } push(@placeholders, $startIndex, $numberOfResults); $sql .= ' order by country desc limit ?,?'; - my $transactions = $db->read($sql, \@placeholders); - my $totalRecords = 0+$db->quickScalar('select found_rows()'); ##Must explicitly convert to numerics - my $tally = $transactions->rows(); - my @records = (); - while (my $row = $transactions->hashRef) { - push(@records, $row); - } - my %results = ( - totalRecords => $totalRecords + 0, - recordsReturned => $tally, - startIndex => $startIndex, - sort => undef, - dir => "desc", - records => \@records, - ); + my %results = $db->buildDataTableStructure($sql, \@placeholders); + $results{'startIndex'} = $startIndex; + $results{'sort'} = undef; + $results{'dir'} = "desc"; $session->http->setMimeType('text/json'); return JSON::to_json(\%results); } diff --git a/lib/WebGUI/Shop/Transaction.pm b/lib/WebGUI/Shop/Transaction.pm index a8d53ee5f..19ae91884 100644 --- a/lib/WebGUI/Shop/Transaction.pm +++ b/lib/WebGUI/Shop/Transaction.pm @@ -361,22 +361,10 @@ sub www_getTransactionsAsJson { } push(@placeholders, $startIndex, $numberOfResults); $sql .= ' order by dateOfPurchase desc limit ?,?'; - my $transactions = $db->read($sql, \@placeholders); - my $totalRecords = $db->quickScalar('select found_rows()'); - my $tally = 0; - my @records = (); - while (my $row = $transactions->hashRef) { - push(@records, $row); - $tally++; - } - my %results = ( - totalRecords => $totalRecords + 0, - recordsReturned => $tally, - startIndex => $startIndex, - sort => undef, - dir => "desc", - records => \@records, - ); + my %results = $db->buildDataTableStructure($sql, \@placeholders); + $results{'startIndex'} = $startIndex; + $results{'sort'} = undef; + $results{'dir'} = "desc"; $session->http->setMimeType('text/json'); return JSON::to_json(\%results); } diff --git a/t/Shop/Transaction.t b/t/Shop/Transaction.t index 32a6ef092..5953b9296 100644 --- a/t/Shop/Transaction.t +++ b/t/Shop/Transaction.t @@ -199,6 +199,7 @@ is(scalar @{$transaction->getItems}, 0, "can delete items"); $session->user({userId=>3}); my $json = WebGUI::Shop::Transaction->www_getTransactionsAsJson($session); ok($json, 'www_getTransactionsAsJson returned something'); +diag $json; my $jsonTransactions = JSON::from_json($json); cmp_deeply( $jsonTransactions,