Class: Specify::Database

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

Overview

A class that represents a Specify database.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, host: 'localhost', port: 3306, user: 'root', password: nil) {|_self| ... } ⇒ Database

Creates a new instance. database: the name of the MySQL database to connect to host: the host name or IP user: the MySQL user password: the password

Yields:

  • (_self)

Yield Parameters:


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/specify/database.rb', line 14

def initialize(database,
               host: 'localhost',
               port: 3306,
               user: 'root',
               password: nil)
  @database = database
  @host = host
  @port = port
  @user = user
  @password = password || prompt
  @connection = nil
  @sessions = []
  ObjectSpace.define_finalizer(self, method(:close))
  yield(self) if block_given?
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection


6
7
8
# File 'lib/specify/database.rb', line 6

def connection
  @connection
end

#databaseObject (readonly)

Returns the value of attribute database


7
8
9
# File 'lib/specify/database.rb', line 7

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host


7
8
9
# File 'lib/specify/database.rb', line 7

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port


7
8
9
# File 'lib/specify/database.rb', line 7

def port
  @port
end

#sessionsObject (readonly)

Returns the value of attribute sessions


7
8
9
# File 'lib/specify/database.rb', line 7

def sessions
  @sessions
end

#userObject (readonly)

Returns the value of attribute user


7
8
9
# File 'lib/specify/database.rb', line 7

def user
  @user
end

Class Method Details

.load_config(host, database, config_file = nil) ⇒ Object

Creates a new instance from a config file. Default config file is '/usr/local/etc/sp_view_loader/db.yml' database: the name of the database config_file: the path to the file


34
35
36
37
# File 'lib/specify/database.rb', line 34

def self.load_config(host, database, config_file = nil)
  config = Configuration::DBConfig.new(host, database, config_file)
  new(database, config.connection)
end

Instance Method Details

#<<(session) ⇒ Object

Adds a new Session to the sessions pool.


40
41
42
43
44
# File 'lib/specify/database.rb', line 40

def <<(session)
  session.open
  session.add_observer self
  sessions << session
end

#closeObject

Closes all sessions. FIXME: should probably also close database connection


48
49
50
51
52
53
54
55
# File 'lib/specify/database.rb', line 48

def close
  return if sessions.empty?
  sessions.each do |session|
    session.close
    session.delete_observer self
  end
  # connection.disconnect
end

#connectObject

Returns the Sequel::Database object for the database. Establishes a connection and creates the object if it does not already exist.


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/specify/database.rb', line 59

def connect
  return connection if connection
  @connection = Sequel.connect adapter: :mysql2,
                               user: @user,
                               password: @password,
                               host: @host,
                               port: @port,
                               database: @database
  require_relative 'models'
  connection
end

#inspectObject

Returns a string containing a human-readable representation of Database.


72
73
74
75
# File 'lib/specify/database.rb', line 72

def inspect
  "#{self} database: #{@database}, host: #{@host}, port: #{@port}"\
  ", user: #{@user}, connected: #{connection ? true : false}"
end

#start_session(user, collection) ⇒ Object


82
83
84
85
86
87
# File 'lib/specify/database.rb', line 82

def start_session(user, collection)
  connect
  session = Session.new user, collection
  self << session
  session
end

#update(session) ⇒ Object

Deletes a closed session from the sessions pool


78
79
80
# File 'lib/specify/database.rb', line 78

def update(session)
  sessions.delete session
end