composite_primary_keys 0.9.91 → 0.9.92
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.
- data/History.txt +4 -0
- data/lib/composite_primary_keys/associations.rb +28 -0
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/fixtures/db_definitions/db2-create-tables.sql +14 -0
- data/test/fixtures/db_definitions/db2-drop-tables.sql +2 -0
- data/test/fixtures/db_definitions/mysql.sql +15 -1
- data/test/fixtures/db_definitions/oracle.drop.sql +3 -1
- data/test/fixtures/db_definitions/oracle.sql +14 -0
- data/test/fixtures/db_definitions/sqlite.sql +14 -0
- data/test/test_associations.rb +10 -1
- data/tmp/test.db +0 -0
- data/website/index.html +3 -3
- data/website/index.txt +1 -1
- data/website/version-raw.js +1 -1
- data/website/version.js +1 -1
- metadata +2 -2
data/History.txt
CHANGED
|
@@ -251,6 +251,15 @@ module ActiveRecord::Associations
|
|
|
251
251
|
"(#{where_clause})"
|
|
252
252
|
end
|
|
253
253
|
|
|
254
|
+
def full_composite_join_clause(table1, full_keys1, table2, full_keys2)
|
|
255
|
+
full_keys1 = full_keys1.split(CompositePrimaryKeys::ID_SEP) if full_keys1.is_a?(String)
|
|
256
|
+
full_keys2 = full_keys2.split(CompositePrimaryKeys::ID_SEP) if full_keys2.is_a?(String)
|
|
257
|
+
where_clause = [full_keys1, full_keys2].transpose.map do |key_pair|
|
|
258
|
+
"#{table1}.#{key_pair.first}=#{table2}.#{key_pair.last}"
|
|
259
|
+
end.join(" AND ")
|
|
260
|
+
"(#{where_clause})"
|
|
261
|
+
end
|
|
262
|
+
|
|
254
263
|
def full_keys(table_name, keys)
|
|
255
264
|
keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String)
|
|
256
265
|
keys.is_a?(Array) ?
|
|
@@ -269,6 +278,25 @@ module ActiveRecord::Associations
|
|
|
269
278
|
end
|
|
270
279
|
end
|
|
271
280
|
|
|
281
|
+
class HasAndBelongsToManyAssociation < AssociationCollection #:nodoc:
|
|
282
|
+
def construct_sql
|
|
283
|
+
interpolate_sql_options!(@reflection.options, :finder_sql)
|
|
284
|
+
|
|
285
|
+
if @reflection.options[:finder_sql]
|
|
286
|
+
@finder_sql = @reflection.options[:finder_sql]
|
|
287
|
+
else
|
|
288
|
+
@finder_sql = full_columns_equals(
|
|
289
|
+
@reflection.options[:join_table],
|
|
290
|
+
@reflection.primary_key_name,
|
|
291
|
+
@owner.quoted_id)
|
|
292
|
+
@finder_sql << " AND (#{conditions})" if conditions
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
@join_sql = "INNER JOIN #{@reflection.options[:join_table]} ON " +
|
|
296
|
+
full_composite_join_clause(@reflection.klass.table_name, @reflection.klass.primary_key, @reflection.options[:join_table], @reflection.association_foreign_key)
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
272
300
|
class HasManyAssociation < AssociationCollection #:nodoc:
|
|
273
301
|
def construct_sql
|
|
274
302
|
case
|
|
@@ -97,3 +97,17 @@ create table kitchen_sinks (
|
|
|
97
97
|
a_string varchar(100),
|
|
98
98
|
primary key (id_1, id_2)
|
|
99
99
|
);
|
|
100
|
+
|
|
101
|
+
create table restaurants (
|
|
102
|
+
franchise_id integer not null,
|
|
103
|
+
store_id integer not null,
|
|
104
|
+
name varchar(100),
|
|
105
|
+
primary key (franchise_id, store_id)
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
create table restaurants_suburbs (
|
|
109
|
+
franchise_id integer not null,
|
|
110
|
+
store_id integer not null,
|
|
111
|
+
city_id integer not null,
|
|
112
|
+
suburb_id integer not null
|
|
113
|
+
);
|
|
@@ -121,4 +121,18 @@ create table kitchen_sinks (
|
|
|
121
121
|
a_date date,
|
|
122
122
|
a_string varchar(100),
|
|
123
123
|
primary key (id_1, id_2)
|
|
124
|
-
) TYPE=InnoDB
|
|
124
|
+
) TYPE=InnoDB;
|
|
125
|
+
|
|
126
|
+
create table restaurants (
|
|
127
|
+
franchise_id int(11) not null,
|
|
128
|
+
store_id int(11) not null,
|
|
129
|
+
name varchar(100),
|
|
130
|
+
primary key (franchise_id, store_id)
|
|
131
|
+
) TYPE=InnoDB;
|
|
132
|
+
|
|
133
|
+
create table restaurants_suburbs (
|
|
134
|
+
franchise_id int(11) not null,
|
|
135
|
+
store_id int(11) not null,
|
|
136
|
+
city_id int(11) not null,
|
|
137
|
+
suburb_id int(11) not null
|
|
138
|
+
) TYPE=InnoDB;
|
|
@@ -143,4 +143,18 @@ create table kitchen_sinks (
|
|
|
143
143
|
a_date date,
|
|
144
144
|
a_string varchar(100),
|
|
145
145
|
constraint kitchen_sinks_pk primary key(id_1, id_2)
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
create table restaurants (
|
|
149
|
+
franchise_id number(11) not null,
|
|
150
|
+
store_id number(11) not null,
|
|
151
|
+
name varchar(100),
|
|
152
|
+
constraint restaurants_pk primary key (franchise_id, store_id)
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
create table restaurants_suburbs (
|
|
156
|
+
franchise_id number(11) not null,
|
|
157
|
+
store_id number(11) not null,
|
|
158
|
+
city_id number(11) not null,
|
|
159
|
+
suburb_id number(11) not null
|
|
146
160
|
);
|
|
@@ -110,4 +110,18 @@ create table kitchen_sinks (
|
|
|
110
110
|
a_date date,
|
|
111
111
|
a_string varchar(100),
|
|
112
112
|
primary key (id_1, id_2)
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
create table restaurants (
|
|
116
|
+
franchise_id integer not null,
|
|
117
|
+
store_id integer not null,
|
|
118
|
+
name varchar(100),
|
|
119
|
+
primary key (franchise_id, store_id)
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
create table restaurants_suburbs (
|
|
123
|
+
franchise_id integer not null,
|
|
124
|
+
store_id integer not null,
|
|
125
|
+
city_id integer not null,
|
|
126
|
+
suburb_id integer not null
|
|
113
127
|
);
|
data/test/test_associations.rb
CHANGED
|
@@ -4,9 +4,10 @@ require 'fixtures/tariff'
|
|
|
4
4
|
require 'fixtures/product_tariff'
|
|
5
5
|
require 'fixtures/suburb'
|
|
6
6
|
require 'fixtures/street'
|
|
7
|
+
require 'fixtures/restaurant'
|
|
7
8
|
|
|
8
9
|
class TestAssociations < Test::Unit::TestCase
|
|
9
|
-
fixtures :products, :tariffs, :product_tariffs, :suburbs, :streets
|
|
10
|
+
fixtures :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants, :restaurants_suburbs
|
|
10
11
|
|
|
11
12
|
def test_quoted_table_columns
|
|
12
13
|
assert_equal "product_tariffs.product_id,product_tariffs.tariff_id,product_tariffs.tariff_start_date",
|
|
@@ -107,4 +108,12 @@ class TestAssociations < Test::Unit::TestCase
|
|
|
107
108
|
@suburb = Suburb.find([2, 1], :include => :first_streets)
|
|
108
109
|
assert_equal 1, @suburb.first_streets.size
|
|
109
110
|
end
|
|
111
|
+
|
|
112
|
+
def test_has_and_belongs_to_many
|
|
113
|
+
@restaurant = Restaurant.find([1,1])
|
|
114
|
+
assert_equal 2, @restaurant.suburbs.size
|
|
115
|
+
|
|
116
|
+
@restaurant = Restaurant.find([1,1], :include => :suburbs)
|
|
117
|
+
assert_equal 2, @restaurant.suburbs.size
|
|
118
|
+
end
|
|
110
119
|
end
|
data/tmp/test.db
CHANGED
|
Binary file
|
data/website/index.html
CHANGED
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
<h1>Composite Primary Keys</h1>
|
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/compositekeys"; return false'>
|
|
35
35
|
Get Version
|
|
36
|
-
<a href="http://rubyforge.org/projects/compositekeys" class="numbers">0.9.
|
|
36
|
+
<a href="http://rubyforge.org/projects/compositekeys" class="numbers">0.9.92</a>
|
|
37
37
|
</div>
|
|
38
38
|
<h1>→ Ruby on Rails</h1>
|
|
39
39
|
|
|
@@ -81,7 +81,7 @@ to the database layer of Rails – <a href="http://wiki.rubyonrails.com/rail
|
|
|
81
81
|
<span class="ident">set_primary_keys</span> <span class="symbol">:user_id</span><span class="punct">,</span> <span class="symbol">:group_id</span>
|
|
82
82
|
<span class="ident">belongs_to</span> <span class="symbol">:user</span>
|
|
83
83
|
<span class="ident">belongs_to</span> <span class="symbol">:group</span>
|
|
84
|
-
<span class="ident">has_many</span> <span class="symbol">:statuses</span><span class="punct">,</span> <span class="symbol">:
|
|
84
|
+
<span class="ident">has_many</span> <span class="symbol">:statuses</span><span class="punct">,</span> <span class="symbol">:class_name</span> <span class="punct">=></span> <span class="punct">'</span><span class="string">MembershipStatus</span><span class="punct">',</span> <span class="symbol">:foreign_key</span> <span class="punct">=></span> <span class="punct">[</span><span class="symbol">:user_id</span><span class="punct">,</span> <span class="symbol">:group_id</span><span class="punct">]</span>
|
|
85
85
|
<span class="keyword">end</span></pre></p>
|
|
86
86
|
|
|
87
87
|
|
|
@@ -291,7 +291,7 @@ other stories and things.</p>
|
|
|
291
291
|
|
|
292
292
|
<p>Comments are welcome. Send an email to <a href="mailto:drnicwilliams@gmail.com">Dr Nic Williams</a>.</p>
|
|
293
293
|
<p class="coda">
|
|
294
|
-
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>,
|
|
294
|
+
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 25th February 2008<br>
|
|
295
295
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
|
296
296
|
</p>
|
|
297
297
|
</div>
|
data/website/index.txt
CHANGED
|
@@ -33,7 +33,7 @@ A model with composite primary keys would look like...
|
|
|
33
33
|
set_primary_keys :user_id, :group_id
|
|
34
34
|
belongs_to :user
|
|
35
35
|
belongs_to :group
|
|
36
|
-
has_many :statuses, :
|
|
36
|
+
has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
|
|
37
37
|
end</pre>
|
|
38
38
|
|
|
39
39
|
A model associated with a composite key model would be defined like...
|
data/website/version-raw.js
CHANGED
data/website/version.js
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: composite_primary_keys
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.9.
|
|
4
|
+
version: 0.9.92
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dr Nic Williams
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2008-02-
|
|
12
|
+
date: 2008-02-25 00:00:00 +10:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|