1 require 'arjdbc/mssql/tsql_helper'
2 require 'arjdbc/mssql/limit_helpers'
10 unless @lob_callback_added
11 ActiveRecord::Base.class_eval do
12 def after_save_with_mssql_lob
13 self.class.columns.select { |c| c.sql_type =~ /image/i }.each do |c|
15 value = value.to_yaml if unserializable_attribute?(c.name, c)
16 next if value.nil? || (value == '')
18 connection.write_large_object(c.type == :binary, c.name, self.class.table_name, self.class.primary_key, quote_value(id), value)
23 ActiveRecord::Base.after_save :after_save_with_mssql_lob
24 @lob_callback_added = true
26 mod.add_version_specific_add_limit_offset
29 def self.column_selector
30 [/sqlserver|tds|Microsoft SQL/i, lambda {|cfg,col| col.extend(::ArJdbc::MsSQL::Column)}]
33 def self.jdbc_connection_class
34 ::ActiveRecord::ConnectionAdapters::MssqlJdbcConnection
38 require 'arel/visitors/sql_server'
39 visitor_class = sqlserver_version == "2000" ? ::Arel::Visitors::SQLServer2000 : ::Arel::Visitors::SQLServer
40 { 'mssql' => visitor_class, 'sqlserver' => visitor_class, 'jdbcmssql' => visitor_class}
44 @sqlserver_version ||= select_value("select @@version")[/Microsoft SQL Server\s+(\d{4})/, 1]
47 def add_version_specific_add_limit_offset
48 if sqlserver_version == "2000"
49 extend LimitHelpers::SqlServer2000AddLimitOffset
51 extend LimitHelpers::SqlServerAddLimitOffset
55 def modify_types(tp) #:nodoc:
57 tp[:string] = {:name => "NVARCHAR", :limit => 255}
58 if sqlserver_version == "2000"
59 tp[:text] = {:name => "NTEXT"}
61 tp[:text] = {:name => "NVARCHAR(MAX)"}
65 tp[:big_integer] = { :name => "bigint"}
71 def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
72 # MSSQL's NVARCHAR(n | max) column supports either a number between 1 and
73 # 4000, or the word "MAX", which corresponds to 2**30-1 UCS-2 characters.
75 # It does not accept NVARCHAR(1073741823) here, so we have to change it
76 # to NVARCHAR(MAX), even though they are logically equivalent.
78 # MSSQL Server 2000 is skipped here because I don't know how it will behave.
80 # See: http://msdn.microsoft.com/en-us/library/ms186939.aspx
81 if type.to_s == 'string' and limit == 1073741823 and sqlserver_version != "2000"
82 # SONAR-6884 collation added
83 'NVARCHAR(MAX) COLLATE SQL_Latin1_General_CP1_CS_AS'
85 elsif %w( boolean date datetime ).include?(type.to_s)
86 super(type) # cannot specify limit/precision/scale with these types
87 elsif type.to_s == 'string'
89 super + ' COLLATE SQL_Latin1_General_CP1_CS_AS'
97 attr_accessor :identity, :is_special
99 def simplified_type(field_type)
101 when /int|bigint|smallint|tinyint/i then :integer
102 when /numeric/i then (@scale.nil? || @scale == 0) ? :integer : :decimal
103 when /float|double|decimal|money|real|smallmoney/i then :decimal
104 when /datetime|smalldatetime/i then :datetime
105 when /timestamp/i then :timestamp
106 when /time/i then :time
107 when /date/i then :date
108 when /text|ntext|xml/i then :text
109 when /binary|image|varbinary/i then :binary
110 when /char|nchar|nvarchar|string|varchar/i then (@limit == 1073741823 ? (@limit = nil; :text) : :string)
111 when /bit/i then :boolean
112 when /uniqueidentifier/i then :string
116 def default_value(value)
117 return $1 if value =~ /^\(N?'(.*)'\)$/
122 return nil if value.nil? || value == "(null)" || value == "(NULL)"
124 when :integer then value.to_i rescue unquote(value).to_i rescue value ? 1 : 0
125 when :primary_key then value == true || value == false ? value == true ? 1 : 0 : value.to_i
126 when :decimal then self.class.value_to_decimal(unquote(value))
127 when :datetime then cast_to_datetime(value)
128 when :timestamp then cast_to_time(value)
129 when :time then cast_to_time(value)
130 when :date then cast_to_date(value)
131 when :boolean then value == true or (value =~ /^t(rue)?$/i) == 0 or unquote(value)=="1"
132 when :binary then unquote value
137 def extract_limit(sql_type)
139 when /text|ntext|xml|binary|image|varbinary|bit/
147 sql_type =~ /nvarchar|ntext|nchar/i
151 value.to_s.sub(/\A\([\(\']?/, "").sub(/[\'\)]?\)\Z/, "")
154 def cast_to_time(value)
155 return value if value.is_a?(Time)
156 time_array = ParseDate.parsedate(value)
157 return nil if !time_array.any?
158 time_array[0] ||= 2000
161 return Time.send(ActiveRecord::Base.default_timezone, *time_array) rescue nil
163 # Try DateTime instead - the date may be outside the time period support by Time.
164 DateTime.new(*time_array[0..5]) rescue nil
167 def cast_to_date(value)
168 return value if value.is_a?(Date)
169 return Date.parse(value) rescue nil
172 def cast_to_datetime(value)
174 if value.year != 0 and value.month != 0 and value.day != 0
177 return Time.mktime(2000, 1, 1, value.hour, value.min, value.sec) rescue nil
180 if value.is_a?(DateTime)
182 # Attempt to convert back to a Time, but it could fail for dates significantly in the past/future.
183 return Time.mktime(value.year, value.mon, value.day, value.hour, value.min, value.sec)
189 return cast_to_time(value) if value.is_a?(Date) or value.is_a?(String) rescue nil
191 return value.is_a?(Date) ? value : nil
194 # These methods will only allow the adapter to insert binary data with a length of 7K or less
195 # because of a SQL Server statement length policy.
196 def self.string_to_binary(value)
202 def quote(value, column = nil)
203 return value.quoted_id if value.respond_to?(:quoted_id)
206 # SQL Server 2000 doesn't let you insert an integer into a NVARCHAR
207 # column, so we include Integer here.
208 when String, ActiveSupport::Multibyte::Chars, Integer
210 if column && column.type == :binary
211 "'#{quote_string(ArJdbc::MsSQL::Column.string_to_binary(value))}'" # ' (for ruby-mode)
212 elsif column && [:integer, :float].include?(column.type)
213 value = column.type == :integer ? value.to_i : value.to_f
215 elsif !column.respond_to?(:is_utf8?) || column.is_utf8?
216 "N'#{quote_string(value)}'" # ' (for ruby-mode)
220 when TrueClass then '1'
221 when FalseClass then '0'
226 def quote_string(string)
227 string.gsub(/\'/, "''")
230 def quote_table_name(name)
231 quote_column_name(name)
234 def quote_column_name(name)
246 def adapter_name #:nodoc:
250 def change_order_direction(order)
251 order.split(",").collect do |fragment|
253 when /\bDESC\b/i then fragment.gsub(/\bDESC\b/i, "ASC")
254 when /\bASC\b/i then fragment.gsub(/\bASC\b/i, "DESC")
255 else String.new(fragment).split(',').join(' DESC,') + ' DESC'
260 def supports_ddl_transactions?
264 def recreate_database(name)
266 create_database(name)
269 def drop_database(name)
271 execute "DROP DATABASE #{name}"
274 def create_database(name)
275 execute "CREATE DATABASE #{name}"
276 execute "USE #{name}"
279 def rename_table(name, new_name)
280 clear_cached_table(name)
281 execute "EXEC sp_rename '#{name}', '#{new_name}'"
284 # Adds a new column to the named table.
285 # See TableDefinition#column for details of the options you can use.
286 def add_column(table_name, column_name, type, options = {})
287 clear_cached_table(table_name)
288 add_column_sql = "ALTER TABLE #{table_name} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
289 add_column_options!(add_column_sql, options)
290 # TODO: Add support to mimic date columns, using constraints to mark them as such in the database
291 # add_column_sql << " CONSTRAINT ck__#{table_name}__#{column_name}__date_only CHECK ( CONVERT(CHAR(12), #{quote_column_name(column_name)}, 14)='00:00:00:000' )" if type == :date
292 execute(add_column_sql)
295 def rename_column(table, column, new_column_name)
296 clear_cached_table(table)
297 execute "EXEC sp_rename '#{table}.#{column}', '#{new_column_name}'"
300 def change_column(table_name, column_name, type, options = {}) #:nodoc:
301 clear_cached_table(table_name)
302 change_column_type(table_name, column_name, type, options)
303 change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
306 def change_column_type(table_name, column_name, type, options = {}) #:nodoc:
307 clear_cached_table(table_name)
308 sql = "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
309 if options.has_key?(:null)
310 sql += (options[:null] ? " NULL" : " NOT NULL")
315 def change_column_default(table_name, column_name, default) #:nodoc:
316 clear_cached_table(table_name)
317 remove_default_constraint(table_name, column_name)
319 execute "ALTER TABLE #{table_name} ADD CONSTRAINT DF_#{table_name}_#{column_name} DEFAULT #{quote(default)} FOR #{quote_column_name(column_name)}"
323 def remove_column(table_name, column_name)
324 clear_cached_table(table_name)
325 remove_check_constraints(table_name, column_name)
326 remove_default_constraint(table_name, column_name)
327 execute "ALTER TABLE #{table_name} DROP COLUMN [#{column_name}]"
330 def remove_default_constraint(table_name, column_name)
331 clear_cached_table(table_name)
332 defaults = select "select def.name from sysobjects def, syscolumns col, sysobjects tab where col.cdefault = def.id and col.name = '#{column_name}' and tab.name = '#{table_name}' and col.id = tab.id"
333 defaults.each {|constraint|
334 execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint["name"]}"
338 def remove_check_constraints(table_name, column_name)
339 clear_cached_table(table_name)
340 # TODO remove all constraints in single method
341 constraints = select "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_NAME = '#{table_name}' and COLUMN_NAME = '#{column_name}'"
342 constraints.each do |constraint|
343 execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint["CONSTRAINT_NAME"]}"
347 def remove_index(table_name, options = {})
348 execute "DROP INDEX #{table_name}.#{index_name(table_name, options)}"
351 def columns(table_name, name = nil)
352 # It's possible for table_name to be an empty string, or nil, if something attempts to issue SQL
353 # which doesn't involve a table. IE. "SELECT 1" or "SELECT * from someFunction()".
354 return [] if table_name.blank?
355 table_name = table_name.to_s if table_name.is_a?(Symbol)
357 # Remove []'s from around the table name, valid in a select statement, but not when matching metadata.
358 table_name = table_name.gsub(/[\[\]]/, '')
360 return [] if table_name =~ /^information_schema\./i
361 @table_columns = {} unless @table_columns
362 unless @table_columns[table_name]
363 @table_columns[table_name] = super
364 @table_columns[table_name].each do |col|
365 col.identity = true if col.sql_type =~ /identity/i
366 col.is_special = true if col.sql_type =~ /text|ntext|image|xml/i
369 @table_columns[table_name]
372 def _execute(sql, name = nil)
373 # Match the start of the sql to determine appropriate behaviour. Be aware of
374 # multi-line sql which might begin with 'create stored_proc' and contain 'insert into ...' lines.
375 # Possible improvements include ignoring comment blocks prior to the first statement.
376 if sql.lstrip =~ /\Ainsert/i
377 if query_requires_identity_insert?(sql)
378 table_name = get_table_name(sql)
379 with_identity_insert_enabled(table_name) do
380 id = @connection.execute_insert(sql)
383 @connection.execute_insert(sql)
385 elsif sql.lstrip =~ /\A(create|exec)/i
386 @connection.execute_update(sql)
387 elsif sql.lstrip =~ /\A\(?\s*(select|show)/i
388 repair_special_columns(sql)
389 @connection.execute_query(sql)
391 @connection.execute_update(sql)
395 def select(sql, name = nil)
397 @connection.execute_query(sql)
401 #SELECT .. FOR UPDATE is not supported on Microsoft SQL Server
402 def add_lock!(sql, options)
406 # Turns IDENTITY_INSERT ON for table during execution of the block
407 # N.B. This sets the state of IDENTITY_INSERT to OFF after the
408 # block has been executed without regard to its previous state
409 def with_identity_insert_enabled(table_name, &block)
410 set_identity_insert(table_name, true)
413 set_identity_insert(table_name, false)
416 def set_identity_insert(table_name, enable = true)
417 execute "SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}"
418 rescue Exception => e
419 raise ActiveRecord::ActiveRecordError, "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
422 def identity_column(table_name)
423 columns(table_name).each do |col|
424 return col.name if col.identity
429 def query_requires_identity_insert?(sql)
430 table_name = get_table_name(sql)
431 id_column = identity_column(table_name)
432 if sql.strip =~ /insert into [^ ]+ ?\((.+?)\)/i
433 insert_columns = $1.split(/, */).map(&method(:unquote_column_name))
434 return table_name if insert_columns.include?(id_column)
438 def unquote_column_name(name)
439 if name =~ /^\[.*\]$/
446 def get_special_columns(table_name)
448 columns(table_name).each do |col|
449 special << col.name if col.is_special
454 def repair_special_columns(sql)
455 special_cols = get_special_columns(get_table_name(sql))
456 for col in special_cols.to_a
457 sql.gsub!(Regexp.new(" #{col.to_s} = "), " #{col.to_s} LIKE ")
458 sql.gsub!(/ORDER BY #{col.to_s}/i, '')
463 def determine_order_clause(sql)
464 return $1 if sql =~ /ORDER BY (.*)$/
465 table_name = get_table_name(sql)
466 "#{table_name}.#{determine_primary_key(table_name)}"
469 def determine_primary_key(table_name)
470 primary_key = columns(table_name).detect { |column| column.primary || column.identity }
471 return primary_key.name if primary_key
472 # Look for an id column. Return it, without changing case, to cover dbs with a case-sensitive collation.
473 columns(table_name).each { |column| return column.name if column.name =~ /^id$/i }
474 # Give up and provide something which is going to crash almost certainly
475 columns(table_name)[0].name
478 def clear_cached_table(name)
479 (@table_columns ||= {}).delete(name.to_s)