@@ -6,35 +6,114 @@ def self.column_selector
66 [ /postgre/i , lambda { |cfg , column | column . extend ( Column ) } ]
77 end
88
9+ # @private these are defined on the Adapter class since 4.2
10+ module ColumnHelpers
11+
12+ def extract_limit ( sql_type ) # :nodoc:
13+ case sql_type
14+ when /^bigint/i , /^int8/i then 8
15+ when /^smallint/i then 2
16+ when /^timestamp/i then nil
17+ else
18+ super
19+ end
20+ end
21+
22+ # Extracts the value from a PostgreSQL column default definition.
23+ def extract_value_from_default ( oid , default ) # :nodoc:
24+ case default
25+ # Quoted types
26+ when /\A [\( B]?'(.*)'::/m
27+ $1. gsub ( /''/ , "'" )
28+ # Boolean types
29+ when 'true' , 'false'
30+ default
31+ # Numeric types
32+ when /\A \( ?(-?\d +(\. \d *)?)\) ?(::bigint)?\z /
33+ $1
34+ # Object identifier types
35+ when /\A -?\d +\z /
36+ $1
37+ else
38+ # Anything else is blank, some user type, or some function
39+ # and we can't know the value of that, so return nil.
40+ nil
41+ end
42+ end
43+
44+ def extract_default_function ( default_value , default ) # :nodoc:
45+ default if ! default_value && ( %r{\w +\( .*\) } === default )
46+ end
47+
48+ end
49+
950 # Column behavior based on PostgreSQL adapter in Rails.
1051 # @see ActiveRecord::ConnectionAdapters::JdbcColumn
1152 module Column
1253
54+ attr_accessor :array
55+
56+ def initialize ( name , default , cast_type , sql_type = nil , null = true , default_function = nil )
57+ if sql_type . to_s [ -2 , 2 ] == '[]'
58+ @array = true
59+ super ( name , default , cast_type , sql_type [ 0 ..-3 ] , null )
60+ else
61+ @array = false
62+ super ( name , default , cast_type , sql_type , null )
63+ end
64+
65+ @default_function = default_function
66+ end
67+
68+ end if AR42_COMPAT
69+
70+ # @private (AR < 4.2 version) documented above
71+ module Column
72+
73+ def initialize ( name , default , oid_type = nil , sql_type = nil , null = true ,
74+ fmod = nil , adapter = nil ) # added due resolving #oid_type
75+ if oid_type . is_a? ( Integer ) # the "main" if branch (on AR 4.x)
76+ @oid = oid_type ; @fmod = fmod ; @adapter = adapter # see Column#oid_type
77+ elsif oid_type . respond_to? ( :type_cast ) # MRI compatibility
78+ @oid_type = oid_type ; # @fmod = fmod; @adapter = adapter
79+ else # NOTE: AR <= 3.2 : (name, default, sql_type = nil, null = true)
80+ null , sql_type , oid_type = !! sql_type , oid_type , nil
81+ end
82+ if sql_type . to_s [ -2 , 2 ] == '[]' && ArJdbc ::PostgreSQL ::AR4_COMPAT
83+ @array = true if respond_to? ( :array )
84+ super ( name , default , sql_type [ 0 ..-3 ] , null )
85+ else
86+ @array = false if respond_to? ( :array )
87+ super ( name , default , sql_type , null )
88+ end
89+
90+ @default_function = extract_default_function ( @default , default )
91+ end
92+
1393 def self . included ( base )
1494 # NOTE: assumes a standalone PostgreSQLColumn class
15- class << base
16-
17- attr_accessor :money_precision
18-
19- # Loads pg_array_parser if available. String parsing can be
20- # performed quicker by a native extension, which will not create
21- # a large amount of Ruby objects that will need to be garbage
22- # collected. pg_array_parser has a C and Java extension
23- begin
24- require 'pg_array_parser'
25- include PgArrayParser
26- rescue LoadError
27- if AR42_COMPAT
28- require 'active_record/connection_adapters/postgresql/array_parser'
29- else
30- require 'arjdbc/postgresql/base/array_parser'
31- end
32- include ActiveRecord ::ConnectionAdapters ::PostgreSQL ::ArrayParser
33- end if AR4_COMPAT
95+ base_meta = class << base ; self end
96+ base_meta . send :attr_accessor , :money_precision
97+
98+ # Loads pg_array_parser if available. String parsing can be
99+ # performed quicker by a native extension, which will not create
100+ # a large amount of Ruby objects that will need to be garbage
101+ # collected. pg_array_parser has a C and Java extension
102+ begin
103+ require 'pg_array_parser'
104+ base_meta . send :include , PgArrayParser
105+ rescue LoadError
106+ if AR42_COMPAT
107+ require 'active_record/connection_adapters/postgresql/array_parser'
108+ else
109+ require 'arjdbc/postgresql/base/array_parser'
110+ end
111+ base_meta . send :include , ActiveRecord ::ConnectionAdapters ::PostgreSQL ::ArrayParser
112+ end if AR4_COMPAT
34113
35- include Cast
114+ base_meta . send : include, Cast
36115
37- end
116+ base . send :include , ColumnHelpers
38117 end
39118
40119 # @private
@@ -202,15 +281,6 @@ def type_cast(value, type = false) # AR >= 4.0 version
202281
203282 private
204283
205- def extract_limit ( sql_type )
206- case sql_type
207- when /^bigint/i ; 8
208- when /^smallint/i ; 2
209- when /^timestamp/i ; nil
210- else super
211- end
212- end
213-
214284 # Extracts the scale from PostgreSQL-specific data types.
215285 def extract_scale ( sql_type )
216286 # Money type has a fixed scale of 2.
@@ -555,6 +625,6 @@ def type_cast_array(oid, value)
555625
556626 end
557627
558- end
628+ end unless AR42_COMPAT
559629 end
560630end
0 commit comments