|
25 | 25 | from functools import cmp_to_key
|
26 | 26 |
|
27 | 27 | from qgis.PyQt.QtWidgets import QApplication
|
| 28 | +from qgis.PyQt.QtCore import QThread |
28 | 29 |
|
29 | 30 | from ..connector import DBConnector
|
30 | 31 | from ..plugin import ConnectionError, DbError, Table
|
@@ -65,6 +66,26 @@ def _opendb(self):
|
65 | 66 | raise ConnectionError(QApplication.translate("DBManagerPlugin", '"{dbname}" not recognized as GPKG ({shortname} reported instead.)').format(dbname=self.dbname, shortname=self.gdal_ds.GetDriver().ShortName))
|
66 | 67 | self.has_raster = self.gdal_ds.RasterCount != 0 or self.gdal_ds.GetMetadata('SUBDATASETS') is not None
|
67 | 68 | self.connection = None
|
| 69 | + self._current_thread = None |
| 70 | + |
| 71 | + @property |
| 72 | + def connection(self): |
| 73 | + """Creates and returns a spatialite connection, if |
| 74 | + the existing connection was created in another thread |
| 75 | + invalidates it and create a new one. |
| 76 | + """ |
| 77 | + |
| 78 | + if self._connection is None or self._current_thread != int(QThread.currentThreadId()): |
| 79 | + self._current_thread = int(QThread.currentThreadId()) |
| 80 | + try: |
| 81 | + self._connection = spatialite_connect(str(self.dbname)) |
| 82 | + except self.connection_error_types() as e: |
| 83 | + raise ConnectionError(e) |
| 84 | + return self._connection |
| 85 | + |
| 86 | + @connection.setter |
| 87 | + def connection(self, conn): |
| 88 | + self._connection = conn |
68 | 89 |
|
69 | 90 | def unquoteId(self, quotedId):
|
70 | 91 | if len(quotedId) <= 2 or quotedId[0] != '"' or quotedId[len(quotedId) - 1] != '"':
|
|
0 commit comments