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
- #add_column(table_name, column_name, type, options = {}) ⇒ Object
- #change_column(table_name, column_name, type, options = {}) ⇒ Object
- #change_column_default(table_name, column_name, default) ⇒ Object
- #change_column_null(table_name, column_name, null, default = nil) ⇒ Object
- #columns(table_name, name = nil) ⇒ Object
- #create_table(table_name, options = {}) ⇒ Object
- #drop_table(table_name, options = {}) ⇒ Object
- #indexes(table_name, name = nil) ⇒ Object
- #native_database_types ⇒ Object
- #remove_column(table_name, *column_names) ⇒ Object
- #remove_index!(table_name, index_name) ⇒ Object
- #rename_column(table_name, column_name, new_column_name) ⇒ Object
- #rename_table(table_name, new_name) ⇒ Object
- #table_exists?(table_name) ⇒ Boolean
- #tables(name = nil) ⇒ Object
- #type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
-
#views(name = nil) ⇒ Object
SQLServer Specific ======================================== #.
Instance Method Details
#add_column(table_name, column_name, type, options = {}) ⇒ Object
64 65 66 67 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 64 def add_column(table_name, column_name, type, = {}) super remove_sqlserver_columns_cache_for(table_name) end |
#change_column(table_name, column_name, type, options = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 80 def change_column(table_name, column_name, type, = {}) sql_commands = [] column_object = 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, [:limit], [:precision], [:scale])}" change_column_sql << " NOT NULL" if [:null] == false sql_commands << change_column_sql if () || (column_object && column_object.type != type.to_sym) remove_default_constraint(table_name,column_name) end if () remove_sqlserver_columns_cache_for(table_name) sql_commands << "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 sql_commands.each { |c| do_execute(c) } remove_sqlserver_columns_cache_for(table_name) end |
#change_column_default(table_name, column_name, default) ⇒ Object
97 98 99 100 101 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 97 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)}" remove_sqlserver_columns_cache_for(table_name) end |
#change_column_null(table_name, column_name, null, default = nil) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 129 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 48 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 41 def columns(table_name, name = nil) return [] if table_name.blank? cache_key = columns_cache_key(table_name) @sqlserver_columns_cache[cache_key] ||= column_definitions(table_name).collect do |ci| = ci.except(:name,:default_value,:type,:null).merge(:database_year=>database_year) SQLServerColumn.new ci[:name], ci[:default_value], ci[:type], ci[:null], end end |
#create_table(table_name, options = {}) ⇒ Object
50 51 52 53 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 50 def create_table(table_name, = {}) super remove_sqlserver_columns_cache_for(table_name) end |
#drop_table(table_name, options = {}) ⇒ Object
59 60 61 62 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 59 def drop_table(table_name, = {}) super remove_sqlserver_columns_cache_for(table_name) end |
#indexes(table_name, name = nil) ⇒ Object
21 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 21 def indexes(table_name, name = nil) unquoted_table_name = unqualify_table_name(table_name) data = select("EXEC sp_helpindex #{quote_table_name(unquoted_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_types ⇒ Object
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
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 69 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 remove_sqlserver_columns_cache_for(table_name) end |
#remove_index!(table_name, index_name) ⇒ Object
109 110 111 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 109 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
103 104 105 106 107 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 103 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'" remove_sqlserver_columns_cache_for(table_name) end |
#rename_table(table_name, new_name) ⇒ Object
55 56 57 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 55 def rename_table(table_name, new_name) do_execute "EXEC sp_rename '#{table_name}', '#{new_name}'" end |
#table_exists?(table_name) ⇒ Boolean
16 17 18 19 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 16 def table_exists?(table_name) unquoted_table_name = unqualify_table_name(table_name) super || tables.include?(unquoted_table_name) || views.include?(unquoted_table_name) end |
#tables(name = nil) ⇒ Object
10 11 12 13 14 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 10 def tables(name = nil) info_schema_query do select_values "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties' AND TABLE_SCHEMA = schema_name()" end end |
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 113 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 |
#views(name = nil) ⇒ Object
SQLServer Specific ======================================== #
141 142 143 144 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 141 def views(name = nil) @sqlserver_views_cache ||= info_schema_query { select_values("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME NOT IN ('sysconstraints','syssegments')") } end |