Class: DataMapper::Adapters::DataObjectsAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::DataObjectsAdapter
- 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
Constants included from SQL
SQL::AND_KEYWORD, SQL::BETWEEN_OPERATOR, SQL::COLUMN_SEPARATOR, SQL::DOUBLE_QUOTE, SQL::EQ_OPERATOR, SQL::GTE_OPERATOR, SQL::GT_OPERATOR, SQL::IDENTIFIER_MAX_LENGTH, SQL::IN_OPERATOR, SQL::IS_OPERATOR, SQL::LIKE_OPERATOR, SQL::LTE_OPERATOR, SQL::LT_OPERATOR, SQL::NULL_KEYWORD, SQL::ONE_EQ_ZERO, SQL::ON_KEYWORD, SQL::QUESTION_MARK, SQL::REGEXP_OPERATOR, SQL::SINGLE_QUOTE, SQL::SPACE
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) ⇒ DataObjects::Result
Execute non-SELECT SQL query.
-
#read(query) ⇒ Array
Constructs and executes SELECT query, then instantiates one or many object from result set.
-
#select(statement, *bind_values) ⇒ Array
Retrieve results using an SQL SELECT statement.
-
#update(attributes, collection) ⇒ Integer
Constructs and executes UPDATE statement for given attributes and a query.
Methods included from SQL
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.
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 |
# File 'lib/dm-do-adapter/adapter.rb', line 82 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
207 208 209 210 211 212 213 214 |
# File 'lib/dm-do-adapter/adapter.rb', line 207 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
60 61 62 63 64 65 |
# File 'lib/dm-do-adapter/adapter.rb', line 60 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.
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 |
# File 'lib/dm-do-adapter/adapter.rb', line 133 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) # 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dm-do-adapter/adapter.rb', line 32 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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/dm-do-adapter/adapter.rb', line 176 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 |