QGIS API Documentation 4.3.0-Master (bf28115e945)
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 if ( aUri.contains( " password='"_L1 ) )
247 {
248 regexp.setPattern( u" password='[^']*'"_s );
249 }
250 else
251 {
252 // InvertedGreedinessOption doesn't work here, disable it
253 regexp.setPatternOptions( QRegularExpression::NoPatternOption );
254 regexp.setPattern( u" password=[^ ]*"_s );
255 }
256
257 if ( hide )
258 {
259 safeName.replace( regexp, u" password=%1"_s.arg( HIDING_TOKEN ) );
260 }
261 else
262 {
263 safeName.replace( regexp, u""_s );
264 }
265 }
266 else if ( aUri.contains( ",password="_L1 ) )
267 {
268 regexp.setPattern( u",password=.*,"_s );
269
270 if ( hide )
271 {
272 safeName.replace( regexp, u",password=%1,"_s.arg( HIDING_TOKEN ) );
273 }
274 else
275 {
276 safeName.replace( regexp, u","_s );
277 }
278 }
279 else if ( aUri.contains( "IDB:"_L1 ) )
280 {
281 regexp.setPattern( u" pass=.* "_s );
282
283 if ( hide )
284 {
285 safeName.replace( regexp, u" pass=%1 "_s.arg( HIDING_TOKEN ) );
286 }
287 else
288 {
289 safeName.replace( regexp, u" "_s );
290 }
291 }
292 else if ( ( aUri.contains( "OCI:"_L1 ) ) || ( aUri.contains( "ODBC:"_L1 ) ) )
293 {
294 regexp.setPattern( u"/.*@"_s );
295
296 if ( hide )
297 {
298 safeName.replace( regexp, u"/%1@"_s.arg( HIDING_TOKEN ) );
299 }
300 else
301 {
302 safeName.replace( regexp, u"/@"_s );
303 }
304 }
305 else if ( aUri.contains( "postgresql:"_L1 ) )
306 {
307 // postgresql://user:pwd@...
308 regexp.setPattern( u"/.*@"_s );
309 const QString matched = regexp.match( aUri ).captured();
310
311 QString anonymised = matched;
312 const QStringList items = matched.split( u":"_s );
313 if ( items.size() > 1 )
314 {
315 anonymised = matched.split( u":"_s )[0];
316 if ( hide )
317 {
318 anonymised.append( u":%1"_s.arg( HIDING_TOKEN ) );
319 }
320 anonymised.append( u"@"_s );
321 }
322
323 safeName.replace( regexp, anonymised );
324 }
325 else if ( aUri.contains( "SDE:"_L1 ) )
326 {
327 QStringList strlist = aUri.split( ',' );
328 safeName = strlist[0] + ',' + strlist[1] + ',' + strlist[2] + ',' + strlist[3];
329 }
330 return safeName;
331}
332
334{
335 return mAuthConfigId;
336}
337
339{
340 return mUsername;
341}
342
344{
345 mUsername = username;
346}
347
349{
350 return mService;
351}
352
354{
355 return mHost;
356}
357
359{
360 return mDatabase;
361}
362
363void QgsDataSourceUri::setPort( const QString &port )
364{
365 mPort = port;
366}
367
369{
370 return mPassword;
371}
372
374{
375 mSSLmode = mode;
376}
377
379{
380 mService = service;
381}
382
384{
385 mPassword = password;
386}
387
389{
390 return mPort;
391}
392
394{
395 return mDriver;
396}
397
399{
400 return mSSLmode;
401}
402
404{
405 return mSchema;
406}
407
409{
410 return mTable;
411}
412
414{
415 return mSql;
416}
417
419{
420 return mGeometryColumn;
421}
422
424{
425 return mKeyColumn;
426}
427
428
430{
431 mDriver = driver;
432}
433
434
435void QgsDataSourceUri::setKeyColumn( const QString &column )
436{
437 mKeyColumn = column;
438}
439
440
442{
443 mUseEstimatedMetadata = flag;
444}
445
447{
448 return mUseEstimatedMetadata;
449}
450
452{
453 mSelectAtIdDisabledSet = true;
454 mSelectAtIdDisabled = flag;
455}
456
458{
459 return mSelectAtIdDisabled;
460}
461
462void QgsDataSourceUri::setSql( const QString &sql )
463{
464 mSql = sql;
465}
466
467void QgsDataSourceUri::setHost( const QString &host )
468{
469 mHost = host;
470}
471
473{
474 mSchema.clear();
475}
476
478{
479 mSchema = schema;
480}
481
482QString QgsDataSourceUri::escape( const QString &val, QChar delim = '\'' ) const
483{
484 QString escaped = val;
485
486 escaped.replace( '\\', "\\\\"_L1 );
487 escaped.replace( delim, u"\\%1"_s.arg( delim ) );
488
489 return escaped;
490}
491
493{
494 mGeometryColumn = geometryColumn;
495}
496
497void QgsDataSourceUri::setTable( const QString &table )
498{
499 mTable = table;
500}
501
502void QgsDataSourceUri::skipBlanks( const QString &uri, int &i )
503{
504 // skip space before value
505 while ( i < uri.length() && uri[i].isSpace() )
506 i++;
507}
508
509QString QgsDataSourceUri::getValue( const QString &uri, int &i )
510{
511 skipBlanks( uri, i );
512
513 // Get the parameter value
514 QString pval;
515 if ( i < uri.length() && ( uri[i] == '\'' || uri[i] == '"' ) )
516 {
517 const QChar delim = uri[i];
518
519 i++;
520
521 // value is quoted
522 for ( ;; )
523 {
524 if ( i == uri.length() )
525 {
526 QgsDebugError( u"unterminated quoted string in connection info string"_s );
527 return pval;
528 }
529
530 if ( uri[i] == '\\' )
531 {
532 i++;
533 if ( i == uri.length() )
534 continue;
535 if ( uri[i] != delim && uri[i] != '\\' )
536 i--;
537 }
538 else if ( uri[i] == delim )
539 {
540 i++;
541 break;
542 }
543
544 pval += uri[i++];
545 }
546 }
547 else
548 {
549 // value is not quoted
550 while ( i < uri.length() )
551 {
552 if ( uri[i].isSpace() )
553 {
554 // end of value
555 break;
556 }
557
558 if ( uri[i] == '\\' )
559 {
560 i++;
561 if ( i == uri.length() )
562 break;
563 if ( uri[i] != '\\' && uri[i] != '\'' )
564 i--;
565 }
566
567 pval += uri[i++];
568 }
569 }
570
571 skipBlanks( uri, i );
572
573 return pval;
574}
575
576QString QgsDataSourceUri::connectionInfo( bool expandAuthConfig ) const
577{
578 QStringList connectionItems;
579
580 if ( !mDatabase.isEmpty() )
581 {
582 connectionItems << "dbname='" + escape( mDatabase ) + '\'';
583 }
584
585 if ( !mService.isEmpty() )
586 {
587 connectionItems << "service='" + escape( mService ) + '\'';
588 }
589 else if ( !mHost.isEmpty() )
590 {
591 connectionItems << "host=" + mHost;
592 }
593
594 if ( mService.isEmpty() )
595 {
596 if ( !mPort.isEmpty() )
597 connectionItems << "port=" + mPort;
598 }
599
600 if ( !mDriver.isEmpty() )
601 {
602 connectionItems << "driver='" + escape( mDriver ) + '\'';
603 }
604
605 if ( !mUsername.isEmpty() )
606 {
607 connectionItems << "user='" + escape( mUsername ) + '\'';
608
609 if ( !mPassword.isEmpty() )
610 {
611 connectionItems << "password='" + escape( mPassword ) + '\'';
612 }
613 }
614
615 if ( mSSLmode != SslPrefer ) // no need to output the default
616 {
617 connectionItems << u"sslmode="_s + encodeSslMode( mSSLmode );
618 }
619
620 if ( !mAuthConfigId.isEmpty() )
621 {
622 if ( expandAuthConfig )
623 {
624 if ( !QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, mAuthConfigId ) )
625 {
626 QgsDebugError( u"Data source URI FAILED to update via loading configuration ID '%1'"_s.arg( mAuthConfigId ) );
627 }
628 }
629 else
630 {
631 connectionItems << "authcfg=" + mAuthConfigId;
632 }
633 }
634
635 return connectionItems.join( ' '_L1 );
636}
637
638QString QgsDataSourceUri::uri( bool expandAuthConfig ) const
639{
640 QString uri = connectionInfo( expandAuthConfig );
641
642 if ( !mKeyColumn.isEmpty() )
643 {
644 uri += u" key='%1'"_s.arg( escape( mKeyColumn ) );
645 }
646
647 if ( mUseEstimatedMetadata )
648 {
649 uri += " estimatedmetadata=true"_L1;
650 }
651
652 if ( !mSrid.isEmpty() )
653 {
654 uri += u" srid=%1"_s.arg( mSrid );
655 }
656
657 if ( mWkbType != Qgis::WkbType::Unknown && mWkbType != Qgis::WkbType::NoGeometry )
658 {
659 uri += " type="_L1;
660 uri += QgsWkbTypes::displayString( mWkbType );
661 }
662
663 if ( mSelectAtIdDisabled )
664 {
665 uri += " selectatid=false"_L1;
666 }
667
668 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
669 {
670 if ( it.key().contains( '=' ) || it.key().contains( ' ' ) )
671 {
672 QgsDebugError( u"invalid uri parameter %1 skipped"_s.arg( it.key() ) );
673 continue;
674 }
675
676 uri += ' ' + it.key() + "='" + escape( it.value() ) + '\'';
677 }
678
679 uri += mHttpHeaders.toSpacedString();
680
681 QString columnName( mGeometryColumn );
682 columnName.replace( '\\', "\\\\"_L1 );
683 columnName.replace( ')', "\\)"_L1 );
684
685 if ( !mTable.isEmpty() )
686 {
687 uri += u" table=%1%2"_s.arg( quotedTablename(), mGeometryColumn.isEmpty() ? QString() : u" (%1)"_s.arg( columnName ) );
688 }
689 else if ( !mSchema.isEmpty() )
690 {
691 uri += u" schema='%1'"_s.arg( escape( mSchema ) );
692 }
693
694 if ( !mSql.isEmpty() )
695 {
696 uri += u" sql="_s + mSql;
697 }
698
699 return uri;
700}
701
702// from qurl.h
703QByteArray toLatin1_helper( const QString &string )
704{
705 if ( string.isEmpty() )
706 return string.isNull() ? QByteArray() : QByteArray( "" );
707 return string.toLatin1();
708}
709
711{
712 QUrlQuery url;
713 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
714 {
715 url.addQueryItem( it.key(), QUrl::toPercentEncoding( it.value() ) );
716 }
717
718 if ( !mUsername.isEmpty() )
719 url.addQueryItem( u"username"_s, QUrl::toPercentEncoding( mUsername ) );
720
721 if ( !mPassword.isEmpty() )
722 url.addQueryItem( u"password"_s, QUrl::toPercentEncoding( mPassword ) );
723
724 if ( !mAuthConfigId.isEmpty() )
725 url.addQueryItem( u"authcfg"_s, QUrl::toPercentEncoding( mAuthConfigId ) );
726
727 mHttpHeaders.updateUrlQuery( url );
728
729 return toLatin1_helper( url.toString( QUrl::FullyEncoded ) );
730}
731
732void QgsDataSourceUri::setEncodedUri( const QByteArray &uri )
733{
734 mParams.clear();
735 mUsername.clear();
736 mPassword.clear();
737 mAuthConfigId.clear();
738
739 QUrl url;
740 url.setQuery( QString::fromLatin1( uri ) );
741 const QUrlQuery query( url );
742
743 mHttpHeaders.setFromUrlQuery( query );
744
745 const auto constQueryItems = query.queryItems( QUrl::ComponentFormattingOption::FullyDecoded );
746 for ( const QPair<QString, QString> &item : constQueryItems )
747 {
748 if ( !item.first.startsWith( QgsHttpHeaders::PARAM_PREFIX ) && item.first != QgsHttpHeaders::KEY_REFERER )
749 {
750 if ( item.first == "username"_L1 )
751 mUsername = query.queryItemValue( u"username"_s, QUrl::ComponentFormattingOption::FullyDecoded );
752 else if ( item.first == "password"_L1 )
753 mPassword = query.queryItemValue( u"password"_s, QUrl::ComponentFormattingOption::FullyDecoded );
754 else if ( item.first == "authcfg"_L1 )
755 mAuthConfigId = query.queryItemValue( u"authcfg"_s, QUrl::ComponentFormattingOption::FullyDecoded );
756 else
757 mParams.insert( item.first, item.second );
758 }
759 }
760}
761
763{
764 QUrl url;
765 url.setQuery( uri );
766 setEncodedUri( url.query( QUrl::EncodeUnicode ).toLatin1() );
767}
768
770{
771 if ( !mSchema.isEmpty() )
772 return u"\"%1\".\"%2\""_s.arg( escape( mSchema, '"' ), escape( mTable, '"' ) );
773 else
774 return u"\"%1\""_s.arg( escape( mTable, '"' ) );
775}
776
777void QgsDataSourceUri::setConnection( const QString &host, const QString &port, const QString &database, const QString &username, const QString &password, SslMode sslmode, const QString &authConfigId )
778{
779 mHost = host;
780 mDatabase = database;
781 mPort = port;
782 mUsername = username;
783 mPassword = password;
784 mSSLmode = sslmode;
785 mAuthConfigId = authConfigId;
786}
787
788void QgsDataSourceUri::setConnection( const QString &service, const QString &database, const QString &username, const QString &password, SslMode sslmode, const QString &authConfigId )
789{
790 mService = service;
791 mDatabase = database;
792 mUsername = username;
793 mPassword = password;
794 mSSLmode = sslmode;
795 mAuthConfigId = authConfigId;
796}
797
798void QgsDataSourceUri::setDataSource( const QString &schema, const QString &table, const QString &geometryColumn, const QString &sql, const QString &keyColumn )
799{
800 mSchema = schema;
801 mTable = table;
802 mGeometryColumn = geometryColumn;
803 mSql = sql;
804 mKeyColumn = keyColumn;
805}
806
807void QgsDataSourceUri::setAuthConfigId( const QString &authcfg )
808{
809 mAuthConfigId = authcfg;
810}
811
813{
814 mDatabase = database;
815}
816
818{
819 return mWkbType;
820}
821
826
828{
829 return mSrid;
830}
831
832void QgsDataSourceUri::setSrid( const QString &srid )
833{
834 mSrid = srid;
835}
836
838{
839 if ( sslMode == "prefer"_L1 )
840 return SslPrefer;
841 else if ( sslMode == "disable"_L1 )
842 return SslDisable;
843 else if ( sslMode == "allow"_L1 )
844 return SslAllow;
845 else if ( sslMode == "require"_L1 )
846 return SslRequire;
847 else if ( sslMode == "verify-ca"_L1 )
848 return SslVerifyCa;
849 else if ( sslMode == "verify-full"_L1 )
850 return SslVerifyFull;
851 else
852 return SslPrefer; // default
853}
854
856{
857 switch ( sslMode )
858 {
859 case SslPrefer:
860 return u"prefer"_s;
861 case SslDisable:
862 return u"disable"_s;
863 case SslAllow:
864 return u"allow"_s;
865 case SslRequire:
866 return u"require"_s;
867 case SslVerifyCa:
868 return u"verify-ca"_s;
869 case SslVerifyFull:
870 return u"verify-full"_s;
871 }
872 return QString();
873}
874
875void QgsDataSourceUri::setParam( const QString &key, const QString &value )
876{
877 // maintain old API
878 if ( key == "username"_L1 )
879 mUsername = value;
880 else if ( key == "password"_L1 )
881 mPassword = value;
882 else if ( key == "authcfg"_L1 )
883 mAuthConfigId = value;
884 else
885 {
886 // may be multiple
887 mParams.insert( key, value );
888 }
889}
890
891void QgsDataSourceUri::setParam( const QString &key, const QStringList &value )
892{
893 for ( const QString &val : value )
894 {
895 setParam( key, val );
896 }
897}
898
899int QgsDataSourceUri::removeParam( const QString &key )
900{
901 if ( key == "username"_L1 && !mUsername.isEmpty() )
902 {
903 mUsername.clear();
904 return 1;
905 }
906 else if ( key == "password"_L1 && !mPassword.isEmpty() )
907 {
908 mPassword.clear();
909 return 1;
910 }
911 else if ( key == "authcfg"_L1 && !mAuthConfigId.isEmpty() )
912 {
913 mAuthConfigId.clear();
914 return 1;
915 }
916
917 return mParams.remove( key );
918}
919
920QString QgsDataSourceUri::param( const QString &key ) const
921{
922 // maintain old api
923 if ( key == "username"_L1 && !mUsername.isEmpty() )
924 return mUsername;
925 else if ( key == "password"_L1 && !mPassword.isEmpty() )
926 return mPassword;
927 else if ( key == "authcfg"_L1 && !mAuthConfigId.isEmpty() )
928 return mAuthConfigId;
929
930 return mParams.value( key );
931}
932
933QStringList QgsDataSourceUri::params( const QString &key ) const
934{
935 // maintain old api
936 if ( key == "username"_L1 && !mUsername.isEmpty() )
937 return QStringList() << mUsername;
938 else if ( key == "password"_L1 && !mPassword.isEmpty() )
939 return QStringList() << mPassword;
940 else if ( key == "authcfg"_L1 && !mAuthConfigId.isEmpty() )
941 return QStringList() << mAuthConfigId;
942
943 return mParams.values( key );
944}
945
946bool QgsDataSourceUri::hasParam( const QString &key ) const
947{
948 // maintain old api
949 if ( key == "username"_L1 && !mUsername.isEmpty() )
950 return true;
951 else if ( key == "password"_L1 && !mPassword.isEmpty() )
952 return true;
953 else if ( key == "authcfg"_L1 && !mAuthConfigId.isEmpty() )
954 return true;
955
956 return mParams.contains( key );
957}
958
960{
961 QSet<QString> paramKeys;
962 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); it++ )
963 paramKeys.insert( it.key() );
964
965 if ( !mHost.isEmpty() )
966 paramKeys.insert( "host"_L1 );
967 if ( !mPort.isEmpty() )
968 paramKeys.insert( "port"_L1 );
969 if ( !mDriver.isEmpty() )
970 paramKeys.insert( "driver"_L1 );
971 if ( !mService.isEmpty() )
972 paramKeys.insert( "service"_L1 );
973 if ( !mDatabase.isEmpty() )
974 paramKeys.insert( "dbname"_L1 );
975 if ( !mSchema.isEmpty() )
976 paramKeys.insert( "schema"_L1 );
977 if ( !mTable.isEmpty() )
978 paramKeys.insert( "table"_L1 );
979 // Ignore mGeometryColumn: not a key ==> embedded in table value
980 if ( !mSql.isEmpty() )
981 paramKeys.insert( "sql"_L1 );
982 if ( !mAuthConfigId.isEmpty() )
983 paramKeys.insert( "authcfg"_L1 );
984 if ( !mUsername.isEmpty() )
985 paramKeys.insert( "username"_L1 );
986 if ( !mPassword.isEmpty() )
987 paramKeys.insert( "password"_L1 );
988 if ( mSSLmode != SslPrefer )
989 paramKeys.insert( "sslmode"_L1 );
990 if ( !mKeyColumn.isEmpty() )
991 paramKeys.insert( "key"_L1 );
992 if ( mUseEstimatedMetadata )
993 paramKeys.insert( "estimatedmetadata"_L1 );
994 if ( mSelectAtIdDisabledSet )
995 paramKeys.insert( "selectatid"_L1 );
996 if ( mWkbType != Qgis::WkbType::Unknown )
997 paramKeys.insert( "type"_L1 );
998 if ( !mSrid.isEmpty() )
999 paramKeys.insert( "srid"_L1 );
1000 return paramKeys;
1001}
1002
1004{
1005 // cheap comparisons first:
1006 if ( mUseEstimatedMetadata != other.mUseEstimatedMetadata
1007 || mSelectAtIdDisabled != other.mSelectAtIdDisabled
1008 || mSelectAtIdDisabledSet != other.mSelectAtIdDisabledSet
1009 || mSSLmode != other.mSSLmode
1010 || mWkbType != other.mWkbType )
1011 {
1012 return false;
1013 }
1014
1015 if ( mHost != other.mHost
1016 || mPort != other.mPort
1017 || mDriver != other.mDriver
1018 || mService != other.mService
1019 || mDatabase != other.mDatabase
1020 || mSchema != other.mSchema
1021 || mTable != other.mTable
1022 || mGeometryColumn != other.mGeometryColumn
1023 || mSql != other.mSql
1024 || mAuthConfigId != other.mAuthConfigId
1025 || mUsername != other.mUsername
1026 || mPassword != other.mPassword
1027 || mKeyColumn != other.mKeyColumn
1028 || mSrid != other.mSrid
1029 || mParams != other.mParams
1030 || mHttpHeaders != other.mHttpHeaders )
1031 {
1032 return false;
1033 }
1034
1035 return true;
1036}
1037
1039{
1040 return !( *this == other );
1041}
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ NoGeometry
No geometry.
Definition qgis.h:312
@ Unknown
Unknown.
Definition qgis.h:295
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:80
#define QgsDebugError(str)
Definition qgslogger.h:71