update fonctionnement init package
This commit is contained in:
parent
52293d614b
commit
c522a6b586
@ -1,9 +1,9 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
from .zh import *
|
from .zh import zh,ref_hab, ref_hydro
|
||||||
from .bilan import *
|
from .bilan import *
|
||||||
from .update import update
|
from .update import update_to_sql,update_ref
|
||||||
from .pers import pers
|
from .pers import pers
|
||||||
from .sites import sites
|
from .sites import sites
|
||||||
from .ps import *
|
from .ps import *
|
||||||
|
|||||||
127
pycen/update.py
127
pycen/update.py
@ -230,74 +230,73 @@ class update_ref_table:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
class update:
|
def update_ref(file, table, schema=None, update=False):
|
||||||
def update_ref(file, table, schema=None, update=False):
|
update_ref_table(file, table, schema, update)
|
||||||
update_ref_table(file, table, schema, update)
|
|
||||||
|
|
||||||
|
|
||||||
def update_to_sql(df, con, table_name, schema_name, key_name):
|
def update_to_sql(df, con, table_name, schema_name, key_name):
|
||||||
from sys import exit
|
from sys import exit
|
||||||
|
|
||||||
a = []
|
a = []
|
||||||
b = []
|
b = []
|
||||||
table = table_name
|
table = table_name
|
||||||
schema = schema_name
|
schema = schema_name
|
||||||
primary_key = key_name
|
primary_key = key_name
|
||||||
pkey = __get_pkey__(
|
pkey = __get_pkey__(
|
||||||
con,table_name=table,schema=schema)
|
con,table_name=table,schema=schema)
|
||||||
type_cols = __get_dtype__(
|
type_cols = __get_dtype__(
|
||||||
con,table_name=table,schema=schema)
|
con,table_name=table,schema=schema)
|
||||||
|
|
||||||
if not all(item in pkey['constrained_columns'] for item in df.columns):
|
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'])
|
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) ')
|
Q = input('Voulez-vous continuer la mise à jour ? (y/n) ')
|
||||||
if Q.lower() == 'y':
|
if Q.lower() == 'y':
|
||||||
pass
|
pass
|
||||||
else :
|
else :
|
||||||
return print('Données non mise à jour')
|
return print('Données non mise à jour')
|
||||||
# if pkey not in df.columns:
|
# if pkey not in df.columns:
|
||||||
# exit('Le champs clé primaire "%s" ne figure pas dans le DataFrame'%pkey)
|
# exit('Le champs clé primaire "%s" ne figure pas dans le DataFrame'%pkey)
|
||||||
|
|
||||||
if isinstance(primary_key, str):
|
if isinstance(primary_key, str):
|
||||||
primary_key = [primary_key]
|
primary_key = [primary_key]
|
||||||
|
|
||||||
for col in df.columns:
|
for col in df.columns:
|
||||||
if col in primary_key:
|
if col in primary_key:
|
||||||
b.append("t.{col}=f.{col}".format(col=col))
|
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
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
df.to_sql(
|
dtype = type_cols[col]
|
||||||
name = 'temp_table',
|
if hasattr(dtype,'enums') :
|
||||||
con = con,
|
dty = '.'.join([dtype.schema,dtype.name])
|
||||||
schema = schema,
|
a.append("{col}=t.{col}::{typ}".format(col=col,typ=dty))
|
||||||
if_exists = 'replace',
|
else:
|
||||||
index = False,
|
a.append("{col}=t.{col}".format(col=col))
|
||||||
method = 'multi'
|
|
||||||
)
|
|
||||||
|
|
||||||
update_stmt_1 = "UPDATE {sch}.{final_table} f".format(sch=schema,final_table=table)
|
if isinstance(df, gpd.GeoDataFrame):
|
||||||
update_stmt_2 = " FROM {sch}.temp_table t".format(sch=schema)
|
df.to_postgis(
|
||||||
update_stmt_6 = " WHERE %s"%' AND '.join(b)
|
name = 'temp_table',
|
||||||
update_stmt_3 = " SET "
|
con = con,
|
||||||
update_stmt_4 = ", ".join(a)
|
schema = schema,
|
||||||
update_stmt_5 = update_stmt_1 + update_stmt_3 + update_stmt_4 + update_stmt_2 + update_stmt_6 + ";"
|
if_exists = 'replace',
|
||||||
drop_stmt = "DROP TABLE {sch}.temp_table ;".format(sch=schema)
|
geom_col = df.geometry.name
|
||||||
with con.begin() as cnx:
|
)
|
||||||
cnx.execute(update_stmt_5)
|
else:
|
||||||
cnx.execute(drop_stmt)
|
df.to_sql(
|
||||||
return print('END update')
|
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')
|
||||||
Loading…
x
Reference in New Issue
Block a user