Skip to content
Permalink
Browse files

Merge pull request #7926 from elpaso/bugfix-19680-spatialite-load-def…

…ault-style

[bugfix] spatialite load default style
  • Loading branch information
elpaso committed Sep 18, 2018
2 parents e005d6e + 4d057e9 commit 5c15a6589dd089a3a2926a8f24b7e099824124c5
@@ -10620,6 +10620,7 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q

// create the layer
QgsVectorLayer::LayerOptions options;
// Default style is loaded later in this method
options.loadDefaultStyle = false;
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, options );

@@ -673,7 +673,7 @@ void QgsSpatiaLiteProvider::loadFieldsAbstractInterface( gaiaVectorLayerPtr lyr
return;

mAttributeFields.clear();
mPrimaryKey.clear(); // cazzo cazzo cazzo
mPrimaryKey.clear();
mPrimaryKeyAttrs.clear();
mDefaultValues.clear();

@@ -816,6 +816,13 @@ QString QgsSpatiaLiteProvider::spatialiteVersion()
return mSpatialiteVersionInfo;
}

QString QgsSpatiaLiteProvider::tableSchemaCondition( const QgsDataSourceUri &dsUri )
{
return dsUri.schema().isEmpty() ?
QStringLiteral( "IS NULL" ) :
QStringLiteral( "= %1" ).arg( quotedValue( dsUri.schema( ) ) );
}

void QgsSpatiaLiteProvider::fetchConstraints()
{
char **results = nullptr;
@@ -5662,11 +5669,11 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS

QString checkQuery = QString( "SELECT styleName"
" FROM layer_styles"
" WHERE f_table_schema=%1"
" WHERE f_table_schema %1"
" AND f_table_name=%2"
" AND f_geometry_column=%3"
" AND styleName=%4" )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) );
@@ -5699,7 +5706,7 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
",styleSLD=%3"
",description=%4"
",owner=%5"
" WHERE f_table_schema=%6"
" WHERE f_table_schema %6"
" AND f_table_name=%7"
" AND f_geometry_column=%8"
" AND styleName=%9" )
@@ -5708,7 +5715,7 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
.arg( QgsSpatiaLiteProvider::quotedValue( sldStyle ) )
.arg( QgsSpatiaLiteProvider::quotedValue( styleDescription.isEmpty() ? QDateTime::currentDateTime().toString() : styleDescription ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.username() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) );
@@ -5718,10 +5725,10 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
{
QString removeDefaultSql = QString( "UPDATE layer_styles"
" SET useAsDefault=0"
" WHERE f_table_schema=%1"
" WHERE f_table_schema %1"
" AND f_table_name=%2"
" AND f_geometry_column=%3" )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );
sql = QStringLiteral( "BEGIN; %1; %2; COMMIT;" ).arg( removeDefaultSql, sql );
@@ -5763,12 +5770,12 @@ QGISEXTERN QString loadStyle( const QString &uri, QString &errCause )

QString selectQmlQuery = QString( "SELECT styleQML"
" FROM layer_styles"
" WHERE f_table_schema=%1"
" WHERE f_table_schema %1"
" AND f_table_name=%2"
" AND f_geometry_column=%3"
" ORDER BY CASE WHEN useAsDefault THEN 1 ELSE 2 END"
",update_time DESC LIMIT 1" )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );

@@ -5842,13 +5849,13 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
}

// get them
QString selectRelatedQuery = QString( "SELECT id,styleName,description"
" FROM layer_styles"
" WHERE f_table_schema=%1"
" AND f_table_name=%2"
" AND f_geometry_column=%3"
" ORDER BY useasdefault DESC, update_time DESC" )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
QString selectRelatedQuery = QStringLiteral( "SELECT id,styleName,description"
" FROM layer_styles"
" WHERE f_table_schema %1"
" AND f_table_name=%2"
" AND f_geometry_column=%3"
" ORDER BY useasdefault DESC, update_time DESC" )
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );

@@ -5870,11 +5877,11 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
}
sqlite3_free_table( results );

QString selectOthersQuery = QString( "SELECT id,styleName,description"
" FROM layer_styles"
" WHERE NOT (f_table_schema=%1 AND f_table_name=%2 AND f_geometry_column=%3)"
" ORDER BY update_time DESC" )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
QString selectOthersQuery = QStringLiteral( "SELECT id,styleName,description"
" FROM layer_styles"
" WHERE NOT (f_table_schema %1 AND f_table_name=%2 AND f_geometry_column=%3)"
" ORDER BY update_time DESC" )
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );

@@ -86,6 +86,8 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
bool setSubsetString( const QString &theSQL, bool updateFeatureCount = true ) override;
bool supportsSubsetString() const override { return true; }
QgsWkbTypes::Type wkbType() const override;
//! Return the table schema condition
static QString tableSchemaCondition( const QgsDataSourceUri &dsUri );

/**
* Returns the number of layers for the current data source
@@ -752,6 +752,43 @@ def testDecodeUri(self):
components = registry.decodeUri('spatialite', uri)
self.assertEqual(components['path'], filename)

def testLoadStyle(self):
"""Check that we can store and load a style"""

# create test db
dbname = os.path.join(tempfile.gettempdir(), "test_loadstyle.sqlite")
if os.path.exists(dbname):
os.remove(dbname)
con = spatialite_connect(dbname, isolation_level=None)
cur = con.cursor()
cur.execute("BEGIN")
sql = "SELECT InitSpatialMetadata()"
cur.execute(sql)

# simple table with primary key
sql = "CREATE TABLE test_pg (id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL)"
cur.execute(sql)

sql = "SELECT AddGeometryColumn('test_pg', 'geometry', 4326, 'POLYGON', 'XY')"
cur.execute(sql)

sql = "INSERT INTO test_pg (id, name, geometry) "
sql += "VALUES (1, 'toto', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
cur.execute(sql)

cur.execute("COMMIT")
con.close()

testPath = "dbname=%s table='test_pg' (geometry) key='id'" % dbname
vl = QgsVectorLayer(testPath, 'test', 'spatialite')
self.assertTrue(vl.isValid())
self.assertEqual(vl.featureCount(), 1)
err, ok = vl.loadDefaultStyle()
self.assertFalse(ok)
vl.saveStyleToDatabase('my_style', 'My description', True, '')
err, ok = vl.loadDefaultStyle()
self.assertTrue(ok)


if __name__ == '__main__':
unittest.main()

0 comments on commit 5c15a65

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