kora 3 bulan lalu
induk
melakukan
b34f75de9e

+ 15 - 0
flask_be/Config/config_TLIO.json

@@ -0,0 +1,15 @@
+{
+    "LOCAL_ENVIRONMENT": true,
+    "DATA_CONFIG": {
+        "dbPath": "/home/kora/Desktop/Work/DBs/tlio_04-04-24.gat4/",
+        "dbfile_default": "tlio.db",
+        "data_interface": "sqlite3",
+        "dynamic_occ_tables": true,
+        "db_encoded": true,
+        "texts_encoded": true
+    },
+    "LOGGER_CONFIG": {
+        "filename": "Progetto2023_BE.log",
+        "level": "info"
+    }
+}

+ 40 - 15
flask_be/interface_sqlite3/actual_queries.py

@@ -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)}

+ 9 - 5
flask_be/interface_sqlite3/encdec/de_code.py

@@ -46,9 +46,10 @@ class keyRing:
     def getKeyByCode(self, keyFile):
         with open(self.keyPath + keyFile, 'r') as file1:
             reader = csv.reader(file1)
-            key = [int(row[0]) for index, row in enumerate(reader) if index>1]
-            halfKeyLen = len(key)//2
-            key=key[:halfKeyLen]
+            key = [int(row[0]) for row in reader][1:]
+            #key = [int(row[0]) for index, row in enumerate(reader) if index>0]
+            #halfKeyLen = len(key)//2
+            #key=key[:halfKeyLen]
             return key
 
 
@@ -82,10 +83,13 @@ def db_encode(vettDictEnc, string0):
 def decodeTextByKey(text, key, startInFile):
     initialOffset = startInFile % len(key)
     res = ""
+    len2 = len(key)//2 # Modify to use only 1/2 key as in Gatto
     for k, char0 in enumerate(text):
         offset = k + initialOffset
-        if offset >= len(key):
-            offset = offset % len(key)
+        if offset >= len2: # MOD
+            offset = offset % len2 # MOD
+#        if offset >= len(key):
+#            offset = offset % len(key)
         res += shiftchar(char0, -key[offset])
     return res
 #

+ 6 - 2
flask_be/interface_sqlite3/query_handlers.py

@@ -34,6 +34,7 @@ class queryHandlerBasicSqlite:
         # Now the method processes a query data OBJECT
         # and creates the query (which may be complex)
         # accordingly
+        queryData['pandas'] = pandas # Mostly redundant
         if self.dbEncoded:
             queryData = self.encodeQuery(queryData)
         queryToExecute = prepareQuery(queryData)
@@ -63,8 +64,11 @@ class queryHandlerBasicSqlite:
         else:
             # If not a string, 'queryToExecute' should be a method/function reference
             results = queryToExecute(connection, queryData)
-            if(self.dbEncoded):
-                results = self.db_results_decode_pandas(results)
+            if self.dbEncoded:
+                if pandas:
+                    results = self.db_results_decode_pandas(results)
+                else:
+                    results = self.db_results_decode(results)
 
         connection.close()