composite_primary_keys 12.0.2 → 12.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.rdoc +13 -0
- data/README.rdoc +3 -2
- data/lib/composite_primary_keys.rb +56 -56
- data/lib/composite_primary_keys/active_model/attribute_assignment.rb +19 -0
- data/lib/composite_primary_keys/arel/sqlserver.rb +1 -3
- data/lib/composite_primary_keys/associations/through_association.rb +2 -1
- data/lib/composite_primary_keys/attribute_methods.rb +1 -1
- data/lib/composite_primary_keys/attribute_methods/primary_key.rb +13 -0
- data/lib/composite_primary_keys/base.rb +11 -0
- data/lib/composite_primary_keys/composite_arrays.rb +0 -8
- data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +8 -3
- data/lib/composite_primary_keys/connection_adapters/mysql/database_statements.rb +24 -0
- data/lib/composite_primary_keys/connection_adapters/sqlserver/database_statements.rb +29 -11
- data/lib/composite_primary_keys/persistence.rb +2 -2
- data/lib/composite_primary_keys/relation.rb +36 -27
- data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +1 -1
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/abstract_unit.rb +3 -2
- data/test/fixtures/article.rb +4 -0
- data/test/fixtures/articles.yml +4 -3
- data/test/fixtures/comment.rb +1 -3
- data/test/fixtures/comments.yml +10 -9
- data/test/fixtures/db_definitions/db2-create-tables.sql +0 -14
- data/test/fixtures/db_definitions/db2-drop-tables.sql +1 -3
- data/test/fixtures/db_definitions/mysql.sql +6 -43
- data/test/fixtures/db_definitions/oracle.drop.sql +3 -9
- data/test/fixtures/db_definitions/oracle.sql +12 -48
- data/test/fixtures/db_definitions/postgresql.sql +7 -44
- data/test/fixtures/db_definitions/sqlite.sql +6 -42
- data/test/fixtures/db_definitions/sqlserver.sql +5 -41
- data/test/fixtures/department.rb +8 -3
- data/test/fixtures/departments.yml +4 -4
- data/test/fixtures/readings.yml +2 -2
- data/test/fixtures/restaurants_suburbs.yml +1 -1
- data/test/fixtures/streets.yml +2 -2
- data/test/fixtures/suburbs.yml +2 -2
- data/test/fixtures/user.rb +3 -2
- data/test/test_associations.rb +30 -23
- data/test/test_create.rb +15 -18
- data/test/test_delete.rb +3 -3
- data/test/test_exists.rb +5 -5
- data/test/test_find.rb +2 -2
- data/test/test_habtm.rb +2 -2
- data/test/test_ids.rb +2 -6
- data/test/test_nested_attributes.rb +0 -57
- data/test/test_polymorphic.rb +29 -13
- data/test/test_preload.rb +4 -3
- data/test/test_serialize.rb +2 -2
- data/test/test_update.rb +19 -1
- metadata +5 -19
- data/test/fixtures/hack.rb +0 -5
- data/test/fixtures/hacks.yml +0 -3
- data/test/fixtures/pk_called_id.rb +0 -5
- data/test/fixtures/pk_called_ids.yml +0 -11
- data/test/fixtures/reference_code_using_composite_key_alias.rb +0 -8
- data/test/fixtures/reference_code_using_simple_key_alias.rb +0 -8
- data/test/fixtures/seat.rb +0 -5
- data/test/fixtures/seats.yml +0 -9
- data/test/fixtures/topic.rb +0 -6
- data/test/fixtures/topic_source.rb +0 -7
- data/test/test_aliases.rb +0 -18
- data/test/test_enum.rb +0 -21
- data/test/test_suite.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c7ee90af81fc2755ecd8b49d91808047736a1ec2d99fea0961bdc6813e9c6c1
|
4
|
+
data.tar.gz: e0ff0a387dbd7ff3b7d3c0777ecf1fd54d4a5637128179c6a0788f71481079c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e9a6b4eb511b992f4ed4aa971fb6eb8a19af49cff1b06998f4d7093a155f2b6c39674c444d70595833ed887bbe2406d665f5c48751316c743597ae442c0d44fd
|
7
|
+
data.tar.gz: dbdb54e9141e4d8dd959712f9f7f52f43ce22e7b184f0cc274769ca241418af4fd9775c7532fa492f1988c61fcb6a2b69143fe94889ebbf1b9a76c3b30cb0bdd
|
data/History.rdoc
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
== 12.0.3 (2020-11-11)
|
2
|
+
* Prevents infinite loops with gems which modify the 'attributes' method (Nicholas Guarino)
|
3
|
+
* Improve delete_all and update_all queries (Charlie Savage)
|
4
|
+
* Improve Oracle support (Charlie Savage)
|
5
|
+
* Improve SQL Server support. Note delete_all and update_all queries still do not work due to
|
6
|
+
quirks in SQL Server syntax (Charlie Savage)
|
7
|
+
* Improve support for polymorphic associations (Charlie Savage)
|
8
|
+
* Update fixtures and tests to include a model that uses an :id column as part of a composite
|
9
|
+
key (Charlie Savage)
|
10
|
+
* Fixed bug where id values were not correctly mapped between primary and foreign keys in associations (Charlie Savage)
|
11
|
+
* Cleanup how the gem is loaded (Charlie Savage)
|
12
|
+
* Remove older, unused or minimally used fixtures (Charlie Savage)
|
13
|
+
|
1
14
|
== 12.0.2 (2020-05-19)
|
2
15
|
* Remove deprecation warning for Ruby 2.7 due to keyword parameters (Alexandru Anca )
|
3
16
|
* Use sqlite for default in tests (Charlie Savage)
|
data/README.rdoc
CHANGED
@@ -113,7 +113,8 @@ divided into the following bundler groups:
|
|
113
113
|
Since it is likely you do not have all the above databases installed on your computer, you want to install just the
|
114
114
|
gems for your database. For example, to test postgresql you would install the appropriate gems like this:
|
115
115
|
|
116
|
-
bundler
|
116
|
+
bundler config set --local without "mysql oracle sqlite sqlserver"
|
117
|
+
bundler install
|
117
118
|
|
118
119
|
Once you have installed the appropriate gems, the next step is to create the test database. There is a rake
|
119
120
|
command for each database. Using our example:
|
@@ -132,7 +133,7 @@ Finally, to run tests:
|
|
132
133
|
|
133
134
|
rake postgresql:test
|
134
135
|
|
135
|
-
Travis build status: {<img src="https://travis-ci.
|
136
|
+
Travis build status: {<img src="https://travis-ci.com/composite-primary-keys/composite_primary_keys.svg" alt="Build Status" />}[https://travis-ci.com/composite-primary-keys/composite_primary_keys]
|
136
137
|
|
137
138
|
=== DB2
|
138
139
|
|
@@ -21,16 +21,17 @@
|
|
21
21
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
22
|
#++
|
23
23
|
|
24
|
-
$:.unshift(File.dirname(__FILE__)) unless
|
25
|
-
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
26
|
-
|
27
24
|
unless defined?(ActiveRecord)
|
28
25
|
require 'rubygems'
|
29
26
|
gem 'activerecord', '~>6.0.0'
|
30
27
|
require 'active_record'
|
31
28
|
end
|
32
29
|
|
33
|
-
#
|
30
|
+
# ActiveModel files we override
|
31
|
+
# _write_attribute
|
32
|
+
require 'active_model/attribute_assignment'
|
33
|
+
|
34
|
+
# ActiveRecord files we override
|
34
35
|
require 'active_record/attribute_methods'
|
35
36
|
require 'active_record/autosave_association'
|
36
37
|
require 'active_record/counter_cache'
|
@@ -64,55 +65,54 @@ require 'active_record/connection_adapters/postgresql/database_statements'
|
|
64
65
|
|
65
66
|
require 'active_record/relation/where_clause'
|
66
67
|
|
67
|
-
# CPK
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
#require 'composite_primary_keys/arel/sqlserver'
|
68
|
+
# CPK overrides
|
69
|
+
require_relative 'composite_primary_keys/active_model/attribute_assignment'
|
70
|
+
require_relative 'composite_primary_keys/attribute_methods'
|
71
|
+
require_relative 'composite_primary_keys/autosave_association'
|
72
|
+
require_relative 'composite_primary_keys/persistence'
|
73
|
+
require_relative 'composite_primary_keys/base'
|
74
|
+
require_relative 'composite_primary_keys/core'
|
75
|
+
require_relative 'composite_primary_keys/composite_arrays'
|
76
|
+
require_relative 'composite_primary_keys/composite_predicates'
|
77
|
+
require_relative 'composite_primary_keys/counter_cache'
|
78
|
+
require_relative 'composite_primary_keys/fixtures'
|
79
|
+
require_relative 'composite_primary_keys/reflection'
|
80
|
+
require_relative 'composite_primary_keys/relation'
|
81
|
+
require_relative 'composite_primary_keys/sanitization'
|
82
|
+
require_relative 'composite_primary_keys/transactions'
|
83
|
+
require_relative 'composite_primary_keys/version'
|
84
|
+
|
85
|
+
require_relative 'composite_primary_keys/associations/association'
|
86
|
+
require_relative 'composite_primary_keys/associations/association_scope'
|
87
|
+
require_relative 'composite_primary_keys/associations/foreign_association'
|
88
|
+
require_relative 'composite_primary_keys/associations/has_many_association'
|
89
|
+
require_relative 'composite_primary_keys/associations/has_many_through_association'
|
90
|
+
require_relative 'composite_primary_keys/associations/join_dependency'
|
91
|
+
require_relative 'composite_primary_keys/associations/preloader/association'
|
92
|
+
require_relative 'composite_primary_keys/associations/collection_association'
|
93
|
+
require_relative 'composite_primary_keys/associations/through_association'
|
94
|
+
|
95
|
+
require_relative 'composite_primary_keys/attribute_methods/primary_key'
|
96
|
+
require_relative 'composite_primary_keys/attribute_methods/read'
|
97
|
+
require_relative 'composite_primary_keys/attribute_methods/write'
|
98
|
+
require_relative 'composite_primary_keys/nested_attributes'
|
99
|
+
|
100
|
+
require_relative 'composite_primary_keys/connection_adapters/abstract/database_statements'
|
101
|
+
require_relative 'composite_primary_keys/connection_adapters/abstract_adapter'
|
102
|
+
require_relative 'composite_primary_keys/connection_adapters/mysql/database_statements'
|
103
|
+
require_relative 'composite_primary_keys/connection_adapters/postgresql/database_statements'
|
104
|
+
require_relative 'composite_primary_keys/connection_adapters/sqlserver/database_statements'
|
105
|
+
|
106
|
+
require_relative 'composite_primary_keys/relation/batches'
|
107
|
+
require_relative 'composite_primary_keys/relation/where_clause'
|
108
|
+
require_relative 'composite_primary_keys/relation/calculations'
|
109
|
+
require_relative 'composite_primary_keys/relation/finder_methods'
|
110
|
+
require_relative 'composite_primary_keys/relation/predicate_builder/association_query_value'
|
111
|
+
require_relative 'composite_primary_keys/relation/query_methods'
|
112
|
+
|
113
|
+
require_relative 'composite_primary_keys/validations/uniqueness'
|
114
|
+
|
115
|
+
require_relative 'composite_primary_keys/composite_relation'
|
116
|
+
|
117
|
+
require_relative 'composite_primary_keys/arel/to_sql'
|
118
|
+
require_relative 'composite_primary_keys/arel/sqlserver'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module AttributeAssignment
|
3
|
+
def _assign_attribute(k, v)
|
4
|
+
# CPK. This is super ugly, but if a table has a composite key where one of the fields is named :id we need
|
5
|
+
# to handle it as a single value. Otherwise, we would call the id=(value) method which is expecting
|
6
|
+
# and array of values.
|
7
|
+
if k == 'id' && self.kind_of?(ActiveRecord::Base) && self.composite? && !self.column_for_attribute(k).null
|
8
|
+
self._write_attribute(k, v)
|
9
|
+
else
|
10
|
+
setter = :"#{k}="
|
11
|
+
if respond_to?(setter)
|
12
|
+
public_send(setter, v)
|
13
|
+
else
|
14
|
+
raise UnknownAttributeError.new(self, k)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -5,7 +5,8 @@ module ActiveRecord
|
|
5
5
|
|
6
6
|
def construct_join_attributes(*records)
|
7
7
|
# CPK
|
8
|
-
|
8
|
+
is_composite = self.source_reflection.polymorphic? ? source_reflection.active_record.composite? : source_reflection.klass.composite?
|
9
|
+
if is_composite
|
9
10
|
ensure_mutable
|
10
11
|
|
11
12
|
ids = records.map do |record|
|
@@ -1,6 +1,19 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module AttributeMethods
|
3
3
|
module PrimaryKey
|
4
|
+
module ClassMethods
|
5
|
+
def suppress_composite_primary_key(pk)
|
6
|
+
pk
|
7
|
+
# return pk unless pk.is_a?(Array)
|
8
|
+
#
|
9
|
+
# warn <<~WARNING
|
10
|
+
# WARNING: Active Record does not support composite primary key.
|
11
|
+
#
|
12
|
+
# #{table_name} has composite primary key. Composite primary key is ignored.
|
13
|
+
# WARNING
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
4
17
|
# Returns the primary key previous value.
|
5
18
|
def id_was
|
6
19
|
sync_with_transaction_state
|
@@ -89,6 +89,17 @@ module ActiveRecord
|
|
89
89
|
end
|
90
90
|
alias_method :ids, :id
|
91
91
|
|
92
|
+
# This is overridden purely for json serialization support. If the model is composite
|
93
|
+
# and one of the keys is id, then we don't want to call the id method, instead we want
|
94
|
+
# to get the id attribute value
|
95
|
+
def read_attribute_for_serialization(attribute)
|
96
|
+
if self.composite? && attribute == 'id'
|
97
|
+
read_attribute(attribute)
|
98
|
+
else
|
99
|
+
send(attribute)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
92
103
|
def ids_hash
|
93
104
|
self.class.primary_key.zip(ids).inject(Hash.new) do |hash, (key, value)|
|
94
105
|
hash[key] = value
|
@@ -49,14 +49,6 @@ module CompositePrimaryKeys
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
def in(other)
|
53
|
-
case other
|
54
|
-
when Arel::SelectManager
|
55
|
-
Arel::Nodes::In.new(self, other.ast)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
|
60
52
|
def to_s
|
61
53
|
# Doing this makes it easier to parse Base#[](attr_name)
|
62
54
|
map { |key| Utils.escape_string_key(key.to_s) }.join(ID_SEP)
|
@@ -5,9 +5,14 @@ module ActiveRecord
|
|
5
5
|
sql, binds = to_sql_and_binds(arel, binds)
|
6
6
|
value = exec_insert(sql, name, binds, pk, sequence_name)
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
if pk.is_a?(Array) && !value.empty?
|
9
|
+
# This is a CPK model and the query result is not empty. Thus we can figure out the new ids for each
|
10
|
+
# auto incremented field
|
11
|
+
id_value || pk.map {|key| value.first[key]}
|
12
|
+
elsif pk.is_a?(Array)
|
13
|
+
# This is CPK, but we don't know what autoincremented fields were updated. So return nil, which means
|
14
|
+
# the existing id_value of the model will be used.
|
15
|
+
id_value || Array.new(pk.size)
|
11
16
|
else
|
12
17
|
id_value || last_inserted_id(value)
|
13
18
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module MySQL
|
6
|
+
module DatabaseStatements
|
7
|
+
def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
|
8
|
+
sql, binds = to_sql_and_binds(arel, binds)
|
9
|
+
value = exec_insert(sql, name, binds, pk, sequence_name)
|
10
|
+
|
11
|
+
# CPK
|
12
|
+
if pk.is_a?(Array)
|
13
|
+
pk.map do |key|
|
14
|
+
column = self.column_for(arel.ast.relation.name, key)
|
15
|
+
column.auto_increment? ? last_inserted_id(value) : nil
|
16
|
+
end
|
17
|
+
else
|
18
|
+
id_value || last_inserted_id(value)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -3,19 +3,37 @@ module ActiveRecord
|
|
3
3
|
module SQLServer
|
4
4
|
module DatabaseStatements
|
5
5
|
def sql_for_insert(sql, pk, binds)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
# sql.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk}"
|
10
|
-
quoted_pks = [pk].flatten.map {|pk| "INSERTED.#{SQLServer::Utils.extract_identifiers(pk).quoted}"}
|
11
|
-
sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT #{quoted_pks.join(", ")}"
|
12
|
-
else
|
13
|
-
"#{sql}; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
|
6
|
+
if pk.nil?
|
7
|
+
table_name = query_requires_identity_insert?(sql)
|
8
|
+
pk = primary_key(table_name)
|
14
9
|
end
|
15
10
|
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
sql = if pk && use_output_inserted? && !database_prefix_remote_server?
|
12
|
+
# CPK
|
13
|
+
#quoted_pk = SQLServer::Utils.extract_identifiers(pk).quoted
|
14
|
+
quoted_pk = Array(pk).map {|subkey| SQLServer::Utils.extract_identifiers(subkey).quoted}
|
15
|
+
|
16
|
+
table_name ||= get_table_name(sql)
|
17
|
+
exclude_output_inserted = exclude_output_inserted_table_name?(table_name, sql)
|
18
|
+
|
19
|
+
if exclude_output_inserted
|
20
|
+
id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? "bigint" : exclude_output_inserted
|
21
|
+
<<~SQL.squish
|
22
|
+
DECLARE @ssaIdInsertTable table (#{quoted_pk} #{id_sql_type});
|
23
|
+
# CPK
|
24
|
+
# #{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
|
25
|
+
#{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk.join(', INSERTED.')} INTO @ssaIdInsertTable"}
|
26
|
+
SELECT CAST(#{quoted_pk.join(',')} AS #{id_sql_type}) FROM @ssaIdInsertTable
|
27
|
+
SQL
|
28
|
+
else
|
29
|
+
# CPK
|
30
|
+
# sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk}"
|
31
|
+
sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk.join(', INSERTED.')}"
|
32
|
+
end
|
33
|
+
else
|
34
|
+
"#{sql}; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
|
35
|
+
end
|
36
|
+
super
|
19
37
|
end
|
20
38
|
end
|
21
39
|
end
|
@@ -65,8 +65,8 @@ module ActiveRecord
|
|
65
65
|
)
|
66
66
|
|
67
67
|
# CPK
|
68
|
-
if self.composite?
|
69
|
-
self.id = self.id.zip(Array(new_id)).map {|id1, id2|
|
68
|
+
if self.composite?
|
69
|
+
self.id = self.id.zip(Array(new_id)).map {|id1, id2| id2.nil? ? id1 : id2}
|
70
70
|
else
|
71
71
|
self.id ||= new_id if self.class.primary_key
|
72
72
|
end
|
@@ -27,15 +27,8 @@ module ActiveRecord
|
|
27
27
|
stmt = Arel::UpdateManager.new
|
28
28
|
# CPK
|
29
29
|
if @klass.composite?
|
30
|
-
stmt.table(
|
31
|
-
|
32
|
-
arel_attributes = primary_keys.map do |key|
|
33
|
-
arel_attribute(key)
|
34
|
-
end.to_composite_keys
|
35
|
-
|
36
|
-
subselect = subquery_for(arel_attributes, arel)
|
37
|
-
|
38
|
-
stmt.wheres = [arel_attributes.in(subselect)]
|
30
|
+
stmt.table(arel_table)
|
31
|
+
cpk_in_subquery(stmt)
|
39
32
|
else
|
40
33
|
stmt.table(arel.join_sources.empty? ? table : arel.source)
|
41
34
|
stmt.key = arel_attribute(primary_key)
|
@@ -75,22 +68,16 @@ module ActiveRecord
|
|
75
68
|
end
|
76
69
|
|
77
70
|
stmt = Arel::DeleteManager.new
|
78
|
-
# CPK
|
79
|
-
if @klass.composite?
|
80
|
-
stmt.from(table)
|
81
|
-
|
82
|
-
arel_attributes = primary_keys.map do |key|
|
83
|
-
arel_attribute(key)
|
84
|
-
end.to_composite_keys
|
85
71
|
|
86
|
-
|
87
|
-
|
88
|
-
stmt
|
72
|
+
if @klass.composite?
|
73
|
+
stmt.from(arel_table)
|
74
|
+
cpk_in_subquery(stmt)
|
89
75
|
else
|
90
76
|
stmt.from(arel.join_sources.empty? ? table : arel.source)
|
91
77
|
stmt.key = arel_attribute(primary_key)
|
92
78
|
stmt.wheres = arel.constraints
|
93
79
|
end
|
80
|
+
|
94
81
|
stmt.take(arel.limit)
|
95
82
|
stmt.offset(arel.offset)
|
96
83
|
stmt.order(*arel.orders)
|
@@ -102,17 +89,39 @@ module ActiveRecord
|
|
102
89
|
end
|
103
90
|
|
104
91
|
# CPK
|
105
|
-
def
|
106
|
-
|
107
|
-
|
92
|
+
def cpk_in_subquery(stmt)
|
93
|
+
# Setup the subquery
|
94
|
+
subquery = arel.clone
|
95
|
+
subquery.projections = primary_keys.map do |key|
|
96
|
+
arel_table[key]
|
97
|
+
end
|
108
98
|
|
109
|
-
|
110
|
-
|
111
|
-
|
99
|
+
where_fields = primary_keys.map do |key|
|
100
|
+
arel_table[key]
|
101
|
+
end
|
102
|
+
where = Arel::Nodes::Grouping.new(where_fields).in(subquery)
|
103
|
+
stmt.wheres = [where]
|
104
|
+
end
|
112
105
|
|
113
|
-
|
106
|
+
def cpk_exists_subquery(stmt)
|
107
|
+
# Alias the outer table so we can join to in from the subquery
|
108
|
+
aliased_table = arel_table.alias("cpk_outer_relation")
|
109
|
+
stmt.table(aliased_table)
|
114
110
|
|
115
|
-
|
111
|
+
# Setup the subquery
|
112
|
+
subquery = arel.clone
|
113
|
+
subquery.projections = primary_keys.map do |key|
|
114
|
+
arel_table[key]
|
115
|
+
end
|
116
|
+
|
117
|
+
# Setup correlation to the outer query via where clauses
|
118
|
+
primary_keys.map do |key|
|
119
|
+
outer_attribute = aliased_table[key]
|
120
|
+
inner_attribute = arel_table[key]
|
121
|
+
where = outer_attribute.eq(inner_attribute)
|
122
|
+
subquery.where(where)
|
123
|
+
end
|
124
|
+
stmt.wheres = [Arel::Nodes::Exists.new(subquery)]
|
116
125
|
end
|
117
126
|
end
|
118
127
|
end
|