FIQLEx v0.1.0 FIQLEx.QueryBuilders.SQLQueryBuilder View Source

Builds SQL queries from FIQL AST.

Possible options for this query builder are:

  • table: The table name to use in the FROM statement (defaults to "table")
  • select: SELECT statement to build (see below).

Select option

Possible values of the select option are:

  • :all: use SELECT * (default value)
  • :from_selectors: Searches for all selectors in the FIQL AST and use them as SELECT statement. For instance, for the following query: age=ge=25;name==*Doe, the SELECT statement will be SELECT age, name
  • selectors: You specify a list of items you want to use in the SELECT statement.

Examples

iex> FIQLEx.build_query(FIQLEx.parse!("name==John"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name = 'John'"}

iex> FIQLEx.build_query(FIQLEx.parse!("name==John"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors, table: "author")
{:ok, "SELECT name FROM author WHERE name = 'John'"}

iex> FIQLEx.build_query(FIQLEx.parse!("name==John"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :all)
{:ok, "SELECT * FROM table WHERE name = 'John'"}

iex> FIQLEx.build_query(FIQLEx.parse!("name==John"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: ["another", "other"])
{:ok, "SELECT another, other FROM table WHERE name = 'John'"}

iex> FIQLEx.build_query(FIQLEx.parse!("name==John;(age=gt=25,age=lt=18)"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name, age FROM table WHERE (name = 'John' AND (age > 25 OR age < 18))"}

iex> FIQLEx.build_query(FIQLEx.parse!("name=ge=John"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:error, :invalid_format}

iex> FIQLEx.build_query(FIQLEx.parse!("name=ge=2019-02-02T18:23:03Z"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name >= '2019-02-02T18:23:03Z'"}

iex> FIQLEx.build_query(FIQLEx.parse!("name!=12.4"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name <> 12.4"}

iex> FIQLEx.build_query(FIQLEx.parse!("name!=true"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name <> true"}

iex> FIQLEx.build_query(FIQLEx.parse!("name!=false"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name <> false"}

iex> FIQLEx.build_query(FIQLEx.parse!("name"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name IS NOT NULL"}

iex> FIQLEx.build_query(FIQLEx.parse!("name!=(1,2,Hello)"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name NOT IN (1, 2, 'Hello')"}

iex> FIQLEx.build_query(FIQLEx.parse!("name!='Hello \\'World'"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name <> 'Hello ''World'"}

iex> FIQLEx.build_query(FIQLEx.parse!("name!=*Hello"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name NOT LIKE '%Hello'"}

iex> FIQLEx.build_query(FIQLEx.parse!("name==Hello*"), FIQLEx.QueryBuilders.SQLQueryBuilder, select: :from_selectors)
{:ok, "SELECT name FROM table WHERE name LIKE 'Hello%'"}

Link to this section Summary

Functions

Returns a list of all selectors for a given AST

This function will go deeper in the ast traversal.

Same as handle_ast/3 but returns the state or raises an exception.

Link to this section Functions

Link to this function

get_selectors(ast)

View Source
get_selectors(ast :: FIQLEx.ast()) :: [binary()]

Returns a list of all selectors for a given AST

Link to this function

handle_ast(curr_ast, ast, state)

View Source
handle_ast(FIQLEx.ast(), FIQLEx.ast(), any()) ::
  {:ok, any()} | {:error, any()}

This function will go deeper in the ast traversal.

Parameters are:

  • curr_ast: The AST we want to go deeper with
  • ast: The global AST
  • state: The current state of your query builder

The function returns {:ok, state} if everything is fine, and {:error, reason} if there is an error

Link to this function

handle_ast!(curr_ast, ast, state)

View Source
handle_ast!(FIQLEx.ast(), FIQLEx.ast(), any()) :: any()

Same as handle_ast/3 but returns the state or raises an exception.