def createDbFactory(self, driver): #TODO Treat none return if not ('QPSQL' in QSqlDatabase.drivers()): #Driver wasn't loaded raise Exception('QT PSQL driver not installed!') if not ('QSQLITE' in QSqlDatabase.drivers()): #Driver wasn't loaded raise Exception('QT QSQLITE driver not installed!') dbs = { DsgEnums.DriverSpatiaLite : lambda : SpatialiteDb(), DsgEnums.DriverPostGIS : lambda : PostgisDb(), DsgEnums.DriverGeopackage : lambda : GeopackageDb(), DsgEnums.DriverShapefile : lambda : ShapefileDb() } return dbs[driver]() if driver in dbs else None
def createDbCreatorFactory(self, driverName, createParam, parentWidget=None): #TODO Treat none return if not ('QPSQL' in QSqlDatabase.drivers()): #Driver wasn't loaded QgsMessageLog.logMessage('QT PSQL driver not installed!', 'DSGTools Plugin', Qgis.Critical) return None if not ('QSQLITE' in QSqlDatabase.drivers()): #Driver wasn't loaded QgsMessageLog.logMessage('QT QSQLITE driver not installed!', 'DSGTools Plugin', Qgis.Critical) return None creators = { "QSQLITE": SpatialiteDbCreator, "QPSQL": PostgisDbCreator, "GPKG": GeopackageDbCreator } return creators[driverName]( createParam, parentWidget) if driverName in creators else None
def getDatabase(self, provider_name, procedure): db = None sql = '' try: if provider_name == "oracle" or provider_name == 'QOCISPATIAL': db = QSqlDatabase.addDatabase( 'QOCISPATIAL') # QSqlDatabase.addDatabase('QOCI') sql = "BEGIN " + procedure + "%s" + ";" + " END;" elif provider_name == "postgres" or provider_name == 'QPSQL': db = QSqlDatabase.addDatabase('QPSQL') sql = "SELECT " + procedure + "%s" + ";" elif provider_name == 'QOCISPATIAL8': db = QSqlDatabase.addDatabase('QOCISPATIAL8') sql = "BEGIN " + procedure + "%s" + ";" + " END;" elif provider_name == 'QSQLITE': db = QSqlDatabase.addDatabase('QSQLITE') sql = procedure elif provider_name == 'QMYSQL': db = QSqlDatabase.addDatabase('QMYSQL') sql = procedure elif provider_name == 'QMYSQL3': db = QSqlDatabase.addDatabase('QMYSQL3') sql = procedure elif provider_name == 'QPSQL7': db = QSqlDatabase.addDatabase('QPSQL7') sql = procedure elif provider_name == 'QODBC': db = QSqlDatabase.addDatabase( 'QODBC') # ODBC Driver (includes Microsoft SQL Server) sql = "EXEC " + procedure + "%s" elif provider_name == 'QODBC3': db = QSqlDatabase.addDatabase( 'QODBC3') # ODBC Driver (includes Microsoft SQL Server) sql = "EXEC " + procedure + "%s" else: self.info.msg("Unknown provider: \n" + provider_name + "\nTry using ODBC...") db = QSqlDatabase.addDatabase( 'QODBC') # ODBC Driver (includes Microsoft SQL Server) sql = "EXEC " + procedure + "%s" if db is None: self.info.log("No Database created!") self.info.log("Available database drivers:", QSqlDatabase.drivers()) except Exception as e: self.info.err(e) finally: return db, sql
def is_libqt5sql5_odbc_available(): from qgis.PyQt.QtSql import QSqlDatabase return 'QODBC' in QSqlDatabase.drivers( ) or 'QODBC3' in QSqlDatabase.drivers()