Class: DataMapper::Adapters::MysqlAdapter
- Inherits:
-
SqlAdapter
- Object
- AbstractAdapter
- SqlAdapter
- DataMapper::Adapters::MysqlAdapter
- Defined in:
- lib/data_mapper/adapters/mysql_adapter.rb
Defined Under Namespace
Modules: Commands
Constant Summary collapse
- TABLE_QUOTING_CHARACTER =
'`'.freeze
- COLUMN_QUOTING_CHARACTER =
'`'.freeze
Constants inherited from SqlAdapter
SqlAdapter::FIND_OPTIONS, SqlAdapter::TYPES
Instance Method Summary collapse
-
#connection ⇒ Object
Yields an available connection.
- #execute(*args) ⇒ Object
-
#initialize(configuration) ⇒ MysqlAdapter
constructor
A new instance of MysqlAdapter.
- #query(*args) ⇒ Object
Methods inherited from SqlAdapter
#[], #delete, #escape_sql, inherited, #load, #save, #schema, #single_threaded?, #table_exists?, #transaction
Methods inherited from AbstractAdapter
Constructor Details
#initialize(configuration) ⇒ MysqlAdapter
Returns a new instance of MysqlAdapter.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/data_mapper/adapters/mysql_adapter.rb', line 21 def initialize(configuration) super create_connection = lambda do Mysql.new(configuration.host, configuration.username, configuration.password, configuration.database) end # Initialize the connection pool. if single_threaded? @connection_factory = create_connection @active_connection = create_connection[] else @connections = Support::ConnectionPool.new(&create_connection) end end |
Instance Method Details
#connection ⇒ Object
Yields an available connection. Flushes the connection-pool if the connection returns an error.
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 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/data_mapper/adapters/mysql_adapter.rb', line 39 def connection if single_threaded? begin # BUG: Single_threaded mode totally breaks shit right now. No real idea why just from # eyeballing this. Probably should move this into the SqlAdapter anyways and just # force derived adapters to implement a #create_connection() and #close_connection(conn) methods. yield(@active_connection) rescue Mysql::Error => me @configuration.log.fatal(me) begin @active_connection.close rescue => se @configuration.log.error(se) end @active_connection = @connection_factory[] end else begin @connections.hold { |dbh| yield(dbh) } rescue Mysql::Error => me @configuration.log.fatal(me) @connections.available_connections.each do |sock| begin sock.close rescue => se @configuration.log.error(se) end end @connections.available_connections.clear raise me end end end |
#execute(*args) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/data_mapper/adapters/mysql_adapter.rb', line 79 def execute(*args) connection do |db| reader = db.query(escape_sql(*args)) result = yield(reader, reader.fetch_fields.map { |field| field.name }) reader.free result end end |
#query(*args) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/data_mapper/adapters/mysql_adapter.rb', line 88 def query(*args) execute(*args) do |reader, fields| struct = Support::Struct::define(fields) results = [] reader.each do |row| results << struct.new(row) end results end end |