[DBManager] Allow to load a layer without primary key

This commit is contained in:
Hugo Mercier 2015-03-27 14:32:41 +01:00
parent 00618fcabf
commit d684c8c749
4 changed files with 43 additions and 8 deletions

View File

@ -208,9 +208,24 @@ class Database(DbItemObject):
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:

View File

@ -144,6 +144,8 @@ class SLDatabase(Database):
return Database.runAction(self, action)
def uniqueIdFunction(self):
return None
class SLTable(Table):
def __init__(self, row, db, schema=None):

View File

@ -178,18 +178,17 @@ class DlgSqlWindow(QDialog, Ui_Dialog):
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

View File

@ -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>