schema_definitions.rb 17.1 KB
Newer Older
1
require 'date'
2 3
require 'bigdecimal'
require 'bigdecimal/util'
4

5 6
module ActiveRecord
  module ConnectionAdapters #:nodoc:
7 8
    # An abstract definition of a column in a table.
    class Column
9 10 11 12 13
      module Format
        ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/
        ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
      end

14
      attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
15
      attr_accessor :primary
16 17 18 19 20 21 22

      # 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.
23
      def initialize(name, default, sql_type = nil, null = true)
24 25
        @name, @sql_type, @null = name, sql_type, null
        @limit, @precision, @scale  = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type) 
26
        @type = simplified_type(sql_type)
27
        @default = extract_default(default)
28

29 30 31 32
        @primary = nil
      end

      def text?
J
Jeremy Kemper 已提交
33
        [:string, :text].include? type
34 35 36
      end

      def number?
37
        [:float, :integer, :decimal].include? type
38 39
      end

40
      # Returns the Ruby class that corresponds to the abstract data type.
41 42 43 44
      def klass
        case type
          when :integer       then Fixnum
          when :float         then Float
45
          when :decimal       then BigDecimal
46 47 48 49 50 51 52 53 54 55
          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

56
      # Casts value (which is a String) to an appropriate instance.
57
      def type_cast(value)
58
        return nil if value.nil?
59 60 61 62 63
        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
64
          when :decimal   then self.class.value_to_decimal(value)
65 66 67 68 69
          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)
70
          when :boolean   then self.class.value_to_boolean(value)
71 72 73 74
          else value
        end
      end

75 76 77 78 79 80
      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"
81
          when :decimal   then "#{self.class.name}.value_to_decimal(#{var_name})"
82 83 84 85 86
          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})"
87
          when :boolean   then "#{self.class.name}.value_to_boolean(#{var_name})"
88 89 90 91
          else nil
        end
      end

92 93 94 95
      # Returns the human name of the column name.
      #
      # ===== Examples
      #  Column.new('sales_stage', ...).human_name #=> 'Sales stage'
96 97 98 99
      def human_name
        Base.human_attribute_name(@name)
      end

100 101 102 103
      def extract_default(default)
        type_cast(default)
      end

J
Jeremy Kemper 已提交
104 105 106 107 108
      class << self
        # Used to convert from Strings to BLOBs
        def string_to_binary(value)
          value
        end
109

J
Jeremy Kemper 已提交
110 111 112 113
        # Used to convert from BLOBs to Strings
        def binary_to_string(value)
          value
        end
114

J
Jeremy Kemper 已提交
115 116
        def string_to_date(string)
          return string unless string.is_a?(String)
117
          return nil if string.empty?
118

119
          fast_string_to_date(string) || fallback_string_to_date(string)
J
Jeremy Kemper 已提交
120
        end
121

J
Jeremy Kemper 已提交
122 123 124
        def string_to_time(string)
          return string unless string.is_a?(String)
          return nil if string.empty?
125

126
          fast_string_to_time(string) || fallback_string_to_time(string)
127
        end
128

J
Jeremy Kemper 已提交
129 130 131
        def string_to_dummy_time(string)
          return string unless string.is_a?(String)
          return nil if string.empty?
132

J
Jeremy Kemper 已提交
133
          string_to_time "2000-01-01 #{string}"
134 135
        end

J
Jeremy Kemper 已提交
136 137 138 139 140 141 142
        # convert something to a boolean
        def value_to_boolean(value)
          if value == true || value == false
            value
          else
            %w(true t 1).include?(value.to_s.downcase)
          end
143 144
        end

J
Jeremy Kemper 已提交
145 146 147 148 149 150 151 152 153
        # convert something to a BigDecimal
        def value_to_decimal(value)
          if value.is_a?(BigDecimal)
            value
          elsif value.respond_to?(:to_d)
            value.to_d
          else
            value.to_s.to_d
          end
