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.
@@ -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
- true
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
@@ -1,9 +1,8 @@
1
1
  class Fixture #:nodoc:
2
2
  def [](key)
3
3
  if key.is_a? Array
4
- return key.map {|a_key| self[a_key.to_s]}.
5
- to_composite_ids.to_s
4
+ return key.map { |a_key| self[a_key.to_s] }.to_composite_ids.to_s
6
5
  end
7
6
  @fixture[key]
8
7
  end
9
- end
8
+ end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- TINY = 8
5
+ TINY = 10
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -1,18 +1,39 @@
1
- # FIXME:
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
- PROJECT_ROOT = '.' #File.join(File.dirname(__FILE__), '..')
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
- adapter = 'mysql'
9
- $:.unshift 'lib'
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 "local/paths"
14
- $:.unshift "#{ENV['EDGE_RAILS_DIR']}/activerecord/lib" if ENV['EDGE_RAILS_DIR']
15
- $:.unshift "#{ENV['EDGE_RAILS_DIR']}/activesupport/lib" if ENV['EDGE_RAILS_DIR']
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 NOT NULL,
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
- 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) NOT NULL,
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;
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
- 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;
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
- 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;
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
- 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;
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
- id int(11) not null auto_increment,
143
- primary key(id)
144
- ) TYPE=InnoDB;
142
+ id int(11) not null auto_increment,
143
+ primary key(id)
144
+ ) type=InnoDB;
145
145
 
146
146
  create table rooms (
147
- dorm_id int(11) not null,
148
- room_id int(11) not null,
149
- primary key (dorm_id, room_id)
150
- ) TYPE=InnoDB;
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
- id int(11) not null auto_increment,
154
- name varchar(50),
155
- primary key(id)
156
- ) TYPE=InnoDB;
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
- dorm_id int(11) not null,
160
- room_id int(11) not null,
161
- room_attribute_id int(11) not null
162
- ) TYPE=InnoDB;
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
- id int(11) not null auto_increment,
166
- primary key(id)
167
- ) TYPE=InnoDB;
165
+ id int(11) not null auto_increment,
166
+ primary key(id)
167
+ ) type=InnoDB;
168
168
 
169
169
  create table room_assignments (
170
- student_id int(11) not null,
171
- dorm_id int(11) not null,
172
- room_id int(11) not null
173
- ) TYPE=InnoDB;
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
+