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