25 #include <QStringList> 39 while ( i < uri.length() )
45 QgsDebugMsg( QStringLiteral(
"parameter name expected before =" ) );
52 while ( i < uri.length() && uri[i] !=
'=' && !uri[i].isSpace() )
55 QString pname = uri.mid( start, i - start );
59 if ( i == uri.length() || uri[i] !=
'=' )
61 QgsDebugMsg( QStringLiteral(
"= expected after parameter name, skipping text '%1'" ).arg( pname ) );
67 if ( pname == QLatin1String(
"sql" ) )
76 QString pval = getValue( uri, i );
78 if ( pname == QLatin1String(
"table" ) )
85 mTable = getValue( 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" ) )
162 QgsDebugMsg( QStringLiteral(
"connection timeout ignored" ) );
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 );
223 regexp.setMinimal(
true );
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 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( QStringLiteral(
" " ) );
542 if ( !mKeyColumn.isEmpty() )
544 uri += QStringLiteral(
" key='%1'" ).arg( escape( mKeyColumn ) );
547 if ( mUseEstimatedMetadata )
549 uri += QStringLiteral(
" estimatedmetadata=true" );
552 if ( !mSrid.isEmpty() )
554 uri += QStringLiteral(
" srid=%1" ).arg( mSrid );
559 uri += QLatin1String(
" type=" );
563 if ( mSelectAtIdDisabled )
565 uri += QStringLiteral(
" selectatid=false" );
568 for ( QMap<QString, QString>::const_iterator it = mParams.begin(); it != mParams.end(); ++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.isNull() ? QString() : QStringLiteral(
" (%1)" ).arg( columnName ) );
590 if ( !mSql.isEmpty() )
592 uri += QStringLiteral(
" sql=" ) + mSql;
601 if (
string.isEmpty() )
602 return string.isNull() ? QByteArray() : QByteArray(
"" );
603 return string.toLatin1();
609 for (
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
611 url.addQueryItem( it.key(), it.value() );
614 if ( !mUsername.isEmpty() )
615 url.addQueryItem( QStringLiteral(
"username" ), mUsername );
617 if ( !mPassword.isEmpty() )
618 url.addQueryItem( QStringLiteral(
"password" ), mPassword );
620 if ( !mAuthConfigId.isEmpty() )
621 url.addQueryItem( QStringLiteral(
"authcfg" ), mAuthConfigId );
631 mAuthConfigId.clear();
634 url.setQuery( QString::fromLatin1( uri ) );
635 const QUrlQuery query( url );
637 const auto constQueryItems = query.queryItems();
638 for (
const QPair<QString, QString> &item : constQueryItems )
640 if ( item.first == QLatin1String(
"username" ) )
641 mUsername = item.second;
642 else if ( item.first == QLatin1String(
"password" ) )
643 mPassword = item.second;
644 else if ( item.first == QLatin1String(
"authcfg" ) )
645 mAuthConfigId = item.second;
647 mParams.insertMulti( item.first, item.second );
658 if ( !mSchema.isEmpty() )
659 return QStringLiteral(
"\"%1\".\"%2\"" )
660 .arg( escape( mSchema,
'"' ),
661 escape( mTable,
'"' ) );
663 return QStringLiteral(
"\"%1\"" )
664 .arg( escape( mTable,
'"' ) );
700 const QString &
table,
714 mAuthConfigId = authcfg;
744 if ( sslMode == QLatin1String(
"prefer" ) )
746 else if ( sslMode == QLatin1String(
"disable" ) )
748 else if ( sslMode == QLatin1String(
"allow" ) )
750 else if ( sslMode == QLatin1String(
"require" ) )
752 else if ( sslMode == QLatin1String(
"verify-ca" ) )
754 else if ( sslMode == QLatin1String(
"verify-full" ) )
764 case SslPrefer:
return QStringLiteral(
"prefer" );
765 case SslDisable:
return QStringLiteral(
"disable" );
766 case SslAllow:
return QStringLiteral(
"allow" );
767 case SslRequire:
return QStringLiteral(
"require" );
768 case SslVerifyCa:
return QStringLiteral(
"verify-ca" );
777 if ( key == QLatin1String(
"username" ) )
779 else if ( key == QLatin1String(
"password" ) )
781 else if ( key == QLatin1String(
"authcfg" ) )
782 mAuthConfigId = value;
786 mParams.insertMulti( key, value );
792 for (
const QString &val : value )
800 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
805 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
810 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
812 mAuthConfigId.clear();
816 return mParams.remove( key );
822 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
824 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
826 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
827 return mAuthConfigId;
829 return mParams.value( key );
835 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
836 return QStringList() << mUsername;
837 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
838 return QStringList() << mPassword;
839 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
840 return QStringList() << mAuthConfigId;
842 return mParams.values( key );
848 if ( key == QLatin1String(
"username" ) && !mUsername.isEmpty() )
850 else if ( key == QLatin1String(
"password" ) && !mPassword.isEmpty() )
852 else if ( key == QLatin1String(
"authcfg" ) && !mAuthConfigId.isEmpty() )
855 return mParams.contains( key );
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
QgsWkbTypes::Type wkbType() const
Returns the WKB type associated with the URI.
QString table() const
Returns the table name stored in the URI.
bool useEstimatedMetadata() const
Returns true if estimated metadata should be used for the connection.
void setUsername(const QString &username)
Sets the username for the URI.
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
void clearSchema()
Clears the schema stored in the URI.
static SslMode decodeSslMode(const QString &sslMode)
Decodes SSL mode string into enum value.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
static Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
SslMode sslMode() const
Returns the SSL mode associated with the URI.
QString keyColumn() const
Returns the name of the (primary) key column for the referenced table.
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.
QString connectionInfo(bool expandAuthConfig=true) const
Returns the connection part of the URI.
QString username() const
Returns the username stored in the URI.
SslMode
Available SSQL connection modes.
void setGeometryColumn(const QString &geometryColumn)
Sets geometry column name to geometryColumn.
QString host() const
Returns the host name stored in the URI.
Type
The WKB type describes the number of dimensions a geometry has.
QStringList params(const QString &key) const
Returns multiple generic parameter values corresponding to the specified key.
void setDriver(const QString &driver)
Sets the driver name stored in the URI.
int removeParam(const QString &key)
Removes a generic parameter by key.
#define QgsDebugMsgLevel(str, level)
QString geometryColumn() const
Returns the name of the geometry column stored in the URI, if set.
void setKeyColumn(const QString &column)
Sets the name of the (primary) key column.
bool selectAtIdDisabled() const
Returns whether the selection by feature ID is disabled.
void setTable(const QString &table)
Sets table to table.
bool updateDataSourceUriItems(QStringList &connectionItems, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QgsDataSourceUri with an authentication config.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
QString quotedTablename() const
Returns the URI's table name, escaped and quoted.
static QString encodeSslMode(SslMode sslMode)
Encodes SSL mode enum value into a string.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
QString port() const
Returns the port stored in the URI.
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
void setSrid(const QString &srid)
Sets the spatial reference ID associated with the URI.
void setWkbType(QgsWkbTypes::Type type)
Sets the WKB type associated with the URI.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
void disableSelectAtId(bool flag)
Set to true to disable selection by feature ID.
QString driver() const
Returns the driver name stored in the URI.
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.
QByteArray toLatin1_helper(const QString &string)
void setSql(const QString &sql)
Sets the SQL query for the URI.
void setPassword(const QString &password)
Sets the password for the URI.
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
QString sql() const
Returns the SQL query stored in the URI, if set.
QString service() const
Returns the service name associated with the URI.
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
QString password() const
Returns the password stored in the URI.
void setSchema(const QString &schema)
Sets the scheme for the URI.
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 database() const
Returns the database name stored in the URI.
void setDatabase(const QString &database)
Sets the URI database name.
QString srid() const
Returns the spatial reference ID associated with the URI.
static QString removePassword(const QString &aUri)
Removes the password element from a URI.