Skip to content

Commit

Permalink
Removed opts setting for runner and reset the way functions are calle…
Browse files Browse the repository at this point in the history
…d (possibly a breaking change). Bumpted to 1.0.7
  • Loading branch information
robconery committed Nov 11, 2015
1 parent 1bf2175 commit ec0d583
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
40 changes: 20 additions & 20 deletions lib/moebius/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ defmodule Moebius.Query do


@doc """
Insert multiple rows at once, within a single transaction, returning the inserted records. Pass in a composite list, containing the rows to be inserted.
Insert multiple rows at once, within a single transaction, returning the inserted records. Pass in a composite list, containing the rows to be inserted.
Note, the columns to be inserted are defined based on the first record in the list. All records to be inserted must adhere to the same schema.
Example:
Expand All @@ -514,32 +514,32 @@ defmodule Moebius.Query do
transaction fn(pid) ->
bulk_insert_batch(cmd, records, [], column_map)
|> Enum.map(fn(cmd) -> execute(cmd, pid) end)
|> List.flatten
|> List.flatten
end
end

defp bulk_insert_batch(cmd, records, acc, column_map) do
[first | rest] = records

# 20,000 seems to be the optimal number here. Technically you can go up to 34,464, but I think Postgrex imposes a lower limit, as I
# hit a wall at 34,000, but succeeded at 30,000. Perf on 100k records is best at 20,000.
max_params = 20000
# hit a wall at 34,000, but succeeded at 30,000. Perf on 100k records is best at 20,000.
max_params = 20000
cmd = %{ cmd | columns: column_map}
max_records_per_command = div(max_params, length(cmd.columns))

{ current, next_batch } = Enum.split(records, max_records_per_command)
this_cmd = bulk_insert_command(cmd, current)
case next_batch do
[] -> Enum.reverse([this_cmd | acc])
_ ->
_ ->
db(cmd.table_name) |> bulk_insert_batch(next_batch, [this_cmd | acc], column_map)
end
end

defp bulk_insert_command(cmd, [first | rest]) do
records = [first | rest]
cols = cmd.columns
vals = Enum.reduce(Enum.reverse(records), [], fn(listitem, acc) ->
vals = Enum.reduce(Enum.reverse(records), [], fn(listitem, acc) ->
Enum.concat(Keyword.values(listitem), acc) end)

params_sql = elem(Enum.map_reduce(vals, 0, fn(v, acc) -> {"$#{acc + 1}", acc + 1} end),0)
Expand Down Expand Up @@ -850,8 +850,8 @@ defmodule Moebius.Query do
```
"""
def function(cmd, function_name) do
function_command(cmd, function_name, [])
def function(function_name) do
function_command(function_name, [])
|> execute
end

Expand All @@ -868,8 +868,8 @@ defmodule Moebius.Query do
```
"""
def function(cmd, function_name, :single) do
function_command(cmd, function_name, [])
def function(function_name, :single) do
function_command(function_name, [])
|> execute(:single)
end

Expand All @@ -886,8 +886,8 @@ defmodule Moebius.Query do
```
"""
def function(cmd, function_name, params) do
function_command(cmd, function_name, params)
def function(function_name, params) do
function_command(function_name, params)
|> execute
end

Expand All @@ -902,27 +902,27 @@ defmodule Moebius.Query do
```
"""
def function(cmd, function_name, :single, params) do
function_command(cmd, function_name, params)
def function(function_name, :single, params) do
function_command(function_name, params)
|> execute(:single)
end

@doc """
Creates a function command
"""
def function_command(cmd, function_name, params \\ [])
def function_command(function_name, params \\ [])

def function_command(cmd, function_name, params) when not is_list(params),
do: function_command(cmd, function_name, [params])
def function_command(function_name, params) when not is_list(params),
do: function_command(function_name, [params])

def function_command(cmd, function_name, params) do
def function_command(function_name, params) do
arg_list = cond do
length(params) > 0 -> Enum.map_join(1..length(params), ", ", &"$#{&1}")
true -> ""
end

sql = "select * from #{function_name}(#{arg_list});"
%{cmd | sql: sql, params: params}
%Moebius.QueryCommand{sql: sql, params: params}
end


Expand Down
13 changes: 8 additions & 5 deletions lib/moebius/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ defmodule Moebius.Runner do
"""
def connect do
extensions = [{Postgrex.Extensions.JSON, library: Poison}]

Application.get_env(:moebius, :connection)
|> Keyword.update(:extensions, extensions, &(&1 ++ extensions))
|> Postgrex.Connection.start_link
|> Keyword.update(:extensions, extensions, &(&1 ++ extensions))
|> Postgrex.Connection.start_link
end

@doc """
Expand Down Expand Up @@ -61,12 +61,15 @@ defmodule Moebius.Runner do
currently. These functions hand off to PSQL because Postgrex can't run more than
one command per query.
"""
def run_with_psql(sql, db \\ @opts[:database]) do
def run_with_psql(sql, db \\ nil) do
if db == nil, do: [database: db] = Application.get_env(:moebius, :connection)
["-d", db, "-c", sql, "--quiet", "--set", "ON_ERROR_STOP=1", "--no-psqlrc"]
|> call_psql
end

def run_file_with_psql(file, db \\ @opts[:database]) do
def run_file_with_psql(file, db \\ nil) do
if db == nil, do: [database: db] = Application.get_env(:moebius, :connection)

["-d", db, "-f", file, "--quiet", "--set", "ON_ERROR_STOP=1", "--no-psqlrc"]
|> call_psql
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Moebius.Mixfile do
use Mix.Project

@version "1.0.6"
@version "1.0.7"

def project do
[app: :moebius,
Expand Down
6 changes: 2 additions & 4 deletions test/moebius/function_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ defmodule Moebius.FunctionTest do

test "a simple function call is constructed" do

cmd = db(:users)
|> function_command(:all_users)
cmd = function_command(:all_users)


assert cmd.sql == "select * from all_users();"
end

test "a simple function call is constructed with args" do

cmd = db(:users)
|> function_command(:all_users, name: "steve")
cmd = function_command(:all_users, name: "steve")


assert cmd.sql == "select * from all_users($1);"
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ insert into users(email, first, last) values('[email protected]','Mary','Muggtler');
insert into users(email, first, last) values('[email protected]','Mike','Ghruoisl');
"

case Moebius.Runner.run_with_psql sql, "meebuss" do
case Moebius.Runner.run_with_psql sql do
{_res, 0} -> true
{:error, err} -> raise err
end

0 comments on commit ec0d583

Please sign in to comment.