Skip to content

Commit

Permalink
Merge pull request #11 from tsloughter/type-casts-fix
Browse files Browse the repository at this point in the history
handle type casts when parsing named params
  • Loading branch information
artemeff authored Nov 12, 2017
2 parents a7bc4ab + 413128b commit 0741484
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
5 changes: 4 additions & 1 deletion examples/queries.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

--
-- Get all users from database
-- :get_all_users
Expand All @@ -14,3 +13,7 @@ users WHERE id = ?
-- name: get_all_schema_users
SELECT *
FROM :schema.users

--
-- name: accept_type_casts
select '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->2
4 changes: 2 additions & 2 deletions src/eql_parse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ lists:map(fun({param, P}) -> binary_to_atom(iolist_to_binary(P), utf8);

-spec 'substatement'(input(), index()) -> parse_result().
'substatement'(Input, Index) ->
p(Input, Index, 'substatement', fun(I,D) -> (p_zero_or_more(p_seq([p_not(p_string(<<":">>)), p_not(fun 'comment_marker'/2), p_anything()])))(I,D) end, fun(Node, _Idx) ->Node end).
p(Input, Index, 'substatement', fun(I,D) -> (p_zero_or_more(p_seq([p_choose([p_not(p_string(<<":">>)), p_string(<<"::">>), p_seq([p_string(<<":">>), p_not(p_charclass(<<"[a-zA-Z0-9_-]">>))])]), p_not(fun 'comment_marker'/2), p_anything()])))(I,D) end, fun(Node, _Idx) ->Node end).

-spec 'param'(input(), index()) -> parse_result().
'param'(Input, Index) ->
p(Input, Index, 'param', fun(I,D) -> (p_seq([p_string(<<":">>), p_label('key', p_one_or_more(p_seq([p_not(p_string(<<"\s">>)), p_not(p_string(<<".">>)), p_not(fun 'crlf'/2), p_anything()])))]))(I,D) end, fun(Node, _Idx) ->proplists:get_value(key, Node) end).
p(Input, Index, 'param', fun(I,D) -> (p_seq([p_string(<<":">>), p_label('key', p_one_or_more(p_charclass(<<"[a-zA-Z0-9_-]">>)))]))(I,D) end, fun(Node, _Idx) ->proplists:get_value(key, Node) end).

-spec 'comment_marker'(input(), index()) -> parse_result().
'comment_marker'(Input, Index) ->
Expand Down
2 changes: 1 addition & 1 deletion src/eql_parse.peg
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ lists:map(fun({param, P}) -> binary_to_atom(iolist_to_binary(P), utf8);
end, lists:flatten(Node))
`;

substatement <- (!':' !comment_marker .)* ~;
substatement <- ((!':' / "::" / ":" ![a-zA-Z0-9_-] ) !comment_marker .)* ~;

param <- ":" key:([a-zA-Z0-9_-])+ `proplists:get_value(key, Node)`;

Expand Down
3 changes: 2 additions & 1 deletion test/eql_compiler_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ test_file() ->
{ok, Queries} = eql_parse:parse(Source),
?assertMatch([ {get_all_users, <<"SELECT * FROM users">>}
, {get_user_by_id, <<"SELECT * FROM users WHERE id = ?">>}
, {get_all_schema_users, [<<"SELECT * FROM ">>, schema, <<".users">>]}], Queries).
, {get_all_schema_users, [<<"SELECT * FROM ">>, schema, <<".users">>]}
, {accept_type_casts, <<"select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json->2">>}], Queries).

test_file_namespace() ->
eql:new_tab(test_tab),
Expand Down

0 comments on commit 0741484

Please sign in to comment.