Source code for stetl.outputs.dboutput

# Output classes for ETL, databases.
# Author: Just van den Broecke
from stetl.output import Output
from stetl.util import Util
from stetl.packet import FORMAT
from stetl.component import Config
from stetl.postgis import PostGIS

log = Util.get_log('dboutput')

[docs]class DbOutput(Output): """ Output to any database (abstract base class). """ def __init__(self, configdict, section, consumes): Output.__init__(self, configdict, section, consumes) def write(self, packet): return packet
[docs]class PostgresDbOutput(DbOutput): """ Output to PostgreSQL database. Input is an SQL string. Output by executing input SQL string. consumes=FORMAT.string """ # Start attribute config meta
[docs] @Config(ptype=str, required=True, default=None) def database(self): """ Database name. """ pass
[docs] @Config(ptype=str, required=False, default=None) def user(self): """ DB User name. """ pass
[docs] @Config(ptype=str, required=False, default=None) def password(self): """ DB Password for user. """ pass
[docs] @Config(ptype=str, required=False, default=None) def host(self): """ Hostname for DB. """ pass
[docs] @Config(ptype=str, required=False, default='public') def schema(self): """ Postgres schema name for DB. """ pass
# End attribute config meta def __init__(self, configdict, section): DbOutput.__init__(self, configdict, section, consumes=FORMAT.string) def write(self, packet): if is None: return packet'executing SQL') db = PostGIS(self.cfg.get_dict()) rowcount = db.tx_execute('executed SQL, rowcount=%d' % rowcount) return packet
[docs]class PostgresInsertOutput(PostgresDbOutput): """ Output by inserting a single record in a Postgres database table. Input is a Stetl record (Python dict structure) or a list of records. Creates an INSERT for Postgres to insert each single record. When the "replace" parameter is True, any existing record keyed by "key" is attempted to be UPDATEd first. NB a constraint is that the first and each subsequent each record needs to contain all values as an INSERT and UPDATE query template is built once for the columns in the first record. consumes=[FORMAT.record_array, FORMAT.record] """ # Start attribute config meta
[docs] @Config(ptype=str, required=False, default='public') def table(self): """ Table for inserts. """ pass
[docs] @Config(ptype=bool, required=False, default=False) def replace(self): """ Replace record if exists? """ pass
[docs] @Config(ptype=str, required=False, default=None) def key(self): """ The key column name of the table, required when replacing records. """ pass
# End attribute config meta def __init__(self, configdict, section, consumes=FORMAT.record): DbOutput.__init__(self, configdict, section, consumes=[FORMAT.record_array, FORMAT.record]) self.query = None self.update_query = None self.db = None
[docs] def init(self): # Connect only once to DB'Init: connect to DB') self.db = PostGIS(self.cfg.get_dict()) self.db.connect()
[docs] def exit(self): # Disconnect from DB when done'Exit: disconnect from DB') self.db.disconnect()
def create_query(self, record): # We assume that all records do the same INSERT key/values # See # e.g. INSERT INTO lml_files ("file_name", "file_data") VALUES (%s,%s) query = "INSERT INTO %s (%s) VALUES (%s)" % ( self.cfg.get('table'), ",".join(['%s' % k for k in record]), ",".join(["%s", ] * len(record.keys())))'query is %s', query) return query def create_update_query(self, record): # We assume that all records do the same UPDATE key/values # # e.g. UPDATE table SET field='C', field2='Z' WHERE id=3; query = "UPDATE %s SET (%s) = (%s) WHERE %s = %s" % ( self.cfg.get('table'), ",".join(['%s ' % k for k in record]), ",".join(["%s", ] * len(record.keys())), self.key, "%s")'update query is %s', query) return query def insert(self, record): res = 0 if self.replace and self.key and self.key in record: # Replace option: try UPDATE if existing # values = list(record.values()) values.append(record[self.key]) res = self.db.execute(self.update_query, values) # del_query = "DELETE FROM %s WHERE %s = '%s'" % (self.cfg.get('table'), self.key, record[self.key]) # res = self.db.execute(del_query) if res < 1: # Do insert with values from the record dict # only if we did not do an UPDATE (res==0) on existing record. self.db.execute(self.query, list(record.values())) self.db.commit(close=False) def write(self, packet): # Deal with empty or zero-length data structures (list or dict) if is None or len( == 0: return packet # ASSERT: record data present # record is Python dict (single record) or list of Python dict (multiple records) record = # Generate INSERT query template once first_record = record if type(record) is list and len(record) > 0: first_record = record[0] # Create INSERT and optional UPDATE query-templates once if self.query is None: self.query = self.create_query(first_record) if self.replace and self.key and not self.update_query: self.update_query = self.create_update_query(first_record) # Check if record is single (dict) or array (list of dict) if type(record) is dict: # Do insert with values from the single record self.insert(record) #'committed record key=%s' % record[self.key]) elif type(record) is list: # Multiple records in list for rec in record: # Do insert with values from the record self.insert(rec)'committed %d records' % len(record)) return packet