25 #include <QStringList> 
   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         mSelectAtIdDisabled = pval == QLatin1String( 
"false" );
 
  144       else if ( pname == QLatin1String( 
"service" ) )
 
  148       else if ( pname == QLatin1String( 
"authcfg" ) )
 
  150         mAuthConfigId = pval;
 
  152       else if ( pname == QLatin1String( 
"user" ) || pname == QLatin1String( 
"username" ) ) 
 
  156       else if ( pname == QLatin1String( 
"password" ) )
 
  160       else if ( pname == QLatin1String( 
"connect_timeout" ) )
 
  164       else if ( pname == QLatin1String( 
"dbname" ) )
 
  168       else if ( pname == QLatin1String( 
"host" ) )
 
  172       else if ( pname == QLatin1String( 
"hostaddr" ) )
 
  174         QgsDebugMsg( QStringLiteral( 
"database host ip address ignored" ) );
 
  176       else if ( pname == QLatin1String( 
"port" ) )
 
  180       else if ( pname == QLatin1String( 
"driver" ) )
 
  184       else if ( pname == QLatin1String( 
"tty" ) )
 
  186         QgsDebugMsg( QStringLiteral( 
"backend debug tty ignored" ) );
 
  188       else if ( pname == QLatin1String( 
"options" ) )
 
  190         QgsDebugMsg( QStringLiteral( 
"backend debug options ignored" ) );
 
  192       else if ( pname == QLatin1String( 
"sslmode" ) )
 
  196       else if ( pname == QLatin1String( 
"requiressl" ) )
 
  198         if ( pval == QLatin1String( 
"0" ) )
 
  203       else if ( pname == QLatin1String( 
"krbsrvname" ) )
 
  205         QgsDebugMsg( QStringLiteral( 
"kerberos server name ignored" ) );
 
  207       else if ( pname == QLatin1String( 
"gsslib" ) )
 
  213         QgsDebugMsgLevel( 
"parameter \"" + pname + 
"\":\"" + pval + 
"\" added", 4 );
 
  222   QRegularExpression regexp;
 
  223   regexp.setPatternOptions( QRegularExpression::InvertedGreedinessOption );
 
  224   QString safeName( aUri );
 
  225   if ( aUri.contains( QLatin1String( 
" password=" ) ) )
 
  227     regexp.setPattern( QStringLiteral( 
" password=.* " ) );
 
  228     safeName.replace( regexp, QStringLiteral( 
" " ) );
 
  230   else if ( aUri.contains( QLatin1String( 
",password=" ) ) )
 
  232     regexp.setPattern( QStringLiteral( 
",password=.*," ) );
 
  233     safeName.replace( regexp, QStringLiteral( 
"," ) );
 
  235   else if ( aUri.contains( QLatin1String( 
"IDB:" ) ) )
 
  237     regexp.setPattern( QStringLiteral( 
" pass=.* " ) );
 
  238     safeName.replace( regexp, QStringLiteral( 
" " ) );
 
  240   else if ( ( aUri.contains( QLatin1String( 
"OCI:" ) ) )
 
  241             || ( aUri.contains( QLatin1String( 
"ODBC:" ) ) ) )
 
  243     regexp.setPattern( QStringLiteral( 
"/.*@" ) );
 
  244     safeName.replace( regexp, QStringLiteral( 
"/@" ) );
 
  246   else if ( aUri.contains( QLatin1String( 
"SDE:" ) ) )
 
  248     QStringList strlist = aUri.split( 
',' );
 
  249     safeName = strlist[0] + 
',' + strlist[1] + 
',' + strlist[2] + 
',' + strlist[3];
 
  256   return mAuthConfigId;
 
  326   return mGeometryColumn;
 
  349   mUseEstimatedMetadata = flag;
 
  354   return mUseEstimatedMetadata;
 
  359   mSelectAtIdDisabled = flag;
 
  364   return mSelectAtIdDisabled;
 
  382 QString QgsDataSourceUri::escape( 
const QString &val, QChar delim = 
'\'' )
 const 
  384   QString escaped = val;
 
  386   escaped.replace( 
'\\', QLatin1String( 
"\\\\" ) );
 
  387   escaped.replace( delim, QStringLiteral( 
"\\%1" ).arg( delim ) );
 
  402 void QgsDataSourceUri::skipBlanks( 
const QString &uri, 
int &i )
 
  405   while ( i < 
uri.length() && 
uri[i].isSpace() )
 
  409 QString QgsDataSourceUri::getValue( 
const QString &uri, 
int &i )
 
  411   skipBlanks( 
uri, i );
 
  415   if ( i < 
uri.length() && ( 
uri[i] == 
'\'' || 
uri[i] == 
'"' ) )
 
  417     const QChar delim = 
