| Class | Select |
| In: |
lib/select.rb
|
| Parent: | SqlStatement |
Returns a Select instance with the table names, joined by ’, ’ appended to the SQL statement.
Select[1, :column1, 'book'].from[:table1, :table2].to_sql #=> "select 1, column1, 'book' from table1, table2"
# File lib/select.rb, line 47
47: def [](*table_names)
48: @tables = []
49: @to_sql += table_names.inject([]) do |result, element|
50: if element.to_s =~ / as /
51: @tables << element.to_s.split(/ as /).last.to_sym
52: result << element.to_s.gsub(/ as /, " ").to_sym
53: else
54: @tables << element
55: result << element
56: end
57: end.to_sql
58: self
59: end
Returns a JoinBuilder instance.
Select.all.from[:table1].inner_join
#=> #<JoinBuilder:0x654f4 @select_builder=#<Select:0x65968 @tables=[:table1], @to_sql="select * from table1">>
# File lib/select.rb, line 77
77: def inner_join
78: JoinBuilder.new(self, "inner")
79: end
Returns a JoinBuilder instance.
Select.all.from[:table1].left_join
#=> #<JoinBuilder:0x654f4 @select_builder=#<Select:0x65968 @tables=[:table1], @to_sql="select * from table1">>
# File lib/select.rb, line 87
87: def left_join
88: JoinBuilder.new(self, "left")
89: end
Creates a new OnWhereBuilder instance, passing the block as a parameter, then executes to_sql on the OnWhereBuilder instance. The resulting string from the OnWhereBuilder instance is appended to the SQL statement. Returns self.
Select.all.from[:table1].inner_join[:table2].on { equal :table1.column1, :table2.column1 }.to_sql
#=> "select * from table1 inner join table2 on table1.column1 = table2.column2"
# File lib/select.rb, line 124
124: def on(&block)
125: @to_sql += OnWhereBuilder.new(self.tables, &block).to_sql
126: self
127: end
Returns a Select instance with the order arguments, joined by ’, ’ appended to the SQL statement.
Select[1].from[:table1].order_by(:column1, :column2).to_sql #=> "select 1 from table1 order by column1, column2"
# File lib/select.rb, line 66
66: def order_by(*column)
67: @to_sql << " order by #{column.to_sql}"
68: self
69: end