Skip to content

Commit

Permalink
Determine whether there's an integer column with unique values that c…
Browse files Browse the repository at this point in the history
…an be used as a pkey
  • Loading branch information
szekerest authored and alexbruy committed Apr 7, 2012
1 parent 0b1cfe2 commit 700f709
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions src/providers/mssql/qgsmssqlprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,47 @@ void QgsMssqlProvider::loadFields()
}
}
}
}
// get primary key
if ( mFidColName.isEmpty() )
{
mQuery.clear();
mQuery.exec( QString( "exec sp_pkeys N'%1', NULL, NULL" ).arg( mTableName ) );
if ( mQuery.isActive() )
// get primary key
if ( mFidColName.isEmpty() )
{
if ( mQuery.next() )
mQuery.clear();
if (!mQuery.exec( QString( "exec sp_pkeys N'%1', NULL, NULL" ).arg( mTableName ) ))
{
QString msg = mQuery.lastError().text();
QgsDebugMsg( msg );
}
if ( mQuery.isActive() )
{
mFidColName = mQuery.value( 3 ).toString();
if ( mQuery.next() )
{
mFidColName = mQuery.value( 3 ).toString();
}
else
{
foreach( QString pk, pkCandidates )
{
mQuery.clear();
if (!mQuery.exec( QString( "select count(distinct [%1]), count([%1]) from [%2].[%3]" )
.arg( pk )
.arg( mSchemaName )
.arg( mTableName ) ))
{
QString msg = mQuery.lastError().text();
QgsDebugMsg( msg );
}
if ( mQuery.isActive() )
{
if ( mQuery.next() )
{
if (mQuery.value( 0 ).toInt() == mQuery.value( 1 ).toInt())
{
mFidColName = pk;
break;
}
}
}
}
}
}
}
}
Expand Down

0 comments on commit 700f709

Please sign in to comment.