Skip to content

Commit

Permalink
Patched _explicit_query() method so that it doesn't fail on SQL math …
Browse files Browse the repository at this point in the history
…operations.
  • Loading branch information
hover2pi committed Mar 14, 2016
1 parent 79fb2ab commit 21f0545
Showing 1 changed file with 50 additions and 45 deletions.
95 changes: 50 additions & 45 deletions astrodbkit/astrodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ def query(self, SQL, params='', fmt='array', fetch='all', unpack=False, export='
table = at.Table(dictionary)

# Reorder the columns
table = table[columns]
try: table = table[columns]
except: pass

# Make an array
array = np.asarray(table)
Expand Down Expand Up @@ -884,61 +885,65 @@ def _explicit_query(self, SQL, use_converters=True):
The new SQLite string to use in the query and the ordered column names
"""
# If field names are given, sort so that they come out in the same order they are fetched
if 'select' in SQL.lower() and 'from' in SQL.lower():
try:
# If field names are given, sort so that they come out in the same order they are fetched
if 'select' in SQL.lower() and 'from' in SQL.lower():

# Make a dictionary of the table aliases
tdict = {}
from_clause = SQL.lower().split('from ')[-1].split(' where')[0]
tables = [j for k in [i.split(' on ') for i in from_clause.split(' join ')] for j in k if '=' not in j]
# Make a dictionary of the table aliases
tdict = {}
from_clause = SQL.lower().split('from ')[-1].split(' where')[0]
tables = [j for k in [i.split(' on ') for i in from_clause.split(' join ')] for j in k if '=' not in j]

for t in tables:
t = t.replace('as','')
try:
name, alias = t.split()
tdict[alias] = name
except:
tdict[t] = t
for t in tables:
t = t.replace('as','')
try:
name, alias = t.split()
tdict[alias] = name
except:
tdict[t] = t

# Get all the column names and dtype placeholders
columns = SQL.replace(' ','').lower().split('distinct' if 'distinct' in SQL.lower() else 'select')[1].split('from')[0].split(',')
# Get all the column names and dtype placeholders
columns = SQL.replace(' ','').lower().split('distinct' if 'distinct' in SQL.lower() else 'select')[1].split('from')[0].split(',')

# Replace * with the field names
for n,col in enumerate(columns):
if '.' in col:
t, col = col.split('.')
else:
t = tables[0]
# Replace * with the field names
for n,col in enumerate(columns):
if '.' in col:
t, col = col.split('.')
else:
t = tables[0]

if '*' in col:
col = np.array(self.list("PRAGMA table_info({})".format(tdict.get(t))).fetchall()).T[1]
else:
col = [col]
if '*' in col:
col = np.array(self.list("PRAGMA table_info({})".format(tdict.get(t))).fetchall()).T[1]
else:
col = [col]

columns[n] = ["{}.{}".format(t,c) if len(tables)>1 else c for c in col]
columns[n] = ["{}.{}".format(t,c) if len(tables)>1 else c for c in col]

# Flatten the list of columns and dtypes
columns = [j for k in columns for j in k]
# Flatten the list of columns and dtypes
columns = [j for k in columns for j in k]

# Get the dtypes
dSQL = "SELECT " \
+ ','.join(["typeof({})".format(col) for col in columns])\
+ ' FROM '+SQL.replace('from','FROM').split('FROM')[-1]
if use_converters: dtypes = [None]*len(columns)
else: dtypes = self.list(dSQL).fetchone()
# Get the dtypes
dSQL = "SELECT " \
+ ','.join(["typeof({})".format(col) for col in columns])\
+ ' FROM '+SQL.replace('from','FROM').split('FROM')[-1]
if use_converters: dtypes = [None]*len(columns)
else: dtypes = self.list(dSQL).fetchone()

# Reconstruct SQL query
SQL = "SELECT {}".format('DISTINCT ' if 'distinct' in SQL.lower() else '')\
+ (','.join(["{0} AS '{0}'".format(col) for col in columns])\
if use_converters else ','.join(["{1}{0}{2} AS '{0}'".format(col,'CAST(' if dt!='null' else '',' AS {})'.format(dt) if dt!='null' else '') \
for dt,col in zip(dtypes,columns)])) \
+ ' FROM '\
+ SQL.replace('from','FROM').split('FROM')[-1]
# Reconstruct SQL query
SQL = "SELECT {}".format('DISTINCT ' if 'distinct' in SQL.lower() else '')\
+ (','.join(["{0} AS '{0}'".format(col) for col in columns])\
if use_converters else ','.join(["{1}{0}{2} AS '{0}'".format(col,'CAST(' if dt!='null' else '',' AS {})'.format(dt) if dt!='null' else '') \
for dt,col in zip(dtypes,columns)])) \
+ ' FROM '\
+ SQL.replace('from','FROM').split('FROM')[-1]

elif 'pragma' in SQL.lower():
columns = ['cid','name','type','notnull','dflt_value','pk']
elif 'pragma' in SQL.lower():
columns = ['cid','name','type','notnull','dflt_value','pk']

return SQL, columns
return SQL, columns

except:
return SQL, ''

# ==============================================================================================================================================
# ================================= Adapters and converters for special data types =============================================================
Expand Down

0 comments on commit 21f0545

Please sign in to comment.