Commit 7e02b7ce authored by ARCHER's avatar ARCHER 💬

create temporary table like existing.

todo: fallback if not exist
parent 0265ea6a
......@@ -4,7 +4,7 @@ import shapely as shp
import shapely.geometry as shpg
from sqlalchemy.orm import sessionmaker
from sqlalchemy.dialects import postgresql
from sqlalchemy import text, MetaData, inspect
from sqlalchemy import text, MetaData, inspect, Table
from geoalchemy2 import select
from sqlalchemy.sql import except_all
......@@ -150,16 +150,24 @@ def to_sql(df,*args,**kwargs):
kwargs['if_exists'] = 'append'
if (kwargs['if_exists'] == 'append') and (table in metadata.tables):
# will use a temporary table as a buffer ( for upsert copy )
logger.info("Temporary table used for fast update")
pandas_engine = pd.io.sql.pandasSQL_builder(con,meta=metadata)
# create a temporary table like destination table
tmp_table = "_%s" % table
if 'dtype' in kwargs:
dtype=kwargs['dtype']
else:
dtype=None
tmp_table_pd = TemporaryPandasTable("_%s" % table, pandas_engine, frame=df, if_exists="replace", dtype=dtype)
con.execute("""
DROP TABLE IF EXISTS %s;
CREATE TEMP TABLE %s (
LIKE %s INCLUDING CONSTRAINTS INCLUDING DEFAULTS INCLUDING INDEXES
) ;""" % (tmp_table, tmp_table, table))
tmp_table_orm = Table(tmp_table, metadata, autoload=True, autoload_with=con) # metadata.tables[tmp_table]
# will use a temporary table as a buffer ( for upsert copy )
#logger.info("Temporary table used for fast update")
#pandas_engine = pd.io.sql.pandasSQL_builder(con,meta=metadata)
#tmp_table = "_%s" % table
#if 'dtype' in kwargs:
# dtype=kwargs['dtype']
#else:
# dtype=None
#tmp_table_pd = TemporaryPandasTable("_%s" % table, pandas_engine, frame=df, if_exists="replace", dtype=dtype)
#tmp_table_pd.create()
......@@ -188,7 +196,7 @@ def to_sql(df,*args,**kwargs):
table_orm=metadata.tables[table]
tmp_table_orm = tmp_table_pd.table
#tmp_table_orm = tmp_table_pd.table
logger.debug("merging tmp table")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment