| Class | WhereBuilder |
| In: |
lib/where_builder.rb
|
| Parent: | Object |
| tables | [R] |
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"
# 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
Appends a text clause to the where SQL clause.
where { add_clause '(any string)' }.to_sql #=> " where (any string)"
# 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"
# File lib/where_builder.rb, line 21
21: def equal(lval, rval)
22: add_condition(lval, "=", rval)
23: end
Appends a greater than condition to the where SQL clause.
where { greater_than :column1, 10 }.to_sql #=> " where column1 > 10"
# File lib/where_builder.rb, line 57
57: def greater_than(lval, rval)
58: add_condition(lval, ">", rval)
59: end
Appends an in condition to the where SQL clause.
where { is_in :column1, [10, 20] }.to_sql #=> " where column1 in (10, 20)"
# 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)"
# 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"
# 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"
# File lib/where_builder.rb, line 39
39: def less_than(lval, rval)
40: add_condition(lval, "<", rval)
41: 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'"
# File lib/where_builder.rb, line 132
132: def to_sql
133: " where #{sql_parts.join(' and ')}"
134: end