|
@@ -1,4 +1,6 @@
|
|
|
import pandas as pd
|
|
|
+import itertools
|
|
|
+
|
|
|
|
|
|
def prepareQuery(queryData):
|
|
|
|
|
@@ -113,21 +115,7 @@ def prepareQuery(queryData):
|
|
|
|
|
|
#################
|
|
|
elif type=='rifAlt':
|
|
|
- try:
|
|
|
- coordsSet = queryData['coordsSet']
|
|
|
- except KeyError as err:
|
|
|
- raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
|
|
|
-
|
|
|
- subQueries = []
|
|
|
- for coords in coordsSet:
|
|
|
- try:
|
|
|
- numorg = coords[0]
|
|
|
- ntx = coords[1]
|
|
|
- except IndexError as err:
|
|
|
- raise KeyError('Incomplete required data for query type ' + type + ': ' + str(err))
|
|
|
- subQueries.append( f"SELECT indice AS numorg, ntx, head AS Rif_organico, full AS Rif_completo FROM org WHERE (indice='{numorg}' AND ntx='{ntx}')" )
|
|
|
-
|
|
|
- return ' UNION ALL '.join(subQueries)
|
|
|
+ return rifAlt
|
|
|
|
|
|
#################
|
|
|
elif type=='highlight':
|
|
@@ -176,6 +164,38 @@ def prepareQuery(queryData):
|
|
|
else:
|
|
|
raise ValueError('Unrecognized query type: ' + type)
|
|
|
|
|
|
+def rifAlt(connection, queryData):
|
|
|
+ try:
|
|
|
+ coordsSet = queryData['coordsSet']
|
|
|
+ except KeyError as err:
|
|
|
+ raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
|
|
|
+
|
|
|
+ subQueries = []
|
|
|
+ for coords in coordsSet:
|
|
|
+ try:
|
|
|
+ numorg = coords[0]
|
|
|
+ ntx = coords[1]
|
|
|
+ except IndexError as err:
|
|
|
+ raise KeyError('Incomplete required data for query type ' + type + ': ' + str(err))
|
|
|
+ subQueries.append( f"SELECT indice AS numorg, ntx, head AS Rif_organico, full AS Rif_completo FROM org WHERE (indice='{numorg}' AND ntx='{ntx}')" )
|
|
|
+
|
|
|
+ pandas = queryData.get('pandas')
|
|
|
+ resultList = []
|
|
|
+ for query in subQueries:
|
|
|
+ if pandas:
|
|
|
+ resultList.append( pd.read_sql(query, connection) )
|
|
|
+ else:
|
|
|
+ connection.row_factory = dict_factory
|
|
|
+ queryReponse = connection.cursor().execute(query)
|
|
|
+ resultList.append( queryReponse.fetchall() )
|
|
|
+
|
|
|
+ if pandas:
|
|
|
+ results = pd.concat(resultList)
|
|
|
+ else:
|
|
|
+ results = list(itertools.chain.from_iterable(resultList))
|
|
|
+
|
|
|
+ return results
|
|
|
+
|
|
|
|
|
|
def complexQueryTexts(connection, queryData):
|
|
|
try:
|
|
@@ -310,3 +330,8 @@ def complexQueryCooccurrences(connection, queryData):
|
|
|
results[['backup_piniz', 'backup_pfin']] = resultsPeriodi[['backup_piniz', 'backup_pfin']]
|
|
|
|
|
|
return results
|
|
|
+
|
|
|
+# Dict factory non-Pandas queries
|
|
|
+def dict_factory(cursor, row):
|
|
|
+ fields = [column[0] for column in cursor.description]
|
|
|
+ return {key: value for key, value in zip(fields, row)}
|