Skip to content

Commit a6936d8

Browse files
committed
Greatly simplify #pk_and_sequence_for. Also moving some methods around.
1 parent 6b4675b commit a6936d8

File tree

1 file changed

+31
-50
lines changed

1 file changed

+31
-50
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,23 @@ def tables(name = nil)
494494
select_values "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'"
495495
end
496496

497+
def indexes(table_name, name = nil)
498+
select("EXEC sp_helpindex #{quote_table_name(table_name)}",name).inject([]) do |indexes,index|
499+
if index['index_description'] =~ /primary key/
500+
indexes
501+
else
502+
name = index['index_name']
503+
unique = index['index_description'] =~ /unique/
504+
columns = index['index_keys'].split(',').map do |column|
505+
column.strip!
506+
column.gsub! '(-)', '' if column.ends_with?('(-)')
507+
column
508+
end
509+
indexes << IndexDefinition.new(table_name, name, unique, columns)
510+
end
511+
end
512+
end
513+
497514
def columns(table_name, name = nil)
498515
return [] if table_name.blank?
499516
cache_key = unqualify_table_name(table_name)
@@ -560,27 +577,23 @@ def rename_column(table_name, column_name, new_column_name)
560577
def remove_index(table_name, options = {})
561578
execute "DROP INDEX #{table_name}.#{quote_column_name(index_name(table_name, options))}"
562579
end
563-
564-
def indexes(table_name, name = nil)
565-
select("EXEC sp_helpindex #{quote_table_name(table_name)}",name).inject([]) do |indexes,index|
566-
if index['index_description'] =~ /primary key/
567-
indexes
568-
else
569-
name = index['index_name']
570-
unique = index['index_description'] =~ /unique/
571-
columns = index['index_keys'].split(',').map do |column|
572-
column.strip!
573-
column.gsub! '(-)', '' if column.ends_with?('(-)')
574-
column
575-
end
576-
indexes << IndexDefinition.new(table_name, name, unique, columns)
580+
581+
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
582+
limit = nil unless self.class.type_limitable?(type)
583+
if type.to_s == 'integer'
584+
case limit
585+
when 1..2 then 'smallint'
586+
when 3..4, nil then 'integer'
587+
when 5..8 then 'bigint'
588+
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
577589
end
590+
else
591+
super
578592
end
579593
end
580594

581595
def add_order_by_for_association_limiting!(sql, options)
582596
return sql if options[:order].blank?
583-
584597
# Strip any ASC or DESC from the orders for the select list
585598
# Build fields and order arrays
586599
# e.g.: options[:order] = 'table.[id], table2.[col2] desc'
@@ -597,55 +610,23 @@ def add_order_by_for_association_limiting!(sql, options)
597610
fields << "MIN(#{$1}) AS #{$2}"
598611
order << "#{$2}#{$3}"
599612
end
600-
601613
sql.gsub!(/(.+?) FROM/, "\\1, #{fields.join(',')} FROM")
602614
sql << " ORDER BY #{order.join(',')}"
603615
end
604616

605-
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
606-
limit = nil unless self.class.type_limitable?(type)
607-
if type.to_s == 'integer'
608-
case limit
609-
when 1..2 then 'smallint'
610-
when 3..4, nil then 'integer'
611-
when 5..8 then 'bigint'
612-
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
613-
end
614-
else
615-
super
616-
end
617-
end
618-
619-
# Clear the given table and reset the table's id to 1
620-
# Argument:
621-
# +table_name+:: (String) Name of the table to be cleared and reset
622-
def truncate(table_name)
623-
execute("TRUNCATE TABLE #{table_name}; DBCC CHECKIDENT ('#{table_name}', RESEED, 1)")
624-
end
625-
626617
def change_column_null(table_name, column_name, null, default = nil)
627-
column = columns(table_name).find { |c| c.name == column_name.to_s }
628-
618+
column = column_for(table_name,column_name)
629619
unless null || default.nil?
630620
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
631621
end
632-
633-
# TODO - work out what the reason is for column.sql_type != type_to_sql(column.type, column.limit, column.precision, column.scale)
634622
sql = "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql column.type, column.limit, column.precision, column.scale}"
635623
sql << " NOT NULL" unless null
636624
execute sql
637625
end
638626

639-
# Returns a table's primary key and belonging sequence (not applicable to SQL server).
640627
def pk_and_sequence_for(table_name)
641-
keys = []
642-
execute("EXEC sp_helpindex '#{table_name}'") do |handle|
643-
if handle.column_info.any?
644-
pk_index = handle.detect {|index| index[1] =~ /primary key/ }
645-
keys << pk_index[2] if pk_index
646-
end
647-
end
648-
keys.length == 1 ? [keys.first, nil] : nil
628+
idcol = identity_column(table_name)
629+
idcol ? [idcol.name,nil] : nil
649630
end
650631

651632
# CONNECTION MANAGEMENT ====================================#

0 commit comments

Comments
 (0)