Class WhereBuilder
In: lib/where_builder.rb
Parent: Object

Methods

Attributes

tables  [R] 

Public Class methods

Returns a new WhereBuilder. At initialization time the block is instance evaled on the new WhereBuilder instance.

   WhereBuilder.new [:table1] { equal :column1, 10 }.to_sql       #=> " where column1 = 10"

[Source]

    # File lib/where_builder.rb, line 10
10:   def initialize(tables, &block)
11:     raise ArgumentError.new("no block given to where, check for parenthesis usage") unless block_given?
12:     @tables = tables.concat(eval("respond_to?(:tables) ? tables : []", block.binding))
13:     instance_eval(&block)
14:   end

Public Instance methods

Appends a text clause to the where SQL clause.

   where { add_clause '(any string)' }.to_sql       #=> " where (any string)"

[Source]

     # File lib/where_builder.rb, line 141
141:   def add_clause(arg)
142:     sql_parts << arg
143:   end

Appends an equality condition to the where SQL clause.

   where { equal :column1, 10 }.to_sql       #=> " where column1 = 10"

[Source]

    # File lib/where_builder.rb, line 21
21:   def equal(lval, rval)
22:     add_condition(lval, "=", rval)
23:   end

Appends an exists condition to the where SQL clause.

   where { exists 'select id from table1' }.to_sql       #=> " where exists (select id from table1)"

[Source]

     # File lib/where_builder.rb, line 111
111:   def exists(clause)
112:     sql_parts << "exists (#{clause.to_sql})"
113:   end

Appends a greater than condition to the where SQL clause.

   where { greater_than :column1, 10 }.to_sql       #=> " where column1 > 10"

[Source]

    # File lib/where_builder.rb, line 57
57:   def greater_than(lval, rval)
58:     add_condition(lval, ">", rval)
59:   end

Appends a greater than or equal condition to the where SQL clause.

   where { greater_than_or_equal :column1, 10 }.to_sql       #=> " where column1 >= 10"

[Source]

    # File lib/where_builder.rb, line 66
66:   def greater_than_or_equal(lval, rval)
67:     add_condition(lval, ">=", rval)
68:   end

Appends an in condition to the where SQL clause.

   where { is_in :column1, [10, 20] }.to_sql       #=> " where column1 in (10, 20)"

[Source]

    # File lib/where_builder.rb, line 75
75:   def is_in(lval, rval)
76:     add_parenthesis_condition(lval, "in", rval)
77:   end

Appends a not in condition to the where SQL clause.

   where { is_not_in :column1, [10, 20] }.to_sql       #=> " where column1 not in (10, 20)"

[Source]

    # File lib/where_builder.rb, line 84
84:   def is_not_in(lval, rval)
85:     add_parenthesis_condition(lval, "not in", rval)
86:   end

Appends a not null condition to the where SQL clause.

   where { is_not_null :column1 }.to_sql       #=> " where column1 is not null"

[Source]

    # File lib/where_builder.rb, line 93
93:   def is_not_null(column)
94:     sql_parts << "#{column.to_sql} is not null"
95:   end

Appends a less than condition to the where SQL clause.

   where { less_than :column1, 10 }.to_sql       #=> " where column1 < 10"

[Source]

    # File lib/where_builder.rb, line 39
39:   def less_than(lval, rval)
40:     add_condition(lval, "<", rval)
41:   end

Appends a less than or equal condition to the where SQL clause.

   where { less_than_or_equal :column1, 10 }.to_sql       #=> " where column1 <= 10"

[Source]

    # File lib/where_builder.rb, line 48
48:   def less_than_or_equal(lval, rval)
49:     add_condition(lval, "<=", rval)
50:   end

Appends a like condition to the where SQL clause.

   where { like :column1, 'any' }.to_sql       #=> " where column1 like 'any'"

[Source]

     # File lib/where_builder.rb, line 102
102:   def like(lval, rval)
103:     add_condition(lval, "like", rval)
104:   end

Appends a not equal condition to the where SQL clause.

   where { not_equal :column1, 10 }.to_sql       #=> " where column1 <> 10"

[Source]

    # File lib/where_builder.rb, line 30
30:   def not_equal(lval, rval)
31:     add_condition(lval, "<>", rval)
32:   end

Appends an exists condition to the where SQL clause.

   where { not_exists 'select id from table1' }.to_sql       #=> " where not exists (select id from table1)"

[Source]

     # File lib/where_builder.rb, line 120
120:   def not_exists(clause)
121:     sql_parts << "not exists (#{clause.to_sql})"
122:   end

Returns a string by collecting all the conditions and joins them with ’ and ’.

   WhereBuilder.new [] do
     equal :column1, 10
     equal :column2, 'book'
   end.to_sql         #=> " where column1 = 10 and column2 = 'book'"

[Source]

     # File lib/where_builder.rb, line 132
132:   def to_sql
133:     " where #{sql_parts.join(' and ')}"
134:   end

[Validate]