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"
83 elsif %w( boolean date datetime ).include?(type.to_s)
84 super(type) # cannot specify limit/precision/scale with these types
91 attr_accessor :identity, :is_special
93 def simplified_type(field_type)
95 when /int|bigint|smallint|tinyint/i then :integer
96 when /numeric/i then (@scale.nil? || @scale == 0) ? :integer : :decimal
97 when /float|double|decimal|money|real|smallmoney/i then :decimal
98 when /datetime|smalldatetime/i then :datetime
99 when /timestamp/i then :timestamp
100 when /time/i then :time
101 when /date/i then :date
102 when /text|ntext|xml/i then :text
103 when /binary|image|varbinary/i then :binary
104 when /char|nchar|nvarchar|string|varchar/i then (@limit == 1073741823 ? (@limit = nil; :text) : :string)
105 when /bit/i then :boolean
106 when /uniqueidentifier/i then :string
110 def default_value(value)
111 return $1 if value =~ /^\(N?'(.*)'\)$/
116 return nil if value.nil? || value == "(null)" || value == "(NULL)"
118 when :integer then value.to_i rescue unquote(value).to_i rescue value ? 1 : 0
119 when :primary_key then value == true || value == false ? value == true ? 1 : 0 : value.to_i
120 when :decimal then self.class.value_to_decimal(unquote(value))
121 when :datetime then cast_to_datetime(value)
122 when :timestamp then cast_to_time(value)
123 when :time then cast_to_time(value)
124 when :date then cast_to_date(value)
125 when :boolean then value == true or (value =~ /^t(rue)?$/i) == 0 or unquote(value)=="1"
126 when :binary then unquote value
131 def extract_limit(sql_type)
133 when /text|ntext|xml|binary|image|varbinary|bit/
141 sql_type =~ /nvarchar|ntext|nchar/i
145 value.to_s.sub(/\A\([\(\']?/, "").sub(/[\'\)]?\)\Z/, "")
148 def cast_to_time(value)
149 return value if value.is_a?(Time)
150 time_array = ParseDate.parsedate(value)
151 return nil if !time_array.any?
152 time_array[0] ||= 2000
155 return Time.send(ActiveRecord::Base.default_timezone, *time_array) rescue nil
157 # Try DateTime instead - the date may be outside the time period support by Time.
158 DateTime.new(*time_array[0..5]) rescue nil
161 def cast_to_date(value)
162 return value if value.is_a?(Date)
163 return Date.parse(value) rescue nil
166 def cast_to_datetime(value)
168 if value.year != 0 and value.month != 0 and value.day != 0
171 return Time.mktime(2000, 1, 1, value.hour, value.min, value.sec) rescue nil
174 if value.is_a?(DateTime)
176 # Attempt to convert back to a Time, but it could fail for dates significantly in the past/future.
177 return Time.mktime(value.year, value.mon, value.day, value.hour, value.min, value.sec)
183 return cast_to_time(value) if value.is_a?(Date) or value.is_a?(String) rescue nil
185 return value.is_a?(Date) ? value : nil
188 # These methods will only allow the adapter to insert binary data with a length of 7K or less
189 # because of a SQL Server statement length policy.
190 def self.string_to_binary(value)
196 def quote(value, column = nil)
197 return value.quoted_id if value.respond_to?(:quoted_id)
200 # SQL Server 2000 doesn't let you insert an integer into a NVARCHAR
201 # column, so we include Integer here.
202 when String, ActiveSupport::Multibyte::Chars, Integer
204 if column && column.type == :binary
205 "'#{quote_string(ArJdbc::MsSQL::Column.string_to_binary(value))}'" # ' (for ruby-mode)
206 elsif column && [:integer, :float].include?(column.type)
207 value = column.type == :integer ? value.to_i : value.to_f
209 elsif !column.respond_to?(:is_utf8?) || column.is_utf8?
210 "N'#{quote_string(value)}'" # ' (for ruby-mode)
214 when TrueClass then '1'
215 when FalseClass then '0'
220 def quote_string(string)
221 string.gsub(/\'/, "''")
224 def quote_table_name(name)
225 quote_column_name(name)
228 def quote_column_name(name)
240 def adapter_name #:nodoc:
244 def change_order_direction(order)
245 order.split(",").collect do |fragment|
247 when /\bDESC\b/i then fragment.gsub(/\bDESC\b/i, "ASC")
248 when /\bASC\b/i then fragment.gsub(/\bASC\b/i, "DESC")
249 else String.new(fragment).split(',').join(' DESC,') + ' DESC'
254 def supports_ddl_transactions?
258 def recreate_database(name)
260 create_database(name)
263 def drop_database(name)
265 execute "DROP DATABASE #{name}"
268 def create_database(name)
269 execute "CREATE DATABASE #{name}"
270 execute "USE #{name}"
273 def rename_table(name, new_name)
274 clear_cached_table(name)
275 execute "EXEC sp_rename '#{name}', '#{new_name}'"
278 # Adds a new column to the named table.
279 # See TableDefinition#column for details of the options you can use.
280 def add_column(table_name, column_name, type, options = {})
281 clear_cached_table(table_name)
282 add_column_sql = "ALTER TABLE #{table_name} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
283 add_column_options!(add_column_sql, options)
284 # TODO: Add support to mimic date columns, using constraints to mark them as such in the database
285 # 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
286 execute(add_column_sql)
289 def rename_column(table, column, new_column_name)
290 clear_cached_table(table)
291 execute "EXEC sp_rename '#{table}.#{column}', '#{new_column_name}'"
294 def change_column(table_name, column_name, type, options = {}) #:nodoc:
295 clear_cached_table(table_name)
296 change_column_type(table_name, column_name, type, options)
297 change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
300 def change_column_type(table_name, column_name, type, options = {}) #:nodoc:
301 clear_cached_table(table_name)
302 sql = "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
303 if options.has_key?(:null)
304 sql += (options[:null] ? " NULL" : " NOT NULL")
309 def change_column_default(table_name, column_name, default) #:nodoc:
310 clear_cached_table(table_name)
311 remove_default_constraint(table_name, column_name)
313 execute "ALTER TABLE #{table_name} ADD CONSTRAINT DF_#{table_name}_#{column_name} DEFAULT #{quote(default)} FOR #{quote_column_name(column_name)}"
317 def remove_column(table_name, column_name)
318 clear_cached_table(table_name)
319 remove_check_constraints(table_name, column_name)
320 remove_default_constraint(table_name, column_name)
321 execute "ALTER TABLE #{table_name} DROP COLUMN [#{column_name}]"
324 def remove_default_constraint(table_name, column_name)
325 clear_cached_table(table_name)
326 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"
327 defaults.each {|constraint|
328 execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint["name"]}"
332 def remove_check_constraints(table_name, column_name)
333 clear_cached_table(table_name)
334 # TODO remove all constraints in single method
335 constraints = select "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_NAME = '#{table_name}' and COLUMN_NAME = '#{column_name}'"
336 constraints.each do |constraint|
337 execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint["CONSTRAINT_NAME"]}"
341 def remove_index(table_name, options = {})
342 execute "DROP INDEX #{table_name}.#{index_name(table_name, options)}"
345 def columns(table_name, name = nil)
346 # It's possible for table_name to be an empty string, or nil, if something attempts to issue SQL
347 # which doesn't involve a table. IE. "SELECT 1" or "SELECT * from someFunction()".
348 return [] if table_name.blank?
349 table_name = table_name.to_s if table_name.is_a?(Symbol)
351 # Remove []'s from around the table name, valid in a select statement, but not when matching metadata.
352 table_name = table_name.gsub(/[\[\]]/, '')
354 return [] if table_name =~ /^information_schema\./i
355 @table_columns = {} unless @table_columns
356 unless @table_columns[table_name]
357 @table_columns[table_name] = super
358 @table_columns[table_name].each do |col|
359 col.identity = true if col.sql_type =~ /identity/i
360 col.is_special = true if col.sql_type =~ /text|ntext|image|xml/i
363 @table_columns[table_name]
366 def _execute(sql, name = nil)
367 # Match the start of the sql to determine appropriate behaviour. Be aware of
368 # multi-line sql which might begin with 'create stored_proc' and contain 'insert into ...' lines.
369 # Possible improvements include ignoring comment blocks prior to the first statement.
370 if sql.lstrip =~ /\Ainsert/i
371 if query_requires_identity_insert?(sql)
372 table_name = get_table_name(sql)
373 with_identity_insert_enabled(table_name) do
374 id = @connection.execute_insert(sql)
377 @connection.execute_insert(sql)
379 elsif sql.lstrip =~ /\A(create|exec)/i
380 @connection.execute_update(sql)
381 elsif sql.lstrip =~ /\A\(?\s*(select|show)/i
382 repair_special_columns(sql)
383 @connection.execute_query(sql)
385 @connection.execute_update(sql)
389 def select(sql, name = nil)
391 @connection.execute_query(sql)
395 #SELECT .. FOR UPDATE is not supported on Microsoft SQL Server
396 def add_lock!(sql, options)
400 # Turns IDENTITY_INSERT ON for table during execution of the block
401 # N.B. This sets the state of IDENTITY_INSERT to OFF after the
402 # block has been executed without regard to its previous state
403 def with_identity_insert_enabled(table_name, &block)
404 set_identity_insert(table_name, true)
407 set_identity_insert(table_name, false)
410 def set_identity_insert(table_name, enable = true)
411 execute "SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}"
412 rescue Exception => e
413 raise ActiveRecord::ActiveRecordError, "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
416 def identity_column(table_name)
417 columns(table_name).each do |col|
418 return col.name if col.identity
423 def query_requires_identity_insert?(sql)
424 table_name = get_table_name(sql)
425 id_column = identity_column(table_name)
426 if sql.strip =~ /insert into [^ ]+ ?\((.+?)\)/i
427 insert_columns = $1.split(/, */).map(&method(:unquote_column_name))
428 return table_name if insert_columns.include?(id_column)
432 def unquote_column_name(name)
433 if name =~ /^\[.*\]$/
440 def get_special_columns(table_name)
442 columns(table_name).each do |col|
443 special << col.name if col.is_special
448 def repair_special_columns(sql)
449 special_cols = get_special_columns(get_table_name(sql))
450 for col in special_cols.to_a
451 sql.gsub!(Regexp.new(" #{col.to_s} = "), " #{col.to_s} LIKE ")
452 sql.gsub!(/ORDER BY #{col.to_s}/i, '')
457 def determine_order_clause(sql)
458 return $1 if sql =~ /ORDER BY (.*)$/
459 table_name = get_table_name(sql)
460 "#{table_name}.#{determine_primary_key(table_name)}"
463 def determine_primary_key(table_name)
464 primary_key = columns(table_name).detect { |column| column.primary || column.identity }
465 return primary_key.name if primary_key
466 # Look for an id column. Return it, without changing case, to cover dbs with a case-sensitive collation.
467 columns(table_name).each { |column| return column.name if column.name =~ /^id$/i }
468 # Give up and provide something which is going to crash almost certainly
469 columns(table_name)[0].name
472 def clear_cached_table(name)
473 (@table_columns ||= {}).delete(name.to_s)