Class: DataMapper::Database

Inherits:
Object show all
Defined in:
lib/data_mapper/database.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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

#adapterObject

Returns the value of attribute adapter.



177
178
179
# File 'lib/data_mapper/database.rb', line 177

def adapter
  @adapter
end

#databaseObject

Returns the value of attribute database.



179
180
181
# File 'lib/data_mapper/database.rb', line 179

def database
  @database
end

#hostObject

Returns the value of attribute host.



179
180
181
# File 'lib/data_mapper/database.rb', line 179

def host
  @host
end

#index_pathObject

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_levelObject

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_streamObject

Returns the value of attribute log_stream.



177
178
179
# File 'lib/data_mapper/database.rb', line 177

def log_stream
  @log_stream
end

#nameObject (readonly)

Returns the value of attribute name.



177
178
179
# File 'lib/data_mapper/database.rb', line 177

def name
  @name
end

#passwordObject

Returns the value of attribute password.



179
180
181
# File 'lib/data_mapper/database.rb', line 179

def password
  @password
end

#schema_search_pathObject

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

#socketObject

Returns the value of attribute socket.



179
180
181
# File 'lib/data_mapper/database.rb', line 179

def socket
  @socket
end

#usernameObject

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

.contextObject

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, options = 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, options = :default, args[0]
  elsif args.size == 2
    name, options = args[0], args[1]
  end        
  
  current = self.new(name)
  
  current.single_threaded = false if options[:single_threaded] == false
  
  options.each_pair do |k,v|
    current.send("#{k}=", v)
  end
  
  @databases[name] = current
end

Instance Method Details

#create_loggerObject



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

#loggerObject

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