Skip to content
Permalink
Browse files
Determine whether there's an integer column with unique values that c…
…an be used as a pkey
  • Loading branch information
szekerest authored and NathanW2 committed Mar 28, 2012
1 parent 1425449 commit 45def2169a98da5fc5bba9f4fb5380e28f4dcf33
Showing with 60 additions and 13 deletions.
  1. +60 −13 src/providers/mssql/qgsmssqlprovider.cpp
@@ -292,7 +292,11 @@ void QgsMssqlProvider::loadMetadata()

mQuery = QSqlQuery( mDatabase );
mQuery.setForwardOnly( true );
mQuery.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 ).arg( mTableName ) );
if (!mQuery.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 ).arg( mTableName ) ))
{
QString msg = mQuery.lastError().text();
QgsDebugMsg( msg );
}
if ( mQuery.isActive() )
{
if ( mQuery.next() )
@@ -310,10 +314,16 @@ void QgsMssqlProvider::loadFields()
// get field spec
mQuery = QSqlQuery( mDatabase );
mQuery.setForwardOnly( true );
mQuery.exec( QString( "exec sp_columns N'%1', NULL, NULL, NULL, NULL" ).arg( mTableName ) );
if (!mQuery.exec( QString( "exec sp_columns N'%1', NULL, NULL, NULL, NULL" ).arg( mTableName ) ))
{
QString msg = mQuery.lastError().text();
QgsDebugMsg( msg );
return;
}
if ( mQuery.isActive() )
{
int i = 0;
QStringList pkCandidates;
while ( mQuery.next() )
{
QString sqlTypeName = mQuery.value( 5 ).toString();
@@ -327,6 +337,10 @@ void QgsMssqlProvider::loadFields()
QVariant::Type sqlType = DecodeSqlType( sqlTypeName );
if ( sqlTypeName == "int identity" || sqlTypeName == "bigint identity" )
mFidColName = mQuery.value( 3 ).toString();
else if (sqlTypeName == "int" || sqlTypeName == "bigint")
{
pkCandidates << mQuery.value( 3 ).toString();
}
mAttributeFields.insert(
i, QgsField(
mQuery.value( 3 ).toString(), sqlType,
@@ -336,17 +350,47 @@ void QgsMssqlProvider::loadFields()
++i;
}
}
}
// 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 ) ))
{
mFidColName = mQuery.value( 3 ).toString();
QString msg = mQuery.lastError().text();
QgsDebugMsg( msg );
}
if ( mQuery.isActive() )
{
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;
}
}
}
}
}
}
}
}
@@ -407,7 +451,11 @@ bool QgsMssqlProvider::featureAtId( QgsFeatureId featureId,
// issue the sql query
mQuery = QSqlQuery( mDatabase );
mQuery.setForwardOnly( true );
mQuery.exec( query );
if (!mQuery.exec( query ))
{
QString msg = mQuery.lastError().text();
QgsDebugMsg( msg );
}

return nextFeature( feature );
}
@@ -517,7 +565,6 @@ void QgsMssqlProvider::select( QgsAttributeList fetchAttributes,
mQuery.setForwardOnly( true );
if ( mFieldCount > 0 )
{
mQuery.exec( mStatement );
if ( !mQuery.exec( mStatement ) )
{
QString msg = mQuery.lastError().text();

0 comments on commit 45def21

Please sign in to comment.