154 155
        end

J
Jeremy Kemper 已提交
156 157 158 159 160 161 162 163
        protected
          # '0.123456' -> 123456
          # '1.123456' -> 123456
          def microseconds(time)
            ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i
          end

          def new_date(year, mon, mday)
164 165 166
            if year && year != 0
              Date.new(year, mon, mday) rescue nil
            end
J
Jeremy Kemper 已提交
167 168 169 170
          end

          def new_time(year, mon, mday, hour, min, sec, microsec)
            # Treat 0000-00-00 00:00:00 as nil.
171
            return nil if year.nil? || year == 0
J
Jeremy Kemper 已提交
172 173 174 175

            Time.send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec)
          # Over/underflow to DateTime
          rescue ArgumentError, TypeError
176
            zone_offset = Base.default_timezone == :local ? DateTime.local_offset : 0
177
            DateTime.civil(year, mon, mday, hour, min, sec, zone_offset) rescue nil
J
Jeremy Kemper 已提交
178
          end
179 180 181 182 183 184 185 186 187 188

          def fast_string_to_date(string)
            if string =~ Format::ISO_DATE
              new_date $1.to_i, $2.to_i, $3.to_i
            end
          end

          # Doesn't handle time zones.
          def fast_string_to_time(string)
            if string =~ Format::ISO_DATETIME
J
Jeremy Kemper 已提交
189
              microsec = ($7.to_f * 1_000_000).to_i
190 191 192 193 194
              new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
            end
          end

          def fallback_string_to_date(string)
195
            new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
196 197 198 199 200 201
          end

          def fallback_string_to_time(string)
            time_hash = Date._parse(string)
            time_hash[:sec_fraction] = microseconds(time_hash)

202
            new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
203
          end
J
Jeremy Kemper 已提交
204 205 206
      end

      private
207 208 209 210
        def extract_limit(sql_type)
          $1.to_i if sql_type =~ /\((.*)\)/
        end

211
        def extract_precision(sql_type)
212
          $2.to_i if sql_type =~ /^(numeric|decimal|number)\((\d+)(,\d+)?\)/i
213 214 215 216
        end

        def extract_scale(sql_type)
          case sql_type
217 218
            when /^(numeric|decimal|number)\((\d+)\)/i then 0
            when /^(numeric|decimal|number)\((\d+)(,(\d+))\)/i then $4.to_i
219 220 221
          end
        end

222 223 224 225
        def simplified_type(field_type)
          case field_type
            when /int/i
              :integer
226
            when /float|double/i
227
              :float
228
            when /decimal|numeric|number/i
229
              extract_scale(field_type) == 0 ? :integer : :decimal
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            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
249

250 251 252
    class IndexDefinition < Struct.new(:table, :name, :unique, :columns) #:nodoc:
    end

253
    class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :precision, :scale, :default, :null) #:nodoc:
254 255 256 257 258
      
      def sql_type
        base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
      end
      
259
      def to_sql
260 261
        column_sql = "#{base.quote_column_name(name)} #{sql_type}"
        add_column_options!(column_sql, :null => null, :default => default) unless type.to_sym == :primary_key
262 263 264
        column_sql
      end
      alias to_s :to_sql
265

266 267 268 269 270 271 272
      private

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

273
    # Represents a SQL table in an abstract way.
274
    # Columns are stored as a ColumnDefinition in the #columns attribute.
275
    class TableDefinition
276 277 278 279 280 281 282
      attr_accessor :columns

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

283 284
      # Appends a primary key definition to the table definition.
      # Can be called multiple times, but this is probably not a good idea.
285
      def primary_key(name)
286
        column(name, :primary_key)
287
      end
288 289

      # Returns a ColumnDefinition for the column with name +name+.
290
      def [](name)
291
        @columns.find {|column| column.name.to_s == name.to_s}
292 293
      end

294
      # Instantiates a new column for the table.
