25 #include <QStringList> 38 while ( i < uri.length() )
44 QgsDebugMsg( QStringLiteral(
"parameter name expected before =" ) );
51 while ( i < uri.length() && uri[i] !=
'=' && !uri[i].isSpace() )
54 QString pname = uri.mid( start, i - start );
58 if ( i == uri.length() || uri[i] !=
'=' )
60 QgsDebugMsg( QStringLiteral(
"= expected after parameter name, skipping text '%1'" ).arg( pname ) );
66 if ( pname == QLatin1String(
"sql" ) )
75 QString pval = getValue( uri, i );
77 if ( pname == QLatin1String(
"table" ) )
84 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(
"key" ) )
124 else if ( pname == QLatin1String(
"estimatedmetadata" ) )
126 mUseEstimatedMetadata = pval == QLatin1String(
"true" );
128 else if ( pname == QLatin1String(
"srid" ) )
132 else if ( pname == QLatin1String(
"type" ) )
136 else if ( pname == QLatin1String(
"selectatid" ) )
138 mSelectAtIdDisabled = pval == QLatin1String(
"false" );
140 else if ( pname == QLatin1String(
"service" ) )
144 else if ( pname == QLatin1String(
"authcfg" ) )
146 mAuthConfigId = pval;
148 else if ( pname == QLatin1String(
"user" ) || pname == QLatin1String(
"username" ) )
152 else if ( pname == QLatin1String(
"password" ) )
156 else if ( pname == QLatin1String(
"connect_timeout" ) )
158 QgsDebugMsg( QStringLiteral(
"connection timeout ignored" ) );
160 else if ( pname == QLatin1String(
"dbname" ) )
164 else if ( pname == QLatin1String(
"host" ) )
168 else if ( pname == QLatin1String(
"hostaddr" ) )
170 QgsDebugMsg( QStringLiteral(
"database host ip address ignored" ) );
172 else if ( pname == QLatin1String(
"port" ) )
176 else if ( pname == QLatin1String(
"driver" ) )
180 else if ( pname == QLatin1String(
"tty" ) )
182 QgsDebugMsg( QStringLiteral(
"backend debug tty ignored" ) );
184 else if ( pname == QLatin1String(
"options" ) )
186 QgsDebugMsg( QStringLiteral(
"backend debug options ignored" ) );
188 else if ( pname == QLatin1String(
"sslmode" ) )
192 else if ( pname == QLatin1String(
"requiressl" ) )
194 if ( pval == QLatin1String(
"0" ) )
199 else if ( pname == QLatin1String(
"krbsrvname" ) )
201 QgsDebugMsg( QStringLiteral(
"kerberos server name ignored" ) );
203 else if ( pname == QLatin1String(
"gsslib" ) )
209 QgsDebugMsgLevel(
"parameter \"" + pname +
"\":\"" + pval +
"\" added", 4 );
219 regexp.setMinimal(
true );
220 QString safeName( aUri );
221 if ( aUri.contains( QLatin1String(
" password=" ) ) )
223 regexp.setPattern( QStringLiteral(
" password=.* " ) );
224 safeName.replace( regexp, QStringLiteral(
" " ) );
226 else if ( aUri.contains( QLatin1String(
",password=" ) ) )
228 regexp.setPattern( QStringLiteral(
",password=.*," ) );
229 safeName.replace( regexp, QStringLiteral(
"," ) );
231 else if ( aUri.contains( QLatin1String(
"IDB:" ) ) )
233 regexp.setPattern( QStringLiteral(
" pass=.* " ) );
234 safeName.replace( regexp, QStringLiteral(
" " ) );
236 else if ( ( aUri.contains( QLatin1String(
"OCI:" ) ) )
237 || ( aUri.contains( QLatin1String(
"ODBC:" ) ) ) )
239 regexp.setPattern( QStringLiteral(
"/.*@" ) );
240 safeName.replace( regexp, QStringLiteral(
"/@" ) );
242 else if ( aUri.contains( QLatin1String(
"SDE:" ) ) )
244 QStringList strlist = aUri.split(
',' );
245 safeName = strlist[0] +
',' + strlist[1] +
',' + strlist[2] +
',' + strlist[3];
252 return mAuthConfigId;
322 return mGeometryColumn;
345 mUseEstimatedMetadata = flag;
350 return mUseEstimatedMetadata;
355 mSelectAtIdDisabled = flag;
360 return mSelectAtIdDisabled;
378 QString QgsDataSourceUri::escape(
const QString &val, QChar delim =
'\'' )
const 380 QString escaped = val;
382 escaped.replace(
'\\', QLatin1String(
"\\\\" ) );
383 escaped.replace( delim, QStringLiteral(
"\\%1" ).arg( delim ) );
398 void QgsDataSourceUri::skipBlanks(
const QString &
uri,
int &i )
401 while ( i <
uri.length() &&
uri[i].isSpace() )
405 QString QgsDataSourceUri::getValue(
const QString &
uri,
int &i )
407 skipBlanks(
uri, i );
411 if ( i <
uri.length() && (
uri[i] ==
'\'' ||
uri[i] ==
'"' ) )
413 QChar delim =
uri[i];
420 if ( i ==
uri.length() )
422 QgsDebugMsg( QStringLiteral(
"unterminated quoted string in connection info string" ) );
426 if (
uri[i] ==
'\\' )
429 if ( i ==
uri.length() )
431 if (
uri[i] != delim &&
uri[i] !=
'\\' )
434 else if (
uri[i] == delim )
446 while ( i <
uri.length() )
448 if (
uri[i].isSpace() )
454 if (
uri[i] ==
'\\' )
457 if ( i ==
uri.length() )
459 if (
uri[i] !=
'\\' &&
uri[i] !=
'\'' )
467 skipBlanks(
uri, i );
474 QStringList connectionItems;
476 if ( !mDatabase.isEmpty() )
478 connectionItems <<
"dbname='" + escape( mDatabase ) +
'\'';
481 if ( !mService.isEmpty() )
483 connectionItems <<
"service='" + escape( mService ) +
'\'';
485 else if ( !mHost.isEmpty() )
487 connectionItems <<
"host=" + mHost;
490 if ( mService.isEmpty() )
492 if ( !mPort.isEmpty() )
493 connectionItems <<
"port=" + mPort;
496 if ( !mDriver.isEmpty() )
498 connectionItems <<
"driver='" + escape( mDriver ) +
'\'';
501 if ( !mUsername.isEmpty() )
503 connectionItems <<
"user='" + escape( mUsername ) +
'\'';
505 if ( !mPassword.isEmpty() )
507 connectionItems <<
"password='" + escape( mPassword ) +
'\'';
513 connectionItems << QStringLiteral(
"sslmode=" ) +
encodeSslMode( mSSLmode );
516 if ( !mAuthConfigId.isEmpty() )
518 if ( expandAuthConfig )
522 QgsDebugMsg( QStringLiteral(
"Data source URI FAILED to update via loading configuration ID '%1'" ).arg( mAuthConfigId ) );
527 connectionItems <<
"authcfg=" + mAuthConfigId;
531 return connectionItems.join( QStringLiteral(
" " ) );
538 if ( !mKeyColumn.isEmpty() )
540 uri += QStringLiteral(
" key='%1'" ).arg( escape( mKeyColumn ) );
543 if ( mUseEstimatedMetadata )
545 uri += QStringLiteral(
" estimatedmetadata=true" );
548 if ( !mSrid.isEmpty() )
550 uri += QStringLiteral(
" srid=%1" ).arg( mSrid );
555 uri += QLatin1String(
" type=" );
559 if ( mSelectAtIdDisabled )
561 uri += QStringLiteral(
" selectatid=false" );
564 for ( QMap<QString, QString>::const_iterator it = mParams.begin(); it != mParams.end(); ++it )
566 if ( it.key().contains(
'=' ) || it.key().contains(
' ' ) )
568 QgsDebugMsg( QStringLiteral(
"invalid uri parameter %1 skipped" ).arg( it.key() ) );
572 uri +=
' ' + it.key() +
"='" + escape( it.value() ) +
'\'';
575 QString columnName( mGeometryColumn );
576 columnName.replace(
'\\', QLatin1String(
"\\\\" ) );
577 columnName.replace(
')', QLatin1String(
"\\)" ) );
579 uri += QStringLiteral(
" table=%1%2 sql=%3" )
581 mGeometryColumn.isNull() ? QString() : QStringLiteral(
" (%1)" ).arg( columnName ),
590 for (
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
592 url.addQueryItem( it.key(), it.value() );
594 return url.encodedQuery();
601 url.setEncodedQuery( uri );
603 const auto constQueryItems = url.queryItems();
604 for (
const QPair<QString, QString> &item : constQueryItems )
606 mParams.insertMulti( item.first, item.second );
617 if ( !mSchema.isEmpty() )
618 return QStringLiteral(
"\"%1\".\"%2\"" )
619 .arg( escape( mSchema,
'"' ),
620 escape( mTable,
'"' ) );
622 return QStringLiteral(
"\"%1\"" )
623 .arg( escape( mTable,
'"' ) );
659 const QString &
table,
673 mAuthConfigId = authcfg;
703 if ( sslMode == QLatin1String(
"prefer" ) )
705 else if ( sslMode == QLatin1String(
"disable" ) )
707 else if ( sslMode == QLatin1String(
"allow" ) )
709 else if ( sslMode == QLatin1String(
"require" ) )
711 else if ( sslMode == QLatin1String(
"verify-ca" ) )
713 else if ( sslMode == QLatin1String(
"verify-full" ) )
723 case SslPrefer:
return QStringLiteral(
"prefer" );
724 case SslDisable:
return QStringLiteral(
"disable" );
725 case SslAllow:
return QStringLiteral(
"allow" );
726 case SslRequire:
return QStringLiteral(
"require" );
727 case SslVerifyCa:
return QStringLiteral(
"verify-ca" );
736 mParams.insertMulti( key, value );
741 const auto constValue = value;
742 for (
const QString &val : constValue )
744 mParams.insertMulti( key, val );
750 return mParams.remove( key );
755 return mParams.value( key );
760 return mParams.values( key );
765 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.
SslMode sslMode() const
Returns the SSL mode associated with the URI.
static Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
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.
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.