activerecord 3.0.20 → 3.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- data/CHANGELOG +220 -91
- data/README.rdoc +3 -3
- data/examples/performance.rb +88 -109
- data/lib/active_record.rb +6 -2
- data/lib/active_record/aggregations.rb +22 -45
- data/lib/active_record/associations.rb +264 -991
- data/lib/active_record/associations/alias_tracker.rb +85 -0
- data/lib/active_record/associations/association.rb +231 -0
- data/lib/active_record/associations/association_scope.rb +120 -0
- data/lib/active_record/associations/belongs_to_association.rb +40 -60
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +15 -63
- data/lib/active_record/associations/builder/association.rb +53 -0
- data/lib/active_record/associations/builder/belongs_to.rb +85 -0
- data/lib/active_record/associations/builder/collection_association.rb +75 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +63 -0
- data/lib/active_record/associations/builder/has_many.rb +65 -0
- data/lib/active_record/associations/builder/has_one.rb +63 -0
- data/lib/active_record/associations/builder/singular_association.rb +32 -0
- data/lib/active_record/associations/collection_association.rb +524 -0
- data/lib/active_record/associations/collection_proxy.rb +125 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +27 -118
- data/lib/active_record/associations/has_many_association.rb +50 -79
- data/lib/active_record/associations/has_many_through_association.rb +98 -67
- data/lib/active_record/associations/has_one_association.rb +45 -115
- data/lib/active_record/associations/has_one_through_association.rb +21 -25
- data/lib/active_record/associations/join_dependency.rb +215 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +150 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
- data/lib/active_record/associations/join_helper.rb +56 -0
- data/lib/active_record/associations/preloader.rb +177 -0
- data/lib/active_record/associations/preloader/association.rb +126 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +15 -0
- data/lib/active_record/associations/preloader/has_one.rb +23 -0
- data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
- data/lib/active_record/associations/preloader/singular_association.rb +21 -0
- data/lib/active_record/associations/preloader/through_association.rb +67 -0
- data/lib/active_record/associations/singular_association.rb +55 -0
- data/lib/active_record/associations/through_association.rb +80 -0
- data/lib/active_record/attribute_methods.rb +19 -5
- data/lib/active_record/attribute_methods/before_type_cast.rb +9 -8
- data/lib/active_record/attribute_methods/dirty.rb +8 -2
- data/lib/active_record/attribute_methods/primary_key.rb +33 -13
- data/lib/active_record/attribute_methods/read.rb +17 -17
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +7 -4
- data/lib/active_record/attribute_methods/write.rb +2 -1
- data/lib/active_record/autosave_association.rb +66 -45
- data/lib/active_record/base.rb +445 -273
- data/lib/active_record/callbacks.rb +24 -33
- data/lib/active_record/coders/yaml_column.rb +41 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +106 -13
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +16 -2
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +12 -11
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +83 -12
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +16 -16
- data/lib/active_record/connection_adapters/abstract/quoting.rb +61 -22
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +16 -273
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +80 -42
- data/lib/active_record/connection_adapters/abstract_adapter.rb +44 -25
- data/lib/active_record/connection_adapters/column.rb +268 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +686 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +331 -88
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +295 -267
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +3 -7
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +108 -26
- data/lib/active_record/counter_cache.rb +7 -4
- data/lib/active_record/fixtures.rb +174 -192
- data/lib/active_record/identity_map.rb +131 -0
- data/lib/active_record/locking/optimistic.rb +20 -14
- data/lib/active_record/locking/pessimistic.rb +4 -4
- data/lib/active_record/log_subscriber.rb +24 -4
- data/lib/active_record/migration.rb +265 -144
- data/lib/active_record/migration/command_recorder.rb +103 -0
- data/lib/active_record/named_scope.rb +68 -25
- data/lib/active_record/nested_attributes.rb +58 -15
- data/lib/active_record/observer.rb +3 -7
- data/lib/active_record/persistence.rb +58 -38
- data/lib/active_record/query_cache.rb +25 -3
- data/lib/active_record/railtie.rb +21 -12
- data/lib/active_record/railties/console_sandbox.rb +6 -0
- data/lib/active_record/railties/databases.rake +147 -116
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/reflection.rb +176 -44
- data/lib/active_record/relation.rb +125 -49
- data/lib/active_record/relation/batches.rb +7 -5
- data/lib/active_record/relation/calculations.rb +50 -18
- data/lib/active_record/relation/finder_methods.rb +47 -26
- data/lib/active_record/relation/predicate_builder.rb +24 -21
- data/lib/active_record/relation/query_methods.rb +117 -101
- data/lib/active_record/relation/spawn_methods.rb +27 -20
- data/lib/active_record/result.rb +34 -0
- data/lib/active_record/schema.rb +5 -6
- data/lib/active_record/schema_dumper.rb +11 -13
- data/lib/active_record/serialization.rb +2 -2
- data/lib/active_record/serializers/xml_serializer.rb +10 -10
- data/lib/active_record/session_store.rb +8 -2
- data/lib/active_record/test_case.rb +9 -20
- data/lib/active_record/timestamp.rb +21 -9
- data/lib/active_record/transactions.rb +16 -15
- data/lib/active_record/validations.rb +21 -22
- data/lib/active_record/validations/associated.rb +3 -1
- data/lib/active_record/validations/uniqueness.rb +48 -58
- data/lib/active_record/version.rb +3 -3
- data/lib/rails/generators/active_record.rb +6 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +10 -2
- data/lib/rails/generators/active_record/model/model_generator.rb +2 -1
- data/lib/rails/generators/active_record/model/templates/migration.rb +6 -5
- data/lib/rails/generators/active_record/model/templates/model.rb +2 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +2 -0
- data/lib/rails/generators/active_record/observer/templates/observer.rb +2 -0
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +2 -1
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +2 -2
- metadata +106 -77
- checksums.yaml +0 -7
- data/lib/active_record/association_preload.rb +0 -431
- data/lib/active_record/associations/association_collection.rb +0 -572
- data/lib/active_record/associations/association_proxy.rb +0 -304
- data/lib/active_record/associations/through_association_scope.rb +0 -160
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'active_support/core_ext/object/duplicable'
|
2
|
-
|
3
1
|
module ActiveRecord
|
4
2
|
module ConnectionAdapters # :nodoc:
|
5
3
|
module QueryCache
|
@@ -31,6 +29,14 @@ module ActiveRecord
|
|
31
29
|
@query_cache_enabled = old
|
32
30
|
end
|
33
31
|
|
32
|
+
def enable_query_cache!
|
33
|
+
@query_cache_enabled = true
|
34
|
+
end
|
35
|
+
|
36
|
+
def disable_query_cache!
|
37
|
+
@query_cache_enabled = false
|
38
|
+
end
|
39
|
+
|
34
40
|
# Disable the query cache within the block.
|
35
41
|
def uncached
|
36
42
|
old, @query_cache_enabled = @query_cache_enabled, false
|
@@ -49,32 +55,26 @@ module ActiveRecord
|
|
49
55
|
@query_cache.clear
|
50
56
|
end
|
51
57
|
|
52
|
-
def select_all(
|
58
|
+
def select_all(sql, name = nil, binds = [])
|
53
59
|
if @query_cache_enabled
|
54
|
-
cache_sql(
|
60
|
+
cache_sql(sql, binds) { super }
|
55
61
|
else
|
56
62
|
super
|
57
63
|
end
|
58
64
|
end
|
59
65
|
|
60
66
|
private
|
61
|
-
def cache_sql(sql)
|
67
|
+
def cache_sql(sql, binds)
|
62
68
|
result =
|
63
|
-
if @query_cache.
|
69
|
+
if @query_cache[sql].key?(binds)
|
64
70
|
ActiveSupport::Notifications.instrument("sql.active_record",
|
65
|
-
:sql => sql, :name => "CACHE", :connection_id =>
|
66
|
-
@query_cache[sql]
|
71
|
+
:sql => sql, :name => "CACHE", :connection_id => object_id)
|
72
|
+
@query_cache[sql][binds]
|
67
73
|
else
|
68
|
-
@query_cache[sql] = yield
|
74
|
+
@query_cache[sql][binds] = yield
|
69
75
|
end
|
70
76
|
|
71
|
-
|
72
|
-
result.collect { |row| row.dup }
|
73
|
-
else
|
74
|
-
result.duplicable? ? result.dup : result
|
75
|
-
end
|
76
|
-
rescue TypeError
|
77
|
-
result
|
77
|
+
result.collect { |row| row.dup }
|
78
78
|
end
|
79
79
|
end
|
80
80
|
end
|
@@ -10,29 +10,68 @@ module ActiveRecord
|
|
10
10
|
return value.quoted_id if value.respond_to?(:quoted_id)
|
11
11
|
|
12
12
|
case value
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
13
|
+
when String, ActiveSupport::Multibyte::Chars
|
14
|
+
value = value.to_s
|
15
|
+
return "'#{quote_string(value)}'" unless column
|
16
|
+
|
17
|
+
case column.type
|
18
|
+
when :binary then "'#{quote_string(column.string_to_binary(value))}'"
|
19
|
+
when :integer then value.to_i.to_s
|
20
|
+
when :float then value.to_f.to_s
|
21
|
+
else
|
22
|
+
"'#{quote_string(value)}'"
|
23
|
+
end
|
24
|
+
|
25
|
+
when true, false
|
26
|
+
if column && column.type == :integer
|
27
|
+
value ? '1' : '0'
|
28
|
+
else
|
29
|
+
value ? quoted_true : quoted_false
|
30
|
+
end
|
31
|
+
# BigDecimals need to be put in a non-normalized form and quoted.
|
32
|
+
when nil then "NULL"
|
33
|
+
when BigDecimal then value.to_s('F')
|
34
|
+
when Numeric then value.to_s
|
35
|
+
when Date, Time then "'#{quoted_date(value)}'"
|
36
|
+
when Symbol then "'#{quote_string(value.to_s)}'"
|
37
|
+
else
|
38
|
+
"'#{quote_string(YAML.dump(value))}'"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Cast a +value+ to a type that the database understands. For example,
|
43
|
+
# SQLite does not understand dates, so this method will convert a Date
|
44
|
+
# to a String.
|
45
|
+
def type_cast(value, column)
|
46
|
+
return value.id if value.respond_to?(:quoted_id)
|
47
|
+
|
48
|
+
case value
|
49
|
+
when String, ActiveSupport::Multibyte::Chars
|
50
|
+
value = value.to_s
|
51
|
+
return value unless column
|
52
|
+
|
53
|
+
case column.type
|
54
|
+
when :binary then value
|
55
|
+
when :integer then value.to_i
|
56
|
+
when :float then value.to_f
|
30
57
|
else
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
58
|
+
value
|
59
|
+
end
|
60
|
+
|
61
|
+
when true, false
|
62
|
+
if column && column.type == :integer
|
63
|
+
value ? 1 : 0
|
64
|
+
else
|
65
|
+
value ? 't' : 'f'
|
66
|
+
end
|
67
|
+
# BigDecimals need to be put in a non-normalized form and quoted.
|
68
|
+
when nil then nil
|
69
|
+
when BigDecimal then value.to_s('F')
|
70
|
+
when Numeric then value
|
71
|
+
when Date, Time then quoted_date(value)
|
72
|
+
when Symbol then value.to_s
|
73
|
+
else
|
74
|
+
YAML.dump(value)
|
36
75
|
end
|
37
76
|
end
|
38
77
|
|
@@ -6,259 +6,6 @@ require 'bigdecimal/util'
|
|
6
6
|
|
7
7
|
module ActiveRecord
|
8
8
|
module ConnectionAdapters #:nodoc:
|
9
|
-
# An abstract definition of a column in a table.
|
10
|
-
class Column
|
11
|
-
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
|
12
|
-
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
|
13
|
-
|
14
|
-
module Format
|
15
|
-
ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/
|
16
|
-
ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
|
17
|
-
end
|
18
|
-
|
19
|
-
attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
|
20
|
-
attr_accessor :primary
|
21
|
-
|
22
|
-
# Instantiates a new column in the table.
|
23
|
-
#
|
24
|
-
# +name+ is the column's name, such as <tt>supplier_id</tt> in <tt>supplier_id int(11)</tt>.
|
25
|
-
# +default+ is the type-casted default value, such as +new+ in <tt>sales_stage varchar(20) default 'new'</tt>.
|
26
|
-
# +sql_type+ is used to extract the column's length, if necessary. For example +60+ in
|
27
|
-
# <tt>company_name varchar(60)</tt>.
|
28
|
-
# It will be mapped to one of the standard Rails SQL types in the <tt>type</tt> attribute.
|
29
|
-
# +null+ determines if this column allows +NULL+ values.
|
30
|
-
def initialize(name, default, sql_type = nil, null = true)
|
31
|
-
@name, @sql_type, @null = name, sql_type, null
|
32
|
-
@limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type)
|
33
|
-
@type = simplified_type(sql_type)
|
34
|
-
@default = extract_default(default)
|
35
|
-
|
36
|
-
@primary = nil
|
37
|
-
end
|
38
|
-
|
39
|
-
# Returns +true+ if the column is either of type string or text.
|
40
|
-
def text?
|
41
|
-
type == :string || type == :text
|
42
|
-
end
|
43
|
-
|
44
|
-
# Returns +true+ if the column is either of type integer, float or decimal.
|
45
|
-
def number?
|
46
|
-
type == :integer || type == :float || type == :decimal
|
47
|
-
end
|
48
|
-
|
49
|
-
def has_default?
|
50
|
-
!default.nil?
|
51
|
-
end
|
52
|
-
|
53
|
-
# Returns the Ruby class that corresponds to the abstract data type.
|
54
|
-
def klass
|
55
|
-
case type
|
56
|
-
when :integer then Fixnum
|
57
|
-
when :float then Float
|
58
|
-
when :decimal then BigDecimal
|
59
|
-
when :datetime then Time
|
60
|
-
when :date then Date
|
61
|
-
when :timestamp then Time
|
62
|
-
when :time then Time
|
63
|
-
when :text, :string then String
|
64
|
-
when :binary then String
|
65
|
-
when :boolean then Object
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
# Casts value (which is a String) to an appropriate instance.
|
70
|
-
def type_cast(value)
|
71
|
-
return nil if value.nil?
|
72
|
-
case type
|
73
|
-
when :string then value
|
74
|
-
when :text then value
|
75
|
-
when :integer then value.to_i rescue value ? 1 : 0
|
76
|
-
when :float then value.to_f
|
77
|
-
when :decimal then self.class.value_to_decimal(value)
|
78
|
-
when :datetime then self.class.string_to_time(value)
|
79
|
-
when :timestamp then self.class.string_to_time(value)
|
80
|
-
when :time then self.class.string_to_dummy_time(value)
|
81
|
-
when :date then self.class.string_to_date(value)
|
82
|
-
when :binary then self.class.binary_to_string(value)
|
83
|
-
when :boolean then self.class.value_to_boolean(value)
|
84
|
-
else value
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def type_cast_code(var_name)
|
89
|
-
case type
|
90
|
-
when :string then nil
|
91
|
-
when :text then nil
|
92
|
-
when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
|
93
|
-
when :float then "#{var_name}.to_f"
|
94
|
-
when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})"
|
95
|
-
when :datetime then "#{self.class.name}.string_to_time(#{var_name})"
|
96
|
-
when :timestamp then "#{self.class.name}.string_to_time(#{var_name})"
|
97
|
-
when :time then "#{self.class.name}.string_to_dummy_time(#{var_name})"
|
98
|
-
when :date then "#{self.class.name}.string_to_date(#{var_name})"
|
99
|
-
when :binary then "#{self.class.name}.binary_to_string(#{var_name})"
|
100
|
-
when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
|
101
|
-
else nil
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
# Returns the human name of the column name.
|
106
|
-
#
|
107
|
-
# ===== Examples
|
108
|
-
# Column.new('sales_stage', ...).human_name # => 'Sales stage'
|
109
|
-
def human_name
|
110
|
-
Base.human_attribute_name(@name)
|
111
|
-
end
|
112
|
-
|
113
|
-
def extract_default(default)
|
114
|
-
type_cast(default)
|
115
|
-
end
|
116
|
-
|
117
|
-
class << self
|
118
|
-
# Used to convert from Strings to BLOBs
|
119
|
-
def string_to_binary(value)
|
120
|
-
value
|
121
|
-
end
|
122
|
-
|
123
|
-
# Used to convert from BLOBs to Strings
|
124
|
-
def binary_to_string(value)
|
125
|
-
value
|
126
|
-
end
|
127
|
-
|
128
|
-
def string_to_date(string)
|
129
|
-
return string unless string.is_a?(String)
|
130
|
-
return nil if string.empty?
|
131
|
-
|
132
|
-
fast_string_to_date(string) || fallback_string_to_date(string)
|
133
|
-
end
|
134
|
-
|
135
|
-
def string_to_time(string)
|
136
|
-
return string unless string.is_a?(String)
|
137
|
-
return nil if string.empty?
|
138
|
-
|
139
|
-
fast_string_to_time(string) || fallback_string_to_time(string)
|
140
|
-
end
|
141
|
-
|
142
|
-
def string_to_dummy_time(string)
|
143
|
-
return string unless string.is_a?(String)
|
144
|
-
return nil if string.empty?
|
145
|
-
|
146
|
-
string_to_time "2000-01-01 #{string}"
|
147
|
-
end
|
148
|
-
|
149
|
-
# convert something to a boolean
|
150
|
-
def value_to_boolean(value)
|
151
|
-
if value.is_a?(String) && value.blank?
|
152
|
-
nil
|
153
|
-
else
|
154
|
-
TRUE_VALUES.include?(value)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
# convert something to a BigDecimal
|
159
|
-
def value_to_decimal(value)
|
160
|
-
# Using .class is faster than .is_a? and
|
161
|
-
# subclasses of BigDecimal will be handled
|
162
|
-
# in the else clause
|
163
|
-
if value.class == BigDecimal
|
164
|
-
value
|
165
|
-
elsif value.respond_to?(:to_d)
|
166
|
-
value.to_d
|
167
|
-
else
|
168
|
-
value.to_s.to_d
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
protected
|
173
|
-
# '0.123456' -> 123456
|
174
|
-
# '1.123456' -> 123456
|
175
|
-
def microseconds(time)
|
176
|
-
((time[:sec_fraction].to_f % 1) * 1_000_000).to_i
|
177
|
-
end
|
178
|
-
|
179
|
-
def new_date(year, mon, mday)
|
180
|
-
if year && year != 0
|
181
|
-
Date.new(year, mon, mday) rescue nil
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
def new_time(year, mon, mday, hour, min, sec, microsec)
|
186
|
-
# Treat 0000-00-00 00:00:00 as nil.
|
187
|
-
return nil if year.nil? || year == 0
|
188
|
-
|
189
|
-
Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
|
190
|
-
end
|
191
|
-
|
192
|
-
def fast_string_to_date(string)
|
193
|
-
if string =~ Format::ISO_DATE
|
194
|
-
new_date $1.to_i, $2.to_i, $3.to_i
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
# Doesn't handle time zones.
|
199
|
-
def fast_string_to_time(string)
|
200
|
-
if string =~ Format::ISO_DATETIME
|
201
|
-
microsec = ($7.to_f * 1_000_000).to_i
|
202
|
-
new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
def fallback_string_to_date(string)
|
207
|
-
new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
|
208
|
-
end
|
209
|
-
|
210
|
-
def fallback_string_to_time(string)
|
211
|
-
time_hash = Date._parse(string)
|
212
|
-
time_hash[:sec_fraction] = microseconds(time_hash)
|
213
|
-
|
214
|
-
new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
private
|
219
|
-
def extract_limit(sql_type)
|
220
|
-
$1.to_i if sql_type =~ /\((.*)\)/
|
221
|
-
end
|
222
|
-
|
223
|
-
def extract_precision(sql_type)
|
224
|
-
$2.to_i if sql_type =~ /^(numeric|decimal|number)\((\d+)(,\d+)?\)/i
|
225
|
-
end
|
226
|
-
|
227
|
-
def extract_scale(sql_type)
|
228
|
-
case sql_type
|
229
|
-
when /^(numeric|decimal|number)\((\d+)\)/i then 0
|
230
|
-
when /^(numeric|decimal|number)\((\d+)(,(\d+))\)/i then $4.to_i
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
def simplified_type(field_type)
|
235
|
-
case field_type
|
236
|
-
when /int/i
|
237
|
-
:integer
|
238
|
-
when /float|double/i
|
239
|
-
:float
|
240
|
-
when /decimal|numeric|number/i
|
241
|
-
extract_scale(field_type) == 0 ? :integer : :decimal
|
242
|
-
when /datetime/i
|
243
|
-
:datetime
|
244
|
-
when /timestamp/i
|
245
|
-
:timestamp
|
246
|
-
when /time/i
|
247
|
-
:time
|
248
|
-
when /date/i
|
249
|
-
:date
|
250
|
-
when /clob/i, /text/i
|
251
|
-
:text
|
252
|
-
when /blob/i, /binary/i
|
253
|
-
:binary
|
254
|
-
when /char/i, /string/i
|
255
|
-
:string
|
256
|
-
when /boolean/i
|
257
|
-
:boolean
|
258
|
-
end
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
9
|
class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths) #:nodoc:
|
263
10
|
end
|
264
11
|
|
@@ -268,6 +15,10 @@ module ActiveRecord
|
|
268
15
|
# for generating a number of table creation or table changing SQL statements.
|
269
16
|
class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :precision, :scale, :default, :null) #:nodoc:
|
270
17
|
|
18
|
+
def string_to_binary(value)
|
19
|
+
value
|
20
|
+
end
|
21
|
+
|
271
22
|
def sql_type
|
272
23
|
base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
|
273
24
|
end
|
@@ -318,21 +69,13 @@ module ActiveRecord
|
|
318
69
|
@base = base
|
319
70
|
end
|
320
71
|
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
else
|
326
|
-
super
|
327
|
-
end
|
328
|
-
end
|
72
|
+
def xml(*args)
|
73
|
+
raise NotImplementedError unless %w{
|
74
|
+
sqlite mysql mysql2
|
75
|
+
}.include? @base.adapter_name.downcase
|
329
76
|
|
330
|
-
|
331
|
-
|
332
|
-
when 'sqlite', 'mysql'
|
333
|
-
options = args.extract_options!
|
334
|
-
column(args[0], :text, options)
|
335
|
-
end
|
77
|
+
options = args.extract_options!
|
78
|
+
column(args[0], :text, options)
|
336
79
|
end
|
337
80
|
|
338
81
|
# Appends a primary key definition to the table definition.
|
@@ -361,7 +104,7 @@ module ActiveRecord
|
|
361
104
|
# Available options are (none of these exists by default):
|
362
105
|
# * <tt>:limit</tt> -
|
363
106
|
# Requests a maximum column length. This is number of characters for <tt>:string</tt> and
|
364
|
-
# <tt>:text</tt> columns and number of bytes for
|
107
|
+
# <tt>:text</tt> columns and number of bytes for <tt>:binary</tt> and <tt>:integer</tt> columns.
|
365
108
|
# * <tt>:default</tt> -
|
366
109
|
# The column's default value. Use nil for NULL.
|
367
110
|
# * <tt>:null</tt> -
|
@@ -410,7 +153,7 @@ module ActiveRecord
|
|
410
153
|
# This method returns <tt>self</tt>.
|
411
154
|
#
|
412
155
|
# == Examples
|
413
|
-
# # Assuming td is an instance of TableDefinition
|
156
|
+
# # Assuming +td+ is an instance of TableDefinition
|
414
157
|
# td.column(:granted, :boolean)
|
415
158
|
# # granted BOOLEAN
|
416
159
|
#
|
@@ -461,7 +204,7 @@ module ActiveRecord
|
|
461
204
|
# end
|
462
205
|
#
|
463
206
|
# There's a short-hand method for each of the type values declared at the top. And then there's
|
464
|
-
# TableDefinition#timestamps that'll add created_at and +updated_at+ as datetimes.
|
207
|
+
# TableDefinition#timestamps that'll add +created_at+ and +updated_at+ as datetimes.
|
465
208
|
#
|
466
209
|
# TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type
|
467
210
|
# column if the <tt>:polymorphic</tt> option is supplied. If <tt>:polymorphic</tt> is a hash of
|
@@ -608,7 +351,7 @@ module ActiveRecord
|
|
608
351
|
@base.index_exists?(@table_name, column_name, options)
|
609
352
|
end
|
610
353
|
|
611
|
-
# Adds timestamps (created_at and updated_at) columns to the table. See SchemaStatements#add_timestamps
|
354
|
+
# Adds timestamps (+created_at+ and +updated_at+) columns to the table. See SchemaStatements#add_timestamps
|
612
355
|
# ===== Example
|
613
356
|
# t.timestamps
|
614
357
|
def timestamps
|
@@ -655,7 +398,7 @@ module ActiveRecord
|
|
655
398
|
@base.remove_index(@table_name, options)
|
656
399
|
end
|
657
400
|
|
658
|
-
# Removes the timestamp columns (created_at and updated_at) from the table.
|
401
|
+
# Removes the timestamp columns (+created_at+ and +updated_at+) from the table.
|
659
402
|
# ===== Example
|
660
403
|
# t.remove_timestamps
|
661
404
|
def remove_timestamps
|
@@ -669,7 +412,7 @@ module ActiveRecord
|
|
669
412
|
@base.rename_column(@table_name, column_name, new_column_name)
|
670
413
|
end
|
671
414
|
|
672
|
-
# Adds a reference. Optionally adds a +type+ column.
|
415
|
+
# Adds a reference. Optionally adds a +type+ column, if <tt>:polymorphic</tt> option is provided.
|
673
416
|
# <tt>references</tt> and <tt>belongs_to</tt> are acceptable.
|
674
417
|
# ===== Examples
|
675
418
|
# t.references(:goat)
|