# Module: Tgdatabase

# Tgdatabase (Database)

Tgdatabase provides the database connectivity and initialization routines.

## Embedded

OpenTelegard includes an embedded database option for installations that do not need a larger database server, or for users not comfortable managing a database engine separately. H2 is the included database, and is provided as a JAR file with the distribution. see (h2database.com).

## Remote

For larger deployments, cloud deployments, or for advanced users, OpenTelegard supports external database connections. remote is only tested against Postgresql, but should work on any Sequel JDBC compatible server.

# Public Class Methods

### connect (db)

Create Database Connection based on type of either H2 Embedded or External Remote.

Show source...

         # File bbs/lib/tgdatabase.rb, line 74
     74:   def Tgdatabase.connect(db)
     75:     if db['type'] == "remote"
     76:       begin
     77:         Sequel.connect("#{db['driver']}://#{db['host']}/#{db['name']}?user=#{db['user']}&password=#{db['pass']}")
     78:       # TODO: Try to enhance this rescue as it doesn't seem to respond to JDBC driver errors.
     79:       rescue Exception, NativeException, Sequel::DatabaseConnectionError => e
     80:         raise "ERROR: Unable to connect to remote database. (#{e})"
     81:       end
     82:
     83:     elsif db['type'] == "embedded"
     84:       # -> Try to load Java support
     85:       begin
     86:         require 'java'
     87:       rescue LoadError
     88:         raise "FATAL: Unable to load JAVA hooks. OpenTG requires JRuby >= 1.5.3"
     89:       end
     90:       # -> Try to load H2 Database from JAR.
     91:       # (instead of using the GEM for jdbc-h2 which tends to be outdated)
     92:       begin
     93:         require 'class/h2.jar'          # This loads the entire DB engine from jar
     94:         Java::org.h2.Driver             # This loads the native JDBC driver
     95:       rescue LoadError
     96:         raise "ERROR: unable to load embedded database engine."
     97:       end
     98:       # -> Try to connect to H2 Database from Sequel.
     99:       begin
    100:         Sequel.connect("jdbc:h2:tcp://localhost/opentg")
    101:       rescue Sequel::DatabaseConnectionError => e
    102:         raise "ERROR: Unable to connect to embedded database. (#{e})"
    103:       rescue Java::NativeException => e
    104:         raise "ERROR: Unable to connect to embedded database. (#{e})"
    105:       end
    106:
    107:     else
    108:       puts "FATAL: Database type has been improperly set or is missing."
    109:       exit 100
    110:     end
    111:
    112:   end

### initdb ()

# > Initialize the database (reserved)

Show source...

         # File bbs/lib/tgdatabase.rb, line 116
    116:   def Tgdatabase.initdb
    117:     # THIS IS PROBABLY NOT NEEDED AS THIS IS HANDLED VIA Sequel::Model - BUT RESERVE THE NAME
    118:   end