Class: DataMapper::Adapters::DataObjectsAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::DataObjectsAdapter
- Extended by:
- Chainable
- Includes:
- SQL
- Defined in:
- lib/dm-core/adapters/data_objects_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.
Direct Known Subclasses
MysqlAdapter, OracleAdapter, PostgresAdapter, Sqlite3Adapter
Defined Under Namespace
Modules: SQL
Constant Summary
Constants included from SQL
Instance Attribute Summary
Attributes inherited from AbstractAdapter
#field_naming_convention, #name, #options, #resource_naming_convention
Instance Method Summary collapse
-
#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.
-
#delete(collection) ⇒ Integer
Constructs and executes DELETE statement for given query.
-
#execute(statement, *bind_values) ⇒ Object
Database-specific method TODO: document.
-
#query(statement, *bind_values) ⇒ Object
TODO: document.
-
#read(query) ⇒ Array
Constructs and executes SELECT query, then instantiates one or many object from result set.
-
#update(attributes, collection) ⇒ Integer
Constructs and executes UPDATE statement for given attributes and a query.
Methods included from Chainable
Methods included from SQL
Methods included from Equalizer
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 identity map. A plugin needs to use adapter directly, it is up to plugin developer to keep identity map up to date.
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 |
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 30 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.to_i == 1 && serial serial.set!(resource, result.insert_id) end end end |
#delete(collection) ⇒ Integer
Constructs and executes DELETE statement for given query
146 147 148 149 150 151 152 153 154 |
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 146 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).to_i end |
#execute(statement, *bind_values) ⇒ Object
Database-specific method TODO: document
159 160 161 162 163 164 |
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 159 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
TODO: document
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 |
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 168 def query(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 |
#read(query) ⇒ Array
Constructs and executes SELECT query, then instantiates one or many object from result set.
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 |
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 76 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 |
#update(attributes, collection) ⇒ Integer
Constructs and executes UPDATE statement for given attributes and a query
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/dm-core/adapters/data_objects_adapter.rb', line 114 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).to_i end |