Skip to content
Permalink
Browse files

[spatialite] use view's pkey if defined in meta table (fixes #14232)

  • Loading branch information
wonder-sk committed Feb 24, 2016
1 parent 05471f5 commit f028c0bb5898af12b81953210dabf2a26713095d
@@ -319,7 +319,7 @@ bool QgsSpatiaLiteFeatureIterator::prepareStatement( const QString& whereClause,

QString QgsSpatiaLiteFeatureIterator::quotedPrimaryKey()
{
return !mSource->isQuery ? "ROWID" : QgsSpatiaLiteProvider::quotedIdentifier( mSource->mPrimaryKey );
return !( mSource->isQuery || mSource->mViewBased ) ? "ROWID" : QgsSpatiaLiteProvider::quotedIdentifier( mSource->mPrimaryKey );
}

QString QgsSpatiaLiteFeatureIterator::whereClauseFid()
@@ -562,6 +562,7 @@ QgsSpatiaLiteFeatureSource::QgsSpatiaLiteFeatureSource( const QgsSpatiaLiteProvi
, mFields( p->attributeFields )
, mQuery( p->mQuery )
, isQuery( p->isQuery )
, mViewBased( p->mViewBased )
, mVShapeBased( p->mVShapeBased )
, mIndexTable( p->mIndexTable )
, mIndexGeometry( p->mIndexGeometry )
@@ -40,6 +40,7 @@ class QgsSpatiaLiteFeatureSource : public QgsAbstractFeatureSource
QgsFields mFields;
QString mQuery;
bool isQuery;
bool mViewBased;
bool mVShapeBased;
QString mIndexTable;
QString mIndexGeometry;
@@ -793,6 +793,31 @@ void QgsSpatiaLiteProvider::loadFields()
}
}
sqlite3_free_table( results );


// for views try to get the primary key from the meta table
if ( mViewBased && mPrimaryKey.isEmpty() )
{
QString sql = QString( "SELECT view_rowid"
" FROM views_geometry_columns"
" WHERE upper(view_name) = upper(%1) and upper(view_geometry) = upper(%2)" ).arg( quotedValue( mTableName ),
quotedValue( mGeometryColumn ) );

ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
if ( ret == SQLITE_OK )
{
if ( rows > 0 )
{
mPrimaryKey = results[1 * columns];
int idx = attributeFields.fieldNameIndex( mPrimaryKey );
if ( idx != -1 )
mPrimaryKeyAttrs << idx;
}
sqlite3_free_table( results );
}
}


}
else
{

0 comments on commit f028c0b

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