-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor query builder api *breaking change* (#60)
* refactor query builder api * add @moduledoc since: 2.5.0
- Loading branch information
Showing
9 changed files
with
325 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,59 @@ | ||
defmodule EctoShorts.QueryBuilder do | ||
@moduledoc "Behaviour for query building from filter tuples" | ||
@moduledoc """ | ||
Specifies the query builder API required from adapters. | ||
""" | ||
@moduledoc since: "2.5.0" | ||
|
||
@type filter_tuple :: {filter_type :: atom, value :: any} | ||
@type accumulator_query :: Ecto.Query.t | ||
@type adapter :: module() | ||
@type filter_key :: atom() | ||
@type filter_value :: any() | ||
@type query :: Ecto.Query.t() | ||
@type queryable :: Ecto.Queryable.t() | ||
|
||
@doc "Adds to accumulator query with filter_type and value" | ||
@callback create_schema_filter(filter_tuple, accumulator_query) :: Ecto.Query.t | ||
@doc """ | ||
Adds an expression to they query given a filter key and value. | ||
@spec create_schema_filter(module, filter_tuple, accumulator_query) :: Ecto.Query.t | ||
def create_schema_filter(builder, filter_tuple, query) do | ||
builder.create_schema_filter(filter_tuple, query) | ||
end | ||
The `Ecto.Query` returned should should be same as if it was | ||
written using the `Ecto.Query` dsl. | ||
For example the following function call | ||
```elixir | ||
iex> Ecto.Query.from(c in EctoShorts.Support.Schemas.Comment, where: c.id == 1) | ||
#Ecto.Query<from c0 in EctoShorts.Support.Schemas.Comment, where: c0.id == 1> | ||
``` | ||
is equivalent to | ||
```elixir | ||
iex> EctoShorts.QueryBuilder.create_schema_filter( | ||
...> EctoShorts.QueryBuilder.Schema, | ||
...> EctoShorts.Support.Schemas.Comment, | ||
...> :id, | ||
...> 1 | ||
...> ) | ||
#Ecto.Query<from c0 in EctoShorts.Support.Schemas.Comment, where: c0.id == ^1> | ||
``` | ||
""" | ||
@callback create_schema_filter(query(), filter_key(), filter_value()) :: query() | ||
|
||
@spec query_schema(Ecto.Queryable.t) :: Ecto.Queryable.t() | ||
@doc "Pulls the schema from a query" | ||
def query_schema(%{from: %{source: {_, schema}}}), do: query_schema(schema) | ||
def query_schema(%{from: %{query: %{from: {_, schema}}}}), do: schema | ||
def query_schema(query), do: query | ||
@doc """ | ||
Invokes the callback function `c:EctoShorts.QueryBuilder.create_schema_filter/3`. | ||
Returns an `Ecto.Query`. | ||
### Examples | ||
iex> EctoShorts.QueryBuilder.create_schema_filter( | ||
...> EctoShorts.QueryBuilder.Common, | ||
...> EctoShorts.Support.Schemas.Comment, | ||
...> :first, | ||
...> 1_000 | ||
...> ) | ||
#Ecto.Query<from c0 in EctoShorts.Support.Schemas.Comment, limit: ^1000> | ||
""" | ||
@spec create_schema_filter(adapter(), query(), filter_key(), filter_value()) :: query() | ||
def create_schema_filter(adapter, query, filter_key, filter_value) do | ||
adapter.create_schema_filter(query, filter_key, filter_value) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.