jwulff-composite_primary_keys 1.0.9
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/VERSION.yml +4 -0
- data/lib/adapter_helper/base.rb +63 -0
- data/lib/adapter_helper/mysql.rb +13 -0
- data/lib/adapter_helper/oracle.rb +12 -0
- data/lib/adapter_helper/postgresql.rb +13 -0
- data/lib/adapter_helper/sqlite3.rb +13 -0
- data/lib/composite_primary_keys.rb +55 -0
- data/lib/composite_primary_keys/association_preload.rb +236 -0
- data/lib/composite_primary_keys/associations.rb +428 -0
- data/lib/composite_primary_keys/attribute_methods.rb +84 -0
- data/lib/composite_primary_keys/base.rb +320 -0
- data/lib/composite_primary_keys/calculations.rb +68 -0
- data/lib/composite_primary_keys/composite_arrays.rb +30 -0
- data/lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb +21 -0
- data/lib/composite_primary_keys/connection_adapters/oracle_adapter.rb +15 -0
- data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +53 -0
- data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +15 -0
- data/lib/composite_primary_keys/fixtures.rb +8 -0
- data/lib/composite_primary_keys/migration.rb +20 -0
- data/lib/composite_primary_keys/reflection.rb +19 -0
- data/lib/composite_primary_keys/version.rb +8 -0
- metadata +77 -0
@@ -0,0 +1,30 @@
|
|
1
|
+
module CompositePrimaryKeys
|
2
|
+
ID_SEP = ','
|
3
|
+
ID_SET_SEP = ';'
|
4
|
+
|
5
|
+
module ArrayExtension
|
6
|
+
def to_composite_keys
|
7
|
+
CompositeKeys.new(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_composite_ids
|
11
|
+
CompositeIds.new(self)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class CompositeArray < Array
|
16
|
+
def to_s
|
17
|
+
join(ID_SEP)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class CompositeKeys < CompositeArray
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class CompositeIds < CompositeArray
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Array.send(:include, CompositePrimaryKeys::ArrayExtension)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
class IBM_DBAdapter < AbstractAdapter
|
4
|
+
|
5
|
+
# This mightn't be in Core, but count(distinct x,y) doesn't work for me
|
6
|
+
def supports_count_distinct? #:nodoc:
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
alias_method :quote_original, :quote
|
11
|
+
def quote(value, column = nil)
|
12
|
+
if value.kind_of?(String) && column && [:integer, :float].include?(column.type)
|
13
|
+
value = column.type == :integer ? value.to_i : value.to_f
|
14
|
+
value.to_s
|
15
|
+
else
|
16
|
+
quote_original(value, column)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
class OracleAdapter < AbstractAdapter
|
4
|
+
|
5
|
+
# This mightn't be in Core, but count(distinct x,y) doesn't work for me
|
6
|
+
def supports_count_distinct? #:nodoc:
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def concat(*columns)
|
11
|
+
"(#{columns.join('||')})"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
class PostgreSQLAdapter < AbstractAdapter
|
4
|
+
|
5
|
+
# This mightn't be in Core, but count(distinct x,y) doesn't work for me
|
6
|
+
def supports_count_distinct? #:nodoc:
|
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
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_record/connection_adapters/sqlite_adapter'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters #:nodoc:
|
5
|
+
class SQLite3Adapter < SQLiteAdapter # :nodoc:
|
6
|
+
def supports_count_distinct? #:nodoc:
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def concat(*columns)
|
11
|
+
"(#{columns.join('||')})"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
ActiveRecord::ConnectionAdapters::ColumnDefinition.send(:alias_method, :to_s_without_composite_keys, :to_s)
|
2
|
+
|
3
|
+
ActiveRecord::ConnectionAdapters::ColumnDefinition.class_eval <<-'EOF'
|
4
|
+
def to_s
|
5
|
+
if name.is_a? Array
|
6
|
+
"PRIMARY KEY (#{name.join(',')})"
|
7
|
+
else
|
8
|
+
to_s_without_composite_keys
|
9
|
+
end
|
10
|
+
end
|
11
|
+
EOF
|
12
|
+
|
13
|
+
ActiveRecord::ConnectionAdapters::TableDefinition.class_eval <<-'EOF'
|
14
|
+
def [](name)
|
15
|
+
@columns.find { |column|
|
16
|
+
!column.name.is_a?(Array) && column.name.to_s == name.to_s
|
17
|
+
}
|
18
|
+
end
|
19
|
+
EOF
|
20
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Reflection
|
3
|
+
class AssociationReflection
|
4
|
+
def primary_key_name
|
5
|
+
return @primary_key_name if @primary_key_name
|
6
|
+
case
|
7
|
+
when macro == :belongs_to
|
8
|
+
@primary_key_name = options[:foreign_key] || class_name.foreign_key
|
9
|
+
when options[:as]
|
10
|
+
@primary_key_name = options[:foreign_key] || "#{options[:as]}_id"
|
11
|
+
else
|
12
|
+
@primary_key_name = options[:foreign_key] || active_record.name.foreign_key
|
13
|
+
end
|
14
|
+
@primary_key_name = @primary_key_name.to_composite_keys.to_s if @primary_key_name.is_a? Array
|
15
|
+
@primary_key_name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jwulff-composite_primary_keys
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Wulff
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-21 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: TODO
|
17
|
+
email: john@johnwulff.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- lib/adapter_helper
|
27
|
+
- lib/adapter_helper/base.rb
|
28
|
+
- lib/adapter_helper/mysql.rb
|
29
|
+
- lib/adapter_helper/oracle.rb
|
30
|
+
- lib/adapter_helper/postgresql.rb
|
31
|
+
- lib/adapter_helper/sqlite3.rb
|
32
|
+
- lib/composite_primary_keys
|
33
|
+
- lib/composite_primary_keys/association_preload.rb
|
34
|
+
- lib/composite_primary_keys/associations.rb
|
35
|
+
- lib/composite_primary_keys/attribute_methods.rb
|
36
|
+
- lib/composite_primary_keys/base.rb
|
37
|
+
- lib/composite_primary_keys/calculations.rb
|
38
|
+
- lib/composite_primary_keys/composite_arrays.rb
|
39
|
+
- lib/composite_primary_keys/connection_adapters
|
40
|
+
- lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb
|
41
|
+
- lib/composite_primary_keys/connection_adapters/oracle_adapter.rb
|
42
|
+
- lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb
|
43
|
+
- lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb
|
44
|
+
- lib/composite_primary_keys/fixtures.rb
|
45
|
+
- lib/composite_primary_keys/migration.rb
|
46
|
+
- lib/composite_primary_keys/reflection.rb
|
47
|
+
- lib/composite_primary_keys/version.rb
|
48
|
+
- lib/composite_primary_keys.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/jwulff/composite_primary_keys
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --inline-source
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.2.0
|
73
|
+
signing_key:
|
74
|
+
specification_version: 2
|
75
|
+
summary: TODO
|
76
|
+
test_files: []
|
77
|
+
|