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