Browse Source

Formatting added to Flask BE v0 to test

kora 1 year ago
parent
commit
0b494763f7

+ 1 - 1
flask_be/Config/config_loader.py

@@ -10,7 +10,7 @@ def config(flask_app):
 
     # Get environment label, define conf file name
     env_label = os.environ.get('APP_ENVIRONMENT')    
-    if env_label is not None:
+    if env_label is not None and env_label!="":
         confFileName = "config_" + env_label + ".json"
     # Default
     else:

+ 15 - 11
flask_be/engine/basic_queries.py

@@ -70,6 +70,7 @@ class basicQueries:
         parole = 31
         listOcc = self.listOcc
         contexts = []
+        formats = []
         minChar_list = []
         maxChar_list = []
         for ind, row in textlist.iterrows():
@@ -85,11 +86,13 @@ class basicQueries:
             fileQueryData = {'sigla': sigla, 'minChar': minChar, 'maxChar': maxChar}
             minChar_list.append(minChar)
             maxChar_list.append(maxChar)
-            cont = self.queryHandler.textQuery(fileQueryData)
+            cont, format0 = self.queryHandler.textQuery(fileQueryData, True)
             contexts.append(cont)
+            formats.append(format0)
         textlist['piniz'] = minChar_list
         textlist['pifin'] = maxChar_list
         textlist['contesto'] = contexts
+        textlist['format contesto'] = formats
         return (textlist.reset_index(drop=True))
 
     # %% Ha in input findcontexts, associa i riferimenti bibliografici ad ogni contesto.
@@ -207,9 +210,8 @@ class basicQueries:
                 queryresponse = self.queryHandler.query(queryData, pandas=True)
                 pointerlist = pd.concat([pointerlist, queryresponse])
             fileQueryData = {'sigla': sigla, 'minChar': pointerlist["pitxt"].min(), 'maxChar': pointerlist["pitxt"].max()}
-            cont = self.queryHandler.textQuery(fileQueryData)
+            cont, form = self.queryHandler.textQuery(fileQueryData)
             contexts.append(cont)
-            form = self.queryHandler.formatQuery(fileQueryData)
             formats.append(form)
             context ['piniz'] = pointerlist["pitxt"].min()
             context ['pfin'] = pointerlist["pitxt"].max()
@@ -217,9 +219,8 @@ class basicQueries:
             queryData = {'queryType': 'singlecontext', 'querySubtype': 'parole', 'parole': parole, 'periodi': periodi, 'brani': brani, 'table': table, 'ntxlocal': ntxlocal, 'mappalocal': mappalocal, 'periodlocal': periodlocal, 'numbranolocal': numbranolocal}
             queryresponse = self.queryHandler.query(queryData, pandas=True)
             fileQueryData = {'sigla': sigla, 'minChar': pointerlist["pitxt"].min(), 'maxChar': pointerlist["pitxt"].max()}
-            cont = self.queryHandler.textQuery(fileQueryData)
+            cont, form = self.queryHandler.textQuery(fileQueryData, True)
             contexts.append(cont)
-            form = self.queryHandler.formatQuery(fileQueryData)
             formats.append(form)
             context ['piniz'] = queryresponse["piniz"].min()
             context ['pfin'] = queryresponse["pfin"].max()
@@ -230,9 +231,8 @@ class basicQueries:
                 queryData = {'queryType': 'singlecontext', 'querySubtype': 'brani', 'parole': parole, 'periodi': periodi, 'brani': brani, 'table': table, 'ntxlocal': ntxlocal, 'mappalocal': mappalocal, 'periodlocal': periodlocal, 'numbranolocal': numbranolocal}
                 queryresponse = self.queryHandler.query(queryData, pandas=True)
                 fileQueryData = {'sigla': sigla, 'minChar': pointerlist["pitxt"].min(), 'maxChar': pointerlist["pitxt"].max()}
-                cont = self.queryHandler.textQuery(fileQueryData)
+                cont, form = self.queryHandler.textQuery(fileQueryData, True)
                 contexts.append(cont)
-                form = self.queryHandler.formatQuery(fileQueryData)
                 formats.append(form)
                 context ['piniz'] = queryresponse["piniz"].min()
                 context ['pfin'] = queryresponse["pfin"].max() 
@@ -254,26 +254,30 @@ class basicQueries:
             queryData = {'queryType': 'links', 'querySubtype': 'nota', 'ntxlocal': ntxlocal, 'pinizlocal': pinizlocal, 'pitxtlocal': pitxtlocal, 'pfinlocal': pfinlocal}
             queryresponse = self.queryHandler.query(queryData, pandas=True)
             fileQueryData = {'sigla': siglalocal, 'minChar': queryresponse["piniz"].min(), 'maxChar': queryresponse["pfin"].max()}
-            cont = self.queryHandler.textQuery(fileQueryData)
+            cont, format0 = self.queryHandler.textQuery(fileQueryData, True)
             context['nota'] = cont
+            context['format nota'] = format0
             return context
         if linkslocal == 2:
             queryData = {'queryType': 'links', 'querySubtype': 'testo_associato', 'ntxlocal': ntxlocal, 'pinizlocal': pinizlocal, 'pitxtlocal': pitxtlocal, 'pfinlocal': pfinlocal}
             queryresponse = self.queryHandler.query(queryData, pandas=True)
             fileQueryData = {'sigla': siglalocal, 'minChar': queryresponse["piniz"].min(), 'maxChar': queryresponse["pfin"].max()}
-            cont = self.queryHandler.textQuery(fileQueryData)
+            cont, format0 = self.queryHandler.textQuery(fileQueryData, True)
             context['testo associato'] = cont
+            context['format testo associato'] = format0
         if linkslocal == 3:
             queryData = {'queryType': 'links', 'querySubtype': 'nota', 'ntxlocal': ntxlocal, 'pinizlocal': pinizlocal, 'pitxtlocal': pitxtlocal, 'pfinlocal': pfinlocal}
             queryresponse = self.queryHandler.query(queryData, pandas=True)
             fileQueryData = {'sigla': siglalocal, 'minChar': queryresponse["piniz"].min(), 'maxChar': queryresponse["pfin"].max()}
