Skip to content

Commit 6eeaca5

Browse files
Gustrynyalldawson
authored andcommitted
return the default style first in the list
1 parent 96923c7 commit 6eeaca5

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

python/core/auto_generated/qgsvectorlayer.sip.in

+4
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,10 @@ Lists all the style in db split into related to the layer and not related to
797797
:param msgError:
798798

799799
:return: the number of styles related to current layer
800+
801+
.. note::
802+
803+
Since QGIS 3.2 Styles related to the layer are ordered with the default style first then by update time for Postgres, MySQL and Spatialite.
800804
%End
801805

802806
virtual QString getStyleFromDatabase( const QString &styleId, QString &msgError /Out/ );

src/core/qgsvectorlayer.h

+1
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
800800
* \param descriptions the list in which will be stored the style descriptions
801801
* \param msgError
802802
* \returns the number of styles related to current layer
803+
* \note Since QGIS 3.2 Styles related to the layer are ordered with the default style first then by update time for Postgres, MySQL and Spatialite.
803804
*/
804805
virtual int listStylesInDatabase( QStringList &ids SIP_OUT, QStringList &names SIP_OUT,
805806
QStringList &descriptions SIP_OUT, QString &msgError SIP_OUT );

src/providers/mssql/qgsmssqlprovider.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2244,7 +2244,8 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
22442244
" WHERE f_table_catalog=%1"
22452245
" AND f_table_schema=%2"
22462246
" AND f_table_name=%3"
2247-
" AND f_geometry_column=%4" )
2247+
" AND f_geometry_column=%4"
2248+
" ORDER BY useasdefault DESC, update_time DESC" )
22482249
.arg( QgsMssqlProvider::quotedValue( dsUri.database() ) )
22492250
.arg( QgsMssqlProvider::quotedValue( dsUri.schema() ) )
22502251
.arg( QgsMssqlProvider::quotedValue( dsUri.table() ) )

src/providers/postgres/qgspostgresprovider.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -4822,7 +4822,8 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
48224822
" WHERE f_table_catalog=%1"
48234823
" AND f_table_schema=%2"
48244824
" AND f_table_name=%3"
4825-
" AND f_geometry_column=%4" )
4825+
" AND f_geometry_column=%4"
4826+
" ORDER BY useasdefault DESC, update_time DESC" )
48264827
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
48274828
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
48284829
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )

src/providers/spatialite/qgsspatialiteprovider.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -5836,7 +5836,8 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
58365836
" FROM layer_styles"
58375837
" WHERE f_table_schema=%1"
58385838
" AND f_table_name=%2"
5839-
" AND f_geometry_column=%3" )
5839+
" AND f_geometry_column=%3"
5840+
" ORDER BY useasdefault DESC, update_time DESC" )
58405841
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
58415842
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
58425843
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );

tests/src/python/test_provider_postgres.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
Note: to prepare the DB, you need to run the sql files specified in
55
tests/testdata/provider/testdata_pg.sh
66
7+
Read tests/README.md about writing/launching tests with PostgreSQL.
8+
9+
Run with ctest -V -R PyQgsPostgresProvider
10+
711
.. note:: This program is free software; you can redistribute it and/or modify
812
it under the terms of the GNU General Public License as published by
913
the Free Software Foundation; either version 2 of the License, or
@@ -902,6 +906,7 @@ def testStyle(self):
902906
status = vl.loadNamedStyle(mFilePath)
903907
self.assertTrue(status)
904908

909+
# The style is saved as non-default
905910
errorMsg = vl.saveStyleToDatabase("by day", "faded greens and elegant patterns", False, "")
906911
self.assertEqual(errorMsg, "")
907912

@@ -933,6 +938,27 @@ def testStyle(self):
933938
self.assertTrue(res)
934939
self.assertEqual(errmsg, "")
935940

941+
# We save now the style again twice but with one as default
942+
errorMsg = vl.saveStyleToDatabase("related style", "faded greens and elegant patterns", False, "")
943+
self.assertEqual(errorMsg, "")
944+
errorMsg = vl.saveStyleToDatabase("default style", "faded greens and elegant patterns", True, "")
945+
self.assertEqual(errorMsg, "")
946+
947+
related_count, idlist, namelist, desclist, errmsg = vl.listStylesInDatabase()
948+
self.assertEqual(related_count, 2)
949+
self.assertEqual(errmsg, "")
950+
self.assertEqual(idlist, ["3", "2"]) # Ids must be reversed.
951+
self.assertEqual(namelist, ["default style", "related style"])
952+
self.assertEqual(desclist, ["faded greens and elegant patterns"] * 2)
953+
954+
# We remove these 2 styles
955+
res, errmsg = vl.deleteStyleFromDatabase("2")
956+
self.assertTrue(res)
957+
self.assertEqual(errmsg, "")
958+
res, errmsg = vl.deleteStyleFromDatabase("3")
959+
self.assertTrue(res)
960+
self.assertEqual(errmsg, "")
961+
936962
# table layer_styles does exit, but is now empty
937963
related_count, idlist, namelist, desclist, errmsg = vl.listStylesInDatabase()
938964
self.assertEqual(related_count, 0)
@@ -1066,7 +1092,16 @@ def receive(self, msg):
10661092
self.assertTrue(ok)
10671093

10681094
def testStyleDatabaseWithService(self):
1069-
1095+
"""Test saving style in DB using a service file.
1096+
1097+
To run this test, you first need to create a service with:
1098+
[qgis_test]
1099+
host=localhost
1100+
port=5432
1101+
dbname=qgis_test
1102+
user=USERNAME
1103+
password=PASSWORD
1104+
"""
10701105
myconn = 'service=\'qgis_test\''
10711106
if 'QGIS_PGTEST_DB' in os.environ:
10721107
myconn = os.environ['QGIS_PGTEST_DB']

0 commit comments

Comments
 (0)