Class: Momomoto::Datatype::Array::Base

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

Overview

base datatype for array types

Direct Known Subclasses

Integer, Text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#default, #equal, #not_null?

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.



10
11
12
13
# File 'lib/momomoto/datatype/array/base.rb', line 10

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

Additional operators for instances of Array. See Base#operator_sign



83
84
85
86
87
88
89
90
# File 'lib/momomoto/datatype/array/base.rb', line 83

def self.operator_sign( op )
  case op
    when :contains then '@>'
    when :contained then '<@'
    else
      super( op )
  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.



71
72
73
74
75
76
77
78
79
# File 'lib/momomoto/datatype/array/base.rb', line 71

def compile_rule( field_name, value ) # :nodoc:
  case value
    when ::Array then
      raise Error, "empty or nil array conditions are not allowed for #{field_name}" if value.empty? or value.member?( nil )
      field_name.to_s + ' @> ' + escape(filter_set(value))
    else
      super
  end
end

#default_operatorObject

Get the default value for this Datatype.



65
66
67
# File 'lib/momomoto/datatype/array/base.rb', line 65

def default_operator
  "@>"
end

#escape(input) ⇒ Object

Escape and quote input to be saved in database. Returns ‘NULL’ if input is nil or empty. Otherwise uses Database#quote



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/momomoto/datatype/array/base.rb', line 18

def escape( input )
  if input.nil?
    "NULL"
  elsif input.instance_of?( ::Array )
    if input.empty?
      "'{}'::#{array_type}[]"
    else
      "ARRAY[" + input.map{|m| Database.quote(m)}.join(',') + "]::#{array_type}[]"
    end
  else
    Database.quote( input )
  end
end

#filter_get(value) ⇒ Object

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/momomoto/datatype/array/base.rb', line 35

def filter_get( value ) # :nodoc:
  case value
    when ::Array then value
    when nil,"" then nil
    when "{}" then []
    when /^\{("[^"]+"|[^",]+)(,("[^"]+"|[^",]+))*\}$/
      m = value.match(/^\{(.*)\}$/)
      values = []
      m[1].gsub( /("[^"]+"|[^",]+)/ ) do | element |
        if m = element.match(/^"(.*)"$/)
          values << m[1]
        else
          values << element
        end
      end
      values
    else
      raise Error, "Error parsing array value"
  end
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.



59
60
61
62
# File 'lib/momomoto/datatype/array/base.rb', line 59

def filter_set( value ) # :nodoc:
  value = [value] if value && !value.instance_of?( ::Array )
  value
end