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] !=
'=' )
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 ) );
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();
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.insertMulti( 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.insertMulti( 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 );