uri[i];
 
  424       if ( i == 
uri.length() )
 
  426         QgsDebugMsg( QStringLiteral( 
"unterminated quoted string in connection info string" ) );
 
  430       if ( 
uri[i] == 
'\\' )
 
  433         if ( i == 
uri.length() )
 
  435         if ( 
uri[i] != delim && 
uri[i] != 
'\\' )
 
  438       else if ( 
uri[i] == delim )
 
  450     while ( i < 
uri.length() )
 
  452       if ( 
uri[i].isSpace() )
 
  458       if ( 
uri[i] == 
'\\' )
 
  461         if ( i == 
uri.length() )
 
  463         if ( 
uri[i] != 
'\\' && 
uri[i] != 
'\'' )
 
  471   skipBlanks( 
uri, i );
 
  478   QStringList connectionItems;
 
  480   if ( !mDatabase.isEmpty() )
 
  482     connectionItems << 
"dbname='" + escape( mDatabase ) + 
'\'';
 
  485   if ( !mService.isEmpty() )
 
  487     connectionItems << 
"service='" + escape( mService ) + 
'\'';
 
  489   else if ( !mHost.isEmpty() )
 
  491     connectionItems << 
"host=" + mHost;
 
  494   if ( mService.isEmpty() )
 
  496     if ( !mPort.isEmpty() )
 
  497       connectionItems << 
"port=" + mPort;
 
  500   if ( !mDriver.isEmpty() )
 
  502     connectionItems << 
"driver='" + escape( mDriver ) + 
'\'';
 
  505   if ( !mUsername.isEmpty() )
 
  507     connectionItems << 
"user='" + escape( mUsername ) + 
'\'';
 
  509     if ( !mPassword.isEmpty() )
 
  511       connectionItems << 
"password='" + escape( mPassword ) + 
'\'';
 
  517     connectionItems << QStringLiteral( 
"sslmode=" ) + 
encodeSslMode( mSSLmode );
 
  520   if ( !mAuthConfigId.isEmpty() )
 
  522     if ( expandAuthConfig )
 
  526         QgsDebugMsg( QStringLiteral( 
"Data source URI FAILED to update via loading configuration ID '%1'" ).arg( mAuthConfigId ) );
 
  531       connectionItems << 
"authcfg=" + mAuthConfigId;
 
  535   return connectionItems.join( QLatin1Char( 
' ' ) );
 
  542   if ( !mKeyColumn.isEmpty() )
 
  544     uri += QStringLiteral( 
" key='%1'" ).arg( escape( mKeyColumn ) );
 
  547   if ( mUseEstimatedMetadata )
 
  549     uri += QLatin1String( 
" estimatedmetadata=true" );
 
  552   if ( !mSrid.isEmpty() )
 
  554     uri += QStringLiteral( 
" srid=%1" ).arg( mSrid );
 
  559     uri += QLatin1String( 
" type=" );
 
  563   if ( mSelectAtIdDisabled )
 
  565     uri += QLatin1String( 
" selectatid=false" );
 
  568   for ( 
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
 
  570     if ( it.key().contains( 
'=' ) || it.key().contains( 
' ' ) )
 
  572       QgsDebugMsg( QStringLiteral( 
"invalid uri parameter %1 skipped" ).arg( it.key() ) );
 
  576     uri += 
' ' + it.key() + 
"='" + escape( it.value() ) + 
'\'';
 
  579   QString columnName( mGeometryColumn );
 
  580   columnName.replace( 
'\\', QLatin1String( 
"\\\\" ) );
 
  581   columnName.replace( 
')', QLatin1String( 
"\\)" ) );
 
  583   if ( !mTable.isEmpty() )
 
  585     uri += QStringLiteral( 
" table=%1%2" )
 
  587                  mGeometryColumn.isEmpty() ? QString() : QStringLiteral( 
" (%1)" ).arg( columnName ) );
 
  589   else if ( !mSchema.isEmpty() )
 
  591     uri += QStringLiteral( 
" schema='%1'" ).arg( escape( mSchema ) );
 
  594   if ( !mSql.isEmpty() )
 
  596     uri += QStringLiteral( 
" sql=" ) + mSql;
 
  605   if ( 
string.isEmpty() )
 
  606     return string.isNull() ? QByteArray() : QByteArray( 
"" );
 
  607   return string.toLatin1();
 
  613   for ( 
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
 
  615     url.addQueryItem( it.key(), it.value() );
 
  618   if ( !mUsername.isEmpty() )
 
  619     url.addQueryItem( QStringLiteral( 
"username" ), mUsername );
 
  621   if ( !mPassword.isEmpty() )
 
  622     url.addQueryItem( QStringLiteral( 
"password" ), mPassword );
 
  624   if ( !mAuthConfigId.isEmpty() )
 
  625     url.addQueryItem( QStringLiteral( 
"authcfg" ), mAuthConfigId );
 
  635   mAuthConfigId.clear();
 
  638   url.setQuery( QString::fromLatin1( 
uri ) );
 
  639   const QUrlQuery query( url );
 
  641   const auto constQueryItems = query.queryItems( QUrl::ComponentFormattingOption::FullyDecoded );
 
  642   for ( 
const QPair<QString, QString> &item : constQueryItems )
 
  644     if ( item.first == QLatin1String( 
"username" ) )
 
  645       mUsername = item.second;
 
  646     else if ( item.first == QLatin1String( 
"password" ) )
 
  647       mPassword = item.second;
 
  648     else if ( item.first == QLatin1String( 
"authcfg" ) )
 
  649       mAuthConfigId = item.second;
 
  651       mParams.insert( item.first, item.second );
 
  662   if ( !mSchema.isEmpty() )
 
  663     return QStringLiteral( 
"\"%1\".\"%2\"" )
 
  664            .arg( escape( mSchema, 
'"' ),
 
  665                  escape( mTable, 
'"' ) );
 
  667     return QStringLiteral( 
"\"%1\"" )
 
  668            .arg( escape( mTable, 
'"' ) );
 
  673                                       const QString &database,
 
  674                                       const QString &username,
 
  675                                       const QString &password,
 
  677                                       const QString &authConfigId )
 
  689                                       const QString &database,
 
  690                                       const QString &username,
 
  691                                       const QString &password,
 
  693                                       const QString &authConfigId )
 
  704                                       const QString &table,
 
  705                                       const QString &geometryColumn,
 
  707                                       const QString &keyColumn )
 
  718   mAuthConfigId = authcfg;
 
  748   if ( 
sslMode == QLatin1String( 
"prefer" ) )
 
  750   else if ( 
sslMode == QLatin1String( 
"disable" ) )
 
  752   else if ( 
sslMode == QLatin1String( 
"allow" ) )
 
  754   else if ( 
sslMode == QLatin1String( 
"require" ) )
 
  756   else if ( 
sslMode == QLatin1String( 
"verify-ca" ) )
 
  758   else if ( 
sslMode == QLatin1String( 
"verify-full" ) )
 
  768     case SslPrefer: 
