Skip to content
Permalink
Browse files

mssql provider: cleanup driver and service usage and copy password in…

… feature iterator (fixes #10428)
  • Loading branch information
jef-n committed Jun 12, 2014
1 parent 85ce676 commit 6392fa9234cd022d494c0c25da5c3ba74e994918
@@ -100,8 +100,8 @@ QVector<QgsDataItem*> QgsMssqlConnectionItem::createChildren()
QVector<QgsDataItem*> children;

readConnectionSettings();
QSqlDatabase db = QgsMssqlProvider::GetDatabase( mService,
mHost, mDatabase, mUsername, mPassword );

QSqlDatabase db = QgsMssqlProvider::GetDatabase( mService, mHost, mDatabase, mUsername, mPassword );

if ( !QgsMssqlProvider::OpenDatabase( db ) )
{
@@ -35,7 +35,7 @@ QgsMssqlFeatureIterator::QgsMssqlFeatureIterator( QgsMssqlFeatureSource* source,
BuildStatement( request );

// connect to the database
mDatabase = GetDatabase( mSource->mDriver, mSource->mHost, mSource->mDatabaseName, mSource->mUserName, mSource->mPassword );
mDatabase = QgsMssqlProvider::GetDatabase( mSource->mService, mSource->mHost, mSource->mDatabaseName, mSource->mUserName, mSource->mPassword );

if ( !mDatabase.open() )
{
@@ -63,7 +63,7 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest& request )
// build sql statement
mStatement = QString( "SELECT " );

mStatement += QString("[%1]").arg( mSource->mFidColName );
mStatement += QString( "[%1]" ).arg( mSource->mFidColName );
mFidCol = mSource->mFields.indexFromName( mSource->mFidColName );
mAttributesToFetch.append( mFidCol );

@@ -74,15 +74,15 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest& request )
if ( mSource->mFidColName == fieldname )
continue;

mStatement += QString(",[%1]").arg( fieldname );
mStatement += QString( ",[%1]" ).arg( fieldname );

mAttributesToFetch.append( i );
}

// get geometry col
if ( !( request.flags() & QgsFeatureRequest::NoGeometry ) && mSource->isSpatial() )
{
mStatement += QString(",[%1]").arg( mSource->mGeometryColName );
mStatement += QString( ",[%1]" ).arg( mSource->mGeometryColName );
}

mStatement += QString( "FROM [%1].[%2]" ).arg( mSource->mSchemaName, mSource->mTableName );
@@ -235,78 +235,6 @@ bool QgsMssqlFeatureIterator::close()
return true;
}

QSqlDatabase QgsMssqlFeatureIterator::GetDatabase( QString driver, QString host, QString database, QString username, QString password )
{
QSqlDatabase db;
QString connectionName;

// create a separate database connection for each feature source
QgsDebugMsg( "Creating a separate database connection" );
QString id;

// QString::sprintf adds 0x prefix
id.sprintf( "%p", this );

if ( driver.isEmpty() )
{
if ( host.isEmpty() )
{
QgsDebugMsg( "QgsMssqlProvider host name not specified" );
return db;
}

if ( database.isEmpty() )
{
QgsDebugMsg( "QgsMssqlProvider database name not specified" );
return db;
}
connectionName = host + "." + database + "." + id;
}
else
connectionName = driver;

if ( !QSqlDatabase::contains( connectionName ) )
db = QSqlDatabase::addDatabase( "QODBC", connectionName );
else
db = QSqlDatabase::database( connectionName );

db.setHostName( host );
QString connectionString = "";
if ( !driver.isEmpty() )
{
// driver was specified explicitly
connectionString = driver;
}
else
{
#ifdef WIN32
connectionString = "driver={SQL Server}";
#else
connectionString = "driver={FreeTDS};port=1433";
#endif
}

if ( !host.isEmpty() )
connectionString += ";server=" + host;

if ( !database.isEmpty() )
connectionString += ";database=" + database;

if ( password.isEmpty() )
connectionString += ";trusted_connection=yes";
else
connectionString += ";uid=" + username + ";pwd=" + password;

if ( !username.isEmpty() )
db.setUserName( username );

if ( !password.isEmpty() )
db.setPassword( password );

db.setDatabaseName( connectionString );
return db;
}

///////////////

QgsMssqlFeatureSource::QgsMssqlFeatureSource( const QgsMssqlProvider* p )
@@ -317,6 +245,7 @@ QgsMssqlFeatureSource::QgsMssqlFeatureSource( const QgsMssqlProvider* p )
, mSchemaName( p->mSchemaName )
, mTableName( p->mTableName )
, mUserName( p->mUserName )
, mPassword( p->mPassword )
, mService( p->mService )
, mDatabaseName( p->mDatabaseName )
, mHost( p->mHost )
@@ -55,7 +55,6 @@ class QgsMssqlFeatureSource : public QgsAbstractFeatureSource

// server access
QString mService;
QString mDriver;
QString mDatabaseName;
QString mHost;

@@ -82,11 +81,8 @@ class QgsMssqlFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsM
virtual bool close();

protected:

void BuildStatement( const QgsFeatureRequest& request );

QSqlDatabase GetDatabase( QString driver, QString host, QString database, QString username, QString password );

private:
//! fetch next feature, return true on success
virtual bool fetchFeature( QgsFeature& feature );
@@ -173,8 +173,10 @@ void QgsMssqlNewConnection::testConnection()
}

