Skip to content

Commit a0f1d1f

Browse files
committed
[postgres] fix table column resolution (and slightly align it to our needs) on AR 4.2
1 parent b221817 commit a0f1d1f

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

lib/arjdbc/postgresql/adapter.rb

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,9 +1114,8 @@ def index_name_length
11141114
# Returns the list of all column definitions for a table.
11151115
def columns(table_name, name = nil)
11161116
column = jdbc_column_class
1117-
pass_cast_type = respond_to?(:lookup_cast_type)
1118-
column_definitions(table_name).map do |row|
1119-
# name, type, default, notnull, oid, fmod
1117+
column_definitions(table_name).map! do |row|
1118+
# |name, type, default, notnull, oid, fmod|
11201119
name = row[0]; type = row[1]; default = row[2]
11211120
notnull = row[3]; oid = row[4]; fmod = row[5]
11221121
# oid = OID::TYPE_MAP.fetch(oid.to_i, fmod.to_i) { OID::Identity.new }
@@ -1128,18 +1127,41 @@ def columns(table_name, name = nil)
11281127
elsif default =~ /^\(([-+]?[\d\.]+)\)$/ # e.g. "(-1)" for a negative default
11291128
default = $1
11301129
end
1131-
if pass_cast_type
1132-
cast_type = lookup_cast_type(type)
1133-
column.new(name, default, cast_type, type, ! notnull, fmod, self)
1134-
else
1135-
column.new(name, default, oid, type, ! notnull, fmod, self)
1136-
end
1130+
1131+
column.new(name, default, oid, type, ! notnull, fmod, self)
11371132
end
11381133
end
11391134

1135+
# @private documented above
1136+
def columns(table_name)
1137+
column = jdbc_column_class
1138+
# Limit, precision, and scale are all handled by the superclass.
1139+
column_definitions(table_name).map! do |row|
1140+
# |name, type, default, notnull, oid, fmod|
1141+
name = row[0]; type = row[1]; default = row[2]
1142+
notnull = row[3]; oid = row[4]; fmod = row[5]
1143+
notnull = notnull == 't' if notnull.is_a?(String) # JDBC gets true/false
1144+
1145+
oid_type = get_oid_type(oid.to_i, fmod.to_i, name, type)
1146+
default_value = extract_value_from_default(oid, default)
1147+
default_function = extract_default_function(default_value, default)
1148+
1149+
column.new(name, default_value, oid_type, type, ! notnull, default_function, oid, self)
1150+
end
1151+
end if AR42_COMPAT
1152+
1153+
# @private only for API compatibility
1154+
def new_column(name, default, cast_type, sql_type = nil, null = true, default_function = nil)
1155+
jdbc_column_class.new(name, default, cast_type, sql_type, null, default_function)
1156+
end if AR42_COMPAT
1157+
11401158
# @private
11411159
def column_for(table_name, column_name)
1142-
columns(table_name).detect { |c| c.name == column_name.to_s }
1160+
column_name = column_name.to_s
1161+
for column in columns(table_name)
1162+
return column if column.name == column_name
1163+
end
1164+
nil
11431165
end
11441166

11451167
# Returns the list of a table's column names, data types, and default values.

lib/arjdbc/postgresql/column.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ module Column
5353

5454
attr_accessor :array
5555

56-
def initialize(name, default, cast_type, sql_type = nil, null = true, default_function = nil)
56+
def initialize(name, default, cast_type, sql_type = nil, null = true, default_function = nil,
57+
oid = nil, adapter = nil) # added arguments
5758
if sql_type.to_s[-2, 2] == '[]'
5859
@array = true
5960
super(name, default, cast_type, sql_type[0..-3], null)
@@ -62,6 +63,9 @@ def initialize(name, default, cast_type, sql_type = nil, null = true, default_fu
6263
super(name, default, cast_type, sql_type, null)
6364
end
6465

66+
@oid = oid # used on Java side - expects @oid on Column instances
67+
#@adapter = adapter
68+
6569
@default_function = default_function
6670
end
6771

@@ -116,15 +120,20 @@ def self.included(base)
116120
base.send :include, ColumnHelpers
117121
end
118122

123+
if AR4_COMPAT && ! AR42_COMPAT
124+
119125
# @private
120126
def oid_type
121127
@oid_type ||= begin
122128
raise "oid not defined" unless oid = (@oid ||= nil)
123129
@adapter.get_oid_type(oid.to_i, @fmod.to_i, name)
124130
end
125-
end if AR4_COMPAT
131+
end
126132

127-
def accessor; oid_type.accessor end if AR4_COMPAT
133+
# @private
134+
def accessor; oid_type.accessor end
135+
136+
end
128137

129138
( attr_accessor :array; def array?; array; end ) if AR4_COMPAT
130139

0 commit comments

Comments
 (0)