File: info_model.py

package info (click to toggle)
qgis 2.18.28%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,007,948 kB
  • sloc: cpp: 671,774; python: 158,539; xml: 35,690; ansic: 8,346; sh: 1,766; perl: 1,669; sql: 999; yacc: 836; lex: 461; makefile: 292
file content (457 lines) | stat: -rw-r--r-- 17,669 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# -*- coding: utf-8 -*-

"""
/***************************************************************************
Name                 : DB Manager
Description          : Database manager plugin for QGIS
Date                 : May 23, 2011
copyright            : (C) 2011 by Giuseppe Sucameli
email                : brush.tyler@gmail.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.                                   *
 *                                                                         *
 ***************************************************************************/
"""

from qgis.PyQt.QtWidgets import QApplication

from .html_elems import HtmlContent, HtmlSection, HtmlParagraph, HtmlList, HtmlTable, HtmlTableHeader, HtmlTableCol


class DatabaseInfo:

    def __init__(self, db):
        self.db = db

    def __del__(self):
        self.db = None

    def generalInfo(self):
        info = self.db.connector.getInfo()
        tbl = [
            (QApplication.translate("DBManagerPlugin", "Server version: "), info[0])
        ]
        return HtmlTable(tbl)

    def connectionDetails(self):
        tbl = [
            (QApplication.translate("DBManagerPlugin", "Host:"), self.db.connector.host),
            (QApplication.translate("DBManagerPlugin", "User:"), self.db.connector.user)
        ]
        return HtmlTable(tbl)

    def spatialInfo(self):
        ret = []

        info = self.db.connector.getSpatialInfo()
        if info is None:
            return

        tbl = [
            (QApplication.translate("DBManagerPlugin", "Library:"), info[0]),
            ("GEOS:", info[1]),
            ("Proj:", info[2])
        ]
        ret.append(HtmlTable(tbl))

        if not self.db.connector.has_geometry_columns:
            ret.append(HtmlParagraph(
                QApplication.translate("DBManagerPlugin", "<warning> geometry_columns table doesn't exist!\n"
                                                          "This table is essential for many GIS applications for enumeration of tables.")))

        return ret

    def privilegesDetails(self):
        details = self.db.connector.getDatabasePrivileges()
        lst = []
        if details[0]:
            lst.append(QApplication.translate("DBManagerPlugin", "create new schemas"))
        if details[1]:
            lst.append(QApplication.translate("DBManagerPlugin", "create temporary tables"))
        return HtmlList(lst)

    def toHtml(self):
        if self.db is None:
            return HtmlSection(QApplication.translate("DBManagerPlugin", 'Not connected')).toHtml()

        ret = []

        # connection details
        conn_details = self.connectionDetails()
        if conn_details is None:
            pass
        else:
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'Connection details'), conn_details))

        # database information
        general_info = self.generalInfo()
        if general_info is None:
            pass
        else:
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'General info'), general_info))

        # has spatial enabled?
        spatial_info = self.spatialInfo()
        if spatial_info is None:
            pass
        else:
            typename = self.db.connection().typeNameString()
            spatial_info = HtmlContent(spatial_info)
            if not spatial_info.hasContents():
                spatial_info = QApplication.translate("DBManagerPlugin", '<warning> %s support not enabled!') % typename
            ret.append(HtmlSection(typename, spatial_info))

        # privileges
        priv_details = self.privilegesDetails()
        if priv_details is None:
            pass
        else:
            priv_details = HtmlContent(priv_details)
            if not priv_details.hasContents():
                priv_details = QApplication.translate("DBManagerPlugin", '<warning> This user has no privileges!')
            else:
                priv_details = [QApplication.translate("DBManagerPlugin", "User has privileges:"), priv_details]
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'Privileges'), priv_details))

        return HtmlContent(ret).toHtml()


class SchemaInfo:

    def __init__(self, schema):
        self.schema = schema

    def __del__(self):
        self.schema = None

    def generalInfo(self):
        tbl = [
            # ("Tables:", self.schema.tableCount)
        ]
        if self.schema.owner:
            tbl.append((QApplication.translate("DBManagerPlugin", "Owner:"), self.schema.owner))
        if self.schema.comment:
            tbl.append((QApplication.translate("DBManagerPlugin", "Comment:"), self.schema.comment))
        return HtmlTable(tbl)

    def privilegesDetails(self):
        details = self.schema.database().connector.getSchemaPrivileges(self.schema.name)
        lst = []
        if details[0]:
            lst.append(QApplication.translate("DBManagerPlugin", "create new objects"))
        if details[1]:
            lst.append(QApplication.translate("DBManagerPlugin", "access objects"))
        return HtmlList(lst)

    def toHtml(self):
        ret = []

        general_info = self.generalInfo()
        if general_info is None:
            pass
        else:
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'Schema details'), general_info))

        priv_details = self.privilegesDetails()
        if priv_details is None:
            pass
        else:
            priv_details = HtmlContent(priv_details)
            if not priv_details.hasContents():
                priv_details = QApplication.translate("DBManagerPlugin",
                                                      '<warning> This user has no privileges to access this schema!')
            else:
                priv_details = [QApplication.translate("DBManagerPlugin", "User has privileges:"), priv_details]
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'Privileges'), priv_details))

        return HtmlContent(ret).toHtml()


