class DBX::QueryBuilder

Overview

Query builder.

Direct Known Subclasses

Defined in:

query_builder/builder.cr
query_builder/builder_criteria.cr
query_builder/builder_support.cr

Constant Summary

QUOTE = '"'

Quoting character.

Constructors

Instance Method Summary

Constructor Detail

def self.new #

Creates a new QueryBuilder.


[View source]

Instance Method Detail

def add_arg(value) : String #

Adds value to args and returns the placeholder.


[View source]
def add_args_and_fields_from_data(data : NamedTuple | Hash, sep = ", ") : String #

Extracts arguments and fields from data, populates args and returns SQL part for a listing statement. Example: field1, field2, field3


[View source]
def add_args_and_fields_from_data(data : Array, sep = ", ") : String #

Extracts arguments and fields from data, populates args and returns SQL part for a listing statement. Example: field1, field2, field3


[View source]
def add_args_and_kv_from_data(data : NamedTuple | Hash, sep = ", ") : String #

Extracts arguments and fields from data, populates args and returns SQL part for a combined statement. Example: field1 = $1, field2 = $2, field3 = $3


[View source]
def alter(command : String, field : String, data_type = "") : QueryBuilder #

[View source]
def alter(table : OneOrMoreFieldsType, command : String, field : String, data_type = "") : QueryBuilder #

[View source]
def analyze(table : OneOrMoreFieldsType) : QueryBuilder #

Builds the ANALYZE query


[View source]
def analyze : QueryBuilder #

Builds the ANALYZE query


[View source]
def avg(field : FieldType, name = nil) : QueryBuilder #

Adds AVG to the current query.


[View source]
def between(field : FieldType, value1, value2, type = "", and_or = "AND") : QueryBuilder #

[View source]
def build : SQLandArgsType #

Builds the current query and returns SQL (string) and arguments (array).

sql, args = builder.build

[View source]
def check(table : OneOrMoreFieldsType) : QueryBuilder #

Builds the CHECK query


[View source]
def check : QueryBuilder #

Builds the CHECK query


[View source]
def checksum(table : OneOrMoreFieldsType) : QueryBuilder #

Builds the CHECKSUM query


[View source]
def checksum : QueryBuilder #

Builds the CHECKSUM query


[View source]
def count(field : FieldType, name = nil) : QueryBuilder #

Adds COUNT to the current query.


[View source]
def delete(pk_name, pk_value) : QueryBuilder #

[View source]
def delete(table : OneOrMoreFieldsType) : QueryBuilder #

[View source]
def delete : QueryBuilder #

[View source]
def drop(table : OneOrMoreFieldsType, check_exists = true) : QueryBuilder #

[View source]
def drop(check_exists : Bool | NamedTuple(command: String, field: String, data_type: String) | Nil = true) : QueryBuilder #

[View source]
def find(pk_name, pk_value) : QueryBuilder #

Finds one resource by its primary key.

Same as:

builder.find.where(pk_name, pk_value)

[View source]
def find(table_name : OneOrMoreFieldsType) : QueryBuilder #

[View source]
def find : QueryBuilder #

[View source]
def find(&) : QueryBuilder #

Adds #find to current query and defines in raw form the SQL statement of the table(s).

Be careful, you have to manage arguments (arg) and quotes (q).

Example:

builder.find { "#{q("posts") AS p, articles a" }"

Generates:

SELECT * FROM "posts" AS p, articles a

[View source]
def full_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder #

Adds FULL JOIN to the current query.


[View source]
def full_outer_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder #

Adds FULL OUTER JOIN to the current query.


[View source]
def group_by(field : OneOrMoreFieldsType) : QueryBuilder #

[View source]
def having(field : FieldType, op_or_val, value = nil) : QueryBuilder #

[View source]
def having(&) : QueryBuilder #

Defines in raw form the SQL statement of HAVING.

Be careful, you have to manage arguments (arg) and quotes (q).

Example:

builder.find(:tests).group_by(:payment).having { "SUM(price) > 40" }

Generates:

SELECT * FROM tests GROUP_BY payment HAVING SUM(person) > 40

[View source]
def in(field : FieldType, values : Array | Tuple, type = "", and_or = "AND") : QueryBuilder #

[View source]
def inner_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder #

Adds INNER JOIN to the current query.


[View source]
def insert(table : OneOrMoreFieldsType, data : Hash | NamedTuple) : QueryBuilder #

[View source]
def insert(data : Hash | NamedTuple) : QueryBuilder #

[View source]
def join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil, type = "") : QueryBuilder #

[View source]
def join(&) : QueryBuilder #

Adds a raw #join to current query.

Be careful, you have to manage arguments (arg) and quotes (q).

Example:

