Class: Swift::Adapter
- Inherits:
-
Object
- Object
- Swift::Adapter
- Defined in:
- lib/swift/adapter.rb,
lib/swift/synchrony.rb,
lib/swift/adapter/sql.rb,
lib/swift/eventmachine.rb,
lib/swift/identity_map.rb,
lib/swift/adapter/mysql.rb,
lib/swift/adapter/sqlite3.rb,
lib/swift/adapter/postgres.rb
Overview
IdentityMap
Direct Known Subclasses
Defined Under Namespace
Classes: EMHandler, Mysql, Postgres, Sql, Sqlite3
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
-
#aexecute ⇒ Swift::Result, ...
Execute a command using the underlying concrete adapter.
-
#blocking_execute {|res| ... } ⇒ Object
Execute a command asynchronously and pause the Fiber until the command finishes.
-
#create(record, resources) ⇒ Swift::Record+
Create one or more.
-
#delete(record, resources) ⇒ Swift::Record+
Delete one or more.
-
#execute(command, *bind) ⇒ Object
Execute a command asynchronously.
-
#get(record, keys) ⇒ Swift::Record?
Select by id(s).
- #identity_map ⇒ Object
-
#initialize(db) ⇒ Adapter
constructor
A new instance of Adapter.
-
#log_command(start, command, bind) ⇒ Object
:nodoc:.
- #pending ⇒ Object
-
#prepare(record = nil, command) ⇒ Swift::Statement, ...
Create a server side prepared statement.
-
#trace(io = $stdout) ⇒ Object
Trace commands being executed.
-
#trace? ⇒ TrueClass, FalseClass
Check if the adapter commands are being traced.
-
#update(record, resources) ⇒ Swift::Record, Swift::Result
Update one or more.
Constructor Details
#initialize(db) ⇒ Adapter
Returns a new instance of Adapter.
13 14 15 |
# File 'lib/swift/adapter.rb', line 13 def initialize db @db = db end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
11 12 13 |
# File 'lib/swift/adapter.rb', line 11 def db @db end |
Instance Method Details
#execute(record, command, *bind) ⇒ Swift::Result, ... #execute(command, *bind) ⇒ Swift::Result, ...
Execute a command using the underlying concrete adapter.
10 11 12 13 14 15 16 |
# File 'lib/swift/synchrony.rb', line 10 def execute command, *bind start = Time.now record, command = command, bind.shift if command.kind_of?(Class) && command < Record record ? Result.new(record, db.execute(command, *bind)) : db.execute(command, *bind) ensure log_command(start, command, bind) if @trace end |
#blocking_execute {|res| ... } ⇒ Object
Execute a command asynchronously and pause the Fiber until the command finishes.
9 10 11 12 13 14 15 |
# File 'lib/swift/eventmachine.rb', line 9 def execute command, *bind start = Time.now record, command = command, bind.shift if command.kind_of?(Class) && command < Record record ? Result.new(record, db.execute(command, *bind)) : db.execute(command, *bind) ensure log_command(start, command, bind) if @trace end |
#create(record, resources) ⇒ Swift::Record+
Hashes will be coerced into a Swift::Record resource via Swift::Record#new
Passing a scalar will result in a scalar.
Create one or more.
59 60 61 62 63 64 65 66 67 |
# File 'lib/swift/adapter.rb', line 59 def create record, resources result = [resources].flatten.map do |resource| resource = record.new(resource) unless resource.kind_of?(record) result = execute(command_create(record), *resource.tuple.values_at(*record.header.insertable)) resource.tuple[record.header.serial] = result.insert_id if record.header.serial resource end resources.kind_of?(Array) ? result : result.first end |
#delete(record, resources) ⇒ Swift::Record+
Hashes will be coerced into a Swift::Record resource via Swift::Record#new
Passing a scalar will result in a scalar.
Delete one or more.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/swift/adapter.rb', line 138 def delete record, resources result = [resources].flatten.map do |resource| resource = record.new(resource) unless resource.kind_of?(record) keys = resource.tuple.values_at(*record.header.keys) # TODO: Name the key field(s) missing. raise ArgumentError, "#{record} resource has incomplete key: #{resource.inspect}" \ unless keys.select(&:nil?).empty? if result = execute(command_delete(record), *keys) resource.freeze end result end resources.kind_of?(Array) ? result : result.first end |
#execute(command, *bind) ⇒ Object
Execute a command asynchronously.
214 215 216 217 218 219 220 |
# File 'lib/swift/adapter.rb', line 214 def execute command, *bind start = Time.now record, command = command, bind.shift if command.kind_of?(Class) && command < Record record ? Result.new(record, db.execute(command, *bind)) : db.execute(command, *bind) ensure log_command(start, command, bind) if @trace end |
#get(record, keys) ⇒ Swift::Record?
Select by id(s).
– NOTE: Not significantly shorter than Record.db.first(User, ‘id = ?’, 12)
30 31 32 33 |
# File 'lib/swift/adapter.rb', line 30 def get record, keys resource = record.new(keys) execute(record, command_get(record), *resource.tuple.values_at(*record.header.keys)).first end |
#identity_map ⇒ Object
31 32 33 |
# File 'lib/swift/identity_map.rb', line 31 def identity_map @identity_map ||= IdentityMap.new end |
#log_command(start, command, bind) ⇒ Object
:nodoc:
223 224 225 226 227 |
# File 'lib/swift/adapter.rb', line 223 def log_command start, command, bind @trace.print Time.now.strftime('%F %T.%N'), ' - %.9f' % (Time.now - start).to_f, ' - ', command @trace.print ' ', bind if bind && bind.size > 0 @trace.print $/ end |
#pending ⇒ Object
57 58 59 |
# File 'lib/swift/eventmachine.rb', line 57 def pending @pending ||= [] end |
#prepare(record, command) ⇒ Swift::Statement, ... #prepare(command) ⇒ Swift::Statement, ...
Create a server side prepared statement
169 170 171 |
# File 'lib/swift/adapter.rb', line 169 def prepare record = nil, command record ? Statement.new(record, command) : db.prepare(command) end |
#trace(io = $stdout) ⇒ Object
Trace commands being executed.
184 185 186 187 188 189 |
# File 'lib/swift/adapter.rb', line 184 def trace io = $stdout @trace = io result = yield @trace = false result end |
#trace? ⇒ TrueClass, FalseClass
Check if the adapter commands are being traced.
194 195 196 |
# File 'lib/swift/adapter.rb', line 194 def trace? !!@trace end |
#update(record, resources) ⇒ Swift::Record, Swift::Result
Hashes will be coerced into a Swift::Record resource via Swift::Record#new
Passing a scalar will result in a scalar.
Update one or more.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/swift/adapter.rb', line 98 def update record, resources result = [resources].flatten.map do |resource| resource = record.new(resource) unless resource.kind_of?(record) keys = resource.tuple.values_at(*record.header.keys) # TODO: Name the key field(s) missing. raise ArgumentError, "#{record} resource has incomplete key: #{resource.inspect}" \ unless keys.select(&:nil?).empty? execute(command_update(record), *resource.tuple.values_at(*record.header.updatable), *keys) resource end resources.kind_of?(Array) ? result : result.first end |