Class: N::Db
- Inherits:
-
Object
- Object
- N::Db
- Defined in:
- lib/n/db.rb
Overview
Database
Design:
-
This is NOT a singleton, an application may access multiple databases.
Instance Attribute Summary collapse
-
#cache ⇒ Object
Entities cache.
-
#config ⇒ Object
hash of configuration options.
-
#fields ⇒ Object
Managed classes fields.
-
#pool ⇒ Object
Pool of connections to the backend.
Instance Method Summary collapse
-
#connect(deserialize = nil, &block) ⇒ Object
(also: #open)
Utility method, automatically restores a connection to the pool.
-
#entity_from_row(row, klass) ⇒ Object
Utility method, creates an entity from a row.
-
#get_connection ⇒ Object
Get a connection from the pool to access the database.
-
#initialize(config) ⇒ Db
constructor
Initialize the database interface.
-
#put_connection(connection) ⇒ Object
Restore an unused connection to the pool.
-
#shutdown ⇒ Object
(also: #close)
Shutdown the database interface.
-
#transaction(deserialize = nil, &block) ⇒ Object
Utility method, gets connection and encloses in transaction.
Constructor Details
#initialize(config) ⇒ Db
Initialize the database interface.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/n/db.rb', line 83 def initialize(config) @config = config if backend = config[:backend] require "n/db/#{backend}" else require "n/db/psql" end $log.info "Connecting to database '#{config[:database]}'." @pool = N::Pool.new @fields = N::SafeHash.new for i in (0...config[:connection_count]) @pool << DbConnection.new(config) end end |
Instance Attribute Details
#cache ⇒ Object
Entities cache.
76 77 78 |
# File 'lib/n/db.rb', line 76 def cache @cache end |
#config ⇒ Object
hash of configuration options.
70 71 72 |
# File 'lib/n/db.rb', line 70 def config @config end |
#fields ⇒ Object
Managed classes fields. A hash of hashes.
79 80 81 |
# File 'lib/n/db.rb', line 79 def fields @fields end |
#pool ⇒ Object
Pool of connections to the backend.
73 74 75 |
# File 'lib/n/db.rb', line 73 def pool @pool end |
Instance Method Details
#connect(deserialize = nil, &block) ⇒ Object Also known as: open
Utility method, automatically restores a connection to the pool.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/n/db.rb', line 125 def connect(deserialize = nil, &block) result = nil begin db = @pool.pop() db.deserialize = deserialize unless deserialize.nil? result = yield(db) db.deserialize = true ensure @pool.push(db) end return result end |
#entity_from_row(row, klass) ⇒ Object
Utility method, creates an entity from a row
166 167 168 169 170 |
# File 'lib/n/db.rb', line 166 def entity_from_row(row, klass) entity = klass.new() entity.__db_read_row__(row) return entity end |
#get_connection ⇒ Object
Get a connection from the pool to access the database.
113 114 115 |
# File 'lib/n/db.rb', line 113 def get_connection return @pool.pop() end |
#put_connection(connection) ⇒ Object
Restore an unused connection to the pool.
119 120 121 |
# File 'lib/n/db.rb', line 119 def put_connection(connection) return @pool.push(connection) end |
#shutdown ⇒ Object Also known as: close
Shutdown the database interface.
104 105 106 107 108 |
# File 'lib/n/db.rb', line 104 def shutdown for con in @pool con.close() end end |
#transaction(deserialize = nil, &block) ⇒ Object
Utility method, gets connection and encloses in transaction.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/n/db.rb', line 145 def transaction(deserialize = nil, &block) result = nil begin db = @pool.pop() db.deserialize = deserialize if deserialize db.start() result = yield(db) db.commit() db.deserialize = true ensure @pool.push(db) end return result end |