plugin.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. """
  2. /***************************************************************************
  3. Name : DB Manager
  4. Description : Database manager plugin for QGIS (Oracle)
  5. Date : Aug 27, 2014
  6. copyright : (C) 2014 by Médéric RIBREUX
  7. email : mederic.ribreux@gmail.com
  8. The content of this file is based on
  9. - PG_Manager by Martin Dobias <wonder.sk@gmail.com> (GPLv2 license)
  10. - DB Manager by Giuseppe Sucameli <brush.tyler@gmail.com> (GPLv2 license)
  11. ***************************************************************************/
  12. /***************************************************************************
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. ***************************************************************************/
  20. """
  21. # this will disable the dbplugin if the connector raise an ImportError
  22. from typing import (
  23. Optional,
  24. Union
  25. )
  26. from .connector import OracleDBConnector
  27. from qgis.PyQt.QtCore import Qt, QCoreApplication
  28. from qgis.PyQt.QtGui import QIcon, QKeySequence
  29. from qgis.PyQt.QtWidgets import QAction, QApplication, QMessageBox
  30. from qgis.core import QgsApplication, QgsVectorLayer, NULL, QgsSettings
  31. from ..plugin import ConnectionError, InvalidDataException, DBPlugin, \
  32. Database, Schema, Table, VectorTable, TableField, TableConstraint, \
  33. TableIndex, TableTrigger
  34. from qgis.core import QgsCredentials
  35. def classFactory():
  36. return OracleDBPlugin
  37. class OracleDBPlugin(DBPlugin):
  38. @classmethod
  39. def icon(self):
  40. return QgsApplication.getThemeIcon("/mIconOracle.svg")
  41. @classmethod
  42. def typeName(self):
  43. return 'oracle'
  44. @classmethod
  45. def typeNameString(self):
  46. return QCoreApplication.translate('db_manager', 'Oracle Spatial')
  47. @classmethod
  48. def providerName(self):
  49. return 'oracle'
  50. @classmethod
  51. def connectionSettingsKey(self):
  52. return '/Oracle/connections'
  53. def connectToUri(self, uri):
  54. self.db = self.databasesFactory(self, uri)
  55. if self.db:
  56. return True
  57. return False
  58. def databasesFactory(self, connection, uri):
  59. return ORDatabase(connection, uri)
  60. def connect(self, parent=None):
  61. conn_name = self.connectionName()
  62. settings = QgsSettings()
  63. settings.beginGroup("/{}/{}".format(
  64. self.connectionSettingsKey(), conn_name))
  65. if not settings.contains("database"): # non-existent entry?
  66. raise InvalidDataException(
  67. self.tr('There is no defined database connection "{}".'.format(
  68. conn_name)))
  69. from qgis.core import QgsDataSourceUri
  70. uri = QgsDataSourceUri()
  71. settingsList = ["host", "port", "database", "username", "password"]
  72. host, port, database, username, password = (
  73. settings.value(x, "", type=str) for x in settingsList)
  74. # get all of the connection options
  75. useEstimatedMetadata = settings.value(
  76. "estimatedMetadata", False, type=bool)
  77. uri.setParam('userTablesOnly', str(
  78. settings.value("userTablesOnly", False, type=bool)))
  79. uri.setParam('geometryColumnsOnly', str(
  80. settings.value("geometryColumnsOnly", False, type=bool)))
  81. uri.setParam('allowGeometrylessTables', str(
  82. settings.value("allowGeometrylessTables", False, type=bool)))
  83. uri.setParam('onlyExistingTypes', str(
  84. settings.value("onlyExistingTypes", False, type=bool)))
  85. uri.setParam('includeGeoAttributes', str(
  86. settings.value("includeGeoAttributes", False, type=bool)))
  87. settings.endGroup()
  88. uri.setConnection(host, port, database, username, password)
  89. uri.setUseEstimatedMetadata(useEstimatedMetadata)
  90. err = ""
  91. try:
  92. return self.connectToUri(uri)
  93. except ConnectionError as e:
  94. err = str(e)
  95. # ask for valid credentials
  96. max_attempts = 3
  97. for i in range(max_attempts):
  98. (ok, username, password) = QgsCredentials.instance().get(
  99. uri.connectionInfo(False), username, password, err)
  100. if not ok:
  101. return False
  102. uri.setConnection(host, port, database, username, password)
  103. try:
  104. self.connectToUri(uri)
  105. except ConnectionError as e:
  106. if i == max_attempts - 1: # failed the last attempt
  107. raise e
  108. err = str(e)
  109. continue
  110. QgsCredentials.instance().put(
  111. uri.connectionInfo(False), username, password)
  112. return True
  113. return False
  114. class ORDatabase(Database):
  115. def __init__(self, connection, uri):
  116. self.connName = connection.connectionName()
  117. Database.__init__(self, connection, uri)
  118. def connectorsFactory(self, uri):
  119. return OracleDBConnector(uri, self.connName)
  120. def dataTablesFactory(self, row, db, schema=None):
  121. return ORTable(row, db, schema)
  122. def vectorTablesFactory(self, row, db, schema=None):
  123. return ORVectorTable(row, db, schema)
  124. def info(self):
  125. from .info_model import ORDatabaseInfo
  126. return ORDatabaseInfo(self)
  127. def schemasFactory(self, row, db):
  128. return ORSchema(row, db)
  129. def columnUniqueValuesModel(self, col, table, limit=10):
  130. l = ""
  131. if limit:
  132. l = "WHERE ROWNUM < {:d}".format(limit)
  133. con = self.database().connector
  134. # Prevent geometry column show
  135. tableName = table.replace('"', "").split(".")
  136. if len(tableName) == 0:
  137. tableName = [None, tableName[0]]
  138. colName = col.replace('"', "").split(".")[-1]
  139. if con.isGeometryColumn(tableName, colName):
  140. return None
  141. query = "SELECT DISTINCT {} FROM {} {}".format(col, table, l)
  142. return self.sqlResultModel(query, self)
  143. def sqlResultModel(self, sql, parent):
  144. from .data_model import ORSqlResultModel
  145. return ORSqlResultModel(self, sql, parent)
  146. def sqlResultModelAsync(self, sql, parent):
  147. from .data_model import ORSqlResultModelAsync
  148. return ORSqlResultModelAsync(self, sql, parent)
  149. def toSqlLayer(self, sql, geomCol, uniqueCol,
  150. layerName="QueryLayer", layerType=None,
  151. avoidSelectById=False, filter=""):
  152. uri = self.uri()
  153. con = self.database().connector
  154. if uniqueCol is not None:
  155. uniqueCol = uniqueCol.strip('"').replace('""', '"')
  156. uri.setDataSource("", "({}\n)".format(
  157. sql), geomCol, filter, uniqueCol)
  158. if avoidSelectById:
  159. uri.disableSelectAtId(True)
  160. provider = self.dbplugin().providerName()
  161. vlayer = QgsVectorLayer(uri.uri(False), layerName, provider)
  162. # handling undetermined geometry type
  163. if not vlayer.isValid():
  164. wkbType, srid = con.getTableMainGeomType(
  165. "({}\n)".format(sql), geomCol)
  166. uri.setWkbType(wkbType)
  167. if srid:
  168. uri.setSrid(str(srid))
  169. vlayer = QgsVectorLayer(uri.uri(False), layerName, provider)
  170. return vlayer
  171. def registerDatabaseActions(self, mainWindow):
  172. action = QAction(QApplication.translate(
  173. "DBManagerPlugin", "&Re-connect"), self)
  174. mainWindow.registerAction(action, QApplication.translate(
  175. "DBManagerPlugin", "&Database"), self.reconnectActionSlot)
  176. if self.schemas():
  177. action = QAction(QApplication.translate(
  178. "DBManagerPlugin", "&Create Schema…"), self)
  179. mainWindow.registerAction(action, QApplication.translate(
  180. "DBManagerPlugin", "&Schema"), self.createSchemaActionSlot)
  181. action = QAction(QApplication.translate(
  182. "DBManagerPlugin", "&Delete (Empty) Schema…"), self)
  183. mainWindow.registerAction(action, QApplication.translate(
  184. "DBManagerPlugin", "&Schema"), self.deleteSchemaActionSlot)
  185. action = QAction(QApplication.translate(
  186. "DBManagerPlugin", "Delete Selected Item"), self)
  187. mainWindow.registerAction(action, None, self.deleteActionSlot)
  188. action.setShortcuts(QKeySequence.Delete)
  189. action = QAction(QgsApplication.getThemeIcon("/mActionCreateTable.svg"),
  190. QApplication.translate(
  191. "DBManagerPlugin", "&Create Table…"), self)
  192. mainWindow.registerAction(action, QApplication.translate(
  193. "DBManagerPlugin", "&Table"), self.createTableActionSlot)
  194. action = QAction(QgsApplication.getThemeIcon("/mActionEditTable.svg"),
  195. QApplication.translate(
  196. "DBManagerPlugin", "&Edit Table…"), self)
  197. mainWindow.registerAction(action, QApplication.translate(
  198. "DBManagerPlugin", "&Table"), self.editTableActionSlot)
  199. action = QAction(QgsApplication.getThemeIcon("/mActionDeleteTable.svg"),
  200. QApplication.translate(
  201. "DBManagerPlugin", "&Delete Table/View…"), self)
  202. mainWindow.registerAction(action, QApplication.translate(
  203. "DBManagerPlugin", "&Table"), self.deleteTableActionSlot)
  204. action = QAction(QApplication.translate(
  205. "DBManagerPlugin", "&Empty Table…"), self)
  206. mainWindow.registerAction(action, QApplication.translate(
  207. "DBManagerPlugin", "&Table"), self.emptyTableActionSlot)
  208. def supportsComment(self):
  209. return False
  210. class ORSchema(Schema):
  211. def __init__(self, row, db):
  212. Schema.__init__(self, db)
  213. # self.oid, self.name, self.owner, self.perms, self.comment = row
  214. self.name = row[0]
  215. class ORTable(Table):
  216. def __init__(self, row, db, schema=None):
  217. Table.__init__(self, db, schema)
  218. self.name, self.owner, isView = row
  219. self.estimatedRowCount = None
  220. self.objectType: Optional[Union[str, bool]] = None
  221. self.isView = False
  222. self.isMaterializedView = False
  223. if isView == 1:
  224. self.isView = True
  225. self.creationDate = None
  226. self.modificationDate = None
  227. def getDates(self):
  228. """Grab the creation/modification dates of the table"""
  229. self.creationDate, self.modificationDate = (
  230. self.database().connector.getTableDates((self.schemaName(),
  231. self.name)))
  232. def refreshRowEstimation(self):
  233. """Use ALL_ALL_TABLE to get an estimation of rows"""
  234. if self.isView:
  235. self.estimatedRowCount = 0
  236. self.estimatedRowCount = (
  237. self.database().connector.getTableRowEstimation(
  238. (self.schemaName(), self.name)))
  239. def getType(self):
  240. """Grab the type of object for the table"""
  241. self.objectType = self.database().connector.getTableType(
  242. (self.schemaName(), self.name))
  243. def getComment(self):
  244. """Grab the general comment of the table/view"""
  245. self.comment = self.database().connector.getTableComment(
  246. (self.schemaName(), self.name), self.objectType)
  247. def getDefinition(self):
  248. return self.database().connector.getDefinition(
  249. (self.schemaName(), self.name), self.objectType)
  250. def getMViewInfo(self):
  251. if self.objectType == "MATERIALIZED VIEW":
  252. return self.database().connector.getMViewInfo(
  253. (self.schemaName(), self.name))
  254. else:
  255. return None
  256. def runAction(self, action):
  257. action = str(action)
  258. if action.startswith("rows/"):
  259. if action == "rows/recount":
  260. self.refreshRowCount()
  261. return True
  262. elif action.startswith("index/"):
  263. parts = action.split('/')
  264. index_name = parts[1]
  265. index_action = parts[2]
  266. msg = QApplication.translate(
  267. "DBManagerPlugin",
  268. "Do you want to {} index {}?".format(
  269. index_action, index_name))
  270. QApplication.restoreOverrideCursor()
  271. try:
  272. if QMessageBox.question(
  273. None,
  274. QApplication.translate(
  275. "DBManagerPlugin", "Table Index"),
  276. msg,
  277. QMessageBox.Yes | QMessageBox.No) == QMessageBox.No:
  278. return False
  279. finally:
  280. QApplication.setOverrideCursor(Qt.WaitCursor)
  281. if index_action == "rebuild":
  282. self.aboutToChange.emit()
  283. self.database().connector.rebuildTableIndex(
  284. (self.schemaName(), self.name), index_name)
  285. self.refreshIndexes()
  286. return True
  287. elif action.startswith("mview/"):
  288. if action == "mview/refresh":
  289. self.aboutToChange.emit()
  290. self.database().connector.refreshMView(
  291. (self.schemaName(), self.name))
  292. return True
  293. return Table.runAction(self, action)
  294. def tableFieldsFactory(self, row, table):
  295. return ORTableField(row, table)
  296. def tableConstraintsFactory(self, row, table):
  297. return ORTableConstraint(row, table)
  298. def tableIndexesFactory(self, row, table):
  299. return ORTableIndex(row, table)
  300. def tableTriggersFactory(self, row, table):
  301. return ORTableTrigger(row, table)
  302. def info(self):
  303. from .info_model import ORTableInfo
  304. return ORTableInfo(self)
  305. def tableDataModel(self, parent):
  306. from .data_model import ORTableDataModel
  307. return ORTableDataModel(self, parent)
  308. def getValidQgisUniqueFields(self, onlyOne=False):
  309. """ list of fields valid to load the table as layer in QGIS canvas.
  310. QGIS automatically search for a valid unique field, so it's
  311. needed only for queries and views.
  312. """
  313. ret = []
  314. # add the pk
  315. pkcols = [x for x in self.fields() if x.primaryKey]
  316. if len(pkcols) == 1:
  317. ret.append(pkcols[0])
  318. # then add integer fields with an unique index
  319. indexes = self.indexes()
  320. if indexes is not None:
  321. for idx in indexes:
  322. if idx.isUnique and len(idx.columns) == 1:
  323. fld = idx.fields()[idx.columns[0]]
  324. if (fld.dataType == "NUMBER" and not fld.modifier and fld.notNull and fld not in ret):
  325. ret.append(fld)
  326. # and finally append the other suitable fields
  327. for fld in self.fields():
  328. if (fld.dataType == "NUMBER" and not fld.modifier and fld.notNull and fld not in ret):
  329. ret.append(fld)
  330. if onlyOne:
  331. return ret[0] if len(ret) > 0 else None
  332. return ret
  333. def uri(self):
  334. uri = self.database().uri()
  335. schema = self.schemaName() if self.schemaName() else ''
  336. geomCol = self.geomColumn if self.type in [
  337. Table.VectorType, Table.RasterType] else ""
  338. uniqueCol = self.getValidQgisUniqueFields(
  339. True) if self.isView else None
  340. uri.setDataSource(schema, self.name, geomCol if geomCol else None,
  341. None, uniqueCol.name if uniqueCol else "")
  342. # Handle geographic table
  343. if geomCol:
  344. uri.setWkbType(self.wkbType)
  345. uri.setSrid(str(self.srid))
  346. return uri
  347. class ORVectorTable(ORTable, VectorTable):
  348. def __init__(self, row, db, schema=None):
  349. ORTable.__init__(self, row[0:3], db, schema)
  350. VectorTable.__init__(self, db, schema)
  351. self.geomColumn, self.geomType, self.wkbType, self.geomDim, \
  352. self.srid = row[-7:-2]
  353. def info(self):
  354. from .info_model import ORVectorTableInfo
  355. return ORVectorTableInfo(self)
  356. def runAction(self, action):
  357. if action.startswith("extent/"):
  358. if action == "extent/update":
  359. self.aboutToChange.emit()
  360. self.updateExtent()
  361. return True
  362. if ORTable.runAction(self, action):
  363. return True
  364. return VectorTable.runAction(self, action)
  365. def canUpdateMetadata(self):
  366. return self.database().connector.canUpdateMetadata((self.schemaName(),
  367. self.name))
  368. def updateExtent(self):
  369. self.database().connector.updateMetadata(
  370. (self.schemaName(), self.name),
  371. self.geomColumn, extent=self.extent)
  372. self.refreshTableEstimatedExtent()
  373. self.refresh()
  374. def hasSpatialIndex(self, geom_column=None):
  375. geom_column = geom_column if geom_column else self.geomColumn
  376. for idx in self.indexes():
  377. if geom_column == idx.column:
  378. return True
  379. return False
  380. class ORTableField(TableField):
  381. def __init__(self, row, table):
  382. """ build fields information from query and find primary key """
  383. TableField.__init__(self, table)
  384. self.num, self.name, self.dataType, self.charMaxLen, \
  385. self.modifier, self.notNull, self.hasDefault, \
  386. self.default, typeStr, self.comment = row
  387. self.primaryKey = False
  388. self.num = int(self.num)
  389. if self.charMaxLen == NULL:
  390. self.charMaxLen = None
  391. else:
  392. self.charMaxLen = int(self.charMaxLen)
  393. if self.modifier == NULL:
  394. self.modifier = None
  395. else:
  396. self.modifier = int(self.modifier)
  397. if self.notNull.upper() == "Y":
  398. self.notNull = False
  399. else:
  400. self.notNull = True
  401. if self.comment == NULL:
  402. self.comment = ""
  403. # find out whether fields are part of primary key
  404. for con in self.table().constraints():
  405. if con.type == ORTableConstraint.TypePrimaryKey and self.name == con.column:
  406. self.primaryKey = True
  407. break
  408. def type2String(self):
  409. if ("TIMESTAMP" in self.dataType or self.dataType in ["DATE", "SDO_GEOMETRY", "BINARY_FLOAT", "BINARY_DOUBLE"]):
  410. return "{}".format(self.dataType)
  411. if self.charMaxLen in [None, -1]:
  412. return "{}".format(self.dataType)
  413. elif self.modifier in [None, -1, 0]:
  414. return "{}({})".format(self.dataType, self.charMaxLen)
  415. return "{}({},{})".format(self.dataType, self.charMaxLen,
  416. self.modifier)
  417. def update(self, new_name, new_type_str=None, new_not_null=None,
  418. new_default_str=None):
  419. self.table().aboutToChange.emit()
  420. if self.name == new_name:
  421. new_name = None
  422. if self.type2String() == new_type_str:
  423. new_type_str = None
  424. if self.notNull == new_not_null:
  425. new_not_null = None
  426. if self.default2String() == new_default_str:
  427. new_default_str = None
  428. ret = self.table().database().connector.updateTableColumn(
  429. (self.table().schemaName(), self.table().name),
  430. self.name, new_name, new_type_str,
  431. new_not_null, new_default_str)
  432. # When changing a field, refresh also constraints and
  433. # indexes.
  434. if ret is not False:
  435. self.table().refreshFields()
  436. self.table().refreshConstraints()
  437. self.table().refreshIndexes()
  438. return ret
  439. class ORTableConstraint(TableConstraint):
  440. TypeCheck, TypeForeignKey, TypePrimaryKey, \
  441. TypeUnique, TypeUnknown = list(range(5))
  442. types = {"c": TypeCheck, "r": TypeForeignKey,
  443. "p": TypePrimaryKey, "u": TypeUnique}
  444. def __init__(self, row, table):
  445. """ build constraints info from query """
  446. TableConstraint.__init__(self, table)
  447. self.name, constr_type_str, self.column, self.validated, \
  448. self.generated, self.status = row[0:6]
  449. constr_type_str = constr_type_str.lower()
  450. if constr_type_str in ORTableConstraint.types:
  451. self.type = ORTableConstraint.types[constr_type_str]
  452. else:
  453. self.type = ORTableConstraint.TypeUnknown
  454. if row[6] == NULL:
  455. self.checkSource = ""
  456. else:
  457. self.checkSource = row[6]
  458. if row[8] == NULL:
  459. self.foreignTable = ""
  460. else:
  461. self.foreignTable = row[8]
  462. if row[7] == NULL:
  463. self.foreignOnDelete = ""
  464. else:
  465. self.foreignOnDelete = row[7]
  466. if row[9] == NULL:
  467. self.foreignKey = ""
  468. else:
  469. self.foreignKey = row[9]
  470. def type2String(self):
  471. if self.type == ORTableConstraint.TypeCheck:
  472. return QApplication.translate("DBManagerPlugin", "Check")
  473. if self.type == ORTableConstraint.TypePrimaryKey:
  474. return QApplication.translate("DBManagerPlugin", "Primary key")
  475. if self.type == ORTableConstraint.TypeForeignKey:
  476. return QApplication.translate("DBManagerPlugin", "Foreign key")
  477. if self.type == ORTableConstraint.TypeUnique:
  478. return QApplication.translate("DBManagerPlugin", "Unique")
  479. return QApplication.translate("DBManagerPlugin", 'Unknown')
  480. def fields(self):
  481. """ Hack to make edit dialog box work """
  482. fields = self.table().fields()
  483. field = None
  484. for fld in fields:
  485. if fld.name == self.column:
  486. field = fld
  487. cols = {}
  488. cols[0] = field
  489. return cols
  490. class ORTableIndex(TableIndex):
  491. def __init__(self, row, table):
  492. TableIndex.__init__(self, table)
  493. self.name, self.column, self.indexType, self.status, \
  494. self.analyzed, self.compression, self.isUnique = row
  495. def fields(self):
  496. """ Hack to make edit dialog box work """
  497. self.table().refreshFields()
  498. fields = self.table().fields()
  499. field = None
  500. for fld in fields:
  501. if fld.name == self.column:
  502. field = fld
  503. cols = {}
  504. cols[0] = field
  505. return cols
  506. class ORTableTrigger(TableTrigger):
  507. def __init__(self, row, table):
  508. TableTrigger.__init__(self, table)
  509. self.name, self.event, self.type, self.enabled = row