Skip to content

Commit 5c15a65

Browse files
authored
Merge pull request #7926 from elpaso/bugfix-19680-spatialite-load-default-style
[bugfix] spatialite load default style
2 parents e005d6e + 4d057e9 commit 5c15a65

File tree

4 files changed

+68
-21
lines changed

4 files changed

+68
-21
lines changed

src/app/qgisapp.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -10620,6 +10620,7 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
1062010620

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

src/providers/spatialite/qgsspatialiteprovider.cpp

+28-21
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ void QgsSpatiaLiteProvider::loadFieldsAbstractInterface( gaiaVectorLayerPtr lyr
673673
return;
674674

675675
mAttributeFields.clear();
676-
mPrimaryKey.clear(); // cazzo cazzo cazzo
676+
mPrimaryKey.clear();
677677
mPrimaryKeyAttrs.clear();
678678
mDefaultValues.clear();
679679

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

819+
QString QgsSpatiaLiteProvider::tableSchemaCondition( const QgsDataSourceUri &dsUri )
820+
{
821+
return dsUri.schema().isEmpty() ?
822+
QStringLiteral( "IS NULL" ) :
823+
QStringLiteral( "= %1" ).arg( quotedValue( dsUri.schema( ) ) );
824+
}
825+
819826
void QgsSpatiaLiteProvider::fetchConstraints()
820827
{
821828
char **results = nullptr;
@@ -5662,11 +5669,11 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
56625669

56635670
QString checkQuery = QString( "SELECT styleName"
56645671
" FROM layer_styles"
5665-
" WHERE f_table_schema=%1"
5672+
" WHERE f_table_schema %1"
56665673
" AND f_table_name=%2"
56675674
" AND f_geometry_column=%3"
56685675
" AND styleName=%4" )
5669-
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
5676+
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
56705677
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
56715678
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) )
56725679
.arg( QgsSpatiaLiteProvider::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) );
@@ -5699,7 +5706,7 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
56995706
",styleSLD=%3"
57005707
",description=%4"
57015708
",owner=%5"
5702-
" WHERE f_table_schema=%6"
5709+
" WHERE f_table_schema %6"
57035710
" AND f_table_name=%7"
57045711
" AND f_geometry_column=%8"
57055712
" AND styleName=%9" )
@@ -5708,7 +5715,7 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
57085715
.arg( QgsSpatiaLiteProvider::quotedValue( sldStyle ) )
57095716
.arg( QgsSpatiaLiteProvider::quotedValue( styleDescription.isEmpty() ? QDateTime::currentDateTime().toString() : styleDescription ) )
57105717
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.username() ) )
5711-
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
5718+
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
57125719
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
57135720
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) )
57145721
.arg( QgsSpatiaLiteProvider::quotedValue( styleName.isEmpty() ? dsUri.table() : styleName ) );
@@ -5718,10 +5725,10 @@ QGISEXTERN bool saveStyle( const QString &uri, const QString &qmlStyle, const QS
57185725
{
57195726
QString removeDefaultSql = QString( "UPDATE layer_styles"
57205727
" SET useAsDefault=0"
5721-
" WHERE f_table_schema=%1"
5728+
" WHERE f_table_schema %1"
57225729
" AND f_table_name=%2"
57235730
" AND f_geometry_column=%3" )
5724-
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
5731+
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
57255732
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
57265733
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );
57275734
sql = QStringLiteral( "BEGIN; %1; %2; COMMIT;" ).arg( removeDefaultSql, sql );
@@ -5763,12 +5770,12 @@ QGISEXTERN QString loadStyle( const QString &uri, QString &errCause )
57635770

57645771
QString selectQmlQuery = QString( "SELECT styleQML"
57655772
" FROM layer_styles"
5766-
" WHERE f_table_schema=%1"
5773+
" WHERE f_table_schema %1"
57675774
" AND f_table_name=%2"
57685775
" AND f_geometry_column=%3"
57695776
" ORDER BY CASE WHEN useAsDefault THEN 1 ELSE 2 END"
57705777
",update_time DESC LIMIT 1" )
5771-
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
5778+
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
57725779
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
57735780
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );
57745781

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

