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 #ifndef QGSDATASOURCEURI_H
20 #define QGSDATASOURCEURI_H
21 
22 #include "qgis_core.h"
23 #include "qgis_sip.h"
24 #include "qgswkbtypes.h"
25 
26 #include <QMap>
27 
28 /**
29  * \ingroup core
30  * \brief Class for storing the component parts of a RDBMS data source URI (e.g. a Postgres data source).
31  *
32  * This structure stores the database connection information, including host, database,
33  * user name, password, schema, password, and SQL where clause.
34  */
35 class CORE_EXPORT QgsDataSourceUri
36 {
37     Q_GADGET
38   public:
39 
40     /**
41      * Available SSL connection modes.
42      */
43     enum SslMode
44     {
45       SslPrefer,
46       SslDisable,
47       SslAllow,
48       SslRequire,
49       SslVerifyCa,
50       SslVerifyFull
51     };
52     Q_ENUM( SslMode )
53 
54     QgsDataSourceUri();
55 
56     /**
57      * Constructor for QgsDataSourceUri which parses an input \a uri string.
58      */
59     QgsDataSourceUri( const QString &uri );
60 
61     /**
62      * Constructor for QgsDataSourceUri which parses an input encoded \a uri).
63      * \note not available in Python bindings
64      */
65     QgsDataSourceUri( const QByteArray &uri ) SIP_SKIP;
66 
67     /**
68      * Returns the connection part of the URI.
69      */
70     QString connectionInfo( bool expandAuthConfig = true ) const;
71 
72     /**
73      * Returns the complete URI as a string.
74      */
75     QString uri( bool expandAuthConfig = true ) const;
76 
77     /**
78      * Returns the complete encoded URI as a byte array.
79      */
80     QByteArray encodedUri() const;
81 
82     /**
83      * Sets the complete encoded \a uri.
84      *
85      * \note not available in Python bindings
86      */
87     void setEncodedUri( const QByteArray &uri ) SIP_SKIP;
88 
89     /**
90      * Sets the complete encoded \a uri from a string value.
91      */
92     void setEncodedUri( const QString &uri );
93 
94     /**
95      * Returns the URI's table name, escaped and quoted.
96      */
97     QString quotedTablename() const;
98 
99     /**
100      * Sets a generic parameter \a value on the URI.
101      * \note If a parameter with the specified \a key already exists, another is inserted
102      * and the existing value is left unchanged.
103      */
104     void setParam( const QString &key, const QString &value );
105 
106     /**
107      * Sets a generic parameter list \a value on the URI.
108      * \note available in Python as setParamList
109      */
110     void setParam( const QString &key, const QStringList &value ) SIP_PYNAME( setParamList );
111 
112     /**
113      * Removes a generic parameter by \a key.
114      * \note Calling this method removes all the occurrences of key, and returns the number of parameters removed.
115      */
116     int removeParam( const QString &key );
117 
118     /**
119      * Returns a generic parameter value corresponding to the specified \a key.
120      */
121     QString param( const QString &key ) const;
122 
123     /**
124      * Returns multiple generic parameter values corresponding to the specified \a key.
125      */
126     QStringList params( const QString &key ) const;
127 
128     /**
129      * Returns TRUE if a parameter with the specified \a key exists.
130      */
131     bool hasParam( const QString &key ) const;
132 
133     /**
134      * Sets all connection related members at once.
135      */
136     void setConnection( const QString &aHost,
137                         const QString &aPort,
138                         const QString &aDatabase,
139                         const QString &aUsername,
140                         const QString &aPassword,
141                         SslMode sslmode = SslPrefer,
142                         const QString &authConfigId = QString() );
143 
144     /**
145      * Sets all connection related members at once (for a service case).
146      */
147     void setConnection( const QString &aService,
148                         const QString &aDatabase,
149                         const QString &aUsername,
150                         const QString &aPassword,
151                         SslMode sslmode = SslPrefer,
152                         const QString &authConfigId = QString() );
153 
154     /**
155      * Sets the URI database name.
156      */
157     void setDatabase( const QString &database );
158 
159     /**
160      * Sets all data source related members at once.
161      *
162      * The \a aSql argument represents a subset filter string to be applied to the source, and should take the
163      * form of a SQL "where" clause (e.g. "VALUE > 5", "CAT IN (1,2,3)").
164      */
165     void setDataSource( const QString &aSchema,
166                         const QString &aTable,
167                         const QString &aGeometryColumn,
168                         const QString &aSql = QString(),
169                         const QString &aKeyColumn = QString() );
170 
171     /**
172      * Sets the authentication configuration ID for the URI.
173      */
174     void setAuthConfigId( const QString &authcfg );
175 
176     /**
177      * Sets the \a username for the URI.
178      */
179     void setUsername( const QString &username );
180 
181     /**
182      * Sets the \a password for the URI.
183      */
184     void setPassword( const QString &password );
185 
186     /**
187      * Removes the password element from a URI.
188      */
189     static QString removePassword( const QString &aUri );
190 
191     /**
192      * Returns any associated authentication configuration ID stored in the URI.
193      */
194     QString authConfigId() const;
195 
196     //! Returns the username stored in the URI.
197     QString username() const;
198 
199     //! Returns the schema stored in the URI.
200     QString schema() const;
201 
202     //! Returns the table name stored in the URI.
203     QString table() const;
204 
205     /**
206      * Returns the SQL filter stored in the URI, if set.
207      *
208      * This represents a subset filter string to be applied to the source, and takes the
209      * form of a SQL "where" clause (e.g. "VALUE > 5", "CAT IN (1,2,3)").
210      *
211      * \see setSql()
212      */
213     QString sql() const;
214 
215     //! Returns the name of the geometry column stored in the URI, if set.
216     QString geometryColumn() const;
217 
218     //! Sets whether estimated metadata should be used for the connection.
219     void setUseEstimatedMetadata( bool flag );
220 
221     //! Returns TRUE if estimated metadata should be used for the connection.
222     bool useEstimatedMetadata() const;
223 
224     //! Set to TRUE to disable selection by feature ID.
225     void disableSelectAtId( bool flag );
226 
227     //! Returns whether the selection by feature ID is disabled.
228     bool selectAtIdDisabled() const;
229 
230     //! Clears the schema stored in the URI.
231     void clearSchema();
232 
233     /**
234      * Sets the \a scheme for the URI.
235      * \since QGIS 2.12
236      */
237     void setSchema( const QString &schema );
238 
239     /**
240      * Sets the \a sql filter for the URI.
241      *
242      * The \a sql represents a subset filter string to be applied to the source, and should take the
243      * form of a SQL "where" clause (e.g. "VALUE > 5", "CAT IN (1,2,3)").
244      *
245      * \see sql()
246      */
247     void setSql( const QString &sql );
248 
249     //! Returns the host name stored in the URI.
250     QString host() const;
251     //! Returns the database name stored in the URI.
252     QString database() const;
253     //! Returns the port stored in the URI.
254     QString port() const;
255 
256     /**
257      * Returns the driver name stored in the URI
258      * \since QGIS 2.16
259      */
260     QString driver() const;
261 
262     /**
263      * Sets the \a driver name stored in the URI.
264      * \since QGIS 2.16
265      */
266     void setDriver( const QString &driver );
267 
268     //! Returns the password stored in the URI.
269     QString password() const;
270 
271     //! Returns the SSL mode associated with the URI.
272     SslMode sslMode() const;
273 
274     //! Returns the service name associated with the URI.
275     QString service() const;
276 
277     //! Returns the name of the (primary) key column for the referenced table.
278     QString keyColumn() const;
279 
280     //! Sets the name of the (primary) key \a column.
281     void setKeyColumn( const QString &column );
282 
283     /**
284      * Returns the WKB type associated with the URI.
285      */
286     QgsWkbTypes::Type wkbType() const;
287 
288     //! Sets the WKB \a type associated with the URI.
289     void setWkbType( QgsWkbTypes::Type type );
290 
291     //! Returns the spatial reference ID associated with the URI.
292     QString srid() const;
293 
294     //! Sets the spatial reference ID associated with the URI.
295     void setSrid( const QString &srid );
296 
297     /**
298      * Decodes SSL mode string into enum value. If the string is not recognized, SslPrefer is returned.
299      * \since QGIS 3.2
300      */
301     static SslMode decodeSslMode( const QString &sslMode );
302 
303     /**
304      * Encodes SSL mode enum value into a string.
305      * \since QGIS 3.2
306      */
307     static QString encodeSslMode( SslMode sslMode );
308 
309     /**
310      * Sets table to \a table
311      * \since QGIS 3.10
312      */
313     void setTable( const QString &table );
314 
315     /**
316      * Sets geometry column name to \a geometryColumn
317      * \since QGIS 3.10
318      */
319     void setGeometryColumn( const QString &geometryColumn );
320 
321 #ifdef SIP_RUN
322     SIP_PYOBJECT __repr__();
323     % MethodCode
324     QString str = QStringLiteral( "<QgsDataSourceUri: %1>" ).arg( sipCpp->uri( false ) );
325     sipRes = PyUnicode_FromString( str.toUtf8().constData() );
326     % End
327 #endif
328 
329   private:
330     void skipBlanks( const QString &uri, int &i );
331     QString getValue( const QString &uri, int &i );
332     QString escape( const QString &uri, QChar delim ) const;
333 
334     /* data */
335 
336     //! host name
337     QString mHost;
338     //! port the database server listens on
339     QString mPort;
340     //! device driver for ODBC
341     QString mDriver;
342     //! service name
343     QString mService;
344     //! database name
345     QString mDatabase;
346     //! schema
347     QString mSchema;
348     //! spatial table
349     QString mTable;
350     //! geometry column
351     QString mGeometryColumn;
352     //! SQL query or where clause used to limit features returned from the layer
353     QString mSql;
354     //! authentication configuration ID
355     QString mAuthConfigId;
356     //! username
357     QString mUsername;
358     //! password
359     QString mPassword;
360     //! ssl mode
361     SslMode mSSLmode = SslPrefer;
362     //! key column
363     QString mKeyColumn;
364     //! Use estimated metadata flag
365     bool mUseEstimatedMetadata = false;
366     //! Disable SelectAtId capability (e.g., to trigger the attribute table memory model for expensive views)
367     bool mSelectAtIdDisabled = false;
368     //! geometry type (or QgsWkbTypes::Unknown if not specified)
369     QgsWkbTypes::Type mWkbType = QgsWkbTypes::Unknown;
370     //! SRID or a null string if not specified
371     QString mSrid;
372     //! Generic params store
373     QMultiMap<QString, QString> mParams;
374 };
375 
376 #endif //QGSDATASOURCEURI_H
377 
378