Skip to content

Commit 99316a3

Browse files
committed
use the (shared) AR4x constants in PostgreSQL adapter
1 parent 090feea commit 99316a3

File tree

4 files changed

+62
-61
lines changed

4 files changed

+62
-61
lines changed

lib/arjdbc/postgresql/adapter.rb

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ module ArJdbc
66
# Strives to provide Rails built-in PostgreSQL adapter (API) compatibility.
77
module PostgreSQL
88

9+
# @deprecated no longer used
910
# @private
10-
AR4_COMPAT = ::ActiveRecord::VERSION::MAJOR > 3 unless const_defined?(:AR4_COMPAT)
11+
AR4_COMPAT = AR40
12+
# @deprecated no longer used
1113
# @private
12-
AR42_COMPAT = ::ActiveRecord::VERSION::MAJOR > 4 ||
13-
( ::ActiveRecord::VERSION::MAJOR == 4 && ::ActiveRecord::VERSION::MINOR >= 2 )
14+
AR42_COMPAT = AR42
1415

1516
require 'arjdbc/postgresql/column'
1617
require 'arjdbc/postgresql/explain_support'
@@ -22,7 +23,7 @@ module PostgreSQL
2223
ForeignKeyDefinition = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition if ::ActiveRecord::ConnectionAdapters.const_defined? :ForeignKeyDefinition
2324

2425
# @private
25-
Type = ::ActiveRecord::Type if AR42_COMPAT
26+
Type = ::ActiveRecord::Type if AR42
2627

2728
# @see ActiveRecord::ConnectionAdapters::JdbcAdapter#jdbc_connection_class
2829
def self.jdbc_connection_class
@@ -217,7 +218,7 @@ def type_cast(value, column, array_member = false)
217218
else
218219
super(value, column)
219220
end
220-
end if AR4_COMPAT && ! AR42_COMPAT
221+
end if AR40 && ! AR42
221222

222223
# @private
223224
def _type_cast(value)
@@ -232,8 +233,8 @@ def _type_cast(value)
232233
else
233234
super
234235
end
235-
end if AR42_COMPAT
236-
private :_type_cast if AR42_COMPAT
236+
end if AR42
237+
private :_type_cast if AR42
237238

238239
NATIVE_DATABASE_TYPES = {
239240
:primary_key => "serial primary key",
@@ -275,14 +276,14 @@ def _type_cast(value)
275276
:tstzrange => { :name => "tstzrange" },
276277
:int4range => { :name => "int4range" },
277278
:int8range => { :name => "int8range" },
278-
}) if AR4_COMPAT
279+
}) if AR40
279280

280281
NATIVE_DATABASE_TYPES.update(
281282
:bigserial => "bigserial",
282283
:bigint => { :name => "bigint" },
283284
:bit => { :name => "bit" },
284285
:bit_varying => { :name => "bit varying" }
285-
) if AR42_COMPAT
286+
) if AR42
286287

287288
def native_database_types
288289
NATIVE_DATABASE_TYPES
@@ -295,13 +296,13 @@ def prepare_column_options(column, types)
295296
spec[:array] = 'true' if column.respond_to?(:array) && column.array
296297
spec[:default] = "\"#{column.default_function}\"" if column.default_function
297298
spec
298-
end if AR4_COMPAT
299+
end if AR40
299300

300301
# Adds `:array` as a valid migration key.
301302
# @override
302303
def migration_keys
303304
super + [:array]
304-
end if AR4_COMPAT
305+
end if AR40
305306

306307
# Enable standard-conforming strings if available.
307308
def set_standard_conforming_strings
@@ -369,12 +370,12 @@ def supports_transaction_isolation?; true end
369370

370371
def supports_index_sort_order?; true end
371372

372-
def supports_partial_index?; true end if AR4_COMPAT
373+
def supports_partial_index?; true end if AR40
373374

374375
# Range data-types weren't introduced until PostgreSQL 9.2.
375376
def supports_ranges?
376377
postgresql_version >= 90200
377-
end if AR4_COMPAT
378+
end if AR40
378379

