This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
/
data_objects_adapter.rb
641 lines (541 loc) · 20.5 KB
/
data_objects_adapter.rb
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
require 'data_objects'
module DataMapper
module Adapters
# DataObjectsAdapter is the base class for all adapers for relational
# databases. If you want to add support for a new RDBMS, it makes
# sense to make your adapter class inherit from this class.
#
# By inheriting from DataObjectsAdapter, you get a copy of all the
# standard sub-modules (Quoting, Coersion and Queries) in your own Adapter.
# You can extend and overwrite these copies without affecting the originals.
class DataObjectsAdapter < AbstractAdapter
extend Chainable
extend Deprecate
deprecate :query, :select
# Retrieve results using an SQL SELECT statement
#
# @param [String] statement
# the SQL SELECT statement
# @param [Array] *bind_values
# optional bind values to merge into the statement
#
# @return [Array]
# if fields > 1, return an Array of Struct objects
# if fields == 1, return an Array of objects
#
# @api public
def select(statement, *bind_values)
with_connection do |connection|
reader = connection.create_command(statement).execute_reader(*bind_values)
fields = reader.fields
results = []
begin
if fields.size > 1
fields = fields.map { |field| Extlib::Inflection.underscore(field).to_sym }
struct = Struct.new(*fields)
while reader.next!
results << struct.new(*reader.values)
end
else
while reader.next!
results << reader.values.at(0)
end
end
ensure
reader.close
end
results
end
end
# Execute non-SELECT SQL query
#
# @param [String] statement
# the SQL statement
# @param [Array] *bind_values
# optional bind values to merge into the statement
#
# @return [DataObjects::Result]
# result with number of affected rows, and insert id if any
#
# @api public
def execute(statement, *bind_values)
with_connection do |connection|
command = connection.create_command(statement)
command.execute_non_query(*bind_values)
end
end
# For each model instance in resources, issues an SQL INSERT
# (or equivalent) statement to create a new record in the data store for
# the instance
#
# Note that this method does not update identity map. A plugin needs to use
# adapter directly, it is up to plugin developer to keep identity map
# up to date.
#
# @param [Enumerable(Resource)] resources
# The list of resources (model instances) to create
#
# @return [Integer]
# The number of records that were actually saved into the database
#
# @api semipublic
def create(resources)
resources.each do |resource|
model = resource.model
serial = model.serial(name)
attributes = resource.dirty_attributes
properties = []
bind_values = []
# make the order of the properties consistent
model.properties(name).each do |property|
next unless attributes.key?(property)
bind_value = attributes[property]
# skip insering NULL for columns that are serial or without a default
next if bind_value.nil? && (property.serial? || !property.default?)
# if serial is being set explicitly, do not set it again
if property.equal?(serial)
serial = nil
end
properties << property
bind_values << bind_value
end
statement = insert_statement(model, properties, serial)
result = execute(statement, *bind_values)
if result.affected_rows == 1 && serial
serial.set!(resource, result.insert_id)
end
end
end
# Constructs and executes SELECT query, then instantiates
# one or many object from result set.
#
# @param [Query] query
# composition of the query to perform
#
# @return [Array]
# result set of the query
#
# @api semipublic
def read(query)
fields = query.fields
types = fields.map { |property| property.primitive }
statement, bind_values = select_statement(query)
records = []
with_connection do |connection|
command = connection.create_command(statement)
command.set_types(types)
reader = command.execute_reader(*bind_values)
begin
while reader.next!
records << fields.zip(reader.values).to_hash
end
ensure
reader.close
end
end
records
end
# Constructs and executes UPDATE statement for given
# attributes and a query
#
# @param [Hash(Property => Object)] attributes
# hash of attribute values to set, keyed by Property
# @param [Collection] collection
# collection of records to be updated
#
# @return [Integer]
# the number of records updated
#
# @api semipublic
def update(attributes, collection)
query = collection.query
# TODO: if the query contains any links, a limit or an offset
# use a subselect to get the rows to be updated
properties = []
bind_values = []
# make the order of the properties consistent
query.model.properties(name).each do |property|
next unless attributes.key?(property)
properties << property
bind_values << attributes[property]
end
statement, conditions_bind_values = update_statement(properties, query)
bind_values.concat(conditions_bind_values)
execute(statement, *bind_values).affected_rows
end
# Constructs and executes DELETE statement for given query
#
# @param [Collection] collection
# collection of records to be deleted
#
# @return [Integer]
# the number of records deleted
#
# @api semipublic
def delete(collection)
query = collection.query
# TODO: if the query contains any links, a limit or an offset
# use a subselect to get the rows to be deleted
statement, bind_values = delete_statement(query)
execute(statement, *bind_values).affected_rows
end
protected
# TODO: document
# @api private
def normalized_uri
@normalized_uri ||=
begin
query = @options.except(:adapter, :user, :password, :host, :port, :path, :fragment, :scheme, :query, :username, :database)
query = nil if query.empty?
DataObjects::URI.new(
@options[:adapter],
@options[:user] || @options[:username],
@options[:password],
@options[:host],
@options[:port],
@options[:path] || @options[:database],
query,
@options[:fragment]
).freeze
end
end
chainable do
protected
# Instantiates new connection object
#
# @api semipublic
def open_connection
# DataObjects::Connection.new(uri) will give you back the right
# driver based on the DataObjects::URI#scheme
connection = connection_stack.last || DataObjects::Connection.new(normalized_uri)
connection_stack << connection
connection
end
# Takes connection and closes it
#
# @api semipublic
def close_connection(connection)
connection_stack.pop
connection.close if connection_stack.empty?
end
end
private
# TODO: document
# @api public
def initialize(name, uri_or_options)
super
# Default the driver-specific logger to DataMapper's logger
if driver_module = DataObjects.const_get(normalized_uri.scheme.capitalize)
driver_module.logger = DataMapper.logger if driver_module.respond_to?(:logger=)
end
end
# TODO: document
# @api private
def connection_stack
connection_stack_for = Thread.current[:dm_do_connection_stack] ||= {}
connection_stack_for[self] ||= []
end
# TODO: document
# @api private
def with_connection
begin
yield connection = open_connection
rescue Exception => exception
DataMapper.logger.error(exception.to_s)
raise exception
ensure
close_connection(connection) if connection
end
end
# This module is just for organization. The methods are included into the
# Adapter below.
module SQL #:nodoc:
IDENTIFIER_MAX_LENGTH = 128
# TODO: document
# @api semipublic
def property_to_column_name(property, qualify)
if qualify
table_name = property.model.storage_name(name)
"#{quote_name(table_name)}.#{quote_name(property.field)}"
else
quote_name(property.field)
end
end
private
# Adapters requiring a RETURNING syntax for INSERT statements
# should overwrite this to return true.
#
# @api private
def supports_returning?
false
end
# Adapters that do not support the DEFAULT VALUES syntax for
# INSERT statements should overwrite this to return false.
#
# @api private
def supports_default_values?
true
end
# Constructs SELECT statement for given query,
#
# @return [String] SELECT statement as a string
#
# @api private
def select_statement(query)
qualify = query.links.any?
fields = query.fields
order_by = query.order
group_by = if qualify || query.unique?
fields.select { |property| property.kind_of?(Property) }
end
conditions_statement, bind_values = conditions_statement(query.conditions, qualify)
statement = "SELECT #{columns_statement(fields, qualify)}"
statement << " FROM #{quote_name(query.model.storage_name(name))}"
statement << join_statement(query, qualify) if qualify
statement << " WHERE #{conditions_statement}" unless conditions_statement.blank?
statement << " GROUP BY #{columns_statement(group_by, qualify)}" if group_by && group_by.any?
statement << " ORDER BY #{order_statement(order_by, qualify)}" if order_by && order_by.any?
add_limit_offset!(statement, query.limit, query.offset, bind_values)
return statement, bind_values
end
# default construction of LIMIT and OFFSET
# overriden in Oracle adapter
def add_limit_offset!(statement, limit, offset, bind_values)
if limit
statement << ' LIMIT ?'
bind_values << limit
end
if limit && offset > 0
statement << ' OFFSET ?'
bind_values << offset
end
end
# Constructs INSERT statement for given query,
#
# @return [String] INSERT statement as a string
#
# @api private
def insert_statement(model, properties, serial)
statement = "INSERT INTO #{quote_name(model.storage_name(name))} "
if supports_default_values? && properties.empty?
statement << default_values_clause
else
statement << <<-SQL.compress_lines
(#{properties.map { |property| quote_name(property.field) }.join(', ')})
VALUES
(#{(['?'] * properties.size).join(', ')})
SQL
end
if supports_returning? && serial
statement << returning_clause(serial)
end
statement
end
# by default PostgreSQL syntax
# overrided in Oracle adapter
def default_values_clause
'DEFAULT VALUES'
end
# by default PostgreSQL syntax
# overrided in Oracle adapter
def returning_clause(serial)
" RETURNING #{quote_name(serial.field)}"
end
# Constructs UPDATE statement for given query,
#
# @return [String] UPDATE statement as a string
#
# @api private
def update_statement(properties, query)
conditions_statement, bind_values = conditions_statement(query.conditions)
statement = "UPDATE #{quote_name(query.model.storage_name(name))}"
statement << " SET #{properties.map { |property| "#{quote_name(property.field)} = ?" }.join(', ')}"
statement << " WHERE #{conditions_statement}" unless conditions_statement.blank?
return statement, bind_values
end
# Constructs DELETE statement for given query,
#
# @return [String] DELETE statement as a string
#
# @api private
def delete_statement(query)
conditions_statement, bind_values = conditions_statement(query.conditions)
statement = "DELETE FROM #{quote_name(query.model.storage_name(name))}"
statement << " WHERE #{conditions_statement}" unless conditions_statement.blank?
return statement, bind_values
end
# Constructs comma separated list of fields
#
# @return [String]
# list of fields as a string
#
# @api private
def columns_statement(properties, qualify)
properties.map { |property| property_to_column_name(property, qualify) }.join(', ')
end
# Constructs joins clause
#
# @return [String]
# joins clause
#
# @api private
def join_statement(query, qualify)
statement = ''
query.links.reverse_each do |relationship|
statement << " INNER JOIN #{quote_name(relationship.source_model.storage_name(name))} ON "
statement << relationship.target_key.zip(relationship.source_key).map do |target_property, source_property|
"#{property_to_column_name(target_property, qualify)} = #{property_to_column_name(source_property, qualify)}"
end.join(' AND ')
end
statement
end
# Constructs where clause
#
# @return [String]
# where clause
#
# @api private
def conditions_statement(conditions, qualify = false)
case conditions
when Query::Conditions::NotOperation then negate_operation(conditions, qualify)
when Query::Conditions::AbstractOperation then operation_statement(conditions, qualify)
when Query::Conditions::AbstractComparison then comparison_statement(conditions, qualify)
when Array
statement, bind_values = conditions # handle raw conditions
[ "(#{statement})", bind_values ]
end
end
# Constructs order clause
#
# @return [String]
# order clause
#
# @api private
def order_statement(order, qualify)
statements = order.map do |direction|
statement = property_to_column_name(direction.target, qualify)
statement << ' DESC' if direction.operator == :desc
statement
end
statements.join(', ')
end
# TODO: document
# @api private
def negate_operation(operation, qualify)
@negated = !@negated
begin
conditions_statement(operation.operands.first, qualify)
ensure
@negated = !@negated
end
end
# TODO: document
# @api private
def operation_statement(operation, qualify)
statements = []
bind_values = []
operation.each do |operand|
statement, values = conditions_statement(operand, qualify)
statements << statement
bind_values.concat(values)
end
join_with = operation.kind_of?(@negated ? Query::Conditions::OrOperation : Query::Conditions::AndOperation) ? 'AND' : 'OR'
statement = statements.join(" #{join_with} ")
if statements.size > 1
statement = "(#{statement})"
end
return statement, bind_values
end
# Constructs comparison clause
#
# @return [String]
# comparison clause
#
# @api private
def comparison_statement(comparison, qualify)
value = comparison.value
# TODO: move exclusive Range handling into another method, and
# update conditions_statement to use it
# break exclusive Range queries up into two comparisons ANDed together
if value.kind_of?(Range) && value.exclude_end?
operation = Query::Conditions::Operation.new(:and,
Query::Conditions::Comparison.new(:gte, comparison.subject, value.first),
Query::Conditions::Comparison.new(:lt, comparison.subject, value.last)
)
statement, bind_values = conditions_statement(operation, qualify)
return "(#{statement})", bind_values
elsif comparison.relationship?
return conditions_statement(comparison.foreign_key_mapping, qualify)
end
operator = case comparison
when Query::Conditions::EqualToComparison then @negated ? inequality_operator(comparison.subject, value) : equality_operator(comparison.subject, value)
when Query::Conditions::InclusionComparison then @negated ? exclude_operator(comparison.subject, value) : include_operator(comparison.subject, value)
when Query::Conditions::RegexpComparison then @negated ? not_regexp_operator(value) : regexp_operator(value)
when Query::Conditions::LikeComparison then @negated ? unlike_operator(value) : like_operator(value)
when Query::Conditions::GreaterThanComparison then @negated ? '<=' : '>'
when Query::Conditions::LessThanComparison then @negated ? '>=' : '<'
when Query::Conditions::GreaterThanOrEqualToComparison then @negated ? '<' : '>='
when Query::Conditions::LessThanOrEqualToComparison then @negated ? '>' : '<='
end
# if operator return value contains ? then it means that it is function call
# and it contains placeholder (%s) for property name as well (used in Oracle adapter for regexp operator)
if operator.include?('?')
return operator % property_to_column_name(comparison.subject, qualify), [ value ]
else
return "#{property_to_column_name(comparison.subject, qualify)} #{operator} #{value.nil? ? 'NULL' : '?'}", [ value ].compact
end
end
# TODO: document
# @api private
def equality_operator(property, operand)
operand.nil? ? 'IS' : '='
end
# TODO: document
# @api private
def inequality_operator(property, operand)
operand.nil? ? 'IS NOT' : '<>'
end
# TODO: document
# @api private
def include_operator(property, operand)
case operand
when Array then 'IN'
when Range then 'BETWEEN'
end
end
# TODO: document
# @api private
def exclude_operator(property, operand)
"NOT #{include_operator(property, operand)}"
end
# TODO: document
# @api private
def regexp_operator(operand)
'~'
end
# TODO: document
# @api private
def not_regexp_operator(operand)
'!~'
end
# TODO: document
# @api private
def like_operator(operand)
'LIKE'
end
# TODO: document
# @api private
def unlike_operator(operand)
'NOT LIKE'
end
# TODO: document
# @api private
def quote_name(name)
"\"#{name[0, self.class::IDENTIFIER_MAX_LENGTH].gsub('"', '""')}\""
end
end #module SQL
include SQL
end # class DataObjectsAdapter
const_added(:DataObjectsAdapter)
end # module Adapters
end # module DataMapper