From cc0ca06470d498678dc9ba84177bb5323bea53d8 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 18 Dec 2017 14:46:32 +0000 Subject: [PATCH 1/2] fixed counts for sqlite vs postgres --- lib/Pear/LocalLoop/Controller/Admin/Reports.pm | 5 ++++- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 10 ++++++++-- .../LocalLoop/Controller/Api/V1/Customer/Graphs.pm | 5 ++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Admin/Reports.pm b/lib/Pear/LocalLoop/Controller/Admin/Reports.pm index aff6bc9..494cc10 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Reports.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Reports.pm @@ -18,7 +18,10 @@ sub transaction_data { columns => [ { quantised => $quantised_column, - count => \"COUNT(*)", + count => $c->pg_or_sqlite( + 'count', + "COUNT(*)", + ), sum_distance => $c->pg_or_sqlite( 'SUM("me"."distance")', 'SUM("me"."distance")', diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index e2b98e2..2db7371 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -86,7 +86,10 @@ sub post_customer { columns => [ { quantised => 'quantised_weeks', - count => \"COUNT(*)", + count => $c->pg_or_sqlite( + 'count', + "COUNT(*)", + ), } ], group_by => 'quantised_weeks', @@ -103,7 +106,10 @@ sub post_customer { join => { 'seller' => 'organisation' }, columns => { sector => "organisation.sector", - count => \"COUNT(*)", + count => $c->pg_or_sqlite( + 'count', + "COUNT(*)", + ), }, group_by => "organisation.sector", order_by => { '-desc' => "COUNT(*)" }, diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm index f48e52b..b461220 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Customer/Graphs.pm @@ -112,7 +112,10 @@ sub _purchases_avg_spend_duration { columns => [ { quantised => 'quantised_days', - count => \"COUNT(*)", + count => $c->pg_or_sqlite( + 'count', + "COUNT(*)", + ), sum_value => $c->pg_or_sqlite( 'SUM("me"."value")', 'SUM("me"."value")', From 8e34f34bd6e70b844030e36926752b4c08184181 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 18 Dec 2017 15:08:15 +0000 Subject: [PATCH 2/2] added missing pg_or_sqlite sub --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index 2db7371..5d5f32c 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -244,4 +244,19 @@ sub post_leaderboards_paged { }); } +sub pg_or_sqlite { + my ( $c, $pg_sql, $sqlite_sql ) = @_; + + my $driver = $c->schema->storage->dbh->{Driver}->{Name}; + + if ( $driver eq 'Pg' ) { + return \$pg_sql; + } elsif ( $driver eq 'SQLite' ) { + return \$sqlite_sql; + } else { + $c->app->log->warn('Unknown Driver Used'); + return undef; + } +} + 1;