PostgreSQL.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. __author__ = 'wanger'
  3. __date__ = '2024-08-20'
  4. __copyright__ = '(C) 2024 by siwei'
  5. __revision__ = '1.0'
  6. import time
  7. from typing import Optional
  8. import os
  9. import psycopg2
  10. import uuid
  11. class PostgreSQL:
  12. # 矢量数据元数据表
  13. Vector_Storage = "t_vector_storage"
  14. def __init__(
  15. self,
  16. host: Optional[str] = "192.168.60.2", # default host during installation
  17. port: Optional[str] = "5432", # default port during pg installation
  18. user: Optional[str] = "postgres", # default user during pg installation
  19. password: Optional[str] = "postgis", # default password during pg installation
  20. dbname: Optional[str] = "real3d", # default dbname during pg installation
  21. schema: Optional[str] = None
  22. ):
  23. # 配置数据库连接参数并指定schema
  24. self.connparams = {
  25. "dbname": dbname,
  26. "user": user,
  27. "password": password,
  28. "host": host,
  29. "port": port,
  30. "options": "-c search_path=otherSchema," + schema if schema is not None else None
  31. }
  32. self.conn = psycopg2.connect(**self.connparams)
  33. # 创建一个游标对象
  34. self.cur = self.conn.cursor()
  35. # 执行一个查询
  36. # self.cur.execute("SELECT 省,类型 from \"XZQH3857\";")
  37. # 获取查询结果
  38. # rows = self.cur.fetchall()
  39. # 打印结果
  40. # for row in rows:
  41. # for v in row:
  42. # print(v)
  43. # def execute(self, sql):
  44. # # 执行一个查询
  45. # self.cur.execute(sql)
  46. # # 获取查询结果
  47. # return self.cur.fetchall()
  48. def execute(self, sql, params=None):
  49. try:
  50. if params:
  51. self.cur.execute(sql, params)
  52. else:
  53. self.cur.execute(sql)
  54. # 如果是 SELECT,才 fetch 结果
  55. if sql.strip().lower().startswith("select"):
  56. return self.cur.fetchall()
  57. else:
  58. self.conn.commit()
  59. return None
  60. except Exception as e:
  61. print(f"SQL执行出错:{e}")
  62. self.conn.rollback()
  63. raise
  64. def close(self):
  65. # 关闭游标和连接
  66. self.cur.close()
  67. self.conn.close()
  68. # 获取资源目录
  69. def getZyml(self):
  70. # self.cur.execute("select t.ywlx as bsm,t.ywlx as name , '' as pbsm,'' as type from (select distinct(ywlx) from t_vector_field order by ywlx) t union all select a.name as bsm, case when a.table_alias is null or a.table_alias = '' then a.name else a.table_alias end as name, a.ywlx as pbsm, a.sjlx as type from t_vector_storage a order by name")
  71. self.cur.execute(
  72. "select * from (select t.bsm ,t.name , t.pbsm,'' as type from t_vector_zyml t order by t.sort) t union all select a.name as bsm, case when a.table_alias is null or a.table_alias = '' then a.name else a.table_alias end as name, a.xmlx as pbsm, a.sjlx as type from t_vector_storage a")
  73. rows = self.cur.fetchall()
  74. return rows
  75. def getManagerTables(self, username='admin'):
  76. sql = f'select t.id "id",name "name", case when t.table_alias != \'\' then t.table_alias else t.name end as "alias", t.ywlx "ywlx" ' \
  77. f'from {self.Vector_Storage} t where t.glbm = (select dept_name from sys_dept d where d.dept_id = (select dept_id from sys_user u where u.user_name = \'{username}\' )) and t.sjlx = \'vector\''
  78. self.cur.execute(sql)
  79. rows = self.cur.fetchall()
  80. return rows
  81. def getVectorZyml(self):
  82. self.cur.execute(
  83. "select bsm,name from t_vector_zyml t order by t.sort,t.name")
  84. rows = self.cur.fetchall()
  85. return rows
  86. def dropTable(self, tablename):
  87. self.cur.execute(
  88. "delete from t_vector_storage where name = '{}'".format(tablename))
  89. self.conn.commit()
  90. self.cur.execute("drop table {}".format(tablename))
  91. self.conn.commit()
  92. def addZyml(self, uid, pid, name):
  93. self.cur.execute(
  94. "insert into t_vector_zyml (bsm , name , pbsm) values ('{}' , '{}', '{}')".format(uid, name, pid))
  95. self.conn.commit()
  96. def renameZyml(self, id, name):
  97. self.cur.execute("update t_vector_zyml set name = '{}' where bsm = '{}'".format(name, id))
  98. self.conn.commit()
  99. def deleteZyml(self, id):
  100. self.cur.execute(
  101. "delete from t_vector_zyml where bsm in (WITH RECURSIVE a AS ( SELECT g.bsm, g.pbsm FROM t_vector_zyml g WHERE bsm = '{}' UNION ALL SELECT d.bsm, d.pbsm FROM t_vector_zyml d JOIN a ON a.bsm = d.pbsm ) SELECT bsm FROM a order by a.bsm)".format(
  102. id))
  103. self.conn.commit()
  104. def getResourceAttr(self, tablename):
  105. self.cur.execute(
  106. "select t.*, case when t.sjlx = 'vector' then '矢量数据' else '栅格数据' end from t_vector_storage t where name = '{}'".format(
  107. tablename))
  108. rows = self.cur.fetchall()
  109. return rows[0]
  110. # 获取行政区划
  111. def getXzqh(self):
  112. self.cur.execute(
  113. "select t.id, t.name, case when t.pid = '0' then '' else t.pid end from vector.xzqh t order by t.id")
  114. rows = self.cur.fetchall()
  115. return rows
  116. # 判断数据是否为字符串
  117. def is_string(self, var):
  118. return isinstance(var, str)