Skip to content

Commit d684c8c

Browse files
author
Hugo Mercier
committed
[DBManager] Allow to load a layer without primary key
1 parent 00618fc commit d684c8c

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

python/plugins/db_manager/db_plugins/plugin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,24 @@ def sqlResultModel(self, sql, parent):
208208

209209
return SqlResultModel(self, sql, parent)
210210

211+
def uniqueIdFunction(self):
212+
"""Return a SQL function used to generate a unique id for rows of a query"""
213+
# may be overloaded by derived classes
214+
return "row_number() over ()"
215+
211216
def toSqlLayer(self, sql, geomCol, uniqueCol, layerName="QueryLayer", layerType=None, avoidSelectById=False):
212217
from qgis.core import QgsMapLayer, QgsVectorLayer, QgsRasterLayer
213218

219+
if uniqueCol is None:
220+
if hasattr(self, 'uniqueIdFunction'):
221+
uniqueFct = self.uniqueIdFunction()
222+
if uniqueFct is not None:
223+
q = 1
224+
while "_subq_%d_" % q in sql:
225+
q += 1
226+
sql = "SELECT %s AS _uid_,* FROM (%s) AS _subq_%d_" % (uniqueFct, sql, q)
227+
uniqueCol = "_uid_"
228+
214229
uri = self.uri()
215230
uri.setDataSource("", u"(%s\n)" % sql, geomCol, "", uniqueCol)
216231
if avoidSelectById:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def runAction(self, action):
144144

145145
return Database.runAction(self, action)
146146

147+
def uniqueIdFunction(self):
148+
return None
147149

148150
class SLTable(Table):
149151
def __init__(self, row, db, schema=None):

python/plugins/db_manager/dlg_sql_window.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,17 @@ def executeSql(self):
178178
QApplication.restoreOverrideCursor()
179179

180180
def loadSqlLayer(self):
181-
uniqueFieldName = self.uniqueCombo.currentText()
181+
hasUniqueField = self.uniqueColumnCheck.checkState() == Qt.Checked
182+
if hasUniqueField:
183+
uniqueFieldName = self.uniqueCombo.currentText()
184+
else:
185+
uniqueFieldName = None
182186
hasGeomCol = self.hasGeometryCol.checkState() == Qt.Checked
183187
if hasGeomCol:
184188
geomFieldName = self.geomCombo.currentText()
185189
else:
186190
geomFieldName = None
187191

188-
if (hasGeomCol and geomFieldName == "") or uniqueFieldName == "":
189-
QMessageBox.warning(self, self.tr("DB Manager"), self.tr(
190-
"You must fill the required fields: \ngeometry column - column with unique integer values"))
191-
return
192-
193192
query = self.editSql.text()
194193
if query == "":
195194
return

python/plugins/db_manager/ui/DlgSqlWindow.ui

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>747</width>
9+
<width>800</width>
1010
<height>525</height>
1111
</rect>
1212
</property>
@@ -181,7 +181,7 @@
181181
<item row="0" column="0">
182182
<layout class="QHBoxLayout" name="horizontalLayout_6">
183183
<item>
184-
<widget class="QLabel" name="label_4">
184+
<widget class="QCheckBox" name="uniqueColumnCheck">
185185
<property name="text">
186186
<string>Column with unique
187187
integer values</string>
@@ -190,6 +190,9 @@ integer values</string>
190190
</item>
191191
<item>
192192
<widget class="QComboBox" name="uniqueCombo">
193+
<property name="enabled">
194+
<bool>false</bool>
195+
</property>
193196
<property name="sizePolicy">
194197
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
195198
<horstretch>0</horstretch>
@@ -403,5 +406,21 @@ columns</string>
403406
</hint>
404407
</hints>
405408
</connection>
409+
<connection>
410+
<sender>uniqueColumnCheck</sender>
411+
<signal>toggled(bool)</signal>
412+
<receiver>uniqueCombo</receiver>
413+
<slot>setEnabled(bool)</slot>
414+
<hints>
415+
<hint type="sourcelabel">
416+
<x>109</x>
417+
<y>385</y>
418+
</hint>
419+
<hint type="destinationlabel">
420+
<x>274</x>
421+
<y>385</y>
422+
</hint>
423+
</hints>
424+
</connection>
406425
</connections>
407426
</ui>

0 commit comments

Comments
 (0)