Class: SearchApi::SqlFragment
- Inherits:
-
Array
- Object
- Array
- SearchApi::SqlFragment
- Defined in:
- lib/search_api/sql_fragment.rb
Overview
Utility class that implements logic for sql fragments (such as [“escaped_sql = ?”, dirty_string])
Instance Attribute Summary collapse
-
#logical_operator ⇒ Object
Returns the value of attribute logical_operator.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(inSqlFragment) ⇒ Object
- #and(inSqlFragment) ⇒ Object
- #and!(inSqlFragment) ⇒ Object
-
#initialize(*args) ⇒ SqlFragment
constructor
A new instance of SqlFragment.
- #not ⇒ Object
- #or(inSqlFragment) ⇒ Object
- #or!(inSqlFragment) ⇒ Object
- #sanitize ⇒ Object
- #sqlParameters ⇒ Object
- #sqlString ⇒ Object
Constructor Details
#initialize(*args) ⇒ SqlFragment
Returns a new instance of SqlFragment.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/search_api/sql_fragment.rb', line 27 def initialize(*args) if args.length > 1 else args = args.first end # default operator is AND self.logical_operator = :and return if args.nil? or args.empty? if args.is_a? Array replace(args) else self.push(args) end end |
Instance Attribute Details
#logical_operator ⇒ Object
Returns the value of attribute logical_operator.
25 26 27 |
# File 'lib/search_api/sql_fragment.rb', line 25 def logical_operator @logical_operator end |
Class Method Details
.sanitize(*args) ⇒ Object
20 21 22 |
# File 'lib/search_api/sql_fragment.rb', line 20 def sanitize(*args) self.new(*args).sanitize end |
Instance Method Details
#<<(inSqlFragment) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/search_api/sql_fragment.rb', line 65 def <<(inSqlFragment) case logical_operator when :and!, :and and!(inSqlFragment) when :or!, :or or!(inSqlFragment) else raise "Unsupported logical_operator #{logical_operator.inspect}" end end |
#and(inSqlFragment) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/search_api/sql_fragment.rb', line 49 def and(inSqlFragment) inSqlFragment = SqlFragment.new(inSqlFragment) unless inSqlFragment.is_a?(SqlFragment) return self if inSqlFragment.empty? return inSqlFragment if empty? SqlFragment(["(#{sqlString}) AND (#{inSqlFragment[0]})"] + sqlParameters + inSqlFragment[1..-1]) end |
#and!(inSqlFragment) ⇒ Object
55 |
# File 'lib/search_api/sql_fragment.rb', line 55 def and!(inSqlFragment) replace(self.and(inSqlFragment)) end |
#not ⇒ Object
76 77 78 79 |
# File 'lib/search_api/sql_fragment.rb', line 76 def not return self if empty? SqlFragment(["NOT(#{sqlString})"]+sqlParameters) end |
#or(inSqlFragment) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/search_api/sql_fragment.rb', line 57 def or(inSqlFragment) inSqlFragment = SqlFragment.new(inSqlFragment) unless inSqlFragment.is_a?(SqlFragment) return self if inSqlFragment.empty? return inSqlFragment if empty? SqlFragment(["(#{sqlString}) OR (#{inSqlFragment[0]})"] + sqlParameters + inSqlFragment[1..-1]) end |
#or!(inSqlFragment) ⇒ Object
63 |
# File 'lib/search_api/sql_fragment.rb', line 63 def or!(inSqlFragment) replace(self.or(inSqlFragment)) end |
#sanitize ⇒ Object
88 89 90 91 |
# File 'lib/search_api/sql_fragment.rb', line 88 def sanitize fragment = self ActiveRecord::Base.instance_eval do sanitize_sql(fragment) end end |
#sqlParameters ⇒ Object
84 85 86 |
# File 'lib/search_api/sql_fragment.rb', line 84 def sqlParameters self[1..-1] end |
#sqlString ⇒ Object
81 82 83 |
# File 'lib/search_api/sql_fragment.rb', line 81 def sqlString self[0] end |