25 #include <QStringList> 37 while ( i < uri.length() )
43 QgsDebugMsg( QStringLiteral(
"parameter name expected before =" ) );
50 while ( i < uri.length() && uri[i] !=
'=' && !uri[i].isSpace() )
53 QString pname = uri.mid( start, i - start );
57 if ( i == uri.length() || uri[i] !=
'=' )
59 QgsDebugMsg( QStringLiteral(
"= expected after parameter name, skipping text '%1'" ).arg( pname ) );
65 if ( pname == QLatin1String(
"sql" ) )
74 QString pval = getValue( uri, i );
76 if ( pname == QLatin1String(
"table" ) )
83 mTable = getValue( uri, i );
96 while ( i < uri.length() && uri[i] !=
')' )
103 if ( i == uri.length() )
105 QgsDebugMsg( QStringLiteral(
"closing parenthesis missing" ) );
108 mGeometryColumn = uri.mid( start, i - start );
109 mGeometryColumn.replace( QLatin1String(
"\\)" ), QLatin1String(
")" ) );
110 mGeometryColumn.replace( QLatin1String(
"\\\\" ), QLatin1String(
"\\" ) );
116 mGeometryColumn = QString();
119 else if ( pname == QLatin1String(
"key" ) )
123 else if ( pname == QLatin1String(
"estimatedmetadata" ) )
125 mUseEstimatedMetadata = pval == QLatin1String(
"true" );
127 else if ( pname == QLatin1String(
"srid" ) )
131 else if ( pname == QLatin1String(
"type" ) )
135 else if ( pname == QLatin1String(
"selectatid" ) )
137 mSelectAtIdDisabled = pval == QLatin1String(
"false" );
139 else if ( pname == QLatin1String(
"service" ) )
143 else if ( pname == QLatin1String(
"authcfg" ) )
145 mAuthConfigId = pval;
147 else if ( pname == QLatin1String(
"user" ) || pname == QLatin1String(
"username" ) )
151 else if ( pname == QLatin1String(
"password" ) )
155 else if ( pname == QLatin1String(
"connect_timeout" ) )
157 QgsDebugMsg( QStringLiteral(
"connection timeout ignored" ) );
159 else if ( pname == QLatin1String(
"dbname" ) )
163 else if ( pname == QLatin1String(
"host" ) )
167 else if ( pname == QLatin1String(
"hostaddr" ) )
169 QgsDebugMsg( QStringLiteral(
"database host ip address ignored" ) );
171 else if ( pname == QLatin1String(
"port" ) )
175 else if ( pname == QLatin1String(
"driver" ) )
179 else if ( pname == QLatin1String(
"tty" ) )
181 QgsDebugMsg( QStringLiteral(
"backend debug tty ignored" ) );
183 else if ( pname == QLatin1String(
"options" ) )
185 QgsDebugMsg( QStringLiteral(
"backend debug options ignored" ) );
187 else if ( pname == QLatin1String(
"sslmode" ) )
191 else if ( pname == QLatin1String(
"requiressl" ) )
193 if ( pval == QLatin1String(
"0" ) )
198 else if ( pname == QLatin1String(
"krbsrvname" ) )
200 QgsDebugMsg( QStringLiteral(
"kerberos server name ignored" ) );
202 else if ( pname == QLatin1String(
"gsslib" ) )
208 QgsDebugMsgLevel(
"parameter \"" + pname +
"\":\"" + pval +
"\" added", 4 );
218 regexp.setMinimal(
true );
219 QString safeName( aUri );
220 if ( aUri.contains( QLatin1String(
" password=" ) ) )
222 regexp.setPattern( QStringLiteral(
" password=.* " ) );
223 safeName.replace( regexp, QStringLiteral(
" " ) );
225 else if ( aUri.contains( QLatin1String(
",password=" ) ) )
227 regexp.setPattern( QStringLiteral(
",password=.*," ) );
228 safeName.replace( regexp, QStringLiteral(
"," ) );
230 else if ( aUri.contains( QLatin1String(
"IDB:" ) ) )
232 regexp.setPattern( QStringLiteral(
" pass=.* " ) );
233 safeName.replace( regexp, QStringLiteral(
" " ) );
235 else if ( ( aUri.contains( QLatin1String(
"OCI:" ) ) )
236 || ( aUri.contains( QLatin1String(
"ODBC:" ) ) ) )
238 regexp.setPattern( QStringLiteral(
"/.*@" ) );
239 safeName.replace( regexp, QStringLiteral(
"/@" ) );
241 else if ( aUri.contains( QLatin1String(
"SDE:" ) ) )
243 QStringList strlist = aUri.split(
',' );
244 safeName = strlist[0] +
',' + strlist[1] +
',' + strlist[2] +
',' + strlist[3];
251 return mAuthConfigId;
321 return mGeometryColumn;
344 mUseEstimatedMetadata = flag;
349 return mUseEstimatedMetadata;
354 mSelectAtIdDisabled = flag;
359 return mSelectAtIdDisabled;
377 QString QgsDataSourceUri::escape(
const QString &val, QChar delim =
'\'' )
const 379 QString escaped = val;
381 escaped.replace(
'\\', QLatin1String(
"\\\\" ) );
382 escaped.replace( delim, QStringLiteral(
"\\%1" ).arg( delim ) );
387 void QgsDataSourceUri::skipBlanks(
const QString &
uri,
int &i )
390 while ( i <
uri.length() &&
uri[i].isSpace() )
394 QString QgsDataSourceUri::getValue(
const QString &
uri,
int &i )
396 skipBlanks(
uri, i );
400 if ( i <
uri.length() && (
uri[i] ==
'\'' ||
uri[i] ==
'"' ) )
402 QChar delim =
uri[i];
409 if ( i ==
uri.length() )
411 QgsDebugMsg( QStringLiteral(
"unterminated quoted string in connection info string" ) );
415 if (
uri[i] ==
'\\' )
418 if ( i ==
uri.length() )
420 if (
uri[i] != delim &&
uri[i] !=
'\\' )
423 else if (
uri[i] == delim )
435 while ( i <
uri.length() )
437 if (
uri[i].isSpace() )
443 if (
uri[i] ==
'\\' )
446 if ( i ==
uri.length() )
448 if (
uri[i] !=
'\\' &&
uri[i] !=
'\'' )
456 skipBlanks(
uri, i );
463 QStringList connectionItems;
465 if ( !mDatabase.isEmpty() )
467 connectionItems <<
"dbname='" + escape( mDatabase ) +
'\'';
470 if ( !mService.isEmpty() )
472 connectionItems <<
"service='" + escape( mService ) +
'\'';
474 else if ( !mHost.isEmpty() )
476 connectionItems <<
"host=" + mHost;
479 if ( mService.isEmpty() )
481 if ( !mPort.isEmpty() )
482 connectionItems <<
"port=" + mPort;
485 if ( !mDriver.isEmpty() )
487 connectionItems <<
"driver='" + escape( mDriver ) +
'\'';
490 if ( !mUsername.isEmpty() )
492 connectionItems <<
"user='" + escape( mUsername ) +
'\'';
494 if ( !mPassword.isEmpty() )
496 connectionItems <<
"password='" + escape( mPassword ) +
'\'';
502 connectionItems << QStringLiteral(
"sslmode=" ) +
encodeSslMode( mSSLmode );
505 if ( !mAuthConfigId.isEmpty() )
507 if ( expandAuthConfig )
511 QgsDebugMsg( QStringLiteral(
"Data source URI FAILED to update via loading configuration ID '%1'" ).arg( mAuthConfigId ) );
516 connectionItems <<
"authcfg=" + mAuthConfigId;
520 return connectionItems.join( QStringLiteral(
" " ) );
527 if ( !mKeyColumn.isEmpty() )
529 uri += QStringLiteral(
" key='%1'" ).arg( escape( mKeyColumn ) );
532 if ( mUseEstimatedMetadata )
534 uri += QStringLiteral(
" estimatedmetadata=true" );
537 if ( !mSrid.isEmpty() )
539 uri += QStringLiteral(
" srid=%1" ).arg( mSrid );
544 uri += QLatin1String(
" type=" );
548 if ( mSelectAtIdDisabled )
550 uri += QStringLiteral(
" selectatid=false" );
553 for ( QMap<QString, QString>::const_iterator it = mParams.begin(); it != mParams.end(); ++it )
555 if ( it.key().contains(
'=' ) || it.key().contains(
' ' ) )
557 QgsDebugMsg( QStringLiteral(
"invalid uri parameter %1 skipped" ).arg( it.key() ) );
561 uri +=
' ' + it.key() +
"='" + escape( it.value() ) +
'\'';
564 QString columnName( mGeometryColumn );
565 columnName.replace(
'\\', QLatin1String(
"\\\\" ) );
566 columnName.replace(
')', QLatin1String(
"\\)" ) );
568 uri += QStringLiteral(
" table=%1%2 sql=%3" )
570 mGeometryColumn.isNull() ? QString() : QStringLiteral(
" (%1)" ).arg( columnName ),
579 for (
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
581 url.addQueryItem( it.key(), it.value() );
583 return url.encodedQuery();
590 url.setEncodedQuery( uri );
591 QPair<QString, QString> item;
592 Q_FOREACH ( item, url.queryItems() )
594 mParams.insertMulti( item.first, item.second );
605 if ( !mSchema.isEmpty() )
606 return QStringLiteral(
"\"%1\".\"%2\"" )
607 .arg( escape( mSchema,
'"' ),
608 escape( mTable,
'"' ) );
610 return QStringLiteral(
"\"%1\"" )
611 .arg( escape( mTable,
'"' ) );
647 const QString &
table,
661 mAuthConfigId = authcfg;
691 if ( sslMode == QLatin1String(
"prefer" ) )
693 else if ( sslMode == QLatin1String(
"disable" ) )
695 else if ( sslMode == QLatin1String(
"allow" ) )
697 else if ( sslMode == QLatin1String(
"require" ) )
699 else if ( sslMode == QLatin1String(
"verify-ca" ) )
701 else if ( sslMode == QLatin1String(
"verify-full" ) )
711 case SslPrefer:
return QStringLiteral(
"prefer" );
712 case SslDisable:
return QStringLiteral(
"disable" );
713 case SslAllow:
return QStringLiteral(
"allow" );
714 case SslRequire:
return QStringLiteral(
"require" );
715 case SslVerifyCa:
return QStringLiteral(
"verify-ca" );
724 mParams.insertMulti( key, value );
729 Q_FOREACH (
const QString &val, value )
731 mParams.insertMulti( key, val );
737 return mParams.remove( key );
742 return mParams.value( key );
747 return mParams.values( key );
752 return mParams.contains( key );
QString schema() const
Returns the schema.
void setUsername(const QString &username)
Sets username.
SslMode sslMode() const
Returns the SSL mode.
QString keyColumn() const
Returns the name of the (primary) key column.
QStringList params(const QString &key) const
Gets multiple generic param (generic mode)
void clearSchema()
Clears the schema.
QString driver() const
Returns the driver.
static SslMode decodeSslMode(const QString &sslMode)
Decodes SSL mode string into enum value.
QgsWkbTypes::Type wkbType() const
The wkb type.
static Type parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
QString host() const
Returns the host.
QString port() const
Returns the port.
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 param(const QString &key) const
Gets generic param (generic mode)
QString database() const
Returns the database.
QString connectionInfo(bool expandAuthConfig=true) const
Returns connection part of URI.
Type
The WKB type describes the number of dimensions a geometry has.
bool selectAtIdDisabled() const
Returns whether the selection by id is disabled.
QString uri(bool expandAuthConfig=true) const
Returns complete uri.
QgsDataSourceUri()
default constructor
void setDriver(const QString &driver)
Sets the driver name.
int removeParam(const QString &key)
Remove generic param (generic mode)
#define QgsDebugMsgLevel(str, level)
QString authConfigId() const
Any associated authentication configuration ID.
QString srid() const
Returns the srid.
void setKeyColumn(const QString &column)
Sets the name of the (primary) key column.
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)
set complete encoded uri (generic mode)
static QString encodeSslMode(SslMode sslMode)
Encodes SSL mode enum value into a string.
void setParam(const QString &key, const QString &value)
Set generic param (generic mode)
void setAuthConfigId(const QString &authcfg)
Sets authentication configuration ID.
QByteArray encodedUri() const
Returns complete encoded uri (generic mode)
QString quotedTablename() const
quoted table name
void setSrid(const QString &srid)
Sets the srid.
void setWkbType(QgsWkbTypes::Type type)
Sets the wkb type.
bool useEstimatedMetadata() const
Returns true if estimated metadata are used.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
void disableSelectAtId(bool flag)
Sets to true to disable selection by id.
QString geometryColumn() const
Returns the name of the geometry column.
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.
void setPassword(const QString &password)
Sets password.
QString sql() const
Returns the SQL query.
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
bool hasParam(const QString &key) const
Test if param exists (generic mode)
void setSchema(const QString &schema)
Sets the table schema.
QString service() const
Returns the service name.
QString table() const
Returns the table.
void setUseEstimatedMetadata(bool flag)
Sets use Estimated Metadata.
void setDatabase(const QString &database)
Sets database.
QString username() const
Returns the username.
static QString removePassword(const QString &aUri)
Removes password element from uris.
QString password() const
Returns the password.