File: plugin.py

package info (click to toggle)
qgis 3.10.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 889,896 kB
  • sloc: cpp: 986,392; python: 156,157; xml: 12,700; perl: 3,286; sh: 2,637; ansic: 2,085; sql: 1,514; javascript: 1,363; yacc: 1,000; lex: 562; lisp: 411; makefile: 234
file content (197 lines) | stat: -rw-r--r-- 6,103 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-

"""
/***************************************************************************
Name                 : DB Manager plugin for virtual layers
Date                 : December 2015
copyright            : (C) 2015 by Hugo Mercier
email                : hugo dot mercier at oslandia 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.                                   *
 *                                                                         *
 ***************************************************************************/
"""

# this will disable the dbplugin if the connector raise an ImportError
from .connector import VLayerConnector

from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsApplication, QgsVectorLayer, QgsProject, QgsVirtualLayerDefinition

from ..plugin import DBPlugin, Database, Table, VectorTable, TableField


def classFactory():
    return VLayerDBPlugin


class VLayerDBPlugin(DBPlugin):

    @classmethod
    def icon(self):
        return QgsApplication.getThemeIcon("/mIconVirtualLayer.svg")

    def connectionIcon(self):
        return QgsApplication.getThemeIcon("/providerQgis.svg")

    @classmethod
    def typeName(self):
        return 'vlayers'

    @classmethod
    def typeNameString(self):
        return QCoreApplication.translate('db_manager', 'Virtual Layers')

    @classmethod
    def providerName(self):
        return 'virtual'

    @classmethod
    def connectionSettingsKey(self):
        return 'vlayers'

    @classmethod
    def connections(self):
        return [VLayerDBPlugin(QCoreApplication.translate('db_manager', 'Project layers'))]

    def databasesFactory(self, connection, uri):
        return FakeDatabase(connection, uri)

    def database(self):
        return self.db

    # def info( self ):

    def connect(self, parent=None):
        self.connectToUri("qgis")
        return True


class FakeDatabase(Database):

    def __init__(self, connection, uri):
        Database.__init__(self, connection, uri)

    def connectorsFactory(self, uri):
        return VLayerConnector(uri)

    def dataTablesFactory(self, row, db, schema=None):
        return LTable(row, db, schema)

    def vectorTablesFactory(self, row, db, schema=None):
        return LVectorTable(row, db, schema)

    def rasterTablesFactory(self, row, db, schema=None):
        return None

    def info(self):
        from .info_model import LDatabaseInfo
        return LDatabaseInfo(self)

    def sqlResultModel(self, sql, parent):
        from .data_model import LSqlResultModel
        return LSqlResultModel(self, sql, parent)

    def sqlResultModelAsync(self, sql, parent):
        from .data_model import LSqlResultModelAsync
        return LSqlResultModelAsync(self, sql, parent)

    def toSqlLayer(self, sql, geomCol, uniqueCol, layerName="QueryLayer", layerType=None, avoidSelectById=False, _filter=""):
        df = QgsVirtualLayerDefinition()
        df.setQuery(sql)
        if uniqueCol is not None:
            uniqueCol = uniqueCol.strip('"').replace('""', '"')
            df.setUid(uniqueCol)
        if geomCol is not None:
            df.setGeometryField(geomCol)
        vl = QgsVectorLayer(df.toString(), layerName, "virtual")
        if _filter:
            vl.setSubsetString(_filter)
        return vl

    def registerDatabaseActions(self, mainWindow):
        return

    def runAction(self, action):
        return

    def uniqueIdFunction(self):
        return None

    def explicitSpatialIndex(self):
        return True

    def spatialIndexClause(self, src_table, src_column, dest_table, dest_column):
        return '"%s"._search_frame_ = "%s"."%s"' % (src_table, dest_table, dest_column)

    def supportsComment(self):
        return False


class LTable(Table):

    def __init__(self, row, db, schema=None):
        Table.__init__(self, db, None)
        self.name, self.isView, self.isSysTable = row

    def tableFieldsFactory(self, row, table):
        return LTableField(row, table)

    def tableDataModel(self, parent):
        from .data_model import LTableDataModel
        return LTableDataModel(self, parent)

    def canBeAddedToCanvas(self):
        return False


class LVectorTable(LTable, VectorTable):

    def __init__(self, row, db, schema=None):
        LTable.__init__(self, row[:-5], db, schema)
        VectorTable.__init__(self, db, schema)
        # SpatiaLite does case-insensitive checks for table names, but the
        # SL provider didn't do the same in Qgis < 1.9, so self.geomTableName
        # stores the table name like stored in the geometry_columns table
        self.geomTableName, self.geomColumn, self.geomType, self.geomDim, self.srid = row[
            -5:]

    def uri(self):
        uri = self.database().uri()
        uri.setDataSource('', self.geomTableName, self.geomColumn)
        return uri

    def hasSpatialIndex(self, geom_column=None):
        return True

    def createSpatialIndex(self, geom_column=None):
        return

    def deleteSpatialIndex(self, geom_column=None):
        return

    def refreshTableEstimatedExtent(self):
        self.extent = self.database().connector.getTableExtent(
            ("id", self.geomTableName), None)

    def runAction(self, action):
        return

    def toMapLayer(self):
        return QgsProject.instance().mapLayer(self.geomTableName)


class LTableField(TableField):

    def __init__(self, row, table):
        TableField.__init__(self, table)
        self.num, self.name, self.dataType, self.notNull, self.default, self.primaryKey = row
        self.hasDefault = self.default