Skip to content
Permalink
Browse files

Improved fix for spatialite views (fixes #14232)

  • Loading branch information
wonder-sk committed Mar 6, 2016
1 parent 5bb25d8 commit 42d88848f3010365d2c9fcf84b3a8ed5d52749a1
@@ -319,7 +319,7 @@ bool QgsSpatiaLiteFeatureIterator::prepareStatement( const QString& whereClause,

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

QString QgsSpatiaLiteFeatureIterator::whereClauseFid()
@@ -674,6 +674,12 @@ void QgsSpatiaLiteProvider::loadFieldsAbstractInterface( gaiaVectorLayerPtr lyr
}
}

// for views try to get the primary key from the meta table
if ( mViewBased && mPrimaryKey.isEmpty() )
{
determineViewPrimaryKey();
}

updatePrimaryKeyCapabilities();

sqlite3_free_table( results );
@@ -798,26 +804,9 @@ void QgsSpatiaLiteProvider::loadFields()
// 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 );
}
determineViewPrimaryKey();
}


}
else
{
@@ -906,6 +895,33 @@ void QgsSpatiaLiteProvider::loadFields()
}
}


void QgsSpatiaLiteProvider::determineViewPrimaryKey()
{
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 ) );

char **results;
int rows;
int columns;
char *errMsg = nullptr;
int 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 );
}
}


bool QgsSpatiaLiteProvider::hasTriggers()
{
int ret;
@@ -258,6 +258,9 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
/** Loads fields from input file to member attributeFields */
void loadFields();

/** For views, try to get primary key from a dedicated meta table */
void determineViewPrimaryKey();

/** Check if a table/view has any triggers. Triggers can be used on views to make them editable.*/
bool hasTriggers();

5 comments on commit 42d8884

@nyalldawson

This comment has been minimized.

Copy link
Contributor

@nyalldawson nyalldawson replied Mar 6, 2016

@wonder-sk is it safe for me to backport this to 2.14?

@wonder-sk

This comment has been minimized.

Copy link
Member Author

@wonder-sk wonder-sk replied Mar 7, 2016

Yeah I'm just waiting for the original reporter to confirm that the problems are gone before backporting it to 2.14

@Roel

This comment has been minimized.

Copy link
Contributor

@Roel Roel replied Mar 9, 2016

I'd like to add support for backporting. I have Spatialite views that show correctly on the map in QGis 2.12 but fail to be rendered in 2.14 (the attribute table loads correctly but the layer is not drawn on the map). I traced it back to commit f028c0b where the problem first occurred. It is fixed in master since this commit, so I'm in favor of backporting to 2.14 to fix rendering of Spatialite views. Thanks!

@wonder-sk

This comment has been minimized.

Copy link
Member Author

@wonder-sk wonder-sk replied Mar 19, 2016

Backported to 2.14 in 780c587

@brusspi

This comment has been minimized.

Copy link

@brusspi brusspi replied Oct 2, 2016

It seems that the problem still exists in version 2.16.2. I updated to 2.16.3 and even there it occured.

When selecting a feature it shows the correct values. But when you open the feature form, it always gives the values of the feature with the first ID.
spatialite_view

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