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
- #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
- #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(table_type = 'BASE TABLE') ⇒ Object
- #type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
-
#views ⇒ Object
SQLServer Specific ======================================== #.
Instance Method Details
#change_column(table_name, column_name, type, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 62 def change_column(table_name, column_name, type, = {}) 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, [: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 () 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) } end |
#change_column_default(table_name, column_name, default) ⇒ Object
77 78 79 80 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 77 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
107 108 109 110 111 112 113 114 115 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 107 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
39 40 41 42 43 44 45 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 39 def columns(table_name, name = nil) return [] if table_name.blank? 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 |
#indexes(table_name, name = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 20 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_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
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 51 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? ActiveSupport::Deprecation.warn 'Passing array to remove_columns is deprecated, please use multiple arguments, like: `remove_columns(:posts, :foo, :bar)`', caller if column_names.flatten! 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
87 88 89 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 87 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
82 83 84 85 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 82 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
47 48 49 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 47 def rename_table(table_name, new_name) do_execute "EXEC sp_rename '#{table_name}', '#{new_name}'" end |
#table_exists?(table_name) ⇒ Boolean
14 15 16 17 18 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 14 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 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 10 def tables(table_type = 'BASE TABLE') 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", 'SCHEMA' end |
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 91 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 ⇒ Object
SQLServer Specific ======================================== #
119 120 121 |
# File 'lib/active_record/connection_adapters/sqlserver/schema_statements.rb', line 119 def views tables('VIEW') end |