Class: Specify::Database

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

Overview

Databases represent 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

Returns a new Database for the database (the name of the database) on host.

port: the port for the MySQL/MariaDB server for the database.

user: the MySQL/MariaDB user (typically the Specify master user).

password: the password for the MySQL/MariaDB user.

Yields:

  • (_self)

Yield Parameters:



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

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

The Sequel::Database instance for the current connection.



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

def connection
  @connection
end

#databaseObject (readonly)

The name of the Specify database.



10
11
12
# File 'lib/specify/database.rb', line 10

def database
  @database
end

#hostObject (readonly)

The name of the MySQL/MariaDB host for the Specify database.



13
14
15
# File 'lib/specify/database.rb', line 13

def host
  @host
end

#portObject (readonly)

The port for the MySQL/MariaDB server for the Specify database.



16
17
18
# File 'lib/specify/database.rb', line 16

def port
  @port
end

#sessionsObject (readonly)

An Array of Specify::Session instances currently registered (and open).



19
20
21
# File 'lib/specify/database.rb', line 19

def sessions
  @sessions
end

#userObject (readonly)

The MySQL/MariaDB user for the database. This is typically the Specify master user.



23
24
25
# File 'lib/specify/database.rb', line 23

def user
  @user
end

Class Method Details

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

Creates a new Database from config_file file (a YAML file).

config_file should have the structure:

---
:hosts:
  <hostname>:
    :port: <port_number>
    :databases:
      <database_name>:
        :db_user:
          :name: <mysql_username>
          :password: <password>
        :sp_user: <specify_username>

Items prefices with : will be deserialized as symbols. Leave :password: blank to be prompted.

host: the name of the MySQL/MariaDB host for the database. database: the name of the MySQL/MariaDB database.



44
45
46
47
# File 'lib/specify/database.rb', line 44

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.



75
76
77
78
79
# File 'lib/specify/database.rb', line 75

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

#closeObject

Closes all sessions.



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

def close
  return if sessions.empty?
  sessions.each do |session|
    session.close
    session.delete_observer self
  end
  # TODO: should close database connection
end

#connectObject

Establishes a connection and creates the object if it does not already exist. Loads all Specify::Model classes.

Returns the Sequel::Database object for the database.



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/specify/database.rb', line 95

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

Creates a string representation of self.



108
109
110
111
# File 'lib/specify/database.rb', line 108

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

#start_session(user, collection) ⇒ Object

Createas a new Session for user (String, an existing Specify::Model::User#name) in collection (String, an existing Specify::Model::Collection#name) and adds it to the #sessions pool.

Returns the new Session.



118
119
120
121
122
123
# File 'lib/specify/database.rb', line 118

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

#update(session) ⇒ Object

Deletes a session (a Session) from the sessions pool when session has been closed.



127
128
129
# File 'lib/specify/database.rb', line 127

def update(session)
  sessions.delete session
end