return the default style first in the list

This commit is contained in:
Etienne Trimaille 2018-06-04 16:38:55 -04:00 committed by Nyall Dawson
parent 96923c75b1
commit 6eeaca5570
6 changed files with 46 additions and 3 deletions

View File

@ -797,6 +797,10 @@ Lists all the style in db split into related to the layer and not related to
:param msgError:
:return: the number of styles related to current layer
.. 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.
%End
virtual QString getStyleFromDatabase( const QString &styleId, QString &msgError /Out/ );

View File

@ -800,6 +800,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* \param descriptions the list in which will be stored the style descriptions
* \param msgError
* \returns the number of styles related to current layer
* \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.
*/
virtual int listStylesInDatabase( QStringList &ids SIP_OUT, QStringList &names SIP_OUT,
QStringList &descriptions SIP_OUT, QString &msgError SIP_OUT );

View File

@ -2244,7 +2244,8 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
" WHERE f_table_catalog=%1"
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column=%4" )
" AND f_geometry_column=%4"
" ORDER BY useasdefault DESC, update_time DESC" )
.arg( QgsMssqlProvider::quotedValue( dsUri.database() ) )
.arg( QgsMssqlProvider::quotedValue( dsUri.schema() ) )
.arg( QgsMssqlProvider::quotedValue( dsUri.table() ) )

View File

@ -4822,7 +4822,8 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
" WHERE f_table_catalog=%1"
" AND f_table_schema=%2"
" AND f_table_name=%3"
" AND f_geometry_column=%4" )
" AND f_geometry_column=%4"
" ORDER BY useasdefault DESC, update_time DESC" )
.arg( QgsPostgresConn::quotedValue( dsUri.database() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.schema() ) )
.arg( QgsPostgresConn::quotedValue( dsUri.table() ) )

View File

@ -5836,7 +5836,8 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
" FROM layer_styles"
" WHERE f_table_schema=%1"
" AND f_table_name=%2"
" AND f_geometry_column=%3" )
" AND f_geometry_column=%3"
" ORDER BY useasdefault DESC, update_time DESC" )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.schema() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.table() ) )
.arg( QgsSpatiaLiteProvider::quotedValue( dsUri.geometryColumn() ) );

View File

@ -4,6 +4,10 @@
Note: to prepare the DB, you need to run the sql files specified in
tests/testdata/provider/testdata_pg.sh
Read tests/README.md about writing/launching tests with PostgreSQL.
Run with ctest -V -R PyQgsPostgresProvider
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
@ -902,6 +906,7 @@ class TestPyQgsPostgresProvider(unittest.TestCase, ProviderTestCase):
status = vl.loadNamedStyle(mFilePath)
self.assertTrue(status)
# The style is saved as non-default
errorMsg = vl.saveStyleToDatabase("by day", "faded greens and elegant patterns", False, "")
self.assertEqual(errorMsg, "")
@ -933,6 +938,27 @@ class TestPyQgsPostgresProvider(unittest.TestCase, ProviderTestCase):
self.assertTrue(res)
self.assertEqual(errmsg, "")
# We save now the style again twice but with one as default
errorMsg = vl.saveStyleToDatabase("related style", "faded greens and elegant patterns", False, "")
self.assertEqual(errorMsg, "")
errorMsg = vl.saveStyleToDatabase("default style", "faded greens and elegant patterns", True, "")
self.assertEqual(errorMsg, "")
related_count, idlist, namelist, desclist, errmsg = vl.listStylesInDatabase()
self.assertEqual(related_count, 2)
self.assertEqual(errmsg, "")
self.assertEqual(idlist, ["3", "2"]) # Ids must be reversed.
self.assertEqual(namelist, ["default style", "related style"])
self.assertEqual(desclist, ["faded greens and elegant patterns"] * 2)
# We remove these 2 styles
res, errmsg = vl.deleteStyleFromDatabase("2")
self.assertTrue(res)
self.assertEqual(errmsg, "")
res, errmsg = vl.deleteStyleFromDatabase("3")
self.assertTrue(res)
self.assertEqual(errmsg, "")
# table layer_styles does exit, but is now empty
related_count, idlist, namelist, desclist, errmsg = vl.listStylesInDatabase()
self.assertEqual(related_count, 0)
@ -1066,7 +1092,16 @@ class TestPyQgsPostgresProvider(unittest.TestCase, ProviderTestCase):
self.assertTrue(ok)
def testStyleDatabaseWithService(self):
"""Test saving style in DB using a service file.
To run this test, you first need to create a service with:
[qgis_test]
host=localhost
port=5432
dbname=qgis_test
user=USERNAME
password=PASSWORD
"""
myconn = 'service=\'qgis_test\''
if 'QGIS_PGTEST_DB' in os.environ:
myconn = os.environ['QGIS_PGTEST_DB']