25 #include <QStringList> 37 while ( i < uri.length() )
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( QString(
"= 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() )
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" ) )
159 else if ( pname == QLatin1String(
"dbname" ) )
163 else if ( pname == QLatin1String(
"host" ) )
167 else if ( pname == QLatin1String(
"hostaddr" ) )
171 else if ( pname == QLatin1String(
"port" ) )
175 else if ( pname == QLatin1String(
"driver" ) )
179 else if ( pname == QLatin1String(
"tty" ) )
183 else if ( pname == QLatin1String(
"options" ) )
187 else if ( pname == QLatin1String(
"sslmode" ) )
189 if ( pval == QLatin1String(
"disable" ) )
191 else if ( pval == QLatin1String(
"allow" ) )
193 else if ( pval == QLatin1String(
"prefer" ) )
195 else if ( pval == QLatin1String(
"require" ) )
197 else if ( pval == QLatin1String(
"verify-ca" ) )
199 else if ( pval == QLatin1String(
"verify-full" ) )
202 else if ( pname == QLatin1String(
"requiressl" ) )
204 if ( pval == QLatin1String(
"0" ) )
209 else if ( pname == QLatin1String(
"krbsrvname" ) )
213 else if ( pname == QLatin1String(
"gsslib" ) )
219 QgsDebugMsgLevel(
"parameter \"" + pname +
"\":\"" + pval +
"\" added", 4 );
229 regexp.setMinimal(
true );
230 QString safeName( aUri );
231 if ( aUri.contains( QLatin1String(
" password=" ) ) )
233 regexp.setPattern( QStringLiteral(
" password=.* " ) );
234 safeName.replace( regexp, QStringLiteral(
" " ) );
236 else if ( aUri.contains( QLatin1String(
",password=" ) ) )
238 regexp.setPattern( QStringLiteral(
",password=.*," ) );
239 safeName.replace( regexp, QStringLiteral(
"," ) );
241 else if ( aUri.contains( QLatin1String(
"IDB:" ) ) )
243 regexp.setPattern( QStringLiteral(
" pass=.* " ) );
244 safeName.replace( regexp, QStringLiteral(
" " ) );
246 else if ( ( aUri.contains( QLatin1String(
"OCI:" ) ) )
247 || ( aUri.contains( QLatin1String(
"ODBC:" ) ) ) )
249 regexp.setPattern( QStringLiteral(
"/.*@" ) );
250 safeName.replace( regexp, QStringLiteral(
"/@" ) );
252 else if ( aUri.contains( QLatin1String(
"SDE:" ) ) )
254 QStringList strlist = aUri.split(
',' );
255 safeName = strlist[0] +
',' + strlist[1] +
',' + strlist[2] +
',' + strlist[3];
262 return mAuthConfigId;
332 return mGeometryColumn;
355 mUseEstimatedMetadata = flag;
360 return mUseEstimatedMetadata;
365 mSelectAtIdDisabled = flag;
370 return mSelectAtIdDisabled;
388 QString QgsDataSourceUri::escape(
const QString &val, QChar delim =
'\'' )
const 390 QString escaped = val;
392 escaped.replace(
'\\', QLatin1String(
"\\\\" ) );
393 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(
"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 ) +
'\'';
512 connectionItems << QStringLiteral(
"sslmode=disable" );
514 connectionItems << QStringLiteral(
"sslmode=allow" );
516 connectionItems << QStringLiteral(
"sslmode=require" );
518 else if ( mSSLmode == SSLprefer )
519 connectionItems <<
"sslmode=prefer";
522 connectionItems << QStringLiteral(
"sslmode=verify-ca" );
524 connectionItems << QStringLiteral(
"sslmode=verify-full" );
526 if ( !mAuthConfigId.isEmpty() )
528 if ( expandAuthConfig )
532 QgsDebugMsg( QString(
"Data source URI FAILED to update via loading configuration ID '%1'" ).arg( mAuthConfigId ) );
537 connectionItems <<
"authcfg=" + mAuthConfigId;
541 return connectionItems.join( QStringLiteral(
" " ) );
548 if ( !mKeyColumn.isEmpty() )
550 uri += QStringLiteral(
" key='%1'" ).arg( escape( mKeyColumn ) );
553 if ( mUseEstimatedMetadata )
555 uri += QStringLiteral(
" estimatedmetadata=true" );
558 if ( !mSrid.isEmpty() )
560 uri += QStringLiteral(
" srid=%1" ).arg( mSrid );
565 uri += QLatin1String(
" type=" );
569 if ( mSelectAtIdDisabled )
571 uri += QStringLiteral(
" selectatid=false" );
574 for ( QMap<QString, QString>::const_iterator it = mParams.begin(); it != mParams.end(); ++it )
576 if ( it.key().contains(
'=' ) || it.key().contains(
' ' ) )
578 QgsDebugMsg( QString(
"invalid uri parameter %1 skipped" ).arg( it.key() ) );
582 uri +=
' ' + it.key() +
"='" + escape( it.value() ) +
'\'';
585 QString columnName( mGeometryColumn );
586 columnName.replace(
'\\', QLatin1String(
"\\\\" ) );
587 columnName.replace(
')', QLatin1String(
"\\)" ) );
589 uri += QStringLiteral(
" table=%1%2 sql=%3" )
591 mGeometryColumn.isNull() ? QString() : QStringLiteral(
" (%1)" ).arg( columnName ),
600 for (
auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
602 url.addQueryItem( it.key(), it.value() );
604 return url.encodedQuery();
611 url.setEncodedQuery( uri );
612 QPair<QString, QString> item;
613 Q_FOREACH ( item, url.queryItems() )
615 mParams.insertMulti( item.first, item.second );
626 if ( !mSchema.isEmpty() )
627 return QStringLiteral(
"\"%1\".\"%2\"" )
628 .arg( escape( mSchema,
'"' ),
629 escape( mTable,
'"' ) );
631 return QStringLiteral(
"\"%1\"" )
632 .arg( escape( mTable,
'"' ) );
668 const QString &
table,
682 mAuthConfigId = authcfg;
713 mParams.insertMulti( key, value );
718 Q_FOREACH (
const QString &val, value )
720 mParams.insertMulti( key, val );
726 return mParams.remove( key );
731 return mParams.value( key );
736 return mParams.values( key );
741 return mParams.contains( key );
QString param(const QString &key) const
Get generic param (generic mode)
QgsWkbTypes::Type wkbType() const
The wkb type.
QString table() const
Returns the table.
bool useEstimatedMetadata() const
Returns true if estimated metadata are used.
void setUsername(const QString &username)
set username
bool hasParam(const QString &key) const
Test if param exists (generic mode)
void clearSchema()
Clears the schema.
QString authConfigId() const
Any associated authentication configuration ID.
SslMode sslMode() const
Returns the SSL mode.
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.
void setConnection(const QString &aHost, const QString &aPort, const QString &aDatabase, const QString &aUsername, const QString &aPassword, SslMode sslmode=SslPrefer, const QString &authConfigId=QString())
Set all connection related members at once.
QString connectionInfo(bool expandAuthConfig=true) const
return connection part of URI
QString username() const
Returns the username.
QString host() const
Returns the host.
Type
The WKB type describes the number of dimensions a geometry has.
QgsDataSourceUri()
default constructor
QStringList params(const QString &key) const
Get multiple generic param (generic mode)
void setDriver(const QString &driver)
Sets the driver name.
int removeParam(const QString &key)
Remove generic param (generic mode)
#define QgsDebugMsgLevel(str, level)
QString geometryColumn() const
Return the name of the geometry column.
void setKeyColumn(const QString &column)
Sets the name of the (primary) key column.
bool selectAtIdDisabled() const
Returns whether the selection by id is disabled.
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)
QString quotedTablename() const
quoted table name
void setParam(const QString &key, const QString &value)
Set generic param (generic mode)
QString port() const
Returns the port.
void setAuthConfigId(const QString &authcfg)
Set authentication configuration ID.
void setSrid(const QString &srid)
Sets the srid.
void setWkbType(QgsWkbTypes::Type type)
Sets the wkb type.
QString uri(bool expandAuthConfig=true) const
return complete uri
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
void disableSelectAtId(bool flag)
Set to true to disable selection by id.
QString driver() const
Returns the driver.
void setDataSource(const QString &aSchema, const QString &aTable, const QString &aGeometryColumn, const QString &aSql=QString(), const QString &aKeyColumn=QString())
Set all data source related members at once.
void setSql(const QString &sql)
Sets the SQL query.
void setPassword(const QString &password)
set password
QByteArray encodedUri() const
return complete encoded uri (generic mode)
QString sql() const
Returns the SQL query.
QString service() const
Returns the service name.
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.
void setSchema(const QString &schema)
set the table schema
QString schema() const
Returns the schema.
void setUseEstimatedMetadata(bool flag)
set use Estimated Metadata
QString database() const
Returns the database.
void setDatabase(const QString &database)
Set database.
QString srid() const
Returns the srid.
static QString removePassword(const QString &aUri)
Removes password element from uris.