· 8 years ago · Jun 18, 2018, 08:02 AM
1require 'date'
2require 'set'
3require 'bigdecimal'
4require 'bigdecimal/util'
5
6module ActiveRecord
7 module ConnectionAdapters #:nodoc:
8 # An abstract definition of a column in a table.
9 class Column
10 TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
11 FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
12
13 module Format
14 ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/
15 ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
16 end
17
18 attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
19 attr_accessor :primary
20
21 # Instantiates a new column in the table.
22 #
23 # +name+ is the column's name, such as <tt>supplier_id</tt> in <tt>supplier_id int(11)</tt>.
24 # +default+ is the type-casted default value, such as +new+ in <tt>sales_stage varchar(20) default 'new'</tt>.
25 # +sql_type+ is only used to extract the column's length, if necessary. For example +60+ in <tt>company_name varchar(60)</tt>.
26 # +null+ determines if this column allows +NULL+ values.
27 def initialize(name, default, sql_type = nil, null = true)
28 @name, @sql_type, @null = name, sql_type, null
29 @limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type)
30 @type = simplified_type(sql_type)
31 @default = extract_default(default)
32
33 @primary = nil
34 end
35
36 # Returns +true+ if the column is either of type string or text.
37 def text?
38 type == :string || type == :text
39 end
40
41 # Returns +true+ if the column is either of type integer, float or decimal.
42 def number?
43 type == :integer || type == :float || type == :decimal
44 end
45
46 def has_default?
47 !default.nil?
48 end
49
50 # Returns the Ruby class that corresponds to the abstract data type.
51 def klass
52 case type
53 when :integer then Fixnum
54 when :float then Float
55 when :decimal then BigDecimal
56 when :datetime then Time
57 when :date then Date
58 when :timestamp then Time
59 when :time then Time
60 when :text, :string then String
61 when :binary then String
62 when :boolean then Object
63 end
64 end
65
66 # Casts value (which is a String) to an appropriate instance.
67 def type_cast(value)
68 return nil if value.nil?
69 case type
70 when :string then value
71 when :text then value
72 when :integer then value.to_i rescue value ? 1 : 0
73 when :float then value.to_f
74 when :decimal then self.class.value_to_decimal(value)
75 when :datetime then self.class.string_to_time(value)
76 when :timestamp then self.class.string_to_time(value)
77 when :time then self.class.string_to_dummy_time(value)
78 when :date then self.class.string_to_date(value)
79 when :binary then self.class.binary_to_string(value)
80 when :boolean then self.class.value_to_boolean(value)
81 else value
82 end
83 end
84
85 def type_cast_code(var_name)
86 case type
87 when :string then nil
88 when :text then nil
89 when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
90 when :float then "#{var_name}.to_f"
91 when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})"
92 when :datetime then "#{self.class.name}.string_to_time(#{var_name})"
93 when :timestamp then "#{self.class.name}.string_to_time(#{var_name})"
94 when :time then "#{self.class.name}.string_to_dummy_time(#{var_name})"
95 when :date then "#{self.class.name}.string_to_date(#{var_name})"
96 when :binary then "#{self.class.name}.binary_to_string(#{var_name})"
97 when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
98 else nil
99 end
100 end
101
102 # Returns the human name of the column name.
103 #
104 # ===== Examples
105 # Column.new('sales_stage', ...).human_name # => 'Sales stage'
106 def human_name
107 Base.human_attribute_name(@name)
108 end
109
110 def extract_default(default)
111 type_cast(default)
112 end
113
114 class << self
115 # Used to convert from Strings to BLOBs
116 def string_to_binary(value)
117 value
118 end
119
120 # Used to convert from BLOBs to Strings
121 def binary_to_string(value)
122 value
123 end
124
125 def string_to_date(string)
126 return string unless string.is_a?(String)
127 return nil if string.empty?
128
129 fast_string_to_date(string) || fallback_string_to_date(string)
130 end
131
132 def string_to_time(string)
133 return string unless string.is_a?(String)
134 return nil if string.empty?
135
136 fast_string_to_time(string) || fallback_string_to_time(string)
137 end
138
139 def string_to_dummy_time(string)
140 return string unless string.is_a?(String)
141 return nil if string.empty?
142
143 string_to_time "2000-01-01 #{string}"
144 end
145
146 # convert something to a boolean
147 def value_to_boolean(value)
148 if value.is_a?(String) && value.blank?
149 nil
150 else
151 TRUE_VALUES.include?(value)
152 end
153 end
154
155 # convert something to a BigDecimal
156 def value_to_decimal(value)
157 # Using .class is faster than .is_a? and
158 # subclasses of BigDecimal will be handled
159 # in the else clause
160 if value.class == BigDecimal
161 value
162 elsif value.respond_to?(:to_d)
163 value.to_d
164 else
165 value.to_s.to_d
166 end
167 end
168
169 protected
170 # '0.123456' -> 123456
171 # '1.123456' -> 123456
172 def microseconds(time)
173 ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i
174 end
175
176 def new_date(year, mon, mday)
177 if year && year != 0
178 Date.new(year, mon, mday) rescue nil
179 end
180 end
181
182 def new_time(year, mon, mday, hour, min, sec, microsec)
183 # Treat 0000-00-00 00:00:00 as nil.
184 return nil if year.nil? || year == 0
185
186 Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
187 end
188
189 def fast_string_to_date(string)
190 if string =~ Format::ISO_DATE
191 new_date $1.to_i, $2.to_i, $3.to_i
192 end
193 end
194
195 # Doesn't handle time zones.
196 def fast_string_to_time(string)
197 if string =~ Format::ISO_DATETIME
198 microsec = ($7.to_f * 1_000_000).to_i
199 new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
200 end
201 end
202
203 def fallback_string_to_date(string)
204 new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
205 end
206
207 def fallback_string_to_time(string)
208 time_hash = Date._parse(string)
209 time_hash[:sec_fraction] = microseconds(time_hash)
210
211 new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
212 end
213 end
214
215 private
216 def extract_limit(sql_type)
217 $1.to_i if sql_type =~ /\((.*)\)/
218 end
219
220 def extract_precision(sql_type)
221 $2.to_i if sql_type =~ /^(numeric|decimal|number)\((\d+)(,\d+)?\)/i
222 end
223
224 def extract_scale(sql_type)
225 case sql_type
226 when /^(numeric|decimal|number)\((\d+)\)/i then 0
227 when /^(numeric|decimal|number)\((\d+)(,(\d+))\)/i then $4.to_i
228 end
229 end
230
231 def simplified_type(field_type)
232 case field_type
233 when /int/i
234 :integer
235 when /float|double/i
236 :float
237 when /decimal|numeric|number/i
238 extract_scale(field_type) == 0 ? :integer : :decimal
239 when /datetime/i
240 :datetime
241 when /timestamp/i
242 :timestamp
243 when /time/i
244 :time
245 when /date/i
246 :date
247 when /clob/i, /text/i
248 :text
249 when /blob/i, /binary/i
250 :binary
251 when /char/i, /string/i
252 :string
253 when /boolean/i
254 :boolean
255 end
256 end
257 end
258
259 class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths) #:nodoc:
260 end
261
262 # Abstract representation of a column definition. Instances of this type
263 # are typically created by methods in TableDefinition, and added to the
264 # +columns+ attribute of said TableDefinition object, in order to be used
265 # for generating a number of table creation or table changing SQL statements.
266 class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :precision, :scale, :default, :null) #:nodoc:
267
268 def sql_type
269 base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
270 end
271
272 def to_sql
273 column_sql = "#{base.quote_column_name(name)} #{sql_type}"
274 column_options = {}
275 column_options[:null] = null unless null.nil?
276 column_options[:default] = default unless default.nil?
277 add_column_options!(column_sql, column_options) unless type.to_sym == :primary_key
278 column_sql
279 end
280
281 private
282
283 def add_column_options!(sql, options)
284 base.add_column_options!(sql, options.merge(:column => self))
285 end
286 end
287
288 # Represents the schema of an SQL table in an abstract way. This class
289 # provides methods for manipulating the schema representation.
290 #
291 # Inside migration files, the +t+ object in +create_table+ and
292 # +change_table+ is actually of this type:
293 #
294 # class SomeMigration < ActiveRecord::Migration
295 # def self.up
296 # create_table :foo do |t|
297 # puts t.class # => "ActiveRecord::ConnectionAdapters::TableDefinition"
298 # end
299 # end
300 #
301 # def self.down
302 # ...
303 # end
304 # end
305 #
306 # The table definitions
307 # The Columns are stored as a ColumnDefinition in the +columns+ attribute.
308 class TableDefinition
309 # An array of ColumnDefinition objects, representing the column changes
310 # that have been defined.
311 attr_accessor :columns
312
313 def initialize(base)
314 @columns = []
315 @base = base
316 end
317
318 #Handles non supported datatypes - e.g. XML
319 def method_missing(symbol, *args)
320 if symbol.to_s == 'xml'
321 xml_column_fallback(args)
322 end
323 end
324
325 def xml_column_fallback(*args)
326 case @base.adapter_name.downcase
327 when 'sqlite', 'mysql'
328 options = args.extract_options!
329 column(args[0], :text, options)
330 end
331 end
332 # Appends a primary key definition to the table definition.
333 # Can be called multiple times, but this is probably not a good idea.
334 def primary_key(name)
335 column(name, :primary_key)
336 end
337
338 # Returns a ColumnDefinition for the column with name +name+.
339 def [](name)
340 @columns.find {|column| column.name.to_s == name.to_s}
341 end
342
343 # Instantiates a new column for the table.
344 # The +type+ parameter is normally one of the migrations native types,
345 # which is one of the following:
346 # <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>,
347 # <tt>:integer</tt>, <tt>:float</tt>, <tt>:decimal</tt>,
348 # <tt>:datetime</tt>, <tt>:timestamp</tt>, <tt>:time</tt>,
349 # <tt>:date</tt>, <tt>:binary</tt>, <tt>:boolean</tt>.
350 #
351 # You may use a type not in this list as long as it is supported by your
352 # database (for example, "polygon" in MySQL), but this will not be database
353 # agnostic and should usually be avoided.
354 #
355 # Available options are (none of these exists by default):
356 # * <tt>:limit</tt> -
357 # Requests a maximum column length. This is number of characters for <tt>:string</tt> and <tt>:text</tt> columns and number of bytes for :binary and :integer columns.
358 # * <tt>:default</tt> -
359 # The column's default value. Use nil for NULL.
360 # * <tt>:null</tt> -
361 # Allows or disallows +NULL+ values in the column. This option could
362 # have been named <tt>:null_allowed</tt>.
363 # * <tt>:precision</tt> -
364 # Specifies the precision for a <tt>:decimal</tt> column.
365 # * <tt>:scale</tt> -
366 # Specifies the scale for a <tt>:decimal</tt> column.
367 #
368 # For clarity's sake: the precision is the number of significant digits,
369 # while the scale is the number of digits that can be stored following
370 # the decimal point. For example, the number 123.45 has a precision of 5
371 # and a scale of 2. A decimal with a precision of 5 and a scale of 2 can
372 # range from -999.99 to 999.99.
373 #
374 # Please be aware of different RDBMS implementations behavior with
375 # <tt>:decimal</tt> columns:
376 # * The SQL standard says the default scale should be 0, <tt>:scale</tt> <=
377 # <tt>:precision</tt>, and makes no comments about the requirements of
378 # <tt>:precision</tt>.
379 # * MySQL: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..30].
380 # Default is (10,0).
381 # * PostgreSQL: <tt>:precision</tt> [1..infinity],
382 # <tt>:scale</tt> [0..infinity]. No default.
383 # * SQLite2: Any <tt>:precision</tt> and <tt>:scale</tt> may be used.
384 # Internal storage as strings. No default.
385 # * SQLite3: No restrictions on <tt>:precision</tt> and <tt>:scale</tt>,
386 # but the maximum supported <tt>:precision</tt> is 16. No default.
387 # * Oracle: <tt>:precision</tt> [1..38], <tt>:scale</tt> [-84..127].
388 # Default is (38,0).
389 # * DB2: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..62].
390 # Default unknown.
391 # * Firebird: <tt>:precision</tt> [1..18], <tt>:scale</tt> [0..18].
392 # Default (9,0). Internal types NUMERIC and DECIMAL have different
393 # storage rules, decimal being better.
394 # * FrontBase?: <tt>:precision</tt> [1..38], <tt>:scale</tt> [0..38].
395 # Default (38,0). WARNING Max <tt>:precision</tt>/<tt>:scale</tt> for
396 # NUMERIC is 19, and DECIMAL is 38.
397 # * SqlServer?: <tt>:precision</tt> [1..38], <tt>:scale</tt> [0..38].
398 # Default (38,0).
399 # * Sybase: <tt>:precision</tt> [1..38], <tt>:scale</tt> [0..38].
400 # Default (38,0).
401 # * OpenBase?: Documentation unclear. Claims storage in <tt>double</tt>.
402 #
403 # This method returns <tt>self</tt>.
404 #
405 # == Examples
406 # # Assuming td is an instance of TableDefinition
407 # td.column(:granted, :boolean)
408 # # granted BOOLEAN
409 #
410 # td.column(:picture, :binary, :limit => 2.megabytes)
411 # # => picture BLOB(2097152)
412 #
413 # td.column(:sales_stage, :string, :limit => 20, :default => 'new', :null => false)
414 # # => sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL
415 #
416 # td.column(:bill_gates_money, :decimal, :precision => 15, :scale => 2)
417 # # => bill_gates_money DECIMAL(15,2)
418 #
419 # td.column(:sensor_reading, :decimal, :precision => 30, :scale => 20)
420 # # => sensor_reading DECIMAL(30,20)
421 #
422 # # While <tt>:scale</tt> defaults to zero on most databases, it
423 # # probably wouldn't hurt to include it.
424 # td.column(:huge_integer, :decimal, :precision => 30)
425 # # => huge_integer DECIMAL(30)
426 #
427 # # Defines a column with a database-specific type.
428 # td.column(:foo, 'polygon')
429 # # => foo polygon
430 #
431 # == Short-hand examples
432 #
433 # Instead of calling +column+ directly, you can also work with the short-hand definitions for the default types.
434 # They use the type as the method name instead of as a parameter and allow for multiple columns to be defined
435 # in a single statement.
436 #
437 # What can be written like this with the regular calls to column:
438 #
439 # create_table "products", :force => true do |t|
440 # t.column "shop_id", :integer
441 # t.column "creator_id", :integer
442 # t.column "name", :string, :default => "Untitled"
443 # t.column "value", :string, :default => "Untitled"
444 # t.column "created_at", :datetime
445 # t.column "updated_at", :datetime
446 # end
447 #
448 # Can also be written as follows using the short-hand:
449 #
450 # create_table :products do |t|
451 # t.integer :shop_id, :creator_id
452 # t.string :name, :value, :default => "Untitled"
453 # t.timestamps
454 # end
455 #
456 # There's a short-hand method for each of the type values declared at the top. And then there's
457 # TableDefinition#timestamps that'll add created_at and +updated_at+ as datetimes.
458 #
459 # TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type
460 # column if the <tt>:polymorphic</tt> option is supplied. If <tt>:polymorphic</tt> is a hash of options, these will be
461 # used when creating the <tt>_type</tt> column. So what can be written like this:
462 #
463 # create_table :taggings do |t|
464 # t.integer :tag_id, :tagger_id, :taggable_id
465 # t.string :tagger_type
466 # t.string :taggable_type, :default => 'Photo'
467 # end
468 #
469 # Can also be written as follows using references:
470 #
471 # create_table :taggings do |t|
472 # t.references :tag
473 # t.references :tagger, :polymorphic => true
474 # t.references :taggable, :polymorphic => { :default => 'Photo' }
475 # end
476 def column(name, type, options = {})
477 column = self[name] || ColumnDefinition.new(@base, name, type)
478 if options[:limit]
479 column.limit = options[:limit]
480 elsif native[type.to_sym].is_a?(Hash)
481 column.limit = native[type.to_sym][:limit]
482 end
483 column.precision = options[:precision]
484 column.scale = options[:scale]
485 column.default = options[:default]
486 column.null = options[:null]
487 @columns << column unless @columns.include? column
488 self
489 end
490
491 %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
492 class_eval <<-EOV
493 def #{column_type}(*args) # def string(*args)
494 options = args.extract_options! # options = args.extract_options!
495 column_names = args # column_names = args
496 #
497 column_names.each { |name| column(name, '#{column_type}', options) } # column_names.each { |name| column(name, 'string', options) }
498 end # end
499 EOV
500 end
501
502 # Appends <tt>:datetime</tt> columns <tt>:created_at</tt> and
503 # <tt>:updated_at</tt> to the table.
504 def timestamps(*args)
505 options = args.extract_options!
506 column(:created_at, :datetime, options)
507 column(:updated_at, :datetime, options)
508 end
509
510 def references(*args)
511 options = args.extract_options!
512 polymorphic = options.delete(:polymorphic)
513 args.each do |col|
514 column("#{col}_id", :integer, options)
515 column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
516 end
517 end
518 alias :belongs_to :references
519
520 # Returns a String whose contents are the column definitions
521 # concatenated together. This string can then be prepended and appended to
522 # to generate the final SQL to create the table.
523 def to_sql
524 @columns.map(&:to_sql) * ', '
525 end
526
527 private
528 def native
529 @base.native_database_types
530 end
531 end
532
533 # Represents a SQL table in an abstract way for updating a table.
534 # Also see TableDefinition and SchemaStatements#create_table
535 #
536 # Available transformations are:
537 #
538 # change_table :table do |t|
539 # t.column
540 # t.index
541 # t.timestamps
542 # t.change
543 # t.change_default
544 # t.rename
545 # t.references
546 # t.belongs_to
547 # t.string
548 # t.text
549 # t.integer
550 # t.float
551 # t.decimal
552 # t.datetime
553 # t.timestamp
554 # t.time
555 # t.date
556 # t.binary
557 # t.boolean
558 # t.remove
559 # t.remove_references
560 # t.remove_belongs_to
561 # t.remove_index
562 # t.remove_timestamps
563 # end
564 #
565 class Table
566 def initialize(table_name, base)
567 @table_name = table_name
568 @base = base
569 end
570
571 # Adds a new column to the named table.
572 # See TableDefinition#column for details of the options you can use.
573 # ===== Example
574 # ====== Creating a simple column
575 # t.column(:name, :string)
576 def column(column_name, type, options = {})
577 @base.add_column(@table_name, column_name, type, options)
578 end
579
580 # Adds a new index to the table. +column_name+ can be a single Symbol, or
581 # an Array of Symbols. See SchemaStatements#add_index
582 #
583 # ===== Examples
584 # ====== Creating a simple index
585 # t.index(:name)
586 # ====== Creating a unique index
587 # t.index([:branch_id, :party_id], :unique => true)
588 # ====== Creating a named index
589 # t.index([:branch_id, :party_id], :unique => true, :name => 'by_branch_party')
590 def index(column_name, options = {})
591 @base.add_index(@table_name, column_name, options)
592 end
593
594 # Adds timestamps (created_at and updated_at) columns to the table. See SchemaStatements#add_timestamps
595 # ===== Example
596 # t.timestamps
597 def timestamps
598 @base.add_timestamps(@table_name)
599 end
600
601 # Changes the column's definition according to the new options.
602 # See TableDefinition#column for details of the options you can use.
603 # ===== Examples
604 # t.change(:name, :string, :limit => 80)
605 # t.change(:description, :text)
606 def change(column_name, type, options = {})
607 @base.change_column(@table_name, column_name, type, options)
608 end
609
610 # Sets a new default value for a column. See SchemaStatements#change_column_default
611 # ===== Examples
612 # t.change_default(:qualification, 'new')
613 # t.change_default(:authorized, 1)
614 def change_default(column_name, default)
615 @base.change_column_default(@table_name, column_name, default)
616 end
617
618 # Removes the column(s) from the table definition.
619 # ===== Examples
620 # t.remove(:qualification)
621 # t.remove(:qualification, :experience)
622 def remove(*column_names)
623 @base.remove_column(@table_name, column_names)
624 end
625
626 # Removes the given index from the table.
627 #
628 # ===== Examples
629 # ====== Remove the suppliers_name_index in the suppliers table
630 # t.remove_index :name
631 # ====== Remove the index named accounts_branch_id_index in the accounts table
632 # t.remove_index :column => :branch_id
633 # ====== Remove the index named accounts_branch_id_party_id_index in the accounts table
634 # t.remove_index :column => [:branch_id, :party_id]
635 # ====== Remove the index named by_branch_party in the accounts table
636 # t.remove_index :name => :by_branch_party
637 def remove_index(options = {})
638 @base.remove_index(@table_name, options)
639 end
640
641 # Removes the timestamp columns (created_at and updated_at) from the table.
642 # ===== Example
643 # t.remove_timestamps
644 def remove_timestamps
645 @base.remove_timestamps(@table_name)
646 end
647
648 # Renames a column.
649 # ===== Example
650 # t.rename(:description, :name)
651 def rename(column_name, new_column_name)
652 @base.rename_column(@table_name, column_name, new_column_name)
653 end
654
655 # Adds a reference. Optionally adds a +type+ column.
656 # <tt>references</tt> and <tt>belongs_to</tt> are acceptable.
657 # ===== Examples
658 # t.references(:goat)
659 # t.references(:goat, :polymorphic => true)
660 # t.belongs_to(:goat)
661 def references(*args)
662 options = args.extract_options!
663 polymorphic = options.delete(:polymorphic)
664 args.each do |col|
665 @base.add_column(@table_name, "#{col}_id", :integer, options)
666 @base.add_column(@table_name, "#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
667 end
668 end
669 alias :belongs_to :references
670
671 # Removes a reference. Optionally removes a +type+ column.
672 # <tt>remove_references</tt> and <tt>remove_belongs_to</tt> are acceptable.
673 # ===== Examples
674 # t.remove_references(:goat)
675 # t.remove_references(:goat, :polymorphic => true)
676 # t.remove_belongs_to(:goat)
677 def remove_references(*args)
678 options = args.extract_options!
679 polymorphic = options.delete(:polymorphic)
680 args.each do |col|
681 @base.remove_column(@table_name, "#{col}_id")
682 @base.remove_column(@table_name, "#{col}_type") unless polymorphic.nil?
683 end
684 end
685 alias :remove_belongs_to :remove_references
686
687 # Adds a column or columns of a specified type
688 # ===== Examples
689 # t.string(:goat)
690 # t.string(:goat, :sheep)
691 %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
692 class_eval <<-EOV
693 def #{column_type}(*args) # def string(*args)
694 options = args.extract_options! # options = args.extract_options!
695 column_names = args # column_names = args
696 #
697 column_names.each do |name| # column_names.each do |name|
698 column = ColumnDefinition.new(@base, name, '#{column_type}') # column = ColumnDefinition.new(@base, name, 'string')
699 if options[:limit] # if options[:limit]
700 column.limit = options[:limit] # column.limit = options[:limit]
701 elsif native['#{column_type}'.to_sym].is_a?(Hash) # elsif native['string'.to_sym].is_a?(Hash)
702 column.limit = native['#{column_type}'.to_sym][:limit] # column.limit = native['string'.to_sym][:limit]
703 end # end
704 column.precision = options[:precision] # column.precision = options[:precision]
705 column.scale = options[:scale] # column.scale = options[:scale]
706 column.default = options[:default] # column.default = options[:default]
707 column.null = options[:null] # column.null = options[:null]
708 @base.add_column(@table_name, name, column.sql_type, options) # @base.add_column(@table_name, name, column.sql_type, options)
709 end # end
710 end # end
711 EOV
712 end
713
714 private
715 def native
716 @base.native_database_types
717 end
718 end
719
720 end
721end