datamapper 0.3.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +0 -0
- data/Manifest.txt +5 -0
- data/README.txt +11 -0
- data/Rakefile +70 -0
- data/lib/datamapper.rb +8 -0
- metadata +152 -319
- data/CHANGELOG +0 -145
- data/FAQ +0 -96
- data/MIT-LICENSE +0 -22
- data/QUICKLINKS +0 -12
- data/README +0 -105
- data/environment.rb +0 -62
- data/example.rb +0 -156
- data/lib/data_mapper.rb +0 -88
- data/lib/data_mapper/adapters/abstract_adapter.rb +0 -43
- data/lib/data_mapper/adapters/data_object_adapter.rb +0 -480
- data/lib/data_mapper/adapters/mysql_adapter.rb +0 -72
- data/lib/data_mapper/adapters/postgresql_adapter.rb +0 -258
- data/lib/data_mapper/adapters/sql/coersion.rb +0 -134
- data/lib/data_mapper/adapters/sql/commands/load_command.rb +0 -545
- data/lib/data_mapper/adapters/sql/mappings/associations_set.rb +0 -34
- data/lib/data_mapper/adapters/sql/mappings/column.rb +0 -279
- data/lib/data_mapper/adapters/sql/mappings/conditions.rb +0 -172
- data/lib/data_mapper/adapters/sql/mappings/schema.rb +0 -60
- data/lib/data_mapper/adapters/sql/mappings/table.rb +0 -459
- data/lib/data_mapper/adapters/sql/quoting.rb +0 -24
- data/lib/data_mapper/adapters/sqlite3_adapter.rb +0 -159
- data/lib/data_mapper/associations.rb +0 -106
- data/lib/data_mapper/associations/belongs_to_association.rb +0 -160
- data/lib/data_mapper/associations/has_and_belongs_to_many_association.rb +0 -437
- data/lib/data_mapper/associations/has_many_association.rb +0 -283
- data/lib/data_mapper/associations/has_n_association.rb +0 -143
- data/lib/data_mapper/associations/reference.rb +0 -47
- data/lib/data_mapper/attributes.rb +0 -73
- data/lib/data_mapper/auto_migrations.rb +0 -36
- data/lib/data_mapper/base.rb +0 -17
- data/lib/data_mapper/callbacks.rb +0 -107
- data/lib/data_mapper/context.rb +0 -112
- data/lib/data_mapper/database.rb +0 -234
- data/lib/data_mapper/dependency_queue.rb +0 -28
- data/lib/data_mapper/embedded_value.rb +0 -145
- data/lib/data_mapper/identity_map.rb +0 -47
- data/lib/data_mapper/is/tree.rb +0 -121
- data/lib/data_mapper/migration.rb +0 -155
- data/lib/data_mapper/persistence.rb +0 -852
- data/lib/data_mapper/property.rb +0 -310
- data/lib/data_mapper/query.rb +0 -164
- data/lib/data_mapper/support/blank.rb +0 -35
- data/lib/data_mapper/support/connection_pool.rb +0 -117
- data/lib/data_mapper/support/enumerable.rb +0 -35
- data/lib/data_mapper/support/errors.rb +0 -16
- data/lib/data_mapper/support/inflector.rb +0 -265
- data/lib/data_mapper/support/object.rb +0 -54
- data/lib/data_mapper/support/serialization.rb +0 -96
- data/lib/data_mapper/support/silence.rb +0 -10
- data/lib/data_mapper/support/string.rb +0 -72
- data/lib/data_mapper/support/struct.rb +0 -7
- data/lib/data_mapper/support/symbol.rb +0 -82
- data/lib/data_mapper/support/typed_set.rb +0 -65
- data/lib/data_mapper/types/base.rb +0 -44
- data/lib/data_mapper/types/string.rb +0 -34
- data/lib/data_mapper/validatable_extensions/errors.rb +0 -12
- data/lib/data_mapper/validatable_extensions/macros.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validatable_instance_methods.rb +0 -62
- data/lib/data_mapper/validatable_extensions/validation_base.rb +0 -18
- data/lib/data_mapper/validatable_extensions/validations/formats/email.rb +0 -43
- data/lib/data_mapper/validatable_extensions/validations/validates_acceptance_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_confirmation_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_each.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_format_of.rb +0 -28
- data/lib/data_mapper/validatable_extensions/validations/validates_length_of.rb +0 -15
- data/lib/data_mapper/validatable_extensions/validations/validates_numericality_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_presence_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_true_for.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_uniqueness_of.rb +0 -40
- data/lib/data_mapper/validations.rb +0 -20
- data/lib/data_mapper/validations/number_validator.rb +0 -40
- data/lib/data_mapper/validations/string_validator.rb +0 -20
- data/lib/data_mapper/validations/validator.rb +0 -13
- data/performance.rb +0 -307
- data/plugins/can_has_sphinx/LICENSE +0 -23
- data/plugins/can_has_sphinx/README +0 -4
- data/plugins/can_has_sphinx/REVISION +0 -1
- data/plugins/can_has_sphinx/Rakefile +0 -22
- data/plugins/can_has_sphinx/init.rb +0 -1
- data/plugins/can_has_sphinx/install.rb +0 -1
- data/plugins/can_has_sphinx/lib/acts_as_sphinx.rb +0 -123
- data/plugins/can_has_sphinx/lib/sphinx.rb +0 -460
- data/plugins/can_has_sphinx/scripts/sphinx.sh +0 -47
- data/plugins/can_has_sphinx/tasks/acts_as_sphinx_tasks.rake +0 -41
- data/profile_data_mapper.rb +0 -40
- data/rakefile.rb +0 -159
- data/spec/acts_as_tree_spec.rb +0 -67
- data/spec/adapters/data_object_adapter_spec.rb +0 -31
- data/spec/associations/belongs_to_association_spec.rb +0 -98
- data/spec/associations/has_and_belongs_to_many_association_spec.rb +0 -377
- data/spec/associations/has_many_association_spec.rb +0 -337
- data/spec/attributes_spec.rb +0 -52
- data/spec/auto_migrations_spec.rb +0 -101
- data/spec/callbacks_spec.rb +0 -186
- data/spec/can_has_sphinx.rb +0 -5
- data/spec/coersion_spec.rb +0 -41
- data/spec/column_spec.rb +0 -114
- data/spec/count_command_spec.rb +0 -45
- data/spec/database_spec.rb +0 -18
- data/spec/dataobjects_spec.rb +0 -27
- data/spec/delete_command_spec.rb +0 -11
- data/spec/dependency_spec.rb +0 -29
- data/spec/embedded_value_spec.rb +0 -161
- data/spec/fixtures/animals.yaml +0 -33
- data/spec/fixtures/animals_exhibits.yaml +0 -2
- data/spec/fixtures/careers.yaml +0 -5
- data/spec/fixtures/comments.yaml +0 -1
- data/spec/fixtures/exhibits.yaml +0 -90
- data/spec/fixtures/fruit.yaml +0 -6
- data/spec/fixtures/people.yaml +0 -37
- data/spec/fixtures/posts.yaml +0 -3
- data/spec/fixtures/projects.yaml +0 -13
- data/spec/fixtures/sections.yaml +0 -5
- data/spec/fixtures/serializers.yaml +0 -6
- data/spec/fixtures/tasks.yaml +0 -6
- data/spec/fixtures/tasks_tasks.yaml +0 -2
- data/spec/fixtures/tomatoes.yaml +0 -1
- data/spec/fixtures/users.yaml +0 -1
- data/spec/fixtures/zoos.yaml +0 -24
- data/spec/is_a_tree_spec.rb +0 -149
- data/spec/legacy_spec.rb +0 -16
- data/spec/load_command_spec.rb +0 -322
- data/spec/magic_columns_spec.rb +0 -26
- data/spec/migration_spec.rb +0 -267
- data/spec/mock_adapter.rb +0 -20
- data/spec/models/animal.rb +0 -12
- data/spec/models/candidate.rb +0 -8
- data/spec/models/career.rb +0 -7
- data/spec/models/chain.rb +0 -8
- data/spec/models/comment.rb +0 -6
- data/spec/models/exhibit.rb +0 -14
- data/spec/models/fence.rb +0 -7
- data/spec/models/fruit.rb +0 -8
- data/spec/models/job.rb +0 -8
- data/spec/models/person.rb +0 -30
- data/spec/models/post.rb +0 -14
- data/spec/models/project.rb +0 -41
- data/spec/models/sales_person.rb +0 -5
- data/spec/models/section.rb +0 -8
- data/spec/models/serializer.rb +0 -5
- data/spec/models/task.rb +0 -9
- data/spec/models/tomato.rb +0 -27
- data/spec/models/user.rb +0 -12
- data/spec/models/zoo.rb +0 -13
- data/spec/natural_key_spec.rb +0 -36
- data/spec/paranoia_spec.rb +0 -38
- data/spec/persistence_spec.rb +0 -479
- data/spec/postgres_spec.rb +0 -96
- data/spec/property_spec.rb +0 -151
- data/spec/query_spec.rb +0 -77
- data/spec/save_command_spec.rb +0 -94
- data/spec/schema_spec.rb +0 -8
- data/spec/serialize_spec.rb +0 -19
- data/spec/single_table_inheritance_spec.rb +0 -43
- data/spec/spec_helper.rb +0 -45
- data/spec/support/blank_spec.rb +0 -8
- data/spec/support/inflector_spec.rb +0 -41
- data/spec/support/object_spec.rb +0 -9
- data/spec/support/serialization_spec.rb +0 -61
- data/spec/support/silence_spec.rb +0 -15
- data/spec/support/string_spec.rb +0 -7
- data/spec/support/struct_spec.rb +0 -12
- data/spec/support/typed_set_spec.rb +0 -66
- data/spec/symbolic_operators_spec.rb +0 -27
- data/spec/table_spec.rb +0 -79
- data/spec/types/string.rb +0 -81
- data/spec/validates_confirmation_of_spec.rb +0 -55
- data/spec/validates_format_of_spec.rb +0 -78
- data/spec/validates_length_of_spec.rb +0 -117
- data/spec/validates_uniqueness_of_spec.rb +0 -92
- data/spec/validations/number_validator.rb +0 -59
- data/spec/validations/string_validator.rb +0 -14
- data/spec/validations_spec.rb +0 -141
- data/tasks/fixtures.rb +0 -53
@@ -1,34 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
module Adapters
|
3
|
-
module Sql
|
4
|
-
module Mappings
|
5
|
-
|
6
|
-
class AssociationsSet
|
7
|
-
|
8
|
-
include Enumerable
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
@set = {}
|
12
|
-
end
|
13
|
-
|
14
|
-
def <<(association)
|
15
|
-
@set[association.name] = association
|
16
|
-
end
|
17
|
-
|
18
|
-
def [](name)
|
19
|
-
@set[name]
|
20
|
-
end
|
21
|
-
|
22
|
-
def each
|
23
|
-
@set.each { |name, association| yield(association) }
|
24
|
-
end
|
25
|
-
|
26
|
-
def empty?
|
27
|
-
@set.empty?
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end # module Mappings
|
32
|
-
end # module Sql
|
33
|
-
end # module Adapters
|
34
|
-
end # module DataMapper
|
@@ -1,279 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
module Adapters
|
3
|
-
module Sql
|
4
|
-
module Mappings
|
5
|
-
|
6
|
-
# TODO: There are of course many more options to add here.
|
7
|
-
# Ordinal, Length/Size, Nullability are just a few.
|
8
|
-
class Column
|
9
|
-
attr_reader :type, :name, :ordinal, :size, :default, :check
|
10
|
-
attr_writer :lazy, :index, :unique
|
11
|
-
attr_accessor :table, :options
|
12
|
-
|
13
|
-
def initialize(adapter, table, name, type, ordinal, options = {})
|
14
|
-
@adapter = adapter
|
15
|
-
@table = table
|
16
|
-
@name, self.type, @options = name.to_sym, type, options
|
17
|
-
@ordinal = ordinal
|
18
|
-
parse_options!
|
19
|
-
end
|
20
|
-
|
21
|
-
def parse_options!
|
22
|
-
@key = @options[:key] == true || @options[:serial] == true
|
23
|
-
@nullable = @options.has_key?(:nullable) ? @options[:nullable] : !@key
|
24
|
-
@lazy = @options.has_key?(:lazy) ? @options[:lazy] : (@type == :text && !@key)
|
25
|
-
@serial = @options[:serial] == true
|
26
|
-
@default = @options[:default]
|
27
|
-
|
28
|
-
@unique = if @options[:index] == :unique then @options.delete(:index); true else false end
|
29
|
-
@index = @options[:index]
|
30
|
-
@check = @options[:check] # only for postgresql
|
31
|
-
|
32
|
-
@size = if @options.has_key?(:size)
|
33
|
-
@options[:size]
|
34
|
-
elsif @options.has_key?(:length)
|
35
|
-
@options[:length]
|
36
|
-
else
|
37
|
-
case type
|
38
|
-
when :integer then 11
|
39
|
-
when :string, :class then 50
|
40
|
-
else nil
|
41
|
-
end
|
42
|
-
end
|
43
|
-
@size = @size.last if @size.is_a?(Range)
|
44
|
-
end
|
45
|
-
|
46
|
-
def defaulted?() instance_variables.include?("@default") end
|
47
|
-
|
48
|
-
def type=(value)
|
49
|
-
self.flush_sql_caches!
|
50
|
-
@type = value
|
51
|
-
(class << self; self end).class_eval <<-EOS
|
52
|
-
def type_cast_value(value)
|
53
|
-
@adapter.type_cast_#{@type}(value)
|
54
|
-
end
|
55
|
-
EOS
|
56
|
-
@type
|
57
|
-
end
|
58
|
-
|
59
|
-
def name=(value)
|
60
|
-
flush_sql_caches!
|
61
|
-
@name = value
|
62
|
-
end
|
63
|
-
|
64
|
-
# Determines if the field should be lazy loaded.
|
65
|
-
# You can set this explicitly, or accept the default,
|
66
|
-
# which is false for all but text fields.
|
67
|
-
def lazy?
|
68
|
-
@lazy
|
69
|
-
end
|
70
|
-
|
71
|
-
def nullable?
|
72
|
-
@nullable
|
73
|
-
end
|
74
|
-
|
75
|
-
def key?
|
76
|
-
@key
|
77
|
-
end
|
78
|
-
|
79
|
-
def serial?
|
80
|
-
@serial
|
81
|
-
end
|
82
|
-
|
83
|
-
def unique?
|
84
|
-
@unique
|
85
|
-
end
|
86
|
-
|
87
|
-
def index?
|
88
|
-
@index
|
89
|
-
end
|
90
|
-
|
91
|
-
def default=(value)
|
92
|
-
self.flush_sql_caches!
|
93
|
-
@default = value
|
94
|
-
end
|
95
|
-
|
96
|
-
def to_sym
|
97
|
-
@name
|
98
|
-
end
|
99
|
-
|
100
|
-
def instance_variable_name
|
101
|
-
@instance_variable_name || (@instance_variable_name = "@#{@name.to_s.gsub(/\?$/, '')}".freeze)
|
102
|
-
end
|
103
|
-
|
104
|
-
def to_s
|
105
|
-
@name.to_s
|
106
|
-
end
|
107
|
-
|
108
|
-
def column_name
|
109
|
-
@column_name || (@column_name = (@options.has_key?(:column) ? @options[:column].to_s : name.to_s.gsub(/\?$/, '')).freeze)
|
110
|
-
end
|
111
|
-
|
112
|
-
def to_sql(include_table_name = false)
|
113
|
-
if include_table_name
|
114
|
-
@to_sql_with_table_name || @to_sql_with_table_name = begin
|
115
|
-
(@table.to_sql + '.' + @adapter.quote_column_name(column_name)).freeze
|
116
|
-
end
|
117
|
-
else
|
118
|
-
@to_sql || (@to_sql = @adapter.quote_column_name(column_name).freeze)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
def size=(val)
|
123
|
-
self.flush_sql_caches!
|
124
|
-
@size = val
|
125
|
-
end
|
126
|
-
|
127
|
-
def inspect
|
128
|
-
"#<%s:0x%x @name=%s, @type=%s, @options=%s>" % [self.class.name, (object_id * 2), to_sql, type.inspect, options.inspect]
|
129
|
-
end
|
130
|
-
|
131
|
-
def to_create_sql
|
132
|
-
"ALTER TABLE " << table.to_sql << " ADD " << to_long_form
|
133
|
-
end
|
134
|
-
|
135
|
-
def to_alter_sql
|
136
|
-
"ALTER TABLE " << table.to_sql << " MODIFY COLUMN " << to_long_form
|
137
|
-
end
|
138
|
-
|
139
|
-
def to_drop_sql
|
140
|
-
"ALTER TABLE " << table.to_sql << " DROP COLUMN " << to_sql
|
141
|
-
end
|
142
|
-
|
143
|
-
def create!
|
144
|
-
@table.columns << self
|
145
|
-
flush_sql_caches!
|
146
|
-
|
147
|
-
@adapter.connection do |db|
|
148
|
-
command = db.create_command(to_create_sql)
|
149
|
-
command.execute_non_query
|
150
|
-
end
|
151
|
-
true
|
152
|
-
end
|
153
|
-
|
154
|
-
def drop!
|
155
|
-
@table.columns.delete(self)
|
156
|
-
flush_sql_caches!
|
157
|
-
|
158
|
-
@adapter.connection do |db|
|
159
|
-
command = db.create_command(to_drop_sql)
|
160
|
-
command.execute_non_query
|
161
|
-
end
|
162
|
-
true
|
163
|
-
end
|
164
|
-
|
165
|
-
def alter!
|
166
|
-
flush_sql_caches!
|
167
|
-
@adapter.connection do |db|
|
168
|
-
command = db.create_command(to_alter_sql)
|
169
|
-
command.execute_non_query
|
170
|
-
end
|
171
|
-
true
|
172
|
-
end
|
173
|
-
|
174
|
-
def rename!(new_name)
|
175
|
-
old_name = name # Store the old_name
|
176
|
-
|
177
|
-
new_column = @table.add_column(new_name, self.type, self.options.merge(:ordinal => self.ordinal))
|
178
|
-
|
179
|
-
# Create the new column
|
180
|
-
new_column.create!
|
181
|
-
|
182
|
-
# Copy the data from one column to the other.
|
183
|
-
@adapter.connection do |db|
|
184
|
-
command = db.create_command <<-EOS.compress_lines
|
185
|
-
UPDATE #{@table.to_sql} SET
|
186
|
-
#{new_column.to_sql} = #{to_sql}
|
187
|
-
EOS
|
188
|
-
command.execute_non_query
|
189
|
-
end
|
190
|
-
|
191
|
-
self.drop!
|
192
|
-
new_column
|
193
|
-
end
|
194
|
-
|
195
|
-
def to_long_form
|
196
|
-
@to_long_form || begin
|
197
|
-
@to_long_form = "#{to_sql} #{type_declaration}"
|
198
|
-
|
199
|
-
unless nullable? || not_null_declaration.blank?
|
200
|
-
@to_long_form << " #{not_null_declaration}"
|
201
|
-
end
|
202
|
-
|
203
|
-
# NOTE: We only do inline PRIMARY KEY declarations
|
204
|
-
# if the column is also serial since we know
|
205
|
-
# "there can be only one".
|
206
|
-
if key? && serial? && !primary_key_declaration.blank?
|
207
|
-
@to_long_form << " #{primary_key_declaration}"
|
208
|
-
end
|
209
|
-
|
210
|
-
if serial? && !serial_declaration.blank?
|
211
|
-
@to_long_form << " #{serial_declaration}"
|
212
|
-
end
|
213
|
-
|
214
|
-
unless default.nil? || (value = default_declaration).blank?
|
215
|
-
@to_long_form << " #{value}"
|
216
|
-
end
|
217
|
-
|
218
|
-
if unique? && !unique_declaration.blank?
|
219
|
-
@to_long_form << " #{unique_declaration}"
|
220
|
-
end
|
221
|
-
|
222
|
-
@to_long_form
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
def <=>(other)
|
227
|
-
ordinal <=> other.ordinal
|
228
|
-
end
|
229
|
-
|
230
|
-
def hash
|
231
|
-
@hash || @hash = to_sql(true).hash
|
232
|
-
end
|
233
|
-
|
234
|
-
def eql?(other)
|
235
|
-
name == other.name
|
236
|
-
end
|
237
|
-
|
238
|
-
protected
|
239
|
-
|
240
|
-
def primary_key_declaration
|
241
|
-
"PRIMARY KEY"
|
242
|
-
end
|
243
|
-
|
244
|
-
def type_declaration
|
245
|
-
sql = "#{@adapter.class::TYPES[type] || type}"
|
246
|
-
sql << "(#{size})" unless size.nil?
|
247
|
-
sql
|
248
|
-
end
|
249
|
-
|
250
|
-
def not_null_declaration
|
251
|
-
"NOT NULL"
|
252
|
-
end
|
253
|
-
|
254
|
-
def serial_declaration
|
255
|
-
"AUTO_INCREMENT"
|
256
|
-
end
|
257
|
-
|
258
|
-
def unique_declaration
|
259
|
-
"UNIQUE"
|
260
|
-
end
|
261
|
-
|
262
|
-
def default_declaration
|
263
|
-
@adapter.connection { |db| db.create_command("DEFAULT ?").escape_sql([default]) }
|
264
|
-
end
|
265
|
-
|
266
|
-
def flush_sql_caches!
|
267
|
-
@to_long_form = nil
|
268
|
-
@to_sql = nil
|
269
|
-
@to_sql_with_table_name = nil
|
270
|
-
@column_name = nil
|
271
|
-
@table.flush_sql_caches!(false)
|
272
|
-
end
|
273
|
-
|
274
|
-
end
|
275
|
-
|
276
|
-
end
|
277
|
-
end
|
278
|
-
end
|
279
|
-
end
|
@@ -1,172 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
module Adapters
|
3
|
-
module Sql
|
4
|
-
module Mappings
|
5
|
-
|
6
|
-
class Conditions
|
7
|
-
def initialize(table, adapter, qualify_columns=false, options={})
|
8
|
-
@table = table
|
9
|
-
@qualify_columns = qualify_columns
|
10
|
-
|
11
|
-
# BEGIN: Partion out the options hash into general options,
|
12
|
-
# and conditions.
|
13
|
-
standard_find_options = adapter.class::FIND_OPTIONS
|
14
|
-
conditions_hash = {}
|
15
|
-
|
16
|
-
options.each do |key,value|
|
17
|
-
unless standard_find_options.include?(key) && key != :conditions
|
18
|
-
conditions_hash[key] = value
|
19
|
-
end
|
20
|
-
end
|
21
|
-
# END
|
22
|
-
|
23
|
-
@conditions = parse_conditions(conditions_hash)
|
24
|
-
end
|
25
|
-
|
26
|
-
# Generate a statement after 'WHERE' based on the initialization
|
27
|
-
# arguments.
|
28
|
-
def to_params_sql
|
29
|
-
parameters = []
|
30
|
-
sql = ""
|
31
|
-
|
32
|
-
unless @conditions.empty?
|
33
|
-
sql << ' WHERE ('
|
34
|
-
|
35
|
-
last_index = @conditions.size
|
36
|
-
current_index = 0
|
37
|
-
|
38
|
-
@conditions.each do |condition|
|
39
|
-
case condition
|
40
|
-
when String then sql << condition
|
41
|
-
when Array then
|
42
|
-
sql << condition.shift
|
43
|
-
parameters += condition
|
44
|
-
else
|
45
|
-
raise "Unable to parse condition: #{condition.inspect}" if condition
|
46
|
-
end
|
47
|
-
|
48
|
-
if (current_index += 1) == last_index
|
49
|
-
sql << ')'
|
50
|
-
else
|
51
|
-
sql << ') AND ('
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
parameters.unshift(sql)
|
57
|
-
end
|
58
|
-
|
59
|
-
def parse_conditions(conditions_hash)
|
60
|
-
collection = []
|
61
|
-
|
62
|
-
case x = conditions_hash.delete(:conditions)
|
63
|
-
when Array then
|
64
|
-
# DO NOT mutate incoming Array values!!!
|
65
|
-
# Otherwise the mutated version may impact all the
|
66
|
-
# way up to the options passed to the finders,
|
67
|
-
# and have unintended side-effects.
|
68
|
-
array_copy = x.dup
|
69
|
-
clause = array_copy.shift
|
70
|
-
expression_to_sql(clause, array_copy, collection)
|
71
|
-
when Hash then
|
72
|
-
x.each_pair do |key,value|
|
73
|
-
expression_to_sql(key, value, collection)
|
74
|
-
end
|
75
|
-
else
|
76
|
-
raise "Unable to parse conditions: #{x.inspect}" if x
|
77
|
-
end
|
78
|
-
|
79
|
-
if @table.paranoid?
|
80
|
-
conditions_hash[@table.paranoid_column.name] = nil
|
81
|
-
end
|
82
|
-
|
83
|
-
conditions_hash.each_pair do |key,value|
|
84
|
-
expression_to_sql(key, value, collection)
|
85
|
-
end
|
86
|
-
|
87
|
-
collection
|
88
|
-
end
|
89
|
-
|
90
|
-
# expression_to_sql takes a set of arguments, and turns them into a an
|
91
|
-
# Array of generated SQL, followed by optional Values to interpolate as SQL-Parameters.
|
92
|
-
#
|
93
|
-
# Parameters:
|
94
|
-
# +clause+ The name of the column as a Symbol, a raw-SQL String, a Mappings::Column
|
95
|
-
# instance, or a Symbol::Operator.
|
96
|
-
# +value+ The Value for the condition.
|
97
|
-
# +collector+ An Array representing all conditions that is appended to by expression_to_sql
|
98
|
-
#
|
99
|
-
# Returns: Undefined Output. The work performed is added to the +collector+ argument.
|
100
|
-
# Example:
|
101
|
-
# conditions = []
|
102
|
-
# expression_to_sql(:name, 'Bob', conditions)
|
103
|
-
# => +undefined return value+
|
104
|
-
# conditions.inspect
|
105
|
-
# => ["name = ?", 'Bob']
|
106
|
-
def expression_to_sql(clause, value, collector)
|
107
|
-
case clause
|
108
|
-
when Symbol::Operator then
|
109
|
-
operator = case clause.type
|
110
|
-
when :gt then '>'
|
111
|
-
when :gte then '>='
|
112
|
-
when :lt then '<'
|
113
|
-
when :lte then '<='
|
114
|
-
when :not then inequality_operator(value)
|
115
|
-
when :eql then equality_operator(value)
|
116
|
-
when :like then equality_operator(value, 'LIKE')
|
117
|
-
when :in then equality_operator(value)
|
118
|
-
else raise ArgumentError.new('Operator type not supported')
|
119
|
-
end
|
120
|
-
#Table[column name] is column.to_sql(true/false based on associations or not)
|
121
|
-
collector << ["#{@table[clause].to_sql(@qualify_columns)} #{operator} ?", value]
|
122
|
-
when Symbol then
|
123
|
-
collector << ["#{@table[clause].to_sql(@qualify_columns)} #{equality_operator(value)} ?", value]
|
124
|
-
when String then
|
125
|
-
collector << [clause, *value]
|
126
|
-
when Mappings::Column then
|
127
|
-
collector << ["#{clause.to_sql(@qualify_columns)} #{equality_operator(value)} ?", value]
|
128
|
-
else raise "CAN HAS CRASH? #{clause.inspect}"
|
129
|
-
end
|
130
|
-
rescue => e
|
131
|
-
raise ConditionsError.new(clause, value, e)
|
132
|
-
end
|
133
|
-
|
134
|
-
def equality_operator(value, default = '=')
|
135
|
-
case value
|
136
|
-
when NilClass then 'IS'
|
137
|
-
when Array then 'IN'
|
138
|
-
else default
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def inequality_operator(value, default = '<>')
|
143
|
-
case value
|
144
|
-
when NilClass then 'IS NOT'
|
145
|
-
when Array then 'NOT IN'
|
146
|
-
else default
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
class ConditionsError < StandardError
|
151
|
-
|
152
|
-
attr_reader :inner_error
|
153
|
-
|
154
|
-
def initialize(clause, value, inner_error)
|
155
|
-
@clause, @value, @inner_error = clause, value, inner_error
|
156
|
-
end
|
157
|
-
|
158
|
-
def message
|
159
|
-
"Conditions (:clause => #{@clause.inspect}, :value => #{@value.inspect}) failed: #{@inner_error}"
|
160
|
-
end
|
161
|
-
|
162
|
-
def backtrace
|
163
|
-
@inner_error.backtrace
|
164
|
-
end
|
165
|
-
|
166
|
-
end
|
167
|
-
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|