Skip to content
Permalink
Browse files

Avoid race condition when populating ms sql items in browser

  • Loading branch information
nyalldawson committed Feb 3, 2016
1 parent ac328a6 commit 316a941f439617ef971d89469db8c687d3117b60
Showing with 30 additions and 12 deletions.
  1. +25 −9 src/providers/mssql/qgsmssqldataitems.cpp
  2. +5 −3 src/providers/mssql/qgsmssqldataitems.h
@@ -39,13 +39,15 @@ QgsMssqlConnectionItem::QgsMssqlConnectionItem( QgsDataItem* parent, QString nam
, mUseGeometryColumns( false )
, mUseEstimatedMetadata( false )
, mAllowGeometrylessTables( true )
, mColumnTypeThread( nullptr )
{
mCapabilities |= Fast;
mIconName = "mIconConnect.png";
}

QgsMssqlConnectionItem::~QgsMssqlConnectionItem()
{
stop();
}

void QgsMssqlConnectionItem::readConnectionSettings()
@@ -76,9 +78,21 @@ void QgsMssqlConnectionItem::readConnectionSettings()
mConnInfo += " estimatedmetadata=true";
}

void QgsMssqlConnectionItem::stop()
{
if ( mColumnTypeThread )
{
mColumnTypeThread->stop();
mColumnTypeThread->wait();
delete mColumnTypeThread;
mColumnTypeThread = nullptr;
}
}

void QgsMssqlConnectionItem::refresh()
{
QgsDebugMsg( "mPath = " + mPath );
stop();

// read up the schemas and layers from database
QVector<QgsDataItem*> items = createChildren();
@@ -102,7 +116,11 @@ QVector<QgsDataItem*> QgsMssqlConnectionItem::createChildren()
{
QgsDebugMsg( "Entered" );

stop();

QVector<QgsDataItem*> children;
if ( deferredDelete() )
return children;

readConnectionSettings();

@@ -116,8 +134,6 @@ QVector<QgsDataItem*> QgsMssqlConnectionItem::createChildren()

QString connectionName = db.connectionName();

QgsMssqlGeomColumnTypeThread *columnTypeThread = nullptr;

// build sql statement
QString query( "select " );
if ( mUseGeometryColumns )
@@ -197,15 +213,15 @@ QVector<QgsDataItem*> QgsMssqlConnectionItem::createChildren()
{
if ( type == "GEOMETRY" || type.isNull() || srid.isEmpty() )
{
if ( !columnTypeThread )
if ( !mColumnTypeThread )
{
columnTypeThread = new QgsMssqlGeomColumnTypeThread(
mColumnTypeThread = new QgsMssqlGeomColumnTypeThread(
connectionName, true /* use estimated metadata */ );

connect( columnTypeThread, SIGNAL( setLayerType( QgsMssqlLayerProperty ) ),
connect( mColumnTypeThread, SIGNAL( setLayerType( QgsMssqlLayerProperty ) ),
this, SLOT( setLayerType( QgsMssqlLayerProperty ) ) );
connect( this, SIGNAL( addGeometryColumn( QgsMssqlLayerProperty ) ),
columnTypeThread, SLOT( addGeometryColumn( QgsMssqlLayerProperty ) ) );
mColumnTypeThread, SLOT( addGeometryColumn( QgsMssqlLayerProperty ) ) );
}

emit addGeometryColumn( layer );
@@ -229,8 +245,8 @@ QVector<QgsDataItem*> QgsMssqlConnectionItem::createChildren()
}

// spawn threads (new layers will be added later on)
if ( columnTypeThread )
columnTypeThread->start();
if ( mColumnTypeThread )
mColumnTypeThread->start();
}

return children;
@@ -517,7 +533,7 @@ bool QgsMssqlSchemaItem::handleDrop( const QMimeData* data, Qt::DropAction )
return conn->handleDrop( data, mName );
}

QgsMssqlLayerItem* QgsMssqlSchemaItem::addLayer( QgsMssqlLayerProperty layerProperty, bool refresh )
QgsMssqlLayerItem* QgsMssqlSchemaItem::addLayer( const QgsMssqlLayerProperty& layerProperty, bool refresh )
{
QGis::WkbType wkbType = QgsMssqlTableModel::wkbTypeFromMssql( layerProperty.type );
QString tip = tr( "%1 as %2 in %3" ).arg( layerProperty.geometryColName, QgsMssqlTableModel::displayStringForWkbType( wkbType ), layerProperty.srid );
@@ -63,8 +63,6 @@ class QgsMssqlConnectionItem : public QgsDataCollectionItem

bool handleDrop( const QMimeData * data, const QString& toSchema );

void refresh() override;

QString connInfo() const { return mConnInfo; }

signals:
@@ -77,6 +75,8 @@ class QgsMssqlConnectionItem : public QgsDataCollectionItem

void setLayerType( QgsMssqlLayerProperty layerProperty );

void refresh() override;

private:
QString mConnInfo;
QString mService;
@@ -87,8 +87,10 @@ class QgsMssqlConnectionItem : public QgsDataCollectionItem
bool mUseGeometryColumns;
bool mUseEstimatedMetadata;
bool mAllowGeometrylessTables;
QgsMssqlGeomColumnTypeThread* mColumnTypeThread;

void readConnectionSettings();
void stop();
};

class QgsMssqlSchemaItem : public QgsDataCollectionItem
@@ -100,7 +102,7 @@ class QgsMssqlSchemaItem : public QgsDataCollectionItem

QVector<QgsDataItem*> createChildren() override;

QgsMssqlLayerItem* addLayer( QgsMssqlLayerProperty layerProperty, bool refresh );
QgsMssqlLayerItem* addLayer( const QgsMssqlLayerProperty& layerProperty, bool refresh );
void refresh() override {} // do not refresh directly
void addLayers( QgsDataItem* newLayers );
virtual bool acceptDrop() override { return true; }

0 comments on commit 316a941

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