QSqlDatabase db = QgsMssqlProvider::GetDatabase( txtService->text().trimmed(),
txtHost->text().trimmed(), txtDatabase->text().trimmed(),
txtUsername->text().trimmed(), txtPassword->text().trimmed() );
txtHost->text().trimmed(),
txtDatabase->text().trimmed(),
txtUsername->text().trimmed(),
txtPassword->text().trimmed() );

if ( db.isOpen() )
db.close();
@@ -51,6 +51,7 @@

static const QString TEXT_PROVIDER_KEY = "mssql";
static const QString TEXT_PROVIDER_DESCRIPTION = "MSSQL spatial data provider";
int QgsMssqlProvider::sConnectionId = 0;

QgsMssqlProvider::QgsMssqlProvider( QString uri )
: QgsVectorDataProvider( uri )
@@ -81,7 +82,7 @@ QgsMssqlProvider::QgsMssqlProvider( QString uri )

mSqlWhereClause = anUri.sql();

mDatabase = QgsMssqlProvider::GetDatabase( mDriver, mHost, mDatabaseName, mUserName, mPassword );
mDatabase = GetDatabase( mService, mHost, mDatabaseName, mUserName, mPassword );

if ( !OpenDatabase( mDatabase ) )
{
@@ -199,31 +200,29 @@ bool QgsMssqlProvider::OpenDatabase( QSqlDatabase db )
return true;
}

QSqlDatabase QgsMssqlProvider::GetDatabase( QString driver, QString host, QString database, QString username, QString password )
QSqlDatabase QgsMssqlProvider::GetDatabase( QString service, QString host, QString database, QString username, QString password )
{
QSqlDatabase db;
QString connectionName;

// create a separate database connection for each feature source
QgsDebugMsg( "Creating a separate database connection" );

if ( driver.isEmpty() )
if ( service.isEmpty() )
{
if ( host.isEmpty() )
{
QgsDebugMsg( "QgsMssqlProvider host name not specified" );
return db;
}
if ( !host.isEmpty() )
connectionName = host + ".";

if ( database.isEmpty() )
{
QgsDebugMsg( "QgsMssqlProvider database name not specified" );
return db;
}
connectionName = host + "." + database;

connectionName += QString( "%1.%2" ).arg( database ).arg( sConnectionId++ );
}
else
connectionName = driver;
connectionName = service;

if ( !QSqlDatabase::contains( connectionName ) )
db = QSqlDatabase::addDatabase( "QODBC", connectionName );
@@ -232,10 +231,10 @@ QSqlDatabase QgsMssqlProvider::GetDatabase( QString driver, QString host, QStrin

db.setHostName( host );
QString connectionString = "";
if ( !driver.isEmpty() )
if ( !service.isEmpty() )
{
// driver was specified explicitly
connectionString = driver;
connectionString = service;
}
else
{
@@ -1505,8 +1504,7 @@ QgsVectorLayerImport::ImportError QgsMssqlProvider::createEmptyLayer(
QgsDataSourceURI dsUri( uri );

// connect to database
QSqlDatabase db = QgsMssqlProvider::GetDatabase( dsUri.service(),
dsUri.host(), dsUri.database(), dsUri.username(), dsUri.password() );
QSqlDatabase db = QgsMssqlProvider::GetDatabase( dsUri.service(), dsUri.host(), dsUri.database(), dsUri.username(), dsUri.password() );

if ( !QgsMssqlProvider::OpenDatabase( db ) )
{
@@ -54,7 +54,7 @@ class QgsMssqlProvider : public QgsVectorDataProvider

virtual ~QgsMssqlProvider();

static QSqlDatabase GetDatabase( QString driver, QString host, QString database, QString username, QString password );
static QSqlDatabase GetDatabase( QString service, QString host, QString database, QString username, QString password );

virtual QgsAbstractFeatureSource* featureSource() const;

@@ -300,7 +300,6 @@ class QgsMssqlProvider : public QgsVectorDataProvider

// server access
QString mService;
QString mDriver;
QString mDatabaseName;
QString mHost;

@@ -320,6 +319,8 @@ class QgsMssqlProvider : public QgsVectorDataProvider
static QGis::WkbType getWkbType( QString geometryType, int dim );

friend class QgsMssqlFeatureSource;

static int sConnectionId;
};

#endif // QGSMSSQLPROVIDER_H
@@ -429,6 +429,7 @@ void QgsMssqlSourceSelect::populateConnectionList()
// Slot for performing action when the Add button is clicked
void QgsMssqlSourceSelect::addTables()
{
QgsDebugMsg( QString( "mConnInfo:%1" ).arg( mConnInfo ) );
mSelectedTables.clear();

foreach ( QModelIndex idx, mTablesTreeView->selectionModel()->selection().indexes() )
@@ -494,12 +495,18 @@ void QgsMssqlSourceSelect::on_btnConnect_clicked()

bool estimateMetadata = settings.value( key + "/estimatedMetadata", true ).toBool();

mConnInfo = "dbname='" + database + "' host=" + host + " user='" + username + "' password='" + password + "'";
mConnInfo = "dbname='" + database + "'";
if ( !host.isEmpty() )
mConnInfo += " host=" + host + "'";
if ( !username.isEmpty() )
mConnInfo += " user='" + username + "'";
if ( !password.isEmpty() )
mConnInfo += " password='" + password + "'";
if ( !service.isEmpty() )
mConnInfo += " service='" + service + "'";

QSqlDatabase db = QgsMssqlProvider::GetDatabase( service,
host, database, username, password );
QgsDebugMsg( "GetDatabase" );
QSqlDatabase db = QgsMssqlProvider::GetDatabase( service, host, database, username, password );

if ( !QgsMssqlProvider::OpenDatabase( db ) )
{

0 comments on commit 6392fa9

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