class DBX::QueryBuilder
- DBX::QueryBuilder
- Reference
- Object
Overview
Query builder.
Direct Known Subclasses
Defined in:
query_builder/builder.crquery_builder/builder_criteria.cr
query_builder/builder_support.cr
Constant Summary
-
QUOTE =
'"'
-
Quoting character.
Constructors
-
.new
Creates a new
QueryBuilder
.
Instance Method Summary
-
#add_arg(value) : String
Adds value to args and returns the
placeholder
. -
#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. -
#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. -
#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. - #alter(command : String, field : String, data_type = "") : QueryBuilder
- #alter(table : OneOrMoreFieldsType, command : String, field : String, data_type = "") : QueryBuilder
-
#analyze(table : OneOrMoreFieldsType) : QueryBuilder
Builds the
ANALYZE
query -
#analyze : QueryBuilder
Builds the
ANALYZE
query -
#avg(field : FieldType, name = nil) : QueryBuilder
Adds
AVG
to the current query. - #between(field : FieldType, value1, value2, type = "", and_or = "AND") : QueryBuilder
-
#build : SQLandArgsType
Builds the current query and returns SQL (string) and arguments (array).
-
#check(table : OneOrMoreFieldsType) : QueryBuilder
Builds the
CHECK
query -
#check : QueryBuilder
Builds the
CHECK
query -
#checksum(table : OneOrMoreFieldsType) : QueryBuilder
Builds the
CHECKSUM
query -
#checksum : QueryBuilder
Builds the
CHECKSUM
query -
#count(field : FieldType, name = nil) : QueryBuilder
Adds
COUNT
to the current query. - #delete(pk_name, pk_value) : QueryBuilder
- #delete(table : OneOrMoreFieldsType) : QueryBuilder
- #delete : QueryBuilder
- #drop(table : OneOrMoreFieldsType, check_exists = true) : QueryBuilder
- #drop(check_exists : Bool | NamedTuple(command: String, field: String, data_type: String) | Nil = true) : QueryBuilder
-
#find(pk_name, pk_value) : QueryBuilder
Finds one resource by its primary key.
- #find(table_name : OneOrMoreFieldsType) : QueryBuilder
- #find : QueryBuilder
-
#find(&) : QueryBuilder
Adds
#find
to current query and defines in raw form the SQL statement of the table(s). -
#full_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder
Adds
FULL JOIN
to the current query. -
#full_outer_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder
Adds
FULL OUTER JOIN
to the current query. - #group_by(field : OneOrMoreFieldsType) : QueryBuilder
- #having(field : FieldType, op_or_val, value = nil) : QueryBuilder
-
#having(&) : QueryBuilder
Defines in raw form the SQL statement of
HAVING
. - #in(field : FieldType, values : Array | Tuple, type = "", and_or = "AND") : QueryBuilder
-
#inner_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder
Adds
INNER JOIN
to the current query. - #insert(table : OneOrMoreFieldsType, data : Hash | NamedTuple) : QueryBuilder
- #insert(data : Hash | NamedTuple) : QueryBuilder
- #join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil, type = "") : QueryBuilder
-
#join(&) : QueryBuilder
Adds a raw
#join
to current query. -
#join : String
Returns jointure.
-
#last_query : String
Returns the last query.
-
#last_query_method : Symbol | Nil
Returns the last query method.
-
#left_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder
Adds
LEFT JOIN
to the current query. -
#left_outer_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder
Adds
LEFT OUTER JOIN
to the current query. - #like(field : FieldType, value, type = "", and_or = "AND") : QueryBuilder
- #limit(limit, limit_end = nil) : QueryBuilder
-
#limit : String
Returns
#limit
value. -
#max(field : FieldType, name = nil) : QueryBuilder
Adds
MAX
to the current query. -
#min(field : FieldType, name = nil) : QueryBuilder
Adds
MIN
to the current query. - #not_between(field : FieldType, value1, value2) : QueryBuilder
- #not_in(field : FieldType, values : Array | Tuple) : QueryBuilder
- #not_like(field : FieldType, value) : QueryBuilder
- #not_where(field : FieldType, op_or_val, value = nil) : QueryBuilder
- #offset(offset) : QueryBuilder
-
#offset : String
Returns
#offset
value. -
#optimize(table : OneOrMoreFieldsType) : QueryBuilder
Builds the
OPTIMIZE
query -
#optimize : QueryBuilder
Builds the
OPTIMIZE
query - #or_between(field : FieldType, value1, value2) : QueryBuilder
- #or_in(field : FieldType, values : Array | Tuple) : QueryBuilder
- #or_like(field : FieldType, value) : QueryBuilder
- #or_not_between(field : FieldType, value1, value2) : QueryBuilder
- #or_not_in(field : FieldType, values : Array | Tuple) : QueryBuilder
- #or_not_like(field : FieldType, value) : QueryBuilder
- #or_not_where(field : FieldType, op_or_val, value = nil) : QueryBuilder
- #or_where(field : FieldType, op_or_val, value = nil) : QueryBuilder
- #order_by(field : FieldType, dir = nil) : QueryBuilder
- #paginate(per_page, page) : QueryBuilder
-
#ph(position : Int) : String
Adds placeholder for a SQL argument.
-
#query(&) : QueryBuilder
Generates a raw query.
-
#query_count : Int
Returns number of queries made by the current instance.
-
#query_method : Symbol | Nil
Returns the query method.
-
#quote(field : FieldType) : String
Wraps field with quotes (
QUOTE
). -
#repair(table : OneOrMoreFieldsType) : QueryBuilder
Builds the
REPAIR
query -
#repair : QueryBuilder
Builds the
REPAIR
query -
#reset_query
Resets current query.
-
#returning(fields : OneOrMoreFieldsType) : QueryBuilder
SQL field(s) to be returned after an
#insert
statement. -
#returning : String | Nil
Returns the SQL field(s) to be returned after an
#insert
statement. -
#returning(*name : FieldType) : QueryBuilder
SQL field(s) to be returned after an
#insert
statement. -
#right_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder
Adds
RIGHT JOIN
to the current query. -
#right_outer_join(table : FieldType, field1 : FieldType, field2 : FieldType | Nil = nil) : QueryBuilder
Adds
RIGHT OUTER JOIN
to the current query. -
#select(fields : OneOrMoreFieldsType) : QueryBuilder
Selects one or more fields.
-
#select : String
Returns selected field(s).
-
#select(*name : FieldType) : QueryBuilder
Selects one or more fields.
-
#sum(field : FieldType, name = nil) : QueryBuilder
Adds
SUM
to the current query. -
#table(name : OneOrMoreFieldsType) : QueryBuilder
Targets one or more tables.
-
#table : String
Returns table name(s).
-
#table(*name : FieldType) : QueryBuilder
Targets tables defined by variadic arguments.
-
#table(&) : QueryBuilder
Defines in raw form the SQL statement of the table(s).
-
#to_data_h(data : Hash | NamedTuple) : DataHashType
Converts
NamedTuple
toDataHashType
. - #update(pk_name, pk_value, data : Hash | NamedTuple) : QueryBuilder
- #update(table : OneOrMoreFieldsType, data : Hash | NamedTuple) : QueryBuilder
- #update(data : Hash | NamedTuple) : QueryBuilder
-
#where(field : FieldType, op_or_val, value = nil, type = "", and_or = "AND") : QueryBuilder
Where clause.
-
#where(&) : QueryBuilder
Adds a raw
#where
to current query.
Constructor Detail
Instance Method Detail
Extracts arguments and fields from data, populates args
and returns SQL part for a listing statement.
Example: field1, field2, field3
Extracts arguments and fields from data, populates args
and returns SQL part for a listing statement.
Example: field1, field2, field3
Extracts arguments and fields from data, populates args
and returns SQL part for a combined statement.
Example: field1 = $1, field2 = $2, field3 = $3
Adds AVG
to the current query.
Builds the current query and returns SQL (string) and arguments (array).
sql, args = builder.build
Adds COUNT
to the current query.
Finds one resource by its primary key.
Same as:
builder.find.where(pk_name, pk_value)
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
Adds FULL JOIN
to the current query.
Adds FULL OUTER JOIN
to the current query.
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
Adds INNER JOIN
to the current query.
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)" }
Returns jointure. Returns empty string if no jointure has been defined.
puts builder.join unless builder.join.blank? # or .empty?
Adds LEFT JOIN
to the current query.
Adds LEFT OUTER JOIN
to the current query.
Returns #limit
value. Returns empty string if no limit has been defined.
puts builder.limit unless builder.limit.blank? # or .empty?
Adds MAX
to the current query.
Adds MIN
to the current query.
Returns #offset
value. Returns empty string if no offset has been defined.
puts builder.offset unless builder.offset.blank? # or .empty?
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
SQL field(s) to be returned after an #insert
statement.
*
(wildcard) means all fields.
Returns the SQL field(s) to be returned after an #insert
statement.
*
(wildcard) means all fields.
SQL field(s) to be returned after an #insert
statement.
*
(wildcard) means all fields.
Adds RIGHT JOIN
to the current query.
Adds RIGHT OUTER JOIN
to the current query.
Adds SUM
to the current query.
Returns table name(s). Returns empty string if no table has been defined.
puts builder.table unless builder.table.blank? # or .empty?
Targets tables defined by variadic arguments.
builder.table(:table1, :table2)
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
Where clause.
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)