Class: ActiveRecord::ConnectionAdapters::ColumnWithIdentityAndOrdinal

Inherits:
Column
  • Object
show all
Defined in:
lib/column_with_identity_and_ordinal.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default, sql_type = nil, is_identity = false, null = true, scale_value = 0, ordinal = 0) ⇒ ColumnWithIdentityAndOrdinal

Returns a new instance of ColumnWithIdentityAndOrdinal.



6
7
8
9
10
11
12
13
14
# File 'lib/column_with_identity_and_ordinal.rb', line 6

def initialize(name, default, sql_type = nil, is_identity = false, null = true, scale_value = 0, ordinal = 0)
  super(name, default, sql_type, null)
  @identity = is_identity
  @is_special = sql_type =~ /text|ntext|image/i ? true : false
  @scale = scale_value
  # SQL Server only supports limits on *char and float types
  @limit = nil unless @type == :float or @type == :string
				@ordinal = ordinal
end

Instance Attribute Details

#identityObject (readonly)

Returns the value of attribute identity.



4
5
6
# File 'lib/column_with_identity_and_ordinal.rb', line 4

def identity
  @identity
end

#is_specialObject (readonly)

Returns the value of attribute is_special.



4
5
6
# File 'lib/column_with_identity_and_ordinal.rb', line 4

def is_special
  @is_special
end

#scaleObject (readonly)

Returns the value of attribute scale.



4
5
6
# File 'lib/column_with_identity_and_ordinal.rb', line 4

def scale
  @scale
end

Class Method Details

.binary_to_string(value) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/column_with_identity_and_ordinal.rb', line 80

def self.binary_to_string(value)
  value.gsub(/(%00|%01|%02|%03)/) do
    case $1
      when "%00"    then  "\r"
      when "%01"    then  "\n"
      when "%02\0"  then  "\0"
      when "%03"    then  "\x1a"
    end
  end
end

.string_to_binary(value) ⇒ Object

These methods will only allow the adapter to insert binary data with a length of 7K or less because of a SQL Server statement length policy.



69
70
71
72
73
74
75
76
77
78
# File 'lib/column_with_identity_and_ordinal.rb', line 69

def self.string_to_binary(value)
  value.gsub(/(\r|\n|\0|\x1a)/) do
    case $1
      when "\r"   then  "%00"
      when "\n"   then  "%01"
      when "\0"   then  "%02"
      when "\x1a" then  "%03"
    end
  end
end

Instance Method Details

#cast_to_datetime(value) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/column_with_identity_and_ordinal.rb', line 55

def cast_to_datetime(value)
  if value.is_a?(Time)
    if value.year != 0 and value.month != 0 and value.day != 0
      return value
    else
      return Time.mktime(2000, 1, 1, value.hour, value.min, value.sec) rescue nil
    end
  end
  return cast_to_time(value) if value.is_a?(Date) or value.is_a?(String) rescue nil
  value
end

#cast_to_time(value) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/column_with_identity_and_ordinal.rb', line 46

def cast_to_time(value)
  return value if value.is_a?(Time)
  time_array = ParseDate.parsedate(value)
  time_array[0] ||= 2000
  time_array[1] ||= 1
  time_array[2] ||= 1
  Time.send(Base.default_timezone, *time_array) rescue nil
end

#simplified_type(field_type) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/column_with_identity_and_ordinal.rb', line 16

def simplified_type(field_type)
  case field_type
    when /int|bigint|smallint|tinyint/i                        then :integer
    when /float|double|decimal|money|numeric|real|smallmoney/i then @scale == 0 ? :integer : :float
    when /datetime|smalldatetime/i                             then :datetime
    when /timestamp/i                                          then :timestamp
    when /time/i                                               then :time
    when /text|ntext/i                                         then :text
    when /binary|image|varbinary/i                             then :binary
    when /char|nchar|nvarchar|string|varchar/i                 then :string
    when /bit/i                                                then :boolean
    when /uniqueidentifier/i                                   then :string
  end
end

#type_cast(value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/column_with_identity_and_ordinal.rb', line 31

def type_cast(value)
  return nil if value.nil? || value =~ /^\s*null\s*$/i
  case type
  when :string    then value
  when :integer   then value == true || value == false ? value == true ? 1 : 0 : value.to_i
  when :float     then value.to_f
  when :datetime  then cast_to_datetime(value)
  when :timestamp then cast_to_time(value)
  when :time      then cast_to_time(value)
  when :date      then cast_to_datetime(value)
  when :boolean   then value == true or (value =~ /^t(rue)?$/i) == 0 or value.to_s == '1'
  else value
  end
end