Class: Sqldump::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/sqldump/connector.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connector

Returns a new instance of Connector.



7
8
9
# File 'lib/sqldump/connector.rb', line 7

def initialize(options)
  @options = options
end

Instance Method Details

#connectObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/sqldump/connector.rb', line 11

def connect
  begin
    dbh = DBI.connect(get_url, get_user, get_password)
    yield dbh

  ensure
    dbh.disconnect if dbh
  end

end

#get_driverObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sqldump/connector.rb', line 31

def get_driver
  case @options.database_type
    when :sqlite3
      "SQLite3"
    when :postgresql
      "Pg"
    when :mysql
      "Mysql"
    else
      raise "Unknown database type " + @options.database_type
  end
end

#get_passwordObject



52
53
54
# File 'lib/sqldump/connector.rb', line 52

def get_password
  @options.password
end

#get_urlObject



22
23
24
25
26
27
28
29
# File 'lib/sqldump/connector.rb', line 22

def get_url
  driver = get_driver
  url = "DBI:#{driver}:#{@options.database}"
  if use_host
    url += ":#{@options.host}"
  end
  url
end

#get_userObject



48
49
50
# File 'lib/sqldump/connector.rb', line 48

def get_user
  @options.username
end

#use_hostObject



44
45
46
# File 'lib/sqldump/connector.rb', line 44

def use_host
  @options.database_type != :sqlite3
end