Class: Select

Inherits:
SqlStatement show all
Defined in:
lib/select.rb

Direct Known Subclasses

DistinctSelect

Instance Attribute Summary

Attributes inherited from SqlStatement

#tables, #to_sql

Class Method Summary collapse

Instance Method Summary collapse

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

call-seq: Select -> a_select

Returns a Select instance with the SQL initialized to ‘select ’ plus the args joined by ‘, ’

Select[1, :column1, 'book'].to_sql       #=> "select 1, column1, 'book'"


8
9
10
# File 'lib/select.rb', line 8

def [](*columns)
  self.new("select #{columns.to_sql}")
end

.allObject

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

.distinctObject

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

call-seq: select -> a_select

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"


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

#fromObject

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_joinObject

call-seq: select.inner_join -> a_join_builder

Returns a JoinBuilder instance.

Select.all.from[:table1].inner_join
  #=> #<JoinBuilder:0x654f4 @select_builder=#<Select:0x65968 @tables=[:table1], @to_sql="select * from table1">>


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_joinObject

call-seq: select.left_join -> a_join_builder

Returns a JoinBuilder instance.

Select.all.from[:table1].left_join
  #=> #<JoinBuilder:0x654f4 @select_builder=#<Select:0x65968 @tables=[:table1], @to_sql="select * from table1">>


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_joinObject

call-seq: select.right_join -> a_join_builder

Returns a JoinBuilder instance.

Select.all.from[:table1].right_join
  #=> #<JoinBuilder:0x654f4 @select_builder=#<Select:0x65968 @tables=[:table1], @to_sql="select * from table1">>


97
98
99
# File 'lib/select.rb', line 97

def right_join
  JoinBuilder.new(self, "right")
end