58445851
// get them
5845-
QString selectRelatedQuery = QString( "SELECT id,styleName,description"
5846-
" FROM layer_styles"
5847-
" WHERE f_table_schema=%1"
5848-
" AND f_table_name=%2"
5849-
" AND f_geometry_column=%3"
5850-
" ORDER BY useasdefault DESC, update_time DESC" )
5851-
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
5852+
QString selectRelatedQuery = QStringLiteral( "SELECT id,styleName,description"
5853+
" FROM layer_styles"
5854+
" WHERE f_table_schema %1"
5855+
" AND f_table_name=%2"
5856+
" AND f_geometry_column=%3"
5857+
" ORDER BY useasdefault DESC, update_time DESC" )
5858+
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
58525859
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
58535860
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );
58545861

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

5873-
QString selectOthersQuery = QString( "SELECT id,styleName,description"
5874-
" FROM layer_styles"
5875-
" WHERE NOT (f_table_schema=%1 AND f_table_name=%2 AND f_geometry_column=%3)"
5876-
" ORDER BY update_time DESC" )
5877-
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
5880+
QString selectOthersQuery = QStringLiteral( "SELECT id,styleName,description"
5881+
" FROM layer_styles"
5882+
" WHERE NOT (f_table_schema %1 AND f_table_name=%2 AND f_geometry_column=%3)"
5883+
" ORDER BY update_time DESC" )
5884+
.arg( QgsSpatiaLiteProvider::tableSchemaCondition( dsUri ) )
58785885
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
58795886
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );
58805887

src/providers/spatialite/qgsspatialiteprovider.h

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
8686
bool setSubsetString( const QString &theSQL, bool updateFeatureCount = true ) override;
8787
bool supportsSubsetString() const override { return true; }
8888
QgsWkbTypes::Type wkbType() const override;
89+
//! Return the table schema condition
90+
static QString tableSchemaCondition( const QgsDataSourceUri &dsUri );
8991

9092
/**
9193
* Returns the number of layers for the current data source

tests/src/python/test_provider_spatialite.py

+37
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,43 @@ def testDecodeUri(self):
752752
components = registry.decodeUri('spatialite', uri)
753753
self.assertEqual(components['path'], filename)
754754

755+
def testLoadStyle(self):
756+
"""Check that we can store and load a style"""
757+
758+
# create test db
759+
dbname = os.path.join(tempfile.gettempdir(), "test_loadstyle.sqlite")
760+
if os.path.exists(dbname):
761+
os.remove(dbname)
762+
con = spatialite_connect(dbname, isolation_level=None)
763+
cur = con.cursor()
764+
cur.execute("BEGIN")
765+
sql = "SELECT InitSpatialMetadata()"
766+
cur.execute(sql)
767+
768+
# simple table with primary key
769+
sql = "CREATE TABLE test_pg (id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL)"
770+
cur.execute(sql)
771+
772+
sql = "SELECT AddGeometryColumn('test_pg', 'geometry', 4326, 'POLYGON', 'XY')"
773+
cur.execute(sql)
774+
775+
sql = "INSERT INTO test_pg (id, name, geometry) "
776+
sql += "VALUES (1, 'toto', GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))', 4326))"
777+
cur.execute(sql)
778+
779+
cur.execute("COMMIT")
780+
con.close()
781+
782+
testPath = "dbname=%s table='test_pg' (geometry) key='id'" % dbname
783+
vl = QgsVectorLayer(testPath, 'test', 'spatialite')
784+
self.assertTrue(vl.isValid())
785+
self.assertEqual(vl.featureCount(), 1)
786+
err, ok = vl.loadDefaultStyle()
787+
self.assertFalse(ok)
788+
vl.saveStyleToDatabase('my_style', 'My description', True, '')
789+
err, ok = vl.loadDefaultStyle()
790+
self.assertTrue(ok)
791+
755792

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

0 commit comments

Comments
 (0)