-            cont = self.queryHandler.textQuery(fileQueryData)
+            cont, format0 = self.queryHandler.textQuery(fileQueryData, True)
             context['nota'] = cont
+            context['format nota'] = format0
             queryData2 = {'queryType': 'links', 'querySubtype': 'testo_associato', 'ntxlocal': ntxlocal, 'pinizlocal': pinizlocal, 'pitxtlocal': pitxtlocal, 'pfinlocal': pfinlocal}
             queryresponse2 = self.queryHandler.query(queryData2, pandas=True)
             fileQueryData2 = {'sigla': siglalocal, 'minChar': queryresponse2["piniz"].min(), 'maxChar': queryresponse2["pfin"].max()}
-            cont2 = self.queryHandler.textQuery(fileQueryData2)
+            cont2, format2 = self.queryHandler.textQuery(fileQueryData2, True)
             context['testo associato'] = cont2
+            context['format testo associato'] = format2
         return context
     
     #%% funzione contesti singoli cumulativa

+ 171 - 0
flask_be/interface_sqlite3/actual_queries.py

@@ -0,0 +1,171 @@
+def prepareQueryString(queryData):
+
+    type = queryData.get('queryType') # KeyError protected -- returns None if the key is not defined
+
+    #################
+    if type=='occ_tables':
+        return "SELECT name FROM sqlite_master WHERE type='table'"
+
+    #################
+    if type=='forma':
+        try:
+            data = queryData['data']
+            dataNorm = queryData['dataNorm']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        joinedQueryData = "'" + "' OR spec LIKE '".join(data) + "'"
+        if len(dataNorm)==0:
+            return f"SELECT spec AS forma, nocc AS occ, cod FROM form WHERE spec LIKE {joinedQueryData} ORDER BY idfor"
+        else:
+            joinedQueryDataNorm = "'" + "' OR norm LIKE '".join(dataNorm) + "'"
+            return f"SELECT DISTINCT spec AS forma, nocc AS occ, cod FROM form WHERE (spec LIKE {joinedQueryData}) OR (norm LIKE {joinedQueryDataNorm}) ORDER BY idfor"
+
+    ###################
+    elif type=='lemma':
+        try:
+            data = queryData['data']
+            dataNorm = queryData['dataNorm']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        joinedQueryData = "'" + "' OR spec LIKE '".join(data) + "'"
+        if len(dataNorm)==0:
+            return f"SELECT spec AS lemma, cat AS cat_gr, omo AS disambiguatore, nocc AS occ, cod FROM lem WHERE spec LIKE {joinedQueryData} ORDER BY idlem"
+        else:
+            joinedQueryDataNorm = "'" + "' OR norm LIKE '".join(dataNorm) + "'"
+            return f"SELECT DISTINCT spec AS lemma, cat AS cat_gr, omo AS disambiguatore, nocc AS occ, cod FROM lem WHERE (spec LIKE {joinedQueryData}) OR (norm LIKE {joinedQueryDataNorm}) ORDER BY idlem"
+
+    ########################
+    elif type=='lemmaForma':
+        try:
+            data = queryData['data']
+            dataNorm = queryData['dataNorm']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        joinedQueryData = "'" + "' OR form.spec LIKE '".join(data) + "'"
+        if len(dataNorm)==0:
+            return f"SELECT lem.spec AS lemma, lem.cat AS cat_gr, form.spec AS forma, lem.omo AS disambiguatore, pfl.nocc AS occ, lem.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod != 0 AND lem.cod = pfl.lemma WHERE lem.spec LIKE {joinedQueryData} ORDER BY lem.idlem"
+        else:
+            joinedQueryData = "'" + "' OR lem.spec LIKE '".join(data) + "'"
+            joinedQueryDataNorm = "'" + "' OR lem.norm LIKE '".join(dataNorm) + "'"
+            return f"SELECT DISTINCT lem.spec AS lemma, lem.cat AS cat_gr, form.spec AS forma, lem.omo AS disambiguatore, pfl.nocc AS occ, lem.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE (lem.spec LIKE {joinedQueryData}) OR (lem.norm LIKE {joinedQueryDataNorm}) ORDER BY lem.idlem"
+
+    ########################
+    elif type=='formaLemma':
+        try:
+            data = queryData['data']
+            dataNorm = queryData['dataNorm']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        joinedQueryData = "'" + "' OR form.spec LIKE '".join(data) + "'"
+        if len(dataNorm)==0:
+            return f"SELECT form.spec AS forma, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore, pfl.nocc AS occ, form.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE form.spec LIKE {joinedQueryData} ORDER BY form.idfor"
+        else:
+            joinedQueryDataNorm = "'" + "' OR form.norm LIKE '".join(dataNorm) + "'"
+            return f"SELECT DISTINCT form.spec AS forma, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore, pfl.nocc AS occ, form.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE (form.spec LIKE {joinedQueryData}) OR (form.norm LIKE {joinedQueryDataNorm}) ORDER BY form.idfor"
+
+    #################
+    elif type=='pfl':
+        try:
+            codList = queryData['codList']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+
+        strlist = ",".join(str(c) for c in codList)
+        return f"SELECT DISTINCT lemma as codLemma, forma as codForma FROM pfl WHERE lemma IN ({strlist})"
+
+    ###################
+    elif type=='texts':
+        try:
+            codList = queryData['codList']
+            table = queryData['table']
+            subtype = queryData['querySubtype']
+            formCodList = queryData.get('formCodList') # KeyError-safe (None if absent)
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        
+        strlist = ",".join(str(c) for c in codList)
+        if subtype==0:
+            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.cod IN ({strlist})"
+        elif subtype==1:
+            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.indlem IN ({strlist})"
+        elif subtype==2:
+            if formCodList is None:
+                return None
+            strform = ",".join(str(c) for c in formCodList)
+            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.indlem IN ({strlist}) OR (tab.indlem = 0 AND tab.cod IN ({strform}))"
+    ######################
+    elif type=='contexts':
+        try:
+            table = queryData['table']
+            ntxlocal = queryData['ntxlocal']
+            mappalocal = queryData['mappalocal']
+            parole = queryData['parole']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        return f"SELECT tab.pitxt, tab.elemlen FROM {table} AS tab WHERE tab.ntx = {ntxlocal} AND tab.mappa <= {mappalocal+int(parole/2)} AND tab.mappa >= {mappalocal-int(parole/2)}"
+
+    #################
+    elif type=='bib':
+        try:
+            row = queryData['row']
+            sigla = row['sigla']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        return f"SELECT [Anno iniziale], [Anno finale], [Data codificata], [Titolo Abbreviato], [Autore], [Titolo], [Curatore], [Data descrittiva], [Area generica], [Area specifica], [Genere], [Forma], [Tipo], IQ FROM datibib WHERE Sigla='{sigla}'"
+    
+    #################
+    elif type=='rif':
+        try:
+            row = queryData['row']
+            numorg = row['numorg']
+            ntx = row['ntx']
+        except:
+            return None
+        return f"SELECT head AS Rif_organico, full AS Rif_completo FROM org WHERE (indice='{numorg}' AND ntx='{ntx}')"
+    
+    #################
+    elif type=='highlight':
+        try:
+            row = queryData['row']
+            col = queryData['col']
+        except:
+            return None
+        return f"SELECT spec as highlight FROM form WHERE cod={row[col]}"
+    
+    #################
+    elif type =='singlecontext':
+        try:
+            subtype = queryData['querySubtype']
+            table = queryData['table']
+            parole = queryData['parole']
+            periodi = queryData['periodi']
+            brani = queryData['brani']
+            ntxlocal = queryData['ntxlocal']
+            mappalocal = queryData['mappalocal']
+            periodlocal = queryData['periodlocal']
+            numbranolocal = queryData['numbranolocal']
+        except:
+            return None
+        if subtype == 'parole':
+            return f"SELECT tab.pitxt, tab.elemlen FROM {table} AS tab WHERE tab.ntx = {ntxlocal} AND tab.mappa <= {mappalocal+int(parole/2)} AND tab.mappa >= {mappalocal-int(parole/2)}"
+        elif subtype == 'periodi':
+            return f"SELECT piniz, pfin FROM periodi WHERE ntx = {ntxlocal} AND numperiod <= {periodlocal+int(periodi/2)} AND numperiod >= {periodlocal-int(periodi/2)}"
+        elif subtype == 'brani':
+            return f"SELECT piniz, pfin FROM linkbase WHERE {ntxlocal} = ntx AND tipo = 2 AND id BETWEEN {numbranolocal-int(brani/2)} AND {numbranolocal+int(brani/2)}"
+    #################
+    elif type =='links':
+        try:
+            subtype = queryData['querySubtype']
+            ntxlocal = queryData['ntxlocal']
+            pinizlocal = queryData['pinizlocal']
+            pfinlocal = queryData['pfinlocal']
+            pitxtlocal = queryData['pitxtlocal']
+        except:
+            return None   
+        if subtype == 'nota':
+            return f"SELECT ta.ntx, ta.id, ta.piniz, ta.pfin, tb.mappain, tb.mappafin FROM linkbase AS tb INNER JOIN linknoteass AS ta ON tb.ntx = ta.ntx AND tb.id = ta.id WHERE (((tb.tipo= 1) AND (tb.ntx = {ntxlocal})) AND ((tb.piniz BETWEEN {pinizlocal} AND {pfinlocal}) OR ({pitxtlocal} BETWEEN tb.piniz AND tb.pfin)))"
+        if subtype == 'testo_associato':
+            return f"SELECT ta.ntx, ta.id, ta.piniz, ta.pfin, tb.mappain, tb.mappafin FROM linkbase AS tb INNER JOIN linknoteass AS ta ON tb.ntx = ta.ntx AND tb.id = ta.id WHERE (((tb.tipo= 2) AND (tb.ntx = {ntxlocal})) AND ((tb.piniz BETWEEN {pinizlocal} AND {pfinlocal}) OR ({pitxtlocal} BETWEEN tb.piniz AND tb.pfin)))"
+    #####
+    else:
+        raise ValueError('Unrecognized query type: ' + type)

