Skip to content

Commit e440dee

Browse files
author
Hugo Mercier
committed
[DBManager] Add a 'create view' button
1 parent 00276e2 commit e440dee

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

python/plugins/db_manager/db_plugins/connector.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def hasCustomQuerySupport(self):
5757
def hasTableColumnEditingSupport(self):
5858
return False
5959

60+
def hasCreateSpatialViewSupport( self ):
61+
return False
6062

6163
def execution_error_types(self):
6264
raise Exception("DBConnector.execution_error_types() is an abstract method")

python/plugins/db_manager/db_plugins/postgis/connector.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ def hasCustomQuerySupport(self):
176176
def hasTableColumnEditingSupport(self):
177177
return True
178178

179+
def hasCreateSpatialViewSupport( self ):
180+
return True
179181

180182
def fieldTypes(self):
181183
return [
@@ -761,6 +763,9 @@ def createView(self, view, query):
761763
sql = u"CREATE VIEW %s AS %s" % (self.quoteId(view), query)
762764
self._execute_and_commit(sql)
763765

766+
def createSpatialView(self, view, query):
767+
self.createView(view, query)
768+
764769
def deleteView(self, view):
765770
sql = u"DROP VIEW %s" % self.quoteId(view)
766771
self._execute_and_commit(sql)

python/plugins/db_manager/db_plugins/spatialite/connector.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def hasCustomQuerySupport(self):
122122
def hasTableColumnEditingSupport(self):
123123
return False
124124

125+
def hasCreateSpatialViewSupport(self):
126+
return True
125127

126128
def fieldTypes(self):
127129
return [
@@ -468,13 +470,61 @@ def createView(self, view, query):
468470
self._execute_and_commit(sql)
469471

470472
def deleteView(self, view):
473+
c = self._get_cursor()
474+
471475
sql = u"DROP VIEW %s" % self.quoteId(view)
472-
self._execute_and_commit(sql)
476+
self._execute(c, sql)
477+
478+
# update geometry_columns
479+
if self.has_geometry_columns:
480+
sql = u"DELETE FROM geometry_columns WHERE f_table_name = %s" % self.quoteString(view)
481+
self._execute(c, sql)
482+
483+
self._commit()
473484

474485
def renameView(self, view, new_name):
475486
""" rename view """
476487
return self.renameTable(view, new_name)
477488

489+
def createSpatialView(self, view, query):
490+
self.createView(view, query)
491+
# get type info about the view
492+
sql = u"PRAGMA table_info(%s)" % self.quoteString(view)
493+
c = self._execute( None, sql )
494+
geom_col = None
495+
for r in c.fetchall():
496+
if r[2].upper() in ('POINT', 'LINESTRING', 'POLYGON',
497+
'MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON'):
498+
geom_col = r[1]
499+
break
500+
if geom_col is None:
501+
return
502+
503+
# get geometry type and srid
504+
sql = u"SELECT geometrytype(%s), srid(%s) FROM %s LIMIT 1" % (self.quoteId(geom_col), self.quoteId(geom_col), self.quoteId(view))
505+
c = self._execute( None, sql )
506+
r = c.fetchone()
507+
if r is None:
508+
return
509+
510+
gtype, gsrid = r
511+
gdim = 'XY'
512+
if ' ' in gtype:
513+
zm = gtype.split(' ')[1]
514+
gtype = gtype.split(' ')[0]
515+
gdim += zm
516+
try:
517+
wkbType = ('POINT', 'LINESTRING', 'POLYGON', 'MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON').index(gtype) + 1
518+
except:
519+
wkbType = 0
520+
if 'Z' in gdim:
521+
wkbType += 1000
522+
if 'M' in gdim:
523+
wkbType += 2000
524+
525+
sql = u"""INSERT INTO geometry_columns (f_table_name, f_geometry_column, geometry_type, coord_dimension, srid, spatial_index_enabled)
526+
VALUES (%s, %s, %s, %s, %s, 0)""" % (self.quoteId(view), self.quoteId(geom_col), wkbType, len(gdim), gsrid)
527+
self._execute_and_commit(sql)
478528

479529
def runVacuum(self):
480530
""" run vacuum on the db """

python/plugins/db_manager/dlg_sql_window.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"""
2424

2525
from PyQt4.QtCore import Qt, QObject, QSettings, QByteArray, SIGNAL
26-
from PyQt4.QtGui import QDialog, QAction, QKeySequence, QDialogButtonBox, QApplication, QCursor, QMessageBox, QClipboard
26+
from PyQt4.QtGui import QDialog, QAction, QKeySequence, QDialogButtonBox, QApplication, QCursor, QMessageBox, QClipboard, QInputDialog
2727
from PyQt4.Qsci import QsciAPIs
2828

2929
from qgis.core import QgsProject
@@ -89,6 +89,11 @@ def __init__(self, iface, db, parent=None):
8989
self.connect(self.loadAsLayerGroup, SIGNAL("toggled(bool)"), self.loadAsLayerToggled)
9090
self.loadAsLayerToggled(False)
9191

92+
self._createViewAvailable = self.db.connector.hasCreateSpatialViewSupport()
93+
self.btnCreateView.setVisible( self._createViewAvailable )
94+
if self._createViewAvailable:
95+
self.connect( self.btnCreateView, SIGNAL("clicked()"), self.createView )
96+
9297
self.queryBuilderFirst = True
9398
self.connect( self.queryBuilderBtn, SIGNAL("clicked()"), self.displayQueryBuilder )
9499

@@ -338,3 +343,12 @@ def displayQueryBuilder( self ):
338343
if r == QDialog.Accepted:
339344
self.editSql.setText( dlg.query )
340345

346+
def createView( self ):
347+
name, ok = QInputDialog.getText(None, "View name", "View name")
348+
if ok:
349+
try:
350+
self.db.connector.createSpatialView( name, self.editSql.text() )
351+
except BaseError as e:
352+
DlgDbError.showError(e, self)
353+
354+

python/plugins/db_manager/ui/DlgSqlWindow.ui

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@
102102
</property>
103103
</widget>
104104
</item>
105+
<item>
106+
<widget class="QPushButton" name="btnCreateView">
107+
<property name="text">
108+
<string>Create a view</string>
109+
</property>
110+
</widget>
111+
</item>
105112
<item>
106113
<spacer>
107114
<property name="orientation">

0 commit comments

Comments
 (0)