bug fix with sql handling sqlalchemy

pull/1/head
Steve Nyemba 1 year ago
parent 2cee6db63b
commit 18cd1d8b11

@ -8,7 +8,7 @@ def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read() return open(os.path.join(os.path.dirname(__file__), fname)).read()
args = { args = {
"name":"data-transport", "name":"data-transport",
"version":"1.7.0", "version":"1.7.2",
"author":"The Phi Technology LLC","author_email":"info@the-phi.com", "author":"The Phi Technology LLC","author_email":"info@the-phi.com",
"license":"MIT", "license":"MIT",
"packages":["transport"]} "packages":["transport"]}

@ -123,28 +123,31 @@ class SQLRW :
return self.schema +'.'+name if self.schema not in [None, ''] and '.' not in name else name return self.schema +'.'+name if self.schema not in [None, ''] and '.' not in name else name
def has(self,**_args): def has(self,**_args):
found = False return self.meta(**_args)
try: # found = False
# try:
table = self._tablename(_args['table'])if 'table' in _args else self._tablename(self.table)
sql = "SELECT * FROM :table LIMIT 1".replace(":table",table) # table = self._tablename(_args['table'])if 'table' in _args else self._tablename(self.table)
if self._engine : # sql = "SELECT * FROM :table LIMIT 1".replace(":table",table)
_conn = self._engine.connect() # if self._engine :
else: # _conn = self._engine.connect()
_conn = self.conn # else:
found = pd.read_sql(sql,_conn).shape[0] # _conn = self.conn
found = True # found = pd.read_sql(sql,_conn).shape[0]
# found = True
except Exception as e:
pass # except Exception as e:
finally: # print (e)
if self._engine : # pass
_conn.close() # finally:
return found # if not self._engine :
# _conn.close()
# return found
def isready(self): def isready(self):
_sql = "SELECT * FROM :table LIMIT 1".replace(":table",self.table) _sql = "SELECT * FROM :table LIMIT 1".replace(":table",self.table)
try: try:
return pd.read_sql(_sql,self.conn).columns.tolist() _conn = self.conn if not hasattr(self,'_engine') else self._engine
return pd.read_sql(_sql,_conn).columns.tolist()
except Exception as e: except Exception as e:
pass pass
return False return False
@ -154,22 +157,24 @@ class SQLRW :
:param _sql insert/select statement :param _sql insert/select statement
@TODO: Store procedure calls @TODO: Store procedure calls
""" """
cursor = self.conn.cursor() #
_out = None _out = None
try: try:
if "select" in _sql.lower() : if _sql.lower().startswith('select') :
# _conn = self._engine if self._engine else self.conn _conn = self._engine if self._engine else self.conn
return pd.read_sql(_sql,self.conn) return pd.read_sql(_sql,_conn)
else: else:
# Executing a command i.e no expected return values ... # Executing a command i.e no expected return values ...
cursor = self.conn.cursor()
cursor.execute(_sql) cursor.execute(_sql)
self.conn.commit() self.conn.commit()
except Exception as e : except Exception as e :
print (e) print (e)
finally: finally:
if not self._engine :
self.conn.commit() self.conn.commit()
cursor.close() # cursor.close()
def close(self): def close(self):
try: try:
self.conn.close() self.conn.close()
@ -184,12 +189,21 @@ class SQLReader(SQLRW,Reader) :
if 'sql' in _args : if 'sql' in _args :
_sql = (_args['sql']) _sql = (_args['sql'])
else: else:
table = self.table if self.table is not None else _args['table'] if 'table' in _args :
table = _args['table']
else:
table = self.table
# table = self.table if self.table is not None else _args['table']
_sql = "SELECT :fields FROM "+self._tablename(table) _sql = "SELECT :fields FROM "+self._tablename(table)
if 'filter' in _args : if 'filter' in _args :
_sql = _sql +" WHERE "+_args['filter'] _sql = _sql +" WHERE "+_args['filter']
if 'fields' in _args :
_fields = _args['fields']
else:
_fields = '*' if not self.fields else ",".join(self.fields) _fields = '*' if not self.fields else ",".join(self.fields)
_sql = _sql.replace(":fields",_fields) _sql = _sql.replace(":fields",_fields)
#
# At this point we have a query we can execute gracefully
if 'limit' in _args : if 'limit' in _args :
_sql = _sql + " LIMIT "+str(_args['limit']) _sql = _sql + " LIMIT "+str(_args['limit'])
# #

Loading…
Cancel
Save