38 const QString columnNameRx( QStringLiteral(
"[a-zA-Z_\\x80-\\xFF][a-zA-Z0-9_\\x80-\\xFF]*" ) );
44 const QList<QPair<QString, QString> > items = QUrlQuery( url ).queryItems( QUrl::FullyEncoded );
45 for (
int i = 0; i < items.size(); i++ )
47 const QString key = items.
at( i ).first;
48 const QString value = items.at( i ).second;
49 if ( key == QLatin1String(
"layer_ref" ) )
53 const int pos = value.indexOf(
':' );
54 QString layerId, vlayerName;
57 layerId = QUrl::fromPercentEncoding( value.toUtf8() );
58 vlayerName = QStringLiteral(
"vtab%1" ).arg( layerIdx );
62 layerId = QUrl::fromPercentEncoding( value.left( pos ).toUtf8() );
63 vlayerName = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
68 else if ( key == QLatin1String(
"layer" ) )
72 const int pos = value.indexOf(
':' );
75 QString providerKey, source, vlayerName, encoding = QStringLiteral(
"UTF-8" );
77 providerKey = value.left( pos );
78 int pos2 = value.indexOf(
':', pos + 1 );
79 if ( pos2 - pos == 2 )
80 pos2 = value.indexOf(
':', pos + 3 );
83 source = QUrl::fromPercentEncoding( value.mid( pos + 1, pos2 - pos - 1 ).toUtf8() );
84 const int pos3 = value.indexOf(
':', pos2 + 1 );
87 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1, pos3 - pos2 - 1 ).toUtf8() );
88 encoding = value.mid( pos3 + 1 );
92 vlayerName = QUrl::fromPercentEncoding( value.mid( pos2 + 1 ).toUtf8() );
97 source = QUrl::fromPercentEncoding( value.mid( pos + 1 ).toUtf8() );
98 vlayerName = QStringLiteral(
"vtab%1" ).arg( layerIdx );
101 def.
addSource( vlayerName, source, providerKey, encoding );
104 else if ( key == QLatin1String(
"geometry" ) )
108 const thread_local QRegularExpression reGeom(
"(" + columnNameRx +
")(?::([a-zA-Z0-9]+):(\\d+))?" );
109 const QRegularExpressionMatch match = reGeom.match( value );
110 if ( match.hasMatch() )
113 if ( match.capturedTexts().size() > 2 )
119 wkbType =
static_cast<Qgis::WkbType>( match.captured( 2 ).toLong() );
126 else if ( key == QLatin1String(
"nogeometry" ) )
130 else if ( key == QLatin1String(
"uid" ) )
134 else if ( key == QLatin1String(
"query" ) )
137 def.
setQuery( QUrl::fromPercentEncoding( value.toUtf8() ) );
139 else if ( key == QLatin1String(
"field" ) )
142 const thread_local QRegularExpression reField(
"(" + columnNameRx +
"):(int|real|text)" );
143 const QRegularExpressionMatch match = reField.match( value );
144 if ( match.hasMatch() )
146 const QString fieldName( match.captured( 1 ) );
147 const QString fieldType( match.captured( 2 ) );
148 if ( fieldType == QLatin1String(
"int" ) )
152 else if ( fieldType == QLatin1String(
"real" ) )
156 if ( fieldType == QLatin1String(
"text" ) )
162 else if ( key == QLatin1String(
"lazy" ) )
166 else if ( key == QLatin1String(
"subsetstring" ) )
180 url = QUrl::fromLocalFile(
filePath() );
182 QUrlQuery urlQuery( url );
187 if ( l.isReferenced() )
188 urlQuery.addQueryItem( QStringLiteral(
"layer_ref" ), QStringLiteral(
"%1:%2" ).arg( l.reference(), l.name() ) );
195 QString( QUrl::toPercentEncoding( l.name() ) ),
197 QString( QUrl::toPercentEncoding( l.source() ) ) ).toUtf8() ) );
200 if ( !
query().isEmpty() )
202 urlQuery.addQueryItem( QStringLiteral(
"query" ),
query() );
205 if ( !
uid().isEmpty() )
206 urlQuery.addQueryItem( QStringLiteral(
"uid" ),
uid() );
209 urlQuery.addQueryItem( QStringLiteral(
"nogeometry" ), QString() );
215 urlQuery.addQueryItem( QStringLiteral(
"geometry" ),
geometryField() );
218 const auto constFields =
fields();
219 for (
const QgsField &f : constFields )
221 if ( f.type() == QMetaType::Type::Int
222 || f.type() == QMetaType::Type::UInt
223 || f.type() == QMetaType::Type::Bool
224 || f.type() == QMetaType::Type::LongLong )
225 urlQuery.addQueryItem( QStringLiteral(
"field" ), f.name() +
":int" );
226 else if ( f.type() == QMetaType::Type::Double )
227 urlQuery.addQueryItem( QStringLiteral(
"field" ), f.name() +
":real" );
228 else if ( f.type() == QMetaType::Type::QString )
229 urlQuery.addQueryItem( QStringLiteral(
"field" ), f.name() +
":text" );
234 urlQuery.addQueryItem( QStringLiteral(
"lazy" ), QString() );
239 urlQuery.addQueryItem( QStringLiteral(
"subsetstring" ), QUrl::toPercentEncoding(
subsetString() ) );
242 url.setQuery( urlQuery );