Class: DataMapper::Database
Overview
The Database class allows us to setup a default database for use throughout our applications or allows us to setup a collection of databases to use.
Example
To setup a default database
DataMapper::Database.setup({
:adapter => 'mysql'
:host => 'localhost'
:username => 'root'
:password => 'R00tPaswooooord'
:database => 'selecta_development'
})
To setup a named database
DataMapper::Database.setup(:second_database, {
:adapter => 'postgresql'
:host => 'localhost'
:username => 'second_user'
:password => 'second_password'
:database => 'second_database'
})
Working with multiple databases (see #DataMapper::database)
DataMapper.database(:second_database) do
...
end
DataMapper.database(:default) do
...
end
or even…
#The below variables still hold on to their database sessions.
#So no confusion happens when passing variables around scopes.
DataMapper.database(:second_database) do
animal = Animal.first
DataMapper.database(:default) do
Animal.new(animal).save
end # :default database
end # :second_database
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#database ⇒ Object
Returns the value of attribute database.
-
#host ⇒ Object
Returns the value of attribute host.
-
#index_path ⇒ Object
Returns the value of attribute index_path.
-
#log_level ⇒ Object
Returns the value of attribute log_level.
-
#log_stream ⇒ Object
Returns the value of attribute log_stream.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#password ⇒ Object
Returns the value of attribute password.
-
#schema_search_path ⇒ Object
Returns the value of attribute schema_search_path.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
-
.[](name) ⇒ Object
Allows you to access any of the named databases you have already setup.
-
.context ⇒ Object
Returns the array of Database sessions currently being used.
-
.setup(*args) ⇒ Object
Setup creates a database and sets all of your properties for that database.
Instance Method Summary collapse
- #create_logger ⇒ Object
-
#initialize(name) ⇒ Database
constructor
Creates a new database object with the name you specify, and a default set of options.
-
#logger ⇒ Object
Default Logger from Ruby’s logger.rb.
Constructor Details
#initialize(name) ⇒ Database
Creates a new database object with the name you specify, and a default set of options.
The default options are as follows:
{ :host => 'localhost', :database => nil, :username => 'root', :password => '', :adapter = nil }
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/data_mapper/database.rb', line 162 def initialize(name) @name = name @adapter = nil @host = 'localhost' @database = nil @schema_search_path = nil @username = 'root' @password = '' @socket = nil @log_level = Logger::WARN @log_stream = nil end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
177 178 179 |
# File 'lib/data_mapper/database.rb', line 177 def adapter @adapter end |
#database ⇒ Object
Returns the value of attribute database.
179 180 181 |
# File 'lib/data_mapper/database.rb', line 179 def database @database end |
#host ⇒ Object
Returns the value of attribute host.
179 180 181 |
# File 'lib/data_mapper/database.rb', line 179 def host @host end |
#index_path ⇒ Object
Returns the value of attribute index_path.
179 180 181 |
# File 'lib/data_mapper/database.rb', line 179 def index_path @index_path end |
#log_level ⇒ Object
Returns the value of attribute log_level.
179 180 181 |
# File 'lib/data_mapper/database.rb', line 179 def log_level @log_level end |
#log_stream ⇒ Object
Returns the value of attribute log_stream.
177 178 179 |
# File 'lib/data_mapper/database.rb', line 177 def log_stream @log_stream end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
177 178 179 |
# File 'lib/data_mapper/database.rb', line 177 def name @name end |
#password ⇒ Object
Returns the value of attribute password.
179 180 181 |
# File 'lib/data_mapper/database.rb', line 179 def password @password end |
#schema_search_path ⇒ Object
Returns the value of attribute schema_search_path.
179 180 181 |
# File 'lib/data_mapper/database.rb', line 179 def schema_search_path @schema_search_path end |
#socket ⇒ Object
Returns the value of attribute socket.
179 180 181 |
# File 'lib/data_mapper/database.rb', line 179 def socket @socket end |
#username ⇒ Object
Returns the value of attribute username.
179 180 181 |
# File 'lib/data_mapper/database.rb', line 179 def username @username end |
Class Method Details
.[](name) ⇒ Object
Allows you to access any of the named databases you have already setup.
default_db = DataMapper::Database[:default]
second_db = DataMapper::Database[:second_database]
97 98 99 |
# File 'lib/data_mapper/database.rb', line 97 def self.[](name) @databases[name] end |
.context ⇒ Object
Returns the array of Database sessions currently being used
This is what gives us thread safety, boys and girls
104 105 106 |
# File 'lib/data_mapper/database.rb', line 104 def self.context Thread::current[:context] || Thread::current[:context] = [] end |
.setup(*args) ⇒ Object
Setup creates a database and sets all of your properties for that database. Setup looks for either a hash of options passed in to the database or a symbolized name for your database, as well as it’s hash of parameters
If no options are passed, an ArgumentException will be raised.
DataMapper::Database.setup(name = :default, options_hash)
DataMapper::Database.setup({
:adapter => 'mysql'
:host => 'localhost'
:username => 'root'
:password => 'R00tPaswooooord'
:database => 'selecta_development'
})
DataMapper::Database.setup(:named_database, {
:adapter => 'mysql'
:host => 'localhost'
:username => 'root'
:password => 'R00tPaswooooord'
:database => 'selecta_development'
})
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/data_mapper/database.rb', line 133 def self.setup(*args) name, = nil if (args.nil?) || (args[1].nil? && args[0].class != Hash) raise ArgumentError.new('Database cannot be setup without at least an options hash.') end if args.size == 1 name, = :default, args[0] elsif args.size == 2 name, = args[0], args[1] end current = self.new(name) current.single_threaded = false if [:single_threaded] == false .each_pair do |k,v| current.send("#{k}=", v) end @databases[name] = current end |
Instance Method Details
#create_logger ⇒ Object
225 226 227 228 229 230 |
# File 'lib/data_mapper/database.rb', line 225 def create_logger x = Logger.new(@log_stream, File::WRONLY | File::APPEND | File::CREAT) x.level = @log_level at_exit { x.close } return x end |
#logger ⇒ Object
Default Logger from Ruby’s logger.rb
215 216 217 218 219 220 221 222 223 |
# File 'lib/data_mapper/database.rb', line 215 def logger @logger = create_logger class << self attr_reader :logger end return @logger end |