def getAllEdgvDomainsFromTableName(self, table): """ EDGV databases deployed by DSGTools have a set of domain tables. Gets the value map from such DB. It checks for all attributes found. :param table: (str) layer to be checked for its EDGV mapping. :param table: (QgsVectorLayer) overloaded method - layer to be checked for its EDGV mapping. :param field: (str) field to be checked. :return: (dict) value map for all attributes that have one. """ ret = defaultdict(dict) currentLayer = table if isinstance(table, QgsVectorLayer) else self.layerByName(table) if currentLayer.isValid(): try: uri = currentLayer.dataProvider().uri() if uri.host() == '': db = QSqlDatabase('QSQLITE') db.setDatabaseName( uri.uri().split("|")[0].strip() if uri.uri().split("|")[0].strip().endswith(".gpkg") \ else uri.database() ) sql = 'select code, code_name from dominios_{field} order by code' else: db = QSqlDatabase('QPSQL') db.setHostName(uri.host()) db.setPort(int(uri.port())) db.setDatabaseName(uri.database()) db.setUserName(uri.username()) db.setPassword(uri.password()) sql = 'select code, code_name from dominios.{field} order by code' if not db.open(): db.close() return ret for field in currentLayer.fields(): fieldName = field.name() if fieldName in self.specialEdgvAttributes(): # EDGV "special" attributes that are have different domains depending on # which class it belongs to if self.edgvVersion(db) in ("2.1.3 Pro", "3.0 Pro"): cat = table if isinstance(table, str) else table.name() # Pro versions now follow the logic "{attribute}_{CLASS_NAME}" cat = cat.rsplit("_", 1)[0].split("_", 1)[-1] else: cat = (table if isinstance(table, str) else table.name()).split("_")[0] fieldN = "{attribute}_{cat}".format(attribute=fieldName, cat=cat) query = QSqlQuery(sql.format(field=fieldN), db) else: query = QSqlQuery(sql.format(field=fieldName), db) if not query.isActive(): continue while query.next(): code = str(query.value(0)) code_name = query.value(1) ret[fieldName][code_name] = code db.close() except: pass return ret
def getEdgvDomainsFromTableName(self, table, field=None): """ EDGV databases deployed by DSGTools have a set of domain tables. Gets the value map from such DB. :param table: (str) layer to be checked for its EDGV mapping. :param table: (QgsVectorLayer) overloaded method - layer to be checked for its EDGV mapping. :param field: (str) field to be checked. :return: (dict) value map. """ ret = dict() currentLayer = table if isinstance( table, QgsVectorLayer) else self.layerByName(table) if currentLayer.isValid(): try: uri = currentLayer.dataProvider().uri() field = field or self.currentField() if field in self.specialEdgvAttributes(): # EDGV "special" attributes that are have different domains depending on # which class it belongs to category = self.currentLayerName().split("_")[0] field = "{attribute}_{cat}".format(attribute=field, cat=category) if uri.host() == '': db = QSqlDatabase('QSQLITE') db.setDatabaseName( uri.uri().split("|")[0].strip() if uri.uri().split("|")[0].strip().endswith(".gpkg") \ else uri.database() ) sql = 'select code, code_name from dominios_{field} order by code'.format( field=field) else: db = QSqlDatabase('QPSQL') db.setHostName(uri.host()) db.setPort(int(uri.port())) db.setDatabaseName(uri.database()) db.setUserName(uri.username()) db.setPassword(uri.password()) sql = 'select code, code_name from dominios.{field} order by code'.format( field=field) if not db.open(): db.close() return ret query = QSqlQuery(sql, db) if not query.isActive(): return ret while query.next(): code = str(query.value(0)) code_name = query.value(1) ret[code_name] = code db.close() except: pass return ret
def dropDatabase(self,db): """ Drops the created database case a problem occurs during database creation db: QSqlDatabase to be dropped """ host = db.hostName() port = db.port() user = db.userName() password = db.password() database = 'postgres' pgDB = QSqlDatabase('QPSQL') pgDB.setHostName(host) pgDB.setPort(port) pgDB.setUserName(user) pgDB.setPassword(password) pgDB.setDatabaseName(database) if not pgDB.open(): return False sql = self.gen.dropDatabase(db.databaseName()) query = QSqlQuery(pgDB) return query.exec_(sql)