379380
def supports_transaction_isolation?(level = nil)
380381
true
@@ -875,7 +876,7 @@ def quote(value, column = nil)
875876
sql_type = column.respond_to?(:sql_type) && column.sql_type
876877
sql_type && sql_type[0, 3] == 'bit' ? quote_bit(value) : super
877878
when Array
878-
if AR4_COMPAT && column.array? # will be always falsy in AR < 4.0
879+
if AR40 && column.array? # will be always falsy in AR < 4.0
879880
"'#{jdbc_column_class.array_to_string(value, column, self).gsub(/'/, "''")}'"
880881
elsif column.type == :json # only in AR-4.0
881882
super(jdbc_column_class.json_to_string(value), column)
@@ -896,7 +897,7 @@ def quote(value, column = nil)
896897
end
897898
when Range
898899
sql_type = column.respond_to?(:sql_type) && column.sql_type
899-
if sql_type && sql_type[-5, 5] == 'range' && AR4_COMPAT
900+
if sql_type && sql_type[-5, 5] == 'range' && AR40
900901
escaped = quote_string(jdbc_column_class.range_to_string(value))
901902
"'#{escaped}'::#{sql_type}"
902903
else super
@@ -909,7 +910,7 @@ def quote(value, column = nil)
909910
else
910911
super
911912
end
912-
end unless AR42_COMPAT
913+
end unless AR42
913914

914915
# @private
915916
def _quote(value)
@@ -933,8 +934,8 @@ def _quote(value)
933934
else
934935
super
935936
end
936-
end if AR42_COMPAT
937-
private :_quote if AR42_COMPAT
937+
end if AR42
938+
private :_quote if AR42
938939

939940
# Quotes a string, escaping any ' (single quote) and \ (backslash) chars.
940941
# @return [String]
@@ -963,7 +964,7 @@ def quote_bit(value)
963964

964965
def quote_bit(value)
965966
"B'#{value}'"
966-
end if AR4_COMPAT
967+
end if AR40
967968

968969
def escape_bytea(string)
969970
return unless string
@@ -991,7 +992,7 @@ def quote_table_name(name)
991992
# @override
992993
def quote_table_name_for_assignment(table, attr)
993994
quote_column_name(attr)
994-
end if AR4_COMPAT
995+
end if AR40
995996

996997
# @override
997998
def quote_column_name(name)
@@ -1074,7 +1075,7 @@ def add_column(table_name, column_name, type, options = {})
10741075
end if ::ActiveRecord::VERSION::MAJOR < 4
10751076

10761077
# @private documented above
1077-
def add_column(table_name, column_name, type, options = {}); super end if AR42_COMPAT
1078+
def add_column(table_name, column_name, type, options = {}); super end if AR42
10781079

10791080
# Changes the column of a table.
10801081
def change_column(table_name, column_name, type, options = {})
@@ -1124,7 +1125,7 @@ def change_column_default(table_name, column_name, default)
11241125
else
11251126
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}"
11261127
end
1127-
end unless AR42_COMPAT # unless const_defined? :SchemaCreation
1128+
end unless AR42 # unless const_defined? :SchemaCreation
11281129

11291130
# @private documented above
11301131
def change_column_default(table_name, column_name, default)
@@ -1138,7 +1139,7 @@ def change_column_default(table_name, column_name, default)
11381139
else
11391140
execute alter_column_query % "SET DEFAULT #{quote_default_value(default, column)}"
11401141
end
1141-
end if AR42_COMPAT
1142+
end if AR42
11421143

11431144
# @private
11441145
def change_column_null(table_name, column_name, null, default = nil)
@@ -1150,7 +1151,7 @@ def change_column_null(table_name, column_name, null, default = nil)
11501151
end
11511152
end
11521153
execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
1153-
end unless AR42_COMPAT # unless const_defined? :SchemaCreation
1154+
end unless AR42 # unless const_defined? :SchemaCreation
11541155

11551156
# @private
11561157
def change_column_null(table_name, column_name, null, default = nil)
@@ -1159,7 +1160,7 @@ def change_column_null(table_name, column_name, null, default = nil)
11591160
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote_default_value(default, column)} WHERE #{quote_column_name(column_name)} IS NULL") if column
11601161
end
11611162
execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
1162-
end if AR42_COMPAT
1163+
end if AR42
11631164

