ExecuteSql.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. ***************************************************************************
  3. ExecuteSql.py
  4. ---------------------
  5. Date : November 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'November 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. from qgis.core import (QgsProcessingException,
  21. QgsProcessingParameterDefinition,
  22. QgsProcessingParameterFeatureSource,
  23. QgsProcessingParameterEnum,
  24. QgsProcessingParameterString,
  25. QgsProcessingParameterVectorDestination)
  26. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  27. from processing.algs.gdal.GdalUtils import GdalUtils
  28. class ExecuteSql(GdalAlgorithm):
  29. INPUT = 'INPUT'
  30. SQL = 'SQL'
  31. DIALECT = 'DIALECT'
  32. OPTIONS = 'OPTIONS'
  33. OUTPUT = 'OUTPUT'
  34. def __init__(self):
  35. super().__init__()
  36. def initAlgorithm(self, config=None):
  37. self.dialects = ((self.tr('None'), ''),
  38. (self.tr('OGR SQL'), 'ogrsql'),
  39. (self.tr('SQLite'), 'sqlite'))
  40. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  41. self.tr('Input layer')))
  42. self.addParameter(QgsProcessingParameterString(self.SQL,
  43. self.tr('SQL expression'),
  44. defaultValue=''))
  45. self.addParameter(QgsProcessingParameterEnum(self.DIALECT,
  46. self.tr('SQL dialect'),
  47. options=[i[0] for i in self.dialects],
  48. allowMultiple=False,
  49. defaultValue=0))
  50. options_param = QgsProcessingParameterString(self.OPTIONS,
  51. self.tr('Additional creation options'),
  52. defaultValue='',
  53. optional=True)
  54. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  55. self.addParameter(options_param)
  56. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  57. self.tr('SQL result')))
  58. def name(self):
  59. return 'executesql'
  60. def displayName(self):
  61. return self.tr('Execute SQL')
  62. def group(self):
  63. return self.tr('Vector miscellaneous')
  64. def groupId(self):
  65. return 'vectormiscellaneous'
  66. def commandName(self):
  67. return "ogr2ogr"
  68. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  69. ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  70. sql = self.parameterAsString(parameters, self.SQL, context)
  71. options = self.parameterAsString(parameters, self.OPTIONS, context)
  72. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  73. self.setOutputValue(self.OUTPUT, outFile)
  74. output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  75. if not sql:
  76. raise QgsProcessingException(
  77. self.tr('Empty SQL. Please enter valid SQL expression and try again.'))
  78. arguments = [
  79. output,
  80. ogrLayer,
  81. '-sql',
  82. sql
  83. ]
  84. dialect = self.dialects[self.parameterAsEnum(parameters, self.DIALECT, context)][1]
  85. if dialect:
  86. arguments.append('-dialect')
  87. arguments.append(dialect)
  88. if options:
  89. arguments.append(options)
  90. if outputFormat:
  91. arguments.append(f'-f {outputFormat}')
  92. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]