Skip to content

Commit

Permalink
[mssql] Followup a76359, fix provider methods like featureCount,
Browse files Browse the repository at this point in the history
uniqueValues, etc not working when using DSN for connection
  • Loading branch information
nyalldawson committed May 29, 2017
1 parent 67d778f commit fcafc82
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/providers/mssql/qgsmssqlprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ void QgsMssqlProvider::loadMetadata()
mSRId = 0;
mWkbType = QGis::WKBUnknown;

if ( !mDatabase.isOpen() )
{
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );
}

QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );
if ( !query.exec( QString( "select f_geometry_column, coord_dimension, srid, geometry_type from geometry_columns where f_table_schema = '%1' and f_table_name = '%2'" ).arg( mSchemaName, mTableName ) ) )
Expand All @@ -368,7 +373,12 @@ void QgsMssqlProvider::loadFields()
{
mAttributeFields.clear();
mDefaultValues.clear();

// get field spec
if ( !mDatabase.isOpen() )
{
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );
}
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );
if ( !query.exec( QString( "exec sp_columns @table_name = N'%1', @table_owner = '%2'" ).arg( mTableName, mSchemaName ) ) )
Expand Down Expand Up @@ -532,6 +542,10 @@ QVariant QgsMssqlProvider::minimumValue( int index )
sql += QString( " where (%1)" ).arg( mSqlWhereClause );
}

if ( !mDatabase.isOpen() )
{
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );
}
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );

Expand Down Expand Up @@ -563,6 +577,10 @@ QVariant QgsMssqlProvider::maximumValue( int index )
sql += QString( " where (%1)" ).arg( mSqlWhereClause );
}

if ( !mDatabase.isOpen() )
{
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );
}
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );

Expand Down Expand Up @@ -603,6 +621,10 @@ void QgsMssqlProvider::uniqueValues( int index, QList<QVariant> &uniqueValues, i
sql += QString( " where (%1)" ).arg( mSqlWhereClause );
}

if ( !mDatabase.isOpen() )
{
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );
}
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );

Expand Down Expand Up @@ -631,6 +653,10 @@ void QgsMssqlProvider::UpdateStatistics( bool estimate )
// get features to calculate the statistics
QString statement;

if ( !mDatabase.isOpen() )
{
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );
}
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );

Expand Down Expand Up @@ -758,6 +784,10 @@ long QgsMssqlProvider::featureCount() const

// If there is no subset set we can get the count from the system tables.
// Which is faster then doing select count(*)
if ( !mDatabase.isOpen() )
{
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );
}
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );

Expand Down Expand Up @@ -1419,6 +1449,10 @@ QgsCoordinateReferenceSystem QgsMssqlProvider::crs()
return mCrs;

// try to load crs from the database tables as a fallback
if ( !mDatabase.isOpen() )
{
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );
}
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );
bool execOk = query.exec( QString( "select srtext from spatial_ref_sys where srid = %1" ).arg( QString::number( mSRId ) ) );
Expand Down Expand Up @@ -1472,6 +1506,10 @@ bool QgsMssqlProvider::setSubsetString( const QString& theSQL, bool )
sql += QString( " where %1" ).arg( mSqlWhereClause );
}

if ( !mDatabase.isOpen() )
{
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );
}
QSqlQuery query = QSqlQuery( mDatabase );
query.setForwardOnly( true );
if ( !query.exec( sql ) )
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mssql/qgsmssqlprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class QgsMssqlProvider : public QgsVectorDataProvider
QGis::WkbType mWkbType;

// The database object
QSqlDatabase mDatabase;
mutable QSqlDatabase mDatabase;

// The current sql query
QSqlQuery mQuery;
Expand Down

0 comments on commit fcafc82

Please sign in to comment.