bug fix: insert on an etl with data-typing (tricky)

This commit is contained in:
Steve Nyemba 2021-01-02 17:39:27 -06:00
parent eef84d8069
commit fa615aa931
1 changed files with 34 additions and 9 deletions

View File

@ -20,7 +20,7 @@ import json
# from threading import Lock # from threading import Lock
import pandas as pd import pandas as pd
import numpy as np
class SQLRW : class SQLRW :
PROVIDERS = {"postgresql":"5432","redshift":"5432","mysql":"3306","mariadb":"3306"} PROVIDERS = {"postgresql":"5432","redshift":"5432","mysql":"3306","mariadb":"3306"}
DRIVERS = {"postgresql":pg,"redshift":pg,"mysql":my,"mariadb":my} DRIVERS = {"postgresql":pg,"redshift":pg,"mysql":my,"mariadb":my}
@ -95,6 +95,12 @@ class SQLReader(SQLRW,Reader) :
class SQLWriter(SQLRW,Writer): class SQLWriter(SQLRW,Writer):
def __init__(self,**_args) : def __init__(self,**_args) :
super().__init__(**_args) super().__init__(**_args)
#
# In the advent that data typing is difficult to determine we can inspect and perform a default case
# This slows down the process but improves reliability of the data
# NOTE: Proper data type should be set on the target system if their source is unclear.
self._inspect = False if 'inspect' not in _args else _args['inspect']
self._cast = False if 'cast' not in _args else _args['cast']
def init(self,fields): def init(self,fields):
if not fields : if not fields :
try: try:
@ -118,6 +124,8 @@ class SQLWriter(SQLRW,Writer):
""" """
:param info writes a list of data to a given set of fields :param info writes a list of data to a given set of fields
""" """
# inspect = False if 'inspect' not in _args else _args['inspect']
# cast = False if 'cast' not in _args else _args['cast']
if not self.fields : if not self.fields :
_fields = info.keys() if type(info) == dict else info[0].keys() _fields = info.keys() if type(info) == dict else info[0].keys()
_fields = list (_fields) _fields = list (_fields)
@ -127,9 +135,26 @@ class SQLWriter(SQLRW,Writer):
info = [info] info = [info]
cursor = self.conn.cursor() cursor = self.conn.cursor()
try: try:
_sql = "INSERT INTO :table (:fields) values (:values)".replace(":table",self.table) #.replace(":table",self.table).replace(":fields",_fields)
if self._inspect :
for _row in info :
fields = list(_row.keys())
if self._cast == False :
values = ",".join(_row.values())
else:
values = "'"+"','".join([str(value) for value in _row.values()])+"'"
# values = [ "".join(["'",str(_row[key]),"'"]) if np.nan(_row[key]).isnumeric() else str(_row[key]) for key in _row]
# print (values)
query = _sql.replace(":fields",",".join(fields)).replace(":values",values)
cursor.execute(query)
pass
else:
_fields = ",".join(self.fields) _fields = ",".join(self.fields)
_sql = "INSERT INTO :table (:fields) values (:values)".replace(":table",self.table).replace(":fields",_fields) _sql = _sql.replace(":fields",_fields)
_sql = _sql.replace(":values",",".join(["%("+name+")s" for name in self.fields])) _sql = _sql.replace(":values",",".join(["%("+name+")s" for name in self.fields]))
# for row in info : # for row in info :