QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsdatasourceuri.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatasourceuri.h - Structure to contain the component parts
3 of a data source URI
4 -------------------
5 begin : Dec 5, 2004
6 copyright : (C) 2004 by Gary E.Sherman
7 email : sherman at mrcc.com
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19#include "qgsdatasourceuri.h"
20
21#include "qgsapplication.h"
22#include "qgsauthmanager.h"
23#include "qgslogger.h"
24#include "qgswkbtypes.h"
25
26#include <QRegularExpression>
27#include <QString>
28#include <QStringList>
29#include <QUrl>
30#include <QUrlQuery>
31
32#include "moc_qgsdatasourceuri.cpp"
33
34using namespace Qt::StringLiterals;
35
36#define HIDING_TOKEN u"XXXXXXXX"_s
37
39{
40 // do nothing
41}
42
44{
45 QString uri = u;
46 int i = 0;
47 while ( i < uri.length() )
48 {
49 skipBlanks( uri, i );
50
51 if ( uri[i] == '=' )
52 {
53 QgsDebugError( u"parameter name expected before ="_s );
54 i++;
55 continue;
56 }
57
58 int start = i;
59
60 while ( i < uri.length() && uri[i] != '=' && !uri[i].isSpace() )
61 i++;
62
63 const QString pname = uri.mid( start, i - start );
64
65 skipBlanks( uri, i );
66
67 if ( i == uri.length() || uri[i] != '=' )
68 {
69 // no "=", so likely not a parameter name
70 continue;
71 }
72
73 i++;
74
75 if ( pname == "sql"_L1 )
76 {
77 // rest of line is a sql where clause
78 skipBlanks( uri, i );
79 mSql = uri.mid( i );
80
81 // handle empty sql specified by a empty '' or "" encapsulated value
82 // possibly we should be calling getValue here, but there's a very high risk of regressions
83 // if we change that now...
84 if ( mSql == "''"_L1 || mSql == "\"\""_L1 )
85 mSql.clear();
86 break;
87 }
88 else
89 {
90 const QString pval = getValue( uri, i );
91
92 if ( pname == "table"_L1 )
93 {
94 if ( i < uri.length() && uri[i] == '.' )
95 {
96 i++;
97
98 mSchema = pval;
99 mTable = getValue( uri, i );
100 }
101 else
102 {
103 mTable = pval;
104 }
105
106 if ( i < uri.length() && uri[i] == '(' )
107 {
108 i++;
109
110 start = i;
111 while ( i < uri.length() && uri[i] != ')' )
112 {
113 if ( uri[i] == '\\' )
114 i++;
115 i++;
116 }
117
118 if ( i == uri.length() )
119 {
120 QgsDebugError( u"closing parenthesis missing"_s );
121 }
122
123 mGeometryColumn = uri.mid( start, i - start );
124 mGeometryColumn.replace( "\\)"_L1, ")"_L1 );
125 mGeometryColumn.replace( "\\\\"_L1, "\\"_L1 );
126
127 i++;
128 }
129 else
130 {
131 mGeometryColumn = QString();
132 }
133 }
134 else if ( pname == "schema"_L1 )
135 {
136 mSchema = pval;
137 }
138 else if ( pname == "key"_L1 )
139 {
140 mKeyColumn = pval;
141 }
142 else if ( pname == "estimatedmetadata"_L1 )
143 {
144 mUseEstimatedMetadata = pval == "true"_L1;
145 }
146 else if ( pname == "srid"_L1 )
147 {
148 mSrid = pval;
149 }
150 else if ( pname == "type"_L1 )
151 {
152 mWkbType = QgsWkbTypes::parseType( pval );
153 }
154 else if ( pname == "selectatid"_L1 )
155 {
156 mSelectAtIdDisabledSet = true;
157 mSelectAtIdDisabled = pval == "false"_L1;
158 }
159 else if ( pname == "service"_L1 )
160 {
161 mService = pval;
162 }
163 else if ( pname == "authcfg"_L1 )
164 {
165 mAuthConfigId = pval;
166 }
167 else if ( pname == "user"_L1 || pname == "username"_L1 ) // Also accepts new WFS provider naming
168 {
169 mUsername = pval;
170 }
171 else if ( pname == "password"_L1 )
172 {
173 mPassword = pval;
174 }
175 else if ( pname == "connect_timeout"_L1 )
176 {
177 QgsDebugMsgLevel( u"connection timeout ignored"_s, 3 );
178 }
179 else if ( pname == "dbname"_L1 )
180 {
181 mDatabase = pval;
182 }
183 else if ( pname == "host"_L1 )
184 {
185 mHost = pval;
186 }
187 else if ( pname == "hostaddr"_L1 )
188 {
189 QgsDebugMsgLevel( u"database host ip address ignored"_s, 2 );
190 }
191 else if ( pname == "port"_L1 )
192 {
193 mPort = pval;
194 }
195 else if ( pname == "driver"_L1 )
196 {
197 mDriver = pval;
198 }
199 else if ( pname == "tty"_L1 )
200 {
201 QgsDebugMsgLevel( u"backend debug tty ignored"_s, 2 );
202 }
203 else if ( pname == "options"_L1 )
204 {
205 QgsDebugMsgLevel( u"backend debug options ignored"_s, 2 );
206 }
207 else if ( pname == "sslmode"_L1 )
208 {
209 mSSLmode = decodeSslMode( pval );
210 }
211 else if ( pname == "requiressl"_L1 )
212 {
213 if ( pval == "0"_L1 )
214 mSSLmode = SslDisable;
215 else
216 mSSLmode = SslPrefer;
217 }
218 else if ( pname == "krbsrvname"_L1 )
219 {
220 QgsDebugMsgLevel( u"kerberos server name ignored"_s, 2 );
221 }
222 else if ( pname == "gsslib"_L1 )
223 {
224 QgsDebugMsgLevel( u"gsslib ignored"_s, 2 );
225 }
226 else if ( pname.startsWith( QgsHttpHeaders::PARAM_PREFIX ) )
227 {
228 mHttpHeaders.insert( pname, pval );
229 }
230 else
231 {
232 QgsDebugMsgLevel( "parameter \"" + pname + "\":\"" + pval + "\" added", 4 );
233 setParam( pname, pval );
234 }
235 }
236 }
237}
238
239QString QgsDataSourceUri::removePassword( const QString &aUri, bool hide )
240{
241 QRegularExpression regexp;
242 regexp.setPatternOptions( QRegularExpression::InvertedGreedinessOption );
243 QString safeName( aUri );
244 if ( aUri.contains( " password="_L1 ) )
245 {
246 regexp.setPattern( u" password=.* "_s );
247
248 if ( hide )
249 {
250 safeName.replace( regexp, u" password=%1 "_s.arg( HIDING_TOKEN ) );
251 }
252 else
253 {
254 safeName.replace( regexp, u" "_s );
255 }
256 }
257 else if ( aUri.contains( ",password="_L1 ) )
258 {
259 regexp.setPattern( u",password=.*,"_s );
260
261 if ( hide )
262 {
263 safeName.replace( regexp, u",password=%1,"_s.arg( HIDING_TOKEN ) );
264 }
265 else
266 {
267 safeName.replace( regexp, u","_s );
268 }
269 }
270 else if ( aUri.contains( "IDB:"_L1 ) )
271 {
272 regexp.setPattern( u" pass=.* "_s );
273
274 if ( hide )
275 {
276 safeName.replace( regexp, u" pass=%1 "_s.arg( HIDING_TOKEN ) );
277 }
278 else
279 {
280 safeName.replace( regexp, u" "_s );
281 }
282 }
283 else if ( ( aUri.contains( "OCI:"_L1 ) )
284 || ( aUri.contains( "ODBC:"_L1 ) ) )
285 {
286 regexp.setPattern( u"/.*@"_s );
287
288 if ( hide )
289 {
290 safeName.replace( regexp, u"/%1@"_s.arg( HIDING_TOKEN ) );
291 }
292 else
293 {
294 safeName.replace( regexp, u"/@"_s );
295 }
296 }
297 else if ( aUri.contains( "postgresql:"_L1 ) )
298 {
299 // postgresql://user:pwd@...
300 regexp.setPattern( u"/.*@"_s );
301 const QString matched = regexp.match( aUri ).captured();
302
303 QString anonymised = matched;
304 const QStringList items = matched.split( u":"_s );
305 if ( items.size() > 1 )
306 {
307 anonymised = matched.split( u":"_s )[0];
308 if ( hide )
309 {
310 anonymised.append( u":%1"_s.arg( HIDING_TOKEN ) );
311 }
312 anonymised.append( u"@"_s );
313 }
314
315 safeName.replace( regexp, anonymised );
316 }
317 else if ( aUri.contains( "SDE:"_L1 ) )
318 {
319 QStringList strlist = aUri.split( ',' );
320 safeName = strlist[0] + ',' + strlist[1] + ',' + strlist[2] + ',' + strlist[3];
321 }
322 return safeName;
323}
324
326{
327 return mAuthConfigId;
328}
329
331{
332 return mUsername;
333}
334
336{
337 mUsername = username;
338}
339
341{
342 return mService;
343}
344
346{
347 return mHost;
348}
349
351{
352 return mDatabase;
353}
354
355void QgsDataSourceUri::setPort( const QString &port )
356{
357 mPort = port;
358}
359
361{
362 return mPassword;
363}
364
366{
367 mSSLmode = mode;
368}
369
371{
372 mService = service;
373}
374
376{
377 mPassword = password;
378}
379
381{
382 return mPort;
383}
384
386{
387 return mDriver;
388}
389
391{
392 return mSSLmode;
393}
394
396{
397 return mSchema;
398}
399
401{
402 return mTable;
403}
404
406{
407 return mSql;
408}
409
411{
412 return mGeometryColumn;
413}
414
416{
417 return mKeyColumn;
418}
419
420
422{
423 mDriver = driver;
424}
425
426
427void QgsDataSourceUri::setKeyColumn( const QString &column )
428{
429 mKeyColumn = column;
430}
431
432
434{
435 mUseEstimatedMetadata = flag;
436}
437
439{
440 return mUseEstimatedMetadata;
441}
442
444{
445 mSelectAtIdDisabledSet = true;
446 mSelectAtIdDisabled = flag;
447}
448
450{
451 return mSelectAtIdDisabled;
452}
453
454void QgsDataSourceUri::setSql( const QString &sql )
455{
456 mSql = sql;
457}
458
459void QgsDataSourceUri::setHost( const QString &host )
460{
461 mHost = host;
462}
463
465{
466 mSchema.clear();
467}
468
470{
471 mSchema = schema;
472}
473
474QString QgsDataSourceUri::escape( const QString &val, QChar delim = '\'' ) const
475{
476 QString escaped = val;
477
478 escaped.replace( '\\', "\\\\"_L1 );
479 escaped.replace( delim, u"\\%1"_s.arg( delim ) );
480
481 return escaped;
482}
483
485{
486 mGeometryColumn = geometryColumn;
487}
488
489void QgsDataSourceUri::setTable( const QString &table )
490{
491 mTable = table;
492}
493
494void QgsDataSourceUri::skipBlanks( const QString &uri, int &i )
495{
496 // skip space before value
497 while ( i < uri.length() && uri[i].isSpace() )
498 i++;
499}
500
501QString QgsDataSourceUri::getValue( const QString &uri, int &i )
502{
503 skipBlanks( uri, i );
504
505 // Get the parameter value
506 QString pval;
507 if ( i < uri.length() && ( uri[i] == '\'' || uri[i] == '"' ) )
508 {
509 const QChar delim = uri[i];
510
511 i++;
512
513 // value is quoted
514 for ( ;; )
515 {
516 if ( i == uri.length() )
517 {
518 QgsDebugError( u"unterminated quoted string in connection info string"_s );
519 return pval;
520 }
521
522 if ( uri[i] == '\\' )
523 {
524 i++;
525 if ( i == uri.length() )
526 continue;
527 if ( uri[i] != delim && uri[i] != '\\' )
528 i--;
529 }
530 else if ( uri[i] == delim )
531 {
532 i++;
533 break;
534 }
535
536 pval += uri[i++];
537 }
538 }
539 else
540 {
541 // value is not quoted
542 while ( i < uri.length() )
543 {
544 if ( uri[i].isSpace() )
545 {
546 // end of value
547 break;
548 }
549
550 if ( uri[i] == '\\' )
551 {
552 i++;
553 if ( i == uri.length() )
554 break;
555 if ( uri[i] != '\\' && uri[i] != '\'' )
556 i--;
557 }
558
559 pval += uri[i++];
560 }
561 }
562
563 skipBlanks( uri, i );
564
565 return pval;
566}
567
568QString QgsDataSourceUri::connectionInfo( bool expandAuthConfig ) const
569{
570 QStringList connectionItems;
571
572 if ( !mDatabase.isEmpty() )
573 {
574 connectionItems << "dbname='" + escape( mDatabase ) + '\'';
575 }
576
577 if ( !mService.isEmpty() )
578 {
579 connectionItems << "service='" + escape( mService ) + '\'';
580 }
581 else if ( !mHost.isEmpty() )
582 {
583 connectionItems << "host=" + mHost;
584 }
585
586 if ( mService.isEmpty() )
587 {
588 if ( !mPort.isEmpty() )
589 connectionItems << "port=" + mPort;
590 }
591
592 if ( !mDriver.isEmpty() )
593 {
594 connectionItems << "driver='" + escape( mDriver ) + '\'';
595 }
596
597 if ( !mUsername.isEmpty() )
598 {
599 connectionItems << "user='" + escape( mUsername ) + '\'';
600
601 if ( !mPassword.isEmpty() )
602 {
603 connectionItems << "password='" + escape( mPassword ) + '\'';
604 }
605 }
606
607 if ( mSSLmode != SslPrefer ) // no need to output the default
608 {
609 connectionItems << u"sslmode="_s + encodeSslMode( mSSLmode );
610 }
611
612 if ( !mAuthConfigId.isEmpty() )
613 {
614 if ( expandAuthConfig )
615 {
616 if ( !QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, mAuthConfigId ) )
617 {
618 QgsDebugError( u"Data source URI FAILED to update via loading configuration ID '%1'"_s.arg( mAuthConfigId ) );
619 }
620 }
621 else
622 {
623 connectionItems << "authcfg=" + mAuthConfigId;
624 }
625 }
626
627 return connectionItems.join( ' '_L1 );
628}
629
630QString QgsDataSourceUri::uri( bool expandAuthConfig ) const
631{
632 QString uri = connectionInfo( expandAuthConfig );
633
634 if ( !mKeyColumn.isEmpty() )
635 {
636 uri += u" key='%1'"_s.arg( escape( mKeyColumn ) );
637 }
638
639 if ( mUseEstimatedMetadata )
640 {
641 uri += " estimatedmetadata=true"_L1;
642 }
643
644 if ( !mSrid.isEmpty() )
645 {
646 uri += u" srid=%1"_s.arg( mSrid );
647 }
648
649 if ( mWkbType != Qgis::WkbType::Unknown && mWkbType != Qgis::WkbType::NoGeometry )
650 {
651 uri += " type="_L1;
652 uri += QgsWkbTypes::displayString( mWkbType );
653 }
654
655 if ( mSelectAtIdDisabled )
656 {
657 uri += " selectatid=false"_L1;
658 }
659
660 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
661 {
662 if ( it.key().contains( '=' ) || it.key().contains( ' ' ) )
663 {
664 QgsDebugError( u"invalid uri parameter %1 skipped"_s.arg( it.key() ) );
665 continue;
666 }
667
668 uri += ' ' + it.key() + "='" + escape( it.value() ) + '\'';
669 }
670
671 uri += mHttpHeaders.toSpacedString();
672
673 QString columnName( mGeometryColumn );
674 columnName.replace( '\\', "\\\\"_L1 );
675 columnName.replace( ')', "\\)"_L1 );
676
677 if ( !mTable.isEmpty() )
678 {
679 uri += u" table=%1%2"_s
680 .arg( quotedTablename(),
681 mGeometryColumn.isEmpty() ? QString() : u" (%1)"_s.arg( columnName ) );
682 }
683 else if ( !mSchema.isEmpty() )
684 {
685 uri += u" schema='%1'"_s.arg( escape( mSchema ) );
686 }
687
688 if ( !mSql.isEmpty() )
689 {
690 uri += u" sql="_s + mSql;
691 }
692
693 return uri;
694}
695
696// from qurl.h
697QByteArray toLatin1_helper( const QString &string )
698{
699 if ( string.isEmpty() )
700 return string.isNull() ? QByteArray() : QByteArray( "" );
701 return string.toLatin1();
702}
703
705{
706 QUrlQuery url;
707 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
708 {
709 url.addQueryItem( it.key(), QUrl::toPercentEncoding( it.value() ) );
710 }
711
712 if ( !mUsername.isEmpty() )
713 url.addQueryItem( u"username"_s, QUrl::toPercentEncoding( mUsername ) );
714
715 if ( !mPassword.isEmpty() )
716 url.addQueryItem( u"password"_s, QUrl::toPercentEncoding( mPassword ) );
717
718 if ( !mAuthConfigId.isEmpty() )
719 url.addQueryItem( u"authcfg"_s, QUrl::toPercentEncoding( mAuthConfigId ) );
720
721 mHttpHeaders.updateUrlQuery( url );
722
723 return toLatin1_helper( url.toString( QUrl::FullyEncoded ) );
724}
725
726void QgsDataSourceUri::setEncodedUri( const QByteArray &uri )
727{
728 mParams.clear();
729 mUsername.clear();
730 mPassword.clear();
731 mAuthConfigId.clear();
732
733 QUrl url;
734 url.setQuery( QString::fromLatin1( uri ) );
735 const QUrlQuery query( url );
736
737 mHttpHeaders.setFromUrlQuery( query );
738
739 const auto constQueryItems = query.queryItems( QUrl::ComponentFormattingOption::FullyDecoded );
740 for ( const QPair<QString, QString> &item : constQueryItems )
741 {
742 if ( !item.first.startsWith( QgsHttpHeaders::PARAM_PREFIX ) && item.first != QgsHttpHeaders::KEY_REFERER )
743 {
744 if ( item.first == "username"_L1 )
745 mUsername = query.queryItemValue( u"username"_s, QUrl::ComponentFormattingOption::FullyDecoded );
746 else if ( item.first == "password"_L1 )
747 mPassword = query.queryItemValue( u"password"_s, QUrl::ComponentFormattingOption::FullyDecoded );
748 else if ( item.first == "authcfg"_L1 )
749 mAuthConfigId = query.queryItemValue( u"authcfg"_s, QUrl::ComponentFormattingOption::FullyDecoded );
750 else
751 mParams.insert( item.first, item.second );
752 }
753 }
754}
755
757{
758 QUrl url;
759 url.setQuery( uri );
760 setEncodedUri( url.query( QUrl::EncodeUnicode ).toLatin1() );
761}
762
764{
765 if ( !mSchema.isEmpty() )
766 return u"\"%1\".\"%2\""_s
767 .arg( escape( mSchema, '"' ),
768 escape( mTable, '"' ) );
769 else
770 return u"\"%1\""_s
771 .arg( escape( mTable, '"' ) );
772}
773
775 const QString &port,
776 const QString &database,
777 const QString &username,
778 const QString &password,
779 SslMode sslmode,
780 const QString &authConfigId )
781{
782 mHost = host;
783 mDatabase = database;
784 mPort = port;
785 mUsername = username;
786 mPassword = password;
787 mSSLmode = sslmode;
788 mAuthConfigId = authConfigId;
789}
790
792 const QString &database,
793 const QString &username,
794 const QString &password,
795 SslMode sslmode,
796 const QString &authConfigId )
797{
798 mService = service;
799 mDatabase = database;
800 mUsername = username;
801 mPassword = password;
802 mSSLmode = sslmode;
803 mAuthConfigId = authConfigId;
804}
805
807 const QString &table,
808 const QString &geometryColumn,
809 const QString &sql,
810 const QString &keyColumn )
811{
812 mSchema = schema;
813 mTable = table;
814 mGeometryColumn = geometryColumn;
815 mSql = sql;
816 mKeyColumn = keyColumn;
817}
818
819void QgsDataSourceUri::setAuthConfigId( const QString &authcfg )
820{
821 mAuthConfigId = authcfg;
822}
823
825{
826 mDatabase = database;
827}
828
830{
831 return mWkbType;
832}
833
838
840{
841 return mSrid;
842}
843
844void QgsDataSourceUri::setSrid( const QString &srid )
845{
846 mSrid = srid;
847}
848
850{
851 if ( sslMode == "prefer"_L1 )
852 return SslPrefer;
853 else if ( sslMode == "disable"_L1 )
854 return SslDisable;
855 else if ( sslMode == "allow"_L1 )
856 return SslAllow;
857 else if ( sslMode == "require"_L1 )
858 return SslRequire;
859 else if ( sslMode == "verify-ca"_L1 )
860 return SslVerifyCa;
861 else if ( sslMode == "verify-full"_L1 )
862 return SslVerifyFull;
863 else
864 return SslPrefer; // default
865}
866
868{
869 switch ( sslMode )
870 {
871 case SslPrefer: return u"prefer"_s;
872 case SslDisable: return u"disable"_s;
873 case SslAllow: return u"allow"_s;
874 case SslRequire: return u"require"_s;
875 case SslVerifyCa: return u"verify-ca"_s;
876 case SslVerifyFull: return u"verify-full"_s;
877 }
878 return QString();
879}
880
881void QgsDataSourceUri::setParam( const QString &key, const QString &value )
882{
883 // maintain old API
884 if ( key == "username"_L1 )
885 mUsername = value;
886 else if ( key == "password"_L1 )
887 mPassword = value;
888 else if ( key == "authcfg"_L1 )
889 mAuthConfigId = value;
890 else
891 {
892 // may be multiple
893 mParams.insert( key, value );
894 }
895}
896
897void QgsDataSourceUri::setParam( const QString &key, const QStringList &value )
898{
899 for ( const QString &val : value )
900 {
901 setParam( key, val );
902 }
903}
904
905int QgsDataSourceUri::removeParam( const QString &key )
906{
907 if ( key == "username"_L1 && !mUsername.isEmpty() )
908 {
909 mUsername.clear();
910 return 1;
911 }
912 else if ( key == "password"_L1 && !mPassword.isEmpty() )
913 {
914 mPassword.clear();
915 return 1;
916 }
917 else if ( key == "authcfg"_L1 && !mAuthConfigId.isEmpty() )
918 {
919 mAuthConfigId.clear();
920 return 1;
921 }
922
923 return mParams.remove( key );
924}
925
926QString QgsDataSourceUri::param( const QString &key ) const
927{
928 // maintain old api
929 if ( key == "username"_L1 && !mUsername.isEmpty() )
930 return mUsername;
931 else if ( key == "password"_L1 && !mPassword.isEmpty() )
932 return mPassword;
933 else if ( key == "authcfg"_L1 && !mAuthConfigId.isEmpty() )
934 return mAuthConfigId;
935
936 return mParams.value( key );
937}
938
939QStringList QgsDataSourceUri::params( const QString &key ) const
940{
941 // maintain old api
942 if ( key == "username"_L1 && !mUsername.isEmpty() )
943 return QStringList() << mUsername;
944 else if ( key == "password"_L1 && !mPassword.isEmpty() )
945 return QStringList() << mPassword;
946 else if ( key == "authcfg"_L1 && !mAuthConfigId.isEmpty() )
947 return QStringList() << mAuthConfigId;
948
949 return mParams.values( key );
950}
951
952bool QgsDataSourceUri::hasParam( const QString &key ) const
953{
954 // maintain old api
955 if ( key == "username"_L1 && !mUsername.isEmpty() )
956 return true;
957 else if ( key == "password"_L1 && !mPassword.isEmpty() )
958 return true;
959 else if ( key == "authcfg"_L1 && !mAuthConfigId.isEmpty() )
960 return true;
961
962 return mParams.contains( key );
963}
964
966{
967 QSet<QString> paramKeys;
968 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); it++ )
969 paramKeys.insert( it.key() );
970
971 if ( !mHost.isEmpty() )
972 paramKeys.insert( "host"_L1 );
973 if ( !mPort.isEmpty() )
974 paramKeys.insert( "port"_L1 );
975 if ( !mDriver.isEmpty() )
976 paramKeys.insert( "driver"_L1 );
977 if ( !mService.isEmpty() )
978 paramKeys.insert( "service"_L1 );
979 if ( !mDatabase.isEmpty() )
980 paramKeys.insert( "dbname"_L1 );
981 if ( !mSchema.isEmpty() )
982 paramKeys.insert( "schema"_L1 );
983 if ( !mTable.isEmpty() )
984 paramKeys.insert( "table"_L1 );
985 // Ignore mGeometryColumn: not a key ==> embedded in table value
986 if ( !mSql.isEmpty() )
987 paramKeys.insert( "sql"_L1 );
988 if ( !mAuthConfigId.isEmpty() )
989 paramKeys.insert( "authcfg"_L1 );
990 if ( !mUsername.isEmpty() )
991 paramKeys.insert( "username"_L1 );
992 if ( !mPassword.isEmpty() )
993 paramKeys.insert( "password"_L1 );
994 if ( mSSLmode != SslPrefer )
995 paramKeys.insert( "sslmode"_L1 );
996 if ( !mKeyColumn.isEmpty() )
997 paramKeys.insert( "key"_L1 );
998 if ( mUseEstimatedMetadata )
999 paramKeys.insert( "estimatedmetadata"_L1 );
1000 if ( mSelectAtIdDisabledSet )
1001 paramKeys.insert( "selectatid"_L1 );
1002 if ( mWkbType != Qgis::WkbType::Unknown )
1003 paramKeys.insert( "type"_L1 );
1004 if ( !mSrid.isEmpty() )
1005 paramKeys.insert( "srid"_L1 );
1006 return paramKeys;
1007}
1008
1010{
1011 // cheap comparisons first:
1012 if ( mUseEstimatedMetadata != other.mUseEstimatedMetadata ||
1013 mSelectAtIdDisabled != other.mSelectAtIdDisabled ||
1014 mSelectAtIdDisabledSet != other.mSelectAtIdDisabledSet ||
1015 mSSLmode != other.mSSLmode ||
1016 mWkbType != other.mWkbType )
1017 {
1018 return false;
1019 }
1020
1021 if ( mHost != other.mHost ||
1022 mPort != other.mPort ||
1023 mDriver != other.mDriver ||
1024 mService != other.mService ||
1025 mDatabase != other.mDatabase ||
1026 mSchema != other.mSchema ||
1027 mTable != other.mTable ||
1028 mGeometryColumn != other.mGeometryColumn ||
1029 mSql != other.mSql ||
1030 mAuthConfigId != other.mAuthConfigId ||
1031 mUsername != other.mUsername ||
1032 mPassword != other.mPassword ||
1033 mKeyColumn != other.mKeyColumn ||
1034 mSrid != other.mSrid ||
1035 mParams != other.mParams ||
1036 mHttpHeaders != other.mHttpHeaders )
1037 {
1038 return false;
1039 }
1040
1041 return true;
1042}
1043
1045{
1046 return !( *this == other );
1047}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ NoGeometry
No geometry.
Definition qgis.h:298
@ Unknown
Unknown.
Definition qgis.h:281
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
QString srid() const
Returns the spatial reference ID associated with the URI.
SslMode
Available SSL connection modes.
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.
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
QStringList params(const QString &key) const
Returns multiple generic parameter values corresponding to the specified key.
void setSchema(const QString &schema)
Sets the scheme for the URI.
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
int removeParam(const QString &key)
Removes a generic parameter by key.
static SslMode decodeSslMode(const QString &sslMode)
Decodes SSL mode string into enum value.
void setSql(const QString &sql)
Sets the sql filter for the URI.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
QString table() const
Returns the table name stored in the URI.
void setTable(const QString &table)
Sets table to table.
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
QString quotedTablename() const
Returns the URI's table name, escaped and quoted.
void setGeometryColumn(const QString &geometryColumn)
Sets geometry column name to geometryColumn.
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 connectionInfo(bool expandAuthConfig=true) const
Returns the connection part of the URI.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
void setUsername(const QString &username)
Sets the username for the URI.
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
void disableSelectAtId(bool flag)
Set to true to disable selection by feature ID.
bool selectAtIdDisabled() const
Returns whether the selection by feature ID is disabled.
void setHost(const QString &host)
Sets the host 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.
QString username() const
Returns the username stored in the URI.
void setService(const QString &service)
Sets the service name associated with the URI.
static QString encodeSslMode(SslMode sslMode)
Encodes SSL mode enum value into a string.
Qgis::WkbType wkbType() const
Returns the WKB type associated with the URI.
void setSslMode(SslMode mode)
Sets the SSL mode associated with the URI.
void setWkbType(Qgis::WkbType type)
Sets the WKB type associated with the URI.
QString driver() const
Returns the driver name stored in the URI.
QString host() const
Returns the host name stored in the URI.
void setPort(const QString &port)
Sets the port stored in the URI.
bool operator==(const QgsDataSourceUri &other) const
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
QString service() const
Returns the service name associated with the URI.
void setKeyColumn(const QString &column)
Sets the name of the (primary) key column.
bool useEstimatedMetadata() const
Returns true if estimated metadata should be used for the connection.
SslMode sslMode() const
Returns the SSL mode associated with the URI.
QString password() const
Returns the password stored in the URI.
QString keyColumn() const
Returns the name of the (primary) key column for the referenced table.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
QString port() const
Returns the port stored in the URI.
QSet< QString > parameterKeys() const
Returns parameter keys used in the uri: specialized ones ("table", "schema", etc.) or generic paramet...
QString database() const
Returns the database name stored in the URI.
void clearSchema()
Clears the schema stored in the URI.
void setDriver(const QString &driver)
Sets the driver name stored in the URI.
static QString removePassword(const QString &aUri, bool hide=false)
Removes the password element from a URI.
void setDatabase(const QString &database)
Sets the URI database name.
bool operator!=(const QgsDataSourceUri &other) const
QString geometryColumn() const
Returns the name of the geometry column stored in the URI, if set.
void setSrid(const QString &srid)
Sets the spatial reference ID associated with the URI.
QString sql() const
Returns the SQL filter stored in the URI, if set.
void setPassword(const QString &password)
Sets the password for the URI.
static const QString PARAM_PREFIX
Used in uri to pass headers as params.
static const QString KEY_REFERER
Used in settings as the referer key.
static Qgis::WkbType parseType(const QString &wktStr)
Attempts to extract the WKB type from a WKT string.
static Q_INVOKABLE QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
QByteArray toLatin1_helper(const QString &string)
#define HIDING_TOKEN
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59