update fonctionnement init package

This commit is contained in:
Colas Geier 2022-06-17 13:39:25 +02:00
parent 52293d614b
commit c522a6b586
2 changed files with 65 additions and 66 deletions

View File

@ -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 *

View File

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