return QStringLiteral( 
"prefer" );
 
  769     case SslDisable: 
return QStringLiteral( 
"disable" );
 
  770     case SslAllow: 
return QStringLiteral( 
"allow" );
 
  771     case SslRequire: 
return QStringLiteral( 
"require" );
 
  772     case SslVerifyCa: 
return QStringLiteral( 
"verify-ca" );
 
  781   if ( key == QLatin1String( 
"username" ) )
 
  783   else if ( key == QLatin1String( 
"password" ) )
 
  785   else if ( key == QLatin1String( 
"authcfg" ) )
 
  786     mAuthConfigId = value;
 
  790     mParams.insert( key, value );
 
  796   for ( 
const QString &val : value )
 
  804   if ( key == QLatin1String( 
"username" ) && !mUsername.isEmpty() )
 
  809   else if ( key == QLatin1String( 
"password" ) && !mPassword.isEmpty() )
 
  814   else if ( key == QLatin1String( 
"authcfg" ) && !mAuthConfigId.isEmpty() )
 
  816     mAuthConfigId.clear();
 
  820   return mParams.remove( key );
 
  826   if ( key == QLatin1String( 
"username" ) && !mUsername.isEmpty() )
 
  828   else if ( key == QLatin1String( 
"password" ) && !mPassword.isEmpty() )
 
  830   else if ( key == QLatin1String( 
"authcfg" ) && !mAuthConfigId.isEmpty() )
 
  831     return mAuthConfigId;
 
  833   return mParams.value( key );
 
  839   if ( key == QLatin1String( 
"username" ) && !mUsername.isEmpty() )
 
  840     return QStringList() << mUsername;
 
  841   else if ( key == QLatin1String( 
"password" ) && !mPassword.isEmpty() )
 
  842     return QStringList() << mPassword;
 
  843   else if ( key == QLatin1String( 
"authcfg" ) && !mAuthConfigId.isEmpty() )
 
  844     return QStringList() << mAuthConfigId;
 
  846   return mParams.values( key );
 
  852   if ( key == QLatin1String( 
"username" ) && !mUsername.isEmpty() )
 
  854   else if ( key == QLatin1String( 
"password" ) && !mPassword.isEmpty() )
 
  856   else if ( key == QLatin1String( 
"authcfg" ) && !mAuthConfigId.isEmpty() )
 
  859   return mParams.contains( key );
 
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.
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) 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)