turbo_ecto v0.1.7 Turbo.Ecto.Services.BuildSearchQuery
Turbo.Ecto.Services.BuildSearchQuery is a service module which serves the search hook.
@search_types is a collection of all the 8 valid search_types that come shipped with
Turbo.Ecto’s default search hook. The types are:
- [x]
eq: equal. (SQL:col = 'value') - [x]
not_eq: not equal. (SQL: col != ‘value’) - [x]
lt: less than. (SQL: col < 1024) - [x]
lteq: less than or equal. (SQL: col <= 1024) - [x]
gt: greater than. (SQL: col > 1024) - [x]
gteq: greater than or equal. (SQL: col >= 1024) - [x]
is_present: not null and not empty. (SQL: col is not null AND col != ‘’) - [ ]
blank: is null or empty. (SQL: col is null OR col = ‘’) - [x]
is_null: is null or not null (SQL: col is null) - [x]
like: contains trem value. (SQL: col like “%value%”) - [ ]
not_like: not contains value. (SQL: col not like ‘%value%’) - [x]
ilike: contains value in a case insensitive fashion. (SQL: ) - [ ]
not_ilike: not contains value in a case insensitive fashion. (SQL: - [x]
incontains. (SQL: col in (‘1024’, ‘1025’)) - [ ]
not_innot contains. (SQL: col not in (‘1024’, ‘1025’)) - [ ]
start_withstart with. (SQL: col like ‘value%’) - [ ]
not_start_withnot start with. (SQL: col not like ‘value%’) - [ ]
end_withend with. (SQL: col like ‘%value’) - [ ]
not_end_with(SQL: col not like ‘%value’) - [x]
is_trueis true. (SQL: col is true) - [x]
between: between begin and end. (SQL: begin <= col <= end)
Link to this section Summary
Functions
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is between
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is eq
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is gt
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is gteq
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is ilike
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is in
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is is_null
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is is_present
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is is_null
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is like
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is lt
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is lteq
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is not_eq
Link to this section Types
search_type() :: :eq | :not_eq | :gt | :lt | :gteq | :lteq | :is_null | :in | :not_in | :present | :blank | :like | :not_like | :ilike | :not_ilike | :start_with | :end_with | true | false | :between
Link to this section Functions
handle_between( Ecto.Query.t(), atom(), Keyword.t(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is between.
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_between(queryable, :field_1, [1, 10], :where)
#Ecto.Query<from p in "parents", where: p.field_1 >= ^1 and p.field_1 <= ^10>
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_between(queryable, :field_1, [1, 10], :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 >= ^1 and p.field_1 <= ^10>
handle_eq( Ecto.Query.t(), atom(), term(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is eq.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_eq(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 == ^"field_!">
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_eq(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 == ^"field_!">
handle_gt( Ecto.Query.t(), atom(), term(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is gt.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gt(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 > ^"field_!">
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gt(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 > ^"field_!">
handle_gteq( Ecto.Query.t(), atom(), term(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is gteq.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gteq(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 >= ^"field_!">
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_gteq(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 >= ^"field_!">
handle_ilike( Ecto.Query.t(), atom(), String.t(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is ilike.
Checkout Ecto.Query.API.ilike/2 for more info.
Assumes that search_expr is in [:where, :or_where].
## Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_ilike(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: ilike(p.field_1, ^"%field_!%")>
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_ilike(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: ilike(p.field_1, ^"%field_!%")>
handle_in( Ecto.Query.t(), atom(), Keyword.t(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is in.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_in(queryable, :field_1, [1,10], :where)
#Ecto.Query<from p in "parents", where: p.field_1 in ^[1, 10]>
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_in(queryable, :field_1, [1, 10], :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 in ^[1, 10]>
handle_is_null( Ecto.Query.t(), atom(), boolean(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is is_null.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_is_null(queryable, :field_1, true, :where)
#Ecto.Query<from p in "parents", where: is_nil(p.field_1)>
iex> BuildSearchQuery.handle_is_null(queryable, :field_1, false, :where)
#Ecto.Query<from p in "parents", where: not(is_nil(p.field_1))>
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_is_null(queryable, :field_1, true, :or_where)
#Ecto.Query<from p in "parents", or_where: is_nil(p.field_1)>
iex> BuildSearchQuery.handle_is_null(queryable, :field_1, false, :or_where)
#Ecto.Query<from p in "parents", or_where: not(is_nil(p.field_1))>
handle_is_present( Ecto.Query.t(), atom(), boolean(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is is_present.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_is_present(queryable, :field_1, true, :where)
#Ecto.Query<from p in "parents", where: p.field_1 == true or not(is_nil(p.field_1))>
iex> BuildSearchQuery.handle_is_present(queryable, :field_1, false, :where)
#Ecto.Query<from p in "parents", where: p.field_1 == false or is_nil(p.field_1)>
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_is_present(queryable, :field_1, true, :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 == true or not(is_nil(p.field_1))>
iex> BuildSearchQuery.handle_is_present(queryable, :field_1, false, :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 == false or is_nil(p.field_1)>
handle_is_true( Ecto.Query.t(), atom(), boolean(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is is_null.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_is_true(queryable, :field_1, true, :where)
#Ecto.Query<from p in "parents", where: p.field_1 == true>
iex> BuildSearchQuery.handle_is_true(queryable, :field_1, false, :where)
#Ecto.Query<from p in "parents", where: p.field_1 == false>
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_is_true(queryable, :field_1, true, :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 == true>
iex> BuildSearchQuery.handle_is_true(queryable, :field_1, false, :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 == false>
handle_like( Ecto.Query.t(), atom(), String.t(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is like.
Checkout Ecto.Query.API.like/2 for more info.
NOTE: Be careful of Like Injections
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_like(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: like(p.field_1, ^"%field_!%")>
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_like(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: like(p.field_1, ^"%field_!%")>
handle_lt( Ecto.Query.t(), atom(), term(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is lt.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lt(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 < ^"field_!">
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lt(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 < ^"field_!">
handle_lteq( Ecto.Query.t(), atom(), term(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is lteq.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lteq(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 <= ^"field_!">
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_lteq(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 <= ^"field_!">
handle_not_eq( Ecto.Query.t(), atom(), term(), Turbo.Ecto.Services.BuildSearchQuery.search_expr() ) :: Ecto.Query.t()
Builds a searched queryable on top of the given queryable using
field, search_term and search_expr when the search_type is not_eq.
Assumes that search_expr is in [:where, :or_where].
Examples
When search_expr is :where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_not_eq(queryable, :field_1, "field_!", :where)
#Ecto.Query<from p in "parents", where: p.field_1 != ^"field_!">
When search_expr is :or_where
iex> alias Turbo.Ecto.Services.BuildSearchQuery
iex> import Ecto.Query
iex> queryable = from u in "parents"
#Ecto.Query<from p in "parents">
iex> BuildSearchQuery.handle_not_eq(queryable, :field_1, "field_!", :or_where)
#Ecto.Query<from p in "parents", or_where: p.field_1 != ^"field_!">
run( Ecto.Query.t(), atom(), {Turbo.Ecto.Services.BuildSearchQuery.search_expr(), Turbo.Ecto.Services.BuildSearchQuery.search_type()}, any() ) :: {Ecto.Query.t()}