+ 35 - 189
flask_be/interface_sqlite3/query_handlers.py

@@ -2,6 +2,8 @@ import sqlite3
 import pandas as pd
 import interface_sqlite3.encdec.de_code as dc
 
+from .actual_queries import prepareQueryString
+
 # First version
 class queryHandlerBasicSqlite:
 
@@ -58,7 +60,7 @@ class queryHandlerBasicSqlite:
 
         return results
     
-    def textQuery(self, queryData):
+    def textQuery(self, queryData, getFormatting=False):
         try:
             sigla = queryData['sigla']
             minChar = queryData['minChar']
@@ -69,26 +71,33 @@ class queryHandlerBasicSqlite:
         with open(f"{self.dbPath}/itxt/{sigla}", 'r', encoding="utf-32-le") as file1:
             file1.seek(4*minChar)
             cont = file1.read(maxChar-minChar)
-        
+
         if self.textsEncoded and self.keyRing.textKeys.get(sigla) is not None:
             key = self.keyRing.textKeys.get(sigla)
             cont = dc.decodeTextByKey(cont, key, minChar-1)
-        
-        return cont
-    
-    def formatQuery(self, queryData):
-        try:
-            sigla = queryData['sigla']
-            minChar = queryData['minChar']
-            maxChar = queryData['maxChar']
-        except:
-            return None
 
