Class: DataMapper::Adapters::DataObjectsAdapter

Inherits:
AbstractAdapter
  • Object
show all
Extended by:
Chainable, Deprecate
Includes:
SQL
Defined in:
lib/dm-do-adapter/adapter.rb

Overview

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.

Defined Under Namespace

Modules: SQL

Constant Summary collapse

SQL_FALSE =
'1 = 0'.freeze

Constants included from SQL

SQL::IDENTIFIER_MAX_LENGTH

Instance Method Summary collapse

Methods included from SQL

#property_to_column_name

Instance Method Details

#create(resources) ⇒ Integer

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 the identity map. If a plugin needs to use an adapter directly, it is up to plugin developer to keep the identity map up to date.


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

def create(resources)
  name = self.name

  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 = with_connection do |connection|
      connection.create_command(statement).execute_non_query(*bind_values)
    end

    if result.affected_rows == 1 && serial
      serial.set!(resource, result.insert_id)
    end
  end
end

#delete(collection) ⇒ Integer

Constructs and executes DELETE statement for given query


209
210
211
212
213
214
215
216
# File 'lib/dm-do-adapter/adapter.rb', line 209

def delete(collection)
  query = collection.query
  statement, bind_values = delete_statement(query)

  with_connection do |connection|
    connection.create_command(statement).execute_non_query(*bind_values)
  end.affected_rows
end

#execute(statement, *bind_values) ⇒ DataObjects::Result

Execute non-SELECT SQL query


62
63
64
65
66
67
# File 'lib/dm-do-adapter/adapter.rb', line 62

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

#read(query) ⇒ Array

Constructs and executes SELECT query, then instantiates one or many object from result set.


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

def read(query)
  fields = query.fields
  types  = fields.map { |property| property.dump_class }

  statement, bind_values = select_statement(query)

  records = []

  with_connection do |connection|
    command = connection.create_command(statement)
    command.set_types(types)

    # Handle different splat semantics for nil on 1.8 and 1.9
    reader = if bind_values
      command.execute_reader(*bind_values)
    else
      command.execute_reader
    end

    begin
      while reader.next!
        records << Hash[ fields.zip(reader.values) ]
      end
    ensure
      reader.close
    end
  end

  records
end

#select(statement, *bind_values) ⇒ Array

Retrieve results using an SQL SELECT statement


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dm-do-adapter/adapter.rb', line 34

def select(statement, *bind_values)
  with_connection do |connection|
    reader = connection.create_command(statement).execute_reader(*bind_values)
    fields = reader.fields

    begin
      if fields.size > 1
        select_fields(reader, fields)
      else
        select_field(reader)
      end
    ensure
      reader.close
    end
  end
end

#update(attributes, collection) ⇒ Integer

Constructs and executes UPDATE statement for given attributes and a query


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/dm-do-adapter/adapter.rb', line 178

def update(attributes, collection)
  query = collection.query

  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)

  with_connection do |connection|
    connection.create_command(statement).execute_non_query(*bind_values)
  end.affected_rows
end