Module: ArJdbc::PostgreSQL::Column

Defined in:
lib/arjdbc/postgresql/adapter.rb

Instance Method Summary collapse

Instance Method Details

#cast_to_boolean(value) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/arjdbc/postgresql/adapter.rb', line 55

def cast_to_boolean(value)
  return nil if value.nil?
  if value == true || value == false
    value
  else
    %w(true t 1).include?(value.to_s.downcase)
  end
end

#default_value(value) ⇒ Object

Post process default value from JDBC into a Rails-friendly format (columns-internal)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/arjdbc/postgresql/adapter.rb', line 65

def default_value(value)
  # Boolean types
  return "t" if value =~ /true/i
  return "f" if value =~ /false/i

  # Char/String/Bytea type values
  return $1 if value =~ /^'(.*)'::(bpchar|text|character varying|bytea)$/

  # Numeric values
  return value.delete("()") if value =~ /^\(?-?[0-9]+(\.[0-9]*)?\)?/

  # Fixed dates / timestamp
  return $1 if value =~ /^'(.+)'::(date|timestamp)/

  # Anything else is blank, some user type, or some function
  # and we can't know the value of that, so return nil.
  return nil
end

#extract_limit(sql_type) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/arjdbc/postgresql/adapter.rb', line 30

def extract_limit(sql_type)
  case sql_type
  when /^int2/i;      2
  when /^smallint/i;  2
  when /^int4/i;      nil
  when /^integer/i;   nil
  when /^int8/i;      8
  when /^bigint/i;    8
  when /^(bool|text|date|time|bytea)/i; nil # ACTIVERECORD_JDBC-135,139
  else super
  end
end

#simplified_type(field_type) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/arjdbc/postgresql/adapter.rb', line 43

def simplified_type(field_type)
  return :integer if field_type =~ /^(big|)serial/i
  return :string if field_type =~ /\[\]$/i || field_type =~ /^interval/i
  return :string if field_type =~ /^(?:point|lseg|box|"?path"?|polygon|circle)/i
  return :datetime if field_type =~ /^timestamp/i
  return :float if field_type =~ /^(?:real|double precision)$/i
  return :binary if field_type =~ /^bytea/i
  return :boolean if field_type =~ /^bool/i
  return :decimal if field_type == 'numeric(131089)'
  super
end

#type_cast(value) ⇒ Object



23
24
25
26
27
28
# File 'lib/arjdbc/postgresql/adapter.rb', line 23

def type_cast(value)
  case type
  when :boolean then cast_to_boolean(value)
  else super
  end
end