Module: ActiveRecord::ConnectionAdapters::Sqlserver::SchemaStatements

Included in:
ActiveRecord::ConnectionAdapters::SQLServerAdapter
Defined in:
lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#change_column(table_name, column_name, type, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 63

def change_column(table_name, column_name, type, options = {})
  sql_commands = []
  column_object = schema_cache.columns[table_name].detect { |c| c.name.to_s == column_name.to_s }
  change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
  change_column_sql << " NOT NULL" if options[:null] == false
  sql_commands << change_column_sql
  if options_include_default?(options) || (column_object && column_object.type != type.to_sym)
   	remove_default_constraint(table_name,column_name)
  end
  if options_include_default?(options)
    sql_commands << "ALTER TABLE #{quote_table_name(table_name)} ADD CONSTRAINT #{default_constraint_name(table_name,column_name)} DEFAULT #{quote(options[:default])} FOR #{quote_column_name(column_name)}"
  end
  sql_commands.each { |c| do_execute(c) }
end

#change_column_default(table_name, column_name, default) ⇒ Object



78
79
80
81
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 78

def change_column_default(table_name, column_name, default)
  remove_default_constraint(table_name, column_name)
  do_execute "ALTER TABLE #{quote_table_name(table_name)} ADD CONSTRAINT #{default_constraint_name(table_name, column_name)} DEFAULT #{quote(default)} FOR #{quote_column_name(column_name)}"
end

#change_column_null(table_name, column_name, null, default = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 108

def change_column_null(table_name, column_name, null, default = nil)
  column = detect_column_for! table_name, column_name
  unless null || default.nil?
    do_execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
  end
  sql = "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql column.type, column.limit, column.precision, column.scale}"
  sql << " NOT NULL" unless null
  do_execute sql
end

#columns(table_name, name = nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 41

def columns(table_name, name = nil)
  return [] if table_name.blank?
  column_definitions(table_name).collect do |ci|
    sqlserver_options = ci.except(:name,:default_value,:type,:null).merge(:database_year=>database_year)
    SQLServerColumn.new ci[:name], ci[:default_value], ci[:type], ci[:null], sqlserver_options
  end
end

#indexes(table_name, name = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 22

def indexes(table_name, name = nil)
  data = select("EXEC sp_helpindex #{quote(table_name)}",name) rescue []
  data.inject([]) do |indexes,index|
    index = index.with_indifferent_access
    if index[:index_description] =~ /primary key/
      indexes
    else
      name    = index[:index_name]
      unique  = index[:index_description] =~ /unique/
      columns = index[:index_keys].split(',').map do |column|
        column.strip!
        column.gsub! '(-)', '' if column.ends_with?('(-)')
        column
      end
      indexes << IndexDefinition.new(table_name, name, unique, columns)
    end
  end
end

#native_database_typesObject



6
7
8
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 6

def native_database_types
  @native_database_types ||= initialize_native_database_types.freeze
end

#remove_column(table_name, *column_names) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 53

def remove_column(table_name, *column_names)
  raise ArgumentError.new("You must specify at least one column name.  Example: remove_column(:people, :first_name)") if column_names.empty?
  column_names.flatten.each do |column_name|
    remove_check_constraints(table_name, column_name)
    remove_default_constraint(table_name, column_name)
    remove_indexes(table_name, column_name)
    do_execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)}"
  end
end

#remove_index!(table_name, index_name) ⇒ Object



88
89
90
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 88

def remove_index!(table_name, index_name)
  do_execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}"
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object



83
84
85
86
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 83

def rename_column(table_name, column_name, new_column_name)
  detect_column_for! table_name, column_name
  do_execute "EXEC sp_rename '#{table_name}.#{column_name}', '#{new_column_name}', 'COLUMN'"
end

#rename_table(table_name, new_name) ⇒ Object



49
50
51
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 49

def rename_table(table_name, new_name)
  do_execute "EXEC sp_rename '#{table_name}', '#{new_name}'"
end

#table_exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 16

def table_exists?(table_name)
  return false if table_name.blank?
  unquoted_table_name = Utils.unqualify_table_name(table_name)
  super || tables.include?(unquoted_table_name) || views.include?(unquoted_table_name)
end

#tables(table_type = 'BASE TABLE') ⇒ Object



10
11
12
13
14
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 10

def tables(table_type = 'BASE TABLE')
  info_schema_query do
    select_values "SELECT #{lowercase_schema_reflection_sql('TABLE_NAME')} FROM INFORMATION_SCHEMA.TABLES #{"WHERE TABLE_TYPE = '#{table_type}'" if table_type} ORDER BY TABLE_NAME"
  end
end

#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 92

def type_to_sql(type, limit = nil, precision = nil, scale = nil)
  type_limitable = ['string','integer','float','char','nchar','varchar','nvarchar'].include?(type.to_s)
  limit = nil unless type_limitable
  case type.to_s
  when 'integer'
    case limit
      when 1..2       then  'smallint'
      when 3..4, nil  then  'integer'
      when 5..8       then  'bigint'
      else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
    end
  else
    super
  end
end

#viewsObject

SQLServer Specific ======================================== #



120
121
122
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 120

def views
  tables('VIEW')
end