made QgsPostgresStringUtils namespace to class because of private functions we use there and possibly in future there are more coming and renamed the methods

fixed indents and comments
This commit is contained in:
David Signer 2019-07-18 09:25:34 +02:00
parent 890e699c10
commit b4dab2df38
7 changed files with 108 additions and 107 deletions

View File

@ -12,10 +12,20 @@
#include "qgspostgresstringutils.h"
%End
namespace QgsPostgresStringUtils
class QgsPostgresStringUtils
{
%Docstring
The QgsPostgresStringUtils provides functions to handle postgres array like formatted lists in strings.
QVariantList parse( const QString &string );
.. versionadded:: 3.8
%End
%TypeHeaderCode
#include "qgspostgresstringutils.h"
%End
public:
static QVariantList parseArray( const QString &string );
%Docstring
Returns a QVariantList created out of a string containing an array in postgres array format {1,2,3} or {"a","b","c"}
@ -24,23 +34,12 @@ Returns a QVariantList created out of a string containing an array in postgres a
.. versionadded:: 3.8
%End
QString build( const QVariantList &list );
static QString buildArray( const QVariantList &list );
%Docstring
Build a postgres array like formatted list in a string from a QVariantList
:param list: The list that needs to be stored to the string
.. versionadded:: 3.8
%End
QString getNextString( const QString &txt, int &i, const QString &sep );
%Docstring
get the string until the separator
:param txt: the input text
:param i: the current position
:param sep: the separator
.. versionadded:: 3.8
%End

View File

@ -180,7 +180,7 @@ QStringList QgsValueRelationFieldFormatter::valueToStringList( const QVariant &v
if ( value.type() == QVariant::String )
{
// This must be an array representation
valuesList = QgsPostgresStringUtils::parse( value.toString() );
valuesList = QgsPostgresStringUtils::parseArray( value.toString() );
}
else if ( value.type() == QVariant::List )
{

View File

@ -60,7 +60,7 @@ QString QgsPostgresStringUtils::getNextString( const QString &txt, int &i, const
}
}
QVariantList QgsPostgresStringUtils::parse( const QString &string )
QVariantList QgsPostgresStringUtils::parseArray( const QString &string )
{
QVariantList variantList;
@ -145,7 +145,7 @@ QVariantList QgsPostgresStringUtils::parse( const QString &string )
}
QString QgsPostgresStringUtils::build( const QVariantList &list )
QString QgsPostgresStringUtils::buildArray( const QVariantList &list )
{
QStringList sl;
for ( const QVariant &v : qgis::as_const( list ) )

View File

@ -28,35 +28,38 @@
/**
* \ingroup core
* The QgsPostgresStringUtils namespace provides functions to handle postgres array like formatted lists in strings.
* \since QGIS 3.4
* The QgsPostgresStringUtils provides functions to handle postgres array like formatted lists in strings.
* \since QGIS 3.8
*/
namespace QgsPostgresStringUtils
class CORE_EXPORT QgsPostgresStringUtils
{
/**
* Returns a QVariantList created out of a string containing an array in postgres array format {1,2,3} or {"a","b","c"}
* \param string The formatted list in a string
* \since QGIS 3.8
*/
CORE_EXPORT QVariantList parse( const QString &string );
public:
/**
* Build a postgres array like formatted list in a string from a QVariantList
* \param list The list that needs to be stored to the string
* \since QGIS 3.8
*/
CORE_EXPORT QString build( const QVariantList &list );
/**
* Returns a QVariantList created out of a string containing an array in postgres array format {1,2,3} or {"a","b","c"}
* \param string The formatted list in a string
* \since QGIS 3.8
*/
static QVariantList parseArray( const QString &string );
/**
* get the string until the separator
* \param txt the input text
* \param i the current position
* \param sep the separator
* \since QGIS 3.8
*/
CORE_EXPORT QString getNextString( const QString &txt, int &i, const QString &sep );
/**
* Build a postgres array like formatted list in a string from a QVariantList
* \param list The list that needs to be stored to the string
* \since QGIS 3.8
*/
static QString buildArray( const QVariantList &list );
private:
/**
* get the string until the separator
* \param txt the input text
* \param i the current position
* \param sep the separator
* \since QGIS 3.8
*/
static QString getNextString( const QString &txt, int &i, const QString &sep );
};
#endif //QGSPOSTGRESSTRINGUTILS_H

View File

@ -103,7 +103,7 @@ QVariant QgsValueRelationWidgetWrapper::value() const
else
{
//make string
v = QgsPostgresStringUtils::build( vl );
v = QgsPostgresStringUtils::buildArray( vl );
}
}

View File

@ -1,9 +1,9 @@
/***************************************************************************
testqgshstoreutils.cpp
testqgspostgresstringutils.cpp
--------------------------------------
Date : October 2018
Copyright : (C) 2018 Etienne Trimaille
Email : etienne dot trimaille at gmail dot com
Date : July 2019
Copyright : (C) 2019 David Signer
email : david at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
@ -43,10 +43,10 @@ void TestQgsPostgresStringUtils::testPgArrayStringToListAndBack()
vl.push_back( QStringLiteral( "...all the etceteras" ) );
QString string = QStringLiteral( "{\"one\",\"}two{\",\"thr\\\"ee\",\"fo,ur\",\"fiv'e\",6,\"and 7garcìa][\",\"...all the etceteras\"}" );
QCOMPARE( QgsPostgresStringUtils::parse( string ), vl );
QCOMPARE( QgsPostgresStringUtils::parseArray( string ), vl );
// and back
QCOMPARE( QgsPostgresStringUtils::build( vl ), string );
QCOMPARE( QgsPostgresStringUtils::buildArray( vl ), string );
}
void TestQgsPostgresStringUtils::testUnquotedPgArrayStringToListAndBack()
@ -61,11 +61,11 @@ void TestQgsPostgresStringUtils::testUnquotedPgArrayStringToListAndBack()
vl.push_back( QStringLiteral( "that genius" ) );
QString fallback_string = QStringLiteral( "{one,two,three,four,five,that genius}" );
QCOMPARE( QgsPostgresStringUtils::parse( fallback_string ), vl );
QCOMPARE( QgsPostgresStringUtils::parseArray( fallback_string ), vl );
// and back including quotes
QString new_string = QStringLiteral( "{\"one\",\"two\",\"three\",\"four\",\"five\",\"that genius\"}" );
QCOMPARE( QgsPostgresStringUtils::build( vl ), new_string );
QCOMPARE( QgsPostgresStringUtils::buildArray( vl ), new_string );
}
void TestQgsPostgresStringUtils::testNumberArrayStringToListAndBack()
@ -78,10 +78,10 @@ void TestQgsPostgresStringUtils::testNumberArrayStringToListAndBack()
vl.push_back( 4 );
QString number_string = QStringLiteral( "{1,2,3,4}" );
QCOMPARE( QgsPostgresStringUtils::parse( number_string ), vl );
QCOMPARE( QgsPostgresStringUtils::parseArray( number_string ), vl );
// and back without quotes
QCOMPARE( QgsPostgresStringUtils::build( vl ), number_string );
QCOMPARE( QgsPostgresStringUtils::buildArray( vl ), number_string );
}
void TestQgsPostgresStringUtils::testMultidimensionalPgArrayStringToListAndBack()
@ -93,10 +93,10 @@ void TestQgsPostgresStringUtils::testMultidimensionalPgArrayStringToListAndBack(
vl.push_back( QStringLiteral( "{three one third,three two third,three three third}" ) );
QString string = QStringLiteral( "{{one one third,one two third,one three third},{\"two one third\",\"two two third\",\"two three third\"},{three one third,three two third,three three third}}" );
QCOMPARE( QgsPostgresStringUtils::parse( string ), vl );
QCOMPARE( QgsPostgresStringUtils::parseArray( string ), vl );
// and back without quotes
QCOMPARE( QgsPostgresStringUtils::build( vl ), string );
QCOMPARE( QgsPostgresStringUtils::buildArray( vl ), string );
}
QGSTEST_MAIN( TestQgsPostgresStringUtils )

View File

@ -1476,87 +1476,87 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
//check if first feature checked correctly (1,2,3,5) ) );
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( { "1gamma", "2helm,comma", "3johnson\"quote", "5adams'singlequote" } )
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( { "1gamma", "2helm,comma", "3johnson\"quote", "5adams'singlequote" } ) ) );
#else
QCOMPARE( w_favoriteauthors.value(), QVariant( QStringLiteral( "{\"1gamma\",\"2helm,comma\",\"3johnson\\\"quote\",\"5adams'singlequote\"}" ) ) );
#endif
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
vl_json->changeAttributeValue( 1, fk_field_idx, w_favoriteauthors.value() );
// check if stored correctly
vl_json->commitChanges();
QgsFeature f = vl_json->getFeature( 1 );
QVariant attribute = f.attribute( fk_field );
QVariantList value = attribute.toList();
vl_json->changeAttributeValue( 1, fk_field_idx, w_favoriteauthors.value() );
// check if stored correctly
vl_json->commitChanges();
QgsFeature f = vl_json->getFeature( 1 );
QVariant attribute = f.attribute( fk_field );
QVariantList value = attribute.toList();
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
QCOMPARE( value, QVariantList( { "1gamma", "2helm,comma", "3johnson\"quote", "5adams'singlequote" } ) );
QCOMPARE( value, QVariantList( { "1gamma", "2helm,comma", "3johnson\"quote", "5adams'singlequote" } ) );
#else
QCOMPARE( w_favoriteauthors.value(), QVariant( QStringLiteral( "{\"1gamma\",\"2helm,comma\",\"3johnson\\\"quote\",\"5adams'singlequote\"}" ) ) );
QCOMPARE( w_favoriteauthors.value(), QVariant( QStringLiteral( "{\"1gamma\",\"2helm,comma\",\"3johnson\\\"quote\",\"5adams'singlequote\"}" ) ) );
#endif
// FEATURE 2
w_favoriteauthors.setFeature( vl_json->getFeature( 2 ) );
// FEATURE 2
w_favoriteauthors.setFeature( vl_json->getFeature( 2 ) );
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( { "2helm,comma", "5adams'singlequote" } ) ) );
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( { "2helm,comma", "5adams'singlequote" } ) ) );
#else
QCOMPARE( w_favoriteauthors.value(), QVariant( QStringLiteral( "{\"2helm,comma\",\"5adams'singlequote\"}" ) ) );
QCOMPARE( w_favoriteauthors.value(), QVariant( QStringLiteral( "{\"2helm,comma\",\"5adams'singlequote\"}" ) ) );
#endif
//check if second feature checked correctly
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
//check if second feature checked correctly
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Checked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
// FEATURE 4
w_favoriteauthors.setFeature( vl_json->getFeature( 4 ) );
// FEATURE 4
w_favoriteauthors.setFeature( vl_json->getFeature( 4 ) );
//check if first feature checked correctly (NULL)
//check if first feature checked correctly (NULL)
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( ) ) );
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( ) ) );
#else
QCOMPARE( w_favoriteauthors.value(), QVariant( QStringLiteral( "{}" ) ) );
QCOMPARE( w_favoriteauthors.value(), QVariant( QStringLiteral( "{}" ) ) );
#endif
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
// FEATURE 5
w_favoriteauthors.setFeature( vl_json->getFeature( 5 ) );
// FEATURE 5
w_favoriteauthors.setFeature( vl_json->getFeature( 5 ) );
//check if first feature checked correctly (blank)
//check if first feature checked correctly (blank)
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList() ) );
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList() ) );
#else
QCOMPARE( w_favoriteauthors.value(), QVariant( QStringLiteral( "{}" ) ) );
QCOMPARE( w_favoriteauthors.value(), QVariant( QStringLiteral( "{}" ) ) );
#endif
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 6, 0 )->checkState(), Qt::Unchecked );
}
void TestQgsValueRelationWidgetWrapper::testMatchLayerName()
void TestQgsValueRelationWidgetWrapper::testMatchLayerName()
{
// create a vector layer
QgsVectorLayer vl1( QStringLiteral( "Polygon?crs=epsg:4326&field=pk:int&field=province:int&field=municipality:string" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
@ -1606,7 +1606,6 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
w_municipality.setValue( 0 );
QCOMPARE( w_municipality.mComboBox->currentIndex(), 1 );
QCOMPARE( w_municipality.mComboBox->currentText(), QStringLiteral( "Some Place By The River" ) );
}
QGSTEST_MAIN( TestQgsValueRelationWidgetWrapper )