QGIS API Documentation  3.15.0-Master (a49cb7c9f3)
qgsabstractdatabaseproviderconnection.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsabstractdatabaseproviderconnection.cpp - QgsAbstractDatabaseProviderConnection
3 
4  ---------------------
5  begin : 2.8.2019
6  copyright : (C) 2019 by Alessandro Pasotti
7  email : elpaso at itopen dot it
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
17 #include "qgsvectorlayer.h"
18 #include "qgsexception.h"
19 #include <QVariant>
20 #include <QObject>
21 
24 {
25 
26 }
27 
29  QgsAbstractProviderConnection( uri, configuration )
30 {
31 
32 }
33 QgsAbstractDatabaseProviderConnection::Capabilities QgsAbstractDatabaseProviderConnection::capabilities() const
34 {
35  return mCapabilities;
36 }
37 
38 QString QgsAbstractDatabaseProviderConnection::tableUri( const QString &schema, const QString &name ) const
39 {
40  Q_UNUSED( schema )
41  Q_UNUSED( name )
42  throw QgsProviderConnectionException( QObject::tr( "Operation 'tableUri' is not supported" ) );
43 }
44 
46 void QgsAbstractDatabaseProviderConnection::checkCapability( QgsAbstractDatabaseProviderConnection::Capability capability ) const
47 {
48  if ( ! mCapabilities.testFlag( capability ) )
49  {
50  static QMetaEnum metaEnum = QMetaEnum::fromType<QgsAbstractDatabaseProviderConnection::Capability>();
51  const QString capName { metaEnum.valueToKey( capability ) };
52  throw QgsProviderConnectionException( QObject::tr( "Operation '%1' is not supported for this connection" ).arg( capName ) );
53  }
54 }
55 
57 {
58  return mProviderKey;
59 }
61 
63  const QString &name,
64  const QgsFields &fields,
65  QgsWkbTypes::Type wkbType,
67  bool overwrite,
68  const QMap<QString, QVariant> *
69  options ) const
70 {
71  Q_UNUSED( schema );
72  Q_UNUSED( name );
73  Q_UNUSED( fields );
74  Q_UNUSED( srs );
75  Q_UNUSED( overwrite );
76  Q_UNUSED( options );
77  Q_UNUSED( wkbType );
78  throw QgsProviderConnectionException( QObject::tr( "Operation 'createVectorTable' is not supported" ) );
79 }
80 
81 void QgsAbstractDatabaseProviderConnection::renameVectorTable( const QString &, const QString &, const QString & ) const
82 {
83  checkCapability( Capability::RenameVectorTable );
84 }
85 
86 void QgsAbstractDatabaseProviderConnection::renameRasterTable( const QString &, const QString &, const QString & ) const
87 {
88  checkCapability( Capability::RenameRasterTable );
89 }
90 
91 void QgsAbstractDatabaseProviderConnection::dropVectorTable( const QString &, const QString & ) const
92 {
93  checkCapability( Capability::DropVectorTable );
94 }
95 
96 bool QgsAbstractDatabaseProviderConnection::tableExists( const QString &schema, const QString &name ) const
97 {
98  checkCapability( Capability::TableExists );
99  const QList<QgsAbstractDatabaseProviderConnection::TableProperty> constTables { tables( schema ) };
100  for ( const auto &t : constTables )
101  {
102  if ( t.tableName() == name )
103  {
104  return true;
105  }
106  }
107  return false;
108 }
109 
110 void QgsAbstractDatabaseProviderConnection::dropRasterTable( const QString &, const QString & ) const
111 {
112  checkCapability( Capability::DropRasterTable );
113 }
114 
116 {
117  checkCapability( Capability::CreateSchema );
118 }
119 
120 void QgsAbstractDatabaseProviderConnection::dropSchema( const QString &, bool ) const
121 {
122  checkCapability( Capability::DropSchema );
123 }
124 
125 void QgsAbstractDatabaseProviderConnection::renameSchema( const QString &, const QString & ) const
126 {
127  checkCapability( Capability::RenameSchema );
128 }
129 
130 QList<QList<QVariant>> QgsAbstractDatabaseProviderConnection::executeSql( const QString & ) const
131 {
132  checkCapability( Capability::ExecuteSql );
133  return QList<QList<QVariant>>();
134 }
135 
136 void QgsAbstractDatabaseProviderConnection::vacuum( const QString &, const QString & ) const
137 {
138  checkCapability( Capability::Vacuum );
139 }
140 
142 {
143  checkCapability( Capability::CreateSpatialIndex );
144 }
145 
146 void QgsAbstractDatabaseProviderConnection::deleteSpatialIndex( const QString &, const QString &, const QString & ) const
147 {
148  checkCapability( Capability::DeleteSpatialIndex );
149 }
150 
151 bool QgsAbstractDatabaseProviderConnection::spatialIndexExists( const QString &, const QString &, const QString & ) const
152 {
153  checkCapability( Capability::SpatialIndexExists );
154  return false;
155 }
156 
157 QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsAbstractDatabaseProviderConnection::tables( const QString &, const QgsAbstractDatabaseProviderConnection::TableFlags & ) const
158 {
159  checkCapability( Capability::Tables );
160  return QList<QgsAbstractDatabaseProviderConnection::TableProperty>();
161 }
162 
163 
165 {
166  checkCapability( Capability::Tables );
167  const QList<QgsAbstractDatabaseProviderConnection::TableProperty> constTables { tables( schema ) };
168  for ( const auto &t : constTables )
169  {
170  if ( t.tableName() == name )
171  {
172  return t;
173  }
174  }
175  throw QgsProviderConnectionException( QObject::tr( "Table '%1' was not found in schema '%2'" )
176  .arg( name )
177  .arg( schema ) );
178 }
179 
180 QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsAbstractDatabaseProviderConnection::tablesInt( const QString &schema, const int flags ) const
181 {
182  return tables( schema, static_cast<QgsAbstractDatabaseProviderConnection::TableFlags>( flags ) );
183 }
184 
185 
187 {
188  checkCapability( Capability::Schemas );
189  return QStringList();
190 }
191 
193 {
194  return mTableName;
195 }
196 
198 {
199  mTableName = name;
200 }
201 
203 {
204  // Do not add the type if it's already present
206  for ( const auto &t : qgis::as_const( mGeometryColumnTypes ) )
207  {
208  if ( t == toAdd )
209  {
210  return;
211  }
212  }
213  mGeometryColumnTypes.push_back( toAdd );
214 }
215 
216 QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> QgsAbstractDatabaseProviderConnection::TableProperty::geometryColumnTypes() const
217 {
218  return mGeometryColumnTypes;
219 }
220 
221 QgsFields QgsAbstractDatabaseProviderConnection::fields( const QString &schema, const QString &tableName ) const
222 {
223  QgsVectorLayer::LayerOptions options { true, true };
224  options.skipCrsValidation = true;
225  QgsVectorLayer vl { tableUri( schema, tableName ), QStringLiteral( "temp_layer" ), mProviderKey, options };
226  if ( vl.isValid() )
227  {
228  return vl.fields();
229  }
230  else
231  {
232  throw QgsProviderConnectionException( QObject::tr( "Error retrieving fields information for uri: %1" ).arg( vl.publicSource() ) );
233  }
234 }
235 
237 {
238  QString n = mTableName;
239  if ( mGeometryColumnCount > 1 ) n += '.' + mGeometryColumn;
240  return n;
241 }
242 
244 {
245  TableProperty property;
246 
247  Q_ASSERT( index >= 0 && index < mGeometryColumnTypes.size() );
248 
249  property.mGeometryColumnTypes << mGeometryColumnTypes[ index ];
250  property.mSchema = mSchema;
251  property.mTableName = mTableName;
252  property.mGeometryColumn = mGeometryColumn;
253  property.mPkColumns = mPkColumns;
254  property.mGeometryColumnCount = mGeometryColumnCount;
255  property.mFlags = mFlags;
256  property.mComment = mComment;
257  property.mInfo = mInfo;
258  return property;
259 }
260 
262 {
263  mFlags.setFlag( flag );
264 }
265 
267 {
268  int res = 0;
269  for ( const TableProperty::GeometryColumnType &ct : qgis::as_const( mGeometryColumnTypes ) )
270  {
271  res = std::max( res, QgsWkbTypes::coordDimensions( ct.wkbType ) );
272  }
273  return res;
274 }
275 
277 {
278  return mSchema == other.mSchema &&
279  mTableName == other.mTableName &&
280  mGeometryColumn == other.mGeometryColumn &&
281  mGeometryColumnCount == other.mGeometryColumnCount &&
282  mPkColumns == other.mPkColumns &&
283  mFlags == other.mFlags &&
284  mComment == other.mComment &&
285  mInfo == other.mInfo;
286 }
287 
288 
289 void QgsAbstractDatabaseProviderConnection::TableProperty::setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &columnTypes )
290 {
291  mGeometryColumnTypes = columnTypes;
292 }
293 
294 
296 {
297  return mGeometryColumnCount;
298 }
299 
301 {
302  mGeometryColumnCount = geometryColumnCount;
303 }
304 
306 {
307  return mInfo;
308 }
309 
311 {
312  mInfo = info;
313 }
314 
316 {
317  return mComment;
318 }
319 
321 {
322  mComment = comment;
323 }
324 
325 QgsAbstractDatabaseProviderConnection::TableFlags QgsAbstractDatabaseProviderConnection::TableProperty::flags() const
326 {
327  return mFlags;
328 }
329 
330 void QgsAbstractDatabaseProviderConnection::TableProperty::setFlags( const QgsAbstractDatabaseProviderConnection::TableFlags &flags )
331 {
332  mFlags = flags;
333 }
334 
335 QList<QgsCoordinateReferenceSystem> QgsAbstractDatabaseProviderConnection::TableProperty::crsList() const
336 {
337  QList<QgsCoordinateReferenceSystem> crss;
338  for ( const auto &t : qgis::as_const( mGeometryColumnTypes ) )
339  {
340  crss.push_back( t.crs );
341  }
342  return crss;
343 }
344 
346 {
347  return mPkColumns;
348 }
349 
351 {
352  mPkColumns = pkColumns;
353 }
354 
356 {
357  return mGeometryColumn;
358 }
359 
361 {
362  mGeometryColumn = geometryColumn;
363 }
364 
366 {
367  return mSchema;
368 }
369 
371 {
372  mSchema = schema;
373 }
374 
QString defaultName() const
Returns the default name for the table entry.
virtual QList< QList< QVariant > > executeSql(const QString &sql) const SIP_THROW(QgsProviderConnectionException)
Executes raw sql and returns the (possibly empty) list of results in a multi-dimensional array...
void setPrimaryKeyColumns(const QStringList &primaryKeyColumns)
Sets the primary key column names to primaryKeyColumns.
Capability
The Capability enum represent the operations supported by the connection.
virtual bool tableExists(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Checks whether a table name exists in the given schema.
virtual QgsAbstractDatabaseProviderConnection::TableProperty table(const QString &schema, const QString &table) const
Returns information on a table in the given schema.
Setting options for loading vector layers.
virtual void dropSchema(const QString &name, bool force=false) const SIP_THROW(QgsProviderConnectionException)
Drops an entire schema with the specified name.
virtual void vacuum(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Vacuum the database table with given schema and name (schema is ignored if not supported by the backe...
void setInfo(const QVariantMap &info)
Sets additional information about the table to info.
QStringList primaryKeyColumns() const
Returns the list of primary key column names.
QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > geometryColumnTypes() const
Returns the list of geometry column types and CRSs.
void addGeometryColumnType(const QgsWkbTypes::Type &type, const QgsCoordinateReferenceSystem &crs)
Appends the geometry column type with the given srid to the geometry column types list...
virtual void createSchema(const QString &name) const SIP_THROW(QgsProviderConnectionException)
Creates a new schema with the specified name.
Container of fields for a vector layer.
Definition: qgsfields.h:44
int geometryColumnCount() const
Returns the number of geometry columns in the original table this entry refers to.
void setGeometryColumnTypes(const QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > &geometryColumnTypes)
Sets the geometry column types to geometryColumnTypes.
bool skipCrsValidation
Controls whether the layer is allowed to have an invalid/unknown CRS.
void setGeometryColumnCount(int geometryColumnCount)
Sets the geometryColumnCount.
QString geometryColumn() const
Returns the geometry column name.
const QgsCoordinateReferenceSystem & crs
virtual void createVectorTable(const QString &schema, const QString &name, const QgsFields &fields, QgsWkbTypes::Type wkbType, const QgsCoordinateReferenceSystem &srs, bool overwrite, const QMap< QString, QVariant > *options) const SIP_THROW(QgsProviderConnectionException)
Creates an empty table with name in the given schema (schema is ignored if not supported by the backe...
virtual void renameSchema(const QString &name, const QString &newName) const SIP_THROW(QgsProviderConnectionException)
Renames a schema with the specified name.
The QgsAbstractProviderConnection provides an interface for data provider connections.
The GeometryColumnType struct represents the combination of geometry type and CRS for the table geome...
virtual void deleteSpatialIndex(const QString &schema, const QString &name, const QString &geometryColumn) const SIP_THROW(QgsProviderConnectionException)
Deletes the existing spatial index for the database table with given schema, name and geometryColumn ...
virtual void renameVectorTable(const QString &schema, const QString &name, const QString &newName) const SIP_THROW(QgsProviderConnectionException)
Renames a vector or aspatial table with given schema (schema is ignored if not supported by the backe...
bool operator==(const QgsAbstractDatabaseProviderConnection::TableProperty &other) const
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
virtual void dropRasterTable(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Drops a raster table with given schema (schema is ignored if not supported by the backend) and name...
virtual void renameRasterTable(const QString &schema, const QString &name, const QString &newName) const SIP_THROW(QgsProviderConnectionException)
Renames a raster table with given schema (schema is ignored if not supported by the backend) and name...
virtual QStringList schemas() const SIP_THROW(QgsProviderConnectionException)
Returns information about the existing schemas.
The TableProperty class represents a database table or view.
QString providerKey() const
Returns the provider key.
virtual QString tableUri(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Returns the URI string for the given table and schema.
virtual void dropVectorTable(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Drops a vector (or aspatial) table with given schema (schema is ignored if not supported by the backe...
QString uri() const
Returns the connection data source URI string representation.
QVariantMap configuration() const
Returns the connection configuration parameters.
void setTableName(const QString &name)
Sets the table name to name.
virtual void createSpatialIndex(const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options=QgsAbstractDatabaseProviderConnection::SpatialIndexOptions()) const SIP_THROW(QgsProviderConnectionException)
Creates a spatial index for the database table with given schema and name (schema is ignored if not s...
QList< QgsAbstractDatabaseProviderConnection::TableProperty > tablesInt(const QString &schema=QString(), const int flags=0) const SIP_THROW(QgsProviderConnectionException)
Returns information on the tables in the given schema.
TableProperty at(int index) const
Returns the table property corresponding to the geometry type at the given index. ...
QList< QgsCoordinateReferenceSystem > crsList() const
Returns the list of CRSs supported by the geometry column.
void setFlags(const TableFlags &flags)
Sets the table flags.
Contains extra options relating to spatial index creation.
Capabilities capabilities() const
Returns connection capabilities.
void setComment(const QString &comment)
Sets the table comment.
virtual bool spatialIndexExists(const QString &schema, const QString &name, const QString &geometryColumn) const SIP_THROW(QgsProviderConnectionException)
Determines whether a spatial index exists for the database table with given schema, name and geometryColumn (schema and geometryColumn are ignored if not supported by the backend).
virtual QList< QgsAbstractDatabaseProviderConnection::TableProperty > tables(const QString &schema=QString(), const QgsAbstractDatabaseProviderConnection::TableFlags &flags=nullptr) const
Returns information on the tables in the given schema.
QgsAbstractDatabaseProviderConnection(const QString &name)
Creates a new connection with name by reading its configuration from the settings.
This class represents a coordinate reference system (CRS).
int maxCoordinateDimensions() const
Returns the maximum coordinate dimensions of the geometries of a vector table.
virtual QgsFields fields(const QString &schema, const QString &table) const SIP_THROW(QgsProviderConnectionException)
Returns the fields of a table and schema.
void setGeometryColumn(const QString &geometryColumn)
Sets the geometry column name to geometryColumn.
QString schema() const
Returns the schema or an empty string for backends that do not support a schema.
Represents a vector layer which manages a vector based data sets.
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:100
static int coordDimensions(Type type)
Returns the coordinate dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:924
QVariantMap info() const
Returns additional information about the table.