bug fix with sql handling sqlalchemy
This commit is contained in:
parent
2cee6db63b
commit
18cd1d8b11
2
setup.py
2
setup.py
|
@ -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)
|
# table = self._tablename(_args['table'])if 'table' in _args else self._tablename(self.table)
|
||||||
sql = "SELECT * FROM :table LIMIT 1".replace(":table",table)
|
# sql = "SELECT * FROM :table LIMIT 1".replace(":table",table)
|
||||||
if self._engine :
|
# if self._engine :
|
||||||
_conn = self._engine.connect()
|
# _conn = self._engine.connect()
|
||||||
else:
|
# else:
|
||||||
_conn = self.conn
|
# _conn = self.conn
|
||||||
found = pd.read_sql(sql,_conn).shape[0]
|
# found = pd.read_sql(sql,_conn).shape[0]
|
||||||
found = True
|
# found = True
|
||||||
|
|
||||||
except Exception as e:
|
# except Exception as e:
|
||||||
pass
|
# print (e)
|
||||||
finally:
|
# pass
|
||||||
if self._engine :
|
# finally:
|
||||||
_conn.close()
|
# if not self._engine :
|
||||||
return found
|
# _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:
|
||||||
self.conn.commit()
|
if not self._engine :
|
||||||
cursor.close()
|
self.conn.commit()
|
||||||
|
# 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']
|
||||||
_fields = '*' if not self.fields else ",".join(self.fields)
|
if 'fields' in _args :
|
||||||
|
_fields = _args['fields']
|
||||||
|
else:
|
||||||
|
_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…
Reference in New Issue