class Jennifer::QueryBuilder::ExpressionBuilder

Overview

Stands for creating criteria for the query.

This class provides straight forward way to define criteria and a bit (pretty huge one) of metaprogramming.

You can use standard method #c to create criteria for current table or for any other (passing it's name as a 2nd argument):

Jennifer::Query["contacts"].join("addresses") { c("contact_id") == c("id", "contacts") }

Also you can use "magic" underscored methods to specify current table fields putting "_" before a name:

Jennifer::Query["contacts"].join("addresses") { _contact_id == c("id", "contacts") }

Obviously, you also can specify same way table name for the field. Just put "__" (double underscore) between table name and field name as well.

Jennifer::Query["contacts"].join("addresses") { _contact_id == _contacts__id }

Because of double underscore between symbols between field and table you can safely reference tables with "_".

Jennifer::Query["facebook_profiles"].join("addresses") { _profile_id == _facebook_profiles__id }

Defined in:

jennifer/query_builder/expression_builder.cr
jennifer/query_builder/function.cr:163
jennifer/query_builder/function.cr:176
jennifer/query_builder/function.cr:189
jennifer/query_builder/function.cr:202
jennifer/query_builder/function.cr:208
jennifer/query_builder/function.cr:214
jennifer/query_builder/function.cr:220
jennifer/query_builder/function.cr:226
jennifer/query_builder/function.cr:239
jennifer/query_builder/function.cr:252
jennifer/query_builder/function.cr:258
jennifer/query_builder/function.cr:271
jennifer/query_builder/function.cr:284
jennifer/query_builder/function.cr:305
jennifer/query_builder/function.cr:318
jennifer/query_builder/function.cr:331
jennifer/query_builder/function.cr:344
jennifer/query_builder/function.cr:357

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(table, relation = nil, query = nil) #

[View source]

Instance Method Detail

def abs(arg0) #

Create "AbsFunction" function.


[View source]
def all(query : Query) #

Creates ALL SQL statement.

nested_query = Jennifer::Query["contacts"].select(:age).where { _tag == "phone" }
Jennifer::Query["contacts"].where { _age > all(nested_query) }

[View source]
def and(first_condition, second_condition, *conditions) #

Combines given first_condition, second_condition and all other conditions by AND operator.

All given conditions will be wrapped in Grouping.

User.all.where { and(_name.like("%on"), _age > 3) }
# WHERE (users.name LIKE '%on' AND users.age > 3)

[View source]
def and(conditions : Array) #

Combines given array of conditions by AND operator.

All given conditions will be wrapped in Grouping.

User.all.where { and([_name.like("%on"), _age > 3]) }
# WHERE (users.name LIKE '%on' AND users.age > 3)

[View source]
def any(query : Query) #

Creates ANY SQL statement.

nested_query = Jennifer::Query["addresses"].where { _main }.select(:contact_id)
Jennifer::Query["contacts"].where { _id == any(nested_query) }

[View source]
def avg(arg0) #

Create "AvgFunction" function.


[View source]
def c(name : String, table_name : String = @table, relation : String? = nil) #

Creates criterion for current table by given name name.

Jennifer::Query["users"].where { c("id") } # => "users"."id"

[View source]
def c_with_relation(name : String, relation : String) #

Creates criterion by given name name for the relation.


[View source]
def cast(expression, type : String) #

Cases expression to the given type.

type field is pasted as-is.

Jennifer::Query[""].select { [cast(sql("'100'", false), "integer").alias("field")] }
# => SELECT CAST('100' AS INTEGER) AS field

[View source]
def ceil(arg0) #

Create "CeilFunction" function.


[View source]
def clone #

Returns a copy of self with all instance variables cloned.


[View source]
def coalesce(*args) #

Create :CoalesceFunction function.


[View source]
def concat(*args) #

Create "ConcatFunction" function.


[View source]
def concat_ws(*args) #

Create "ConcatWsFunction" function.