-        with open(f"{self.dbPath}/ftxt/{sigla}", 'r', encoding="utf-32-le") as file1:
-            file1.seek(4*minChar)
-            formbytes = file1.read(maxChar-minChar)
-            form = [byte for byte in formbytes]
-        return form
+        if not getFormatting:
+            return cont
+        else:
+            return cont, self.getTextFormatting(sigla, minChar, maxChar)
+
+    def getTextFormatting(self, sigla, minChar, maxChar):
+        with open(f"{self.dbPath}/ftxt/{sigla}", 'rb') as file1:
+            file1.seek(minChar-1)
+            preFormats = [char for char in file1.read(maxChar-minChar)]
+
+        # Get format CHANGES + first format; only return non-zero format sequences
+        formatChanges = [(0, preFormats[0])] + [(ind, preFormats[ind]) for ind in range(1, len(preFormats)) if preFormats[ind]!=preFormats[ind-1]]
+        formats = []
+        for ind, form in enumerate(formatChanges):
+            if form[1]>0:
+                format0 = bitToFormat(fourBits(form[1]))
+                start = form[0]
+                end = formatChanges[ind+1][0] if ind < len(formatChanges)-1 else -1
+                formats.append( {'format': format0, 'coordinates': [start, end]} )
+
+        return formats
+
     
     def encodeQuery(self, queryData):
         type = queryData.get('queryType')
@@ -122,178 +131,6 @@ class queryHandlerBasicSqlite:
 
 
 # Utilities
-def prepareQueryString(queryData):
-
-    type = queryData.get('queryType') # KeyError protected -- returns None if the key is not defined
-
-    #################
-    if type=='occ_tables':
-        return "SELECT name FROM sqlite_master WHERE type='table'"
-
-    #################
-    if type=='forma':
-        try:
-            data = queryData['data']
-            dataNorm = queryData['dataNorm']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        joinedQueryData = "'" + "' OR spec LIKE '".join(data) + "'"
-        if len(dataNorm)==0:
-            return f"SELECT spec AS forma, nocc AS occ, cod FROM form WHERE spec LIKE {joinedQueryData} ORDER BY idfor"
-        else:
-            joinedQueryDataNorm = "'" + "' OR norm LIKE '".join(dataNorm) + "'"
-            return f"SELECT DISTINCT spec AS forma, nocc AS occ, cod FROM form WHERE (spec LIKE {joinedQueryData}) OR (norm LIKE {joinedQueryDataNorm}) ORDER BY idfor"
-
-    ###################
-    elif type=='lemma':
-        try:
-            data = queryData['data']
-            dataNorm = queryData['dataNorm']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        joinedQueryData = "'" + "' OR spec LIKE '".join(data) + "'"
-        if len(dataNorm)==0:
-            return f"SELECT spec AS lemma, cat AS cat_gr, omo AS disambiguatore, nocc AS occ, cod FROM lem WHERE spec LIKE {joinedQueryData} ORDER BY idlem"
-        else:
-            joinedQueryDataNorm = "'" + "' OR norm LIKE '".join(dataNorm) + "'"
-            return f"SELECT DISTINCT spec AS lemma, cat AS cat_gr, omo AS disambiguatore, nocc AS occ, cod FROM lem WHERE (spec LIKE {joinedQueryData}) OR (norm LIKE {joinedQueryDataNorm}) ORDER BY idlem"
-
-    ########################
-    elif type=='lemmaForma':
-        try:
-            data = queryData['data']
-            dataNorm = queryData['dataNorm']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        joinedQueryData = "'" + "' OR form.spec LIKE '".join(data) + "'"
-        if len(dataNorm)==0:
-            return f"SELECT lem.spec AS lemma, lem.cat AS cat_gr, form.spec AS forma, lem.omo AS disambiguatore, pfl.nocc AS occ, lem.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod != 0 AND lem.cod = pfl.lemma WHERE lem.spec LIKE {joinedQueryData} ORDER BY lem.idlem"
-        else:
-            joinedQueryData = "'" + "' OR lem.spec LIKE '".join(data) + "'"
-            joinedQueryDataNorm = "'" + "' OR lem.norm LIKE '".join(dataNorm) + "'"
-            return f"SELECT DISTINCT lem.spec AS lemma, lem.cat AS cat_gr, form.spec AS forma, lem.omo AS disambiguatore, pfl.nocc AS occ, lem.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE (lem.spec LIKE {joinedQueryData}) OR (lem.norm LIKE {joinedQueryDataNorm}) ORDER BY lem.idlem"
-
-    ########################
-    elif type=='formaLemma':
-        try:
-            data = queryData['data']
-            dataNorm = queryData['dataNorm']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        joinedQueryData = "'" + "' OR form.spec LIKE '".join(data) + "'"
-        if len(dataNorm)==0:
-            return f"SELECT form.spec AS forma, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore, pfl.nocc AS occ, form.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE form.spec LIKE {joinedQueryData} ORDER BY form.idfor"
-        else:
-            joinedQueryDataNorm = "'" + "' OR form.norm LIKE '".join(dataNorm) + "'"
-            return f"SELECT DISTINCT form.spec AS forma, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore, pfl.nocc AS occ, form.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE (form.spec LIKE {joinedQueryData}) OR (form.norm LIKE {joinedQueryDataNorm}) ORDER BY form.idfor"
-
-    #################
-    elif type=='pfl':
-        try:
-            codList = queryData['codList']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-
-        strlist = ",".join(str(c) for c in codList)
-        return f"SELECT DISTINCT lemma as codLemma, forma as codForma FROM pfl WHERE lemma IN ({strlist})"
-
-    ###################
-    elif type=='texts':
-        try:
-            codList = queryData['codList']
-            table = queryData['table']
-            subtype = queryData['querySubtype']
-            formCodList = queryData.get('formCodList') # KeyError-safe (None if absent)
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        
-        strlist = ",".join(str(c) for c in codList)
-        if subtype==0:
-            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.cod IN ({strlist})"
-        elif subtype==1:
-            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.indlem IN ({strlist})"
-        elif subtype==2:
-            if formCodList is None:
-                return None
-            strform = ",".join(str(c) for c in formCodList)
-            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.indlem IN ({strlist}) OR (tab.indlem = 0 AND tab.cod IN ({strform}))"
-    ######################
-    elif type=='contexts':
-        try:
-            table = queryData['table']
-            ntxlocal = queryData['ntxlocal']
-            mappalocal = queryData['mappalocal']
-            parole = queryData['parole']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        return f"SELECT tab.pitxt, tab.elemlen FROM {table} AS tab WHERE tab.ntx = {ntxlocal} AND tab.mappa <= {mappalocal+int(parole/2)} AND tab.mappa >= {mappalocal-int(parole/2)}"
-
-    #################
-    elif type=='bib':
-        try:
-            row = queryData['row']
-            sigla = row['sigla']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        return f"SELECT [Anno iniziale], [Anno finale], [Data codificata], [Titolo Abbreviato], [Autore], [Titolo], [Curatore], [Data descrittiva], [Area generica], [Area specifica], [Genere], [Forma], [Tipo], IQ FROM datibib WHERE Sigla='{sigla}'"
-    
-    #################
-    elif type=='rif':
-        try:
-            row = queryData['row']
-            numorg = row['numorg']
-            ntx = row['ntx']
-        except:
-            return None
-        return f"SELECT head AS Rif_organico, full AS Rif_completo FROM org WHERE (indice='{numorg}' AND ntx='{ntx}')"
-    
-    #################
-    elif type=='highlight':
-        try:
-            row = queryData['row']
-            col = queryData['col']
-        except:
-            return None
-        return f"SELECT spec as highlight FROM form WHERE cod={row[col]}"
-    
-    #################
-    elif type =='singlecontext':
-        try:
-            subtype = queryData['querySubtype']
-            table = queryData['table']
-            parole = queryData['parole']
-            periodi = queryData['periodi']
-            brani = queryData['brani']
-            ntxlocal = queryData['ntxlocal']
-            mappalocal = queryData['mappalocal']
-            periodlocal = queryData['periodlocal']
-            numbranolocal = queryData['numbranolocal']
-        except:
-            return None
-        if subtype == 'parole':
-            return f"SELECT tab.pitxt, tab.elemlen FROM {table} AS tab WHERE tab.ntx = {ntxlocal} AND tab.mappa <= {mappalocal+int(parole/2)} AND tab.mappa >= {mappalocal-int(parole/2)}"
-        elif subtype == 'periodi':
-            return f"SELECT piniz, pfin FROM periodi WHERE ntx = {ntxlocal} AND numperiod <= {periodlocal+int(periodi/2)} AND numperiod >= {periodlocal-int(periodi/2)}"
-        elif subtype == 'brani':
-            return f"SELECT piniz, pfin FROM linkbase WHERE {ntxlocal} = ntx AND tipo = 2 AND id BETWEEN {numbranolocal-int(brani/2)} AND {numbranolocal+int(brani/2)}"
-    #################
-    elif type =='links':
-        try:
-            subtype = queryData['querySubtype']
-            ntxlocal = queryData['ntxlocal']
-            pinizlocal = queryData['pinizlocal']
-            pfinlocal = queryData['pfinlocal']
-            pitxtlocal = queryData['pitxtlocal']
-        except:
-            return None   
-        if subtype == 'nota':
-            return f"SELECT ta.ntx, ta.id, ta.piniz, ta.pfin, tb.mappain, tb.mappafin FROM linkbase AS tb INNER JOIN linknoteass AS ta ON tb.ntx = ta.ntx AND tb.id = ta.id WHERE (((tb.tipo= 1) AND (tb.ntx = {ntxlocal})) AND ((tb.piniz BETWEEN {pinizlocal} AND {pfinlocal}) OR ({pitxtlocal} BETWEEN tb.piniz AND tb.pfin)))"
-        if subtype == 'testo_associato':
-            return f"SELECT ta.ntx, ta.id, ta.piniz, ta.pfin, tb.mappain, tb.mappafin FROM linkbase AS tb INNER JOIN linknoteass AS ta ON tb.ntx = ta.ntx AND tb.id = ta.id WHERE (((tb.tipo= 2) AND (tb.ntx = {ntxlocal})) AND ((tb.piniz BETWEEN {pinizlocal} AND {pfinlocal}) OR ({pitxtlocal} BETWEEN tb.piniz AND tb.pfin)))"
-    #####
-    else:
-        raise ValueError('Unrecognized query type: ' + type)
-
 
 # Dict factory non-Pandas queries
 def dict_factory(cursor, row):
