Class: ActiveRecord::ConnectionAdapters::FbColumn

Inherits:
Column
  • Object
show all
Defined in:
lib/active_record/connection_adapters/fb_adapter.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, domain, type, sub_type, length, precision, scale, default_source, null_flag) ⇒ FbColumn

Returns a new instance of FbColumn.



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_record/connection_adapters/fb_adapter.rb', line 76

def initialize(name, domain, type, sub_type, length, precision, scale, default_source, null_flag)
  @firebird_type = Fb::SqlType.from_code(type, sub_type || 0)
  super(name.downcase, nil, @firebird_type, !null_flag)
  @default = parse_default(default_source) if default_source
  case @firebird_type
    when 'VARCHAR', 'CHAR'
      @limit = length
    when 'DECIMAL', 'NUMERIC'
      @precision, @scale = precision, scale.abs
  end
  @domain, @sub_type = domain, sub_type
end

Class Method Details

.value_to_boolean(value) ⇒ Object



119
120
121
# File 'lib/active_record/connection_adapters/fb_adapter.rb', line 119

def self.value_to_boolean(value)
  %W(#{FbAdapter.boolean_domain[:true]} true t 1).include? value.to_s.downcase
end

Instance Method Details

#defaultObject

Submits a CAST query to the database, casting the default value to the specified SQL type. This enables Firebird to provide an actual value when context variables are used as column defaults (such as CURRENT_TIMESTAMP).



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_record/connection_adapters/fb_adapter.rb', line 102

def default
  if @default
    sql = "SELECT CAST(#{@default} AS #{column_def}) FROM RDB$DATABASE"
    connection = ActiveRecord::Base.connection
    if connection
      value = connection.select_one(sql)['cast']
      if value.acts_like?(:date) or value.acts_like?(:time)
        nil
      else
        type_cast(value)
      end
    else
      raise ConnectionNotEstablished, "No Firebird connections established."
    end
  end
end

#typeObject



89
90
91
92
93
94
95
96
97
# File 'lib/active_record/connection_adapters/fb_adapter.rb', line 89

def type
  if @domain =~ /BOOLEAN/
    :boolean
  elsif @type == :binary and @sub_type == 1
    :text
  else
    @type
  end
end