activerecord-jdbc-adapter 1.2.2 → 1.2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arjdbc/postgresql/adapter.rb +36 -0
- data/lib/arjdbc/version.rb +1 -1
- data/test/helper.rb +72 -55
- metadata +3 -3
@@ -330,6 +330,42 @@ module ::ArJdbc
|
|
330
330
|
nil
|
331
331
|
end
|
332
332
|
|
333
|
+
# Insert logic for pre-AR-3.1 adapters
|
334
|
+
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
|
335
|
+
# Extract the table from the insert sql. Yuck.
|
336
|
+
table = sql.split(" ", 4)[2].gsub('"', '')
|
337
|
+
|
338
|
+
# Try an insert with 'returning id' if available (PG >= 8.2)
|
339
|
+
if supports_insert_with_returning? && id_value.nil?
|
340
|
+
pk, sequence_name = *pk_and_sequence_for(table) unless pk
|
341
|
+
if pk
|
342
|
+
sql = substitute_binds(sql, binds)
|
343
|
+
id_value = select_value("#{sql} RETURNING #{quote_column_name(pk)}")
|
344
|
+
clear_query_cache #FIXME: Why now?
|
345
|
+
return id_value
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
# Otherwise, plain insert
|
350
|
+
execute(sql, name, binds)
|
351
|
+
|
352
|
+
# Don't need to look up id_value if we already have it.
|
353
|
+
# (and can't in case of non-sequence PK)
|
354
|
+
unless id_value
|
355
|
+
# If neither pk nor sequence name is given, look them up.
|
356
|
+
unless pk || sequence_name
|
357
|
+
pk, sequence_name = *pk_and_sequence_for(table)
|
358
|
+
end
|
359
|
+
|
360
|
+
# If a pk is given, fallback to default sequence name.
|
361
|
+
# Don't fetch last insert id for a table without a pk.
|
362
|
+
if pk && sequence_name ||= default_sequence_name(table, pk)
|
363
|
+
id_value = last_insert_id(table, sequence_name)
|
364
|
+
end
|
365
|
+
end
|
366
|
+
id_value
|
367
|
+
end
|
368
|
+
|
333
369
|
def primary_key(table)
|
334
370
|
pk_and_sequence = pk_and_sequence_for(table)
|
335
371
|
pk_and_sequence && pk_and_sequence.first
|
data/lib/arjdbc/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -15,73 +15,90 @@ module Kernel
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
|
19
|
-
require 'test/unit'
|
20
|
-
class Test::Unit::TestCase
|
21
|
-
def assert_queries(num = 1, matching = nil)
|
22
|
-
ActiveRecord::SQLCounter.log = []
|
23
|
-
yield
|
24
|
-
ensure
|
25
|
-
queries = nil
|
26
|
-
ActiveRecord::SQLCounter.log.tap {|log| queries = (matching ? log.select {|s| s =~ matching } : log) }
|
27
|
-
assert_equal num, queries.size, "#{queries.size} instead of #{num} queries were executed.#{queries.size == 0 ? '' : "\nQueries:\n#{queries.join("\n")}"}"
|
28
|
-
end
|
29
|
-
end
|
18
|
+
if defined?(::ActiveRecord::ConnectionAdapters)
|
30
19
|
|
31
|
-
|
32
|
-
|
33
|
-
class
|
34
|
-
def
|
35
|
-
|
20
|
+
# assert_queries and SQLCounter taken from rails active_record tests
|
21
|
+
require 'test/unit'
|
22
|
+
class Test::Unit::TestCase
|
23
|
+
def assert_queries(num = 1, matching = nil)
|
24
|
+
ActiveRecord::SQLCounter.log = []
|
25
|
+
yield
|
26
|
+
ensure
|
27
|
+
queries = nil
|
28
|
+
ActiveRecord::SQLCounter.log.tap {|log| queries = (matching ? log.select {|s| s =~ matching } : log) }
|
29
|
+
assert_equal num, queries.size, "#{queries.size} instead of #{num} queries were executed.#{queries.size == 0 ? '' : "\nQueries:\n#{queries.join("\n")}"}"
|
36
30
|
end
|
31
|
+
end
|
37
32
|
|
38
|
-
|
39
|
-
|
40
|
-
|
33
|
+
module ActiveRecord
|
34
|
+
class SQLCounter
|
35
|
+
def self.ignored_sql
|
36
|
+
@@ignored_sql
|
37
|
+
end
|
41
38
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
/^SELECT CAST/,
|
46
|
-
/^SELECT @@IDENTITY/,
|
47
|
-
/^SELECT @@ROWCOUNT/,
|
48
|
-
/^SAVEPOINT/,
|
49
|
-
/^ROLLBACK TO SAVEPOINT/,
|
50
|
-
/^RELEASE SAVEPOINT/,
|
51
|
-
/^SHOW max_identifier_length/,
|
52
|
-
/^BEGIN/,
|
53
|
-
/^COMMIT/
|
54
|
-
]
|
39
|
+
def self.ignored_sql=(value)
|
40
|
+
@@ignored_sql = value
|
41
|
+
end
|
55
42
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
43
|
+
self.ignored_sql = [
|
44
|
+
/^PRAGMA (?!(table_info))/,
|
45
|
+
/^SELECT currval/,
|
46
|
+
/^SELECT CAST/,
|
47
|
+
/^SELECT @@IDENTITY/,
|
48
|
+
/^SELECT @@ROWCOUNT/,
|
49
|
+
/^SAVEPOINT/,
|
50
|
+
/^ROLLBACK TO SAVEPOINT/,
|
51
|
+
/^RELEASE SAVEPOINT/,
|
52
|
+
/^SHOW max_identifier_length/,
|
53
|
+
/^BEGIN/,
|
54
|
+
/^COMMIT/
|
55
|
+
]
|
63
56
|
|
64
|
-
|
65
|
-
|
66
|
-
|
57
|
+
# FIXME: this needs to be refactored so specific database can add their own
|
58
|
+
# ignored SQL. This ignored SQL is for Oracle.
|
59
|
+
ignored_sql.concat [/^select .*nextval/i,
|
60
|
+
/^SAVEPOINT/,
|
61
|
+
/^ROLLBACK TO/,
|
62
|
+
/^\s*select .* from all_triggers/im
|
63
|
+
]
|
67
64
|
|
68
|
-
|
69
|
-
|
70
|
-
|
65
|
+
def self.log=(v)
|
66
|
+
@@log = v
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.log
|
70
|
+
@@log
|
71
|
+
end
|
72
|
+
|
73
|
+
self.log = []
|
71
74
|
|
72
|
-
|
75
|
+
def call(name, start, finish, message_id, values)
|
76
|
+
sql = values[:sql]
|
73
77
|
|
74
|
-
|
75
|
-
|
78
|
+
# FIXME: this seems bad. we should probably have a better way to indicate
|
79
|
+
# the query was cached
|
80
|
+
unless 'CACHE' == values[:name]
|
81
|
+
self.class.log << sql unless self.class.ignored_sql.
|
82
|
+
any? { |r| sql =~ r }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.instance
|
87
|
+
@instance || self.new
|
88
|
+
end
|
89
|
+
end
|
76
90
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
91
|
+
begin
|
92
|
+
require 'active_support/notifications'
|
93
|
+
ActiveSupport::Notifications.subscribe('sql.active_record', SQLCounter.new)
|
94
|
+
rescue LoadError
|
95
|
+
class ActiveRecord::ConnectionAdapters::JdbcAdapter
|
96
|
+
def log_info(sql, name, ms)
|
97
|
+
ActiveRecord::SQLCounter.instance.call(name, ms, ms, nil, :sql => sql)
|
98
|
+
super
|
99
|
+
end
|
82
100
|
end
|
83
101
|
end
|
84
102
|
end
|
85
103
|
|
86
|
-
ActiveSupport::Notifications.subscribe('sql.active_record', SQLCounter.new)
|
87
104
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: activerecord-jdbc-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.2.2
|
5
|
+
version: 1.2.2.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nick Sieger, Ola Bini and JRuby contributors
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-10-19 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: |-
|
@@ -273,7 +273,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
273
273
|
requirements: []
|
274
274
|
|
275
275
|
rubyforge_project: jruby-extras
|
276
|
-
rubygems_version: 1.8.
|
276
|
+
rubygems_version: 1.8.24
|
277
277
|
signing_key:
|
278
278
|
specification_version: 3
|
279
279
|
summary: JDBC adapter for ActiveRecord, for use within JRuby on Rails.
|