@@ -307,3 +144,12 @@ def isColumnToDecode(col):
     if col in columns or col.startswith('highlight'):
         return True
     return False
+
+
+# Formatting helper functions
+def fourBits(num):
+    aa = bin(num) # Bin converts the input to a bit string (with prefix \b)
+    return aa[2:].rjust(4, "0")
+
+def bitToFormat(str4):
+    return {'grassetto': str4[-1], 'corsivo': str4[-2], 'sottolineato': str4[-3], 'barrato': str4[-4]}

+ 171 - 0
test_suite/tests_kora_misc/interface_sqlite3/actual_queries.py

@@ -0,0 +1,171 @@
+def prepareQueryString(queryData):
+
+    type = queryData.get('queryType') # KeyError protected -- returns None if the key is not defined
+
+    #################
+    if type=='occ_tables':
+        return "SELECT name FROM sqlite_master WHERE type='table'"
+
+    #################
+    if type=='forma':
+        try:
+            data = queryData['data']
+            dataNorm = queryData['dataNorm']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        joinedQueryData = "'" + "' OR spec LIKE '".join(data) + "'"
+        if len(dataNorm)==0:
+            return f"SELECT spec AS forma, nocc AS occ, cod FROM form WHERE spec LIKE {joinedQueryData} ORDER BY idfor"
+        else:
+            joinedQueryDataNorm = "'" + "' OR norm LIKE '".join(dataNorm) + "'"
+            return f"SELECT DISTINCT spec AS forma, nocc AS occ, cod FROM form WHERE (spec LIKE {joinedQueryData}) OR (norm LIKE {joinedQueryDataNorm}) ORDER BY idfor"
+
+    ###################
+    elif type=='lemma':
+        try:
+            data = queryData['data']
+            dataNorm = queryData['dataNorm']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        joinedQueryData = "'" + "' OR spec LIKE '".join(data) + "'"
+        if len(dataNorm)==0:
+            return f"SELECT spec AS lemma, cat AS cat_gr, omo AS disambiguatore, nocc AS occ, cod FROM lem WHERE spec LIKE {joinedQueryData} ORDER BY idlem"
+        else:
+            joinedQueryDataNorm = "'" + "' OR norm LIKE '".join(dataNorm) + "'"
+            return f"SELECT DISTINCT spec AS lemma, cat AS cat_gr, omo AS disambiguatore, nocc AS occ, cod FROM lem WHERE (spec LIKE {joinedQueryData}) OR (norm LIKE {joinedQueryDataNorm}) ORDER BY idlem"
+
+    ########################
+    elif type=='lemmaForma':
+        try:
+            data = queryData['data']
+            dataNorm = queryData['dataNorm']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        joinedQueryData = "'" + "' OR form.spec LIKE '".join(data) + "'"
+        if len(dataNorm)==0:
+            return f"SELECT lem.spec AS lemma, lem.cat AS cat_gr, form.spec AS forma, lem.omo AS disambiguatore, pfl.nocc AS occ, lem.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod != 0 AND lem.cod = pfl.lemma WHERE lem.spec LIKE {joinedQueryData} ORDER BY lem.idlem"
+        else:
+            joinedQueryData = "'" + "' OR lem.spec LIKE '".join(data) + "'"
+            joinedQueryDataNorm = "'" + "' OR lem.norm LIKE '".join(dataNorm) + "'"
+            return f"SELECT DISTINCT lem.spec AS lemma, lem.cat AS cat_gr, form.spec AS forma, lem.omo AS disambiguatore, pfl.nocc AS occ, lem.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE (lem.spec LIKE {joinedQueryData}) OR (lem.norm LIKE {joinedQueryDataNorm}) ORDER BY lem.idlem"
+
+    ########################
+    elif type=='formaLemma':
+        try:
+            data = queryData['data']
+            dataNorm = queryData['dataNorm']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        joinedQueryData = "'" + "' OR form.spec LIKE '".join(data) + "'"
+        if len(dataNorm)==0:
+            return f"SELECT form.spec AS forma, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore, pfl.nocc AS occ, form.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE form.spec LIKE {joinedQueryData} ORDER BY form.idfor"
+        else:
+            joinedQueryDataNorm = "'" + "' OR form.norm LIKE '".join(dataNorm) + "'"
+            return f"SELECT DISTINCT form.spec AS forma, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore, pfl.nocc AS occ, form.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE (form.spec LIKE {joinedQueryData}) OR (form.norm LIKE {joinedQueryDataNorm}) ORDER BY form.idfor"
+
+    #################
+    elif type=='pfl':
+        try:
+            codList = queryData['codList']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+
+        strlist = ",".join(str(c) for c in codList)
+        return f"SELECT DISTINCT lemma as codLemma, forma as codForma FROM pfl WHERE lemma IN ({strlist})"
+
+    ###################
+    elif type=='texts':
+        try:
+            codList = queryData['codList']
+            table = queryData['table']
+            subtype = queryData['querySubtype']
+            formCodList = queryData.get('formCodList') # KeyError-safe (None if absent)
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        
+        strlist = ",".join(str(c) for c in codList)
+        if subtype==0:
+            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.cod IN ({strlist})"
+        elif subtype==1:
+            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.indlem IN ({strlist})"
+        elif subtype==2:
+            if formCodList is None:
+                return None
+            strform = ",".join(str(c) for c in formCodList)
+            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.indlem IN ({strlist}) OR (tab.indlem = 0 AND tab.cod IN ({strform}))"
+    ######################
+    elif type=='contexts':
+        try:
+            table = queryData['table']
+            ntxlocal = queryData['ntxlocal']
+            mappalocal = queryData['mappalocal']
+            parole = queryData['parole']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        return f"SELECT tab.pitxt, tab.elemlen FROM {table} AS tab WHERE tab.ntx = {ntxlocal} AND tab.mappa <= {mappalocal+int(parole/2)} AND tab.mappa >= {mappalocal-int(parole/2)}"
+
+    #################
+    elif type=='bib':
+        try:
+            row = queryData['row']
+            sigla = row['sigla']
+        except KeyError as err:
+            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
+        return f"SELECT [Anno iniziale], [Anno finale], [Data codificata], [Titolo Abbreviato], [Autore], [Titolo], [Curatore], [Data descrittiva], [Area generica], [Area specifica], [Genere], [Forma], [Tipo], IQ FROM datibib WHERE Sigla='{sigla}'"
+    
+    #################
+    elif type=='rif':
+        try:
+            row = queryData['row']
+            numorg = row['numorg']
+            ntx = row['ntx']
+        except:
+            return None
+        return f"SELECT head AS Rif_organico, full AS Rif_completo FROM org WHERE (indice='{numorg}' AND ntx='{ntx}')"
+    
+    #################
+    elif type=='highlight':
+        try:
+            row = queryData['row']
+            col = queryData['col']
+        except:
+            return None
+        return f"SELECT spec as highlight FROM form WHERE cod={row[col]}"
+    
+    #################
+    elif type =='singlecontext':
+        try:
+            subtype = queryData['querySubtype']
+            table = queryData['table']
+            parole = queryData['parole']
+            periodi = queryData['periodi']
+            brani = queryData['brani']
+            ntxlocal = queryData['ntxlocal']
+            mappalocal = queryData['mappalocal']
+            periodlocal = queryData['periodlocal']
+            numbranolocal = queryData['numbranolocal']
+        except:
+            return None
+        if subtype == 'parole':
+            return f"SELECT tab.pitxt, tab.elemlen FROM {table} AS tab WHERE tab.ntx = {ntxlocal} AND tab.mappa <= {mappalocal+int(parole/2)} AND tab.mappa >= {mappalocal-int(parole/2)}"
+        elif subtype == 'periodi':
+            return f"SELECT piniz, pfin FROM periodi WHERE ntx = {ntxlocal} AND numperiod <= {periodlocal+int(periodi/2)} AND numperiod >= {periodlocal-int(periodi/2)}"
+        elif subtype == 'brani':
+            return f"SELECT piniz, pfin FROM linkbase WHERE {ntxlocal} = ntx AND tipo = 2 AND id BETWEEN {numbranolocal-int(brani/2)} AND {numbranolocal+int(brani/2)}"
+    #################
+    elif type =='links':
+        try:
+            subtype = queryData['querySubtype']
+            ntxlocal = queryData['ntxlocal']
+            pinizlocal = queryData['pinizlocal']
+            pfinlocal = queryData['pfinlocal']
+            pitxtlocal = queryData['pitxtlocal']
+        except:
+            return None   
+        if subtype == 'nota':
+            return f"SELECT ta.ntx, ta.id, ta.piniz, ta.pfin, tb.mappain, tb.mappafin FROM linkbase AS tb INNER JOIN linknoteass AS ta ON tb.ntx = ta.ntx AND tb.id = ta.id WHERE (((tb.tipo= 1) AND (tb.ntx = {ntxlocal})) AND ((tb.piniz BETWEEN {pinizlocal} AND {pfinlocal}) OR ({pitxtlocal} BETWEEN tb.piniz AND tb.pfin)))"
+        if subtype == 'testo_associato':
+            return f"SELECT ta.ntx, ta.id, ta.piniz, ta.pfin, tb.mappain, tb.mappafin FROM linkbase AS tb INNER JOIN linknoteass AS ta ON tb.ntx = ta.ntx AND tb.id = ta.id WHERE (((tb.tipo= 2) AND (tb.ntx = {ntxlocal})) AND ((tb.piniz BETWEEN {pinizlocal} AND {pfinlocal}) OR ({pitxtlocal} BETWEEN tb.piniz AND tb.pfin)))"
+    #####
+    else:
+        raise ValueError('Unrecognized query type: ' + type)

