Class DataMapper::Database
In: lib/data_mapper/database.rb
lib/data_mapper/database.rb
Parent: Object

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

Methods

[]   []   adapter=   adapter=   context   context   create_logger   create_logger   log_stream=   log_stream=   logger   logger   new   new   setup   setup  

Attributes

adapter  [R] 
adapter  [R] 
database  [RW] 
database  [RW] 
host  [RW] 
host  [RW] 
index_path  [RW] 
index_path  [RW] 
log_level  [RW] 
log_level  [RW] 
log_stream  [R] 
log_stream  [R] 
logger  [R] 
logger  [R] 
name  [R] 
name  [R] 
password  [RW] 
password  [RW] 
port  [RW] 
port  [RW] 
schema_search_path  [RW] 
schema_search_path  [RW] 
socket  [RW] 
socket  [RW] 
username  [RW] 
username  [RW] 

Public Class methods

Allows you to access any of the named databases you have already setup.

  default_db = DataMapper::Database[:default]
  second_db = DataMapper::Database[:second_database]

[Source]

    # File lib/data_mapper/database.rb, line 97
97:     def self.[](name)
98:       @databases[name]
99:     end

Allows you to access any of the named databases you have already setup.

  default_db = DataMapper::Database[:default]
  second_db = DataMapper::Database[:second_database]

[Source]

    # File lib/data_mapper/database.rb, line 97
97:     def self.[](name)
98:       @databases[name]
99:     end

Returns the array of Database sessions currently being used

This is what gives us thread safety, boys and girls

[Source]

     # File lib/data_mapper/database.rb, line 104
104:     def self.context
105:       Thread::current[:database_contexts] || Thread::current[:database_contexts] = []
106:     end

Returns the array of Database sessions currently being used

This is what gives us thread safety, boys and girls

[Source]

     # File lib/data_mapper/database.rb, line 104
104:     def self.context
105:       Thread::current[:database_contexts] || Thread::current[:database_contexts] = []
106:     end

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, :port => nil, :username => 'root', :password => '', :adapter = nil }

[Source]

     # File lib/data_mapper/database.rb, line 162
162:     def initialize(name)
163:       @name = name
164:       
165:       @adapter = nil
166:       @host = 'localhost'
167:       @database = nil
168:       @port = nil
169:       @schema_search_path = nil
170:       @username = 'root'
171:       @password = ''
172:       @socket = nil
173:       
174:       @log_level = Logger::WARN
175:       @log_stream = nil
176:     end

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, :port => nil, :username => 'root', :password => '', :adapter = nil }

[Source]

     # File lib/data_mapper/database.rb, line 162
162:     def initialize(name)
163:       @name = name
164:       
165:       @adapter = nil
166:       @host = 'localhost'
167:       @database = nil
168:       @port = nil
169:       @schema_search_path = nil
170:       @username = 'root'
171:       @password = ''
172:       @socket = nil
173:       
174:       @log_level = Logger::WARN
175:       @log_stream = nil
176:     end

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'
  })

[Source]

     # File lib/data_mapper/database.rb, line 133
133:     def self.setup(*args)
134:       
135:       name, options = nil
136:       
137:       if (args.nil?) || (args[1].nil? && args[0].class != Hash)
138:         raise ArgumentError.new('Database cannot be setup without at least an options hash.')
139:       end
140:       
141:       if args.size == 1
142:         name, options = :default, args[0]
143:       elsif args.size == 2
144:         name, options = args[0], args[1]
145:       end        
146:       
147:       current = self.new(name)
148:       
149:       current.single_threaded = false if options[:single_threaded] == false
150:       
151:       options.each_pair do |k,v|
152:         current.send("#{k}=", v)
153:       end
154:       
155:       @databases[name] = current
156:     end

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'
  })

[Source]

     # File lib/data_mapper/database.rb, line 133
133:     def self.setup(*args)
134:       
135:       name, options = nil
136:       
137:       if (args.nil?) || (args[1].nil? && args[0].class != Hash)
138:         raise ArgumentError.new('Database cannot be setup without at least an options hash.')
139:       end
140:       
141:       if args.size == 1
142:         name, options = :default, args[0]
143:       elsif args.size == 2
144:         name, options = args[0], args[1]
145:       end        
146:       
147:       current = self.new(name)
148:       
149:       current.single_threaded = false if options[:single_threaded] == false
150:       
151:       options.each_pair do |k,v|
152:         current.send("#{k}=", v)
153:       end
154:       
155:       @databases[name] = current
156:     end

