Skip to content
Permalink
Browse files

[processing] Use core connections API for Spatialite algorithms instead

of Python reimplementation
  • Loading branch information
nyalldawson committed Mar 16, 2020
1 parent 2dc4329 commit ba010f8803da340fa6bf99e36dbf1720328cd4bf
@@ -32,10 +32,12 @@
QgsProcessingParameterField,
QgsProcessingParameterString,
QgsProcessingParameterBoolean,
QgsWkbTypes)
QgsWkbTypes,
QgsProviderRegistry,
QgsProviderConnectionException,
QgsAbstractDatabaseProviderConnection)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import spatialite


class ImportIntoSpatialite(QgisAlgorithm):
@@ -98,6 +100,7 @@ def tags(self):

def processAlgorithm(self, parameters, context, feedback):
database = self.parameterAsVectorLayer(parameters, self.DATABASE, context)

databaseuri = database.dataProvider().dataSourceUri()
uri = QgsDataSourceUri(databaseuri)
if uri.database() is '':
@@ -106,7 +109,12 @@ def processAlgorithm(self, parameters, context, feedback):
elif '|layerid' in databaseuri:
databaseuri = databaseuri[:databaseuri.find('|layerid')]
uri = QgsDataSourceUri('dbname=\'%s\'' % (databaseuri))
db = spatialite.GeoDB(uri)

try:
md = QgsProviderRegistry.instance().providerMetadata('spatialite')
conn = md.createConnection(uri.uri(), {})
except QgsProviderConnectionException:
raise QgsProcessingException(self.tr('Could not connect to {}').format(uri.uri()))

overwrite = self.parameterAsBoolean(parameters, self.OVERWRITE, context)
createIndex = self.parameterAsBoolean(parameters, self.CREATEINDEX, context)
@@ -148,7 +156,6 @@ def processAlgorithm(self, parameters, context, feedback):
if source.wkbType() == QgsWkbTypes.NoGeometry:
geomColumn = None

uri = db.uri
uri.setDataSource('', table, geomColumn, '', primaryKeyField)

if encoding:
@@ -178,6 +185,11 @@ def processAlgorithm(self, parameters, context, feedback):
self.tr('Error importing to Spatialite\n{0}').format(exporter.errorMessage()))

if geomColumn and createIndex:
db.create_spatial_index(table, geomColumn)
try:
options = QgsAbstractDatabaseProviderConnection.SpatialIndexOptions()
options.geometryColumnName = geomColumn
conn.createSpatialIndex('', table, options)
except QgsProviderConnectionException as e:
raise QgsProcessingException(self.tr('Error creating spatial index:\n{0}').format(e))

return {}
@@ -26,7 +26,9 @@
QgsProcessingAlgorithm,
QgsProcessingException,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterString)
QgsProcessingParameterString,
QgsProviderRegistry,
QgsProviderConnectionException)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import spatialite
@@ -72,12 +74,17 @@ def processAlgorithm(self, parameters, context, feedback):
elif '|layerid' in databaseuri:
databaseuri = databaseuri[:databaseuri.find('|layerid')]
uri = QgsDataSourceUri('dbname=\'%s\'' % (databaseuri))
db = spatialite.GeoDB(uri)

try:
md = QgsProviderRegistry.instance().providerMetadata('spatialite')
conn = md.createConnection(uri.uri(), {})
except QgsProviderConnectionException:
raise QgsProcessingException(self.tr('Could not connect to {}').format(uri.uri()))

sql = self.parameterAsString(parameters, self.SQL, context).replace('\n', ' ')
try:
db._exec_sql_and_commit(str(sql))
except spatialite.DbError as e:
raise QgsProcessingException(
self.tr('Error executing SQL:\n{0}').format(str(e)))
conn.executeSql(sql)
except QgsProviderConnectionException as e:
raise QgsProcessingException(self.tr('Error executing SQL:\n{0}').format(e))

return {}

0 comments on commit ba010f8

Please sign in to comment.
You can’t perform that action at this time.