|
| 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)) |
0 commit comments