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