]> source.dussan.org Git - sonarqube.git/blob
7e269d7bc3a4394d4cbcbd500cbd5a7c22f728d7
[sonarqube.git] /
1 require 'arjdbc/mssql/tsql_helper'
2 require 'arjdbc/mssql/limit_helpers'
3
4 module ::ArJdbc
5   module MsSQL
6     include TSqlMethods
7     include LimitHelpers
8
9     def self.extended(mod)
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|
14               value = self[c.name]
15               value = value.to_yaml if unserializable_attribute?(c.name, c)
16               next if value.nil?  || (value == '')
17
18               connection.write_large_object(c.type == :binary, c.name, self.class.table_name, self.class.primary_key, quote_value(id), value)
19             end
20           end
21         end
22
23         ActiveRecord::Base.after_save :after_save_with_mssql_lob
24         @lob_callback_added = true
25       end
26       mod.add_version_specific_add_limit_offset
27     end
28
29     def self.column_selector
30       [/sqlserver|tds|Microsoft SQL/i, lambda {|cfg,col| col.extend(::ArJdbc::MsSQL::Column)}]
31     end
32
33     def self.jdbc_connection_class
34       ::ActiveRecord::ConnectionAdapters::MssqlJdbcConnection
35     end
36
37     def arel2_visitors
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}
41     end
42
43     def sqlserver_version
44       @sqlserver_version ||= select_value("select @@version")[/Microsoft SQL Server\s+(\d{4})/, 1]
45     end
46
47     def add_version_specific_add_limit_offset
48       if sqlserver_version == "2000"
49         extend LimitHelpers::SqlServer2000AddLimitOffset
50       else
51         extend LimitHelpers::SqlServerAddLimitOffset
52       end
53     end
54
55     def modify_types(tp) #:nodoc:
56       super(tp)
57       tp[:string] = {:name => "NVARCHAR", :limit => 255}
58       if sqlserver_version == "2000"
59         tp[:text] = {:name => "NTEXT"}
60       else
61         tp[:text] = {:name => "NVARCHAR(MAX)"}
62       end
63
64       # sonar
65       tp[:big_integer] = { :name => "bigint"}
66       # /sonar
67
68       tp
69     end
70
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.
74       #
75       # It does not accept NVARCHAR(1073741823) here, so we have to change it
76       # to NVARCHAR(MAX), even though they are logically equivalent.
77       #
78       # MSSQL Server 2000 is skipped here because I don't know how it will behave.
79       #
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'
84         # /SONAR-6884
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'
88         # SONAR-6884
89         super + ' COLLATE SQL_Latin1_General_CP1_CS_AS'
90         # /SONAR-6884
91       else
92         super
93       end
94     end
95
96     module Column
97       attr_accessor :identity, :is_special
98
99       def simplified_type(field_type)
100         case 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
113         end
114       end
115
116       def default_value(value)
117         return $1 if value =~ /^\(N?'(.*)'\)$/
118         value
119       end
120
121       def type_cast(value)
122         return nil if value.nil? || value == "(null)" || value == "(NULL)"
123         case type
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
133         else value
134         end
135       end
136
137       def extract_limit(sql_type)
138         case sql_type
139         when /text|ntext|xml|binary|image|varbinary|bit/
140           nil
141         else
142           super
143         end
144       end
145
146       def is_utf8?
147         sql_type =~ /nvarchar|ntext|nchar/i
148       end
149
150       def unquote(value)
151         value.to_s.sub(/\A\([\(\']?/, "").sub(/[\'\)]?\)\Z/, "")
152       end
153
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
159         time_array[1] ||= 1
160         time_array[2] ||= 1
161         return Time.send(ActiveRecord::Base.default_timezone, *time_array) rescue nil
162
163         # Try DateTime instead - the date may be outside the time period support by Time.
164         DateTime.new(*time_array[0..5]) rescue nil
165       end
166
167       def cast_to_date(value)
168         return value if value.is_a?(Date)
169         return Date.parse(value) rescue nil
170       end
171
172       def cast_to_datetime(value)
173         if value.is_a?(Time)
174           if value.year != 0 and value.month != 0 and value.day != 0
175             return value
176           else
177             return Time.mktime(2000, 1, 1, value.hour, value.min, value.sec) rescue nil
178           end
179         end
180         if value.is_a?(DateTime)
181           begin
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)
184           rescue ArgumentError
185             return value
186           end
187         end
188
189         return cast_to_time(value) if value.is_a?(Date) or value.is_a?(String) rescue nil
190
191         return value.is_a?(Date) ? value : nil
192       end
193
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)
197         ''
198       end
199
200     end
201
202     def quote(value, column = nil)
203       return value.quoted_id if value.respond_to?(:quoted_id)
204
205       case value
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
209         value = value.to_s
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
214           value.to_s
215         elsif !column.respond_to?(:is_utf8?) || column.is_utf8?
216           "N'#{quote_string(value)}'" # ' (for ruby-mode)
217         else
218           super
219         end
220       when TrueClass             then '1'
221       when FalseClass            then '0'
222       else                       super
223       end
224     end
225
226     def quote_string(string)
227       string.gsub(/\'/, "''")
228     end
229
230     def quote_table_name(name)
231       quote_column_name(name)
232     end
233
234     def quote_column_name(name)
235       "[#{name}]"
236     end
237
238     def quoted_true
239       quote true
240     end
241
242     def quoted_false
243       quote false
244     end
245
246     def adapter_name #:nodoc:
247       'MsSQL'
248     end
249
250     def change_order_direction(order)
251       order.split(",").collect do |fragment|
252         case 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'
256         end
257       end.join(",")
258     end
259
260     def supports_ddl_transactions?
261       true
262     end
263
264     def recreate_database(name)
265       drop_database(name)
266       create_database(name)
267     end
268
269     def drop_database(name)
270       execute "USE master"
271       execute "DROP DATABASE #{name}"
272     end
273
274     def create_database(name)
275       execute "CREATE DATABASE #{name}"
276       execute "USE #{name}"
277     end
278
279     def rename_table(name, new_name)
280       clear_cached_table(name)
281       execute "EXEC sp_rename '#{name}', '#{new_name}'"
282     end
283
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)
293     end
294
295     def rename_column(table, column, new_column_name)
296       clear_cached_table(table)
297       execute "EXEC sp_rename '#{table}.#{column}', '#{new_column_name}'"
298     end
299
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)
304     end
305
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")
311       end
312       execute(sql)
313     end
314
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)
318       unless default.nil?
319         execute "ALTER TABLE #{table_name} ADD CONSTRAINT DF_#{table_name}_#{column_name} DEFAULT #{quote(default)} FOR #{quote_column_name(column_name)}"
320       end
321     end
322
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}]"
328     end
329
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"]}"
335       }
336     end
337
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"]}"
344       end
345     end
346
347     def remove_index(table_name, options = {})
348       execute "DROP INDEX #{table_name}.#{index_name(table_name, options)}"
349     end
350
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)
356
357       # Remove []'s from around the table name, valid in a select statement, but not when matching metadata.
358       table_name = table_name.gsub(/[\[\]]/, '')
359
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
367         end
368       end
369       @table_columns[table_name]
370     end
371
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)
381           end
382         else
383           @connection.execute_insert(sql)
384         end
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)
390       else
391         @connection.execute_update(sql)
392       end
393     end
394
395     def select(sql, name = nil)
396       log(sql, name) do
397         @connection.execute_query(sql)
398       end
399     end
400
401     #SELECT .. FOR UPDATE is not supported on Microsoft SQL Server
402     def add_lock!(sql, options)
403       sql
404     end
405
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)
411       yield
412     ensure
413       set_identity_insert(table_name, false)
414     end
415
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}"
420     end
421
422     def identity_column(table_name)
423       columns(table_name).each do |col|
424         return col.name if col.identity
425       end
426       return nil
427     end
428
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)
435       end
436     end
437
438     def unquote_column_name(name)
439       if name =~ /^\[.*\]$/
440         name[1..-2]
441       else
442         name
443       end
444     end
445
446     def get_special_columns(table_name)
447       special = []
448       columns(table_name).each do |col|
449         special << col.name if col.is_special
450       end
451       special
452     end
453
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, '')
459       end
460       sql
461     end
462
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)}"
467     end
468
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
476     end
477
478     def clear_cached_table(name)
479       (@table_columns ||= {}).delete(name.to_s)
480     end
481   end
482 end
483