QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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 ) && item.first != QgsHttpHeaders::KEY_REFERER )
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 QUrl url;
733 url.setQuery( uri );
734 setEncodedUri( url.query( QUrl::EncodeUnicode ).toLatin1() );
735}
736
738{
739 if ( !mSchema.isEmpty() )
740 return QStringLiteral( "\"%1\".\"%2\"" )
741 .arg( escape( mSchema, '"' ),
742 escape( mTable, '"' ) );
743 else
744 return QStringLiteral( "\"%1\"" )
745 .arg( escape( mTable, '"' ) );
746}
747
748void QgsDataSourceUri::setConnection( const QString &host,
749 const QString &port,
750 const QString &database,
751 const QString &username,
752 const QString &password,
753 SslMode sslmode,
754 const QString &authConfigId )
755{
756 mHost = host;
757 mDatabase = database;
758 mPort = port;
759 mUsername = username;
760 mPassword = password;
761 mSSLmode = sslmode;
762 mAuthConfigId = authConfigId;
763}
764
765void QgsDataSourceUri::setConnection( const QString &service,
766 const QString &database,
767 const QString &username,
768 const QString &password,
769 SslMode sslmode,
770 const QString &authConfigId )
771{
772 mService = service;
773 mDatabase = database;
774 mUsername = username;
775 mPassword = password;
776 mSSLmode = sslmode;
777 mAuthConfigId = authConfigId;
778}
779
780void QgsDataSourceUri::setDataSource( const QString &schema,
781 const QString &table,
782 const QString &geometryColumn,
783 const QString &sql,
784 const QString &keyColumn )
785{
786 mSchema = schema;
787 mTable = table;
788 mGeometryColumn = geometryColumn;
789 mSql = sql;
790 mKeyColumn = keyColumn;
791}
792
793void QgsDataSourceUri::setAuthConfigId( const QString &authcfg )
794{
795 mAuthConfigId = authcfg;
796}
797
798void QgsDataSourceUri::setDatabase( const QString &database )
799{
800 mDatabase = database;
801}
802
804{
805 return mWkbType;
806}
807
809{
810 mWkbType = wkbType;
811}
812
814{
815 return mSrid;
816}
817
818void QgsDataSourceUri::setSrid( const QString &srid )
819{
820 mSrid = srid;
821}
822
824{
825 if ( sslMode == QLatin1String( "prefer" ) )
826 return SslPrefer;
827 else if ( sslMode == QLatin1String( "disable" ) )
828 return SslDisable;
829 else if ( sslMode == QLatin1String( "allow" ) )
830 return SslAllow;
831 else if ( sslMode == QLatin1String( "require" ) )
832 return SslRequire;
833 else if ( sslMode == QLatin1String( "verify-ca" ) )
834 return SslVerifyCa;
835 else if ( sslMode == QLatin1String( "verify-full" ) )
836 return SslVerifyFull;
837 else
838 return SslPrefer; // default
839}
840
842{
843 switch ( sslMode )
844 {
845 case SslPrefer: return QStringLiteral( "prefer" );
846 case SslDisable: return QStringLiteral( "disable" );
847 case SslAllow: return QStringLiteral( "allow" );
848 case SslRequire: return QStringLiteral( "require" );
849 case SslVerifyCa: return QStringLiteral( "verify-ca" );
850 case SslVerifyFull: return QStringLiteral( "verify-full" );
851 }
852 return QString();
853}
854
855void QgsDataSourceUri::setParam( const QString &key, const QString &value )
856{
857 // maintain old API
858 if ( key == QLatin1String( "username" ) )
859 mUsername = value;
860 else if ( key == QLatin1String( "password" ) )
861 mPassword = value;
862 else if ( key == QLatin1String( "authcfg" ) )
863 mAuthConfigId = value;
864 else
865 {
866 // may be multiple
867 mParams.insert( key, value );
868 }
869}
870
871void QgsDataSourceUri::setParam( const QString &key, const QStringList &value )
872{
873 for ( const QString &val : value )
874 {
875 setParam( key, val );
876 }
877}
878
879int QgsDataSourceUri::removeParam( const QString &key )
880{
881 if ( key == QLatin1String( "username" ) && !mUsername.isEmpty() )
882 {
883 mUsername.clear();
884 return 1;
885 }
886 else if ( key == QLatin1String( "password" ) && !mPassword.isEmpty() )
887 {
888 mPassword.clear();
889 return 1;
890 }
891 else if ( key == QLatin1String( "authcfg" ) && !mAuthConfigId.isEmpty() )
892 {
893 mAuthConfigId.clear();
894 return 1;
895 }
896
897 return mParams.remove( key );
898}
899
900QString QgsDataSourceUri::param( const QString &key ) const
901{
902 // maintain old api
903 if ( key == QLatin1String( "username" ) && !mUsername.isEmpty() )
904 return mUsername;
905 else if ( key == QLatin1String( "password" ) && !mPassword.isEmpty() )
906 return mPassword;
907 else if ( key == QLatin1String( "authcfg" ) && !mAuthConfigId.isEmpty() )
908 return mAuthConfigId;
909
910 return mParams.value( key );
911}
912
913QStringList QgsDataSourceUri::params( const QString &key ) const
914{
915 // maintain old api
916 if ( key == QLatin1String( "username" ) && !mUsername.isEmpty() )
917 return QStringList() << mUsername;
918 else if ( key == QLatin1String( "password" ) && !mPassword.isEmpty() )
919 return QStringList() << mPassword;
920 else if ( key == QLatin1String( "authcfg" ) && !mAuthConfigId.isEmpty() )
921 return QStringList() << mAuthConfigId;
922
923 return mParams.values( key );
924}
925
926bool QgsDataSourceUri::hasParam( const QString &key ) const
927{
928 // maintain old api
929 if ( key == QLatin1String( "username" ) && !mUsername.isEmpty() )
930 return true;
931 else if ( key == QLatin1String( "password" ) && !mPassword.isEmpty() )
932 return true;
933 else if ( key == QLatin1String( "authcfg" ) && !mAuthConfigId.isEmpty() )
934 return true;
935
936 return mParams.contains( key );
937}
938
940{
941 QSet<QString> paramKeys;
942 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); it++ )
943 paramKeys.insert( it.key() );
944
945 if ( !mHost.isEmpty() )
946 paramKeys.insert( QLatin1String( "host" ) );
947 if ( !mPort.isEmpty() )
948 paramKeys.insert( QLatin1String( "port" ) );
949 if ( !mDriver.isEmpty() )
950 paramKeys.insert( QLatin1String( "driver" ) );
951 if ( !mService.isEmpty() )
952 paramKeys.insert( QLatin1String( "service" ) );
953 if ( !mDatabase.isEmpty() )
954 paramKeys.insert( QLatin1String( "dbname" ) );
955 if ( !mSchema.isEmpty() )
956 paramKeys.insert( QLatin1String( "schema" ) );
957 if ( !mTable.isEmpty() )
958 paramKeys.insert( QLatin1String( "table" ) );
959 // Ignore mGeometryColumn: not a key ==> embedded in table value
960 if ( !mSql.isEmpty() )
961 paramKeys.insert( QLatin1String( "sql" ) );
962 if ( !mAuthConfigId.isEmpty() )
963 paramKeys.insert( QLatin1String( "authcfg" ) );
964 if ( !mUsername.isEmpty() )
965 paramKeys.insert( QLatin1String( "username" ) );
966 if ( !mPassword.isEmpty() )
967 paramKeys.insert( QLatin1String( "password" ) );
968 if ( mSSLmode != SslPrefer )
969 paramKeys.insert( QLatin1String( "sslmode" ) );
970 if ( !mKeyColumn.isEmpty() )
971 paramKeys.insert( QLatin1String( "key" ) );
972 if ( mUseEstimatedMetadata )
973 paramKeys.insert( QLatin1String( "estimatedmetadata" ) );
974 if ( mSelectAtIdDisabledSet )
975 paramKeys.insert( QLatin1String( "selectatid" ) );
976 if ( mWkbType != Qgis::WkbType::Unknown )
977 paramKeys.insert( QLatin1String( "type" ) );
978 if ( !mSrid.isEmpty() )
979 paramKeys.insert( QLatin1String( "srid" ) );
980 return paramKeys;
981}
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.
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.
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:39
#define QgsDebugError(str)
Definition qgslogger.h:38