11641165
def rename_column(table_name, column_name, new_column_name)
11651166
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
@@ -1169,7 +1170,7 @@ def rename_column(table_name, column_name, new_column_name)
11691170
def add_index(table_name, column_name, options = {})
11701171
index_name, index_type, index_columns, index_options, index_algorithm, index_using = add_index_options(table_name, column_name, options)
11711172
execute "CREATE #{index_type} INDEX #{index_algorithm} #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} #{index_using} (#{index_columns})#{index_options}"
1172-
end if AR4_COMPAT
1173+
end if AR40
11731174

11741175
def remove_index!(table_name, index_name)
11751176
execute "DROP INDEX #{quote_table_name(index_name)}"
@@ -1259,12 +1260,12 @@ def columns(table_name)
12591260

12601261
column.new(name, default_value, oid_type, type, ! notnull, default_function, oid, self)
12611262
end
1262-
end if AR42_COMPAT
1263+
end if AR42
12631264

12641265
# @private only for API compatibility
12651266
def new_column(name, default, cast_type, sql_type = nil, null = true, default_function = nil)
12661267
jdbc_column_class.new(name, default, cast_type, sql_type, null, default_function)
1267-
end if AR42_COMPAT
1268+
end if AR42
12681269

12691270
# @private
12701271
def column_for(table_name, column_name)
@@ -1306,7 +1307,7 @@ def tables(name = nil)
13061307
# @private
13071308
TABLE_EXISTS_SQL_PREFIX = 'SELECT COUNT(*) as table_count FROM pg_class c'
13081309
TABLE_EXISTS_SQL_PREFIX << ' LEFT JOIN pg_namespace n ON n.oid = c.relnamespace'
1309-
if AR42_COMPAT # -- (r)elation/table, (v)iew, (m)aterialized view
1310+
if AR42 # -- (r)elation/table, (v)iew, (m)aterialized view
13101311
TABLE_EXISTS_SQL_PREFIX << " WHERE c.relkind IN ('r','v','m')"
13111312
else
13121313
TABLE_EXISTS_SQL_PREFIX << " WHERE c.relkind IN ('r','v')"
@@ -1349,7 +1350,7 @@ def index_name_exists?(table_name, index_name, default)
13491350
AND t.relname = '#{table_name}'
13501351
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
13511352
SQL
1352-
end if AR42_COMPAT
1353+
end if AR42
13531354

13541355
# Returns an array of indexes for the given table.
13551356
def indexes(table_name, name = nil)
@@ -1411,7 +1412,7 @@ def column_name_for_operation(operation, node)
14111412
when 'average' then 'avg'
14121413
else operation.downcase
14131414
end
1414-
end if AR42_COMPAT
1415+
end if AR42
14151416

14161417
private
14171418

@@ -1474,9 +1475,9 @@ class PostgreSQLAdapter < JdbcAdapter
14741475
include ::ArJdbc::PostgreSQL
14751476
include ::ArJdbc::PostgreSQL::ExplainSupport
14761477

1477-
require 'arjdbc/postgresql/oid_types' if AR4_COMPAT
1478+
require 'arjdbc/postgresql/oid_types' if AR40
14781479
include ::ArJdbc::PostgreSQL::OIDTypes if ::ArJdbc::PostgreSQL.const_defined?(:OIDTypes)
1479-
include ::ArJdbc::PostgreSQL::ColumnHelpers if AR42_COMPAT
1480+
include ::ArJdbc::PostgreSQL::ColumnHelpers if AR42
14801481

14811482
include ::ArJdbc::Util::QuotedCache
14821483

@@ -1488,13 +1489,13 @@ def initialize(*args)
14881489

14891490
@table_alias_length = nil
14901491

1491-
initialize_type_map(@type_map = Type::HashLookupTypeMap.new) if AR42_COMPAT
1492+
initialize_type_map(@type_map = Type::HashLookupTypeMap.new) if AR42
14921493

14931494
@use_insert_returning = @config.key?(:insert_returning) ?
14941495
self.class.type_cast_config_to_boolean(@config[:insert_returning]) : nil
14951496
end
14961497

