Navigation Menu

Skip to content

Commit

Permalink
[spatialite] use view's pkey if defined in meta table (fixes #14232)
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Feb 24, 2016
1 parent 05471f5 commit f028c0b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/providers/spatialite/qgsspatialitefeatureiterator.cpp
Expand Up @@ -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()
Expand Down Expand Up @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/providers/spatialite/qgsspatialitefeatureiterator.h
Expand Up @@ -40,6 +40,7 @@ class QgsSpatiaLiteFeatureSource : public QgsAbstractFeatureSource
QgsFields mFields;
QString mQuery;
bool isQuery;
bool mViewBased;
bool mVShapeBased;
QString mIndexTable;
QString mIndexGeometry;
Expand Down
25 changes: 25 additions & 0 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Expand Up @@ -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
{
Expand Down

0 comments on commit f028c0b

Please sign in to comment.