Class: DataDuck::SqlDbSource
- Defined in:
- lib/dataduck/sql_db_source.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#username ⇒ Object
Returns the value of attribute username.
Attributes inherited from Database
Instance Method Summary collapse
- #connection ⇒ Object
- #db_type ⇒ Object
- #escape_char ⇒ Object
-
#initialize(name, config) ⇒ SqlDbSource
constructor
A new instance of SqlDbSource.
- #query(sql) ⇒ Object
- #table_names ⇒ Object
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
#database ⇒ Object
Returns the value of attribute database.
12 13 14 |
# File 'lib/dataduck/sql_db_source.rb', line 12 def database @database end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/dataduck/sql_db_source.rb', line 8 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
11 12 13 |
# File 'lib/dataduck/sql_db_source.rb', line 11 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
9 10 11 |
# File 'lib/dataduck/sql_db_source.rb', line 9 def port @port end |
#username ⇒ Object
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
#connection ⇒ Object
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_type ⇒ Object
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_char ⇒ Object
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_names ⇒ Object
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 |