postgisrestore.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # -*- coding: utf-8 -*-
  2. """
  3. ***************************************************************************
  4. postgisrestore.py
  5. ---------------------
  6. Date : November 2012
  7. Copyright : (C) 2012 by Victor Olaya
  8. Email : volayaf at gmail dot com
  9. ***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************
  17. """
  18. __author__ = 'wanger'
  19. __date__ = 'November 2024'
  20. __copyright__ = '(C) 2024, wanger'
  21. import os
  22. from osgeo import ogr, gdal
  23. from PyQt5.QtGui import QIcon
  24. from PyQt5.QtWidgets import QApplication
  25. from future.moves import sys
  26. from qgis.PyQt import QtWidgets
  27. from qgis.core import (QgsProcessing,
  28. QgsProcessingParameterFeatureSource,
  29. QgsProcessingParameterString,
  30. QgsProcessingParameterFile,
  31. QgsProcessingParameterDateTime,
  32. QgsProcessingParameterEnum,
  33. QgsProcessingParameterCrs,
  34. QgsProcessingParameterField,
  35. QgsProcessingParameterExtent,
  36. QgsProcessingParameterBoolean,
  37. QgsProcessingParameterProviderConnection,
  38. QgsProcessingParameterDatabaseSchema,
  39. QgsProcessingParameterDatabaseTable,
  40. QgsProviderRegistry,
  41. QgsProcessingException,
  42. QgsProcessingParameterDefinition,
  43. QgsProviderConnectionException,
  44. QgsDataSourceUri)
  45. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  46. from processing.algs.gdal.GdalUtils import GdalUtils
  47. from processing.tools.PrintUtils import printStr
  48. from processing.tools.StringUtils import getConnectionStr
  49. from processing.tools.GeoServer.Geoserver import Geoserver
  50. from processing.tools.PostgreSQL.PostgreSQL import PostgreSQL
  51. from processing.tools.Login.Redis import Redis
  52. from processing.tools.system import isWindows
  53. pluginPath = os.path.normpath(os.path.join(
  54. os.path.split(os.path.dirname(__file__))[0], os.pardir))
  55. gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
  56. gdal.SetConfigOption("SHAPE_ENCODING", "GBK")
  57. class Postgisrestore(GdalAlgorithm):
  58. LOGIN_USER = "admin"
  59. UPDATEFILE = 'UPDATEFILE'
  60. RESTORETABLE = 'RESTORETABLE'
  61. RESTOREVERSION = 'RESTOREVERSION'
  62. CURVERSION = "CURVERSION"
  63. ALLOWOVERLAP = "ALLOWOVERLAP"
  64. DATABASE = 'DATABASE'
  65. INPUTFILE = 'INPUTFILE'
  66. SCHEMA = 'SCHEMA'
  67. TABLE = 'TABLE'
  68. HOST = 'HOST'
  69. DBNAME = 'DBNAME'
  70. PORT = 'PORT'
  71. USER = 'USER'
  72. PASSWORD = 'PASSWORD'
  73. selectedValue = "selectedValue"
  74. tables = []
  75. checklogs = []
  76. def __init__(self):
  77. super().__init__()
  78. def initAlgorithm(self, config=None):
  79. pgconn = PostgreSQL(schema='base')
  80. self.tables = pgconn.getManagerTables(username=self.LOGIN_USER)
  81. tablenames = []
  82. for row in self.tables:
  83. tablenames.append(row[2])
  84. self.addParameter(QgsProcessingParameterEnum(name=self.RESTORETABLE,
  85. description=self.tr('目标数据源'),
  86. options=tablenames))
  87. redis = Redis()
  88. tablename = redis.get("curRestoreTable")
  89. print(tablename)
  90. if tablename is not None:
  91. tablename = tablename.decode('utf-8')
  92. versions = pgconn.getTableVersions(tablename=tablename)
  93. curversion = pgconn.getTableCurVersion(tablename=tablename)
  94. tableversion_param = QgsProcessingParameterString(
  95. self.CURVERSION,
  96. self.tr('当前版本号'), defaultValue=curversion)
  97. self.addParameter(tableversion_param)
  98. self.versionlist = []
  99. for row in versions:
  100. self.versionlist.append(row[0])
  101. self.addParameter(QgsProcessingParameterEnum(name=self.RESTOREVERSION,
  102. description=self.tr('目标版本'),
  103. options=self.versionlist))
  104. pgconn.close()
  105. def name(self):
  106. return 'postgisrestore'
  107. def icon(self):
  108. return QIcon(os.path.join(pluginPath, 'images', 'dbms', 'restore.png'))
  109. def displayName(self):
  110. return self.tr('版本回退')
  111. def shortDescription(self):
  112. return self.tr('回退数据库版本')
  113. def tags(self):
  114. t = self.tr('import,into,postgis,database,vector').split(',')
  115. t.extend(super().tags())
  116. return t
  117. def group(self):
  118. return self.tr('数据更新维护')
  119. def groupId(self):
  120. return 'updatedata'
  121. def setSelectedValue(self, v):
  122. printStr(v)
  123. self.selectedValue = v
  124. def getSelectedValue(self):
  125. return self.selectedValue
  126. def restoreVector(self, parameters, context, feedback, executing=True):
  127. print("版本回退开始啦")
  128. pgconn = PostgreSQL(schema='base')
  129. tableinfo = self.tables[parameters[self.RESTORETABLE]]
  130. tablename = tableinfo[1]
  131. # 判断回退版本关系
  132. curversion = parameters[self.CURVERSION]
  133. targetversion = self.versionlist[parameters[self.RESTOREVERSION]]
  134. message = "版本回退成功"
  135. if curversion == targetversion:
  136. message = "版本相同不需要操作"
  137. elif curversion > targetversion:
  138. print("需要删除近期版本更新的数据")
  139. pgconn.deleteVectorRecords(tablename=tablename, rksj_sw=targetversion)
  140. else:
  141. pgconn.insertVectorRecords(tablename=tablename, curversion=curversion, targetversion=targetversion)
  142. print("需要插入近期版本更新的数据")
  143. # print("===========备份表===========")
  144. # pgconn.restoreBackTable(tablename=tablename)
  145. pgconn.close()
  146. return {
  147. "状态": message
  148. }
  149. # 判断数据是否为字符串
  150. def is_string(self, var):
  151. return isinstance(var, str)
  152. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  153. return []
  154. def contains_keys(self, obj, keys):
  155. if isinstance(obj, dict):
  156. return all(key in obj.keys() for key in keys)
  157. elif hasattr(type(obj), '__dict__'):
  158. return all(key in obj.__dict__ for key in keys)
  159. else:
  160. raise ValueError("Invalid object type")
  161. def commandName(self):
  162. return "ogr2ogr"