295 296
      # The +type+ parameter is normally one of the migrations native types,
      # which is one of the following:
297
      # <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>,
298 299 300
      # <tt>:integer</tt>, <tt>:float</tt>, <tt>:decimal</tt>,
      # <tt>:datetime</tt>, <tt>:timestamp</tt>, <tt>:time</tt>,
      # <tt>:date</tt>, <tt>:binary</tt>, <tt>:boolean</tt>.
301
      #
302 303 304 305
      # You may use a type not in this list as long as it is supported by your
      # database (for example, "polygon" in MySQL), but this will not be database
      # agnostic and should usually be avoided.
      #
306
      # Available options are (none of these exists by default):
307
      # * <tt>:limit</tt> -
308 309
      #   Requests a maximum column length (<tt>:string</tt>, <tt>:text</tt>,
      #   <tt>:binary</tt> or <tt>:integer</tt> columns only)
310
      # * <tt>:default</tt> -
311
      #   The column's default value. Use nil for NULL.
312
      # * <tt>:null</tt> -
313 314
      #   Allows or disallows +NULL+ values in the column.  This option could
      #   have been named <tt>:null_allowed</tt>.
315
      # * <tt>:precision</tt> -
316
      #   Specifies the precision for a <tt>:decimal</tt> column. 
317
      # * <tt>:scale</tt> -
318 319 320 321 322 323 324 325 326
      #   Specifies the scale for a <tt>:decimal</tt> column. 
      #
      # Please be aware of different RDBMS implementations behavior with
      # <tt>:decimal</tt> columns:
      # * The SQL standard says the default scale should be 0, <tt>:scale</tt> <=
      #   <tt>:precision</tt>, and makes no comments about the requirements of
      #   <tt>:precision</tt>.
      # * MySQL: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..30]. 
      #   Default is (10,0).
327
      # * PostgreSQL: <tt>:precision</tt> [1..infinity], 
328
      #   <tt>:scale</tt> [0..infinity]. No default.
329
      # * SQLite2: Any <tt>:precision</tt> and <tt>:scale</tt> may be used. 
330
      #   Internal storage as strings. No default.
331
      # * SQLite3: No restrictions on <tt>:precision</tt> and <tt>:scale</tt>,
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
      #   but the maximum supported <tt>:precision</tt> is 16. No default.
      # * Oracle: <tt>:precision</tt> [1..38], <tt>:scale</tt> [-84..127]. 
      #   Default is (38,0).
      # * DB2: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..62]. 
      #   Default unknown.
      # * Firebird: <tt>:precision</tt> [1..18], <tt>:scale</tt> [0..18]. 
      #   Default (9,0). Internal types NUMERIC and DECIMAL have different
      #   storage rules, decimal being better.
      # * FrontBase?: <tt>:precision</tt> [1..38], <tt>:scale</tt> [0..38]. 
      #   Default (38,0). WARNING Max <tt>:precision</tt>/<tt>:scale</tt> for
      #   NUMERIC is 19, and DECIMAL is 38.
      # * SqlServer?: <tt>:precision</tt> [1..38], <tt>:scale</tt> [0..38]. 
      #   Default (38,0).
      # * Sybase: <tt>:precision</tt> [1..38], <tt>:scale</tt> [0..38]. 
      #   Default (38,0).
      # * OpenBase?: Documentation unclear. Claims storage in <tt>double</tt>.
348 349 350
      #
      # This method returns <tt>self</tt>.
      #
351
      # == Examples
352 353
      #  # Assuming td is an instance of TableDefinition
      #  td.column(:granted, :boolean)
354 355
      #    #=> granted BOOLEAN
      #
356
      #  td.column(:picture, :binary, :limit => 2.megabytes)
357 358
      #    #=> picture BLOB(2097152)
      #
359
      #  td.column(:sales_stage, :string, :limit => 20, :default => 'new', :null => false)
360
      #    #=> sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL
