Class: ActiveRecord::ConnectionAdapters::SQLServerColumn

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

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from Column

#default, #limit, #name, #null, #precision, #primary, #scale, #sql_type, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Column

#human_name, #klass, #number?, string_to_date, string_to_dummy_time, #text?, #type_cast_code, value_to_boolean, value_to_decimal

Constructor Details

#initialize(name, default, sql_type = nil, identity = false, null = true) ⇒ SQLServerColumn

TODO: check ok to remove scale_value = 0



56
57
58
59
60
61
62
63
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 56

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

Instance Attribute Details

#identityObject (readonly)

Returns the value of attribute identity.



54
55
56
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 54

def identity
  @identity
end

#is_specialObject (readonly)

Returns the value of attribute is_special.



54
55
56
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 54

def is_special
  @is_special
end

Class Method Details

.binary_to_string(value) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 135

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.



124
125
126
127
128
129
130
131
132
133
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 124

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

.string_to_time(value) ⇒ Object

TODO: Find less hack way to convert DateTime objects into Times



114
115
116
117
118
119
120
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 114

def self.string_to_time(value)
  if value.is_a?(DateTime)
    return Time.mktime(value.year, value.mon, value.day, value.hour, value.min, value.sec)
  else
    super
  end
end

Instance Method Details

#cast_to_datetime(value) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 93

def cast_to_datetime(value)
  return value.to_time if value.is_a?(DBI::Timestamp)
  
  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
   
  if value.is_a?(DateTime)
    return Time.mktime(value.year, value.mon, value.day, value.hour, value.min, value.sec)
  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



87
88
89
90
91
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 87

def cast_to_time(value)
  return value if value.is_a?(Time)
  time_array = ParseDate.parsedate(value)
  Time.send(Base.default_timezone, *time_array) rescue nil
end

#simplified_type(field_type) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 65

def simplified_type(field_type)
  case field_type
    when /money/i             then :decimal
    when /image/i             then :binary
    when /bit/i               then :boolean
    when /uniqueidentifier/i  then :string
    else super
  end
end

#type_cast(value) ⇒ Object



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

def type_cast(value)
  return nil if value.nil?
  case type
  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 super
  end
end