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 if ( mSql == QLatin1String(
"''" ) || mSql == QLatin1String(
"\"\"" ) )
82 const QString pval = getValue(
uri, i );
84 if ( pname == QLatin1String(
"table" ) )
86 if ( i <
uri.length() &&
uri[i] ==
'.' )
91 mTable = getValue(
uri, i );
98 if ( i <
uri.length() &&
uri[i] ==
'(' )
103 while ( i <
uri.length() &&
uri[i] !=
')' )
105 if (
uri[i] ==
'\\' )
110 if ( i ==
uri.length() )
112 QgsDebugMsg( QStringLiteral(
"closing parenthesis missing" ) );
115 mGeometryColumn =
uri.mid( start, i - start );
116 mGeometryColumn.replace( QLatin1String(
"\\)" ), QLatin1String(
")" ) );
117 mGeometryColumn.replace( QLatin1String(
"\\\\" ), QLatin1String(
"\\" ) );
123 mGeometryColumn = QString();
126 else if ( pname == QLatin1String(
"schema" ) )
130 else if ( pname == QLatin1String(
"key" ) )
134 else if ( pname == QLatin1String(
"estimatedmetadata" ) )
136 mUseEstimatedMetadata = pval == QLatin1String(
"true" );
138 else if ( pname == QLatin1String(
"srid" ) )
142 else if ( pname == QLatin1String(
"type" ) )
146 else if ( pname == QLatin1String(
"selectatid" ) )
148 mSelectAtIdDisabledSet =
true;
149 mSelectAtIdDisabled = pval == QLatin1String(
"false" );
151 else if ( pname == QLatin1String(
"service" ) )
155 else if ( pname == QLatin1String(
"authcfg" ) )
157 mAuthConfigId = pval;
159 else if ( pname == QLatin1String(
"user" ) || pname == QLatin1String(
"username" ) )
163 else if ( pname == QLatin1String(
"password" ) )
167 else if ( pname == QLatin1String(
"connect_timeout" ) )
171 else if ( pname == QLatin1String(
"dbname" ) )
175 else if ( pname == QLatin1String(
"host" ) )
179 else if ( pname == QLatin1String(
"hostaddr" ) )
181 QgsDebugMsg( QStringLiteral(
"database host ip address ignored" ) );
183 else if ( pname == QLatin1String(
"port" ) )
187 else if ( pname == QLatin1String(
"driver" ) )
191 else if ( pname == QLatin1String(
"tty" ) )
193 QgsDebugMsg( QStringLiteral(
"backend debug tty ignored" ) );
195 else if ( pname == QLatin1String(
"options" ) )
197 QgsDebugMsg( QStringLiteral(
"backend debug options ignored" ) );
199 else if ( pname == QLatin1String(
"sslmode" ) )
203 else if ( pname == QLatin1String(
"requiressl" ) )
205 if ( pval == QLatin1String(
"0" ) )
210 else if ( pname == QLatin1String(
"krbsrvname" ) )
212 QgsDebugMsg( QStringLiteral(
"kerberos server name ignored" ) );
214 else if ( pname == QLatin1String(
"gsslib" ) )
220 mHttpHeaders.
insert( pname, pval );
224 QgsDebugMsgLevel(
"parameter \"" + pname +
"\":\"" + pval +
"\" added", 4 );
233 QRegularExpression regexp;
234 regexp.setPatternOptions( QRegularExpression::InvertedGreedinessOption );
235 QString safeName( aUri );
236 if ( aUri.contains( QLatin1String(
" password=" ) ) )
238 regexp.setPattern( QStringLiteral(
" password=.* " ) );
239 safeName.replace( regexp, QStringLiteral(
" " ) );
241 else if ( aUri.contains( QLatin1String(
",password=" ) ) )
243 regexp.setPattern( QStringLiteral(
",password=.*," ) );
244 safeName.replace( regexp, QStringLiteral(
"," ) );
246 else if ( aUri.contains( QLatin1String(
"IDB:" ) ) )
248 regexp.setPattern( QStringLiteral(
" pass=.* " ) );
249 safeName.replace( regexp, QStringLiteral(
" " ) );
251 else if ( ( aUri.contains( QLatin1String(
"OCI:" ) ) )
252 || ( aUri.contains( QLatin1String(
"ODBC:" ) ) ) )
254 regexp.setPattern( QStringLiteral(
"/.*@" ) );
255 safeName.replace( regexp, QStringLiteral(
"/@" ) );
257 else if ( aUri.contains( QLatin1String(
"SDE:" ) ) )
259 QStringList strlist = aUri.split(
',' );
260 safeName = strlist[0] +
',' + strlist[1] +
',' + strlist[2] +
',' + strlist[3];
267 return mAuthConfigId;
337 return mGeometryColumn;
360 mUseEstimatedMetadata = flag;
365 return mUseEstimatedMetadata;
370 mSelectAtIdDisabledSet =
true;
371 mSelectAtIdDisabled = flag;
376 return mSelectAtIdDisabled;
394QString QgsDataSourceUri::escape(
const QString &val, QChar delim =
'\'' )
const
396 QString escaped = val;
398 escaped.replace(
'\\', QLatin1String(
"\\\\" ) );
399 escaped.replace( delim, QStringLiteral(
"\\%1" ).arg( delim ) );
414void QgsDataSourceUri::skipBlanks(
const QString &uri,
int &i )
417 while ( i <
uri.length() &&
uri[i].isSpace() )
421QString QgsDataSourceUri::getValue(
const QString &uri,
int &i )
423 skipBlanks(
uri, i );
427 if ( i <
uri.length() && (
uri[i] ==
'\'' ||
uri[i] ==
'"' ) )
429 const QChar delim =
uri[i];
436 if ( i ==
uri.length() )
438 QgsDebugMsg( QStringLiteral(
"unterminated quoted string in connection info string" ) );
442 if (
uri[i] ==
'\\' )
445 if ( i ==
uri.length() )
447 if (
uri[i] != delim &&
uri[i] !=
'\\' )
450 else if (
uri[i] == delim )
462 while ( i <
uri.length() )
464 if (
uri[i].isSpace() )
470 if (
uri[i] ==
'\\' )
473 if ( i ==
uri.length() )
475 if (
uri[i] !=
'\\' &&
uri[i] !=
'\'' )
483 skipBlanks(
uri, i );
490 QStringList connectionItems;
492 if ( !mDatabase.isEmpty() )
494 connectionItems <<
"dbname='" + escape( mDatabase ) +
'\'';
497 if ( !mService.isEmpty() )
499 connectionItems <<
"service='" + escape( mService ) +
'\'';
501 else if ( !mHost.isEmpty() )
503 connectionItems <<
"host=" + mHost;
506 if ( mService.isEmpty() )
508 if ( !mPort.isEmpty() )
509 connectionItems <<
"port=" + mPort;
512 if ( !mDriver.isEmpty() )
514 connectionItems <<
"driver='" + escape( mDriver ) +
'\'';
517 if ( !mUsername.isEmpty() )
519 connectionItems <<
"user='" + escape( mUsername ) +
'\'';
521 if ( !mPassword.isEmpty() )
523 connectionItems <<
"password='" + escape( mPassword ) +
'\'';
529 connectionItems << QStringLiteral(
"sslmode=" ) +
encodeSslMode( mSSLmode );
532 if ( !mAuthConfigId.isEmpty() )
534 if ( expandAuthConfig )
538 QgsDebugMsg( QStringLiteral(
"Data source URI FAILED to update via loading configuration ID '%1'" ).arg( mAuthConfigId ) );
543 connectionItems <<
"authcfg=" + mAuthConfigId;
547 return connectionItems.join( QLatin1Char(
' ' ) );
554 if ( !mKeyColumn.isEmpty() )
556 uri += QStringLiteral(
" key='%1'" ).arg( escape( mKeyColumn ) );
559 if ( mUseEstimatedMetadata )
561 uri += QLatin1String(
" estimatedmetadata=true" );
564 if ( !mSrid.isEmpty() )
566 uri += QStringLiteral(
" srid=%1" ).arg( mSrid );
571 uri += QLatin1String(
" type=" );
575 if ( mSelectAtIdDisabled )
577 uri += QLatin1String(
" selectatid=false" );
580 for (
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
582 if ( it.key().contains(
'=' ) || it.key().contains(
' ' ) )
584 QgsDebugMsg( QStringLiteral(
"invalid uri parameter %1 skipped" ).arg( it.key() ) );
588 uri +=
' ' + it.key() +
"='" + escape( it.value() ) +
'\'';
593 QString columnName( mGeometryColumn );
594 columnName.replace(
'\\', QLatin1String(
"\\\\" ) );
595 columnName.replace(
')', QLatin1String(
"\\)" ) );
597 if ( !mTable.isEmpty() )
599 uri += QStringLiteral(
" table=%1%2" )
601 mGeometryColumn.isEmpty() ? QString() : QStringLiteral(
" (%1)" ).arg( columnName ) );
603 else if ( !mSchema.isEmpty() )
605 uri += QStringLiteral(
" schema='%1'" ).arg( escape( mSchema ) );
608 if ( !mSql.isEmpty() )
610 uri += QStringLiteral(
" sql=" ) + mSql;
619 if (
string.isEmpty() )
620 return string.isNull() ? QByteArray() : QByteArray(
"" );
621 return string.toLatin1();
627 for (
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
629 url.addQueryItem( it.key(), it.value() );
632 if ( !mUsername.isEmpty() )
633 url.addQueryItem( QStringLiteral(
"username" ), mUsername );
635 if ( !mPassword.isEmpty() )
636 url.addQueryItem( QStringLiteral(
"password" ), mPassword );
638 if ( !mAuthConfigId.isEmpty() )
639 url.addQueryItem( QStringLiteral(
"authcfg" ), mAuthConfigId );
651 mAuthConfigId.clear();
654 url.setQuery( QString::fromLatin1(
uri ) );
655 const QUrlQuery query( url );
659 const auto constQueryItems = query.queryItems( QUrl::ComponentFormattingOption::FullyDecoded );
660 for (
const QPair<QString, QString> &item : constQueryItems )
664 if ( item.first == QLatin1String(
"username" ) )
665 mUsername = item.second;
666 else if ( item.first == QLatin1String(
"password" ) )
667 mPassword = item.second;
668 else if ( item.first == QLatin1String(
"authcfg" ) )
669 mAuthConfigId = item.second;
671 mParams.insert( item.first, item.second );
683 if ( !mSchema.isEmpty() )
684 return QStringLiteral(
"\"%1\".\"%2\"" )
685 .arg( escape( mSchema,
'"' ),
686 escape( mTable,
'"' ) );
688 return QStringLiteral(
"\"%1\"" )
689 .arg( escape( mTable,
'"' ) );
694 const QString &database,
695 const QString &username,
696 const QString &password,
698 const QString &authConfigId )
710 const QString &database,
711 const QString &username,
712 const QString &password,
714 const QString &authConfigId )
725 const QString &table,
726 const QString &geometryColumn,
728 const QString &keyColumn )
739 mAuthConfigId = authcfg;
769 if (
sslMode == QLatin1String(
"prefer" ) )
771 else if (
sslMode == QLatin1String(
"disable" ) )
773 else if (
sslMode == QLatin1String(
"allow" ) )
775 else if (
sslMode == QLatin1String(
"require" ) )
777 else if (
sslMode == QLatin1String(
"verify-ca" ) )
779 else if (
sslMode == QLatin1String(
"verify-full" ) )
789 case SslPrefer:
return QStringLiteral(
"prefer" );
790 case SslDisable:
return QStringLiteral(
"disable" );
791 case SslAllow:
return QStringLiteral(
"allow" );
792 case SslRequire:
return QStringLiteral(
"require" );
793 case SslVerifyCa:
return QStringLiteral(
"verify-ca" );
802 if ( key == QLatin1String(
"username" ) )
804 else if ( key == QLatin1String(
"password" ) )
806 else if ( key == QLatin1String(
"authcfg" ) )
807 mAuthConfigId = value;
811 mParams.insert( key, value );
817 for (
const QString &val : value )
825 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
830 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
835 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
837 mAuthConfigId.clear();
841 return mParams.remove( key );
847 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
849 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
851 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
852 return mAuthConfigId;
854 return mParams.value( key );
860 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
861 return QStringList() << mUsername;
862 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
863 return QStringList() << mPassword;
864 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
865 return QStringList() << mAuthConfigId;
867 return mParams.values( key );
873 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
875 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
877 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
880 return mParams.contains( key );
885 QSet<QString> paramKeys;
886 for (
const QString &key : mParams.keys() )
887 paramKeys.insert( key );
888 if ( !mHost.isEmpty() )
889 paramKeys.insert( QLatin1String(
"host" ) );
890 if ( !mPort.isEmpty() )
891 paramKeys.insert( QLatin1String(
"port" ) );
892 if ( !mDriver.isEmpty() )
893 paramKeys.insert( QLatin1String(
"driver" ) );
894 if ( !mService.isEmpty() )
895 paramKeys.insert( QLatin1String(
"service" ) );
896 if ( !mDatabase.isEmpty() )
897 paramKeys.insert( QLatin1String(
"dbname" ) );
898 if ( !mSchema.isEmpty() )
899 paramKeys.insert( QLatin1String(
"schema" ) );
900 if ( !mTable.isEmpty() )
901 paramKeys.insert( QLatin1String(
"table" ) );
903 if ( !mSql.isEmpty() )
904 paramKeys.insert( QLatin1String(
"sql" ) );
905 if ( !mAuthConfigId.isEmpty() )
906 paramKeys.insert( QLatin1String(
"authcfg" ) );
907 if ( !mUsername.isEmpty() )
908 paramKeys.insert( QLatin1String(
"username" ) );
909 if ( !mPassword.isEmpty() )
910 paramKeys.insert( QLatin1String(
"password" ) );
912 paramKeys.insert( QLatin1String(
"sslmode" ) );
913 if ( !mKeyColumn.isEmpty() )
914 paramKeys.insert( QLatin1String(
"key" ) );
915 if ( mUseEstimatedMetadata )
916 paramKeys.insert( QLatin1String(
"estimatedmetadata" ) );
917 if ( mSelectAtIdDisabledSet )
918 paramKeys.insert( QLatin1String(
"selectatid" ) );
920 paramKeys.insert( QLatin1String(
"type" ) );
921 if ( !mSrid.isEmpty() )
922 paramKeys.insert( QLatin1String(
"srid" ) );
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.
QgsWkbTypes::Type wkbType() const
Returns the WKB type associated with the URI.
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.
void setWkbType(QgsWkbTypes::Type type)
Sets the WKB type associated with 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.
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 Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
Type
The WKB type describes the number of dimensions a geometry has.
static QString displayString(Type type)
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)