def delete_data_in_database(self): """Deletes the project (= database schema) in the database. Returns: False: If something went wrong with deleting in the database. Otherwise True. """ try: if self.settings.value("options/general/use_pg_projects_database", False, type=bool): db = get_default_db() else: db = open_psql_db(self.db_host, self.db_name, self.db_port, self.db_admin, self.db_admin_pwd) sql = "BEGIN;" sql += "DROP SCHEMA IF EXISTS %s CASCADE;" % self.db_schema sql += "COMMIT;" query = db.exec_(sql) if not query.isActive(): message = "Error occured while deleting project in database." raise VerisoError( message, long_message=QSqlQuery.lastError(query).text()) db.close() del db return True except Exception as e: message = "Something went wrong while deleting the project." raise VerisoError(message, e)
def delete_data_in_database(self): """Deletes the project (= database schema) in the database. Returns: False: If something went wrong with deleting in the database. Otherwise True. """ try: db = open_psql_db(self.db_host, self.db_name, self.db_port, self.db_admin, self.db_admin_pwd) sql = "BEGIN;" sql += "DROP SCHEMA IF EXISTS %s CASCADE;" % self.db_schema sql += "DELETE FROM geometry_columns WHERE f_table_schema='%s' ;" % self.db_schema sql += "COMMIT;" query = db.exec_(sql) if not query.isActive(): message = "Error occured while deleting project in database." raise VerisoError( message, long_message=QSqlQuery.lastError(query).text()) db.close() del db return True except Exception as e: message = "Something went wrong while deleting the project." raise VerisoError(message, e)
def btnTestConnection_clicked(self): try: db = open_psql_db(self.lineEditDbHost.text(), self.lineEditDbDatabase.text(), self.lineEditDbPort.text(), self.lineEditDbAdmin.text(), self.lineEditDbAdminPwd.text()) sql = "select postgis_full_version();" query = db.exec_(sql) count = query.size() db.close() if count < 1: raise VerisoError( "No PostGIS found on the DB %s" % db.connectionName()) except Exception as e: self.test_connection_failed(e) else: self.test_connection_succes()
def check_project_name(self, project_name): """Makes a database request and checks if the given schema already exists. Returns: -1 if an error occured. 0 if schema was not found. 1 if schema already exists. """ self.db_host = self.settings.value("options/db/host") self.db_name = self.settings.value("options/db/name") self.db_port = self.settings.value("options/db/port") self.db_user = self.settings.value("options/db/user") self.db_pwd = self.settings.value("options/db/pwd") self.db_admin = self.settings.value("options/db/admin") self.db_admin_pwd = self.settings.value("options/db/adminpwd") # 'public' will not be found with the query. if project_name == "public": return 1 try: db = open_psql_db(self.db_host, self.db_name, self.db_port, self.db_admin, self.db_admin_pwd) sql = """SELECT schema_name FROM information_schema.schemata WHERE schema_name = '%s';""" % self.lineEditDbSchema.text().strip( ) query = db.exec_(sql) if query.isActive(): count = query.size() db.close del db if count > 0: return 1 else: return 0 except Exception as e: QgsMessageLog.logMessage(str(e), "VeriSO", Qgis.Critical) return -1
def postprocess_data(self, queries): """Does the postprocessing in the postgresql/postgis database. Returns: -1: If the process fails (e.g. no db connection etc.). Otherwise number of errors occured while postprocessing. """ try: db = open_psql_db(self.db_host, self.db_name, self.db_port, self.db_admin, self.db_admin_pwd) errors = 0 self.report_progress("\n\nInfo: Starting postprocessing...") for sql in queries: self.report_progress("\n\n%s" % sql) query = db.exec_(str(sql)) if not query.isActive(): errors += 1 message = "Error while postprocessing data:" QgsMessageLog.logMessage(tr(message), "VeriSO", Qgis.Critical) QgsMessageLog.logMessage( str(QSqlQuery.lastError(query).text()) + str(sql), "VeriSO", Qgis.Critical) self.report_progress("--> error, see log", 'orange') if errors > 0: self.report_progress( "Error: ...postprocessing completed with errors", "red") if not self.ignore_postprocessing_errors: raise Exception() self.report_progress("Info: ...postprocessing completed") db.close del db except Exception as e: message = "Something went wrong while postprocessing data. You " \ "need to delete the database schema manually." raise VerisoError(message, e)
def test_open_psql_db(self): with self.assertRaises(VerisoError): open_psql_db('rand', 'rand', 'rand', 'rand', 'rand')