Skip to content

Commit bde0f6e

Browse files
authoredOct 21, 2016
Merge pull request #3650 from nirvn/processing_spatialite_sql
[processing] add a "Spatialite execute SQL" algorithm
2 parents 2b9f475 + d83e650 commit bde0f6e

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ qgis:polygonize: >
333333
qgis:polygonstolines: >
334334
This algorithm takes a polygon layer and creates a line layer, with lines representing the rings of the polygons in the input layer.
335335

336+
qgis:spatialiteexecutesql: >
337+
This algorithm performs a SQL database query on a Spatialite database.
338+
336339
qgis:postgisexecutesql: >
337340
This algorithm performs a SQL database query on a PostGIS database connected to QGIS.
338341

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
from .RandomPointsPolygonsVariable import RandomPointsPolygonsVariable
132132
from .RandomPointsAlongLines import RandomPointsAlongLines
133133
from .PointsToPaths import PointsToPaths
134+
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
134135
from .PostGISExecuteSQL import PostGISExecuteSQL
135136
from .ImportIntoPostGIS import ImportIntoPostGIS
136137
from .SetVectorStyle import SetVectorStyle
@@ -213,6 +214,7 @@ def __init__(self):
213214
RandomPointsLayer(), RandomPointsPolygonsFixed(),
214215
RandomPointsPolygonsVariable(),
215216
RandomPointsAlongLines(), PointsToPaths(),
217+
SpatialiteExecuteSQL(),
216218
PostGISExecuteSQL(), ImportIntoPostGIS(),
217219
SetVectorStyle(), SetRasterStyle(),
218220
SelectByExpression(), HypsometricCurves(),
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SpatialiteExecuteSQL.py
6+
---------------------
7+
Date : October 2016
8+
Copyright : (C) 2016 by Mathieu Pellerin
9+
Email : nirvn dot asia at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
from builtins import str
20+
21+
__author__ = 'Mathieu Pellerin'
22+
__date__ = 'October 2016'
23+
__copyright__ = '(C) 2016, Mathieu Pellerin'
24+
25+
# This will get replaced with a git SHA1 when you do a git archive
26+
27+
__revision__ = '$Format:%H$'
28+
29+
from processing.core.GeoAlgorithm import GeoAlgorithm
30+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
31+
from processing.core.parameters import ParameterVector
32+
from processing.core.parameters import ParameterString
33+
from processing.tools import spatialite
34+
35+
from qgis.core import QgsDataSourceUri, QgsMessageLog
36+
37+
38+
class SpatialiteExecuteSQL(GeoAlgorithm):
39+
40+
DATABASE = 'DATABASE'
41+
SQL = 'SQL'
42+
43+
def defineCharacteristics(self):
44+
self.name, self.i18n_name = self.trAlgorithm('Spatialite execute SQL')
45+
self.group, self.i18n_group = self.trAlgorithm('Database')
46+
self.addParameter(ParameterVector(self.DATABASE, self.tr('File Database'), False, False))
47+
self.addParameter(ParameterString(self.SQL, self.tr('SQL query'), '', True))
48+
49+
def processAlgorithm(self, progress):
50+
database = self.getParameterValue(self.DATABASE)
51+
uri = QgsDataSourceUri(database)
52+
if uri.database() is '':
53+
if '|layerid' in database:
54+
database = database[:database.find('|layerid')]
55+
uri = QgsDataSourceUri('dbname=\'%s\'' % (database))
56+
self.db = spatialite.GeoDB(uri)
57+
sql = self.getParameterValue(self.SQL).replace('\n', ' ')
58+
try:
59+
self.db._exec_sql_and_commit(str(sql))
60+
except spatialite.DbError as e:
61+
raise GeoAlgorithmExecutionException(
62+
self.tr('Error executing SQL:\n%s') % str(e))

‎python/plugins/processing/tools/spatialite.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929

3030
from qgis.utils import spatialite_connect
3131

32+
import sqlite3 as sqlite
33+
3234

3335
class DbError(Exception):
3436

3537
def __init__(self, message, query=None):
3638
# Save error. funny that the variables are in utf-8
37-
self.message = str(message, 'utf-8')
38-
self.query = (str(query, 'utf-8') if query is not None else None)
39+
self.message = str(message)
40+
self.query = (str(query) if query is not None else None)
3941

4042
def __str__(self):
4143
return 'MESSAGE: %s\nQUERY: %s' % (self.message, self.query)

0 commit comments

Comments
 (0)
Please sign in to comment.