schema_definitions.rb 9.3 KB
Newer Older
1
require 'date'
2

3 4
module ActiveRecord
  module ConnectionAdapters #:nodoc:
5 6
    # An abstract definition of a column in a table.
    class Column
7
      attr_reader :name, :default, :type, :limit, :null, :sql_type
8
      attr_accessor :primary
9 10 11 12 13 14 15

      # Instantiates a new column in the table.
      #
      # +name+ is the column's name, as in <tt><b>supplier_id</b> int(11)</tt>.
      # +default+ is the type-casted default value, such as <tt>sales_stage varchar(20) default <b>'new'</b></tt>.
      # +sql_type+ is only used to extract the column's length, if necessary.  For example, <tt>company_name varchar(<b>60</b>)</tt>.
      # +null+ determines if this column allows +NULL+ values.
16
      def initialize(name, default, sql_type = nil, null = true)
17 18 19 20
        @name, @sql_type, @null, @limit = name, sql_type, null, extract_limit(sql_type)

        # simplified_type may depend on #limit, type_cast depends on #type
        @type = simplified_type(sql_type)
21
        @default = type_cast(default)
22

23 24 25 26 27 28 29 30 31 32 33
        @primary = nil
        @text    = [:string, :text].include? @type
        @number  = [:float, :integer].include? @type
      end

      def text?
        @text
      end

      def number?
        @number
34 35
      end

36
      # Returns the Ruby class that corresponds to the abstract data type.
37 38 39 40 41 42 43 44 45 46 47 48 49 50
      def klass
        case type
          when :integer       then Fixnum
          when :float         then Float
          when :datetime      then Time
          when :date          then Date
          when :timestamp     then Time
          when :time          then Time
          when :text, :string then String
          when :binary        then String
          when :boolean       then Object
        end
      end

51
      # Casts value (which is a String) to an appropriate instance.
52
      def type_cast(value)
53
        return nil if value.nil?
54 55 56 57 58
        case type
          when :string    then value
          when :text      then value
          when :integer   then value.to_i rescue value ? 1 : 0
          when :float     then value.to_f
59 60 61 62 63
          when :datetime  then self.class.string_to_time(value)
          when :timestamp then self.class.string_to_time(value)
          when :time      then self.class.string_to_dummy_time(value)
          when :date      then self.class.string_to_date(value)
          when :binary    then self.class.binary_to_string(value)
64
          when :boolean   then self.class.value_to_boolean(value)
65 66 67 68
          else value
        end
      end

69 70 71 72 73 74 75 76 77 78 79
      def type_cast_code(var_name)
        case type
          when :string    then nil
          when :text      then nil
          when :integer   then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
          when :float     then "#{var_name}.to_f"
          when :datetime  then "#{self.class.name}.string_to_time(#{var_name})"
          when :timestamp then "#{self.class.name}.string_to_time(#{var_name})"
          when :time      then "#{self.class.name}.string_to_dummy_time(#{var_name})"
          when :date      then "#{self.class.name}.string_to_date(#{var_name})"
          when :binary    then "#{self.class.name}.binary_to_string(#{var_name})"
80
          when :boolean   then "#{self.class.name}.value_to_boolean(#{var_name})"
81 82 83 84
          else nil
        end
      end

85 86 87 88
      # Returns the human name of the column name.
      #
      # ===== Examples
      #  Column.new('sales_stage', ...).human_name #=> 'Sales stage'
89 90 91 92
      def human_name
        Base.human_attribute_name(@name)
      end

93
      # Used to convert from Strings to BLOBs
94
      def self.string_to_binary(value)
95 96 97
        value
      end

98
      # Used to convert from BLOBs to Strings
99
      def self.binary_to_string(value)
100 101 102
        value
      end

103 104 105 106 107 108
      def self.string_to_date(string)
        return string unless string.is_a?(String)
        date_array = ParseDate.parsedate(string)
        # treat 0000-00-00 as nil
        Date.new(date_array[0], date_array[1], date_array[2]) rescue nil
      end
109

110 111
      def self.string_to_time(string)
        return string unless string.is_a?(String)
112 113 114
        time_hash = Date._parse(string)
        time_hash[:sec_fraction] = microseconds(time_hash)
        time_array = time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)
115
        # treat 0000-00-00 00:00:00 as nil
116
        Time.send(Base.default_timezone, *time_array) rescue DateTime.new(*time_array[0..5]) rescue nil
117
      end
118

119 120
      def self.string_to_dummy_time(string)
        return string unless string.is_a?(String)
121
        return nil if string.empty?
122 123
        time_hash = Date._parse(string)
        time_hash[:sec_fraction] = microseconds(time_hash)
