sqlc (sqlc v0.2.1)

View Source

Generate Erlang modules from SQL definitions.

sqlc compiles named SQL statements into an Erlang module with one function per statement. These functions do not run anything: they return the statement, split into SQL text and parameters, for a database client to run.

Definitions

Definitions are either queries or mutations:

-- Comments can appear between definitions.
query user_by_id(:id int8) as
    select id, name
    from example.user
    where id = :id;

mutation rename_user(:id int8, :name text) as
    update example.user
    set name = :name
    where id = :id;
  • query and mutation only differ by the type of the request/0 returned by the generated function.
  • The name of a definition is the name of the generated function. Names start with a lowercase letter, followed by lowercase letters, digits and underscores.
  • Parameters are declared as :name type, separated by commas. The type can be qualified, such as example.kind, or an array, such as int8[]. It is not interpreted, only passed on to the database client.
  • The statement starts on the line following as, and ends at ;. The indentation of its first line is removed from every line, and no other line can be indented less, except for empty lines.

In a statement, :name refers to a parameter. Each parameter must be used, and can be used any number of times. The following are kept as they are, and can contain : and ;:

  • type casts, such as :id::text
  • quoted text: '...', "...", E'...', text quoted with backticks, and dollar quotes, such as $$...$$ and $body$...$body$
  • comments: -- ... and /* ... */

Anywhere else, :name is a parameter, including in array slices: write list[1 : n] rather than list[1:n].

Generated modules

file/1 and string/2 compile definitions into a module, and to_erl/1 returns its source code. Each definition becomes an exported function, which takes a map with a key for each parameter, and returns a request/0:

1> example:user_by_id(#{id => 42}).
#{name => {example,user_by_id},
  type => query,
  statement =>
      [<<"select id, name\nfrom example.user\nwhere id = ">>,
       {parameter,#{type => <<"int8">>,value => 42,
                    key => {example,user_by_id,id}}}]}

The functions have specs, for eqWAlizer and Dialyzer to check their calls:

-spec user_by_id(#{id := term()}) -> request().

The map has exactly a key for each parameter. The values are term(), as the types of parameters are not interpreted. request() is exported by the generated module, which does not depend on sqlc: it is request/0, with the name of the generated module instead of module/0.

Summary

Types

An error returned by file/1 or string/2.

The cause of an error/0.

A module generated by file/1 or string/2.

A parameter of a request/0.

A request, as returned by the functions of a generated module.

Functions

Compile the definitions in the file at Path.

Compile the definitions in Input into a module named ModuleName.

The Erlang source code of a generated module, encoded in UTF-8.

Types

error()

-type error() ::
          #{type := error_type(), message := string(), line => pos_integer(), column => pos_integer()}.

An error returned by file/1 or string/2.

message describes the error. Syntax errors also include the line and the column where they were found.

error_type()

-type error_type() ::
          syntax |
          {duplicate_request, Request :: atom()} |
          {duplicate_parameter, Request :: atom(), Parameter :: atom()} |
          {missing_parameters, Request :: atom(), [Parameter :: atom()]} |
          {unused_parameters, Request :: atom(), [Parameter :: atom()]} |
          {io, file:posix() | badarg | terminated | system_limit}.

The cause of an error/0.

generated_module()

-opaque generated_module()

A module generated by file/1 or string/2.

parameter()

-type parameter() ::
          #{key := {module(), atom(), atom()}, value := term(), type := unicode:unicode_binary()}.

A parameter of a request/0.

key identifies the parameter: the generated module, the name of the definition and the name of the parameter. type is the type of the parameter in the definition, such as <<"int8">>.

request()

-type request() ::
          #{name := {module(), atom()},
            type := query | mutation,
            statement := [unicode:unicode_binary() | {parameter, parameter()}]}.

A request, as returned by the functions of a generated module.

name identifies the request: the generated module and the name of the definition. statement contains the SQL text and the parameters, in order. To run it, replace each parameter with a placeholder of the database, such as $1 in PostgreSQL, and pass its value separately.

Functions

file(Path)

-spec file(file:filename_all()) -> {ok, generated_module()} | {error, error()}.

Compile the definitions in the file at Path.

The module is named after the file, without its .sql extension: the definitions in src/example.sql give the module example.

string(ModuleName, Input)

-spec string(module(), unicode:chardata()) -> {ok, generated_module()} | {error, error()}.

Compile the definitions in Input into a module named ModuleName.

to_erl(Module)

The Erlang source code of a generated module, encoded in UTF-8.

{ok, Module} = sqlc:file("src/example.sql"),
ok = file:write_file("src/example.erl", sqlc:to_erl(Module)).