Class: DataMapper::Adapters::DataObjectsAdapter

Inherits:
AbstractAdapter show all
Extended by:
Migration::ClassMethods
Includes:
Migration, SQL
Defined in:
lib/dm-core/adapters/data_objects_adapter.rb

Overview

You must inherit from the DoAdapter, and implement the required methods to adapt a database library for use with the DataMapper.

NOTE: 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.

Direct Known Subclasses

MysqlAdapter, PostgresAdapter, Sqlite3Adapter

Defined Under Namespace

Modules: Migration, SQL

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#field_naming_convention, #name, #resource_naming_convention, #uri

Instance Method Summary collapse

Methods included from Migration::ClassMethods

type_map

Methods included from Migration

#create_model_storage, #destroy_model_storage, #transaction_primitive, #upgrade_model_storage

Methods included from AbstractAdapter::Migration::ClassMethods

#type_map

Methods included from AbstractAdapter::Transaction

#current_transaction, #pop_transaction, #push_transaction, #transaction_primitive, #within_transaction?

Methods included from AbstractAdapter::Migration

#alter_model_storage, #alter_property_storage, #create_model_storage, #create_property_storage, #destroy_model_storage, #destroy_property_storage, #field_exists?, #storage_exists?, #upgrade_model_storage

Methods included from DataMapper::Assertions

#assert_kind_of

Instance Method Details

#create(resources) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 13

def create(resources)
  created = 0
  resources.each do |resource|
    repository = resource.repository
    model      = resource.model
    attributes = resource.dirty_attributes

    # TODO: make a model.identity_field method
    identity_field = model.key(repository.name).detect { |p| p.serial? }

    statement = create_statement(repository, model, attributes.keys, identity_field)
    bind_values = attributes.values

    result = execute(statement, *bind_values)

    if result.to_i == 1
      if identity_field
        identity_field.set!(resource, result.insert_id)
      end
      created += 1
    end
  end
  created
end

#delete(query) ⇒ Object



83
84
85
86
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 83

def delete(query)
  statement = delete_statement(query)
  execute(statement, *query.bind_values).to_i
end

#execute(statement, *bind_values) ⇒ Object

Database-specific method



89
90
91
92
93
94
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 89

def execute(statement, *bind_values)
  with_connection do |connection|
    command = connection.create_command(statement)
    command.execute_non_query(*bind_values)
  end
end

#query(statement, *bind_values) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 96

def query(statement, *bind_values)
  with_reader(statement, bind_values) do |reader|
    results = []

    if (fields = reader.fields).size > 1
      fields = fields.map { |field| Extlib::Inflection.underscore(field).to_sym }
      struct = Struct.new(*fields)

      while(reader.next!) do
        results << struct.new(*reader.values)
      end
    else
      while(reader.next!) do
        results << reader.values.at(0)
      end
    end

    results
  end
end

#read_many(query) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 38

def read_many(query)
  Collection.new(query) do |collection|
    with_connection do |connection|
      command = connection.create_command(read_statement(query))
      command.set_types(query.fields.map { |p| p.primitive })

      begin
        bind_values = query.bind_values.map do |v|
          v == [] ? [nil] : v
        end
        reader = command.execute_reader(*bind_values)

        while(reader.next!)
          collection.load(reader.values)
        end
      ensure
        reader.close if reader
      end
    end
  end
end

#read_one(query) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 60

def read_one(query)
  with_connection do |connection|
    command = connection.create_command(read_statement(query))
    command.set_types(query.fields.map { |p| p.primitive })

    begin
      reader = command.execute_reader(*query.bind_values)

      if reader.next!
        query.model.load(reader.values, query)
      end
    ensure
      reader.close if reader
    end
  end
end

#update(attributes, query) ⇒ Object



77
78
79
80
81
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 77

def update(attributes, query)
  statement = update_statement(attributes.keys, query)
  bind_values = attributes.values + query.bind_values
  execute(statement, *bind_values).to_i
end