turbo_ecto v0.1.5 Turbo.Ecto.Services.BuildSearchQuery

This is learn from https://raw.githubusercontent.com/aditya7iyengar/rummage_ecto/master/lib/rummage_ecto/services/build_search_query.ex

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:

  • eq: equal. (SQL: col = 'value')
  • not_eq: not equal. (SQL: col != ‘value’)
  • lt: less than. (SQL: col < 1024)
  • lteq: less than or equal. (SQL: col <= 1024)
  • gt: greater than. (SQL: col > 1024)
  • gteq: greater than or equal. (SQL: col >= 1024)
  • present: not null and not empty. (SQL: col is not null AND col != ‘’)
  • blank: is null or empty. (SQL: col is null OR col = ‘’)
  • is_null: is null or not null (SQL: col is null)
  • like: contains trem value. (SQL: col like “%value%”)
  • not_like: not contains value. (SQL: col not like ‘%value%’)
  • ilike: contains value in a case insensitive fashion. (SQL: )
  • not_ilike: not contains value in a case insensitive fashion. (SQL:
  • 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’)
  • is_true is true. (SQL: col is true)
  • 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 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 field is_nil (when term is true), or not is_nil (when term is false), based on search_expr given

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

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_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_is_null(queryable, field, bool, atom)

Builds a searched queryable on field is_nil (when term is true), or not is_nil (when term is false), based on search_expr given.

Checkout Ecto.Query.API.like/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_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_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_!">