Module: DataMapper::Aggregates::DataObjectsAdapter

Extended by:
Chainable
Defined in:
lib/dm-aggregates/adapters/dm-do-adapter.rb

Instance Method Summary collapse

Instance Method Details

#aggregate(query) ⇒ Object



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
# File 'lib/dm-aggregates/adapters/dm-do-adapter.rb', line 6

def aggregate(query)
  fields = query.fields
  types  = fields.map { |p| p.respond_to?(:operator) ? String : p.primitive }

  field_size = fields.size

  records = []

  with_connection do |connection|
    statement, bind_values = select_statement(query)

    command = connection.create_command(statement)
    command.set_types(types)

    reader = command.execute_reader(*bind_values)

    begin
      while(reader.next!)
        row = fields.zip(reader.values).map do |field, value|
          if field.respond_to?(:operator)
            send(field.operator, field.target, value)
          else
            field.load(value)
          end
        end

        records << (field_size > 1 ? row : row[0])
      end
    ensure
      reader.close
    end
  end

  records
end