Class Select
In: lib/select.rb
Parent: SqlStatement

Methods

[]   []   all   distinct   from   inner_join   left_join   on   order_by   right_join  

Public Class methods

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'"

[Source]

    # File lib/select.rb, line 8
 8:     def [](*columns)
 9:       self.new("select #{columns.to_sql}")
10:     end

Returns a Select instance with the SQL initialized to ‘select *’

   Select.all.to_sql       #=> "select *"

[Source]

    # File lib/select.rb, line 26
26:     def all
27:       self.new("select *")
28:     end

Returns a Select class that appends ‘distinct’ to the select clause

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

[Source]

    # File lib/select.rb, line 17
17:     def distinct
18:       DistinctSelect
19:     end

Public Instance methods

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"

[Source]

    # 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 Select instance with ’ from ’ appended to the SQL statement.

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

[Source]

    # File lib/select.rb, line 37
37:   def from
38:     @to_sql += " from "
39:     self
40:   end

Returns a JoinBuilder instance.

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

[Source]

    # 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">>

[Source]

    # 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"

[Source]

     # 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"

[Source]

    # File lib/select.rb, line 66
66:   def order_by(*column)
67:     @to_sql << " order by #{column.to_sql}"
68:     self
69:   end

Returns a JoinBuilder instance.

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

[Source]

    # File lib/select.rb, line 97
97:   def right_join
98:     JoinBuilder.new(self, "right")
99:   end

[Validate]