QGIS API Documentation 3.41.0-Master (25ec5511245)
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
351{
352 return mPassword;
353}
354
355void QgsDataSourceUri::setPassword( const QString &password )
356{
357 mPassword = password;
358}
359
361{
362 return mPort;
363}
364
366{
367 return mDriver;
368}
369
371{
372 return mSSLmode;
373}
374
376{
377 return mSchema;
378}
379
381{
382 return mTable;
383}
384
386{
387 return mSql;
388}
389
391{
392 return mGeometryColumn;
393}
394
396{
397 return mKeyColumn;
398}
399
400
401void QgsDataSourceUri::setDriver( const QString &driver )
402{
403 mDriver = driver;
404}
405
406
407void QgsDataSourceUri::setKeyColumn( const QString &column )
408{
409 mKeyColumn = column;
410}
411
412
414{
415 mUseEstimatedMetadata = flag;
416}
417
419{
420 return mUseEstimatedMetadata;
421}
422
424{
425 mSelectAtIdDisabledSet = true;
426 mSelectAtIdDisabled = flag;
427}
428
430{
431 return mSelectAtIdDisabled;
432}
433
434void QgsDataSourceUri::setSql( const QString &sql )
435{
436 mSql = sql;
437}
438
440{
441 mSchema.clear();
442}
443
444void QgsDataSourceUri::setSchema( const QString &schema )
445{
446 mSchema = schema;
447}
448
449QString QgsDataSourceUri::escape( const QString &val, QChar delim = '\'' ) const
450{
451 QString escaped = val;
452
453 escaped.replace( '\\', QLatin1String( "\\\\" ) );
454 escaped.replace( delim, QStringLiteral( "\\%1" ).arg( delim ) );
455
456 return escaped;
457}
458
459void QgsDataSourceUri::setGeometryColumn( const QString &geometryColumn )
460{
461 mGeometryColumn = geometryColumn;
462}
463
464void QgsDataSourceUri::setTable( const QString &table )
465{
466 mTable = table;
467}
468
469void QgsDataSourceUri::skipBlanks( const QString &uri, int &i )
470{
471 // skip space before value
472 while ( i < uri.length() && uri[i].isSpace() )
473 i++;
474}
475
476QString QgsDataSourceUri::getValue( const QString &uri, int &i )
477{
478 skipBlanks( uri, i );
479
480 // Get the parameter value
481 QString pval;
482 if ( i < uri.length() && ( uri[i] == '\'' || uri[i] == '"' ) )
483 {
484 const QChar delim = uri[i];
485
486 i++;
487
488 // value is quoted
489 for ( ;; )
490 {
491 if ( i == uri.length() )
492 {
493 QgsDebugError( QStringLiteral( "unterminated quoted string in connection info string" ) );
494 return pval;
495 }
496
497 if ( uri[i] == '\\' )
498 {
499 i++;
500 if ( i == uri.length() )
501 continue;
502 if ( uri[i] != delim && uri[i] != '\\' )
503 i--;
504 }
505 else if ( uri[i] == delim )
506 {
507 i++;
508 break;
509 }
510
511 pval += uri[i++];
512 }
513 }
514 else
515 {
516 // value is not quoted
517 while ( i < uri.length() )
518 {
519 if ( uri[i].isSpace() )
520 {
521 // end of value
522 break;
523 }
524
525 if ( uri[i] == '\\' )
526 {
527 i++;
528 if ( i == uri.length() )
529 break;
530 if ( uri[i] != '\\' && uri[i] != '\'' )
531 i--;
532 }
533
534 pval += uri[i++];
535 }
536 }
537
538 skipBlanks( uri, i );
539
540 return pval;
541}
542
543QString QgsDataSourceUri::connectionInfo( bool expandAuthConfig ) const
544{
545 QStringList connectionItems;
546
547 if ( !mDatabase.isEmpty() )
548 {
549 connectionItems << "dbname='" + escape( mDatabase ) + '\'';
550 }
551
552 if ( !mService.isEmpty() )
553 {
554 connectionItems << "service='" + escape( mService ) + '\'';
555 }
556 else if ( !mHost.isEmpty() )
557 {
558 connectionItems << "host=" + mHost;
559 }
560
561 if ( mService.isEmpty() )
562 {
563 if ( !mPort.isEmpty() )
564 connectionItems << "port=" + mPort;
565 }
566
567 if ( !mDriver.isEmpty() )
568 {
569 connectionItems << "driver='" + escape( mDriver ) + '\'';
570 }
571
572 if ( !mUsername.isEmpty() )
573 {
574 connectionItems << "user='" + escape( mUsername ) + '\'';
575
576 if ( !mPassword.isEmpty() )
577 {
578 connectionItems << "password='" + escape( mPassword ) + '\'';
579 }
580 }
581
582 if ( mSSLmode != SslPrefer ) // no need to output the default
583 {
584 connectionItems << QStringLiteral( "sslmode=" ) + encodeSslMode( mSSLmode );
585 }
586
587 if ( !mAuthConfigId.isEmpty() )
588 {
589 if ( expandAuthConfig )
590 {
591 if ( !QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, mAuthConfigId ) )
592 {
593 QgsDebugError( QStringLiteral( "Data source URI FAILED to update via loading configuration ID '%1'" ).arg( mAuthConfigId ) );
594 }
595 }
596 else
597 {
598 connectionItems << "authcfg=" + mAuthConfigId;
599 }
600 }
601
602 return connectionItems.join( QLatin1Char( ' ' ) );
603}
604
605QString QgsDataSourceUri::uri( bool expandAuthConfig ) const
606{
607 QString uri = connectionInfo( expandAuthConfig );
608
609 if ( !mKeyColumn.isEmpty() )
610 {
611 uri += QStringLiteral( " key='%1'" ).arg( escape( mKeyColumn ) );
612 }
613
614 if ( mUseEstimatedMetadata )
615 {
616 uri += QLatin1String( " estimatedmetadata=true" );
617 }
618
619 if ( !mSrid.isEmpty() )
620 {
621 uri += QStringLiteral( " srid=%1" ).arg( mSrid );
622 }
623
624 if ( mWkbType != Qgis::WkbType::Unknown && mWkbType != Qgis::WkbType::NoGeometry )
625 {
626 uri += QLatin1String( " type=" );
627 uri += QgsWkbTypes::displayString( mWkbType );
628 }
629
630 if ( mSelectAtIdDisabled )
631 {
632 uri += QLatin1String( " selectatid=false" );
633 }
634
635 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
636 {
637 if ( it.key().contains( '=' ) || it.key().contains( ' ' ) )
638 {
639 QgsDebugError( QStringLiteral( "invalid uri parameter %1 skipped" ).arg( it.key() ) );
640 continue;
641 }
642
643 uri += ' ' + it.key() + "='" + escape( it.value() ) + '\'';
644 }
645
646 uri += mHttpHeaders.toSpacedString();
647
648 QString columnName( mGeometryColumn );
649 columnName.replace( '\\', QLatin1String( "\\\\" ) );
650 columnName.replace( ')', QLatin1String( "\\)" ) );
651
652 if ( !mTable.isEmpty() )
653 {
654 uri += QStringLiteral( " table=%1%2" )
655 .arg( quotedTablename(),
656 mGeometryColumn.isEmpty() ? QString() : QStringLiteral( " (%1)" ).arg( columnName ) );
657 }
658 else if ( !mSchema.isEmpty() )
659 {
660 uri += QStringLiteral( " schema='%1'" ).arg( escape( mSchema ) );
661 }
662
663 if ( !mSql.isEmpty() )
664 {
665 uri += QStringLiteral( " sql=" ) + mSql;
666 }
667
668 return uri;
669}
670
671// from qurl.h
672QByteArray toLatin1_helper( const QString &string )
673{
674 if ( string.isEmpty() )
675 return string.isNull() ? QByteArray() : QByteArray( "" );
676 return string.toLatin1();
677}
678
680{
681 QUrlQuery url;
682 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); ++it )
683 {
684 url.addQueryItem( it.key(), it.value() );
685 }
686
687 if ( !mUsername.isEmpty() )
688 url.addQueryItem( QStringLiteral( "username" ), mUsername );
689
690 if ( !mPassword.isEmpty() )
691 url.addQueryItem( QStringLiteral( "password" ), mPassword );
692
693 if ( !mAuthConfigId.isEmpty() )
694 url.addQueryItem( QStringLiteral( "authcfg" ), mAuthConfigId );
695
696 mHttpHeaders.updateUrlQuery( url );
697
698 return toLatin1_helper( url.toString( QUrl::FullyEncoded ) );
699}
700
701void QgsDataSourceUri::setEncodedUri( const QByteArray &uri )
702{
703 mParams.clear();
704 mUsername.clear();
705 mPassword.clear();
706 mAuthConfigId.clear();
707
708 QUrl url;
709 url.setQuery( QString::fromLatin1( uri ) );
710 const QUrlQuery query( url );
711
712 mHttpHeaders.setFromUrlQuery( query );
713
714 const auto constQueryItems = query.queryItems();
715 for ( const QPair<QString, QString> &item : constQueryItems )
716 {
717 if ( !item.first.startsWith( QgsHttpHeaders::PARAM_PREFIX ) && item.first != QgsHttpHeaders::KEY_REFERER )
718 {
719 if ( item.first == QLatin1String( "username" ) )
720 mUsername = query.queryItemValue( QStringLiteral( "username" ), QUrl::ComponentFormattingOption::FullyDecoded );
721 else if ( item.first == QLatin1String( "password" ) )
722 mPassword = query.queryItemValue( QStringLiteral( "password" ), QUrl::ComponentFormattingOption::FullyDecoded );
723 else if ( item.first == QLatin1String( "authcfg" ) )
724 mAuthConfigId = query.queryItemValue( QStringLiteral( "authcfg" ), QUrl::ComponentFormattingOption::FullyDecoded );
725 else
726 mParams.insert( item.first, item.second );
727 }
728 }
729}
730
731void QgsDataSourceUri::setEncodedUri( const QString &uri )
732{
733 QUrl url;
734 url.setQuery( uri );
735 setEncodedUri( url.query( QUrl::EncodeUnicode ).toLatin1() );
736}
737
739{
740 if ( !mSchema.isEmpty() )
741 return QStringLiteral( "\"%1\".\"%2\"" )
742 .arg( escape( mSchema, '"' ),
743 escape( mTable, '"' ) );
744 else
745 return QStringLiteral( "\"%1\"" )
746 .arg( escape( mTable, '"' ) );
747}
748
749void QgsDataSourceUri::setConnection( const QString &host,
750 const QString &port,
751 const QString &database,
752 const QString &username,
753 const QString &password,
754 SslMode sslmode,
755 const QString &authConfigId )
756{
757 mHost = host;
758 mDatabase = database;
759 mPort = port;
760 mUsername = username;
761 mPassword = password;
762 mSSLmode = sslmode;
763 mAuthConfigId = authConfigId;
764}
765
766void QgsDataSourceUri::setConnection( const QString &service,
767 const QString &database,
768 const QString &username,
769 const QString &password,
770 SslMode sslmode,
771 const QString &authConfigId )
772{
773 mService = service;
774 mDatabase = database;
775 mUsername = username;
776 mPassword = password;
777 mSSLmode = sslmode;
778 mAuthConfigId = authConfigId;
779}
780
781void QgsDataSourceUri::setDataSource( const QString &schema,
782 const QString &table,
783 const QString &geometryColumn,
784 const QString &sql,
785 const QString &keyColumn )
786{
787 mSchema = schema;
788 mTable = table;
789 mGeometryColumn = geometryColumn;
790 mSql = sql;
791 mKeyColumn = keyColumn;
792}
793
794void QgsDataSourceUri::setAuthConfigId( const QString &authcfg )
795{
796 mAuthConfigId = authcfg;
797}
798
799void QgsDataSourceUri::setDatabase( const QString &database )
800{
801 mDatabase = database;
802}
803
805{
806 return mWkbType;
807}
808
810{
811 mWkbType = wkbType;
812}
813
815{
816 return mSrid;
817}
818
819void QgsDataSourceUri::setSrid( const QString &srid )
820{
821 mSrid = srid;
822}
823
825{
826 if ( sslMode == QLatin1String( "prefer" ) )
827 return SslPrefer;
828 else if ( sslMode == QLatin1String( "disable" ) )
829 return SslDisable;
830 else if ( sslMode == QLatin1String( "allow" ) )
831 return SslAllow;
832 else if ( sslMode == QLatin1String( "require" ) )
833 return SslRequire;
834 else if ( sslMode == QLatin1String( "verify-ca" ) )
835 return SslVerifyCa;
836 else if ( sslMode == QLatin1String( "verify-full" ) )
837 return SslVerifyFull;
838 else
839 return SslPrefer; // default
840}
841
843{
844 switch ( sslMode )
845 {
846 case SslPrefer: return QStringLiteral( "prefer" );
847 case SslDisable: return QStringLiteral( "disable" );
848 case SslAllow: return QStringLiteral( "allow" );
849 case SslRequire: return QStringLiteral( "require" );
850 case SslVerifyCa: return QStringLiteral( "verify-ca" );
851 case SslVerifyFull: return QStringLiteral( "verify-full" );
852 }
853 return QString();
854}
855
856void QgsDataSourceUri::setParam( const QString &key, const QString &value )
857{
858 // maintain old API
859 if ( key == QLatin1String( "username" ) )
860 mUsername = value;
861 else if ( key == QLatin1String( "password" ) )
862 mPassword = value;
863 else if ( key == QLatin1String( "authcfg" ) )
864 mAuthConfigId = value;
865 else
866 {
867 // may be multiple
868 mParams.insert( key, value );
869 }
870}
871
872void QgsDataSourceUri::setParam( const QString &key, const QStringList &value )
873{
874 for ( const QString &val : value )
875 {
876 setParam( key, val );
877 }
878}
879
880int QgsDataSourceUri::removeParam( const QString &key )
881{
882 if ( key == QLatin1String( "username" ) && !mUsername.isEmpty() )
883 {
884 mUsername.clear();
885 return 1;
886 }
887 else if ( key == QLatin1String( "password" ) && !mPassword.isEmpty() )
888 {
889 mPassword.clear();
890 return 1;
891 }
892 else if ( key == QLatin1String( "authcfg" ) && !mAuthConfigId.isEmpty() )
893 {
894 mAuthConfigId.clear();
895 return 1;
896 }
897
898 return mParams.remove( key );
899}
900
901QString QgsDataSourceUri::param( const QString &key ) const
902{
903 // maintain old api
904 if ( key == QLatin1String( "username" ) && !mUsername.isEmpty() )
905 return mUsername;
906 else if ( key == QLatin1String( "password" ) && !mPassword.isEmpty() )
907 return mPassword;
908 else if ( key == QLatin1String( "authcfg" ) && !mAuthConfigId.isEmpty() )
909 return mAuthConfigId;
910
911 return mParams.value( key );
912}
913
914QStringList QgsDataSourceUri::params( const QString &key ) const
915{
916 // maintain old api
917 if ( key == QLatin1String( "username" ) && !mUsername.isEmpty() )
918 return QStringList() << mUsername;
919 else if ( key == QLatin1String( "password" ) && !mPassword.isEmpty() )
920 return QStringList() << mPassword;
921 else if ( key == QLatin1String( "authcfg" ) && !mAuthConfigId.isEmpty() )
922 return QStringList() << mAuthConfigId;
923
924 return mParams.values( key );
925}
926
927bool QgsDataSourceUri::hasParam( const QString &key ) const
928{
929 // maintain old api
930 if ( key == QLatin1String( "username" ) && !mUsername.isEmpty() )
931 return true;
932 else if ( key == QLatin1String( "password" ) && !mPassword.isEmpty() )
933 return true;
934 else if ( key == QLatin1String( "authcfg" ) && !mAuthConfigId.isEmpty() )
935 return true;
936
937 return mParams.contains( key );
938}
939
941{
942 QSet<QString> paramKeys;
943 for ( auto it = mParams.constBegin(); it != mParams.constEnd(); it++ )
944 paramKeys.insert( it.key() );
945
946 if ( !mHost.isEmpty() )
947 paramKeys.insert( QLatin1String( "host" ) );
948 if ( !mPort.isEmpty() )
949 paramKeys.insert( QLatin1String( "port" ) );
950 if ( !mDriver.isEmpty() )
951 paramKeys.insert( QLatin1String( "driver" ) );
952 if ( !mService.isEmpty() )
953 paramKeys.insert( QLatin1String( "service" ) );
954 if ( !mDatabase.isEmpty() )
955 paramKeys.insert( QLatin1String( "dbname" ) );
956 if ( !mSchema.isEmpty() )
957 paramKeys.insert( QLatin1String( "schema" ) );
958 if ( !mTable.isEmpty() )
959 paramKeys.insert( QLatin1String( "table" ) );
960 // Ignore mGeometryColumn: not a key ==> embedded in table value
961 if ( !mSql.isEmpty() )
962 paramKeys.insert( QLatin1String( "sql" ) );
963 if ( !mAuthConfigId.isEmpty() )
964 paramKeys.insert( QLatin1String( "authcfg" ) );
965 if ( !mUsername.isEmpty() )
966 paramKeys.insert( QLatin1String( "username" ) );
967 if ( !mPassword.isEmpty() )
968 paramKeys.insert( QLatin1String( "password" ) );
969 if ( mSSLmode != SslPrefer )
970 paramKeys.insert( QLatin1String( "sslmode" ) );
971 if ( !mKeyColumn.isEmpty() )
972 paramKeys.insert( QLatin1String( "key" ) );
973 if ( mUseEstimatedMetadata )
974 paramKeys.insert( QLatin1String( "estimatedmetadata" ) );
975 if ( mSelectAtIdDisabledSet )
976 paramKeys.insert( QLatin1String( "selectatid" ) );
977 if ( mWkbType != Qgis::WkbType::Unknown )
978 paramKeys.insert( QLatin1String( "type" ) );
979 if ( !mSrid.isEmpty() )
980 paramKeys.insert( QLatin1String( "srid" ) );
981 return paramKeys;
982}
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