Class: DataDuck::SqlDbSource

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

Direct Known Subclasses

MysqlSource, PostgresqlSource

Instance Attribute Summary

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, data) ⇒ SqlDbSource

Returns a new instance of SqlDbSource.



8
9
10
11
12
13
14
15
16
17
# File 'lib/dataduck/sql_db_source.rb', line 8

def initialize(name, data)
  @host = data['host']
  @port = data['port']
  @username = data['username']
  @password = data['password']
  @database = data['database']
  @initialized_db_type = data['db_type']

  super
end

Instance Method Details

#connectionObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/dataduck/sql_db_source.rb', line 19

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

#db_typeObject

Raises:

  • (NotImplementedError)


30
31
32
33
34
# File 'lib/dataduck/sql_db_source.rb', line 30

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)


36
37
38
# File 'lib/dataduck/sql_db_source.rb', line 36

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

#query(sql) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/dataduck/sql_db_source.rb', line 44

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



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

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