from osgeo import ogr

from rosreestrXml.ui.tools.db_helper.dbRsc import DbRsc

path_sourcer=r"E:\Download\RossRestr\newEGRN\test_import.gpkg"
name_layer='view_land_record'
name_layer_new='spatial_land_record'
db=DbRsc(path_sourcer)
''' обновление структуры табли'''
sql="select name AS column_name,type AS column_type FROM  pragma_table_info('"+name_layer+"')"
rows=db.select(sql)
def_names=[]
for row in rows:
    def_names.append({"name":row[0],'type':row[1]})
db.close()
ds=ogr.Open(path_sourcer,1)
sql_base="select parent_row_id from "+name_layer
layer_select=ds.ExecuteSQL(sql_base)
for ft in layer_select:
    id_row=ft.GetFID()
    for name_def in def_names:
        if name_def['name'] == 'parent_row_id' or name_def['name'] == 'geom':
            continue
        sqlupdate = "update " + name_layer_new + " Set " + name_def['name'] + "= ( select " + name_def[
            'name'] + " from " + name_layer + " where parent_row_id=" + str(
            id_row) + ") where " + name_layer_new + ".parent_row_id=" + str(id_row)
        temp_layer=ds.ExecuteSQL(sqlupdate)
        jkl=0
'''
rows=db.select(sql_base)
for row in rows:
    id_row=row[0]
    for name_def in def_names:
        sqlupdate="update "+name_layer_new+" Set "+name_def['name']+"= ( select "+name_def['name']+" from "+name_layer+" where parent_row_id="+str(id_row)+") where "+name_layer_new+".parent_row_id="+str(id_row)
        db.runSqlNoResult(sqlupdate)
'''
ds.FlushCache()
ds.Destroy()
ds=None
print("end")