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

This commit is contained in:
Martin Dobias 2016-02-24 18:30:37 +08:00
parent 05471f55d3
commit f028c0bb58
3 changed files with 28 additions and 1 deletions

View File

@ -319,7 +319,7 @@ bool QgsSpatiaLiteFeatureIterator::prepareStatement( const QString& whereClause,
QString QgsSpatiaLiteFeatureIterator::quotedPrimaryKey() QString QgsSpatiaLiteFeatureIterator::quotedPrimaryKey()
{ {
return !mSource->isQuery ? "ROWID" : QgsSpatiaLiteProvider::quotedIdentifier( mSource->mPrimaryKey ); return !( mSource->isQuery || mSource->mViewBased ) ? "ROWID" : QgsSpatiaLiteProvider::quotedIdentifier( mSource->mPrimaryKey );
} }
QString QgsSpatiaLiteFeatureIterator::whereClauseFid() QString QgsSpatiaLiteFeatureIterator::whereClauseFid()
@ -562,6 +562,7 @@ QgsSpatiaLiteFeatureSource::QgsSpatiaLiteFeatureSource( const QgsSpatiaLiteProvi
, mFields( p->attributeFields ) , mFields( p->attributeFields )
, mQuery( p->mQuery ) , mQuery( p->mQuery )
, isQuery( p->isQuery ) , isQuery( p->isQuery )
, mViewBased( p->mViewBased )
, mVShapeBased( p->mVShapeBased ) , mVShapeBased( p->mVShapeBased )
, mIndexTable( p->mIndexTable ) , mIndexTable( p->mIndexTable )
, mIndexGeometry( p->mIndexGeometry ) , mIndexGeometry( p->mIndexGeometry )

View File

@ -40,6 +40,7 @@ class QgsSpatiaLiteFeatureSource : public QgsAbstractFeatureSource
QgsFields mFields; QgsFields mFields;
QString mQuery; QString mQuery;
bool isQuery; bool isQuery;
bool mViewBased;
bool mVShapeBased; bool mVShapeBased;
QString mIndexTable; QString mIndexTable;
QString mIndexGeometry; QString mIndexGeometry;

View File

@ -793,6 +793,31 @@ void QgsSpatiaLiteProvider::loadFields()
} }
} }
sqlite3_free_table( results ); 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 else
{ {