composite_primary_keys 1.0.8 → 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +10 -0
- data/lib/composite_primary_keys/association_preload.rb +67 -61
- data/lib/composite_primary_keys/associations.rb +428 -409
- data/lib/composite_primary_keys/attribute_methods.rb +3 -3
- data/lib/composite_primary_keys/base.rb +128 -110
- data/lib/composite_primary_keys/calculations.rb +8 -8
- data/lib/composite_primary_keys/composite_arrays.rb +6 -5
- data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +44 -2
- data/lib/composite_primary_keys/fixtures.rb +2 -3
- data/lib/composite_primary_keys/version.rb +1 -1
- data/scripts/console.rb +34 -11
- data/test/fixtures/db_definitions/db2-create-tables.sql +2 -2
- data/test/fixtures/db_definitions/mysql.sql +156 -155
- data/test/fixtures/db_definitions/oracle.drop.sql +4 -1
- data/test/fixtures/db_definitions/oracle.sql +117 -119
- data/test/fixtures/db_definitions/postgresql.sql +164 -98
- data/test/fixtures/db_definitions/sqlite.sql +74 -73
- data/test/fixtures/street.rb +2 -2
- data/test/fixtures/streets.yml +14 -14
- data/test/test_associations.rb +0 -5
- data/test/test_attribute_methods.rb +2 -2
- data/test/test_create.rb +68 -68
- data/test/test_delete.rb +19 -10
- data/test/test_ids.rb +7 -0
- data/tmp/test.db +0 -0
- data/website/template.js +3 -3
- metadata +2 -2
@@ -1,21 +1,22 @@
|
|
1
1
|
module CompositePrimaryKeys
|
2
|
-
ID_SEP
|
2
|
+
ID_SEP = ','
|
3
3
|
ID_SET_SEP = ';'
|
4
|
+
|
4
5
|
module ArrayExtension
|
5
6
|
def to_composite_keys
|
6
7
|
CompositeKeys.new(self)
|
7
8
|
end
|
8
|
-
|
9
|
+
|
9
10
|
def to_composite_ids
|
10
11
|
CompositeIds.new(self)
|
11
12
|
end
|
12
13
|
end
|
13
|
-
|
14
|
+
|
14
15
|
class CompositeArray < Array
|
15
16
|
def to_s
|
16
17
|
join(ID_SEP)
|
17
18
|
end
|
18
|
-
end
|
19
|
+
end
|
19
20
|
|
20
21
|
class CompositeKeys < CompositeArray
|
21
22
|
|
@@ -26,4 +27,4 @@ module CompositePrimaryKeys
|
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
|
-
Array.send(:include, CompositePrimaryKeys::ArrayExtension)
|
30
|
+
Array.send(:include, CompositePrimaryKeys::ArrayExtension)
|
@@ -4,8 +4,50 @@ module ActiveRecord
|
|
4
4
|
|
5
5
|
# This mightn't be in Core, but count(distinct x,y) doesn't work for me
|
6
6
|
def supports_count_distinct? #:nodoc:
|
7
|
-
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def concat(*columns)
|
11
|
+
columns = columns.map { |c| "CAST(#{c} AS varchar)" }
|
12
|
+
"(#{columns.join('||')})"
|
13
|
+
end
|
14
|
+
|
15
|
+
# Executes an INSERT query and returns the new record's ID
|
16
|
+
def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
|
17
|
+
# Extract the table from the insert sql. Yuck.
|
18
|
+
table = sql.split(" ", 4)[2].gsub('"', '')
|
19
|
+
|
20
|
+
# Try an insert with 'returning id' if available (PG >= 8.2)
|
21
|
+
if supports_insert_with_returning?
|
22
|
+
pk, sequence_name = *pk_and_sequence_for(table) unless pk
|
23
|
+
if pk
|
24
|
+
quoted_pk = if pk.is_a?(Array)
|
25
|
+
pk.map { |col| quote_column_name(col) }.join(ID_SEP)
|
26
|
+
else
|
27
|
+
quote_column_name(pk)
|
28
|
+
end
|
29
|
+
id = select_value("#{sql} RETURNING #{quoted_pk}")
|
30
|
+
clear_query_cache
|
31
|
+
return id
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Otherwise, insert then grab last_insert_id.
|
36
|
+
if insert_id = super
|
37
|
+
insert_id
|
38
|
+
else
|
39
|
+
# If neither pk nor sequence name is given, look them up.
|
40
|
+
unless pk || sequence_name
|
41
|
+
pk, sequence_name = *pk_and_sequence_for(table)
|
42
|
+
end
|
43
|
+
|
44
|
+
# If a pk is given, fallback to default sequence name.
|
45
|
+
# Don't fetch last insert id for a table without a pk.
|
46
|
+
if pk && sequence_name ||= default_sequence_name(table, pk)
|
47
|
+
last_insert_id(table, sequence_name)
|
48
|
+
end
|
49
|
+
end
|
8
50
|
end
|
9
51
|
end
|
10
52
|
end
|
11
|
-
end
|
53
|
+
end
|
data/scripts/console.rb
CHANGED
@@ -1,18 +1,39 @@
|
|
1
|
-
|
2
|
-
# I haven't figured out how to setup an irb console yet.
|
3
|
-
# So, from the root, run:
|
4
|
-
# irb -f -r scripts/console
|
1
|
+
#!/usr/bin/env ruby
|
5
2
|
|
6
|
-
|
3
|
+
#
|
4
|
+
# if run as script, load the file as library while starting irb
|
5
|
+
#
|
6
|
+
if __FILE__ == $0
|
7
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
8
|
+
ENV['ADAPTER'] = ARGV[0]
|
9
|
+
exec "#{irb} -f -r #{$0} --simple-prompt"
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
# check if the given adapter is supported (default: mysql)
|
14
|
+
#
|
15
|
+
adapters = %w[mysql sqlite oracle oracle_enhanced postgresql ibm_db]
|
16
|
+
adapter = ENV['ADAPTER'] || 'mysql'
|
17
|
+
unless adapters.include? adapter
|
18
|
+
puts "Usage: #{__FILE__} <adapter>"
|
19
|
+
puts ''
|
20
|
+
puts 'Adapters: '
|
21
|
+
puts adapters.map{ |adapter| " #{adapter}" }.join("\n")
|
22
|
+
exit 1
|
23
|
+
end
|
7
24
|
|
8
|
-
|
9
|
-
|
25
|
+
#
|
26
|
+
# load all necessary libraries
|
27
|
+
#
|
28
|
+
require 'rubygems'
|
29
|
+
require 'local/database_connections'
|
30
|
+
|
31
|
+
$LOAD_PATH.unshift 'lib'
|
10
32
|
|
11
|
-
require "local/database_connections"
|
12
33
|
begin
|
13
|
-
require
|
14
|
-
|
15
|
-
|
34
|
+
require 'local/paths'
|
35
|
+
$LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/activerecord/lib" if ENV['EDGE_RAILS_DIR']
|
36
|
+
$LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/activesupport/lib" if ENV['EDGE_RAILS_DIR']
|
16
37
|
rescue
|
17
38
|
end
|
18
39
|
|
@@ -22,4 +43,6 @@ require 'active_record'
|
|
22
43
|
require "test/connections/native_#{adapter}/connection"
|
23
44
|
require 'composite_primary_keys'
|
24
45
|
|
46
|
+
PROJECT_ROOT = File.join(File.dirname(__FILE__), '..')
|
25
47
|
Dir[File.join(PROJECT_ROOT,'test/fixtures/*.rb')].each { |model| require model }
|
48
|
+
|
@@ -7,7 +7,7 @@ CREATE TABLE reference_types (
|
|
7
7
|
);
|
8
8
|
|
9
9
|
CREATE TABLE reference_codes (
|
10
|
-
reference_type_id integer
|
10
|
+
reference_type_id integer,
|
11
11
|
reference_code integer NOT NULL,
|
12
12
|
code_label varchar(50) default NULL,
|
13
13
|
abbreviation varchar(50) default NULL,
|
@@ -110,4 +110,4 @@ create table restaurants_suburbs (
|
|
110
110
|
store_id integer not null,
|
111
111
|
city_id integer not null,
|
112
112
|
suburb_id integer not null
|
113
|
-
);
|
113
|
+
);
|
@@ -1,173 +1,174 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
)
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
)
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
)
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
)
|
1
|
+
create table reference_types (
|
2
|
+
reference_type_id int(11) not null auto_increment,
|
3
|
+
type_label varchar(50) default null,
|
4
|
+
abbreviation varchar(50) default null,
|
5
|
+
description varchar(50) default null,
|
6
|
+
primary key (reference_type_id)
|
7
|
+
) type=InnoDB;
|
8
|
+
|
9
|
+
create table reference_codes (
|
10
|
+
reference_type_id int(11),
|
11
|
+
reference_code int(11) not null,
|
12
|
+
code_label varchar(50) default null,
|
13
|
+
abbreviation varchar(50) default null,
|
14
|
+
description varchar(50) default null,
|
15
|
+
primary key (reference_type_id, reference_code)
|
16
|
+
) type=InnoDB;
|
17
|
+
|
18
|
+
create table products (
|
19
|
+
id int(11) not null auto_increment,
|
20
|
+
name varchar(50) default null,
|
21
|
+
primary key (id)
|
22
|
+
) type=InnoDB;
|
23
|
+
|
24
|
+
create table tariffs (
|
25
|
+
tariff_id int(11) not null,
|
26
|
+
start_date date not null,
|
27
|
+
amount integer(11) default null,
|
28
|
+
primary key (tariff_id, start_date)
|
29
|
+
) type=InnoDB;
|
30
|
+
|
31
|
+
create table product_tariffs (
|
32
|
+
product_id int(11) not null,
|
33
|
+
tariff_id int(11) not null,
|
34
|
+
tariff_start_date date not null,
|
35
|
+
primary key (product_id, tariff_id, tariff_start_date)
|
36
|
+
) type=InnoDB;
|
37
|
+
|
38
|
+
create table suburbs (
|
39
|
+
city_id int(11) not null,
|
40
|
+
suburb_id int(11) not null,
|
41
|
+
name varchar(50) not null,
|
42
|
+
primary key (city_id, suburb_id)
|
43
|
+
) type=InnoDB;
|
44
|
+
|
45
|
+
create table streets (
|
46
|
+
id int(11) not null auto_increment,
|
47
|
+
city_id int(11) not null,
|
48
|
+
suburb_id int(11) not null,
|
49
|
+
name varchar(50) not null,
|
50
|
+
primary key (id)
|
51
|
+
) type=InnoDB;
|
52
|
+
|
53
|
+
create table users (
|
54
|
+
id int(11) not null auto_increment,
|
55
|
+
name varchar(50) not null,
|
56
|
+
primary key (id)
|
57
|
+
) type=InnoDB;
|
58
|
+
|
59
|
+
create table articles (
|
60
|
+
id int(11) not null auto_increment,
|
61
|
+
name varchar(50) not null,
|
62
|
+
primary key (id)
|
63
|
+
) type=InnoDB;
|
64
|
+
|
65
|
+
create table readings (
|
66
|
+
id int(11) not null auto_increment,
|
67
|
+
user_id int(11) not null,
|
68
|
+
article_id int(11) not null,
|
69
|
+
rating int(11) not null,
|
70
|
+
primary key (id)
|
71
|
+
) type=InnoDB;
|
72
|
+
|
73
|
+
create table groups (
|
74
|
+
id int(11) not null auto_increment,
|
75
|
+
name varchar(50) not null,
|
76
|
+
primary key (id)
|
77
|
+
) type=InnoDB;
|
78
|
+
|
79
|
+
create table memberships (
|
80
|
+
user_id int(11) not null,
|
81
|
+
group_id int(11) not null,
|
82
|
+
primary key (user_id,group_id)
|
83
|
+
) type=InnoDB;
|
84
|
+
|
85
|
+
create table membership_statuses (
|
86
|
+
id int(11) not null auto_increment,
|
87
|
+
user_id int(11) not null,
|
88
|
+
group_id int(11) not null,
|
89
|
+
status varchar(50) not null,
|
90
|
+
primary key (id)
|
91
|
+
) type=InnoDB;
|
92
|
+
|
93
|
+
create table departments (
|
94
|
+
department_id int(11) not null,
|
95
|
+
location_id int(11) not null,
|
96
|
+
primary key (department_id, location_id)
|
97
|
+
) type=InnoDB;
|
98
|
+
|
99
|
+
create table employees (
|
100
|
+
id int(11) not null auto_increment,
|
101
|
+
department_id int(11) default null,
|
102
|
+
location_id int(11) default null,
|
103
|
+
primary key (id)
|
104
|
+
) type=InnoDB;
|
105
|
+
|
106
|
+
create table comments (
|
107
|
+
id int(11) not null auto_increment,
|
108
|
+
person_id varchar(100) default null,
|
109
|
+
person_type varchar(100) default null,
|
110
|
+
hack_id varchar(100) default null,
|
111
|
+
primary key (id)
|
112
|
+
) type=InnoDB;
|
113
|
+
|
114
|
+
create table hacks (
|
115
|
+
name varchar(50) not null,
|
116
|
+
primary key (name)
|
117
|
+
) type=InnoDB;
|
118
118
|
|
119
119
|
create table kitchen_sinks (
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
)
|
120
|
+
id_1 int(11) not null,
|
121
|
+
id_2 int(11) not null,
|
122
|
+
a_date date,
|
123
|
+
a_string varchar(100),
|
124
|
+
primary key (id_1, id_2)
|
125
|
+
) type=InnoDB;
|
126
126
|
|
127
127
|
create table restaurants (
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
)
|
128
|
+
franchise_id int(11) not null,
|
129
|
+
store_id int(11) not null,
|
130
|
+
name varchar(100),
|
131
|
+
primary key (franchise_id, store_id)
|
132
|
+
) type=InnoDB;
|
133
133
|
|
134
134
|
create table restaurants_suburbs (
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
)
|
135
|
+
franchise_id int(11) not null,
|
136
|
+
store_id int(11) not null,
|
137
|
+
city_id int(11) not null,
|
138
|
+
suburb_id int(11) not null
|
139
|
+
) type=InnoDB;
|
140
140
|
|
141
141
|
create table dorms (
|
142
|
-
|
143
|
-
|
144
|
-
)
|
142
|
+
id int(11) not null auto_increment,
|
143
|
+
primary key(id)
|
144
|
+
) type=InnoDB;
|
145
145
|
|
146
146
|
create table rooms (
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
)
|
147
|
+
dorm_id int(11) not null,
|
148
|
+
room_id int(11) not null,
|
149
|
+
primary key (dorm_id, room_id)
|
150
|
+
) type=InnoDB;
|
151
151
|
|
152
152
|
create table room_attributes (
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
)
|
153
|
+
id int(11) not null auto_increment,
|
154
|
+
name varchar(50),
|
155
|
+
primary key(id)
|
156
|
+
) type=InnoDB;
|
157
157
|
|
158
158
|
create table room_attribute_assignments (
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
)
|
159
|
+
dorm_id int(11) not null,
|
160
|
+
room_id int(11) not null,
|
161
|
+
room_attribute_id int(11) not null
|
162
|
+
) type=InnoDB;
|
163
163
|
|
164
164
|
create table students (
|
165
|
-
|
166
|
-
|
167
|
-
)
|
165
|
+
id int(11) not null auto_increment,
|
166
|
+
primary key(id)
|
167
|
+
) type=InnoDB;
|
168
168
|
|
169
169
|
create table room_assignments (
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
)
|
170
|
+
student_id int(11) not null,
|
171
|
+
dorm_id int(11) not null,
|
172
|
+
room_id int(11) not null
|
173
|
+
) type=InnoDB;
|
174
|
+
|