schema_creation.rb 4.5 KB
Newer Older
1 2
require 'active_support/core_ext/string/strip'

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
module ActiveRecord
  module ConnectionAdapters
    class AbstractAdapter
      class SchemaCreation # :nodoc:
        def initialize(conn)
          @conn = conn
          @cache = {}
        end

        def accept(o)
          m = @cache[o.class] ||= "visit_#{o.class.name.split('::').last}"
          send m, o
        end

        def visit_AddColumn(o)
18
          "ADD #{accept(o)}"
19 20 21 22 23 24 25
        end

        private

          def visit_AlterTable(o)
            sql = "ALTER TABLE #{quote_table_name(o.name)} "
            sql << o.adds.map { |col| visit_AddColumn col }.join(' ')
26 27
            sql << o.foreign_key_adds.map { |fk| visit_AddForeignKey fk }.join(' ')
            sql << o.foreign_key_drops.map { |fk| visit_DropForeignKey fk }.join(' ')
28 29 30
          end

          def visit_ColumnDefinition(o)
31
            sql_type = type_to_sql(o.type, o.limit, o.precision, o.scale)
32
            column_sql = "#{quote_column_name(o.name)} #{sql_type}"
R
Ryuta Kamizono 已提交
33
            add_column_options!(column_sql, column_options(o)) unless o.type == :primary_key
34 35 36 37 38
            column_sql
          end

          def visit_TableDefinition(o)
            create_sql = "CREATE#{' TEMPORARY' if o.temporary} TABLE "
39 40 41 42
            create_sql << "#{quote_table_name(o.name)} "
            create_sql << "(#{o.columns.map { |c| accept c }.join(', ')}) " unless o.as
            create_sql << "#{o.options}"
            create_sql << " AS #{@conn.to_sql(o.as)}" if o.as
43 44 45
            create_sql
          end

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
          def visit_AddForeignKey(o)
            sql = <<-SQL.strip_heredoc
              ADD CONSTRAINT #{quote_column_name(o.name)}
              FOREIGN KEY (#{quote_column_name(o.column)})
                REFERENCES #{quote_table_name(o.to_table)} (#{quote_column_name(o.primary_key)})
            SQL
            sql << " #{action_sql('DELETE', o.on_delete)}" if o.on_delete
            sql << " #{action_sql('UPDATE', o.on_update)}" if o.on_update
            sql
          end

          def visit_DropForeignKey(name)
            "DROP CONSTRAINT #{quote_column_name(name)}"
          end

61 62 63 64 65 66 67
          def column_options(o)
            column_options = {}
            column_options[:null] = o.null unless o.null.nil?
            column_options[:default] = o.default unless o.default.nil?
            column_options[:column] = o
            column_options[:first] = o.first
            column_options[:after] = o.after
68
            column_options[:auto_increment] = o.auto_increment
R
Ryuta Kamizono 已提交
69
            column_options[:primary_key] = o.primary_key
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            column_options
          end

          def quote_column_name(name)
            @conn.quote_column_name name
          end

          def quote_table_name(name)
            @conn.quote_table_name name
          end

          def type_to_sql(type, limit, precision, scale)
            @conn.type_to_sql type.to_sym, limit, precision, scale
          end

          def add_column_options!(sql, options)
86
            sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
87 88 89 90 91 92 93
            # must explicitly check for :null to allow change_column to work on migrations
            if options[:null] == false
              sql << " NOT NULL"
            end
            if options[:auto_increment] == true
              sql << " AUTO_INCREMENT"
            end
R
Ryuta Kamizono 已提交
94 95 96
            if options[:primary_key] == true
              sql << " PRIMARY KEY"
            end
97 98 99
            sql
          end

100
          def quote_default_expression(value, column)
101
            column.sql_type ||= type_to_sql(column.type, column.limit, column.precision, column.scale)
102
            value = type_for_column(column).type_cast_for_database(value)
103

104
            @conn.quote(value)
105 106
          end

107 108 109
          def options_include_default?(options)
            options.include?(:default) && !(options[:null] == false && options[:default].nil?)
          end
110

Y
Yves Senn 已提交
111
          def action_sql(action, dependency)
112
            case dependency
113 114 115 116
            when :nullify then "ON #{action} SET NULL"
            when :cascade  then "ON #{action} CASCADE"
            when :restrict then "ON #{action} RESTRICT"
            else
117 118 119
              raise ArgumentError, <<-MSG.strip_heredoc
                '#{dependency}' is not supported for :on_update or :on_delete.
                Supported values are: :nullify, :cascade, :restrict
120
              MSG
121 122
            end
          end
123 124 125 126

          def type_for_column(column)
            @conn.lookup_cast_type(column.sql_type)
          end
127 128 129 130
      end
    end
  end
end