Class: WhereBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/where_builder.rb

Direct Known Subclasses

AndWhereBuilder, OnWhereBuilder, OrWhereBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tables, &block) ⇒ WhereBuilder

call-seq: WhereBuilder.new(tables, &block) -> a_where_builder

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"

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'lib/where_builder.rb', line 10

def initialize(tables, &block)
  raise ArgumentError.new("no block given to where, check for parenthesis usage") unless block_given?
  @tables = tables.concat(eval("respond_to?(:tables) ? tables : []", block.binding))
  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object (protected)

:nodoc:



158
159
160
161
# File 'lib/where_builder.rb', line 158

def method_missing(sym, *args) #:nodoc:
  super unless args.empty?
  ReceiveAny.new(sym, self)
end

Instance Attribute Details

#tablesObject (readonly)

Returns the value of attribute tables.



2
3
4
# File 'lib/where_builder.rb', line 2

def tables
  @tables
end

Instance Method Details

#add_clause(arg) ⇒ Object

call-seq: where.add_clause(clause)

Appends a text clause to the where SQL clause.

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


141
142
143
# File 'lib/where_builder.rb', line 141

def add_clause(arg)
  sql_parts << arg
end

#add_condition(lval, operator, rval) ⇒ Object

:nodoc:



145
146
147
# File 'lib/where_builder.rb', line 145

def add_condition(lval, operator, rval) #:nodoc:
  sql_parts << "#{lval.to_sql} #{operator} #{rval.to_sql}"
end

#add_parenthesis_condition(lval, operator, rval) ⇒ Object

:nodoc:



149
150
151
# File 'lib/where_builder.rb', line 149

def add_parenthesis_condition(lval, operator, rval) #:nodoc:
  sql_parts << "#{lval.to_sql} #{operator} (#{rval.to_sql})"
end

#equal(lval, rval) ⇒ Object

call-seq: where.equal(arg1, arg2)

Appends an equality condition to the where SQL clause.

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


21
22
23
# File 'lib/where_builder.rb', line 21

def equal(lval, rval)
  add_condition(lval, "=", rval)
end

#exists(clause) ⇒ Object

call-seq: where.exists(clause)

Appends an exists condition to the where SQL clause.

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


111
112
113
# File 'lib/where_builder.rb', line 111

def exists(clause)
  sql_parts << "exists (#{clause.to_sql})"
end

#greater_than(lval, rval) ⇒ Object

call-seq: where.greater_than(arg1, arg2)

Appends a greater than condition to the where SQL clause.

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


57
58
59
# File 'lib/where_builder.rb', line 57

def greater_than(lval, rval)
  add_condition(lval, ">", rval)
end

#greater_than_or_equal(lval, rval) ⇒ Object

call-seq: where.greater_than_or_equal(arg1, arg2)

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

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


66
67
68
# File 'lib/where_builder.rb', line 66

def greater_than_or_equal(lval, rval)
  add_condition(lval, ">=", rval)
end

#is_in(lval, rval) ⇒ Object

call-seq: where.is_in(arg1, arg2)

Appends an in condition to the where SQL clause.

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


75
76
77
# File 'lib/where_builder.rb', line 75

def is_in(lval, rval)
  add_parenthesis_condition(lval, "in", rval)
end

#is_not_in(lval, rval) ⇒ Object

call-seq: where.is_not_in(arg1, arg2)

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


84
85
86
# File 'lib/where_builder.rb', line 84

def is_not_in(lval, rval)
  add_parenthesis_condition(lval, "not in", rval)
end

#is_not_null(column) ⇒ Object

call-seq: where.not_null(arg1)

Appends a not null condition to the where SQL clause.

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


93
94
95
# File 'lib/where_builder.rb', line 93

def is_not_null(column)
  sql_parts << "#{column.to_sql} is not null"
end

#less_than(lval, rval) ⇒ Object

call-seq: where.less_than(arg1, arg2)

Appends a less than condition to the where SQL clause.

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


39
40
41
# File 'lib/where_builder.rb', line 39

def less_than(lval, rval)
  add_condition(lval, "<", rval)
end

#less_than_or_equal(lval, rval) ⇒ Object

call-seq: where.less_than_or_equal(arg1, arg2)

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

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


48
49
50
# File 'lib/where_builder.rb', line 48

def less_than_or_equal(lval, rval)
  add_condition(lval, "<=", rval)
end

#like(lval, rval) ⇒ Object

call-seq: where.like(arg1, arg2)

Appends a like condition to the where SQL clause.

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


102
103
104
# File 'lib/where_builder.rb', line 102

def like(lval, rval)
  add_condition(lval, "like", rval)
end

#not_equal(lval, rval) ⇒ Object

call-seq: where.not_equal(arg1, arg2)

Appends a not equal condition to the where SQL clause.

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


30
31
32
# File 'lib/where_builder.rb', line 30

def not_equal(lval, rval)
  add_condition(lval, "<>", rval)
end

#not_exists(clause) ⇒ Object

call-seq: where.not_exists(clause)

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


120
121
122
# File 'lib/where_builder.rb', line 120

def not_exists(clause)
  sql_parts << "not exists (#{clause.to_sql})"
end

#to_sqlObject

call-seq: where.to_sql -> a_string

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


132
133
134
# File 'lib/where_builder.rb', line 132

def to_sql
  " where #{sql_parts.join(' and ')}"
end