124
        # pad the resulting array with dummy date information
125 126
        time_array = [2000, 1, 1]
        time_array += time_hash.values_at(:hour, :min, :sec, :sec_fraction)
127 128
        Time.send(Base.default_timezone, *time_array) rescue nil
      end
129

130 131 132 133 134 135 136 137 138
      # convert something to a boolean
      def self.value_to_boolean(value)
        return value if value==true || value==false
        case value.to_s.downcase
        when "true", "t", "1" then true
        else false
        end
      end

139 140 141 142 143 144 145
      private
        # '0.123456' -> 123456
        # '1.123456' -> 123456
        def self.microseconds(time)
          ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i
        end

146
        def extract_limit(sql_type)
147
          return unless sql_type
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
          $1.to_i if sql_type =~ /\((.*)\)/
        end

        def simplified_type(field_type)
          case field_type
            when /int/i
              :integer
            when /float|double|decimal|numeric/i
              :float
            when /datetime/i
              :datetime
            when /timestamp/i
              :timestamp
            when /time/i
              :time
            when /date/i
              :date
            when /clob/i, /text/i
              :text
            when /blob/i, /binary/i
              :binary
            when /char/i, /string/i
              :string
            when /boolean/i
              :boolean
          end
        end
    end
176

177 178 179 180 181
    class IndexDefinition < Struct.new(:table, :name, :unique, :columns) #:nodoc:
    end

    class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :default, :null) #:nodoc:
      def to_sql
182
        column_sql = "#{base.quote_column_name(name)} #{type_to_sql(type.to_sym, limit)}"
183 184 185 186
        add_column_options!(column_sql, :null => null, :default => default)
        column_sql
      end
      alias to_s :to_sql
187

188 189 190
      private
        def type_to_sql(name, limit)
          base.type_to_sql(name, limit) rescue name
191
        end
192 193 194 195 196 197

        def add_column_options!(sql, options)
          base.add_column_options!(sql, options.merge(:column => self))
        end
    end

198 199 200
    # Represents a SQL table in an abstract way.
    # Columns are stored as ColumnDefinition in the #columns attribute.
    class TableDefinition
201 202 203 204 205 206 207
      attr_accessor :columns

      def initialize(base)
        @columns = []
        @base = base
      end

208 209
      # Appends a primary key definition to the table definition.
      # Can be called multiple times, but this is probably not a good idea.
210 211 212
      def primary_key(name)
        column(name, native[:primary_key])
      end
213 214

      # Returns a ColumnDefinition for the column with name +name+.
215
      def [](name)
216
        @columns.find {|column| column.name.to_s == name.to_s}
217 218
      end

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      # Instantiates a new column for the table.
      # The +type+ parameter must be one of the following values:
      # <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>,
      # <tt>:integer</tt>, <tt>:float</tt>, <tt>:datetime</tt>,
      # <tt>:timestamp</tt>, <tt>:time</tt>, <tt>:date</tt>,
      # <tt>:binary</tt>, <tt>:boolean</tt>.
      #
      # Available options are (none of these exists by default):
      # * <tt>:limit</tt>:
      #   Requests a maximum column length (<tt>:string</tt>, <tt>:text</tt>,
      #   <tt>:binary</tt> or <tt>:integer</tt> columns only)
      # * <tt>:default</tt>:
      #   The column's default value.  You cannot explicitely set the default
      #   value to +NULL+.  Simply leave off this option if you want a +NULL+
      #   default value.
      # * <tt>:null</tt>:
      #   Allows or disallows +NULL+ values in the column.  This option could
      #   have been named <tt>:null_allowed</tt>.
      #
      # This method returns <tt>self</tt>.
      #
      # ===== Examples
241 242
      #  # Assuming td is an instance of TableDefinition
      #  td.column(:granted, :boolean)
243 244
      #    #=> granted BOOLEAN
      #
245
      #  td.column(:picture, :binary, :limit => 2.megabytes)
246 247
      #    #=> picture BLOB(2097152)
      #
248
      #  td.column(:sales_stage, :string, :limit => 20, :default => 'new', :null => false)
249
      #    #=> sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL
250 251 252 253 254 255 256 257
      def column(name, type, options = {})
        column = self[name] || ColumnDefinition.new(@base, name, type)
        column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym]
        column.default = options[:default]
        column.null = options[:null]
        @columns << column unless @columns.include? column
        self
      end
258

259 260 261
      # Returns a String whose contents are the column definitions
      # concatenated together.  This string can then be pre and appended to
      # to generate the final SQL to create the table.
262 263 264
      def to_sql
        @columns * ', '
      end
265

266 267 268 269 270 271
      private
        def native
          @base.native_database_types
        end
    end
  end
272
end