Class: DataDuck::SqlDbSource

Inherits:
Source show all
Defined in:
lib/dataduck/sql_db_source.rb

Direct Known Subclasses

MysqlSource, PostgresqlSource

Instance Attribute Summary collapse

Attributes inherited from Database

#name

Instance Method Summary collapse

Methods inherited from Source

load_config!, only_source, #schema, skip_these_table_names, source, source_config

Constructor Details

#initialize(name, config) ⇒ SqlDbSource

Returns a new instance of SqlDbSource.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dataduck/sql_db_source.rb', line 14

def initialize(name, config)
  load_value('host', name, config)
  load_value('port', name, config)
  load_value('username', name, config)
  load_value('password', name, config)
  load_value('database', name, config)

  @initialized_db_type = config['db_type']

  super
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



12
13
14
# File 'lib/dataduck/sql_db_source.rb', line 12

def database
  @database
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/dataduck/sql_db_source.rb', line 8

def host
  @host
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/dataduck/sql_db_source.rb', line 11

def password
  @password
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/dataduck/sql_db_source.rb', line 9

def port
  @port
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/dataduck/sql_db_source.rb', line 10

def username
  @username
end

Instance Method Details

#connectionObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dataduck/sql_db_source.rb', line 26

def connection
  adapter = self.db_type.to_s
  adapter = 'mysql2' if adapter == 'mysql' # mysql2 adapter is faster than just mysql

  @connection ||= Sequel.connect(
    adapter: adapter,
    user: self.username,
    host: self.host,
    database: self.database,
    password: self.password,
    port: self.port
  )
end

#db_typeObject

Raises:

  • (NotImplementedError)


40
41
42
43
44
# File 'lib/dataduck/sql_db_source.rb', line 40

def db_type
  return @initialized_db_type if @initialized_db_type

  raise NotImplementedError.new("Abstract method db_type must be overwritten by subclass, or passed as data when initializing.")
end

#escape_charObject

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/dataduck/sql_db_source.rb', line 46

def escape_char
  raise NotImplementedError.new("Abstract method escape_char must be overwritten by subclass.")
end

#query(sql) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/dataduck/sql_db_source.rb', line 54

def query(sql)
  if self.is_mutating_sql?(sql)
    raise ArgumentError.new("Database #{ self.name } must not run mutating sql: #{ sql }")
  end

  Logs.debug("SQL executing on #{ self.name }:\n  " + sql)
  self.connection.fetch(sql).all
end

#table_namesObject



50
51
52
# File 'lib/dataduck/sql_db_source.rb', line 50

def table_names
  self.connection.tables.map { |table| DataDuck::Source.skip_these_table_names.include?(table) ? nil : table }.compact
end