[View source]
def count(*args) #

Create "CountFunction" function.


[View source]
def current_date #

Create "CurrentDateFunction" function.


[View source]
def current_time #

Create "CurrentTimeFunction" function.


[View source]
def current_timestamp #

Create "CurrentTimestampFunction" function.


[View source]
def floor(arg0) #

Create "FloorFunction" function.


[View source]
def g(condition) #

Creates grouping for the given condition.

Jennifer::Query["users"].where { c1 & g(c2 | c3) }

[View source]
def group(condition) #

Alias for #g.


[View source]
def lower(arg0) #

Create "LowerFunction" function.


[View source]
def max(arg0) #

Create "MaxFunction" function.


[View source]
def min(arg0) #

Create "MinFunction" function.


[View source]
def now #

Create "NowFunction" function.


[View source]
def or(first_condition, second_condition, *conditions) #

Combines given first_condition, second_condition and all other conditions by OR operator.

All given conditions will be wrapped in Grouping.

User.all.where { or(_name.like("%on"), _age > 3) }
# => WHERE (users.name LIKE '%on' OR users.age > 3)

[View source]
def or(conditions : Array) #

Combines given array of conditions by OR operator.

All given conditions will be wrapped in Grouping.

User.all.where { or([_name.like("%on"), _age > 3]) }
# WHERE (users.name LIKE '%on' OR users.age > 3)

[View source]
def primary #

Query's model primary field criterion.

Can be used only in a scope of IModelQuery.


[View source]
def query : Query? #

[View source]
def query=(query : Query?) #

[View source]
def relation : String? #

[View source]
def round(*args) #

Create "RoundFunction" function.


[View source]
def sql(query : String, args : Array(DBAny) = [] of DBAny, use_brackets : Bool = true) #

Adds plain query query.

If you need to wrap query set use_brackets to true.

Jennifer::Query[""].select { [sql("1", false)] } # => SELECT 1
Jennifer::Query[""].select { [sql("1")] }        # => SELECT (1)
Jennifer::Query[""].select { [sql("TIMESTAMP %s AT TIME ZONE %s", ["2001-02-16 20:38:40", "MST"], false)] }
# => SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'MST';

[View source]
def sql(query : String, use_brackets : Bool = true) #

Adds plain query query.

If you need to wrap query set use_brackets to true.

Jennifer::Query[""].select { [sql("1", false)] } # => SELECT 1
Jennifer::Query[""].select { [sql("1")] }        # => SELECT (1)
Jennifer::Query[""].select { [sql("TIMESTAMP %s AT TIME ZONE %s", ["2001-02-16 20:38:40", "MST"], false)] }
# => SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'MST';

[View source]
def star(table : String = @table) #

Creates select star for table.

Jennifer::Query["users"].select { [star("contacts")] } # => SELECT contacts.* FROM users

[View source]
def sum(arg0) #

Create "SumFunction" function.


[View source]
def table : String #

[View source]
def upper(arg0) #

Create "UpperFunction" function.


[View source]
def values(field : Symbol) #

Returns reference to the field in upsert operation.

Jennifer::Query["orders"].upsert(%w(name uid price), [["Order 1", 123, 3]], %w(uid)) do
  {:price => values(:price) + _price}
end

[View source]
def xor(first_condition, second_condition, *conditions) #

Combines given first_condition, second_condition and all other conditions by XOR operator.

All given conditions will be wrapped in Grouping.

User.all.where { xor(_name.like("%on"), _age > 3) }
# => WHERE (users.name LIKE '%on' XOR users.age > 3)

[View source]
def xor(conditions : Array) #

Combines given array of conditions by XOR operator.

All given conditions will be wrapped in Grouping.

User.all.where { xor([_name.like("%on"), _age > 3]) }
# WHERE (users.name LIKE '%on' XOR users.age > 3)

[View source]

Macro Detail

macro method_missing(call) #

[View source]