colincasey-sequel 2.10.0 → 2.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -1
- data/doc/advanced_associations.rdoc +614 -0
- data/doc/cheat_sheet.rdoc +223 -0
- data/doc/dataset_filtering.rdoc +158 -0
- data/doc/prepared_statements.rdoc +104 -0
- data/doc/release_notes/1.0.txt +38 -0
- data/doc/release_notes/1.1.txt +143 -0
- data/doc/release_notes/1.3.txt +101 -0
- data/doc/release_notes/1.4.0.txt +53 -0
- data/doc/release_notes/1.5.0.txt +155 -0
- data/doc/release_notes/2.0.0.txt +298 -0
- data/doc/release_notes/2.1.0.txt +271 -0
- data/doc/release_notes/2.10.0.txt +328 -0
- data/doc/release_notes/2.2.0.txt +253 -0
- data/doc/release_notes/2.3.0.txt +88 -0
- data/doc/release_notes/2.4.0.txt +106 -0
- data/doc/release_notes/2.5.0.txt +137 -0
- data/doc/release_notes/2.6.0.txt +157 -0
- data/doc/release_notes/2.7.0.txt +166 -0
- data/doc/release_notes/2.8.0.txt +171 -0
- data/doc/release_notes/2.9.0.txt +97 -0
- data/doc/schema.rdoc +29 -0
- data/doc/sharding.rdoc +113 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel_core/adapters/ado.rb +89 -0
- data/lib/sequel_core/adapters/db2.rb +143 -0
- data/lib/sequel_core/adapters/dbi.rb +112 -0
- data/lib/sequel_core/adapters/do/mysql.rb +38 -0
- data/lib/sequel_core/adapters/do/postgres.rb +92 -0
- data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
- data/lib/sequel_core/adapters/do.rb +205 -0
- data/lib/sequel_core/adapters/firebird.rb +298 -0
- data/lib/sequel_core/adapters/informix.rb +85 -0
- data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
- data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
- data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
- data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
- data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
- data/lib/sequel_core/adapters/jdbc.rb +491 -0
- data/lib/sequel_core/adapters/mysql.rb +369 -0
- data/lib/sequel_core/adapters/odbc.rb +174 -0
- data/lib/sequel_core/adapters/openbase.rb +68 -0
- data/lib/sequel_core/adapters/oracle.rb +107 -0
- data/lib/sequel_core/adapters/postgres.rb +456 -0
- data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
- data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
- data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
- data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
- data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
- data/lib/sequel_core/adapters/shared/progress.rb +31 -0
- data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
- data/lib/sequel_core/adapters/sqlite.rb +248 -0
- data/lib/sequel_core/connection_pool.rb +258 -0
- data/lib/sequel_core/core_ext.rb +217 -0
- data/lib/sequel_core/core_sql.rb +202 -0
- data/lib/sequel_core/database/schema.rb +164 -0
- data/lib/sequel_core/database.rb +691 -0
- data/lib/sequel_core/dataset/callback.rb +13 -0
- data/lib/sequel_core/dataset/convenience.rb +237 -0
- data/lib/sequel_core/dataset/pagination.rb +96 -0
- data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
- data/lib/sequel_core/dataset/query.rb +41 -0
- data/lib/sequel_core/dataset/schema.rb +15 -0
- data/lib/sequel_core/dataset/sql.rb +1010 -0
- data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
- data/lib/sequel_core/dataset/unsupported.rb +43 -0
- data/lib/sequel_core/dataset.rb +511 -0
- data/lib/sequel_core/deprecated.rb +26 -0
- data/lib/sequel_core/exceptions.rb +44 -0
- data/lib/sequel_core/migration.rb +212 -0
- data/lib/sequel_core/object_graph.rb +230 -0
- data/lib/sequel_core/pretty_table.rb +71 -0
- data/lib/sequel_core/schema/generator.rb +320 -0
- data/lib/sequel_core/schema/sql.rb +325 -0
- data/lib/sequel_core/schema.rb +2 -0
- data/lib/sequel_core/sql.rb +887 -0
- data/lib/sequel_core/version.rb +11 -0
- data/lib/sequel_core.rb +172 -0
- data/lib/sequel_model/association_reflection.rb +267 -0
- data/lib/sequel_model/associations.rb +499 -0
- data/lib/sequel_model/base.rb +523 -0
- data/lib/sequel_model/caching.rb +82 -0
- data/lib/sequel_model/dataset_methods.rb +26 -0
- data/lib/sequel_model/eager_loading.rb +370 -0
- data/lib/sequel_model/exceptions.rb +7 -0
- data/lib/sequel_model/hooks.rb +101 -0
- data/lib/sequel_model/inflector.rb +281 -0
- data/lib/sequel_model/plugins.rb +62 -0
- data/lib/sequel_model/record.rb +568 -0
- data/lib/sequel_model/schema.rb +49 -0
- data/lib/sequel_model/validations.rb +429 -0
- data/lib/sequel_model.rb +91 -0
- data/spec/adapters/ado_spec.rb +46 -0
- data/spec/adapters/firebird_spec.rb +376 -0
- data/spec/adapters/informix_spec.rb +96 -0
- data/spec/adapters/mysql_spec.rb +881 -0
- data/spec/adapters/oracle_spec.rb +244 -0
- data/spec/adapters/postgres_spec.rb +687 -0
- data/spec/adapters/spec_helper.rb +10 -0
- data/spec/adapters/sqlite_spec.rb +555 -0
- data/spec/integration/dataset_test.rb +134 -0
- data/spec/integration/eager_loader_test.rb +696 -0
- data/spec/integration/prepared_statement_test.rb +130 -0
- data/spec/integration/schema_test.rb +180 -0
- data/spec/integration/spec_helper.rb +58 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/rcov.opts +6 -0
- data/spec/sequel_core/connection_pool_spec.rb +526 -0
- data/spec/sequel_core/core_ext_spec.rb +156 -0
- data/spec/sequel_core/core_sql_spec.rb +522 -0
- data/spec/sequel_core/database_spec.rb +1188 -0
- data/spec/sequel_core/dataset_spec.rb +3481 -0
- data/spec/sequel_core/expression_filters_spec.rb +363 -0
- data/spec/sequel_core/migration_spec.rb +261 -0
- data/spec/sequel_core/object_graph_spec.rb +272 -0
- data/spec/sequel_core/pretty_table_spec.rb +58 -0
- data/spec/sequel_core/schema_generator_spec.rb +167 -0
- data/spec/sequel_core/schema_spec.rb +780 -0
- data/spec/sequel_core/spec_helper.rb +55 -0
- data/spec/sequel_core/version_spec.rb +7 -0
- data/spec/sequel_model/association_reflection_spec.rb +93 -0
- data/spec/sequel_model/associations_spec.rb +1767 -0
- data/spec/sequel_model/base_spec.rb +419 -0
- data/spec/sequel_model/caching_spec.rb +215 -0
- data/spec/sequel_model/dataset_methods_spec.rb +78 -0
- data/spec/sequel_model/eager_loading_spec.rb +1165 -0
- data/spec/sequel_model/hooks_spec.rb +485 -0
- data/spec/sequel_model/inflector_spec.rb +119 -0
- data/spec/sequel_model/model_spec.rb +588 -0
- data/spec/sequel_model/plugins_spec.rb +80 -0
- data/spec/sequel_model/record_spec.rb +1184 -0
- data/spec/sequel_model/schema_spec.rb +90 -0
- data/spec/sequel_model/spec_helper.rb +78 -0
- data/spec/sequel_model/validations_spec.rb +1067 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +177 -3
data/lib/sequel_core.rb
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
%w'bigdecimal bigdecimal/util date enumerator thread time uri yaml'.each do |f|
|
2
|
+
require f
|
3
|
+
end
|
4
|
+
%w"core_ext sql core_sql connection_pool exceptions pretty_table
|
5
|
+
dataset migration schema database object_graph version".each do |f|
|
6
|
+
require "sequel_core/#{f}"
|
7
|
+
end
|
8
|
+
|
9
|
+
# Top level module for Sequel
|
10
|
+
#
|
11
|
+
# There are some class methods that are added via metaprogramming, one for
|
12
|
+
# each supported adapter. For example:
|
13
|
+
#
|
14
|
+
# DB = Sequel.sqlite # Memory database
|
15
|
+
# DB = Sequel.sqlite('blog.db')
|
16
|
+
# DB = Sequel.postgres('database_name', :user=>'user',
|
17
|
+
# :password=>'password', :host=>'host', :port=>5432,
|
18
|
+
# :max_connections=>10)
|
19
|
+
#
|
20
|
+
# If a block is given to these methods, it is passed the opened Database
|
21
|
+
# object, which is closed (disconnected) when the block exits. For example:
|
22
|
+
#
|
23
|
+
# Sequel.sqlite('blog.db'){|db| puts db.users.count}
|
24
|
+
#
|
25
|
+
# Sequel converts the column type tinyint to a boolean by default,
|
26
|
+
# you can override the conversion to use tinyint as an integer:
|
27
|
+
#
|
28
|
+
# Sequel.convert_tinyint_to_bool = false
|
29
|
+
#
|
30
|
+
# Sequel converts two digit years in Dates and DateTimes by default,
|
31
|
+
# so 01/02/03 is interpreted at January 2nd, 2003, and 12/13/99 is interpreted
|
32
|
+
# as December 13, 1999.. You can override this # to treat those dates as
|
33
|
+
# January 2nd, 0003 and December 13, 0099, respectively, by setting:
|
34
|
+
#
|
35
|
+
# Sequel.convert_two_digit_years = false
|
36
|
+
#
|
37
|
+
# Sequel can use either Time or DateTime for times returned from the
|
38
|
+
# database. It defaults to Time. To change it to DateTime, use:
|
39
|
+
#
|
40
|
+
# Sequel.datetime_class = DateTime
|
41
|
+
module Sequel
|
42
|
+
@convert_tinyint_to_bool = true
|
43
|
+
@convert_two_digit_years = true
|
44
|
+
@datetime_class = Time
|
45
|
+
|
46
|
+
metaattr_accessor :convert_tinyint_to_bool
|
47
|
+
metaattr_accessor :convert_two_digit_years
|
48
|
+
metaattr_accessor :datetime_class
|
49
|
+
|
50
|
+
# Creates a new database object based on the supplied connection string
|
51
|
+
# and optional arguments. The specified scheme determines the database
|
52
|
+
# class used, and the rest of the string specifies the connection options.
|
53
|
+
# For example:
|
54
|
+
#
|
55
|
+
# DB = Sequel.connect('sqlite:/') # Memory database
|
56
|
+
# DB = Sequel.connect('sqlite://blog.db') # ./blog.db
|
57
|
+
# DB = Sequel.connect('sqlite:///blog.db') # /blog.db
|
58
|
+
# DB = Sequel.connect('postgres://user:password@host:port/database_name')
|
59
|
+
# DB = Sequel.connect('sqlite:///blog.db', :max_connections=>10)
|
60
|
+
#
|
61
|
+
# If a block is given, it is passed the opened Database object, which is
|
62
|
+
# closed when the block exits. For example:
|
63
|
+
#
|
64
|
+
# Sequel.connect('sqlite://blog.db'){|db| puts db.users.count}
|
65
|
+
#
|
66
|
+
# This is also aliased as Sequel.open.
|
67
|
+
def self.connect(*args, &block)
|
68
|
+
Database.connect(*args, &block)
|
69
|
+
end
|
70
|
+
metaalias :open, :connect
|
71
|
+
|
72
|
+
# Set the method to call on identifiers going into the database. This affects
|
73
|
+
# the literalization of identifiers by calling this method on them before they are input.
|
74
|
+
# Sequel upcases identifiers in all SQL strings for most databases, so to turn that off:
|
75
|
+
#
|
76
|
+
# Sequel.identifier_input_method = nil
|
77
|
+
#
|
78
|
+
# to downcase instead:
|
79
|
+
#
|
80
|
+
# Sequel.identifier_input_method = :downcase
|
81
|
+
#
|
82
|
+
# Other string methods work as well.
|
83
|
+
def self.identifier_input_method=(value)
|
84
|
+
Database.identifier_input_method = value
|
85
|
+
end
|
86
|
+
|
87
|
+
# Set the method to call on identifiers coming out of the database. This affects
|
88
|
+
# the literalization of identifiers by calling this method on them when they are
|
89
|
+
# retrieved from the database. Sequel downcases identifiers retrieved for most
|
90
|
+
# databases, so to turn that off:
|
91
|
+
#
|
92
|
+
# Sequel.identifier_output_method = nil
|
93
|
+
#
|
94
|
+
# to upcase instead:
|
95
|
+
#
|
96
|
+
# Sequel.identifier_output_method = :upcase
|
97
|
+
#
|
98
|
+
# Other string methods work as well.
|
99
|
+
def self.identifier_output_method=(value)
|
100
|
+
Database.identifier_output_method = value
|
101
|
+
end
|
102
|
+
|
103
|
+
# Set whether to quote identifiers for all databases by default. By default,
|
104
|
+
# Sequel quotes identifiers in all SQL strings, so to turn that off:
|
105
|
+
#
|
106
|
+
# Sequel.quote_identifiers = false
|
107
|
+
def self.quote_identifiers=(value)
|
108
|
+
Database.quote_identifiers = value
|
109
|
+
end
|
110
|
+
|
111
|
+
# Set whether to set the single threaded mode for all databases by default. By default,
|
112
|
+
# Sequel uses a threadsafe connection pool, which isn't as fast as the
|
113
|
+
# single threaded connection pool. If your program will only have one thread,
|
114
|
+
# and speed is a priority, you may want to set this to true:
|
115
|
+
#
|
116
|
+
# Sequel.single_threaded = true
|
117
|
+
def self.single_threaded=(value)
|
118
|
+
Database.single_threaded = value
|
119
|
+
end
|
120
|
+
|
121
|
+
# Set whether to upcase identifiers for all databases by default. By default,
|
122
|
+
# Sequel upcases identifiers unless the database folds unquoted identifiers to
|
123
|
+
# lower case (MySQL, PostgreSQL, and SQLite).
|
124
|
+
#
|
125
|
+
# Sequel.upcase_identifiers = false
|
126
|
+
#
|
127
|
+
# This will set the indentifier_input_method to :upcase if value is true
|
128
|
+
# or nil if value is false.
|
129
|
+
def self.upcase_identifiers=(value)
|
130
|
+
Database.upcase_identifiers = value
|
131
|
+
end
|
132
|
+
|
133
|
+
# Always returns false, since ParseTree support has been removed.
|
134
|
+
def self.use_parse_tree
|
135
|
+
false
|
136
|
+
end
|
137
|
+
|
138
|
+
# Raises an error if attempting to turn ParseTree support on (since it no longer exists).
|
139
|
+
# Otherwise, is a no-op.
|
140
|
+
def self.use_parse_tree=(val)
|
141
|
+
raise(Error, 'ParseTree support has been removed from Sequel') if val
|
142
|
+
end
|
143
|
+
|
144
|
+
### Private Class Methods ###
|
145
|
+
|
146
|
+
# Helper method that the database adapter class methods that are added to Sequel via
|
147
|
+
# metaprogramming use to parse arguments.
|
148
|
+
def self.adapter_method(adapter, *args, &block) # :nodoc:
|
149
|
+
raise(::Sequel::Error, "Wrong number of arguments, 0-2 arguments valid") if args.length > 2
|
150
|
+
opts = {:adapter=>adapter.to_sym}
|
151
|
+
opts[:database] = args.shift if args.length >= 1 && !(args[0].is_a?(Hash))
|
152
|
+
if Hash === (arg = args[0])
|
153
|
+
opts.merge!(arg)
|
154
|
+
elsif !arg.nil?
|
155
|
+
raise ::Sequel::Error, "Wrong format of arguments, either use (), (String), (Hash), or (String, Hash)"
|
156
|
+
end
|
157
|
+
connect(opts, &block)
|
158
|
+
end
|
159
|
+
|
160
|
+
# Method that adds a database adapter class method to Sequel that calls
|
161
|
+
# Sequel.adapter_method.
|
162
|
+
def self.def_adapter_method(*adapters) # :nodoc:
|
163
|
+
adapters.each do |adapter|
|
164
|
+
instance_eval("def #{adapter}(*args, &block); adapter_method('#{adapter}', *args, &block) end")
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
private_class_method :adapter_method, :def_adapter_method
|
169
|
+
|
170
|
+
# Add the database adapter class methods to Sequel via metaprogramming
|
171
|
+
def_adapter_method(*Database::ADAPTERS)
|
172
|
+
end
|
@@ -0,0 +1,267 @@
|
|
1
|
+
module Sequel::Model::Associations
|
2
|
+
# Map of association type symbols to association reflection classes.
|
3
|
+
ASSOCIATION_TYPES = {}
|
4
|
+
|
5
|
+
# AssociationReflection is a Hash subclass that keeps information on Sequel::Model associations. It
|
6
|
+
# provides methods to reduce internal code duplication. It should not
|
7
|
+
# be instantiated by the user.
|
8
|
+
class AssociationReflection < Hash
|
9
|
+
# Name symbol for _add_ internal association method
|
10
|
+
def _add_method
|
11
|
+
:"_add_#{self[:name].to_s.singularize}"
|
12
|
+
end
|
13
|
+
|
14
|
+
# Name symbol for _dataset association method
|
15
|
+
def _dataset_method
|
16
|
+
:"_#{self[:name]}_dataset"
|
17
|
+
end
|
18
|
+
|
19
|
+
# Name symbol for _remove_all internal association method
|
20
|
+
def _remove_all_method
|
21
|
+
:"_remove_all_#{self[:name]}"
|
22
|
+
end
|
23
|
+
|
24
|
+
# Name symbol for _remove_ internal association method
|
25
|
+
def _remove_method
|
26
|
+
:"_remove_#{self[:name].to_s.singularize}"
|
27
|
+
end
|
28
|
+
|
29
|
+
# Name symbol for setter association method
|
30
|
+
def _setter_method
|
31
|
+
:"_#{self[:name]}="
|
32
|
+
end
|
33
|
+
|
34
|
+
# Name symbol for add_ association method
|
35
|
+
def add_method
|
36
|
+
:"add_#{self[:name].to_s.singularize}"
|
37
|
+
end
|
38
|
+
|
39
|
+
# Name symbol for association method, the same as the name of the association.
|
40
|
+
def association_method
|
41
|
+
self[:name]
|
42
|
+
end
|
43
|
+
|
44
|
+
# The class associated to the current model class via this association
|
45
|
+
def associated_class
|
46
|
+
self[:class] ||= self[:class_name].constantize
|
47
|
+
end
|
48
|
+
|
49
|
+
# Name symbol for dataset association method
|
50
|
+
def dataset_method
|
51
|
+
:"#{self[:name]}_dataset"
|
52
|
+
end
|
53
|
+
|
54
|
+
# Name symbol for _helper internal association method
|
55
|
+
def dataset_helper_method
|
56
|
+
:"_#{self[:name]}_dataset_helper"
|
57
|
+
end
|
58
|
+
|
59
|
+
# Whether the dataset needs a primary key to function, true by default.
|
60
|
+
def dataset_need_primary_key?
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
# Whether to eagerly graph a lazy dataset, true by default.
|
65
|
+
def eager_graph_lazy_dataset?
|
66
|
+
true
|
67
|
+
end
|
68
|
+
|
69
|
+
# Whether the associated object needs a primary key to be added/removed,
|
70
|
+
# false by default.
|
71
|
+
def need_associated_primary_key?
|
72
|
+
false
|
73
|
+
end
|
74
|
+
|
75
|
+
# Returns/sets the reciprocal association variable, if one exists
|
76
|
+
def reciprocal
|
77
|
+
return self[:reciprocal] if include?(:reciprocal)
|
78
|
+
r_type = reciprocal_type
|
79
|
+
key = self[:key]
|
80
|
+
associated_class.all_association_reflections.each do |assoc_reflect|
|
81
|
+
if assoc_reflect[:type] == r_type && assoc_reflect[:key] == key
|
82
|
+
return self[:reciprocal] = assoc_reflect[:name]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
self[:reciprocal] = nil
|
86
|
+
end
|
87
|
+
|
88
|
+
# Whether the reciprocal of this association returns an array of objects instead of a single object,
|
89
|
+
# true by default.
|
90
|
+
def reciprocal_array?
|
91
|
+
true
|
92
|
+
end
|
93
|
+
|
94
|
+
# Name symbol for remove_all_ association method
|
95
|
+
def remove_all_method
|
96
|
+
:"remove_all_#{self[:name]}"
|
97
|
+
end
|
98
|
+
|
99
|
+
# Name symbol for remove_ association method
|
100
|
+
def remove_method
|
101
|
+
:"remove_#{self[:name].to_s.singularize}"
|
102
|
+
end
|
103
|
+
|
104
|
+
# Whether this association returns an array of objects instead of a single object,
|
105
|
+
# true by default.
|
106
|
+
def returns_array?
|
107
|
+
true
|
108
|
+
end
|
109
|
+
|
110
|
+
# The columns to select when loading the association, nil by default.
|
111
|
+
def select
|
112
|
+
self[:select]
|
113
|
+
end
|
114
|
+
|
115
|
+
# By default, associations shouldn't set the reciprocal association to self.
|
116
|
+
def set_reciprocal_to_self?
|
117
|
+
false
|
118
|
+
end
|
119
|
+
|
120
|
+
# Name symbol for setter association method
|
121
|
+
def setter_method
|
122
|
+
:"#{self[:name]}="
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
class ManyToOneAssociationReflection < AssociationReflection
|
128
|
+
ASSOCIATION_TYPES[:many_to_one] = self
|
129
|
+
|
130
|
+
# Whether the dataset needs a primary key to function, false for many_to_one associations.
|
131
|
+
def dataset_need_primary_key?
|
132
|
+
false
|
133
|
+
end
|
134
|
+
|
135
|
+
# Default foreign key name symbol for foreign key in current model's table that points to
|
136
|
+
# the given association's table's primary key.
|
137
|
+
def default_key
|
138
|
+
:"#{self[:name]}_id"
|
139
|
+
end
|
140
|
+
|
141
|
+
# Whether to eagerly graph a lazy dataset, true for many_to_one associations
|
142
|
+
# only if the key is nil.
|
143
|
+
def eager_graph_lazy_dataset?
|
144
|
+
self[:key].nil?
|
145
|
+
end
|
146
|
+
|
147
|
+
# The key to use for the key hash when eager loading
|
148
|
+
def eager_loader_key
|
149
|
+
self[:key]
|
150
|
+
end
|
151
|
+
|
152
|
+
# The column in the associated table that the key in the current table references.
|
153
|
+
def primary_key
|
154
|
+
self[:primary_key] ||= associated_class.primary_key
|
155
|
+
end
|
156
|
+
|
157
|
+
# Whether this association returns an array of objects instead of a single object,
|
158
|
+
# false for a many_to_one association.
|
159
|
+
def returns_array?
|
160
|
+
false
|
161
|
+
end
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
# The reciprocal type of a many_to_one association is a one_to_many association.
|
166
|
+
def reciprocal_type
|
167
|
+
:one_to_many
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
class OneToManyAssociationReflection < AssociationReflection
|
172
|
+
ASSOCIATION_TYPES[:one_to_many] = self
|
173
|
+
|
174
|
+
# Default foreign key name symbol for key in associated table that points to
|
175
|
+
# current table's primary key.
|
176
|
+
def default_key
|
177
|
+
:"#{self[:model].name.to_s.demodulize.underscore}_id"
|
178
|
+
end
|
179
|
+
|
180
|
+
# The key to use for the key hash when eager loading
|
181
|
+
def eager_loader_key
|
182
|
+
primary_key
|
183
|
+
end
|
184
|
+
|
185
|
+
# The column in the current table that the key in the associated table references.
|
186
|
+
def primary_key
|
187
|
+
self[:primary_key] ||= self[:model].primary_key
|
188
|
+
end
|
189
|
+
|
190
|
+
# One to many associations set the reciprocal to self.
|
191
|
+
def set_reciprocal_to_self?
|
192
|
+
true
|
193
|
+
end
|
194
|
+
|
195
|
+
# Whether the reciprocal of this association returns an array of objects instead of a single object,
|
196
|
+
# false for a one_to_many association.
|
197
|
+
def reciprocal_array?
|
198
|
+
false
|
199
|
+
end
|
200
|
+
|
201
|
+
private
|
202
|
+
|
203
|
+
# The reciprocal type of a one_to_many association is a many_to_one association.
|
204
|
+
def reciprocal_type
|
205
|
+
:many_to_one
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
class ManyToManyAssociationReflection < AssociationReflection
|
210
|
+
ASSOCIATION_TYPES[:many_to_many] = self
|
211
|
+
|
212
|
+
# Default name symbol for the join table.
|
213
|
+
def default_join_table
|
214
|
+
([self[:class_name].demodulize, self[:model].name.to_s.demodulize]. \
|
215
|
+
map{|i| i.pluralize.underscore}.sort.join('_')).to_sym
|
216
|
+
end
|
217
|
+
|
218
|
+
# Default foreign key name symbol for key in join table that points to
|
219
|
+
# current table's primary key (or :left_primary_key column).
|
220
|
+
def default_left_key
|
221
|
+
:"#{self[:model].name.to_s.demodulize.underscore}_id"
|
222
|
+
end
|
223
|
+
|
224
|
+
# Default foreign key name symbol for foreign key in join table that points to
|
225
|
+
# the association's table's primary key (or :right_primary_key column).
|
226
|
+
def default_right_key
|
227
|
+
:"#{self[:name].to_s.singularize}_id"
|
228
|
+
end
|
229
|
+
|
230
|
+
# The key to use for the key hash when eager loading
|
231
|
+
def eager_loader_key
|
232
|
+
self[:left_primary_key]
|
233
|
+
end
|
234
|
+
|
235
|
+
# Whether the associated object needs a primary key to be added/removed,
|
236
|
+
# true for many_to_many associations.
|
237
|
+
def need_associated_primary_key?
|
238
|
+
true
|
239
|
+
end
|
240
|
+
|
241
|
+
# Returns/sets the reciprocal association variable, if one exists
|
242
|
+
def reciprocal
|
243
|
+
return self[:reciprocal] if include?(:reciprocal)
|
244
|
+
left_key = self[:left_key]
|
245
|
+
right_key = self[:right_key]
|
246
|
+
join_table = self[:join_table]
|
247
|
+
associated_class.all_association_reflections.each do |assoc_reflect|
|
248
|
+
if assoc_reflect[:type] == :many_to_many && assoc_reflect[:left_key] == right_key \
|
249
|
+
&& assoc_reflect[:right_key] == left_key && assoc_reflect[:join_table] == join_table
|
250
|
+
return self[:reciprocal] = assoc_reflect[:name]
|
251
|
+
end
|
252
|
+
end
|
253
|
+
self[:reciprocal] = nil
|
254
|
+
end
|
255
|
+
|
256
|
+
# The primary key column to use in the associated table.
|
257
|
+
def right_primary_key
|
258
|
+
self[:right_primary_key] ||= associated_class.primary_key
|
259
|
+
end
|
260
|
+
|
261
|
+
# The columns to select when loading the association, associated_class.table_name.* by default.
|
262
|
+
def select
|
263
|
+
return self[:select] if include?(:select)
|
264
|
+
self[:select] ||= associated_class.table_name.*
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|