builder.find("tests").join { "
  INNER JOIN T2 ON T1.a = T2.a
  AND T1.b = T2.b
  OR T1.b = #{q(some_value_to_quote)}
" }
  .join { "LEFT JOIN payments p USING (product_id)" }

[View source]
def join : String #

Returns jointure. Returns empty string if no jointure has been defined.

puts builder.join unless builder.join.blank? # or .empty?

[View source]
def last_query : String #

Returns the last query.


[View source]
def last_query_method : Symbol | Nil #

Returns the last query method.


[View source]
def left_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder #

Adds LEFT JOIN to the current query.


[View source]
def left_outer_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder #

Adds LEFT OUTER JOIN to the current query.


[View source]
def like(field : FieldType, value, type = "", and_or = "AND") : QueryBuilder #

[View source]
def limit(limit, limit_end = nil) : QueryBuilder #

[View source]
def limit : String #

Returns #limit value. Returns empty string if no limit has been defined.

puts builder.limit unless builder.limit.blank? # or .empty?

[View source]
def max(field : FieldType, name = nil) : QueryBuilder #

Adds MAX to the current query.


[View source]
def min(field : FieldType, name = nil) : QueryBuilder #

Adds MIN to the current query.


[View source]
def not_between(field : FieldType, value1, value2) : QueryBuilder #

[View source]
def not_in(field : FieldType, values : Array | Tuple) : QueryBuilder #

[View source]
def not_like(field : FieldType, value) : QueryBuilder #

[View source]
def not_where(field : FieldType, op_or_val, value = nil) : QueryBuilder #

[View source]
def offset(offset) : QueryBuilder #

[View source]
def offset : String #

Returns #offset value. Returns empty string if no offset has been defined.

puts builder.offset unless builder.offset.blank? # or .empty?

[View source]
def optimize(table : OneOrMoreFieldsType) : QueryBuilder #

Builds the OPTIMIZE query


[View source]
def optimize : QueryBuilder #

Builds the OPTIMIZE query


[View source]
def or_between(field : FieldType, value1, value2) : QueryBuilder #

[View source]
def or_in(field : FieldType, values : Array | Tuple) : QueryBuilder #

[View source]
def or_like(field : FieldType, value) : QueryBuilder #

[View source]
def or_not_between(field : FieldType, value1, value2) : QueryBuilder #

[View source]
def or_not_in(field : FieldType, values : Array | Tuple) : QueryBuilder #

[View source]
def or_not_like(field : FieldType, value) : QueryBuilder #

[View source]
def or_not_where(field : FieldType, op_or_val, value = nil) : QueryBuilder #

[View source]
def or_where(field : FieldType, op_or_val, value = nil) : QueryBuilder #

[View source]
def order_by(field : FieldType, dir = nil) : QueryBuilder #

[View source]
def paginate(per_page, page) : QueryBuilder #

Sets #offset and #limit to get pagination-compatible results.


[View source]
def ph(position : Int) : String #

Adds placeholder for a SQL argument.


[View source]
def query(&) : QueryBuilder #

Generates a raw query.

Be careful, you have to manage arguments and quotes.

Example:

puts builder.query { "
  SELECT * FROM tests
  status = #{arg(true)}
  AND (
    #{q(:date)} <= #{arg(Time.utc - 1.day)}
    OR role = #{arg(:admin)}
  )
  LIMIT 1
" }

Generates:

SELECT *
FROM tests
WHERE status = $1
AND ("date" <= $2 OR role = $3)
LIMIT 1

[View source]
def query_count : Int #

Returns number of queries made by the current instance.


[View source]
def query_method : Symbol | Nil #

Returns the query method.


[View source]
def quote(field : FieldType) : String #

Wraps field with quotes (QUOTE).


[View source]
def repair(table : OneOrMoreFieldsType) : QueryBuilder #

Builds the REPAIR query


[View source]
def repair : QueryBuilder #

Builds the REPAIR query


[View source]
def reset_query #

Resets current query.


[View source]
def returning(fields : OneOrMoreFieldsType) : QueryBuilder #

SQL field(s) to be returned after an #insert statement.

* (wildcard) means all fields.


[View source]
def returning : String | Nil #

Returns the SQL field(s) to be returned after an #insert statement.

* (wildcard) means all fields.


[View source]
def returning(*name : FieldType) : QueryBuilder #

SQL field(s) to be returned after an #insert statement.

* (wildcard) means all fields.


[View source]
def right_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder #

Adds RIGHT JOIN to the current query.


[View source]
def right_outer_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder #

Adds RIGHT OUTER JOIN to the current query.


[View source]
def select(fields : OneOrMoreFieldsType) : QueryBuilder #

Selects one or more fields.


[View source]
def select : String #

Returns selected field(s). Default is *.

puts builder.select

[View source]
def select(*name : FieldType) : QueryBuilder #

Selects one or more fields.


[View source]
def sum(field : FieldType, name = nil) : QueryBuilder #

Adds SUM to the current query.


[View source]
def table(name : OneOrMoreFieldsType) : QueryBuilder #

Targets one or more tables.


[View source]
def table : String #

Returns table name(s). Returns empty string if no table has been defined.

puts builder.table unless builder.table.blank? # or .empty?

[View source]
def table(*name : FieldType) : QueryBuilder #

Targets tables defined by variadic arguments.

builder.table(:table1, :table2)

[View source]
def table(&) : QueryBuilder #

Defines in raw form the SQL statement of the table(s).

Be careful, you have to manage arguments (arg) and quotes (q).

Example:

builder.find { "#{q("posts") AS p, articles a" }"

Generates:

SELECT * FROM "posts" AS p, articles a

[View source]
def to_data_h(data : Hash | NamedTuple) : DataHashType #

Converts NamedTuple to DataHashType.


[View source]
def update(pk_name, pk_value, data : Hash | NamedTuple) : QueryBuilder #

[View source]
def update(table : OneOrMoreFieldsType, data : Hash | NamedTuple) : QueryBuilder #

[View source]
def update(data : Hash | NamedTuple) : QueryBuilder #

[View source]
def where(field : FieldType, op_or_val, value = nil, type = "", and_or = "AND") : QueryBuilder #

Where clause.


[View source]
def where(&) : QueryBuilder #

Adds a raw #where to current query.

Be careful, you have to manage arguments (arg) and quotes (q).

Example:

builder.find("tests").where { "
  status = #{arg(true)}
  AND (
    #{q(:date)} <= #{arg(Time.utc - 1.day)}
    OR role = #{arg(:admin)}
  )
" }

Generates:

SELECT *
FROM tests
WHERE status = $1
AND ("date" <= $2 OR role = $3)

[View source]