Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added count_by_sql function #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added count_by_sql function
This is necessary in order to be able to do things like
paginate_by_sql.
  • Loading branch information
davidw committed Sep 29, 2014
commit e4da7f4d96f2dd1f064e3aab38aa64101f6db283
12 changes: 12 additions & 0 deletions src/boss_db.erl
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@
count/1,
count/2,
count/3,
count_by_sql/3,
count_by_sql/4,
counter/1,
counter/2,
incr/1,
@@ -261,6 +263,16 @@ count(Type, Conditions) when is_list(Conditions) ->
count(Type, Conditions, Timeout) ->
db_call({count, Type, normalize_conditions(Conditions)}, Timeout).

%% @spec count( Type::atom(), Sql::string() ) -> integer()
%% @doc Count the number of BossRecords of type `Type' in the
%% database, using the SQL provided in `Sql'

count_by_sql(Type, Sql, Parameters) when is_list(Sql) ->
count_by_sql(Type, Sql, Parameters, ?DEFAULT_TIMEOUT).

count_by_sql(Type, Sql, Parameters, Timeout) when is_list(Sql) ->
db_call({count_by_sql, Type, Sql, Parameters}, Timeout).

%% @spec counter( Id::string() ) -> integer()
%% @doc Treat the record associated with `Id' as a counter and return its value.
%% Returns 0 if the record does not exist, so to reset a counter just use
4 changes: 4 additions & 0 deletions src/boss_db_controller.erl
Original file line number Diff line number Diff line change
@@ -141,6 +141,10 @@ handle_call({count, Type, Conditions}, _From, State) ->
{Adapter, Conn, _} = db_for_type(Type, State),
{reply, Adapter:count(Conn, Type, Conditions), State};

handle_call({count_by_sql, Type, Sql, Parameters}, _From, State) ->
{Adapter, Conn, _} = db_for_type(Type, State),
{reply, Adapter:count_by_sql(Conn, Sql, Parameters), State};

handle_call({counter, Counter}, _From, State) ->
{Adapter, Conn, _} = db_for_counter(Counter, State),
{reply, Adapter:counter(Conn, Counter), State};
6 changes: 5 additions & 1 deletion src/db_adapters/boss_db_adapter_pgsql.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-module(boss_db_adapter_pgsql).
-behaviour(boss_db_adapter).
-export([init/1, terminate/1, start/1, stop/0, find/2, find/7, find_by_sql/4]).
-export([count/3, counter/2, incr/3, delete/2, save_record/2]).
-export([count/3, count_by_sql/3, counter/2, incr/3, delete/2, save_record/2]).
-export([push/2, pop/2, dump/1, execute/2, execute/3, transaction/2, create_table/3, table_exists/2]).
-export([get_migrations_table/1, migration_done/3]).
-compile(export_all).
@@ -95,6 +95,10 @@ count(Conn, Type, Conditions) ->
["SELECT COUNT(*) AS count FROM ", TableName, " WHERE ", ConditionClause]),
Count.

count_by_sql(Conn, Sql, Parameters) ->
{ok, _, [{Count}]} = pgsql:equery(Conn, Sql, Parameters),
Count.

counter(Conn, Id) when is_list(Id) ->
Res = pgsql:equery(Conn, "SELECT value FROM counters WHERE name = $1", [Id]),
case Res of