Current section
Files
Jump to
Current section
Files
rebar3_sqlc
README.md
README.md
# rebar3_sqlc
rebar3 plugin for compiling .sql SQL modules to Erlang modules, using [sqlc](https://hex.pm/packages/sqlc).
Requires Erlang/OTP 27 or later.
## Use
Add the plugin to your rebar3 config:
```erlang
{plugins, [rebar3_sqlc]}.
{provider_hooks, [
{pre, [
{compile, {sqlc, compile}},
{clean, {sqlc, clean}}
]}
]}.
```
`rebar3 compile` then generates an Erlang module next to each SQL module, and `rebar3 clean` deletes the modules
generated by sqlc, including those of SQL modules renamed or deleted since.
Then create your SQL module file inside your project's src/ directory, for example as `src/my_app_queries.sql`:
```sql
query user_by_id(:id uuid) as
select user_id as id, name, avatar_url, registered_at
from user
where user_id = :id;
mutation update_user(:id uuid, :updates jsonb) as
update user
set
name = case when :updates ? 'name' then :updates ->> 'name' else user.name end,
avatar_url = case when :updates ? 'avatarUrl' then :updates ->> 'avatarUrl' else user.avatar_url end
where user_id = :id
returning user_id AS id, name, avatar_url, registered_at;
```
See the [sqlc documentation](https://sqlc.hexdocs.pm/sqlc.html#module-definitions) for the syntax of SQL modules.
The SQL module above produces `src/my_app_queries.erl`, next to it, with a function and its spec for each
definition:
```erlang
%% @generated by sqlc
-module(my_app_queries).
-moduledoc(false).
-export([user_by_id/1, update_user/1]).
-export_type([request/0]).
-type request() :: #{name := {my_app_queries, atom()}, type := query | mutation,
statement :=
[unicode:unicode_binary() |
{parameter,
#{key := {my_app_queries, atom(), atom()}, value := term(),
type := unicode:unicode_binary()}}]}.
-spec user_by_id(#{id := term()}) -> request().
user_by_id(#{id := Id}) ->
#{name => {my_app_queries, user_by_id}, type => query,
statement =>
[<<"select user_id as id, name, avatar_url, registered_at\nfrom "
"user\nwhere user_id = "/utf8>>,
{parameter,
#{key => {my_app_queries, user_by_id, id}, value => Id, type => <<"uuid">>}}]}.
-spec update_user(#{id := term(), updates := term()}) -> request().
update_user(#{id := Id, updates := Updates}) ->
#{name => {my_app_queries, update_user}, type => mutation,
statement =>
[<<"update user\n set\n name = case when "/utf8>>,
{parameter,
#{key => {my_app_queries, update_user, updates}, value => Updates,
type => <<"jsonb">>}},
<<" ? 'name' then "/utf8>>,
{parameter,
#{key => {my_app_queries, update_user, updates}, value => Updates,
type => <<"jsonb">>}},
<<" ->> 'name' else user.name end,\n avatar_url = case "
"when "/utf8>>,
{parameter,
#{key => {my_app_queries, update_user, updates}, value => Updates,
type => <<"jsonb">>}},
<<" ? 'avatarUrl' then "/utf8>>,
{parameter,
#{key => {my_app_queries, update_user, updates}, value => Updates,
type => <<"jsonb">>}},
<<" ->> 'avatarUrl' else user.avatar_url end\n where user_id = "/utf8>>,
{parameter,
#{key => {my_app_queries, update_user, id}, value => Id, type => <<"uuid">>}},
<<"\n returning user_id AS id, name, avatar_url, registered_at"/utf8>>]}.
```