26#include <QRegularExpression>
39 while ( i <
uri.length() )
45 QgsDebugMsg( QStringLiteral(
"parameter name expected before =" ) );
52 while ( i <
uri.length() &&
uri[i] !=
'=' && !
uri[i].isSpace() )
55 const QString pname =
uri.mid( start, i - start );
59 if ( i ==
uri.length() ||
uri[i] !=
'=' )
67 if ( pname == QLatin1String(
"sql" ) )
76 const QString pval = getValue(
uri, i );
78 if ( pname == QLatin1String(
"table" ) )
80 if ( i <
uri.length() &&
uri[i] ==
'.' )
85 mTable = getValue(
uri, i );
92 if ( i <
uri.length() &&
uri[i] ==
'(' )
97 while ( i <
uri.length() &&
uri[i] !=
')' )
104 if ( i ==
uri.length() )
106 QgsDebugMsg( QStringLiteral(
"closing parenthesis missing" ) );
109 mGeometryColumn =
uri.mid( start, i - start );
110 mGeometryColumn.replace( QLatin1String(
"\\)" ), QLatin1String(
")" ) );
111 mGeometryColumn.replace( QLatin1String(
"\\\\" ), QLatin1String(
"\\" ) );
117 mGeometryColumn = QString();
120 else if ( pname == QLatin1String(
"schema" ) )
124 else if ( pname == QLatin1String(
"key" ) )
128 else if ( pname == QLatin1String(
"estimatedmetadata" ) )
130 mUseEstimatedMetadata = pval == QLatin1String(
"true" );
132 else if ( pname == QLatin1String(
"srid" ) )
136 else if ( pname == QLatin1String(
"type" ) )
140 else if ( pname == QLatin1String(
"selectatid" ) )
142 mSelectAtIdDisabledSet =
true;
143 mSelectAtIdDisabled = pval == QLatin1String(
"false" );
145 else if ( pname == QLatin1String(
"service" ) )
149 else if ( pname == QLatin1String(
"authcfg" ) )
151 mAuthConfigId = pval;
153 else if ( pname == QLatin1String(
"user" ) || pname == QLatin1String(
"username" ) )
157 else if ( pname == QLatin1String(
"password" ) )
161 else if ( pname == QLatin1String(
"connect_timeout" ) )
165 else if ( pname == QLatin1String(
"dbname" ) )
169 else if ( pname == QLatin1String(
"host" ) )
173 else if ( pname == QLatin1String(
"hostaddr" ) )
175 QgsDebugMsg( QStringLiteral(
"database host ip address ignored" ) );
177 else if ( pname == QLatin1String(
"port" ) )
181 else if ( pname == QLatin1String(
"driver" ) )
185 else if ( pname == QLatin1String(
"tty" ) )
187 QgsDebugMsg( QStringLiteral(
"backend debug tty ignored" ) );
189 else if ( pname == QLatin1String(
"options" ) )
191 QgsDebugMsg( QStringLiteral(
"backend debug options ignored" ) );
193 else if ( pname == QLatin1String(
"sslmode" ) )
197 else if ( pname == QLatin1String(
"requiressl" ) )
199 if ( pval == QLatin1String(
"0" ) )
204 else if ( pname == QLatin1String(
"krbsrvname" ) )
206 QgsDebugMsg( QStringLiteral(
"kerberos server name ignored" ) );
208 else if ( pname == QLatin1String(
"gsslib" ) )
214 mHttpHeaders.
insert( pname, pval );
218 QgsDebugMsgLevel(
"parameter \"" + pname +
"\":\"" + pval +
"\" added", 4 );
227 QRegularExpression regexp;
228 regexp.setPatternOptions( QRegularExpression::InvertedGreedinessOption );
229 QString safeName( aUri );
230 if ( aUri.contains( QLatin1String(
" password=" ) ) )
232 regexp.setPattern( QStringLiteral(
" password=.* " ) );
233 safeName.replace( regexp, QStringLiteral(
" " ) );
235 else if ( aUri.contains( QLatin1String(
",password=" ) ) )
237 regexp.setPattern( QStringLiteral(
",password=.*," ) );
238 safeName.replace( regexp, QStringLiteral(
"," ) );
240 else if ( aUri.contains( QLatin1String(
"IDB:" ) ) )
242 regexp.setPattern( QStringLiteral(
" pass=.* " ) );
243 safeName.replace( regexp, QStringLiteral(
" " ) );
245 else if ( ( aUri.contains( QLatin1String(
"OCI:" ) ) )
246 || ( aUri.contains( QLatin1String(
"ODBC:" ) ) ) )
248 regexp.setPattern( QStringLiteral(
"/.*@" ) );
249 safeName.replace( regexp, QStringLiteral(
"/@" ) );
251 else if ( aUri.contains( QLatin1String(
"SDE:" ) ) )
253 QStringList strlist = aUri.split(
',' );
254 safeName = strlist[0] +
',' + strlist[1] +
',' + strlist[2] +
',' + strlist[3];
261 return mAuthConfigId;
331 return mGeometryColumn;
354 mUseEstimatedMetadata = flag;
359 return mUseEstimatedMetadata;
364 mSelectAtIdDisabledSet =
true;
365 mSelectAtIdDisabled = flag;
370 return mSelectAtIdDisabled;
388QString QgsDataSourceUri::escape(
const QString &val, QChar delim =
'\'' )
const
390 QString escaped = val;
392 escaped.replace(
'\\', QLatin1String(
"\\\\" ) );
393 escaped.replace( delim, QStringLiteral(
"\\%1" ).arg( delim ) );
408void QgsDataSourceUri::skipBlanks(
const QString &uri,
int &i )
411 while ( i <
uri.length() &&
uri[i].isSpace() )
415QString QgsDataSourceUri::getValue(
const QString &uri,
int &i )
417 skipBlanks(
uri, i );
421 if ( i <
uri.length() && (
uri[i] ==
'\'' ||
uri[i] ==
'"' ) )
423 const QChar delim =
uri[i];
430 if ( i ==
uri.length() )
432 QgsDebugMsg( QStringLiteral(
"unterminated quoted string in connection info string" ) );
436 if (
uri[i] ==
'\\' )
439 if ( i ==
uri.length() )
441 if (
uri[i] != delim &&
uri[i] !=
'\\' )
444 else if (
uri[i] == delim )
456 while ( i <
uri.length() )
458 if (
uri[i].isSpace() )
464 if (
uri[i] ==
'\\' )
467 if ( i ==
uri.length() )
469 if (
uri[i] !=
'\\' &&
uri[i] !=
'\'' )
477 skipBlanks(
uri, i );
484 QStringList connectionItems;
486 if ( !mDatabase.isEmpty() )
488 connectionItems <<
"dbname='" + escape( mDatabase ) +
'\'';
491 if ( !mService.isEmpty() )
493 connectionItems <<
"service='" + escape( mService ) +
'\'';
495 else if ( !mHost.isEmpty() )
497 connectionItems <<
"host=" + mHost;
500 if ( mService.isEmpty() )
502 if ( !mPort.isEmpty() )
503 connectionItems <<
"port=" + mPort;
506 if ( !mDriver.isEmpty() )
508 connectionItems <<
"driver='" + escape( mDriver ) +
'\'';
511 if ( !mUsername.isEmpty() )
513 connectionItems <<
"user='" + escape( mUsername ) +
'\'';
515 if ( !mPassword.isEmpty() )
517 connectionItems <<
"password='" + escape( mPassword ) +
'\'';
523 connectionItems << QStringLiteral(
"sslmode=" ) +
encodeSslMode( mSSLmode );
526 if ( !mAuthConfigId.isEmpty() )
528 if ( expandAuthConfig )
532 QgsDebugMsg( QStringLiteral(
"Data source URI FAILED to update via loading configuration ID '%1'" ).arg( mAuthConfigId ) );
537 connectionItems <<
"authcfg=" + mAuthConfigId;
541 return connectionItems.join( QLatin1Char(
' ' ) );
548 if ( !mKeyColumn.isEmpty() )
550 uri += QStringLiteral(
" key='%1'" ).arg( escape( mKeyColumn ) );
553 if ( mUseEstimatedMetadata )
555 uri += QLatin1String(
" estimatedmetadata=true" );
558 if ( !mSrid.isEmpty() )
560 uri += QStringLiteral(
" srid=%1" ).arg( mSrid );
565 uri += QLatin1String(
" type=" );
569 if ( mSelectAtIdDisabled )
571 uri += QLatin1String(
" selectatid=false" );
574 for (
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
576 if ( it.key().contains(
'=' ) || it.key().contains(
' ' ) )
578 QgsDebugMsg( QStringLiteral(
"invalid uri parameter %1 skipped" ).arg( it.key() ) );
582 uri +=
' ' + it.key() +
"='" + escape( it.value() ) +
'\'';
587 QString columnName( mGeometryColumn );
588 columnName.replace(
'\\', QLatin1String(
"\\\\" ) );
589 columnName.replace(
')', QLatin1String(
"\\)" ) );
591 if ( !mTable.isEmpty() )
593 uri += QStringLiteral(
" table=%1%2" )
595 mGeometryColumn.isEmpty() ? QString() : QStringLiteral(
" (%1)" ).arg( columnName ) );
597 else if ( !mSchema.isEmpty() )
599 uri += QStringLiteral(
" schema='%1'" ).arg( escape( mSchema ) );
602 if ( !mSql.isEmpty() )
604 uri += QStringLiteral(
" sql=" ) + mSql;
613 if (
string.isEmpty() )
614 return string.isNull() ? QByteArray() : QByteArray(
"" );
615 return string.toLatin1();
621 for (
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
623 url.addQueryItem( it.key(), it.value() );
626 if ( !mUsername.isEmpty() )
627 url.addQueryItem( QStringLiteral(
"username" ), mUsername );
629 if ( !mPassword.isEmpty() )
630 url.addQueryItem( QStringLiteral(
"password" ), mPassword );
632 if ( !mAuthConfigId.isEmpty() )
633 url.addQueryItem( QStringLiteral(
"authcfg" ), mAuthConfigId );
645 mAuthConfigId.clear();
648 url.setQuery( QString::fromLatin1(
uri ) );
649 const QUrlQuery query( url );
653 const auto constQueryItems = query.queryItems( QUrl::ComponentFormattingOption::FullyDecoded );
654 for (
const QPair<QString, QString> &item : constQueryItems )
658 if ( item.first == QLatin1String(
"username" ) )
659 mUsername = item.second;
660 else if ( item.first == QLatin1String(
"password" ) )
661 mPassword = item.second;
662 else if ( item.first == QLatin1String(
"authcfg" ) )
663 mAuthConfigId = item.second;
665 mParams.insert( item.first, item.second );
677 if ( !mSchema.isEmpty() )
678 return QStringLiteral(
"\"%1\".\"%2\"" )
679 .arg( escape( mSchema,
'"' ),
680 escape( mTable,
'"' ) );
682 return QStringLiteral(
"\"%1\"" )
683 .arg( escape( mTable,
'"' ) );
688 const QString &database,
689 const QString &username,
690 const QString &password,
692 const QString &authConfigId )
704 const QString &database,
705 const QString &username,
706 const QString &password,
708 const QString &authConfigId )
719 const QString &table,
720 const QString &geometryColumn,
722 const QString &keyColumn )
733 mAuthConfigId = authcfg;
763 if (
sslMode == QLatin1String(
"prefer" ) )
765 else if (
sslMode == QLatin1String(
"disable" ) )
767 else if (
sslMode == QLatin1String(
"allow" ) )
769 else if (
sslMode == QLatin1String(
"require" ) )
771 else if (
sslMode == QLatin1String(
"verify-ca" ) )
773 else if (
sslMode == QLatin1String(
"verify-full" ) )
783 case SslPrefer:
return QStringLiteral(
"prefer" );
784 case SslDisable:
return QStringLiteral(
"disable" );
785 case SslAllow:
return QStringLiteral(
"allow" );
786 case SslRequire:
return QStringLiteral(
"require" );
787 case SslVerifyCa:
return QStringLiteral(
"verify-ca" );
796 if ( key == QLatin1String(
"username" ) )
798 else if ( key == QLatin1String(
"password" ) )
800 else if ( key == QLatin1String(
"authcfg" ) )
801 mAuthConfigId = value;
805 mParams.insert( key, value );
811 for (
const QString &val : value )
819 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
824 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
829 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
831 mAuthConfigId.clear();
835 return mParams.remove( key );
841 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
843 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
845 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
846 return mAuthConfigId;
848 return mParams.value( key );
854 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
855 return QStringList() << mUsername;
856 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
857 return QStringList() << mPassword;
858 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
859 return QStringList() << mAuthConfigId;
861 return mParams.values( key );
867 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
869 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
871 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
874 return mParams.contains( key );
879 QSet<QString> paramKeys;
880 for (
auto it = mParams.constBegin(); it != mParams.constEnd(); it++ )
881 paramKeys.insert( it.key() );
883 if ( !mHost.isEmpty() )
884 paramKeys.insert( QLatin1String(
"host" ) );
885 if ( !mPort.isEmpty() )
886 paramKeys.insert( QLatin1String(
"port" ) );
887 if ( !mDriver.isEmpty() )
888 paramKeys.insert( QLatin1String(
"driver" ) );
889 if ( !mService.isEmpty() )
890 paramKeys.insert( QLatin1String(
"service" ) );
891 if ( !mDatabase.isEmpty() )
892 paramKeys.insert( QLatin1String(
"dbname" ) );
893 if ( !mSchema.isEmpty() )
894 paramKeys.insert( QLatin1String(
"schema" ) );
895 if ( !mTable.isEmpty() )
896 paramKeys.insert( QLatin1String(
"table" ) );
898 if ( !mSql.isEmpty() )
899 paramKeys.insert( QLatin1String(
"sql" ) );
900 if ( !mAuthConfigId.isEmpty() )
901 paramKeys.insert( QLatin1String(
"authcfg" ) );
902 if ( !mUsername.isEmpty() )
903 paramKeys.insert( QLatin1String(
"username" ) );
904 if ( !mPassword.isEmpty() )
905 paramKeys.insert( QLatin1String(
"password" ) );
907 paramKeys.insert( QLatin1String(
"sslmode" ) );
908 if ( !mKeyColumn.isEmpty() )
909 paramKeys.insert( QLatin1String(
"key" ) );
910 if ( mUseEstimatedMetadata )
911 paramKeys.insert( QLatin1String(
"estimatedmetadata" ) );
912 if ( mSelectAtIdDisabledSet )
913 paramKeys.insert( QLatin1String(
"selectatid" ) );
915 paramKeys.insert( QLatin1String(
"type" ) );
916 if ( !mSrid.isEmpty() )
917 paramKeys.insert( QLatin1String(
"srid" ) );
WkbType
The WKB type describes the number of dimensions a geometry has.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
QString srid() const
Returns the spatial reference ID associated with the URI.
SslMode
Available SSL connection modes.
void setConnection(const QString &aHost, const QString &aPort, const QString &aDatabase, const QString &aUsername, const QString &aPassword, SslMode sslmode=SslPrefer, const QString &authConfigId=QString())
Sets all connection related members at once.
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
QStringList params(const QString &key) const
Returns multiple generic parameter values corresponding to the specified key.
void setSchema(const QString &schema)
Sets the scheme for the URI.
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
int removeParam(const QString &key)
Removes a generic parameter by key.
static SslMode decodeSslMode(const QString &sslMode)
Decodes SSL mode string into enum value.
void setSql(const QString &sql)
Sets the sql filter for the URI.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
QString table() const
Returns the table name stored in the URI.
void setTable(const QString &table)
Sets table to table.
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
QString quotedTablename() const
Returns the URI's table name, escaped and quoted.
void setGeometryColumn(const QString &geometryColumn)
Sets geometry column name to geometryColumn.
QString schema() const
Returns the schema stored in the URI.
void setUseEstimatedMetadata(bool flag)
Sets whether estimated metadata should be used for the connection.
QString connectionInfo(bool expandAuthConfig=true) const
Returns the connection part of the URI.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
void setUsername(const QString &username)
Sets the username for the URI.
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
void disableSelectAtId(bool flag)
Set to true to disable selection by feature ID.
bool selectAtIdDisabled() const
Returns whether the selection by feature ID is disabled.
void setDataSource(const QString &aSchema, const QString &aTable, const QString &aGeometryColumn, const QString &aSql=QString(), const QString &aKeyColumn=QString())
Sets all data source related members at once.
QString username() const
Returns the username stored in the URI.
static QString encodeSslMode(SslMode sslMode)
Encodes SSL mode enum value into a string.
Qgis::WkbType wkbType() const
Returns the WKB type associated with the URI.
void setWkbType(Qgis::WkbType type)
Sets the WKB type associated with the URI.
QString driver() const
Returns the driver name stored in the URI.
QString host() const
Returns the host name stored in the URI.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
QString service() const
Returns the service name associated with the URI.
void setKeyColumn(const QString &column)
Sets the name of the (primary) key column.
bool useEstimatedMetadata() const
Returns true if estimated metadata should be used for the connection.
SslMode sslMode() const
Returns the SSL mode associated with the URI.
QString password() const
Returns the password stored in the URI.
QString keyColumn() const
Returns the name of the (primary) key column for the referenced table.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
QString port() const
Returns the port stored in the URI.
QSet< QString > parameterKeys() const
Returns parameter keys used in the uri: specialized ones ("table", "schema", etc.) or generic paramet...
QString database() const
Returns the database name stored in the URI.
void clearSchema()
Clears the schema stored in the URI.
void setDriver(const QString &driver)
Sets the driver name stored in the URI.
void setDatabase(const QString &database)
Sets the URI database name.
QString geometryColumn() const
Returns the name of the geometry column stored in the URI, if set.
void setSrid(const QString &srid)
Sets the spatial reference ID associated with the URI.
static QString removePassword(const QString &aUri)
Removes the password element from a URI.
QString sql() const
Returns the SQL filter stored in the URI, if set.
void setPassword(const QString &password)
Sets the password for the URI.
static Qgis::WkbType parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
static QString displayString(Qgis::WkbType type) SIP_HOLDGIL
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
QByteArray toLatin1_helper(const QString &string)
#define QgsDebugMsgLevel(str, level)