Class: Lafcadio::DbBridge
- Inherits:
-
Object
- Object
- Lafcadio::DbBridge
- Defined in:
- lib/lafcadio/objectStore/DbBridge.rb
Overview
:nodoc:
Constant Summary collapse
- @@dbh =
nil
- @@lastPkIdInserted =
nil
- @@dbName =
nil
- @@connectionClass =
DBI
Class Method Summary collapse
- ._load(aString) ⇒ Object
- .disconnect ⇒ Object
- .flushConnection ⇒ Object
- .setConnectionClass(aClass) ⇒ Object
- .setDbName(dbName) ⇒ Object
Instance Method Summary collapse
- #_dump(aDepth) ⇒ Object
- #commit(dbObject) ⇒ Object
- #executeCommit(sql, binds) ⇒ Object
- #executeSelect(sql) ⇒ Object
- #getCollectionByQuery(query) ⇒ Object
- #group_query(query) ⇒ Object
-
#initialize(dbh = nil) ⇒ DbBridge
constructor
A new instance of DbBridge.
- #lastPkIdInserted ⇒ Object
- #maybeLog(sql) ⇒ Object
Constructor Details
#initialize(dbh = nil) ⇒ DbBridge
Returns a new instance of DbBridge.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 41 def initialize(dbh = nil) if dbh == nil if @@dbh == nil config = LafcadioConfig.new dbName = @@dbName || config['dbname'] dbAndHost = nil if dbName && config['dbhost'] dbAndHost = "dbi:Mysql:#{ dbName }:#{ config['dbhost'] }" else dbAndHost = "dbi:#{config['dbconn']}" end @@dbh = @@connectionClass.connect( dbAndHost, config['dbuser'], config['dbpassword'] ) end else @@dbh = dbh end @dbh = @@dbh ObjectSpace.define_finalizer( self, proc { |id| DbBridge.disconnect } ) end |
Class Method Details
._load(aString) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 18 def DbBridge._load(aString) aString =~ /dbh:/ dbString = $' begin dbh = Marshal.load(dbString) rescue TypeError dbh = nil end new dbh end |
.disconnect ⇒ Object
37 38 39 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 37 def DbBridge.disconnect @@dbh.disconnect if @@dbh end |
.flushConnection ⇒ Object
29 30 31 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 29 def DbBridge.flushConnection @@dbh = nil end |
.setConnectionClass(aClass) ⇒ Object
33 34 35 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 33 def DbBridge.setConnectionClass( aClass ) @@connectionClass = aClass end |
.setDbName(dbName) ⇒ Object
14 15 16 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 14 def DbBridge.setDbName(dbName) @@dbName = dbName end |
Instance Method Details
#_dump(aDepth) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 113 def _dump(aDepth) if @db.respond_to? '_dump' dbDump = @db._dump else dbDump = @db.class.to_s end "dbh:#{dbDump}" end |
#commit(dbObject) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 74 def commit(dbObject) require 'lafcadio/objectStore/DomainObjectSqlMaker' sqlMaker = DomainObjectSqlMaker.new(dbObject) sqlMaker.sqlStatements.each { |sql, binds| executeCommit( sql, binds ) } if sqlMaker.sqlStatements[0].first =~ /insert/ sql = 'select last_insert_id()' result = executeSelect( sql ) @@lastPkIdInserted = result[0]['last_insert_id()'].to_i end end |
#executeCommit(sql, binds) ⇒ Object
85 86 87 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 85 def executeCommit( sql, binds ) @dbh.do( sql, *binds ) end |
#executeSelect(sql) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 104 def executeSelect(sql) maybeLog sql begin @dbh.select_all( sql ) rescue DBI::DatabaseError => e raise $!.to_s + ": #{ e.errstr }" end end |
#getCollectionByQuery(query) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 89 def getCollectionByQuery(query) require 'lafcadio/objectStore/SqlValueConverter' objectType = query.objectType coll = [] objects = [] result = executeSelect query.toSql result.each { |row_hash| converter = SqlValueConverter.new(objectType, row_hash) obj = objectType.new converter objects << obj } coll = coll.concat objects coll end |
#group_query(query) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 126 def group_query( query ) row = executeSelect( query.toSql )[0] result = [] row.each { |val| if query.field_name != query.objectType.sqlPrimaryKeyName a_field = query.objectType.getField( query.field_name ) result << a_field.valueFromSQL( val ) else result << val.to_i end } result end |
#lastPkIdInserted ⇒ Object
122 123 124 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 122 def lastPkIdInserted @@lastPkIdInserted end |
#maybeLog(sql) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/lafcadio/objectStore/DbBridge.rb', line 62 def maybeLog(sql) config = LafcadioConfig.new if config['logSql'] == 'y' sqllog = Log4r::Logger['sql'] || Log4r::Logger.new( 'sql' ) filename = File.join( config['logdir'], config['sqlLogFile'] || 'sql' ) outputter = Log4r::FileOutputter.new( 'outputter', { :filename => filename } ) sqllog.outputters = outputter sqllog.info sql end end |