2012-11-07 21:04:51 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
PostGISExecuteSQL.py
|
|
|
|
---------------------
|
|
|
|
Date : October 2012
|
2012-11-20 11:01:29 +01:00
|
|
|
Copyright : (C) 2012 by Victor Olaya and Carterix Geomatics
|
2012-11-07 21:04:51 +01:00
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
2012-11-10 00:31:01 +01:00
|
|
|
__author__ = 'Victor Olaya, Carterix Geomatics'
|
2012-11-07 21:04:51 +01:00
|
|
|
__date__ = 'October 2012'
|
2012-11-10 00:31:01 +01:00
|
|
|
__copyright__ = '(C) 2012, Victor Olaya, Carterix Geomatics'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-11-07 21:04:51 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-11-07 21:04:51 +01:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import os
|
|
|
|
from qgis.core import *
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from processing.core.GeoAlgorithmExecutionException import \
|
|
|
|
GeoAlgorithmExecutionException
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.parameters.ParameterString import ParameterString
|
2014-04-17 01:41:25 +02:00
|
|
|
import postgis_utils
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-11-07 21:04:51 +01:00
|
|
|
|
2012-11-10 00:31:01 +01:00
|
|
|
class PostGISExecuteSQL(GeoAlgorithm):
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
DATABASE = 'DATABASE'
|
|
|
|
SQL = 'SQL'
|
2012-11-07 21:04:51 +01:00
|
|
|
|
|
|
|
def getIcon(self):
|
2014-04-17 01:41:25 +02:00
|
|
|
return QIcon(os.path.dirname(__file__) + '/../../images/postgis.png')
|
2012-11-07 21:04:51 +01:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
2012-12-10 00:12:07 +01:00
|
|
|
|
|
|
|
connection = self.getParameterValue(self.DATABASE)
|
2012-11-07 21:04:51 +01:00
|
|
|
settings = QSettings()
|
2013-10-01 20:52:22 +03:00
|
|
|
mySettings = '/PostgreSQL/connections/' + connection
|
2012-12-15 18:49:32 +01:00
|
|
|
try:
|
2013-10-01 20:52:22 +03:00
|
|
|
database = settings.value(mySettings + '/database')
|
|
|
|
username = settings.value(mySettings + '/username')
|
|
|
|
host = settings.value(mySettings + '/host')
|
|
|
|
port = settings.value(mySettings + '/port', type=int)
|
|
|
|
password = settings.value(mySettings + '/password')
|
2012-12-15 18:49:32 +01:00
|
|
|
except Exception, e:
|
2013-10-01 20:52:22 +03:00
|
|
|
raise GeoAlgorithmExecutionException(
|
|
|
|
'Wrong database connection name: ' + connection)
|
2012-11-07 21:04:51 +01:00
|
|
|
try:
|
2013-10-01 20:52:22 +03:00
|
|
|
self.db = postgis_utils.GeoDB(host=host, port=port,
|
|
|
|
dbname=database, user=username, passwd=password)
|
2012-11-07 21:04:51 +01:00
|
|
|
except postgis_utils.DbError, e:
|
2013-10-01 20:52:22 +03:00
|
|
|
raise GeoAlgorithmExecutionException(
|
|
|
|
"Couldn't connect to database:\n" + e.message)
|
2012-11-07 21:04:51 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
sql = self.getParameterValue(self.SQL).replace('\n', ' ')
|
2012-11-07 21:04:51 +01:00
|
|
|
try:
|
2012-12-15 18:49:32 +01:00
|
|
|
self.db._exec_sql_and_commit(str(sql))
|
2012-11-07 21:04:51 +01:00
|
|
|
except postgis_utils.DbError, e:
|
2013-10-01 20:52:22 +03:00
|
|
|
raise GeoAlgorithmExecutionException('Error executing SQL:\n'
|
|
|
|
+ e.message)
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2012-11-07 21:04:51 +01:00
|
|
|
def defineCharacteristics(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.name = 'PostGIS execute SQL'
|
|
|
|
self.group = 'PostGIS management tools'
|
|
|
|
self.addParameter(ParameterString(self.DATABASE, 'Database'))
|
|
|
|
self.addParameter(ParameterString(self.SQL, 'SQL query', '', True))
|