Skip to content
Permalink
Browse files

[DBManager] Allow to load a layer without primary key

  • Loading branch information
mhugo committed Mar 27, 2015
1 parent 00618fc commit d684c8c749ad937b52a305e6b2a1ae6de4b055b8
@@ -208,9 +208,24 @@ def sqlResultModel(self, sql, parent):

return SqlResultModel(self, sql, parent)

def uniqueIdFunction(self):
"""Return a SQL function used to generate a unique id for rows of a query"""
# may be overloaded by derived classes
return "row_number() over ()"

def toSqlLayer(self, sql, geomCol, uniqueCol, layerName="QueryLayer", layerType=None, avoidSelectById=False):
from qgis.core import QgsMapLayer, QgsVectorLayer, QgsRasterLayer

if uniqueCol is None:
if hasattr(self, 'uniqueIdFunction'):
uniqueFct = self.uniqueIdFunction()
if uniqueFct is not None:
q = 1
while "_subq_%d_" % q in sql:
q += 1
sql = "SELECT %s AS _uid_,* FROM (%s) AS _subq_%d_" % (uniqueFct, sql, q)
uniqueCol = "_uid_"

uri = self.uri()
uri.setDataSource("", u"(%s\n)" % sql, geomCol, "", uniqueCol)
if avoidSelectById:
@@ -144,6 +144,8 @@ def runAction(self, action):

return Database.runAction(self, action)

def uniqueIdFunction(self):
return None

class SLTable(Table):
def __init__(self, row, db, schema=None):
@@ -178,18 +178,17 @@ def executeSql(self):
QApplication.restoreOverrideCursor()

def loadSqlLayer(self):
uniqueFieldName = self.uniqueCombo.currentText()
hasUniqueField = self.uniqueColumnCheck.checkState() == Qt.Checked
if hasUniqueField:
uniqueFieldName = self.uniqueCombo.currentText()
else:
uniqueFieldName = None
hasGeomCol = self.hasGeometryCol.checkState() == Qt.Checked
if hasGeomCol:
geomFieldName = self.geomCombo.currentText()
else:
geomFieldName = None

if (hasGeomCol and geomFieldName == "") or uniqueFieldName == "":
QMessageBox.warning(self, self.tr("DB Manager"), self.tr(
"You must fill the required fields: \ngeometry column - column with unique integer values"))
return

query = self.editSql.text()
if query == "":
return
@@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>747</width>
<width>800</width>
<height>525</height>
</rect>
</property>
@@ -181,7 +181,7 @@
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLabel" name="label_4">
<widget class="QCheckBox" name="uniqueColumnCheck">
<property name="text">
<string>Column with unique
integer values</string>
@@ -190,6 +190,9 @@ integer values</string>
</item>
<item>
<widget class="QComboBox" name="uniqueCombo">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
@@ -403,5 +406,21 @@ columns</string>
</hint>
</hints>
</connection>
<connection>
<sender>uniqueColumnCheck</sender>
<signal>toggled(bool)</signal>
<receiver>uniqueCombo</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>109</x>
<y>385</y>
</hint>
<hint type="destinationlabel">
<x>274</x>
<y>385</y>
</hint>
</hints>
</connection>
</connections>
</ui>

0 comments on commit d684c8c

Please sign in to comment.
You can’t perform that action at this time.