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