class TableInfo:

    def __init__(self, table):
        self.table = table

    def __del__(self):
        self.table = None

    def generalInfo(self):
        if self.table.rowCount is None:
            # row count information is not displayed yet, so just block
            # table signals to avoid double refreshing (infoViewer->refreshRowCount->tableChanged->infoViewer)
            self.table.blockSignals(True)
            self.table.refreshRowCount()
            self.table.blockSignals(False)

        tbl = [
            (QApplication.translate("DBManagerPlugin", "Relation type:"),
             QApplication.translate("DBManagerPlugin", "View") if self.table.isView else QApplication.translate(
                 "DBManagerPlugin", "Table")),
            (QApplication.translate("DBManagerPlugin", "Rows:"),
             self.table.rowCount if self.table.rowCount is not None else QApplication.translate("DBManagerPlugin",
                                                                                                'Unknown (<a href="action:rows/count">find out</a>)'))
        ]
        if self.table.comment:
            tbl.append((QApplication.translate("DBManagerPlugin", "Comment:"), self.table.comment))

        return HtmlTable(tbl)

    def spatialInfo(self):  # implemented in subclasses
        return None

    def fieldsDetails(self):
        tbl = []

        # define the table header
        header = (
            "#", QApplication.translate("DBManagerPlugin", "Name"), QApplication.translate("DBManagerPlugin", "Type"),
            QApplication.translate("DBManagerPlugin", "Null"), QApplication.translate("DBManagerPlugin", "Default"))
        tbl.append(HtmlTableHeader(header))

        # add table contents
        for fld in self.table.fields():
            is_null_txt = "N" if fld.notNull else "Y"

            # make primary key field underlined
            attrs = {"class": "underline"} if fld.primaryKey else None
            name = HtmlTableCol(fld.name, attrs)

            tbl.append((fld.num, name, fld.type2String(), is_null_txt, fld.default2String()))

        return HtmlTable(tbl, {"class": "header"})

    def constraintsDetails(self):
        if self.table.constraints() is None or len(self.table.constraints()) <= 0:
            return None

        tbl = []

        # define the table header
        header = (QApplication.translate("DBManagerPlugin", "Name"), QApplication.translate("DBManagerPlugin", "Type"),
                  QApplication.translate("DBManagerPlugin", "Column(s)"))
        tbl.append(HtmlTableHeader(header))

        # add table contents
        for con in self.table.constraints():
            # get the fields the constraint is defined on
            cols = map(lambda p: p[1].name if p[1] is not None else u"??? (#%d)" % p[0], iter(con.fields().items()))
            tbl.append((con.name, con.type2String(), u'\n'.join(cols)))

        return HtmlTable(tbl, {"class": "header"})

    def indexesDetails(self):
        if self.table.indexes() is None or len(self.table.indexes()) <= 0:
            return None

        tbl = []

        # define the table header
        header = (
            QApplication.translate("DBManagerPlugin", "Name"), QApplication.translate("DBManagerPlugin", "Column(s)"))
        tbl.append(HtmlTableHeader(header))

        # add table contents
        for idx in self.table.indexes():
            # get the fields the index is defined on
            cols = map(lambda p: p[1].name if p[1] is not None else u"??? (#%d)" % p[0], iter(idx.fields().items()))
            tbl.append((idx.name, u'\n'.join(cols)))

        return HtmlTable(tbl, {"class": "header"})

    def triggersDetails(self):
        if self.table.triggers() is None or len(self.table.triggers()) <= 0:
            return None

        tbl = []

        # define the table header
        header = (
            QApplication.translate("DBManagerPlugin", "Name"), QApplication.translate("DBManagerPlugin", "Function"))
        tbl.append(HtmlTableHeader(header))

        # add table contents
        for trig in self.table.triggers():
            name = u'%(name)s (<a href="action:trigger/%(name)s/%(action)s">%(action)s</a>)' % {"name": trig.name,
                                                                                                "action": "delete"}
            tbl.append((name, trig.function))

        return HtmlTable(tbl, {"class": "header"})

    def getViewDefinition(self):
        if not self.table.isView:
            return None
        return self.table.database().connector.getViewDefinition((self.table.schemaName(), self.table.name))

    def getTableInfo(self):
        ret = []

        general_info = self.generalInfo()
        if general_info is None:
            pass
        else:
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'General info'), general_info))

        # spatial info
        spatial_info = self.spatialInfo()
        if spatial_info is None:
            pass
        else:
            spatial_info = HtmlContent(spatial_info)
            if not spatial_info.hasContents():
                spatial_info = QApplication.translate("DBManagerPlugin", '<warning> This is not a spatial table.')
            ret.append(HtmlSection(self.table.database().connection().typeNameString(), spatial_info))

        # fields
        fields_details = self.fieldsDetails()
        if fields_details is None:
            pass
        else:
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'Fields'), fields_details))

        # constraints
        constraints_details = self.constraintsDetails()
        if constraints_details is None:
            pass
        else:
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'Constraints'), constraints_details))

        # indexes
        indexes_details = self.indexesDetails()
        if indexes_details is None:
            pass
        else:
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'Indexes'), indexes_details))

        # triggers
        triggers_details = self.triggersDetails()
        if triggers_details is None:
            pass
        else:
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'Triggers'), triggers_details))

        return ret

    def getViewInfo(self):
        if not self.table.isView:
            return []

        ret = self.getTableInfo()

        # view definition
        view_def = self.getViewDefinition()
        if view_def is None:
            pass
        else:
            ret.append(HtmlSection(QApplication.translate("DBManagerPlugin", 'View definition'), view_def))

        return ret

    def toHtml(self):
        if self.table.isView:
            ret = self.getViewInfo()
        else:
            ret = self.getTableInfo()
        return HtmlContent(ret).toHtml()


