@@ -116,54 +116,57 @@ def reload_type_map
116
116
117
117
private
118
118
119
- def initialize_type_map ( m = type_map )
120
- register_class_with_limit m , 'int2' , Type ::Integer
121
- register_class_with_limit m , 'int4' , Type ::Integer
122
- register_class_with_limit m , 'int8' , Type ::Integer
123
- m . register_type 'oid' , OID ::Oid . new
124
- m . register_type 'float4' , Type ::Float . new
125
- m . alias_type 'float8' , 'float4'
126
- m . register_type 'text' , Type ::Text . new
127
- register_class_with_limit m , 'varchar' , Type ::String
128
- m . alias_type 'char' , 'varchar'
129
- m . alias_type 'name' , 'varchar'
130
- m . alias_type 'bpchar' , 'varchar'
131
- m . register_type 'bool' , Type ::Boolean . new
132
- register_class_with_limit m , 'bit' , OID ::Bit
133
- register_class_with_limit m , 'varbit' , OID ::BitVarying
134
- m . alias_type 'timestamptz' , 'timestamp'
135
- m . register_type 'date' , OID ::Date . new
136
-
137
- m . register_type 'money' , OID ::Money . new
138
- m . register_type 'bytea' , OID ::Bytea . new
139
- m . register_type 'point' , OID ::Point . new
140
- m . register_type 'hstore' , OID ::Hstore . new
141
- m . register_type 'json' , Type ::Json . new
142
- m . register_type 'jsonb' , OID ::Jsonb . new
143
- m . register_type 'cidr' , OID ::Cidr . new
144
- m . register_type 'inet' , OID ::Inet . new
145
- m . register_type 'uuid' , OID ::Uuid . new
146
- m . register_type 'xml' , OID ::Xml . new
147
- m . register_type 'tsvector' , OID ::SpecializedString . new ( :tsvector )
148
- m . register_type 'macaddr' , OID ::Macaddr . new
149
- m . register_type 'citext' , OID ::SpecializedString . new ( :citext )
150
- m . register_type 'ltree' , OID ::SpecializedString . new ( :ltree )
151
- m . register_type 'line' , OID ::SpecializedString . new ( :line )
152
- m . register_type 'lseg' , OID ::SpecializedString . new ( :lseg )
153
- m . register_type 'box' , OID ::SpecializedString . new ( :box )
154
- m . register_type 'path' , OID ::SpecializedString . new ( :path )
155
- m . register_type 'polygon' , OID ::SpecializedString . new ( :polygon )
156
- m . register_type 'circle' , OID ::SpecializedString . new ( :circle )
157
-
158
- m . register_type 'interval' do |*args , sql_type |
159
- precision = extract_precision ( sql_type )
160
- OID ::Interval . new ( precision : precision )
161
- end
119
+ def register_class_with_limit ( ...)
120
+ ::ActiveRecord ::ConnectionAdapters ::AbstractAdapter . send ( :register_class_with_limit , ...)
121
+ end
162
122
163
- register_class_with_precision m , 'time' , Type ::Time
164
- register_class_with_precision m , 'timestamp' , OID ::DateTime
123
+ def register_class_with_precision ( ...)
124
+ ::ActiveRecord ::ConnectionAdapters ::AbstractAdapter . send ( :register_class_with_precision , ...)
125
+ end
165
126
166
- m . register_type 'numeric' do |_ , fmod , sql_type |
127
+ def initialize_type_map ( m = type_map )
128
+ m . register_type "int2" , Type ::Integer . new ( limit : 2 )
129
+ m . register_type "int4" , Type ::Integer . new ( limit : 4 )
130
+ m . register_type "int8" , Type ::Integer . new ( limit : 8 )
131
+ m . register_type "oid" , OID ::Oid . new
132
+ m . register_type "float4" , Type ::Float . new
133
+ m . alias_type "float8" , "float4"
134
+ m . register_type "text" , Type ::Text . new
135
+ register_class_with_limit m , "varchar" , Type ::String
136
+ m . alias_type "char" , "varchar"
137
+ m . alias_type "name" , "varchar"
138
+ m . alias_type "bpchar" , "varchar"
139
+ m . register_type "bool" , Type ::Boolean . new
140
+ register_class_with_limit m , "bit" , OID ::Bit
141
+ register_class_with_limit m , "varbit" , OID ::BitVarying
142
+ m . register_type "date" , OID ::Date . new
143
+
144
+ m . register_type "money" , OID ::Money . new
145
+ m . register_type "bytea" , OID ::Bytea . new
146
+ m . register_type "point" , OID ::Point . new
147
+ m . register_type "hstore" , OID ::Hstore . new
148
+ m . register_type "json" , Type ::Json . new
149
+ m . register_type "jsonb" , OID ::Jsonb . new
150
+ m . register_type "cidr" , OID ::Cidr . new
151
+ m . register_type "inet" , OID ::Inet . new
152
+ m . register_type "uuid" , OID ::Uuid . new
153
+ m . register_type "xml" , OID ::Xml . new
154
+ m . register_type "tsvector" , OID ::SpecializedString . new ( :tsvector )
155
+ m . register_type "macaddr" , OID ::Macaddr . new
156
+ m . register_type "citext" , OID ::SpecializedString . new ( :citext )
157
+ m . register_type "ltree" , OID ::SpecializedString . new ( :ltree )
158
+ m . register_type "line" , OID ::SpecializedString . new ( :line )
159
+ m . register_type "lseg" , OID ::SpecializedString . new ( :lseg )
160
+ m . register_type "box" , OID ::SpecializedString . new ( :box )
161
+ m . register_type "path" , OID ::SpecializedString . new ( :path )
162
+ m . register_type "polygon" , OID ::SpecializedString . new ( :polygon )
163
+ m . register_type "circle" , OID ::SpecializedString . new ( :circle )
164
+
165
+ register_class_with_precision m , "time" , Type ::Time
166
+ register_class_with_precision m , "timestamp" , OID ::Timestamp
167
+ register_class_with_precision m , "timestamptz" , OID ::TimestampWithTimeZone
168
+
169
+ m . register_type "numeric" do |_ , fmod , sql_type |
167
170
precision = extract_precision ( sql_type )
168
171
scale = extract_scale ( sql_type )
169
172
@@ -183,7 +186,10 @@ def initialize_type_map(m = type_map)
183
186
end
184
187
end
185
188
186
- load_additional_types ( m )
189
+ m . register_type "interval" do |*args , sql_type |
190
+ precision = extract_precision ( sql_type )
191
+ OID ::Interval . new ( precision : precision )
192
+ end
187
193
188
194
# pgjdbc returns these if the column is auto-incrmenting
189
195
m . alias_type 'serial' , 'int4'
@@ -231,6 +237,21 @@ def load_additional_types(type_map, oid = nil) # :nodoc:
231
237
initializer . run ( records )
232
238
end
233
239
240
+ def extract_scale ( sql_type )
241
+ case sql_type
242
+ when /\( (\d +)\) / then 0
243
+ when /\( (\d +)(,(\d +))\) / then $3. to_i
244
+ end
245
+ end
246
+
247
+ def extract_precision ( sql_type )
248
+ $1. to_i if sql_type =~ /\( (\d +)(,\d +)?\) /
249
+ end
250
+
251
+ def extract_limit ( sql_type )
252
+ $1. to_i if sql_type =~ /\( (.*)\) /
253
+ end
254
+
234
255
# Support arrays/ranges for defining attributes that don't exist in the db
235
256
ActiveRecord ::Type . add_modifier ( { array : true } , OID ::Array , adapter : :postgresql )
236
257
ActiveRecord ::Type . add_modifier ( { range : true } , OID ::Range , adapter : :postgresql )
0 commit comments