|
...
|
...
|
@@ -35,7 +35,7 @@ class Api(ApiTemplate): |
|
35
|
35
|
# 初始化task
|
|
36
|
36
|
task_guid = uuid.uuid1().__str__()
|
|
37
|
37
|
|
|
38
|
|
- refresh_process = multiprocessing.Process(target=self.table_refresh,args=(database,task_guid))
|
|
|
38
|
+ refresh_process = multiprocessing.Process(target=self.table_refresh,args=(database,task_guid,self.para.get("creator")))
|
|
39
|
39
|
refresh_process.start()
|
|
40
|
40
|
|
|
41
|
41
|
task = Task(guid=task_guid,
|
|
...
|
...
|
@@ -61,7 +61,7 @@ class Api(ApiTemplate): |
|
61
|
61
|
return res
|
|
62
|
62
|
|
|
63
|
63
|
|
|
64
|
|
- def table_refresh(self,database,task_guid):
|
|
|
64
|
+ def table_refresh(self,database,task_guid,creator):
|
|
65
|
65
|
|
|
66
|
66
|
pg_ds =None
|
|
67
|
67
|
sys_ds =None
|
|
...
|
...
|
@@ -82,6 +82,8 @@ class Api(ApiTemplate): |
|
82
|
82
|
|
|
83
|
83
|
sys_session = PGUtil.get_db_session(configure.SQLALCHEMY_DATABASE_URI)
|
|
84
|
84
|
sys_ds = PGUtil.open_pg_data_source(0,configure.SQLALCHEMY_DATABASE_URI)
|
|
|
85
|
+ # 实体库连接
|
|
|
86
|
+ data_session: Session = PGUtil.get_db_session(DES.decode(database.sqlalchemy_uri))
|
|
85
|
87
|
|
|
86
|
88
|
this_time = datetime.datetime.now()
|
|
87
|
89
|
database_guid = database.guid
|
|
...
|
...
|
@@ -98,21 +100,17 @@ class Api(ApiTemplate): |
|
98
|
100
|
|
|
99
|
101
|
# 更新空间表
|
|
100
|
102
|
# 增加表
|
|
101
|
|
- db_tables_names = self.add_spatail_table(database, pg_ds, sys_session,spatial_tables_names, this_time,db_tuple)# 实体库中空间表名
|
|
|
103
|
+ db_tables_names = self.add_spatail_table(database, pg_ds, data_session,sys_session,spatial_tables_names, this_time,db_tuple,creator)# 实体库中空间表名
|
|
102
|
104
|
|
|
103
|
105
|
# 删除/修改表
|
|
104
|
|
- self.edit_spatial_table(pg_ds, sys_session,spatial_tables, db_tables_names, this_time,db_tuple)
|
|
|
106
|
+ self.edit_spatial_table(pg_ds, data_session, sys_session,spatial_tables, db_tables_names, this_time,db_tuple)
|
|
105
|
107
|
|
|
106
|
108
|
# 空间表处理完毕
|
|
107
|
109
|
sys_session.commit()
|
|
108
|
110
|
|
|
109
|
111
|
|
|
110
|
|
- # 空间表处理完毕
|
|
111
|
|
- sys_session.commit()
|
|
112
|
|
-
|
|
113
|
112
|
# 注册普通表
|
|
114
|
|
- # 实体库连接
|
|
115
|
|
- data_session: Session = PGUtil.get_db_session(DES.decode(database.sqlalchemy_uri))
|
|
|
113
|
+
|
|
116
|
114
|
|
|
117
|
115
|
# 处理后空间表
|
|
118
|
116
|
spatial_tables = sys_session.query(Table).order_by(Table.create_time.desc()).filter_by(database_guid=database_guid).filter(
|
|
...
|
...
|
@@ -123,7 +121,7 @@ class Api(ApiTemplate): |
|
123
|
121
|
# 原有普通表
|
|
124
|
122
|
common_tables = sys_session.query(Table).order_by(Table.create_time.desc()).filter_by(database_guid=database_guid).filter(
|
|
125
|
123
|
Table.table_type == 0).all()
|
|
126
|
|
- # 原有普通表 名
|
|
|
124
|
+ # 原有普通表名
|
|
127
|
125
|
origin_common_tables_name = [table.name for table in common_tables]
|
|
128
|
126
|
|
|
129
|
127
|
# 现有普通表
|
|
...
|
...
|
@@ -140,7 +138,8 @@ class Api(ApiTemplate): |
|
140
|
138
|
# 增加新普通表
|
|
141
|
139
|
|
|
142
|
140
|
self.add_common_table(data_session, sys_session, database_guid, real_common_tables_name, origin_common_tables_name,
|
|
143
|
|
- this_time,db_tuple)
|
|
|
141
|
+ this_time,db_tuple,creator)
|
|
|
142
|
+ sys_session.commit()
|
|
144
|
143
|
|
|
145
|
144
|
# 删除、修改普通表
|
|
146
|
145
|
self.edit_common_table(data_session,sys_session, database_guid, real_common_tables_name, origin_common_tables_name,
|
|
...
|
...
|
@@ -173,7 +172,7 @@ class Api(ApiTemplate): |
|
173
|
172
|
sys_ds.Destroy()
|
|
174
|
173
|
return result
|
|
175
|
174
|
|
|
176
|
|
- def add_spatail_table(self,database,pg_ds,sys_session,spatial_tables_names,this_time,db_tuple):
|
|
|
175
|
+ def add_spatail_table(self,database,pg_ds,data_session,sys_session,spatial_tables_names,this_time,db_tuple,creator):
|
|
177
|
176
|
'''
|
|
178
|
177
|
注册新增空间表
|
|
179
|
178
|
:param database:
|
|
...
|
...
|
@@ -202,40 +201,68 @@ class Api(ApiTemplate): |
|
202
|
201
|
if layer.GetName().__contains__("_vacuate_"):
|
|
203
|
202
|
continue
|
|
204
|
203
|
|
|
|
204
|
+ # # 没有权限的表跳过
|
|
|
205
|
+ # if not PGUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], pg_ds):
|
|
|
206
|
+ # StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
|
|
|
207
|
+ # continue
|
|
|
208
|
+
|
|
205
|
209
|
# 没有权限的表跳过
|
|
206
|
|
- if not PGUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], pg_ds):
|
|
|
210
|
+ if not SQLUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], data_session):
|
|
207
|
211
|
StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
|
|
208
|
212
|
continue
|
|
209
|
213
|
|
|
210
|
|
-
|
|
211
|
214
|
# 范围统计和数量统计以100w为界限
|
|
212
|
|
- query_count_layer: Layer = pg_ds.ExecuteSQL(
|
|
213
|
|
- '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(
|
|
214
|
|
- l_name))
|
|
|
215
|
+ query_count_layer = data_session.execute('''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(l_name)).fetchone()
|
|
|
216
|
+ feature_count = query_count_layer[0]
|
|
|
217
|
+
|
|
|
218
|
+ # query_count_layer: Layer = pg_ds.ExecuteSQL(
|
|
|
219
|
+ # '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(
|
|
|
220
|
+ # l_name))
|
|
|
221
|
+
|
|
|
222
|
+ # feature_count = query_count_layer.GetFeature(0).GetField("ec")
|
|
|
223
|
+
|
|
|
224
|
+
|
|
|
225
|
+
|
|
215
|
226
|
|
|
216
|
|
- feature_count = query_count_layer.GetFeature(0).GetField("ec")
|
|
217
|
227
|
# 要素少于100w可以精确统计
|
|
218
|
228
|
if feature_count < 1000000:
|
|
219
|
229
|
feature_count = layer.GetFeatureCount()
|
|
220
|
230
|
ext = layer.GetExtent()
|
|
221
|
231
|
else:
|
|
222
|
|
- query_ext_layer: Layer = pg_ds.ExecuteSQL(
|
|
223
|
|
- "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,
|
|
224
|
|
- layer.GetGeometryColumn()))
|
|
225
|
|
- ext = query_ext_layer.GetExtent()
|
|
|
232
|
+ # query_ext_layer: Layer = pg_ds.ExecuteSQL(
|
|
|
233
|
+ # "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,
|
|
|
234
|
+ # layer.GetGeometryColumn()))
|
|
|
235
|
+ #
|
|
|
236
|
+ # ext = query_ext_layer.GetExtent()
|
|
|
237
|
+ try:
|
|
|
238
|
+
|
|
|
239
|
+ ext = data_session.execute("select st_xmin(box3d),st_xmax(box3d),st_ymin(box3d),st_ymax(box3d) from (select box3d(geometry(ST_EstimatedExtent('public', '{}','{}')))) as b".format(l_name,
|
|
|
240
|
+ layer.GetGeometryColumn())).fetchone()
|
|
|
241
|
+ except:
|
|
|
242
|
+ data_session.rollback()
|
|
|
243
|
+ ext = layer.GetExtent()
|
|
|
244
|
+
|
|
|
245
|
+
|
|
226
|
246
|
if ext[0] < 360:
|
|
227
|
247
|
ext = [round(e, 6) for e in ext]
|
|
228
|
248
|
else:
|
|
229
|
249
|
ext = [round(e, 2) for e in ext]
|
|
230
|
250
|
extent = "{},{},{},{}".format(ext[0], ext[1], ext[2], ext[3])
|
|
231
|
251
|
|
|
232
|
|
- StructurePrint().print("空间表增加!")
|
|
|
252
|
+ StructurePrint().print("空间表{}增加!".format(l_name))
|
|
233
|
253
|
|
|
234
|
254
|
geom_type = GeometryAdapter.get_geometry_type(layer)
|
|
235
|
|
- except:
|
|
236
|
|
- StructurePrint().print("表{}注册失败!".format(l_name), "warn")
|
|
|
255
|
+ except Exception as e:
|
|
|
256
|
+ StructurePrint().print("{}".format(e.__str__()+":"+ traceback.format_exc()), "warn")
|
|
|
257
|
+ StructurePrint().print("空间表{}新增失败!".format(l_name), "warn")
|
|
|
258
|
+ data_session.rollback()
|
|
237
|
259
|
continue
|
|
238
|
260
|
|
|
|
261
|
+ olds = sys_session.query(Table).filter_by(name=l_name,database_guid=database.guid).all()
|
|
|
262
|
+ for old in olds:
|
|
|
263
|
+ sys_session.delete(old)
|
|
|
264
|
+
|
|
|
265
|
+
|
|
239
|
266
|
table_guid = uuid.uuid1().__str__()
|
|
240
|
267
|
table = Table(guid=table_guid,
|
|
241
|
268
|
database_guid=database.guid,
|
|
...
|
...
|
@@ -243,6 +270,7 @@ class Api(ApiTemplate): |
|
243
|
270
|
name=layer.GetName(), create_time=this_time, update_time=this_time,
|
|
244
|
271
|
table_type=GeometryAdapter.get_table_type(geom_type),
|
|
245
|
272
|
extent=extent,
|
|
|
273
|
+ creator=creator,
|
|
246
|
274
|
feature_count=feature_count
|
|
247
|
275
|
)
|
|
248
|
276
|
sys_session.add(table)
|
|
...
|
...
|
@@ -301,158 +329,136 @@ class Api(ApiTemplate): |
|
301
|
329
|
|
|
302
|
330
|
|
|
303
|
331
|
|
|
304
|
|
- def edit_spatial_table(self,pg_ds,sys_session,spatial_tables,db_tables_names,this_time,db_tuple):
|
|
305
|
|
-
|
|
306
|
|
-
|
|
|
332
|
+ def edit_spatial_table(self,pg_ds, data_session,sys_session,spatial_tables,db_tables_names,this_time,db_tuple):
|
|
307
|
333
|
|
|
308
|
334
|
for table in spatial_tables:
|
|
309
|
|
-
|
|
310
|
|
- # 删除表
|
|
311
|
|
- if table.name not in db_tables_names:
|
|
312
|
|
- StructurePrint().print("空间表减少!")
|
|
313
|
|
- sys_session.delete(table)
|
|
314
|
|
- # 修改表
|
|
315
|
|
- else:
|
|
316
|
|
- layer: Layer = pg_ds.GetLayerByName(table.name)
|
|
317
|
|
- l_name = layer.GetName()
|
|
318
|
|
-
|
|
319
|
|
- # 只注册public的空间表,其他表空间的表名会有.
|
|
320
|
|
- if layer.GetName().__contains__("."):
|
|
321
|
|
- continue
|
|
322
|
|
-
|
|
323
|
|
- if layer.GetName().__contains__("_vacuate_"):
|
|
324
|
|
- continue
|
|
325
|
|
-
|
|
326
|
|
- # 没有权限的表跳过
|
|
327
|
|
- if not PGUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], pg_ds):
|
|
328
|
|
- StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
|
|
|
335
|
+ try:
|
|
|
336
|
+ # 删除表
|
|
|
337
|
+ if table.name not in db_tables_names:
|
|
|
338
|
+ StructurePrint().print("空间表减少!")
|
|
329
|
339
|
sys_session.delete(table)
|
|
330
|
|
- continue
|
|
|
340
|
+ # 修改表
|
|
|
341
|
+ else:
|
|
|
342
|
+ layer: Layer = pg_ds.GetLayerByName(table.name)
|
|
|
343
|
+ l_name = layer.GetName()
|
|
331
|
344
|
|
|
332
|
|
- columns = table.relate_columns
|
|
333
|
|
- columns_names = [column.name for column in columns]
|
|
334
|
|
- feature_defn: FeatureDefn = layer.GetLayerDefn()
|
|
335
|
|
- db_columns_names = []
|
|
|
345
|
+ # 只注册public的空间表,其他表空间的表名会有.
|
|
|
346
|
+ if layer.GetName().__contains__("."):
|
|
|
347
|
+ continue
|
|
336
|
348
|
|
|
337
|
|
- # 增加列
|
|
338
|
|
- for i in range(feature_defn.GetFieldCount()):
|
|
339
|
|
- field_defn: FieldDefn = feature_defn.GetFieldDefn(i)
|
|
340
|
|
- field_name = field_defn.GetName()
|
|
341
|
|
- db_columns_names.append(field_name)
|
|
342
|
|
-
|
|
343
|
|
- if field_name not in columns_names:
|
|
344
|
|
- StructurePrint().print("{}空间表属性增加!".format(table.name))
|
|
345
|
|
- field_alias = field_name if field_defn.GetAlternativeName() is None or field_defn.GetAlternativeName().__eq__(
|
|
346
|
|
- "") else field_defn.GetAlternativeName()
|
|
347
|
|
- column = Columns(guid=uuid.uuid1().__str__(), table_guid=table.guid,
|
|
348
|
|
- name=field_name, alias=field_alias, create_time=this_time,
|
|
349
|
|
- update_time=this_time)
|
|
350
|
|
- sys_session.add(column)
|
|
|
349
|
+ if layer.GetName().__contains__("_vacuate_"):
|
|
|
350
|
+ continue
|
|
351
|
351
|
|
|
352
|
|
- # 删除列
|
|
353
|
|
- for column in columns:
|
|
354
|
|
- if column.name not in db_columns_names:
|
|
355
|
|
- StructurePrint().print("{}空间表属性减少!".format(table.name))
|
|
356
|
|
- sys_session.delete(column)
|
|
357
|
|
-
|
|
358
|
|
- # 范围统计和数量统计以100w为界限
|
|
359
|
|
- query_count_layer: Layer = pg_ds.ExecuteSQL(
|
|
360
|
|
- '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(
|
|
361
|
|
- l_name))
|
|
362
|
|
- feature_count = query_count_layer.GetFeature(0).GetField("ec")
|
|
363
|
|
- # 要素少于100w可以精确统计
|
|
364
|
|
- if feature_count < 1000000:
|
|
365
|
|
- feature_count = layer.GetFeatureCount()
|
|
366
|
|
- ext = layer.GetExtent()
|
|
|
352
|
+ # # 没有权限的表跳过
|
|
|
353
|
+ # if not PGUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], pg_ds):
|
|
|
354
|
+ # StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
|
|
|
355
|
+ # sys_session.delete(table)
|
|
|
356
|
+ # continue
|
|
367
|
357
|
|
|
368
|
|
- else:
|
|
369
|
|
- query_ext_layer: Layer = pg_ds.ExecuteSQL(
|
|
370
|
|
- "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,
|
|
371
|
|
- layer.GetGeometryColumn()))
|
|
372
|
|
- ext = query_ext_layer.GetExtent()
|
|
373
|
|
- if ext[0] < 360:
|
|
374
|
|
- ext = [round(e, 6) for e in ext]
|
|
375
|
|
- else:
|
|
376
|
|
- ext = [round(e, 2) for e in ext]
|
|
377
|
|
- extent = "{},{},{},{}".format(ext[0], ext[1], ext[2], ext[3])
|
|
|
358
|
+ # 没有权限的表跳过
|
|
|
359
|
+ if not SQLUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], data_session):
|
|
|
360
|
+ StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
|
|
|
361
|
+ sys_session.delete(table)
|
|
|
362
|
+ continue
|
|
378
|
363
|
|
|
379
|
|
- # 修改要素量
|
|
380
|
|
- if not table.feature_count.__eq__(feature_count):
|
|
381
|
|
- StructurePrint().print("{}空间表要素!".format(table.name))
|
|
382
|
|
- sys_session.query(Table).filter_by(guid=table.guid).update({"feature_count": feature_count,
|
|
383
|
|
- "extent": extent})
|
|
|
364
|
+ columns = table.relate_columns
|
|
|
365
|
+ columns_names = [column.name for column in columns]
|
|
|
366
|
+ feature_defn: FeatureDefn = layer.GetLayerDefn()
|
|
|
367
|
+ db_columns_names = []
|
|
|
368
|
+
|
|
|
369
|
+ # 增加列
|
|
|
370
|
+ for i in range(feature_defn.GetFieldCount()):
|
|
|
371
|
+ field_defn: FieldDefn = feature_defn.GetFieldDefn(i)
|
|
|
372
|
+ field_name = field_defn.GetName()
|
|
|
373
|
+ db_columns_names.append(field_name)
|
|
|
374
|
+
|
|
|
375
|
+ if field_name not in columns_names:
|
|
|
376
|
+ StructurePrint().print("{}空间表属性增加!".format(table.name))
|
|
|
377
|
+ field_alias = field_name if field_defn.GetAlternativeName() is None or field_defn.GetAlternativeName().__eq__(
|
|
|
378
|
+ "") else field_defn.GetAlternativeName()
|
|
|
379
|
+ column = Columns(guid=uuid.uuid1().__str__(), table_guid=table.guid,
|
|
|
380
|
+ name=field_name, alias=field_alias, create_time=this_time,
|
|
|
381
|
+ update_time=this_time)
|
|
|
382
|
+ sys_session.add(column)
|
|
384
|
383
|
|
|
|
384
|
+ # 删除列
|
|
|
385
|
+ for column in columns:
|
|
|
386
|
+ if column.name not in db_columns_names:
|
|
|
387
|
+ StructurePrint().print("{}空间表属性减少!".format(table.name))
|
|
|
388
|
+ sys_session.delete(column)
|
|
385
|
389
|
|
|
386
|
|
- def add_common_table(self,data_session,sys_session,database_guid,real_common_tables_name,origin_common_tables_name,this_time,db_tuple):
|
|
387
|
|
- for table_name in real_common_tables_name:
|
|
388
|
|
- if table_name not in origin_common_tables_name:
|
|
389
|
|
- StructurePrint().print("{}非空间表增加!".format(table_name))
|
|
390
|
|
- table_guid = uuid.uuid1().__str__()
|
|
|
390
|
+ # 范围统计和数量统计以100w为界限
|
|
|
391
|
+ # query_count_layer: Layer = pg_ds.ExecuteSQL(
|
|
|
392
|
+ # '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(
|
|
|
393
|
+ # l_name))
|
|
|
394
|
+ # feature_count = query_count_layer.GetFeature(0).GetField("ec")
|
|
391
|
395
|
|
|
|
396
|
+ query_count_layer = data_session.execute('''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(l_name)).fetchone()
|
|
|
397
|
+ feature_count = query_count_layer[0]
|
|
392
|
398
|
|
|
393
|
|
- # 没有权限的表跳过
|
|
394
|
|
- if not SQLUtil.check_table_privilege(table_name, "SELECT", db_tuple[0], data_session):
|
|
395
|
|
- StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], table_name), "warn")
|
|
396
|
|
- continue
|
|
|
399
|
+ # 要素少于100w可以精确统计
|
|
|
400
|
+ if feature_count < 1000000:
|
|
|
401
|
+ feature_count = layer.GetFeatureCount()
|
|
|
402
|
+ ext = layer.GetExtent()
|
|
397
|
403
|
|
|
398
|
|
- count = data_session.execute('select count(*) from "{}"'.format(table_name)).fetchone()[0]
|
|
|
404
|
+ else:
|
|
|
405
|
+ # query_ext_layer: Layer = pg_ds.ExecuteSQL(
|
|
|
406
|
+ # "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,
|
|
|
407
|
+ # layer.GetGeometryColumn()))
|
|
|
408
|
+ # ext = query_ext_layer.GetExtent()
|
|
|
409
|
+ try:
|
|
|
410
|
+ ext = data_session.execute("select st_xmin(box3d),st_xmax(box3d),st_ymin(box3d),st_ymax(box3d) from (select box3d(geometry(ST_EstimatedExtent('public', '{}','{}')))) as b".format(l_name,
|
|
|
411
|
+ layer.GetGeometryColumn())).fetchone()
|
|
|
412
|
+ except:
|
|
|
413
|
+ data_session.rollback()
|
|
|
414
|
+ ext = layer.GetExtent()
|
|
399
|
415
|
|
|
400
|
|
- table = Table(guid=table_guid,
|
|
401
|
|
- database_guid=database_guid,
|
|
402
|
|
- name=table_name, create_time=this_time, update_time=this_time,
|
|
403
|
|
- table_type=0,
|
|
404
|
|
- feature_count=count
|
|
405
|
|
- )
|
|
|
416
|
+ if ext[0] < 360:
|
|
|
417
|
+ ext = [round(e, 6) for e in ext]
|
|
|
418
|
+ else:
|
|
|
419
|
+ ext = [round(e, 2) for e in ext]
|
|
|
420
|
+ extent = "{},{},{},{}".format(ext[0], ext[1], ext[2], ext[3])
|
|
406
|
421
|
|
|
407
|
|
- sys_session.add(table)
|
|
|
422
|
+ # 修改要素量
|
|
|
423
|
+ if not table.feature_count.__eq__(feature_count):
|
|
|
424
|
+ StructurePrint().print("{}空间表要素!".format(table.name))
|
|
|
425
|
+ sys_session.query(Table).filter_by(guid=table.guid).update({"feature_count": feature_count,
|
|
|
426
|
+ "extent": extent})
|
|
|
427
|
+ except Exception as e:
|
|
|
428
|
+ StructurePrint().print("{}".format(e.__str__() + ":" + traceback.format_exc()), "warn")
|
|
|
429
|
+ StructurePrint().print("空间表{}修改失败!".format(table.name), "warn")
|
|
|
430
|
+ data_session.rollback()
|
|
|
431
|
+ continue
|
|
408
|
432
|
|
|
409
|
|
- sql = '''
|
|
410
|
|
- SELECT
|
|
411
|
|
- a.attnum,
|
|
412
|
|
- a.attname AS field
|
|
413
|
|
- FROM
|
|
414
|
|
- pg_class c,
|
|
415
|
|
- pg_attribute a,
|
|
416
|
|
- pg_type t
|
|
417
|
|
- WHERE
|
|
418
|
|
- c.relname = '{}'
|
|
419
|
|
- and a.attnum > 0
|
|
420
|
|
- and a.attrelid = c.oid
|
|
421
|
|
- and a.atttypid = t.oid
|
|
422
|
|
- ORDER BY a.attnum
|
|
423
|
|
- '''.format(table_name)
|
|
424
|
|
-
|
|
425
|
|
- cols = data_session.execute(sql).fetchall()
|
|
426
|
|
- for col in cols:
|
|
427
|
|
- column = Columns(guid=uuid.uuid1().__str__(), table_guid=table_guid,
|
|
428
|
|
- name=col[1], create_time=this_time, update_time=this_time)
|
|
429
|
|
- sys_session.add(column)
|
|
|
433
|
+ def add_common_table(self,data_session,sys_session,database_guid,real_common_tables_name,origin_common_tables_name,this_time,db_tuple,creator):
|
|
|
434
|
+ for table_name in real_common_tables_name:
|
|
|
435
|
+ if table_name not in origin_common_tables_name:
|
|
|
436
|
+ try:
|
|
430
|
437
|
|
|
431
|
|
- # 删除不存在的表
|
|
432
|
|
- for n in origin_common_tables_name:
|
|
433
|
|
- if n not in real_common_tables_name:
|
|
434
|
|
- tables = Table.query.filter_by(name=n).filter_by(database_guid=database_guid).all()
|
|
435
|
|
- for table in tables:
|
|
436
|
|
- sys_session.delete(table)
|
|
|
438
|
+ StructurePrint().print("{}非空间表增加!".format(table_name))
|
|
|
439
|
+ table_guid = uuid.uuid1().__str__()
|
|
437
|
440
|
|
|
438
|
|
- def edit_common_table(self,data_session,sys_session,database_guid,real_common_tables_name,origin_common_tables_name,this_time,db_tuple):
|
|
439
|
|
- for table_name in origin_common_tables_name:
|
|
440
|
|
- tables = sys_session.query(Table).filter_by(name=table_name).filter_by(database_guid=database_guid).all()
|
|
441
|
|
- for table in tables:
|
|
442
|
|
- if table_name not in real_common_tables_name:
|
|
443
|
|
- StructurePrint().print("{}非空间表减少!".format(table_name))
|
|
444
|
|
- sys_session.delete(table)
|
|
445
|
|
- # 修改表
|
|
446
|
|
- else:
|
|
447
|
441
|
|
|
448
|
|
- # 没有权限的表删除
|
|
|
442
|
+ # 没有权限的表跳过
|
|
449
|
443
|
if not SQLUtil.check_table_privilege(table_name, "SELECT", db_tuple[0], data_session):
|
|
450
|
444
|
StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], table_name), "warn")
|
|
451
|
|
- sys_session.delete(table)
|
|
452
|
445
|
continue
|
|
453
|
446
|
|
|
454
|
|
- columns = table.relate_columns
|
|
455
|
|
- columns_names = [column.name for column in columns]
|
|
|
447
|
+ count = data_session.execute('select count(*) from "{}"'.format(table_name)).fetchone()[0]
|
|
|
448
|
+
|
|
|
449
|
+ olds = sys_session.query(Table).filter_by(name=table_name, database_guid=database_guid).all()
|
|
|
450
|
+ for old in olds:
|
|
|
451
|
+ sys_session.delete(old)
|
|
|
452
|
+
|
|
|
453
|
+ table = Table(guid=table_guid,
|
|
|
454
|
+ database_guid=database_guid,
|
|
|
455
|
+ name=table_name, create_time=this_time, update_time=this_time,
|
|
|
456
|
+ table_type=0,
|
|
|
457
|
+ feature_count=count,
|
|
|
458
|
+ creator = creator
|
|
|
459
|
+ )
|
|
|
460
|
+
|
|
|
461
|
+ sys_session.add(table)
|
|
456
|
462
|
|
|
457
|
463
|
sql = '''
|
|
458
|
464
|
SELECT
|
|
...
|
...
|
@@ -471,28 +477,86 @@ class Api(ApiTemplate): |
|
471
|
477
|
'''.format(table_name)
|
|
472
|
478
|
|
|
473
|
479
|
cols = data_session.execute(sql).fetchall()
|
|
474
|
|
- real_cols_name = [col[1] for col in cols]
|
|
475
|
|
-
|
|
476
|
|
- # 属性增加
|
|
477
|
|
- for col in real_cols_name:
|
|
478
|
|
- if col not in columns_names:
|
|
479
|
|
- StructurePrint().print("{}表要素属性增加!".format(table_name))
|
|
480
|
|
- column = Columns(guid=uuid.uuid1().__str__(), table_guid=table.guid,
|
|
481
|
|
- name=col, create_time=this_time, update_time=this_time)
|
|
482
|
|
- sys_session.add(column)
|
|
483
|
|
-
|
|
484
|
|
- # 属性减少
|
|
485
|
|
- for column in columns:
|
|
486
|
|
- if column.name not in real_cols_name:
|
|
487
|
|
- StructurePrint().print("{}表要素属性减少!".format(table_name))
|
|
488
|
|
- sys_session.delete(column)
|
|
|
480
|
+ for col in cols:
|
|
|
481
|
+ column = Columns(guid=uuid.uuid1().__str__(), table_guid=table_guid,
|
|
|
482
|
+ name=col[1], create_time=this_time, update_time=this_time)
|
|
|
483
|
+ sys_session.add(column)
|
|
489
|
484
|
|
|
490
|
|
- count = SQLUtil.get_table_count(table_name,data_session)
|
|
|
485
|
+ # 删除不存在的表
|
|
|
486
|
+ for n in origin_common_tables_name:
|
|
|
487
|
+ if n not in real_common_tables_name:
|
|
|
488
|
+ tables = Table.query.filter_by(name=n).filter_by(database_guid=database_guid).all()
|
|
|
489
|
+ for table in tables:
|
|
|
490
|
+ sys_session.delete(table)
|
|
|
491
|
+ except Exception as e:
|
|
|
492
|
+ StructurePrint().print("{}".format(e.__str__() + ":" + traceback.format_exc()), "warn")
|
|
|
493
|
+ StructurePrint().print("非空间表{}增加失败!".format(table_name), "warn")
|
|
|
494
|
+ data_session.rollback()
|
|
|
495
|
+ continue
|
|
491
|
496
|
|
|
492
|
|
- if not table.feature_count.__eq__(count):
|
|
493
|
|
- StructurePrint().print("{}表要素变化!".format(table_name))
|
|
494
|
|
- sys_session.query(Table).filter_by(guid=table.guid).update({"feature_count": count})
|
|
|
497
|
+ def edit_common_table(self,data_session,sys_session,database_guid,real_common_tables_name,origin_common_tables_name,this_time,db_tuple):
|
|
|
498
|
+ for table_name in origin_common_tables_name:
|
|
|
499
|
+ try:
|
|
|
500
|
+ tables = sys_session.query(Table).filter_by(name=table_name).filter_by(database_guid=database_guid).all()
|
|
|
501
|
+ for table in tables:
|
|
|
502
|
+ if table_name not in real_common_tables_name:
|
|
|
503
|
+ StructurePrint().print("{}非空间表减少!".format(table_name))
|
|
|
504
|
+ sys_session.delete(table)
|
|
|
505
|
+ # 修改表
|
|
|
506
|
+ else:
|
|
495
|
507
|
|
|
|
508
|
+ # 没有权限的表删除
|
|
|
509
|
+ if not SQLUtil.check_table_privilege(table_name, "SELECT", db_tuple[0], data_session):
|
|
|
510
|
+ StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], table_name), "warn")
|
|
|
511
|
+ sys_session.delete(table)
|
|
|
512
|
+ continue
|
|
|
513
|
+
|
|
|
514
|
+ columns = table.relate_columns
|
|
|
515
|
+ columns_names = [column.name for column in columns]
|
|
|
516
|
+
|
|
|
517
|
+ sql = '''
|
|
|
518
|
+ SELECT
|
|
|
519
|
+ a.attnum,
|
|
|
520
|
+ a.attname AS field
|
|
|
521
|
+ FROM
|
|
|
522
|
+ pg_class c,
|
|
|
523
|
+ pg_attribute a,
|
|
|
524
|
+ pg_type t
|
|
|
525
|
+ WHERE
|
|
|
526
|
+ c.relname = '{}'
|
|
|
527
|
+ and a.attnum > 0
|
|
|
528
|
+ and a.attrelid = c.oid
|
|
|
529
|
+ and a.atttypid = t.oid
|
|
|
530
|
+ ORDER BY a.attnum
|
|
|
531
|
+ '''.format(table_name)
|
|
|
532
|
+
|
|
|
533
|
+ cols = data_session.execute(sql).fetchall()
|
|
|
534
|
+ real_cols_name = [col[1] for col in cols]
|
|
|
535
|
+
|
|
|
536
|
+ # 属性增加
|
|
|
537
|
+ for col in real_cols_name:
|
|
|
538
|
+ if col not in columns_names:
|
|
|
539
|
+ StructurePrint().print("{}表要素属性增加!".format(table_name))
|
|
|
540
|
+ column = Columns(guid=uuid.uuid1().__str__(), table_guid=table.guid,
|
|
|
541
|
+ name=col, create_time=this_time, update_time=this_time)
|
|
|
542
|
+ sys_session.add(column)
|
|
|
543
|
+
|
|
|
544
|
+ # 属性减少
|
|
|
545
|
+ for column in columns:
|
|
|
546
|
+ if column.name not in real_cols_name:
|
|
|
547
|
+ StructurePrint().print("{}表要素属性减少!".format(table_name))
|
|
|
548
|
+ sys_session.delete(column)
|
|
|
549
|
+
|
|
|
550
|
+ count = SQLUtil.get_table_count(table_name,data_session)
|
|
|
551
|
+
|
|
|
552
|
+ if not table.feature_count.__eq__(count):
|
|
|
553
|
+ StructurePrint().print("{}表要素变化!".format(table_name))
|
|
|
554
|
+ sys_session.query(Table).filter_by(guid=table.guid).update({"feature_count": count})
|
|
|
555
|
+ except Exception as e:
|
|
|
556
|
+ StructurePrint().print("{}".format(e.__str__() + ":" + traceback.format_exc()), "warn")
|
|
|
557
|
+ StructurePrint().print("非空间表{}修改失败!".format(table_name), "warn")
|
|
|
558
|
+ data_session.rollback()
|
|
|
559
|
+ continue
|
|
496
|
560
|
|
|
497
|
561
|
api_doc={
|
|
498
|
562
|
"tags":["管理接口"],
|
...
|
...
|
|