Class: Select
- Inherits:
-
SqlStatement
- Object
- SqlStatement
- Select
- Defined in:
- lib/select.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from SqlStatement
Class Method Summary collapse
-
.[](*columns) ⇒ Object
call-seq: Select -> a_select.
-
.all ⇒ Object
call-seq: Select.all -> a_select.
-
.distinct ⇒ Object
call-seq: Select.distinct -> a_select.
Instance Method Summary collapse
-
#[](*table_names) ⇒ Object
call-seq: select -> a_select.
-
#from ⇒ Object
call-seq: select.from -> a_select.
-
#inner_join ⇒ Object
call-seq: select.inner_join -> a_join_builder.
-
#join_table(join_type, table_names) ⇒ Object
:nodoc:.
-
#left_join ⇒ Object
call-seq: select.left_join -> a_join_builder.
-
#on(&block) ⇒ Object
call-seq: sql_statement.on { block } -> a_sql_statement.
-
#order_by(*column) ⇒ Object
call-seq: select.order_by -> a_select.
-
#right_join ⇒ Object
call-seq: select.right_join -> a_join_builder.
Methods inherited from SqlStatement
#and, #and_with_or_conditions, #initialize, #or, #where
Constructor Details
This class inherits a constructor from SqlStatement
Class Method Details
.[](*columns) ⇒ Object
8 9 10 |
# File 'lib/select.rb', line 8 def [](*columns) self.new("select #{columns.to_sql}") end |
.all ⇒ Object
call-seq: Select.all -> a_select
Returns a Select instance with the SQL initialized to ‘select *’
Select.all.to_sql #=> "select *"
26 27 28 |
# File 'lib/select.rb', line 26 def all self.new("select *") end |
.distinct ⇒ Object
call-seq: Select.distinct -> a_select
Returns a Select class that appends ‘distinct’ to the select clause
Select.distinct[1, :column1, 'book'].to_sql #=> "select distinct 1, column1, 'book'"
17 18 19 |
# File 'lib/select.rb', line 17 def distinct DistinctSelect end |
Instance Method Details
#[](*table_names) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/select.rb', line 47 def [](*table_names) @tables = [] @to_sql += table_names.inject([]) do |result, element| if element.to_s =~ / as / @tables << element.to_s.split(/ as /).last.to_sym result << element.to_s.gsub(/ as /, " ").to_sym else @tables << element result << element end end.to_sql self end |
#from ⇒ Object
call-seq: select.from -> a_select
Returns a Select instance with ‘ from ’ appended to the SQL statement.
Select[1, :column1, 'book'].from.to_sql #=> "select 1, column1, 'book' from "
37 38 39 40 |
# File 'lib/select.rb', line 37 def from @to_sql += " from " self end |
#inner_join ⇒ Object
77 78 79 |
# File 'lib/select.rb', line 77 def inner_join JoinBuilder.new(self, "inner") end |
#join_table(join_type, table_names) ⇒ Object
:nodoc:
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/select.rb', line 101 def join_table(join_type, table_names) #:nodoc: @to_sql << " #{join_type} join " table_names.flatten! @to_sql += table_names.inject([]) do |result, element| if element.to_s =~ / as / @tables << element.to_s.split(/ as /).last.to_sym result << element.to_s.gsub(/ as /, " ").to_sym else @tables << element result << element end end.to_sql self end |
#left_join ⇒ Object
87 88 89 |
# File 'lib/select.rb', line 87 def left_join JoinBuilder.new(self, "left") end |
#on(&block) ⇒ Object
call-seq: sql_statement.on { block } -> a_sql_statement
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"
124 125 126 127 |
# File 'lib/select.rb', line 124 def on(&block) @to_sql += OnWhereBuilder.new(self.tables, &block).to_sql self end |
#order_by(*column) ⇒ Object
call-seq: select.order_by -> a_select
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"
66 67 68 69 |
# File 'lib/select.rb', line 66 def order_by(*column) @to_sql << " order by #{column.to_sql}" self end |
#right_join ⇒ Object
97 98 99 |
# File 'lib/select.rb', line 97 def right_join JoinBuilder.new(self, "right") end |