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