Skip to content
Permalink
Browse files

Improved fix for spatialite views (fixes #14232)

(cherry picked from commit 42d8884)
  • Loading branch information
wonder-sk committed Mar 19, 2016
1 parent 8ee2e2c commit 780c5873f62cf60d040e776f622bc1ad4edb7e79
@@ -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();

0 comments on commit 780c587

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