361 362 363 364 365 366 367 368 369 370 371
      #
      #  def.column(:bill_gates_money, :decimal, :precision => 15, :scale => 2)
      #    #=> bill_gates_money DECIMAL(15,2)
      #
      #  def.column(:sensor_reading, :decimal, :precision => 30, :scale => 20)
      #    #=> sensor_reading DECIMAL(30,20)
      #
      #  # While <tt>:scale</tt> defaults to zero on most databases, it
      #  # probably wouldn't hurt to include it.
      #  def.column(:huge_integer, :decimal, :precision => 30)
      #    #=> huge_integer DECIMAL(30)
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
      #
      # == Short-hand examples
      #
      # Instead of calling column directly, you can also work with the short-hand definitions for the default types.
      # They use the type as the method name instead of as a parameter and allow for multiple columns to be defined
      # in a single statement.
      #
      # What can be written like this with the regular calls to column:
      #
      #   create_table "products", :force => true do |t|
      #     t.column "shop_id",    :integer
      #     t.column "creator_id", :integer
      #     t.column "name",       :string,   :default => "Untitled"
      #     t.column "value",      :string,   :default => "Untitled"
      #     t.column "created_at", :datetime
      #     t.column "updated_at", :datetime
      #   end
      #
      # Can also be written as follows using the short-hand:
      #
      #   create_table :products do |t|
      #     t.integer :shop_id, :creator_id
      #     t.string  :name, :value, :default => "Untitled"
      #     t.timestamps
      #   end
      #
      # There's a short-hand method for each of the type values declared at the top. And then there's 
      # TableDefinition#timestamps that'll add created_at and updated_at as datetimes.
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
      #
      # TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type
      # column if the :polymorphic option is supplied. If :polymorphic is a hash of options, these will be
      # used when creating the _type column. So what can be written like this:
      #
      #   create_table :taggings do |t|
      #     t.integer :tag_id, :tagger_id, :taggable_id
      #     t.string  :tagger_type
      #     t.string  :taggable_type, :default => 'Photo'
      #   end
      #
      # Can also be written as follows using references:
      #
      #   create_table :taggings do |t|
      #     t.references :tag
      #     t.references :tagger, :polymorphic => true
      #     t.references :taggable, :polymorphic => { :default => 'Photo' }
      #   end
418
      def column(name, type, options = {})
419
        column = self[name] || ColumnDefinition.new(@base, name, type)
420 421 422 423 424
        if options[:limit]
          column.limit = options[:limit]
        elsif native[type.to_sym].is_a?(Hash)
          column.limit = native[type.to_sym][:limit]
        end
425 426
        column.precision = options[:precision]
        column.scale = options[:scale]
427 428 429 430 431
        column.default = options[:default]
        column.null = options[:null]
        @columns << column unless @columns.include? column
        self
      end
432

433 434 435
      %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
        class_eval <<-EOV
          def #{column_type}(*args)
436
            options = args.extract_options!
437 438 439 440 441 442 443
            column_names = args
            
            column_names.each { |name| column(name, '#{column_type}', options) }
          end
        EOV
      end
      
444 445
      # Appends <tt>:datetime</tt> columns <tt>:created_at</tt> and 
      # <tt>:updated_at</tt> to the table.
446 447 448 449 450
      def timestamps
        column(:created_at, :datetime)
        column(:updated_at, :datetime)
      end

451 452 453 454 455
      def references(*args)
        options = args.extract_options!
        polymorphic = options.delete(:polymorphic)
        args.each do |col|
          column("#{col}_id", :integer, options)
456
          column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
457 458 459 460
        end
      end
      alias :belongs_to :references

461
      # Returns a String whose contents are the column definitions
462
      # concatenated together.  This string can then be prepended and appended to
463
      # to generate the final SQL to create the table.
464 465 466
      def to_sql
        @columns * ', '
      end
467

468 469 470 471 472 473
      private
        def native
          @base.native_database_types
        end
    end
  end
474
end