Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[DBManager] Allow to load a layer without primary key
  • Loading branch information
Hugo Mercier committed May 4, 2015
1 parent 00618fc commit d684c8c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
15 changes: 15 additions & 0 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/db_manager/db_plugins/spatialite/plugin.py
Expand Up @@ -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):
Expand Down
11 changes: 5 additions & 6 deletions python/plugins/db_manager/dlg_sql_window.py
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions python/plugins/db_manager/ui/DlgSqlWindow.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>747</width>
<width>800</width>
<height>525</height>
</rect>
</property>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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.