"""General functions for SQL query building."""importsqlite3frompygapsimportloggerfrompygaps.utilities.exceptionsimportParsingError
[docs]defdb_execute_general(statement:str,pth:str,verbose:bool=False,):""" Execute general SQL statements. Parameters ---------- statement : str SQL statement to execute. pth : str Path where the database is located. verbose : bool Print out extra information. """# Attempt to connecttry:withsqlite3.connect(pth)asdb:# Get a cursor objectcursor=db.cursor()cursor.execute('PRAGMA foreign_keys = ON')# Check if table does not exist and create itcursor.executescript(statement)# Catch the exceptionexceptsqlite3.Errorase_info:logger.info(f"Unable to execute statement: \n{statement}")raiseParsingErrorfrome_info
[docs]defbuild_update(table:str,to_set:list,where:list,prefix:str=None,):""" Build an update request. Parameters ---------- table : str Table where query will be directed. to_set: iterable The list of columns to update. where: iterable The list of conditions to constrain the query. prefix: str, optional The prefix to introduce to the second part of the constraint. Returns ------- str Built query string. """return(f"UPDATE \"{table}\" SET "+", ".join(f"{w} = :{w}"forwinto_set)+" WHERE "+" AND ".join(f"{w} = :{prefixor''}{w}"forwinwhere))
[docs]defbuild_insert(table:str,to_insert:list):""" Build an insert request. Parameters ---------- table : str Table where query will be directed. to_insert: iterable The list of columns where the values will be inserted. Returns ------- str Built query string. """return(f"INSERT INTO \"{table}\" ("+", ".join(f"{w}"forwinto_insert)+") VALUES ("+", ".join(f":{w}"forwinto_insert)+")")
[docs]defbuild_select(table:str,to_select:list,where:list=None,):""" Build a select request. Parameters ---------- table : str Table where query will be directed. to_set: iterable The list of columns to select. where: iterable The list of conditions to constrain the query. Returns ------- str Built query string. """ifwhere:return("SELECT "+", ".join(f"{w}"forwinto_select)+f" FROM \"{table}\" WHERE "+" AND ".join(f"{w} = :{w}"forwinwhere))return("SELECT "+", ".join(f"{w}"forwinto_select)+f" FROM \"{table}\"")
[docs]defbuild_select_unnamed(table:str,to_select:list,where:list,join:str="AND"):""" Build an select request with multiple parameters. Parameters ---------- table : str Table where query will be directed. to_set : iterable The list of columns to select where : iterable The list of conditions to constrain the query. join : str The joining clause of the parameters. Returns ------- str Built query string. """return("SELECT "+", ".join(f"{w}"forwinto_select)+f" FROM \"{table}\" WHERE "+(" "+join+" ").join(f"{w} = ?"forwinwhere))
[docs]defbuild_delete(table:str,where:list):""" Build a delete request. Parameters ---------- table : str Table where query will be directed. where: iterable The list of conditions to constrain the query. Returns ------- str Built query string. """returnf"DELETE FROM \"{table}\" WHERE "+" AND ".join(f"{w} = :{w}"forwinwhere)
[docs]defcheck_SQL_bool(val):"""Check if a value is a bool. Useful for storage."""ifvalin['TRUE','FALSE']:ifval=='TRUE':returnTruereturnFalsereturnval
SUPORTED_TYPES=[bool,int,float,str]
[docs]defcheck_SQL_python_type(val):"""Convert between the database string dtype and a python data type."""forsupported_typeinSUPORTED_TYPES:ifval==supported_type.__name__:returnsupported_type
[docs]deffind_SQL_python_type(val):"""Convert between a python data type and a database string."""forsupported_typeinSUPORTED_TYPES:ifisinstance(val,supported_type):returnsupported_type.__name__raiseParsingError(f"Cannot store data of type {type(val)} in the database.")