Permalink
Browse files

Improves implementation of new API

  • Loading branch information...
1 parent ab08ac6 commit fcb23442ccdea09c2cc344bfcf826e4710540b98 Yurii Rashkovskii committed Jul 9, 2012
Showing with 28 additions and 10 deletions.
  1. +5 −3 lib/exql.ex
  2. +7 −7 lib/queries/select.ex
  3. +16 −0 lib/utils.ex
View
8 lib/exql.ex
@@ -16,7 +16,7 @@ defmodule ExQL do
List.reverse(Enum.reduce body, [],
fn(expr, acc) ->
case acc do
- [i] -> [{:/>, 0, [i, expr]}]
+ [i] -> [(quote do: ExQL.Utils.back_pipeline(unquote(i), unquote(expr)))]
_ -> [expr|acc]
end
end)
@@ -25,12 +25,14 @@ defmodule ExQL do
[expr] ->
quote do
import ExQL.Select
- unquote(query) /> unquote(expr)
+ require ExQL.Utils
+ ExQL.Utils.back_pipeline(unquote(query), unquote(expr))
end
[l,r] ->
quote do
import ExQL.Select
- unquote(query) /> (unquote(l) /> unquote(r))
+ require ExQL.Utils
+ ExQL.Utils.back_pipeline(unquote(query), ExQL.Utils.back_pipeline(unquote(l), unquote(r)))
end
end
end
View
14 lib/queries/select.ex
@@ -1,23 +1,23 @@
defrecord ExQL.Select, dict: [fields: :*] do
use ExQL.Query
- def fields(query, v), do: query.dict Keyword.put(query.dict, :fields, v)
- def from(query, v), do: query.dict Keyword.put(query.dict, :from, v)
+ def fields(v, query), do: query.dict Keyword.put(query.dict, :fields, v)
+ def from(v, query), do: query.dict Keyword.put(query.dict, :from, v)
- defmacro where(query, block) when is_tuple(block) do
+ defmacro where(block, query) when is_tuple(block) do
quote do
f = fn() ->
import Elixir.Builtin, except: unquote(ExQL.Condition.__ops__)
import ExQL.Condition, only: unquote(ExQL.Condition.__ops__)
unquote(block)
end
- ExQL.Select._where(unquote(query), f.())
+ ExQL.Select._where(f.(), unquote(query))
end
end
- defmacro where(query, value) do
- quote do: ExQL.Select._where(unquote(query), unquote(value))
+ defmacro where(value, query) do
+ quote do: ExQL.Select._where(unquote(value), unquote(query))
end
- def _where(query, v), do: query.dict Keyword.put(query.dict, :where, v)
+ def _where(v, query), do: query.dict Keyword.put(query.dict, :where, v)
def statement(:modifiers, query) do
ExQL.Expression.join(dict(query)[:modifiers], :raw, " ")
View
16 lib/utils.ex
@@ -8,4 +8,20 @@ defmodule ExQL.Utils do
def space(string) when is_binary(string), do: string
def space(other), do: other
+ defmacro back_pipeline(left, right) do
+ back_pipeline_op(left, right)
+ end
+
+ def back_pipeline_op(left, { {:.,_,[{:__aliases__,_,[:ExQL,:Utils]},:back_pipeline]}, _, [middle, right] }) do
+ back_pipeline_op(back_pipeline_op(left, middle), right)
+ end
+
+ def back_pipeline_op(left, { call, line, atom }) when is_atom(atom) do
+ { call, line, [left] }
+ end
+
+ def back_pipeline_op(left, { call, line, args }) when is_list(args) do
+ { call, line, args ++ [left] }
+ end
+
end

0 comments on commit fcb2344

Please sign in to comment.