1497-
if AR42_COMPAT
1498+
if AR42
14981499
require 'active_record/connection_adapters/postgresql/schema_definitions'
14991500
else
15001501
require 'arjdbc/postgresql/base/schema_definitions'

lib/arjdbc/postgresql/column.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def initialize(name, default, cast_type, sql_type = nil, null = true, default_fu
7070
@default_function = default_function
7171
end
7272

73-
end if AR42_COMPAT
73+
end if AR42
7474

7575
# @private (AR < 4.2 version) documented above
7676
module Column
@@ -84,7 +84,7 @@ def initialize(name, default, oid_type = nil, sql_type = nil, null = true,
8484
else # NOTE: AR <= 3.2 : (name, default, sql_type = nil, null = true)
8585
null, sql_type, oid_type = !! sql_type, oid_type, nil
8686
end
87-
if sql_type.to_s[-2, 2] == '[]' && ArJdbc::PostgreSQL::AR4_COMPAT
87+
if sql_type.to_s[-2, 2] == '[]' && ArJdbc::PostgreSQL::AR40
8888
@array = true if respond_to?(:array)
8989
super(name, default, sql_type[0..-3], null)
9090
else
@@ -108,20 +108,20 @@ def self.included(base)
108108
require 'pg_array_parser'
109109
base_meta.send :include, PgArrayParser
110110
rescue LoadError
111-
if AR42_COMPAT
111+
if AR42
112112
require 'active_record/connection_adapters/postgresql/array_parser'
113113
else
114114
require 'arjdbc/postgresql/base/array_parser'
115115
end
116116
base_meta.send :include, ActiveRecord::ConnectionAdapters::PostgreSQL::ArrayParser
117-
end if AR4_COMPAT
117+
end if AR40
118118

119119
base_meta.send :include, Cast
120120

121121
base.send :include, ColumnHelpers
122122
end
123123

124-
if AR4_COMPAT && ! AR42_COMPAT
124+
if AR40 && ! AR42
125125

126126
# @private
127127
def oid_type
@@ -136,10 +136,10 @@ def accessor; oid_type.accessor end
136136

137137
end
138138

139-
( attr_accessor :array; def array?; array; end ) if AR4_COMPAT
139+
( attr_accessor :array; def array?; array; end ) if AR40
140140

141-
def number?; !array && super end if AR4_COMPAT
142-
def text?; !array && super end if AR4_COMPAT
141+
def number?; !array && super end if AR40
142+
def text?; !array && super end if AR40
143143

144144
# Extracts the value from a PostgreSQL column default definition.
145145
#
@@ -287,7 +287,7 @@ def type_cast(value, type = false) # AR >= 4.0 version
287287
super(value)
288288
end
289289
end
290-
end if AR4_COMPAT
290+
end if AR40
291291

292292
private
293293

@@ -399,7 +399,7 @@ def simplified_type(field_type)
399399
else
400400
super
401401
end
402-
end if AR4_COMPAT
402+
end if AR40
403403

404404
# OID Type::Range helpers :
405405

@@ -410,11 +410,11 @@ def extract_bounds(value)
410410
:to => (value[-2] == ',' || t == 'infinity') ? infinity : t,
411411
:exclude_start => (value[0] == '('), :exclude_end => (value[-1] == ')')
412412
}
413-
end if AR4_COMPAT
413+
end if AR40
414414

415415
def infinity(options = {})
416416
::Float::INFINITY * (options[:negative] ? -1 : 1)
417-
end if AR4_COMPAT
417+
end if AR40
418418

419419
private
420420

@@ -496,7 +496,7 @@ def string_to_bit(value)
496496
else
497497
value # Bit-string notation
498498
end
499-
end if AR4_COMPAT
499+
end if AR40
500500

501501
def hstore_to_string(object, array_member = false)
502502
if Hash === object
@@ -635,6 +635,6 @@ def type_cast_array(oid, value)
635635

636636
end
637637

638-
end unless AR42_COMPAT
638+
end unless AR42
639639
end
640640
end

0 commit comments

Comments
 (0)