QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgsogrhelperfunctions.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsogrhelperfunctions.cpp
3 helper functions to create ogr uris for database and protocol drivers
4 -------------------
5 begin : Mon Jan 2 2009
6 copyright : (C) 2009 by Godofredo Contreras Nava
7 email : frdcn at hotmail.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
20#include "qgslogger.h"
21#include "qgsapplication.h"
22#include "qgsauthmanager.h"
23#include <QRegExp>
24
25QString createDatabaseURI( const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig )
26{
27 QString uri;
28
29 // If an auth configuration is set, override username and password
30 // Note that only Basic auth (username/password) is for now supported for OGR connections
31 if ( ! configId.isEmpty() )
32 {
33 // Blank credentials: we are using authcfg!
34 username = QString();
35 password = QString();
36 // append authcfg is at the end, because we want to append the authcfg as last argument
37 }
38
39 //todo:add default ports for all kind of databases
40 if ( connectionType == QLatin1String( "ESRI Personal GeoDatabase" ) )
41 {
42 uri = "PGeo:" + database;
43 }
44 else if ( connectionType == QLatin1String( "ESRI ArcSDE" ) )
45 {
46 if ( port.isEmpty() )
47 port = QStringLiteral( "5151" );
48
49 uri = "SDE:" + host + ",PORT:" + port + ',' + database + ',' + username + ',' + password;
50 }
51 else if ( connectionType == QLatin1String( "Informix DataBlade" ) )
52 {
53 //not tested
54 uri = "IDB:dbname=" + database;
55
56 if ( !host.isEmpty() )
57 uri += QStringLiteral( " server=%1" ).arg( host );
58
59 if ( !username.isEmpty() )
60 {
61 uri += QStringLiteral( " user=%1" ).arg( username );
62
63 if ( !password.isEmpty() )
64 uri += QStringLiteral( " pass=%1" ).arg( password );
65 }
66 }
67 else if ( connectionType == QLatin1String( "Ingres" ) )
68 {
69 //not tested
70 uri = "@driver=ingres,dbname=" + database;
71 if ( !username.isEmpty() )
72 {
73 uri += QStringLiteral( ",userid=%1" ).arg( username );
74
75 if ( !password.isEmpty() )
76 uri += QStringLiteral( ",password=%1" ).arg( password );
77 }
78 }
79 else if ( connectionType == QLatin1String( "MySQL" ) )
80 {
81 uri = "MySQL:" + database;
82
83 if ( !host.isEmpty() )
84 {
85 uri += QStringLiteral( ",host=%1" ).arg( host );
86
87 if ( !port.isEmpty() )
88 uri += QStringLiteral( ",port=%1" ).arg( port );
89 }
90
91 if ( !username.isEmpty() )
92 {
93 uri += QStringLiteral( ",user=%1" ).arg( username );
94
95 if ( !password.isEmpty() )
96 uri += QStringLiteral( ",password=%1" ).arg( password );
97 }
98 }
99 else if ( connectionType == QLatin1String( "MSSQL" ) )
100 {
101 uri = QStringLiteral( "MSSQL:" );
102
103 if ( !host.isEmpty() )
104 {
105 uri += QStringLiteral( ";server=%1" ).arg( host );
106
107 if ( !port.isEmpty() )
108 uri += QStringLiteral( ",%1" ).arg( port );
109 }
110
111 if ( !username.isEmpty() )
112 {
113 uri += QStringLiteral( ";uid=%1" ).arg( username );
114
115 if ( !password.isEmpty() )
116 uri += QStringLiteral( ";pwd=%1" ).arg( password );
117 }
118 else
119 uri += QLatin1String( ";trusted_connection=yes" );
120
121 if ( !database.isEmpty() )
122 uri += QStringLiteral( ";database=%1" ).arg( database );
123 }
124 else if ( connectionType == QLatin1String( "Oracle Spatial" ) )
125 {
126 uri = "OCI:" + username;
127
128 if ( ( !username.isEmpty() && !password.isEmpty() ) ||
129 ( username.isEmpty() && password.isEmpty() ) )
130 {
131 uri += '/';
132 if ( !password.isEmpty() )
133 uri += password;
134 }
135
136 if ( !host.isEmpty() || !database.isEmpty() )
137 {
138 uri += '@';
139
140 if ( !host.isEmpty() )
141 {
142 uri += host;
143 if ( !port.isEmpty() )
144 uri += ':' + port;
145 }
146
147 if ( !database.isEmpty() )
148 {
149 if ( !host.isEmpty() )
150 uri += '/';
151 uri += database;
152 }
153 }
154 }
155 else if ( connectionType == QLatin1String( "ODBC" ) )
156 {
157 if ( !username.isEmpty() )
158 {
159 if ( password.isEmpty() )
160 {
161 uri = "ODBC:" + username + '@' + database;
162 }
163 else
164 {
165 uri = "ODBC:" + username + '/' + password + '@' + database;
166 }
167
168 }
169 else
170 {
171 uri = "ODBC:" + database;
172 }
173 }
174 else if ( connectionType == QLatin1String( "OGDI Vectors" ) )
175 {
176 }
177 else if ( connectionType == QLatin1String( "PostgreSQL" ) )
178 {
179 uri = "PG:dbname='" + database + '\'';
180
181 if ( !host.isEmpty() )
182 {
183 uri += QStringLiteral( " host='%1'" ).arg( host );
184
185 if ( !port.isEmpty() )
186 uri += QStringLiteral( " port='%1'" ).arg( port );
187 }
188
189 if ( !username.isEmpty() )
190 {
191 uri += QStringLiteral( " user='%1'" ).arg( username );
192
193 if ( !password.isEmpty() )
194 uri += QStringLiteral( " password='%1'" ).arg( password );
195 }
196
197 uri += ' ';
198 }
199 // Append authentication configuration to the URI
200 if ( !( configId.isEmpty() ) )
201 {
202 if ( ! expandAuthConfig )
203 {
204 uri += QStringLiteral( " authcfg='%1'" ).arg( configId );
205 }
206 else
207 {
208 QStringList connectionItems;
209 connectionItems << uri;
210 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
211 {
212 uri = connectionItems.join( QString() );
213 }
214 }
215 }
216 QgsDebugMsgLevel( "Connection type is=" + connectionType + " and uri=" + uri, 2 );
217 return uri;
218}
219
220
221QString createProtocolURI( const QString &type, const QString &url, const QString &configId, const QString &username, const QString &password, bool expandAuthConfig )
222{
223 QString uri;
224 if ( type == QLatin1String( "HTTP/HTTPS/FTP" ) )
225 {
226 uri = url;
227 // If no protocol is provided in the URL, default to HTTP
228 if ( !uri.startsWith( "http://" ) && !uri.startsWith( "https://" ) && !uri.startsWith( "ftp://" ) )
229 {
230 uri.prepend( QStringLiteral( "http://" ) );
231 }
232 uri.prepend( QStringLiteral( "/vsicurl/" ) );
233 }
234 else if ( type == QLatin1String( "AWS S3" ) )
235 {
236 uri = url;
237 uri.prepend( QStringLiteral( "/vsis3/" ) );
238 }
239 else if ( type == QLatin1String( "Google Cloud Storage" ) )
240 {
241 uri = url;
242 uri.prepend( QStringLiteral( "/vsigs/" ) );
243 }
244 else if ( type == QLatin1String( "Microsoft Azure Blob" ) )
245 {
246 uri = url;
247 uri.prepend( QStringLiteral( "/vsiaz/" ) );
248 }
249 else if ( type == QLatin1String( "Alibaba Cloud OSS" ) )
250 {
251 uri = url;
252 uri.prepend( QStringLiteral( "/vsioss/" ) );
253 }
254 else if ( type == QLatin1String( "OpenStack Swift Object Storage" ) )
255 {
256 uri = url;
257 uri.prepend( QStringLiteral( "/vsiswift/" ) );
258 }
259 // catching both GeoJSON and GeoJSONSeq
260 else if ( type.startsWith( QLatin1String( "GeoJSON" ) ) )
261 {
262 uri = url;
263 }
264 else if ( type == QLatin1String( "CouchDB" ) )
265 {
266 uri = QStringLiteral( "couchdb:%1" ).arg( url );
267 }
268 else if ( type == QLatin1String( "DODS/OPeNDAP" ) )
269 {
270 uri = QStringLiteral( "DODS:%1" ).arg( url );
271 }
272 // Check beginning because of "experimental"
273 else if ( type.startsWith( QLatin1String( "WFS3" ) ) )
274 {
275 uri = QStringLiteral( "WFS3:%1" ).arg( url );
276 }
277 QgsDebugMsgLevel( "Connection type is=" + type + " and uri=" + uri, 2 );
278 // Update URI with authentication information
279 if ( ! configId.isEmpty() )
280 {
281 if ( expandAuthConfig )
282 {
283 QStringList connectionItems;
284 connectionItems << uri;
285 if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
286 {
287 uri = connectionItems.join( QString() );
288 }
289 }
290 else
291 {
292 uri += QStringLiteral( " authcfg='%1'" ).arg( configId );
293 }
294 }
295 else if ( !( username.isEmpty() || password.isEmpty( ) ) )
296 {
297 uri.replace( QLatin1String( "://" ), QStringLiteral( "://%1:%2@" ).arg( username, password ) );
298 }
299 return uri;
300}
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
QString createDatabaseURI(const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig)
CreateDatabaseURI.
QString createProtocolURI(const QString &type, const QString &url, const QString &configId, const QString &username, const QString &password, bool expandAuthConfig)
CreateProtocolURI.