Public Instance methods

Allows us to set the adapter for this database object. It can only be set once, and expects two types of values.

You may pass in either a class inheriting from DataMapper::Adapters::AbstractAdapter or pass in a string indicating the type of adapter you would like to use.

To create your own adapters, create a file in data_mapper/adapters/new_adapter.rb that inherits from AbstractAdapter

  database.adapter=("postgresql")

[Source]

     # File lib/data_mapper/database.rb, line 194
194:     def adapter=(value)
195:       if @adapter
196:         raise ArgumentError.new('The adapter is readonly after being set')
197:       end
198:       
199:       if value.is_a?(DataMapper::Adapters::AbstractAdapter)
200:         @adapter = value
201:       elsif value.is_a?(Class)
202:         @adapter = value.new(self)
203:       else
204:         begin
205:           require "data_mapper/adapters/#{Inflector.underscore(value)}_adapter"
206:         rescue LoadError
207:           require "#{Inflector.underscore(value)}_adapter"
208:         end
209:         adapter_class = Adapters::const_get(Inflector.classify(value) + "Adapter")
210:       
211:         @adapter = adapter_class.new(self)
212:       end
213:     end

Allows us to set the adapter for this database object. It can only be set once, and expects two types of values.

You may pass in either a class inheriting from DataMapper::Adapters::AbstractAdapter or pass in a string indicating the type of adapter you would like to use.

To create your own adapters, create a file in data_mapper/adapters/new_adapter.rb that inherits from AbstractAdapter

  database.adapter=("postgresql")

[Source]

     # File lib/data_mapper/database.rb, line 194
194:     def adapter=(value)
195:       if @adapter
196:         raise ArgumentError.new('The adapter is readonly after being set')
197:       end
198:       
199:       if value.is_a?(DataMapper::Adapters::AbstractAdapter)
200:         @adapter = value
201:       elsif value.is_a?(Class)
202:         @adapter = value.new(self)
203:       else
204:         begin
205:           require "data_mapper/adapters/#{Inflector.underscore(value)}_adapter"
206:         rescue LoadError
207:           require "#{Inflector.underscore(value)}_adapter"
208:         end
209:         adapter_class = Adapters::const_get(Inflector.classify(value) + "Adapter")
210:       
211:         @adapter = adapter_class.new(self)
212:       end
213:     end

[Source]

     # File lib/data_mapper/database.rb, line 226
226:     def create_logger
227:       x = Logger.new(@log_stream, File::WRONLY | File::APPEND | File::CREAT)
228:       x.level = @log_level
229:       at_exit { x.close }
230:       return x
231:     end

[Source]

     # File lib/data_mapper/database.rb, line 226
226:     def create_logger
227:       x = Logger.new(@log_stream, File::WRONLY | File::APPEND | File::CREAT)
228:       x.level = @log_level
229:       at_exit { x.close }
230:       return x
231:     end

[Source]

     # File lib/data_mapper/database.rb, line 182
182:     def log_stream=(val)
183:       @log_stream = (val.is_a?(String) && val =~ /STDOUT/ ? STDOUT : val)
184:     end

[Source]

     # File lib/data_mapper/database.rb, line 182
182:     def log_stream=(val)
183:       @log_stream = (val.is_a?(String) && val =~ /STDOUT/ ? STDOUT : val)
184:     end

Default Logger from Ruby‘s logger.rb

[Source]

     # File lib/data_mapper/database.rb, line 216
216:     def logger
217:       @logger = create_logger
218:     
219:       class << self
220:         attr_reader :logger
221:       end
222:     
223:       return @logger
224:     end

Default Logger from Ruby‘s logger.rb

[Source]

     # File lib/data_mapper/database.rb, line 216
216:     def logger
217:       @logger = create_logger
218:     
219:       class << self
220:         attr_reader :logger
221:       end
222:     
223:       return @logger
224:     end

[Validate]