+ 2 - 172
test_suite/tests_kora_misc/interface_sqlite3/query_handlers.py

@@ -2,6 +2,8 @@ import sqlite3
 import pandas as pd
 import interface_sqlite3.encdec.de_code as dc
 
+from .actual_queries import prepareQueryString
+
 # First version
 class queryHandlerBasicSqlite:
 
@@ -143,178 +145,6 @@ class queryHandlerBasicSqlite:
 
 
 # Utilities
-def prepareQueryString(queryData):
-
-    type = queryData.get('queryType') # KeyError protected -- returns None if the key is not defined
-
-    #################
-    if type=='occ_tables':
-        return "SELECT name FROM sqlite_master WHERE type='table'"
-
-    #################
-    if type=='forma':
-        try:
-            data = queryData['data']
-            dataNorm = queryData['dataNorm']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        joinedQueryData = "'" + "' OR spec LIKE '".join(data) + "'"
-        if len(dataNorm)==0:
-            return f"SELECT spec AS forma, nocc AS occ, cod FROM form WHERE spec LIKE {joinedQueryData} ORDER BY idfor"
-        else:
-            joinedQueryDataNorm = "'" + "' OR norm LIKE '".join(dataNorm) + "'"
-            return f"SELECT DISTINCT spec AS forma, nocc AS occ, cod FROM form WHERE (spec LIKE {joinedQueryData}) OR (norm LIKE {joinedQueryDataNorm}) ORDER BY idfor"
-
-    ###################
-    elif type=='lemma':
-        try:
-            data = queryData['data']
-            dataNorm = queryData['dataNorm']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        joinedQueryData = "'" + "' OR spec LIKE '".join(data) + "'"
-        if len(dataNorm)==0:
-            return f"SELECT spec AS lemma, cat AS cat_gr, omo AS disambiguatore, nocc AS occ, cod FROM lem WHERE spec LIKE {joinedQueryData} ORDER BY idlem"
-        else:
-            joinedQueryDataNorm = "'" + "' OR norm LIKE '".join(dataNorm) + "'"
-            return f"SELECT DISTINCT spec AS lemma, cat AS cat_gr, omo AS disambiguatore, nocc AS occ, cod FROM lem WHERE (spec LIKE {joinedQueryData}) OR (norm LIKE {joinedQueryDataNorm}) ORDER BY idlem"
-
-    ########################
-    elif type=='lemmaForma':
-        try:
-            data = queryData['data']
-            dataNorm = queryData['dataNorm']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        joinedQueryData = "'" + "' OR form.spec LIKE '".join(data) + "'"
-        if len(dataNorm)==0:
-            return f"SELECT lem.spec AS lemma, lem.cat AS cat_gr, form.spec AS forma, lem.omo AS disambiguatore, pfl.nocc AS occ, lem.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod != 0 AND lem.cod = pfl.lemma WHERE lem.spec LIKE {joinedQueryData} ORDER BY lem.idlem"
-        else:
-            joinedQueryData = "'" + "' OR lem.spec LIKE '".join(data) + "'"
-            joinedQueryDataNorm = "'" + "' OR lem.norm LIKE '".join(dataNorm) + "'"
-            return f"SELECT DISTINCT lem.spec AS lemma, lem.cat AS cat_gr, form.spec AS forma, lem.omo AS disambiguatore, pfl.nocc AS occ, lem.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE (lem.spec LIKE {joinedQueryData}) OR (lem.norm LIKE {joinedQueryDataNorm}) ORDER BY lem.idlem"
-
-    ########################
-    elif type=='formaLemma':
-        try:
-            data = queryData['data']
-            dataNorm = queryData['dataNorm']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        joinedQueryData = "'" + "' OR form.spec LIKE '".join(data) + "'"
-        if len(dataNorm)==0:
-            return f"SELECT form.spec AS forma, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore, pfl.nocc AS occ, form.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE form.spec LIKE {joinedQueryData} ORDER BY form.idfor"
-        else:
-            joinedQueryDataNorm = "'" + "' OR form.norm LIKE '".join(dataNorm) + "'"
-            return f"SELECT DISTINCT form.spec AS forma, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore, pfl.nocc AS occ, form.cod FROM pfl INNER JOIN form ON form.cod = pfl.forma INNER JOIN lem ON lem.cod = pfl.lemma WHERE (form.spec LIKE {joinedQueryData}) OR (form.norm LIKE {joinedQueryDataNorm}) ORDER BY form.idfor"
-
-    #################
-    elif type=='pfl':
-        try:
-            codList = queryData['codList']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-
-        strlist = ",".join(str(c) for c in codList)
-        return f"SELECT DISTINCT lemma as codLemma, forma as codForma FROM pfl WHERE lemma IN ({strlist})"
-
-    ###################
-    elif type=='texts':
-        try:
-            codList = queryData['codList']
-            table = queryData['table']
-            subtype = queryData['querySubtype']
-            formCodList = queryData.get('formCodList') # KeyError-safe (None if absent)
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        
-        strlist = ",".join(str(c) for c in codList)
-        if subtype==0:
-            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.cod IN ({strlist})"
-        elif subtype==1:
-            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.indlem IN ({strlist})"
-        elif subtype==2:
-            if formCodList is None:
-                return None
-            strform = ",".join(str(c) for c in formCodList)
-            return f"SELECT tab.cod, tab.ntx, tab.pitxt, tab.elemlen, tab.mappa, tab.numperiod, tab.links, tab.numorg, intbib.sigla, tab.vol, tab.pag, tab.riga, tab.col, tab.tipostanza, tab.stanza, tab.verso, tab.numbrano, lem.spec AS lemma, lem.cat AS cat_gr, lem.omo AS disambiguatore FROM {table} AS tab INNER JOIN intbib ON tab.ntx = intbib.ntx INNER JOIN lem ON tab.indlem = lem.cod WHERE tab.indlem IN ({strlist}) OR (tab.indlem = 0 AND tab.cod IN ({strform}))"
-    ######################
-    elif type=='contexts':
-        try:
-            table = queryData['table']
-            ntxlocal = queryData['ntxlocal']
-            mappalocal = queryData['mappalocal']
-            parole = queryData['parole']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        return f"SELECT tab.pitxt, tab.elemlen FROM {table} AS tab WHERE tab.ntx = {ntxlocal} AND tab.mappa <= {mappalocal+int(parole/2)} AND tab.mappa >= {mappalocal-int(parole/2)}"
-
-    #################
-    elif type=='bib':
-        try:
-            row = queryData['row']
-            sigla = row['sigla']
-        except KeyError as err:
-            raise KeyError('Missing required data for query type ' + type + ': ' + str(err))
-        return f"SELECT [Anno iniziale], [Anno finale], [Data codificata], [Titolo Abbreviato], [Autore], [Titolo], [Curatore], [Data descrittiva], [Area generica], [Area specifica], [Genere], [Forma], [Tipo], IQ FROM datibib WHERE Sigla='{sigla}'"
-    
-    #################
-    elif type=='rif':
-        try:
-            row = queryData['row']
-            numorg = row['numorg']
-            ntx = row['ntx']
-        except:
-            return None
-        return f"SELECT head AS Rif_organico, full AS Rif_completo FROM org WHERE (indice='{numorg}' AND ntx='{ntx}')"
-    
-    #################
-    elif type=='highlight':
-        try:
-            row = queryData['row']
-            col = queryData['col']
-        except:
-            return None
-        return f"SELECT spec as highlight FROM form WHERE cod={row[col]}"
-    
-    #################
-    elif type =='singlecontext':
-        try:
-            subtype = queryData['querySubtype']
-            table = queryData['table']
-            parole = queryData['parole']
-            periodi = queryData['periodi']
-            brani = queryData['brani']
-            ntxlocal = queryData['ntxlocal']
-            mappalocal = queryData['mappalocal']
-            periodlocal = queryData['periodlocal']
-            numbranolocal = queryData['numbranolocal']
-        except:
-            return None
-        if subtype == 'parole':
-            return f"SELECT tab.pitxt, tab.elemlen FROM {table} AS tab WHERE tab.ntx = {ntxlocal} AND tab.mappa <= {mappalocal+int(parole/2)} AND tab.mappa >= {mappalocal-int(parole/2)}"
-        elif subtype == 'periodi':
-            return f"SELECT piniz, pfin FROM periodi WHERE ntx = {ntxlocal} AND numperiod <= {periodlocal+int(periodi/2)} AND numperiod >= {periodlocal-int(periodi/2)}"
-        elif subtype == 'brani':
-            return f"SELECT piniz, pfin FROM linkbase WHERE {ntxlocal} = ntx AND tipo = 2 AND id BETWEEN {numbranolocal-int(brani/2)} AND {numbranolocal+int(brani/2)}"
-    #################
-    elif type =='links':
-        try:
-            subtype = queryData['querySubtype']
-            ntxlocal = queryData['ntxlocal']
-            pinizlocal = queryData['pinizlocal']
-            pfinlocal = queryData['pfinlocal']
-            pitxtlocal = queryData['pitxtlocal']
-        except:
-            return None   
-        if subtype == 'nota':
-            return f"SELECT ta.ntx, ta.id, ta.piniz, ta.pfin, tb.mappain, tb.mappafin FROM linkbase AS tb INNER JOIN linknoteass AS ta ON tb.ntx = ta.ntx AND tb.id = ta.id WHERE (((tb.tipo= 1) AND (tb.ntx = {ntxlocal})) AND ((tb.piniz BETWEEN {pinizlocal} AND {pfinlocal}) OR ({pitxtlocal} BETWEEN tb.piniz AND tb.pfin)))"
-        if subtype == 'testo_associato':
-            return f"SELECT ta.ntx, ta.id, ta.piniz, ta.pfin, tb.mappain, tb.mappafin FROM linkbase AS tb INNER JOIN linknoteass AS ta ON tb.ntx = ta.ntx AND tb.id = ta.id WHERE (((tb.tipo= 2) AND (tb.ntx = {ntxlocal})) AND ((tb.piniz BETWEEN {pinizlocal} AND {pfinlocal}) OR ({pitxtlocal} BETWEEN tb.piniz AND tb.pfin)))"
-    #####
-    else:
-        raise ValueError('Unrecognized query type: ' + type)
-
 
 # Dict factory non-Pandas queries
 def dict_factory(cursor, row):