Class: Momomoto::Datatype::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/momomoto/datatype/base.rb

Overview

Every data type class (see #Datatype) is derived from this class.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row = nil) ⇒ Base

Creates a new instance of the special data type, setting not_null and default according to the values from Information Schema.



35
36
37
38
# File 'lib/momomoto/datatype/base.rb', line 35

def initialize( row = nil )
  @not_null = row.respond_to?(:is_nullable) && row.is_nullable == "NO" ? true : false
  @default = row.respond_to?(:column_default) ? row.column_default : nil
end

Class Method Details

.operator_sign(op) ⇒ Object

These are operators supported by all data types. In your select statement use something like:

one_day_ago = Time.now - (3600*24)
Feeds.select( :date => {:ge => one_day_ago.to_s} )

This will select all rows from Feeds that are newer than 24 hours. Same with Momomoto’s TimeInterval:

one_day_ago = Time.now + TimeInterval.new({:hour => -24})
Feeds.select( :date => {:ge => one_day_ago.to_s} )

In case of data type Text also :like and :ilike are supported. For :like and :ilike operators you may use _ as placeholder for a single character or % as placeholder for multiple characters.

# Selects all posts having "surveillance" in their content field
# while ignoring case.
Posts.select( :content => {:ilike => 'surveillance'} )


124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/momomoto/datatype/base.rb', line 124

def self.operator_sign( op )
  case op
    when :le then '<='
    when :lt then '<'
    when :ge then '>='
    when :gt then '>'
    when :eq then '='
    when :ne then '<>'
    else
      raise CriticalError, "unsupported operator"
  end
end

Instance Method Details

#compile_rule(field_name, value) ⇒ Object

This method is used when compiling the where clause. No need for direct use.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/momomoto/datatype/base.rb', line 64

def compile_rule( field_name, value ) # :nodoc:
  case value
    when nil then
      raise Error, "nil values not allowed for #{field_name}"
    when :NULL then
      field_name.to_s + ' IS NULL'
    when :NOT_NULL then
      field_name.to_s + ' IS NOT NULL'
    when ::Array then
      return compile_rule( field_name, value[0] ) if value.length == 1
      raise Error, "empty array conditions are not allowed for #{field_name}" if value.empty?
      raise Error, "nil values not allowed in compile_rule for #{field_name}" if value.member?( nil )
      # do the right thing when NULL is part of an array
      if value.delete( :NULL )
        if value.length == 0
          return compile_rule( field_name, :NULL )
        else
          return " (#{compile_rule( field_name, :NULL)} OR #{compile_rule( field_name, value )} ) "
        end
      end
      field_name.to_s + ' IN (' + value.map{ | v | escape(filter_set(v)) }.join(',') + ')'
    when Hash then
      raise Error, "empty hash conditions are not allowed for #{field_name}" if value.empty?
      rules = []
      value.each do | op, v |
        raise Error, "nil values not allowed in compile_rule for #{field_name}" if v == nil
        v = [v] if not v.kind_of?( ::Array )
        if op == :eq # use IN if comparing for equality
          rules << compile_rule( field_name, v )
        else
          v.each do | v2 |
            rules << field_name.to_s + ' ' + self.class.operator_sign(op) + ' ' + escape(filter_set(v2))
          end
        end
      end
      rules.join( " AND " )
    else
      field_name.to_s + " #{default_operator} " + escape(filter_set(value))
  end
end

#defaultObject

Gets the default value for this column or returns nil if none exists.



19
20
21
# File 'lib/momomoto/datatype/base.rb', line 19

def default
  @default
end

#default_operatorObject

Get the default value for this Datatype.



24
25
26
# File 'lib/momomoto/datatype/base.rb', line 24

def default_operator
  "="
end

#equal(a, b) ⇒ Object

Compares two values and return true if equal or false otherwise. It is used to check if a row field has been changed so that only changed fields are written to database.



50
51
52
# File 'lib/momomoto/datatype/base.rb', line 50

def equal( a, b )
  a == b
end

#escape(input) ⇒ Object

Escapes input to be saved in database. If input equals nil, NULL is returned, otherwise Database#quote is called. This method is overwritten to get data type-specific escaping rules.



58
59
60
# File 'lib/momomoto/datatype/base.rb', line 58

def escape( input )
  input.nil? ? "NULL" : Database.quote( input.to_s )
end

#filter_set(value) ⇒ Object

Values are filtered by this function when being set. See the method in the appropriate derived data type class for allowed values.



43
44
45
# File 'lib/momomoto/datatype/base.rb', line 43

def filter_set( value ) # :nodoc:
  value
end

#not_null?Boolean

Returns true if this column can be NULL otherwise false.

Returns:



29
30
31
# File 'lib/momomoto/datatype/base.rb', line 29

def not_null?
  @not_null
end