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] in contains. (SQL: col in (‘1024’, ‘1025’))
  • [ ] not_in not contains. (SQL: col not in (‘1024’, ‘1025’))
  • [ ] start_with start with. (SQL: col like ‘value%’)
  • [ ] not_start_with not start with. (SQL: col not like ‘value%’)
  • [ ] end_with end with. (SQL: col like ‘%value’)
  • [ ] not_end_with (SQL: col not like ‘%value’)
  • [x] is_true is 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

Link to this type search_expr()
search_expr() :: :where | :or_where
Link to this type search_type()
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

Link to this function handle_between(queryable, field, search_term, atom)

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>
Link to this function handle_eq(queryable, field, search_term, atom)

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_!">
Link to this function handle_gt(queryable, field, search_term, atom)

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_!">
Link to this function handle_gteq(queryable, field, search_term, atom)

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_!">
Link to this function handle_ilike(queryable, field, search_term, atom)

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_!%")>
Link to this function handle_in(queryable, field, search_term, atom)

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]>
Link to this function handle_is_null(queryable, field, bool, atom)

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))>
Link to this function handle_is_present(queryable, field, bool, atom)

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)>
Link to this function handle_is_true(queryable, field, bool, atom)

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>
Link to this function handle_like(queryable, field, search_term, atom)

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_!%")>
Link to this function handle_lt(queryable, field, search_term, atom)

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_!">
Link to this function handle_lteq(queryable, field, search_term, atom)

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_!">
Link to this function handle_not_eq(queryable, field, search_term, atom)

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_!">