PGUtil.py
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# coding=utf-8
#author: 4N
#createtime: 2021/5/24
#email: nheweijun@sina.com
from osgeo import ogr
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker,Session
class PGUtil:
@classmethod
def open_pg_data_source(cls,iswrite, uri):
"""
# 获取PostGIS数据源
:return:
"""
db_conn_tuple = cls.get_info_from_sqlachemy_uri(uri)
fn = "PG: user=%s password=%s host=%s port=%s dbname=%s " % db_conn_tuple
driver = ogr.GetDriverByName("PostgreSQL")
if driver is None:
raise Exception("打开PostgreSQL驱动失败,可能是当前GDAL未支持PostgreSQL驱动!")
ds = driver.Open(fn, iswrite)
if ds is None:
raise Exception("打开数据源失败!")
return ds
@classmethod
def get_info_from_sqlachemy_uri(cls,uri):
parts = uri.split(":")
user = parts[1][2:]
password_list = parts[2].split("@")
if password_list.__len__() > 2:
password = "@".join(password_list[:-1])
else:
password = parts[2].split("@")[0]
host = parts[2].split("@")[-1]
port = parts[3].split("/")[0]
database = parts[3].split("/")[1]
return user, password, host, port, database
@classmethod
def get_db_session(cls,db_url, autocommit=False) -> Session:
engine = create_engine(db_url)
system_session :Session= sessionmaker(bind=engine, autocommit=autocommit)()
return system_session
@classmethod
def get_geo_column(cls,table_name,db_session):
# 判断空间列
geom_col_sql = '''
SELECT a.attname AS field,t.typname AS type
FROM
pg_class c,
pg_attribute a,
pg_type t
WHERE
c.relname = '{}'
and a.attnum > 0
and a.attrelid = c.oid
and a.atttypid = t.oid
'''.format(table_name)
geom_col = None
geom_result = db_session.execute(geom_col_sql)
for row_proxy in geom_result:
if row_proxy[1].__eq__("geometry"):
geom_col = row_proxy[0]
return geom_col
@classmethod
def get_table_count(cls,table_name,db_session):
count_result = db_session.execute('''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(
table_name)).fetchone()
count = count_result[0]
if count< 1000000:
count_result = db_session.execute('select count(*) from "{}"'.format(table_name)).fetchone()
count=count_result[0]
return count
@classmethod
def check_table_privilege(cls,table_name,pri_type,user,pg_ds):
'''
通过pg_ds来判断用户是否对表具有权限
:param table_name:
:param pri_type:
:param user:
:param pg_ds:
:return:
'''
pri = pg_ds.ExecuteSQL("select * from information_schema.table_privileges "
"where grantee='{}' and table_name='{}' and privilege_type='{}' "
.format(user,table_name,pri_type))
if pri.GetNextFeature():
return True
else:
return False
@classmethod
def check_database_privilege(cls,table_name,pri_type,user,session):
pass
@classmethod
def check_database_privilege(cls,table_name,pri_type,user,session):
pass