class VectorTableInfo(TableInfo):

    def __init__(self, table):
        TableInfo.__init__(self, table)

    def spatialInfo(self):
        ret = []
        if self.table.geomType is None:
            return ret

        tbl = [
            (QApplication.translate("DBManagerPlugin", "Column:"), self.table.geomColumn),
            (QApplication.translate("DBManagerPlugin", "Geometry:"), self.table.geomType)
        ]

        # only if we have info from geometry_columns
        if self.table.geomDim:
            tbl.append((QApplication.translate("DBManagerPlugin", "Dimension:"), self.table.geomDim))

        srid = self.table.srid if self.table.srid is not None else -1
        sr_info = self.table.database().connector.getSpatialRefInfo(srid) if srid != -1 else QApplication.translate(
            "DBManagerPlugin", "Undefined")
        if sr_info:
            tbl.append((QApplication.translate("DBManagerPlugin", "Spatial ref:"), u"%s (%d)" % (sr_info, srid)))

        # estimated extent
        if not self.table.isView:
            if self.table.estimatedExtent is None:
                # estimated extent information is not displayed yet, so just block
                # table signals to avoid double refreshing (infoViewer->refreshEstimatedExtent->tableChanged->infoViewer)
                self.table.blockSignals(True)
                self.table.refreshTableEstimatedExtent()
                self.table.blockSignals(False)

            if self.table.estimatedExtent is not None and self.table.estimatedExtent[0] is not None:
                estimated_extent_str = '%.5f, %.5f - %.5f, %.5f' % self.table.estimatedExtent
                tbl.append((QApplication.translate("DBManagerPlugin", "Estimated extent:"), estimated_extent_str))

        # extent
        if self.table.extent is not None and self.table.extent[0] is not None:
            extent_str = '%.5f, %.5f - %.5f, %.5f' % self.table.extent
        else:
            extent_str = QApplication.translate("DBManagerPlugin",
                                                '(unknown) (<a href="action:extent/get">find out</a>)')
        tbl.append((QApplication.translate("DBManagerPlugin", "Extent:"), extent_str))

        ret.append(HtmlTable(tbl))

        # is there an entry in geometry_columns?
        if self.table.geomType.lower() == 'geometry':
            ret.append(HtmlParagraph(
                QApplication.translate("DBManagerPlugin", "<warning> There is no entry in geometry_columns!")))

        # find out whether the geometry column has spatial index on it
        if not self.table.isView:
            if not self.table.hasSpatialIndex():
                ret.append(HtmlParagraph(QApplication.translate("DBManagerPlugin",
                                                                '<warning> No spatial index defined (<a href="action:spatialindex/create">create it</a>)')))

        return ret


class RasterTableInfo(TableInfo):

    def __init__(self, table):
        TableInfo.__init__(self, table)

    def spatialInfo(self):
        ret = []
        if self.table.geomType is None:
            return ret

        tbl = [
            (QApplication.translate("DBManagerPlugin", "Column:"), self.table.geomColumn),
            (QApplication.translate("DBManagerPlugin", "Geometry:"), self.table.geomType)
        ]

        # only if we have info from geometry_columns
        srid = self.table.srid if self.table.srid is not None else -1
        sr_info = self.table.database().connector.getSpatialRefInfo(srid) if srid != -1 else QApplication.translate(
            "DBManagerPlugin", "Undefined")
        if sr_info:
            tbl.append((QApplication.translate("DBManagerPlugin", "Spatial ref:"), u"%s (%d)" % (sr_info, srid)))

        # extent
        if self.table.extent is not None and self.table.extent[0] is not None:
            extent_str = '%.5f, %.5f - %.5f, %.5f' % self.table.extent
        else:
            extent_str = QApplication.translate("DBManagerPlugin",
                                                '(unknown) (<a href="action:extent/get">find out</a>)')
        tbl.append((QApplication.translate("DBManagerPlugin", "Extent:"), extent_str))

        ret.append(HtmlTable(tbl))
        return ret