activerecord-jdbc-adapter 1.3.11 → 1.3.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Appraisals +2 -2
- data/Gemfile +4 -0
- data/History.md +11 -0
- data/lib/arjdbc/db2/adapter.rb +3 -0
- data/lib/arjdbc/h2/adapter.rb +4 -1
- data/lib/arjdbc/hsqldb/adapter.rb +4 -1
- data/lib/arjdbc/jdbc.rb +9 -8
- data/lib/arjdbc/jdbc/adapter.rb +46 -15
- data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
- data/lib/arjdbc/mssql/adapter.rb +12 -6
- data/lib/arjdbc/mysql/adapter.rb +78 -13
- data/lib/arjdbc/mysql/column.rb +20 -0
- data/lib/arjdbc/mysql/connection_methods.rb +56 -2
- data/lib/arjdbc/oracle/adapter.rb +16 -17
- data/lib/arjdbc/postgresql/adapter.rb +136 -98
- data/lib/arjdbc/postgresql/base/oid.rb +78 -146
- data/lib/arjdbc/postgresql/oid_types.rb +132 -10
- data/lib/arjdbc/sqlite3/adapter.rb +58 -9
- data/lib/arjdbc/version.rb +1 -1
- data/rakelib/02-test.rake +2 -2
- data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +48 -10
- data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +81 -11
- metadata +2 -2
data/lib/arjdbc/mysql/column.rb
CHANGED
@@ -10,6 +10,26 @@ module ArJdbc
|
|
10
10
|
# @see ActiveRecord::ConnectionAdapters::JdbcColumn
|
11
11
|
module Column
|
12
12
|
|
13
|
+
def initialize(name, default, sql_type = nil, null = true, collation = nil, strict = false, extra = '')
|
14
|
+
if name.is_a?(Hash)
|
15
|
+
super # first arg: config
|
16
|
+
else
|
17
|
+
@strict = strict; @collation = collation; @extra = extra
|
18
|
+
super(name, default, sql_type, null)
|
19
|
+
# base 4.1: (name, default, sql_type = nil, null = true)
|
20
|
+
end
|
21
|
+
end unless AR42
|
22
|
+
|
23
|
+
def initialize(name, default, cast_type, sql_type = nil, null = true, collation = nil, strict = false, extra = '')
|
24
|
+
if name.is_a?(Hash)
|
25
|
+
super # first arg: config
|
26
|
+
else
|
27
|
+
@strict = strict; @collation = collation; @extra = extra
|
28
|
+
super(name, default, cast_type, sql_type, null)
|
29
|
+
# base 4.2: (name, default, cast_type, sql_type = nil, null = true)
|
30
|
+
end
|
31
|
+
end if AR42
|
32
|
+
|
13
33
|
attr_reader :collation, :strict, :extra
|
14
34
|
|
15
35
|
def extract_default(default)
|
@@ -34,8 +34,13 @@ ArJdbc::ConnectionMethods.module_eval do
|
|
34
34
|
properties['zeroDateTimeBehavior'] ||= 'convertToNull'
|
35
35
|
properties['jdbcCompliantTruncation'] ||= 'false'
|
36
36
|
properties['useUnicode'] = 'true' unless properties.key?('useUnicode') # otherwise platform default
|
37
|
-
|
38
|
-
|
37
|
+
# NOTE: this is "better" than passing what users are used to set on MRI
|
38
|
+
# e.g. 'utf8mb4' will fail cause the driver will check for a Java charset
|
39
|
+
# ... it's smart enough to detect utf8mb4 from server variables :
|
40
|
+
# "character_set_client" && "character_set_connection" (thus UTF-8)
|
41
|
+
if encoding = config.key?(:encoding) ? config[:encoding] : 'utf8'
|
42
|
+
properties['characterEncoding'] = convert_mysql_encoding(encoding) || encoding
|
43
|
+
end
|
39
44
|
if ! ( reconnect = config[:reconnect] ).nil?
|
40
45
|
properties['autoReconnect'] ||= reconnect.to_s
|
41
46
|
# properties['maxReconnects'] ||= '3'
|
@@ -88,4 +93,53 @@ ArJdbc::ConnectionMethods.module_eval do
|
|
88
93
|
mysql_connection(config)
|
89
94
|
end
|
90
95
|
alias_method :jdbcmariadb_connection, :mariadb_connection
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
@@mysql_encodings = nil
|
100
|
+
|
101
|
+
def convert_mysql_encoding(encoding) # from mysql2's ruby_enc_to_mysql
|
102
|
+
( @@mysql_encodings ||= {
|
103
|
+
"big5" => "Big5",
|
104
|
+
"dec8" => nil,
|
105
|
+
"cp850" => "CP850",
|
106
|
+
"hp8" => nil,
|
107
|
+
"koi8r" => "KOI8-R",
|
108
|
+
"latin1" => "ISO-8859-1",
|
109
|
+
"latin2" => "ISO-8859-2",
|
110
|
+
"swe7" => nil,
|
111
|
+
"ascii" => "US-ASCII",
|
112
|
+
#"ujis" => "eucJP-ms",
|
113
|
+
#"sjis" => "Shift_JIS",
|
114
|
+
"hebrew" => "ISO-8859-8",
|
115
|
+
#"tis620" => "TIS-620",
|
116
|
+
#"euckr" => "EUC-KR",
|
117
|
+
#"koi8u" => "KOI8-R",
|
118
|
+
#"gb2312" => "GB2312",
|
119
|
+
"greek" => "ISO-8859-7",
|
120
|
+
"cp1250" => "Windows-1250",
|
121
|
+
#"gbk" => "GBK",
|
122
|
+
"latin5" => "ISO-8859-9",
|
123
|
+
"armscii8" => nil,
|
124
|
+
"utf8" => "UTF-8",
|
125
|
+
"ucs2" => "UTF-16BE",
|
126
|
+
"cp866" => "IBM866",
|
127
|
+
"keybcs2" => nil,
|
128
|
+
#"macce" => "macCentEuro",
|
129
|
+
#"macroman" => "macRoman",
|
130
|
+
"cp852" => "CP852",
|
131
|
+
"latin7" => "ISO-8859-13",
|
132
|
+
"utf8mb4" => "UTF-8",
|
133
|
+
"cp1251" => "Windows-1251",
|
134
|
+
"utf16" => "UTF-16",
|
135
|
+
"cp1256" => "Windows-1256",
|
136
|
+
"cp1257" => "Windows-1257",
|
137
|
+
"utf32" => "UTF-32",
|
138
|
+
"binary" => "ASCII-8BIT",
|
139
|
+
"geostd8" => nil,
|
140
|
+
#"cp932" => "Windows-31J",
|
141
|
+
#"eucjpms" => "eucJP-ms"
|
142
|
+
} )[ encoding ]
|
143
|
+
end
|
144
|
+
|
91
145
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
ArJdbc.load_java_part :Oracle
|
2
2
|
|
3
|
-
require 'arjdbc/oracle/column'
|
4
|
-
|
5
3
|
module ArJdbc
|
6
4
|
module Oracle
|
7
5
|
|
6
|
+
require 'arjdbc/oracle/column'
|
7
|
+
|
8
8
|
# @private
|
9
9
|
def self.extended(adapter); initialize!; end
|
10
10
|
|
@@ -31,9 +31,7 @@ module ArJdbc
|
|
31
31
|
end
|
32
32
|
|
33
33
|
# @see ActiveRecord::ConnectionAdapters::JdbcAdapter#jdbc_column_class
|
34
|
-
def jdbc_column_class
|
35
|
-
::ActiveRecord::ConnectionAdapters::OracleColumn
|
36
|
-
end
|
34
|
+
def jdbc_column_class; Column end
|
37
35
|
|
38
36
|
# @private
|
39
37
|
@@update_lob_values = true
|
@@ -485,24 +483,19 @@ module ArJdbc
|
|
485
483
|
end
|
486
484
|
|
487
485
|
# @override
|
488
|
-
def supports_migrations
|
489
|
-
true
|
490
|
-
end
|
486
|
+
def supports_migrations?; true end
|
491
487
|
|
492
488
|
# @override
|
493
|
-
def supports_primary_key
|
494
|
-
true
|
495
|
-
end
|
489
|
+
def supports_primary_key?; true end
|
496
490
|
|
497
491
|
# @override
|
498
|
-
def supports_savepoints
|
499
|
-
true
|
500
|
-
end
|
492
|
+
def supports_savepoints?; true end
|
501
493
|
|
502
494
|
# @override
|
503
|
-
def supports_explain
|
504
|
-
|
505
|
-
|
495
|
+
def supports_explain?; true end
|
496
|
+
|
497
|
+
# @override
|
498
|
+
def supports_views?; true end
|
506
499
|
|
507
500
|
def explain(arel, binds = [])
|
508
501
|
sql = "EXPLAIN PLAN FOR #{to_sql(arel, binds)}"
|
@@ -690,3 +683,9 @@ module ActiveRecord::ConnectionAdapters
|
|
690
683
|
end
|
691
684
|
|
692
685
|
end
|
686
|
+
|
687
|
+
module ArJdbc
|
688
|
+
module Oracle
|
689
|
+
Column = ::ActiveRecord::ConnectionAdapters::OracleColumn
|
690
|
+
end
|
691
|
+
end
|
@@ -8,6 +8,9 @@ module ArJdbc
|
|
8
8
|
|
9
9
|
# @private
|
10
10
|
AR4_COMPAT = ::ActiveRecord::VERSION::MAJOR > 3 unless const_defined?(:AR4_COMPAT)
|
11
|
+
# @private
|
12
|
+
AR42_COMPAT = ::ActiveRecord::VERSION::MAJOR > 4 ||
|
13
|
+
( ::ActiveRecord::VERSION::MAJOR == 4 && ::ActiveRecord::VERSION::MINOR >= 2 )
|
11
14
|
|
12
15
|
require 'arjdbc/postgresql/column'
|
13
16
|
require 'arjdbc/postgresql/explain_support'
|
@@ -156,15 +159,12 @@ module ArJdbc
|
|
156
159
|
when Array
|
157
160
|
case column.sql_type
|
158
161
|
when 'point'
|
159
|
-
|
160
|
-
column_class.point_to_string(value)
|
162
|
+
Column.point_to_string(value)
|
161
163
|
when 'json'
|
162
|
-
|
163
|
-
column_class.json_to_string(value)
|
164
|
+
Column.json_to_string(value)
|
164
165
|
else
|
165
166
|
return super(value, column) unless column.array?
|
166
|
-
|
167
|
-
column_class.array_to_string(value, column, self)
|
167
|
+
Column.array_to_string(value, column, self)
|
168
168
|
end
|
169
169
|
when NilClass
|
170
170
|
if column.array? && array_member
|
@@ -177,21 +177,17 @@ module ArJdbc
|
|
177
177
|
when Hash
|
178
178
|
case column.sql_type
|
179
179
|
when 'hstore'
|
180
|
-
|
181
|
-
column_class.hstore_to_string(value)
|
180
|
+
Column.hstore_to_string(value)
|
182
181
|
when 'json'
|
183
|
-
|
184
|
-
column_class.json_to_string(value)
|
182
|
+
Column.json_to_string(value)
|
185
183
|
else super(value, column)
|
186
184
|
end
|
187
185
|
when IPAddr
|
188
186
|
return super unless column.sql_type == 'inet' || column.sql_type == 'cidr'
|
189
|
-
|
190
|
-
column_class.cidr_to_string(value)
|
187
|
+
Column.cidr_to_string(value)
|
191
188
|
when Range
|
192
189
|
return super(value, column) unless /range$/ =~ column.sql_type
|
193
|
-
|
194
|
-
column_class.range_to_string(value)
|
190
|
+
Column.range_to_string(value)
|
195
191
|
else
|
196
192
|
super(value, column)
|
197
193
|
end
|
@@ -238,6 +234,11 @@ module ArJdbc
|
|
238
234
|
:int8range => { :name => "int8range" },
|
239
235
|
}) if AR4_COMPAT
|
240
236
|
|
237
|
+
NATIVE_DATABASE_TYPES.update(
|
238
|
+
:bit => { :name => "bit" },
|
239
|
+
:bit_varying => { :name => "bit varying" }
|
240
|
+
) if AR42_COMPAT
|
241
|
+
|
241
242
|
def native_database_types
|
242
243
|
NATIVE_DATABASE_TYPES
|
243
244
|
end
|
@@ -315,21 +316,13 @@ module ArJdbc
|
|
315
316
|
postgresql_version >= 80200
|
316
317
|
end
|
317
318
|
|
318
|
-
def supports_ddl_transactions
|
319
|
-
true
|
320
|
-
end
|
319
|
+
def supports_ddl_transactions?; true end
|
321
320
|
|
322
|
-
def supports_transaction_isolation
|
323
|
-
true
|
324
|
-
end
|
321
|
+
def supports_transaction_isolation?; true end
|
325
322
|
|
326
|
-
def supports_index_sort_order
|
327
|
-
true
|
328
|
-
end
|
323
|
+
def supports_index_sort_order?; true end
|
329
324
|
|
330
|
-
def supports_partial_index
|
331
|
-
true
|
332
|
-
end if AR4_COMPAT
|
325
|
+
def supports_partial_index?; true end if AR4_COMPAT
|
333
326
|
|
334
327
|
# Range data-types weren't introduced until PostgreSQL 9.2.
|
335
328
|
def supports_ranges?
|
@@ -340,12 +333,13 @@ module ArJdbc
|
|
340
333
|
true
|
341
334
|
end
|
342
335
|
|
336
|
+
# @override
|
337
|
+
def supports_views?; true end
|
338
|
+
|
343
339
|
# NOTE: handled by JdbcAdapter we override only to have save-point in logs :
|
344
340
|
|
345
341
|
# @override
|
346
|
-
def supports_savepoints
|
347
|
-
true
|
348
|
-
end
|
342
|
+
def supports_savepoints?; true end
|
349
343
|
|
350
344
|
# @override
|
351
345
|
def create_savepoint(name = current_savepoint_name(true))
|
@@ -832,43 +826,51 @@ module ArJdbc
|
|
832
826
|
sql_type && sql_type[0, 3] == 'bit' ? quote_bit(value) : super
|
833
827
|
when Array
|
834
828
|
if AR4_COMPAT && column.array? # will be always falsy in AR < 4.0
|
835
|
-
|
836
|
-
"'#{column_class.array_to_string(value, column, self).gsub(/'/, "''")}'"
|
829
|
+
"'#{Column.array_to_string(value, column, self).gsub(/'/, "''")}'"
|
837
830
|
elsif column.type == :json # only in AR-4.0
|
838
|
-
|
839
|
-
super(column_class.json_to_string(value), column)
|
831
|
+
super(Column.json_to_string(value), column)
|
840
832
|
elsif column.type == :point # only in AR-4.0
|
841
|
-
|
842
|
-
super(column_class.point_to_string(value), column)
|
833
|
+
super(Column.point_to_string(value), column)
|
843
834
|
else super
|
844
835
|
end
|
845
836
|
when Hash
|
846
837
|
if column.type == :hstore # only in AR-4.0
|
847
|
-
|
848
|
-
super(column_class.hstore_to_string(value), column)
|
838
|
+
super(Column.hstore_to_string(value), column)
|
849
839
|
elsif column.type == :json # only in AR-4.0
|
850
|
-
|
851
|
-
super(column_class.json_to_string(value), column)
|
840
|
+
super(Column.json_to_string(value), column)
|
852
841
|
else super
|
853
842
|
end
|
854
843
|
when Range
|
855
844
|
sql_type = column.respond_to?(:sql_type) && column.sql_type
|
856
845
|
if sql_type && sql_type[-5, 5] == 'range' && AR4_COMPAT
|
857
|
-
|
858
|
-
escaped = quote_string(column_class.range_to_string(value))
|
846
|
+
escaped = quote_string(Column.range_to_string(value))
|
859
847
|
"'#{escaped}'::#{sql_type}"
|
860
848
|
else super
|
861
849
|
end
|
862
850
|
when IPAddr
|
863
851
|
if column.type == :inet || column.type == :cidr # only in AR-4.0
|
864
|
-
|
865
|
-
super(column_class.cidr_to_string(value), column)
|
852
|
+
super(Column.cidr_to_string(value), column)
|
866
853
|
else super
|
867
854
|
end
|
868
855
|
else
|
869
856
|
super
|
870
857
|
end
|
871
|
-
end
|
858
|
+
end unless AR42_COMPAT
|
859
|
+
|
860
|
+
def quote(value, column = nil)
|
861
|
+
return super unless column
|
862
|
+
|
863
|
+
case value
|
864
|
+
when Float
|
865
|
+
if value.infinite? || value.nan?
|
866
|
+
"'#{value.to_s}'"
|
867
|
+
else
|
868
|
+
super
|
869
|
+
end
|
870
|
+
else
|
871
|
+
super
|
872
|
+
end
|
873
|
+
end if AR42_COMPAT
|
872
874
|
|
873
875
|
# Quotes a string, escaping any ' (single quote) and \ (backslash) chars.
|
874
876
|
# @return [String]
|
@@ -897,7 +899,7 @@ module ArJdbc
|
|
897
899
|
|
898
900
|
def quote_bit(value)
|
899
901
|
"B'#{value}'"
|
900
|
-
end if
|
902
|
+
end if AR4_COMPAT
|
901
903
|
|
902
904
|
def escape_bytea(string)
|
903
905
|
return unless string
|
@@ -1002,7 +1004,7 @@ module ArJdbc
|
|
1002
1004
|
|
1003
1005
|
change_column_default(table_name, column_name, default) if options_include_default?(options)
|
1004
1006
|
change_column_null(table_name, column_name, false, default) if notnull
|
1005
|
-
end if ActiveRecord::VERSION::MAJOR < 4
|
1007
|
+
end if ::ActiveRecord::VERSION::MAJOR < 4
|
1006
1008
|
|
1007
1009
|
# Changes the column of a table.
|
1008
1010
|
def change_column(table_name, column_name, type, options = {})
|
@@ -1077,7 +1079,7 @@ module ArJdbc
|
|
1077
1079
|
|
1078
1080
|
# Returns the list of all column definitions for a table.
|
1079
1081
|
def columns(table_name, name = nil)
|
1080
|
-
|
1082
|
+
pass_cast_type = respond_to?(:lookup_cast_type)
|
1081
1083
|
column_definitions(table_name).map do |row|
|
1082
1084
|
# name, type, default, notnull, oid, fmod
|
1083
1085
|
name = row[0]; type = row[1]; default = row[2]
|
@@ -1091,7 +1093,12 @@ module ArJdbc
|
|
1091
1093
|
elsif default =~ /^\(([-+]?[\d\.]+)\)$/ # e.g. "(-1)" for a negative default
|
1092
1094
|
default = $1
|
1093
1095
|
end
|
1094
|
-
|
1096
|
+
if pass_cast_type
|
1097
|
+
cast_type = lookup_cast_type(type)
|
1098
|
+
Column.new(name, default, cast_type, type, ! notnull, fmod, self)
|
1099
|
+
else
|
1100
|
+
Column.new(name, default, oid, type, ! notnull, fmod, self)
|
1101
|
+
end
|
1095
1102
|
end
|
1096
1103
|
end
|
1097
1104
|
|
@@ -1121,44 +1128,55 @@ module ArJdbc
|
|
1121
1128
|
end
|
1122
1129
|
private :column_definitions
|
1123
1130
|
|
1131
|
+
# @private
|
1132
|
+
TABLES_SQL = 'SELECT tablename FROM pg_tables WHERE schemaname = ANY (current_schemas(false))'
|
1133
|
+
|
1124
1134
|
def tables(name = nil)
|
1125
|
-
select_values(
|
1126
|
-
SELECT tablename
|
1127
|
-
FROM pg_tables
|
1128
|
-
WHERE schemaname = ANY (current_schemas(false))
|
1129
|
-
SQL
|
1135
|
+
select_values(TABLES_SQL, 'SCHEMA')
|
1130
1136
|
end
|
1131
1137
|
|
1138
|
+
# @private
|
1139
|
+
TABLE_EXISTS_SQL_PREFIX = 'SELECT COUNT(*) as table_count FROM pg_class c'
|
1140
|
+
TABLE_EXISTS_SQL_PREFIX << ' LEFT JOIN pg_namespace n ON n.oid = c.relnamespace'
|
1141
|
+
if AR42_COMPAT # -- (r)elation/table, (v)iew, (m)aterialized view
|
1142
|
+
TABLE_EXISTS_SQL_PREFIX << " WHERE c.relkind IN ('r','v','m')"
|
1143
|
+
else
|
1144
|
+
TABLE_EXISTS_SQL_PREFIX << " WHERE c.relkind IN ('r','v')"
|
1145
|
+
end
|
1146
|
+
TABLE_EXISTS_SQL_PREFIX << " AND c.relname = ?"
|
1147
|
+
|
1148
|
+
# Returns true if table exists.
|
1149
|
+
# If the schema is not specified as part of +name+ then it will only find tables within
|
1150
|
+
# the current schema search path (regardless of permissions to access tables in other schemas)
|
1132
1151
|
def table_exists?(name)
|
1133
1152
|
schema, table = extract_schema_and_table(name.to_s)
|
1134
|
-
return false unless table
|
1135
|
-
|
1136
|
-
binds = [[
|
1137
|
-
binds << [
|
1138
|
-
|
1139
|
-
|
1140
|
-
FROM pg_tables
|
1141
|
-
WHERE tablename = ?
|
1142
|
-
AND schemaname = #{schema ? "?" : "ANY (current_schemas(false))"}
|
1143
|
-
SQL
|
1153
|
+
return false unless table
|
1154
|
+
|
1155
|
+
binds = [[nil, table]]
|
1156
|
+
binds << [nil, schema] if schema
|
1157
|
+
|
1158
|
+
sql = "#{TABLE_EXISTS_SQL_PREFIX} AND n.nspname = #{schema ? "?" : 'ANY (current_schemas(false))'}"
|
1144
1159
|
|
1145
1160
|
log(sql, 'SCHEMA', binds) do
|
1146
|
-
@connection.execute_query_raw(sql, binds).first[
|
1161
|
+
@connection.execute_query_raw(sql, binds).first['table_count'] > 0
|
1147
1162
|
end
|
1148
1163
|
end
|
1149
1164
|
|
1165
|
+
def index_name_exists?(table_name, index_name, default)
|
1166
|
+
exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
|
1167
|
+
SELECT COUNT(*)
|
1168
|
+
FROM pg_class t
|
1169
|
+
INNER JOIN pg_index d ON t.oid = d.indrelid
|
1170
|
+
INNER JOIN pg_class i ON d.indexrelid = i.oid
|
1171
|
+
WHERE i.relkind = 'i'
|
1172
|
+
AND i.relname = '#{index_name}'
|
1173
|
+
AND t.relname = '#{table_name}'
|
1174
|
+
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
|
1175
|
+
SQL
|
1176
|
+
end if AR42_COMPAT
|
1177
|
+
|
1150
1178
|
# @private
|
1151
1179
|
IndexDefinition = ::ActiveRecord::ConnectionAdapters::IndexDefinition
|
1152
|
-
if ActiveRecord::VERSION::MAJOR < 3 ||
|
1153
|
-
( ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR <= 1 )
|
1154
|
-
# NOTE: make sure we accept 6 arguments (>= 3.2) as well as 5 (<= 3.1) :
|
1155
|
-
# allow 6 on 3.1 : Struct.new(:table, :name, :unique, :columns, :lengths)
|
1156
|
-
IndexDefinition.class_eval do
|
1157
|
-
def initialize(table, name, unique = nil, columns = nil, lengths = nil, orders = nil)
|
1158
|
-
super(table, name, unique, columns, lengths) # @see {#indexes}
|
1159
|
-
end
|
1160
|
-
end
|
1161
|
-
end
|
1162
1180
|
|
1163
1181
|
# Returns an array of indexes for the given table.
|
1164
1182
|
def indexes(table_name, name = nil)
|
@@ -1169,9 +1187,9 @@ module ArJdbc
|
|
1169
1187
|
INNER JOIN pg_index d ON t.oid = d.indrelid
|
1170
1188
|
INNER JOIN pg_class i ON d.indexrelid = i.oid
|
1171
1189
|
WHERE i.relkind = 'i'
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1190
|
+
AND d.indisprimary = 'f'
|
1191
|
+
AND t.relname = '#{table_name}'
|
1192
|
+
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
|
1175
1193
|
ORDER BY i.relname
|
1176
1194
|
SQL
|
1177
1195
|
|
@@ -1198,13 +1216,13 @@ module ArJdbc
|
|
1198
1216
|
desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
|
1199
1217
|
orders = desc_order_columns.any? ? Hash[ desc_order_columns.map { |column| [column, :desc] } ] : {}
|
1200
1218
|
|
1201
|
-
if ActiveRecord::VERSION::MAJOR > 3 # AR4 supports `where` and `using` index options
|
1219
|
+
if ::ActiveRecord::VERSION::MAJOR > 3 # AR4 supports `where` and `using` index options
|
1202
1220
|
where = inddef.scan(/WHERE (.+)$/).flatten[0]
|
1203
1221
|
using = inddef.scan(/USING (.+?) /).flatten[0].to_sym
|
1204
1222
|
|
1205
1223
|
IndexDefinition.new(table_name, index_name, unique, column_names, [], orders, where, nil, using)
|
1206
1224
|
else
|
1207
|
-
|
1225
|
+
new_index_definition(table_name, index_name, unique, column_names, [], orders)
|
1208
1226
|
end
|
1209
1227
|
end
|
1210
1228
|
end
|
@@ -1212,6 +1230,16 @@ module ArJdbc
|
|
1212
1230
|
result
|
1213
1231
|
end
|
1214
1232
|
|
1233
|
+
# @private
|
1234
|
+
def column_name_for_operation(operation, node)
|
1235
|
+
case operation
|
1236
|
+
when 'maximum' then 'max'
|
1237
|
+
when 'minimum' then 'min'
|
1238
|
+
when 'average' then 'avg'
|
1239
|
+
else operation.downcase
|
1240
|
+
end
|
1241
|
+
end if AR42_COMPAT
|
1242
|
+
|
1215
1243
|
private
|
1216
1244
|
|
1217
1245
|
def translate_exception(exception, message)
|
@@ -1225,20 +1253,12 @@ module ArJdbc
|
|
1225
1253
|
end
|
1226
1254
|
end
|
1227
1255
|
|
1228
|
-
#
|
1256
|
+
# @private `Utils.extract_schema_and_table` from AR
|
1229
1257
|
def extract_schema_and_table(name)
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
schema = nil
|
1235
|
-
end
|
1236
|
-
|
1237
|
-
if name =~ /^"/ # Handle quoted table names
|
1238
|
-
table = name
|
1239
|
-
schema = nil
|
1240
|
-
end
|
1241
|
-
[schema, table]
|
1258
|
+
result = name.scan(/[^".\s]+|"[^"]*"/)[0, 2]
|
1259
|
+
result.each { |m| m.gsub!(/(^"|"$)/, '') }
|
1260
|
+
result.unshift(nil) if result.size == 1 # schema == nil
|
1261
|
+
result # [schema, table]
|
1242
1262
|
end
|
1243
1263
|
|
1244
1264
|
def extract_pg_identifier_from_name(name)
|
@@ -1321,8 +1341,8 @@ module ActiveRecord::ConnectionAdapters
|
|
1321
1341
|
|
1322
1342
|
@table_alias_length = nil
|
1323
1343
|
|
1324
|
-
@use_insert_returning = config.key?(:insert_returning) ?
|
1325
|
-
self.class.type_cast_config_to_boolean(config[:insert_returning]) : nil
|
1344
|
+
@use_insert_returning = @config.key?(:insert_returning) ?
|
1345
|
+
self.class.type_cast_config_to_boolean(@config[:insert_returning]) : nil
|
1326
1346
|
end
|
1327
1347
|
|
1328
1348
|
class ColumnDefinition < ActiveRecord::ConnectionAdapters::ColumnDefinition
|
@@ -1392,6 +1412,18 @@ module ActiveRecord::ConnectionAdapters
|
|
1392
1412
|
def json(name, options = {})
|
1393
1413
|
column(name, 'json', options)
|
1394
1414
|
end
|
1415
|
+
|
1416
|
+
def jsonb(name, options = {})
|
1417
|
+
column(name, :jsonb, options)
|
1418
|
+
end
|
1419
|
+
|
1420
|
+
def bit(name, options)
|
1421
|
+
column(name, 'bit', options)
|
1422
|
+
end
|
1423
|
+
|
1424
|
+
def bit_varying(name, options)
|
1425
|
+
column(name, 'bit varying', options)
|
1426
|
+
end
|
1395
1427
|
end
|
1396
1428
|
|
1397
1429
|
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
|
@@ -1402,7 +1434,7 @@ module ActiveRecord::ConnectionAdapters
|
|
1402
1434
|
options[:default] = options.fetch(:default, 'uuid_generate_v4()')
|
1403
1435
|
options[:primary_key] = true
|
1404
1436
|
column name, type, options
|
1405
|
-
end if ActiveRecord::VERSION::MAJOR > 3 # 3.2 super expects (name)
|
1437
|
+
end if ::ActiveRecord::VERSION::MAJOR > 3 # 3.2 super expects (name)
|
1406
1438
|
|
1407
1439
|
def column(name, type = nil, options = {})
|
1408
1440
|
super
|
@@ -1416,7 +1448,7 @@ module ActiveRecord::ConnectionAdapters
|
|
1416
1448
|
|
1417
1449
|
private
|
1418
1450
|
|
1419
|
-
if ActiveRecord::VERSION::MAJOR > 3
|
1451
|
+
if ::ActiveRecord::VERSION::MAJOR > 3
|
1420
1452
|
|
1421
1453
|
def create_column_definition(name, type)
|
1422
1454
|
ColumnDefinition.new name, type
|
@@ -1445,7 +1477,7 @@ module ActiveRecord::ConnectionAdapters
|
|
1445
1477
|
|
1446
1478
|
def update_table_definition(table_name, base)
|
1447
1479
|
Table.new(table_name, base)
|
1448
|
-
end if ActiveRecord::VERSION::MAJOR > 3
|
1480
|
+
end if ::ActiveRecord::VERSION::MAJOR > 3
|
1449
1481
|
|
1450
1482
|
def jdbc_connection_class(spec)
|
1451
1483
|
::ArJdbc::PostgreSQL.jdbc_connection_class
|
@@ -1456,10 +1488,16 @@ module ActiveRecord::ConnectionAdapters
|
|
1456
1488
|
::ActiveRecord::ConnectionAdapters::PostgreSQLColumn
|
1457
1489
|
end
|
1458
1490
|
|
1459
|
-
if ActiveRecord::VERSION::MAJOR < 4 # Rails 3.x compatibility
|
1491
|
+
if ::ActiveRecord::VERSION::MAJOR < 4 # Rails 3.x compatibility
|
1460
1492
|
PostgreSQLJdbcConnection.raw_array_type = true if PostgreSQLJdbcConnection.raw_array_type? == nil
|
1461
1493
|
PostgreSQLJdbcConnection.raw_hstore_type = true if PostgreSQLJdbcConnection.raw_hstore_type? == nil
|
1462
1494
|
end
|
1463
1495
|
|
1464
1496
|
end
|
1465
1497
|
end
|
1498
|
+
|
1499
|
+
module ArJdbc
|
1500
|
+
module PostgreSQL
|
1501
|
+
Column = ::ActiveRecord::ConnectionAdapters::PostgreSQLColumn
|
1502
|
+
end
|
1503
|
+
end
|