From c522a6b586749367cd73d4ec451cf65b0439bf64 Mon Sep 17 00:00:00 2001 From: Colas Geier Date: Fri, 17 Jun 2022 13:39:25 +0200 Subject: [PATCH] update fonctionnement init package --- pycen/__init__.py | 4 +- pycen/update.py | 127 +++++++++++++++++++++++----------------------- 2 files changed, 65 insertions(+), 66 deletions(-) diff --git a/pycen/__init__.py b/pycen/__init__.py index 768436b..622e92b 100644 --- a/pycen/__init__.py +++ b/pycen/__init__.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 # -*- coding: UTF-8 -*- -from .zh import * +from .zh import zh,ref_hab, ref_hydro from .bilan import * -from .update import update +from .update import update_to_sql,update_ref from .pers import pers from .sites import sites from .ps import * diff --git a/pycen/update.py b/pycen/update.py index d529911..4a393e4 100644 --- a/pycen/update.py +++ b/pycen/update.py @@ -230,74 +230,73 @@ class update_ref_table: -class update: - def update_ref(file, table, schema=None, update=False): - update_ref_table(file, table, schema, update) +def update_ref(file, table, schema=None, update=False): + update_ref_table(file, table, schema, update) - def update_to_sql(df, con, table_name, schema_name, key_name): - from sys import exit +def update_to_sql(df, con, table_name, schema_name, key_name): + from sys import exit - a = [] - b = [] - table = table_name - schema = schema_name - primary_key = key_name - pkey = __get_pkey__( - con,table_name=table,schema=schema) - type_cols = __get_dtype__( - con,table_name=table,schema=schema) + a = [] + b = [] + table = table_name + schema = schema_name + primary_key = key_name + pkey = __get_pkey__( + con,table_name=table,schema=schema) + type_cols = __get_dtype__( + con,table_name=table,schema=schema) - if not all(item in pkey['constrained_columns'] for item in df.columns): - print('Le(s) champs clé primaire "%s" ne figure pas dans le DataFrame'%pkey['constrained_columns']) - Q = input('Voulez-vous continuer la mise à jour ? (y/n) ') - if Q.lower() == 'y': - pass - else : - return print('Données non mise à jour') - # if pkey not in df.columns: - # exit('Le champs clé primaire "%s" ne figure pas dans le DataFrame'%pkey) + if not all(item in pkey['constrained_columns'] for item in df.columns): + print('Le(s) champs clé primaire "%s" ne figure pas dans le DataFrame'%pkey['constrained_columns']) + Q = input('Voulez-vous continuer la mise à jour ? (y/n) ') + if Q.lower() == 'y': + pass + else : + return print('Données non mise à jour') + # if pkey not in df.columns: + # exit('Le champs clé primaire "%s" ne figure pas dans le DataFrame'%pkey) - if isinstance(primary_key, str): - primary_key = [primary_key] - - for col in df.columns: - if col in primary_key: - b.append("t.{col}=f.{col}".format(col=col)) - else: - dtype = type_cols[col] - if hasattr(dtype,'enums') : - dty = '.'.join([dtype.schema,dtype.name]) - a.append("{col}=t.{col}::{typ}".format(col=col,typ=dty)) - else: - a.append("{col}=t.{col}".format(col=col)) - - if isinstance(df, gpd.GeoDataFrame): - df.to_postgis( - name = 'temp_table', - con = con, - schema = schema, - if_exists = 'replace', - geom_col = df.geometry.name - ) + if isinstance(primary_key, str): + primary_key = [primary_key] + + for col in df.columns: + if col in primary_key: + b.append("t.{col}=f.{col}".format(col=col)) else: - df.to_sql( - name = 'temp_table', - con = con, - schema = schema, - if_exists = 'replace', - index = False, - method = 'multi' - ) + dtype = type_cols[col] + if hasattr(dtype,'enums') : + dty = '.'.join([dtype.schema,dtype.name]) + a.append("{col}=t.{col}::{typ}".format(col=col,typ=dty)) + else: + a.append("{col}=t.{col}".format(col=col)) - update_stmt_1 = "UPDATE {sch}.{final_table} f".format(sch=schema,final_table=table) - update_stmt_2 = " FROM {sch}.temp_table t".format(sch=schema) - update_stmt_6 = " WHERE %s"%' AND '.join(b) - update_stmt_3 = " SET " - update_stmt_4 = ", ".join(a) - update_stmt_5 = update_stmt_1 + update_stmt_3 + update_stmt_4 + update_stmt_2 + update_stmt_6 + ";" - drop_stmt = "DROP TABLE {sch}.temp_table ;".format(sch=schema) - with con.begin() as cnx: - cnx.execute(update_stmt_5) - cnx.execute(drop_stmt) - return print('END update') \ No newline at end of file + if isinstance(df, gpd.GeoDataFrame): + df.to_postgis( + name = 'temp_table', + con = con, + schema = schema, + if_exists = 'replace', + geom_col = df.geometry.name + ) + else: + df.to_sql( + name = 'temp_table', + con = con, + schema = schema, + if_exists = 'replace', + index = False, + method = 'multi' + ) + + update_stmt_1 = "UPDATE {sch}.{final_table} f".format(sch=schema,final_table=table) + update_stmt_2 = " FROM {sch}.temp_table t".format(sch=schema) + update_stmt_6 = " WHERE %s"%' AND '.join(b) + update_stmt_3 = " SET " + update_stmt_4 = ", ".join(a) + update_stmt_5 = update_stmt_1 + update_stmt_3 + update_stmt_4 + update_stmt_2 + update_stmt_6 + ";" + drop_stmt = "DROP TABLE {sch}.temp_table ;".format(sch=schema) + with con.begin() as cnx: + cnx.execute(update_stmt_5) + cnx.execute(drop_stmt) + return print('END update') \ No newline at end of file