Advertisement
maxsum-corin

DataMapper bitwise comparison operator for MS SQL (tsql)

Oct 15th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Taken from http://pastebin.com/xWU62iT2
  2. # See https://github.com/datamapper/dm-types/issues/44
  3. # with modifications for sql server
  4. module DataMapper
  5.   class Query
  6.     module Conditions
  7.       # Tests whether the value in the record
  8.       # has the specified bits set to true (bitwise and)
  9.       class HasComparison < AbstractComparison
  10.         slug :has
  11.  
  12.         def matches?(record)
  13.           record_value = record_value(record)
  14.           !record_value.nil? && (record_value & expected)
  15.         end
  16.  
  17.         # @return [String]
  18.         #
  19.         # @see AbstractComparison#to_s
  20.         #
  21.         # @api private
  22.         def comparator_string
  23.           '&'
  24.         end
  25.       end # class HasFlagComparison
  26.     end # module Conditions
  27.   end # class Query
  28. end # module DataMapper
  29.  
  30. module DataMapper
  31.   module Adapters
  32.     class DataObjectsAdapter < AbstractAdapter
  33.       def comparison_operator(comparison)
  34.         subject = comparison.subject
  35.         value   = comparison.value
  36.  
  37.         case comparison.slug
  38.         when :has    then has_operator(subject, value)
  39.         when :eql    then equality_operator(subject, value)
  40.         when :in     then include_operator(subject, value)
  41.         when :regexp then regexp_operator(value)
  42.         when :like   then like_operator(value)
  43.         when :gt     then '>'
  44.         when :lt     then '<'
  45.         when :gte    then '>='
  46.         when :lte    then '<='
  47.         end
  48.       end
  49.  
  50.       def has_operator(property, operand)
  51.         "& #{operand} ="
  52.       end
  53.     end
  54.   end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement