Code source de geonature.core.gn_synthese.utils.process
""" functions to insert update or delete data in table gn_synthese.synthese"""fromsqlalchemy.excimportIntegrityError,ProgrammingErrorfromgeonature.utils.envimportDBfromgeonature.utils.errorsimportGeonatureApiError
[docs]defimport_from_table(schema_name,table_name,field_name,value,limit=50):""" insert and/or update data in table gn_synthese.synthese from table <schema_name>.<table_name> for all rows satisfying the condition : <field_name> = <value> """try:# TODO get nbtxt="""SELECT COUNT(*) FROM {}.{} WHERE {}::varchar = '{}'""".format(schema_name,table_name,field_name,value)nb_data=DB.engine.execute(txt).first()[0]# request : call of function gn_synthese.import_row_from_tablei=0# on procède ici par boucle pour traiter un nombre raisonnable de donnée à la foiswhilelimit*i<nb_data:txt="""SELECT gn_synthese.import_row_from_table( '{}', '{}', '{}.{}',{},{});""".format(field_name,value,schema_name,table_name,limit,i*limit# offset)DB.engine.execution_options(autocommit=True).execute(txt)i=i+1print("process synthese {} / {} ".format(min(i*limit,nb_data),nb_data))except(IntegrityError,ProgrammingError)ase:ife.orig.pgcode=="42703":raiseValueError("Undefined column : '{}.{}.{}'. \n This column is mandatory to synchronize with synthese".format(schema_name,table_name,field_name))elife.orig.pgcode=="42P01":raiseValueError("Undefined table '{}.{}'".format(schema_name,table_name))else:raiseeexceptExceptionase:raiseGeonatureApiError(""" Error while executing import_from_table with parameters : schema_name : {} table_name : {} field_name : {} value : {}. {} """.format(schema_name,table_name,field_name,value,e))