class Bones::SQL::SQL

Included Modules

Defined in:

bones/api/sql.cr

Instance Method Summary

Instance methods inherited from module Bones::SQL::QueryJoins

inner_join(from_table = @from_table, to_table = TableDef.new, on = Column.new) : SQL inner_join, join_tables : Array(Joins::Join) join_tables, join_tables=(join_tables : Array(Joins::Join)) join_tables=, left_join(from_table = @from_table, to_table = TableDef.new, on = Column.new) : SQL left_join, outer_join(from_table = @from_table, to_table = TableDef.new, on = Column.new) : SQL outer_join, right_join(from_table = @from_table, to_table = TableDef.new, on = Column.new) : SQL right_join

Instance methods inherited from module Bones::SQL::AggregateFunctions

avg(column = Column.new) : AggregateFunction avg, count(column = Column.new) : AggregateFunction count, count_all(table : TableDef | Nil = nil) : AggregateFunction count_all, sum(column = Column.new) : AggregateFunction sum

Instance Method Detail

def from(table : TableDef) : SQL #

From SQL clause, with a TableDef argument.

sql.select(my_column, sql.sum(my_another_column)).from(my_table)

[View source]
def from_table : TableDef #

[View source]
def from_table=(from_table : TableDef) #

[View source]
def group_by : GroupBy #

[View source]
def group_by(*columns) : SQL #

Group By SQL clause, you can add a series of columns. It checks all columns in select clause are added here or in an aggregate function.

sql.select(my_column).from(my_table).group_by(my_column)

[View source]
def group_by=(group_by : GroupBy) #

[View source]
def having(aggregate_function = AggregateFunction.new) : SQL #

Having SQL clause, allow only one aggregate function argument, the rest can be concatenated.

sql.select(my_column).from(my_table).having(sql.count(my_column).gt(1).and(sql.avg(my_another_column).gt(10)))

[View source]
def having=(having : Having | Nil) #

[View source]
def limit(value : Int32) : SQL #

Limit SQL clause, accept only Int32 values.

sql.select(my_column).from(my_table).limit(100)

[View source]
def limit : Limit | Nil #

[View source]
def limit=(limit : Limit | Nil) #

[View source]
def offset(value : Int32) : SQL #

Offset SQL clause, accept only Int32 values.

sql.select(my_column).from(my_table).limit(100).offset(1)

[View source]
def offset : Offset | Nil #

[View source]
def offset=(offset : Offset | Nil) #

[View source]
def order_by : OrderBy #

[View source]
def order_by(*columns) : SQL #

Order By SQL clause, you can add a series of columns.

sql.select(my_column).from(my_table).order_by(my_column.asc)

[View source]
def order_by=(order_by : OrderBy) #

[View source]
def select(*columns) : SQL #

Starts the query builder, it's the select clause with a series of columns arguments.

class MyColumn < Bones::Column
  column name : String
end

class MyAnotherColumn < Bones::Column
  column name : Int32
end

class MyTable < Bones::TableDef
end

my_table = MyTable.new
my_column = MyColumn.new(my_table)

sql = Bones::SQL::SQL.new
sql.select(my_column, sql.sum(my_another_column))

[View source]
def select_fields : Select #

[View source]
def select_fields=(select_fields : Select) #

[View source]
def to_sql_string : String #

Returns the SQL query in a String.

sql.select(my_column).from(my_table).where(my_column.eq("Peter").and(my_another_column.gt(10))).to_sql_string
# => SELECT my_table.my_column FROM my_table WHERE my_table.my_column = 'Peter' AND my_table.my_another_column > 10

[View source]
def where(column = Column.new) : SQL #

Where SQL clause, allow only one column argument, the rest can be concatenated.

sql.select(my_column).from(my_table).where(my_column.eq("Peter").and(my_another_column.gt(10)))

[View source]
def where=(where : Where | Nil) #

[View source]