Skip to content
Permalink
Browse files
Merge pull request #44099 from domi4484/fix44087MissingTypeNamesForUR…
…IRegexMemoryProvider

Added missing type names to uri parsing regex for memory provider
  • Loading branch information
nirvn committed Jul 12, 2021
2 parents 7d13cce + bd22811 commit f7646705ccd43e7ec3eb1a930625e1d65c658b6b
Showing with 124 additions and 71 deletions.
  1. +76 −69 src/core/providers/memory/qgsmemoryprovider.cpp
  2. +1 −1 src/core/qgsfield.cpp
  3. +47 −1 tests/src/python/test_provider_memory.py
@@ -25,7 +25,7 @@

#include <QUrl>
#include <QUrlQuery>
#include <QRegExp>
#include <QRegularExpression>

///@cond PRIVATE

@@ -119,95 +119,101 @@ QgsMemoryProvider::QgsMemoryProvider( const QString &uri, const ProviderOptions
if ( query.hasQueryItem( QStringLiteral( "field" ) ) )
{
QList<QgsField> attributes;
QRegExp reFieldDef( "\\:"
"(int|integer|long|int8|real|double|string|date|time|datetime|binary|bool|boolean)" // type
"(?:\\((\\-?\\d+)" // length
"(?:\\,(\\-?\\d+))?" // precision
"\\))?(\\[\\])?" // array
"$", Qt::CaseInsensitive );
const thread_local QRegularExpression reFieldDef( "\\:"
"([\\w\\s]+)" // type
"(?:\\((\\-?\\d+)" // length
"(?:\\,(\\-?\\d+))?" // precision
"\\))?(\\[\\])?" // array
"$",
QRegularExpression::CaseInsensitiveOption );
QStringList fields = query.allQueryItemValues( QStringLiteral( "field" ) );
for ( int i = 0; i < fields.size(); i++ )
{
QString name = QUrl::fromPercentEncoding( fields.at( i ).toUtf8() );
QRegularExpressionMatch regularExpressionMatch = reFieldDef.match( name );

// If no match -> use string as type
QVariant::Type type = QVariant::String;
QVariant::Type subType = QVariant::Invalid;
QString typeName( QStringLiteral( "string" ) );
int length = 255;
int precision = 0;

int pos = reFieldDef.indexIn( name );
if ( pos >= 0 )
if ( regularExpressionMatch.hasMatch() )
{
name = name.mid( 0, pos );
typeName = reFieldDef.cap( 1 ).toLower();
if ( typeName == QLatin1String( "int" ) || typeName == QLatin1String( "integer" ) )
name = name.mid( 0, regularExpressionMatch.capturedStart() );
typeName = regularExpressionMatch.captured( 1 ).toLower();

// Search typeName correspondence in native types
bool isNativeType = false;
const QList<QgsVectorDataProvider::NativeType> nativeTypesList( nativeTypes() );
for ( const NativeType &nativeType : nativeTypesList )
{
type = QVariant::Int;
typeName = QStringLiteral( "integer" );
length = -1;
if ( nativeType.mTypeName.toLower() == typeName )
{
isNativeType = true;
type = nativeType.mType;
subType = nativeType.mSubType;
typeName = nativeType.mTypeName;
break;
}
}
else if ( typeName == QLatin1String( "int8" ) || typeName == QLatin1String( "long" ) )

// Not a native type -> check other supported types:
if ( isNativeType == false )
{
type = QVariant::LongLong;
typeName = QStringLiteral( "int8" );
length = -1;
if ( typeName == QLatin1String( "int" ) )
{
type = QVariant::Int;
typeName = QStringLiteral( "integer" );
}
else if ( typeName == QLatin1String( "long" ) )
{
type = QVariant::LongLong;
typeName = QStringLiteral( "int8" );
}
else if ( typeName == QLatin1String( "bool" ) )
{
type = QVariant::Bool;
typeName = QStringLiteral( "boolean" );
}
else
{
QgsLogger::warning( tr( "Unsupported typeName '%1'. Will be handled as string." ).arg( typeName ) );
type = QVariant::String;
typeName = QStringLiteral( "string" );
}
}
else if ( typeName == QLatin1String( "real" ) || typeName == QLatin1String( "double" ) )

// Set default length/precision for double/real
if ( typeName == QLatin1String( "real" ) || typeName == QLatin1String( "double" ) )
{
type = QVariant::Double;
typeName = QStringLiteral( "double" );
length = 20;
precision = 5;
}
else if ( typeName == QLatin1String( "date" ) )
{
type = QVariant::Date;
typeName = QStringLiteral( "date" );
length = -1;
}
else if ( typeName == QLatin1String( "time" ) )
{
type = QVariant::Time;
typeName = QStringLiteral( "time" );
length = -1;
}
else if ( typeName == QLatin1String( "datetime" ) )
{
type = QVariant::DateTime;
typeName = QStringLiteral( "datetime" );
length = -1;
}
else if ( typeName == QLatin1String( "bool" ) || typeName == QLatin1String( "boolean" ) )
{
type = QVariant::Bool;
typeName = QStringLiteral( "boolean" );
length = -1;
}
else if ( typeName == QLatin1String( "binary" ) )
{
type = QVariant::ByteArray;
typeName = QStringLiteral( "binary" );
length = -1;
}

if ( !reFieldDef.cap( 2 ).isEmpty() )
{
length = reFieldDef.cap( 2 ).toInt();
}
if ( !reFieldDef.cap( 3 ).isEmpty() )
{
precision = reFieldDef.cap( 3 ).toInt();
}
if ( !reFieldDef.cap( 4 ).isEmpty() )
if ( !regularExpressionMatch.captured( 2 ).isEmpty() )
length = regularExpressionMatch.captured( 2 ).toInt();

if ( !regularExpressionMatch.captured( 3 ).isEmpty() )
precision = regularExpressionMatch.captured( 3 ).toInt();

// Array
if ( !regularExpressionMatch.captured( 4 ).isEmpty() )
{
//array
subType = type;
type = type == QVariant::String ? QVariant::StringList : QVariant::List;
typeName += QLatin1String( "list" );
if ( subType == QVariant::Invalid )
subType = type;

if ( type != QVariant::List && type != QVariant::StringList )
type = type == QVariant::String ? QVariant::StringList : QVariant::List;

const QLatin1String listSuffix( "list" );
if ( !typeName.endsWith( listSuffix ) )
typeName += QLatin1String( "list" );
}
}
if ( !name.isEmpty() )
attributes.append( QgsField( name, type, typeName, length, precision, QString(), subType ) );

attributes.append( QgsField( name, type, typeName, length, precision, QString(), subType ) );
}
addAttributes( attributes );
}
@@ -564,7 +570,8 @@ bool QgsMemoryProvider::addAttributes( const QList<QgsField> &attributes )
// Make sure added attributes typeName correspond to a native type name
bool isNativeTypeName = false;
NativeType nativeTypeCandidate( QString(), QString(), QVariant::Invalid );
for ( const NativeType &nativeType : nativeTypes() )
const QList<QgsVectorDataProvider::NativeType> nativeTypesList( nativeTypes() );
for ( const NativeType &nativeType : nativeTypesList )
{
if ( nativeType.mTypeName.toLower() == field.typeName().toLower() )
{
@@ -580,7 +587,7 @@ bool QgsMemoryProvider::addAttributes( const QList<QgsField> &attributes )
{
if ( nativeTypeCandidate.mType == QVariant::Invalid )
{
QgsDebugMsg( "Field type not supported: " + field.typeName() );
QgsLogger::warning( "Field type not supported: " + field.typeName() );
continue;
}

@@ -312,7 +312,7 @@ QString QgsField::displayString( const QVariant &v ) const
}
}
// Default for doubles with precision
else if ( d->type == QVariant::Double && d->precision > 0 )
else if ( d->precision > 0 )
{
if ( -1 < v.toDouble() && v.toDouble() < 1 )
{
@@ -924,11 +924,57 @@ def testAddAttributes(self):
# add fields
pr.addAttributes([QgsField("name", QVariant.String),
QgsField("age", QVariant.Int, "invalidInteger"),
QgsField("size", QVariant.Double)])
QgsField("size", QVariant.Double),
QgsField("mytext", QVariant.String, "text"),
QgsField("size2", QVariant.Double, "double precision"),
QgsField("short", QVariant.Int, "int2"),
QgsField("lessshort", QVariant.Int, "int4"),
QgsField("numericfield", QVariant.Double, "numeric"),
QgsField("decimalfield", QVariant.Double, "decimal"),
QgsField("stringlistfield", QVariant.StringList, "stringlist"),
QgsField("integerlistfield", QVariant.List, "integerlist"),
QgsField("doublelistfield", QVariant.List, "doublelist")])

self.assertEqual(pr.fields()[0].typeName(), "string")
self.assertEqual(pr.fields()[1].typeName(), "integer")
self.assertEqual(pr.fields()[2].typeName(), "double")
self.assertEqual(pr.fields()[3].typeName(), "text")
self.assertEqual(pr.fields()[4].typeName(), "double precision")
self.assertEqual(pr.fields()[5].typeName(), "int2")
self.assertEqual(pr.fields()[6].typeName(), "int4")
self.assertEqual(pr.fields()[7].typeName(), "numeric")
self.assertEqual(pr.fields()[8].typeName(), "decimal")
self.assertEqual(pr.fields()[9].typeName(), "stringlist")
self.assertEqual(pr.fields()[10].typeName(), "integerlist")
self.assertEqual(pr.fields()[11].typeName(), "doublelist")

vl2 = vl.clone()

self.assertEqual(pr.fields()[0].name(), vl2.fields()[0].name())
self.assertEqual(pr.fields()[1].name(), vl2.fields()[1].name())
self.assertEqual(pr.fields()[2].name(), vl2.fields()[2].name())
self.assertEqual(pr.fields()[3].name(), vl2.fields()[3].name())
self.assertEqual(pr.fields()[4].name(), vl2.fields()[4].name())
self.assertEqual(pr.fields()[5].name(), vl2.fields()[5].name())
self.assertEqual(pr.fields()[6].name(), vl2.fields()[6].name())
self.assertEqual(pr.fields()[7].name(), vl2.fields()[7].name())
self.assertEqual(pr.fields()[8].name(), vl2.fields()[8].name())
self.assertEqual(pr.fields()[9].name(), vl2.fields()[9].name())
self.assertEqual(pr.fields()[10].name(), vl2.fields()[10].name())
self.assertEqual(pr.fields()[11].name(), vl2.fields()[11].name())

self.assertEqual(pr.fields()[0].typeName(), vl2.fields()[0].typeName())
self.assertEqual(pr.fields()[1].typeName(), vl2.fields()[1].typeName())
self.assertEqual(pr.fields()[2].typeName(), vl2.fields()[2].typeName())
self.assertEqual(pr.fields()[3].typeName(), vl2.fields()[3].typeName())
self.assertEqual(pr.fields()[4].typeName(), vl2.fields()[4].typeName())
self.assertEqual(pr.fields()[5].typeName(), vl2.fields()[5].typeName())
self.assertEqual(pr.fields()[6].typeName(), vl2.fields()[6].typeName())
self.assertEqual(pr.fields()[7].typeName(), vl2.fields()[7].typeName())
self.assertEqual(pr.fields()[8].typeName(), vl2.fields()[8].typeName())
self.assertEqual(pr.fields()[9].typeName(), vl2.fields()[9].typeName())
self.assertEqual(pr.fields()[10].typeName(), vl2.fields()[10].typeName())
self.assertEqual(pr.fields()[11].typeName(), vl2.fields()[11].typeName())


class TestPyQgsMemoryProviderIndexed(unittest.TestCase, ProviderTestCase):

0 comments on commit f764670

Please sign in to comment.