Skip to content
Permalink
Browse files

Some clean

  • Loading branch information
pblottiere committed Mar 2, 2018
1 parent 957e92c commit a948f1b84d0453688fe53728a72d25b614c5412b
@@ -33,6 +33,8 @@
QStandardItem)
from qgis.PyQt.QtWidgets import QApplication

from qgis.core import QgsTask

from .plugin import DbError, BaseError


@@ -173,6 +175,17 @@ def modelDone(self):
self.done.emit()


class SqlResultModelTask(QgsTask):

def __init__(self, db, sql, parent):
QgsTask.__init__(self)
self.db = db
self.sql = sql
self.parent = parent
self.error = BaseError('')
self.model = None


class SqlResultModel(BaseTableModel):

def __init__(self, db, sql, parent=None):
@@ -20,9 +20,12 @@
***************************************************************************/
"""

from qgis.core import QgsTask
from qgis.core import QgsMessageLog
from ..plugin import BaseError
from ..data_model import TableDataModel, SqlResultModel, SqlResultModelAsync
from ..data_model import (TableDataModel,
SqlResultModel,
SqlResultModelAsync,
SqlResultModelTask)


class PGTableDataModel(TableDataModel):
@@ -80,25 +83,18 @@ def fetchMoreData(self, row_start):
self.fetchedFrom = row_start


class PGSqlResultModelTask(QgsTask):
class PGSqlResultModelTask(SqlResultModelTask):

def __init__(self, db, sql, parent):
QgsTask.__init__(self)
self.db = db
self.sql = sql
self.parent = parent
self.error = BaseError('')
self.model = None
SqlResultModelTask.__init__(self, db, sql, parent)

def run(self):

try:
self.model = PGSqlResultModel(self.db, self.sql, self.parent)
self.model = PGSqlResultModel(self.db, self.sql, None)
except BaseError as e:
self.error = e
QgsMessageLog.logMessage(e.msg)
return False

return True

def cancelQuery(self):
@@ -20,9 +20,12 @@
***************************************************************************/
"""

from qgis.core import QgsTask
from qgis.core import QgsMessageLog
from ..plugin import BaseError
from ..data_model import TableDataModel, SqlResultModel, SqlResultModelAsync
from ..data_model import (TableDataModel,
SqlResultModel,
SqlResultModelAsync,
SqlResultModelTask)
from .plugin import SLDatabase


@@ -63,23 +66,18 @@ def rowCount(self, index=None):
return self.fetchedCount


class SLSqlResultModelTask(QgsTask):
class SLSqlResultModelTask(SqlResultModelTask):

def __init__(self, db, sql, parent):
QgsTask.__init__(self)
self.db = db
self.sql = sql
self.parent = parent
self.error = BaseError('')
self.model = None
SqlResultModelTask.__init__(self, db, sql, parent)
self.clone = None

def run(self):
try:
self.clone = SLDatabase(None, self.db.connector.uri())

# import time
# self.clone.connector.connection.create_function("sleep", 1, time.sleep)
#import time
#self.clone.connector.connection.create_function("sleep", 1, time.sleep)

self.model = SLSqlResultModel(self.clone, self.sql, None)
except BaseError as e:
@@ -19,14 +19,21 @@
***************************************************************************/
"""

from ..data_model import TableDataModel, BaseTableModel, SqlResultModelAsync
from ..data_model import (TableDataModel,
BaseTableModel,
SqlResultModelAsync,
SqlResultModelTask)

from .connector import VLayerRegistry, getQueryGeometryName
from .plugin import LVectorTable
from ..plugin import DbError, BaseError

from qgis.PyQt.QtCore import QTime, QTemporaryFile
from qgis.core import QgsVectorLayer, QgsWkbTypes, QgsVirtualLayerDefinition, QgsVirtualLayerTask, QgsTask
from qgis.core import (QgsVectorLayer,
QgsWkbTypes,
QgsVirtualLayerDefinition,
QgsVirtualLayerTask,
QgsTask)


class LTableDataModel(TableDataModel):
@@ -63,13 +70,22 @@ def rowCount(self, index=None):
return 0


class LSqlResultModelTask(QgsTask):
class LSqlResultModelTask(SqlResultModelTask):

def __init__(self, subtask, db):
QgsTask.__init__(self)
self.subtask = subtask
self.db = db
self.model = None
def __init__(self, db, sql, parent):
SqlResultModelTask.__init__(self, db, sql, parent)

tf = QTemporaryFile()
tf.open()
path = tf.fileName()
tf.close()

df = QgsVirtualLayerDefinition()
df.setFilePath(path)
df.setQuery(sql)

self.subtask = QgsVirtualLayerTask(df)
self.addSubTask(self.subtask, [], QgsTask.ParentDependsOnSubTask)

def run(self):
try:
@@ -91,18 +107,7 @@ class LSqlResultModelAsync(SqlResultModelAsync):
def __init__(self, db, sql, parent=None):
SqlResultModelAsync.__init__(self, db, sql, parent)

tf = QTemporaryFile()
tf.open()
path = tf.fileName()
tf.close()

df = QgsVirtualLayerDefinition()
df.setFilePath(path)
df.setQuery(self.sql)

self.subtask = QgsVirtualLayerTask(df)
self.task = LSqlResultModelTask(self.subtask, db)
self.task.addSubTask(self.subtask, [], QgsTask.ParentDependsOnSubTask)
self.task = LSqlResultModelTask(db, sql, parent)
self.task.taskCompleted.connect(self.modelDone)
self.task.taskTerminated.connect(self.modelDone)

@@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsvirtuallayertask.h"
#include "qgslogger.h"

QgsVirtualLayerTask::QgsVirtualLayerTask( const QgsVirtualLayerDefinition &definition )
: QgsTask()
@@ -27,8 +28,18 @@ QgsVirtualLayerTask::QgsVirtualLayerTask( const QgsVirtualLayerDefinition &defin

bool QgsVirtualLayerTask::run()
{
mLayer->reload(); // blocking call because the loading is postponed
return mLayer->isValid();
bool rc = false;
try
{
mLayer->reload(); // blocking call because the loading is postponed
rc = mLayer->isValid();
}
catch ( std::exception &e )
{
QgsDebugMsg( tr( "Reload error: %1" ).arg( e.what() ) );
rc = false;
}
return rc;
}

QgsVirtualLayerDefinition QgsVirtualLayerTask::definition() const

0 comments on commit a948f1b

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