QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 "qgsexception.h"
18 #include <QVariant>
19 #include <QObject>
20 
23 {
24 
25 }
26 
27 QgsAbstractDatabaseProviderConnection::QgsAbstractDatabaseProviderConnection( const QString &uri, const QVariantMap &configuration ):
28  QgsAbstractProviderConnection( uri, configuration )
29 {
30 
31 }
32 QgsAbstractDatabaseProviderConnection::Capabilities QgsAbstractDatabaseProviderConnection::capabilities() const
33 {
34  return mCapabilities;
35 }
36 
37 QString QgsAbstractDatabaseProviderConnection::tableUri( const QString &schema, const QString &name ) const
38 {
39  Q_UNUSED( schema )
40  Q_UNUSED( name )
41  throw QgsProviderConnectionException( QObject::tr( "Operation 'tableUri' is not supported" ) );
42 }
43 
45 void QgsAbstractDatabaseProviderConnection::checkCapability( QgsAbstractDatabaseProviderConnection::Capability capability ) const
46 {
47  if ( ! mCapabilities.testFlag( capability ) )
48  {
49  static QMetaEnum metaEnum = QMetaEnum::fromType<QgsAbstractDatabaseProviderConnection::Capability>();
50  const QString capName { metaEnum.valueToKey( capability ) };
51  throw QgsProviderConnectionException( QObject::tr( "Operation '%1' is not supported for this connection" ).arg( capName ) );
52  }
53 }
55 
57  const QString &name,
58  const QgsFields &fields,
59  QgsWkbTypes::Type wkbType,
61  bool overwrite,
62  const QMap<QString, QVariant> *
63  options ) const
64 {
65  Q_UNUSED( schema );
66  Q_UNUSED( name );
67  Q_UNUSED( fields );
68  Q_UNUSED( srs );
69  Q_UNUSED( overwrite );
70  Q_UNUSED( options );
71  Q_UNUSED( wkbType );
72  throw QgsProviderConnectionException( QObject::tr( "Operation 'createVectorTable' is not supported" ) );
73 }
74 
75 void QgsAbstractDatabaseProviderConnection::renameVectorTable( const QString &, const QString &, const QString & ) const
76 {
77  checkCapability( Capability::RenameVectorTable );
78 }
79 
80 void QgsAbstractDatabaseProviderConnection::renameRasterTable( const QString &, const QString &, const QString & ) const
81 {
82  checkCapability( Capability::RenameRasterTable );
83 }
84 
85 void QgsAbstractDatabaseProviderConnection::dropVectorTable( const QString &, const QString & ) const
86 {
87  checkCapability( Capability::DropVectorTable );
88 }
89 
90 bool QgsAbstractDatabaseProviderConnection::tableExists( const QString &schema, const QString &name ) const
91 {
92  checkCapability( Capability::TableExists );
93  const QList<QgsAbstractDatabaseProviderConnection::TableProperty> constTables { tables( schema ) };
94  for ( const auto &t : constTables )
95  {
96  if ( t.tableName() == name )
97  {
98  return true;
99  }
100  }
101  return false;
102 }
103 
104 void QgsAbstractDatabaseProviderConnection::dropRasterTable( const QString &, const QString & ) const
105 {
106  checkCapability( Capability::DropRasterTable );
107 }
108 
110 {
111  checkCapability( Capability::CreateSchema );
112 }
113 
114 void QgsAbstractDatabaseProviderConnection::dropSchema( const QString &, bool ) const
115 {
116  checkCapability( Capability::DropSchema );
117 }
118 
119 void QgsAbstractDatabaseProviderConnection::renameSchema( const QString &, const QString & ) const
120 {
121  checkCapability( Capability::RenameSchema );
122 }
123 
124 QList<QList<QVariant>> QgsAbstractDatabaseProviderConnection::executeSql( const QString & ) const
125 {
126  checkCapability( Capability::ExecuteSql );
127  return QList<QList<QVariant>>();
128 }
129 
130 void QgsAbstractDatabaseProviderConnection::vacuum( const QString &, const QString & ) const
131 {
132  checkCapability( Capability::Vacuum );
133 }
134 
136 {
137  checkCapability( Capability::CreateSpatialIndex );
138 }
139 
140 void QgsAbstractDatabaseProviderConnection::deleteSpatialIndex( const QString &, const QString &, const QString & ) const
141 {
142  checkCapability( Capability::DeleteSpatialIndex );
143 }
144 
145 bool QgsAbstractDatabaseProviderConnection::spatialIndexExists( const QString &, const QString &, const QString & ) const
146 {
147  checkCapability( Capability::SpatialIndexExists );
148  return false;
149 }
150 
151 QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsAbstractDatabaseProviderConnection::tables( const QString &, const QgsAbstractDatabaseProviderConnection::TableFlags & ) const
152 {
153  checkCapability( Capability::Tables );
154  return QList<QgsAbstractDatabaseProviderConnection::TableProperty>();
155 }
156 
157 
159 {
160  checkCapability( Capability::Tables );
161  const QList<QgsAbstractDatabaseProviderConnection::TableProperty> constTables { tables( schema ) };
162  for ( const auto &t : constTables )
163  {
164  if ( t.tableName() == name )
165  {
166  return t;
167  }
168  }
169  throw QgsProviderConnectionException( QObject::tr( "Table '%1' was not found in schema '%2'" )
170  .arg( name )
171  .arg( schema ) );
172 }
173 
174 QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsAbstractDatabaseProviderConnection::tablesInt( const QString &schema, const int flags ) const
175 {
176  return tables( schema, static_cast<QgsAbstractDatabaseProviderConnection::TableFlags>( flags ) );
177 }
178 
179 
181 {
182  checkCapability( Capability::Schemas );
183  return QStringList();
184 }
185 
187 {
188  return mTableName;
189 }
190 
192 {
193  mTableName = name;
194 }
195 
197 {
198  // Do not add the type if it's already present
200  for ( const auto &t : qgis::as_const( mGeometryColumnTypes ) )
201  {
202  if ( t == toAdd )
203  {
204  return;
205  }
206  }
207  mGeometryColumnTypes.push_back( toAdd );
208 }
209 
210 QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> QgsAbstractDatabaseProviderConnection::TableProperty::geometryColumnTypes() const
211 {
212  return mGeometryColumnTypes;
213 }
214 
215 
217 {
218  QString n = mTableName;
219  if ( mGeometryColumnCount > 1 ) n += '.' + mGeometryColumn;
220  return n;
221 }
222 
224 {
225  TableProperty property;
226 
227  Q_ASSERT( index >= 0 && index < mGeometryColumnTypes.size() );
228 
229  property.mGeometryColumnTypes << mGeometryColumnTypes[ index ];
230  property.mSchema = mSchema;
231  property.mTableName = mTableName;
232  property.mGeometryColumn = mGeometryColumn;
233  property.mPkColumns = mPkColumns;
234  property.mGeometryColumnCount = mGeometryColumnCount;
235  property.mFlags = mFlags;
236  property.mComment = mComment;
237  property.mInfo = mInfo;
238  return property;
239 }
240 
242 {
243  mFlags.setFlag( flag );
244 }
245 
247 {
248  int res = 0;
249  for ( const TableProperty::GeometryColumnType &ct : qgis::as_const( mGeometryColumnTypes ) )
250  {
251  res = std::max( res, QgsWkbTypes::coordDimensions( ct.wkbType ) );
252  }
253  return res;
254 }
255 
257 {
258  return mSchema == other.mSchema &&
259  mTableName == other.mTableName &&
260  mGeometryColumn == other.mGeometryColumn &&
261  mGeometryColumnCount == other.mGeometryColumnCount &&
262  mPkColumns == other.mPkColumns &&
263  mFlags == other.mFlags &&
264  mComment == other.mComment &&
265  mInfo == other.mInfo;
266 }
267 
268 
269 void QgsAbstractDatabaseProviderConnection::TableProperty::setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &columnTypes )
270 {
271  mGeometryColumnTypes = columnTypes;
272 }
273 
274 
276 {
277  return mGeometryColumnCount;
278 }
279 
281 {
282  mGeometryColumnCount = geometryColumnCount;
283 }
284 
286 {
287  return mInfo;
288 }
289 
291 {
292  mInfo = info;
293 }
294 
296 {
297  return mComment;
298 }
299 
301 {
302  mComment = comment;
303 }
304 
305 QgsAbstractDatabaseProviderConnection::TableFlags QgsAbstractDatabaseProviderConnection::TableProperty::flags() const
306 {
307  return mFlags;
308 }
309 
310 void QgsAbstractDatabaseProviderConnection::TableProperty::setFlags( const QgsAbstractDatabaseProviderConnection::TableFlags &flags )
311 {
312  mFlags = flags;
313 }
314 
315 QList<QgsCoordinateReferenceSystem> QgsAbstractDatabaseProviderConnection::TableProperty::crsList() const
316 {
317  QList<QgsCoordinateReferenceSystem> crss;
318  for ( const auto &t : qgis::as_const( mGeometryColumnTypes ) )
319  {
320  crss.push_back( t.crs );
321  }
322  return crss;
323 }
324 
326 {
327  return mPkColumns;
328 }
329 
331 {
332  mPkColumns = pkColumns;
333 }
334 
336 {
337  return mGeometryColumn;
338 }
339 
341 {
342  mGeometryColumn = geometryColumn;
343 }
344 
346 {
347  return mSchema;
348 }
349 
351 {
352  mSchema = schema;
353 }
354 
QgsAbstractDatabaseProviderConnection::TableProperty::setGeometryColumnTypes
void setGeometryColumnTypes(const QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > &geometryColumnTypes)
Sets the geometry column types to geometryColumnTypes.
Definition: qgsabstractdatabaseproviderconnection.cpp:269
QgsAbstractDatabaseProviderConnection::createSpatialIndex
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...
Definition: qgsabstractdatabaseproviderconnection.cpp:135
QgsAbstractDatabaseProviderConnection::TableProperty::setSchema
void setSchema(const QString &schema)
Sets the schema.
Definition: qgsabstractdatabaseproviderconnection.cpp:350
QgsAbstractDatabaseProviderConnection::tablesInt
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.
Definition: qgsabstractdatabaseproviderconnection.cpp:174
QgsAbstractDatabaseProviderConnection::renameRasterTable
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...
Definition: qgsabstractdatabaseproviderconnection.cpp:80
QgsAbstractDatabaseProviderConnection::dropSchema
virtual void dropSchema(const QString &name, bool force=false) const SIP_THROW(QgsProviderConnectionException)
Drops an entire schema with the specified name.
Definition: qgsabstractdatabaseproviderconnection.cpp:114
QgsWkbTypes::coordDimensions
static int coordDimensions(Type type)
Returns the coordinate dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:924
QgsAbstractDatabaseProviderConnection::createSchema
virtual void createSchema(const QString &name) const SIP_THROW(QgsProviderConnectionException)
Creates a new schema with the specified name.
Definition: qgsabstractdatabaseproviderconnection.cpp:109
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsAbstractDatabaseProviderConnection::TableProperty::setPrimaryKeyColumns
void setPrimaryKeyColumns(const QStringList &primaryKeyColumns)
Sets the primary key column names to primaryKeyColumns.
Definition: qgsabstractdatabaseproviderconnection.cpp:330
QgsFields
Definition: qgsfields.h:44
QgsAbstractDatabaseProviderConnection::spatialIndexExists
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,...
Definition: qgsabstractdatabaseproviderconnection.cpp:145
QgsAbstractDatabaseProviderConnection::tables
virtual QList< QgsAbstractDatabaseProviderConnection::TableProperty > tables(const QString &schema=QString(), const QgsAbstractDatabaseProviderConnection::TableFlags &flags=nullptr) const
Returns information on the tables in the given schema.
Definition: qgsabstractdatabaseproviderconnection.cpp:151
QgsAbstractDatabaseProviderConnection::TableProperty::geometryColumn
QString geometryColumn() const
Returns the geometry column name.
Definition: qgsabstractdatabaseproviderconnection.cpp:335
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsAbstractDatabaseProviderConnection::TableProperty::primaryKeyColumns
QStringList primaryKeyColumns() const
Returns the list of primary key column names.
Definition: qgsabstractdatabaseproviderconnection.cpp:325
QgsAbstractDatabaseProviderConnection::TableProperty::addGeometryColumnType
void addGeometryColumnType(const QgsWkbTypes::Type &type, const QgsCoordinateReferenceSystem &crs)
Appends the geometry column type with the given srid to the geometry column types list.
Definition: qgsabstractdatabaseproviderconnection.cpp:196
QgsAbstractDatabaseProviderConnection::TableProperty::defaultName
QString defaultName() const
Returns the default name for the table entry.
Definition: qgsabstractdatabaseproviderconnection.cpp:216
QgsAbstractDatabaseProviderConnection::TableProperty
The TableProperty class represents a database table or view.
Definition: qgsabstractdatabaseproviderconnection.h:92
QgsAbstractDatabaseProviderConnection::TableProperty::setFlag
void setFlag(const TableFlag &flag)
Sets a flag.
Definition: qgsabstractdatabaseproviderconnection.cpp:241
QgsAbstractDatabaseProviderConnection::tableExists
virtual bool tableExists(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Checks whether a table name exists in the given schema.
Definition: qgsabstractdatabaseproviderconnection.cpp:90
QgsAbstractDatabaseProviderConnection::TableProperty::info
QVariantMap info() const
Returns additional information about the table.
Definition: qgsabstractdatabaseproviderconnection.cpp:285
QgsAbstractDatabaseProviderConnection::renameSchema
virtual void renameSchema(const QString &name, const QString &newName) const SIP_THROW(QgsProviderConnectionException)
Renames a schema with the specified name.
Definition: qgsabstractdatabaseproviderconnection.cpp:119
QgsAbstractDatabaseProviderConnection::TableProperty::setGeometryColumnCount
void setGeometryColumnCount(int geometryColumnCount)
Sets the geometryColumnCount.
Definition: qgsabstractdatabaseproviderconnection.cpp:280
QgsAbstractDatabaseProviderConnection::QgsAbstractDatabaseProviderConnection
QgsAbstractDatabaseProviderConnection(const QString &name)
Creates a new connection with name by reading its configuration from the settings.
Definition: qgsabstractdatabaseproviderconnection.cpp:21
QgsAbstractDatabaseProviderConnection::mCapabilities
Capabilities mCapabilities
Definition: qgsabstractdatabaseproviderconnection.h:522
QgsAbstractDatabaseProviderConnection::TableProperty::maxCoordinateDimensions
int maxCoordinateDimensions() const
Returns the maximum coordinate dimensions of the geometries of a vector table.
Definition: qgsabstractdatabaseproviderconnection.cpp:246
QgsAbstractDatabaseProviderConnection::TableProperty::geometryColumnTypes
QList< QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType > geometryColumnTypes() const
Returns the list of geometry column types and CRSs.
Definition: qgsabstractdatabaseproviderconnection.cpp:210
QgsAbstractDatabaseProviderConnection::TableProperty::crsList
QList< QgsCoordinateReferenceSystem > crsList() const
Returns the list of CRSs supported by the geometry column.
Definition: qgsabstractdatabaseproviderconnection.cpp:315
QgsAbstractDatabaseProviderConnection::dropVectorTable
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...
Definition: qgsabstractdatabaseproviderconnection.cpp:85
QgsAbstractDatabaseProviderConnection::table
virtual QgsAbstractDatabaseProviderConnection::TableProperty table(const QString &schema, const QString &table) const
Returns information on a table in the given schema.
Definition: qgsabstractdatabaseproviderconnection.cpp:158
QgsProviderConnectionException
Definition: qgsexception.h:100
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsAbstractDatabaseProviderConnection::vacuum
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...
Definition: qgsabstractdatabaseproviderconnection.cpp:130
QgsAbstractDatabaseProviderConnection::deleteSpatialIndex
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 ...
Definition: qgsabstractdatabaseproviderconnection.cpp:140
QgsAbstractDatabaseProviderConnection::executeSql
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.
Definition: qgsabstractdatabaseproviderconnection.cpp:124
QgsAbstractDatabaseProviderConnection::SpatialIndexOptions
Contains extra options relating to spatial index creation.
Definition: qgsabstractdatabaseproviderconnection.h:436
QgsAbstractDatabaseProviderConnection::TableProperty::setFlags
void setFlags(const TableFlags &flags)
Sets the table flags.
Definition: qgsabstractdatabaseproviderconnection.cpp:310
QgsAbstractDatabaseProviderConnection::TableProperty::comment
QString comment() const
Returns the table comment.
Definition: qgsabstractdatabaseproviderconnection.cpp:295
QgsAbstractDatabaseProviderConnection::TableProperty::tableName
QString tableName() const
Returns the table name.
Definition: qgsabstractdatabaseproviderconnection.cpp:186
QgsAbstractDatabaseProviderConnection::Capability
Capability
The Capability enum represent the operations supported by the connection.
Definition: qgsabstractdatabaseproviderconnection.h:290
QgsAbstractDatabaseProviderConnection::TableProperty::at
TableProperty at(int index) const
Returns the table property corresponding to the geometry type at the given index.
Definition: qgsabstractdatabaseproviderconnection.cpp:223
QgsAbstractDatabaseProviderConnection::TableProperty::setTableName
void setTableName(const QString &name)
Sets the table name to name.
Definition: qgsabstractdatabaseproviderconnection.cpp:191
QgsAbstractDatabaseProviderConnection::TableProperty::setInfo
void setInfo(const QVariantMap &info)
Sets additional information about the table to info.
Definition: qgsabstractdatabaseproviderconnection.cpp:290
QgsAbstractDatabaseProviderConnection::createVectorTable
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...
Definition: qgsabstractdatabaseproviderconnection.cpp:56
QgsAbstractDatabaseProviderConnection::TableProperty::geometryColumnCount
int geometryColumnCount() const
Returns the number of geometry columns in the original table this entry refers to.
Definition: qgsabstractdatabaseproviderconnection.cpp:275
QgsAbstractDatabaseProviderConnection::TableFlag
TableFlag
Flags for table properties.
Definition: qgsabstractdatabaseproviderconnection.h:67
qgsexception.h
QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType
The GeometryColumnType struct represents the combination of geometry type and CRS for the table geome...
Definition: qgsabstractdatabaseproviderconnection.h:107
QgsAbstractProviderConnection
The QgsAbstractProviderConnection provides an interface for data provider connections.
Definition: qgsabstractproviderconnection.h:44
QgsAbstractDatabaseProviderConnection::renameVectorTable
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...
Definition: qgsabstractdatabaseproviderconnection.cpp:75
QgsAbstractDatabaseProviderConnection::TableProperty::flags
TableFlags flags() const
Returns the table flags.
Definition: qgsabstractdatabaseproviderconnection.cpp:305
QgsAbstractDatabaseProviderConnection::TableProperty::setComment
void setComment(const QString &comment)
Sets the table comment.
Definition: qgsabstractdatabaseproviderconnection.cpp:300
qgsabstractdatabaseproviderconnection.h
QgsAbstractDatabaseProviderConnection::TableProperty::setGeometryColumn
void setGeometryColumn(const QString &geometryColumn)
Sets the geometry column name to geometryColumn.
Definition: qgsabstractdatabaseproviderconnection.cpp:340
QgsAbstractDatabaseProviderConnection::dropRasterTable
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.
Definition: qgsabstractdatabaseproviderconnection.cpp:104
QgsAbstractDatabaseProviderConnection::TableProperty::schema
QString schema() const
Returns the schema or an empty string for backends that do not support a schema.
Definition: qgsabstractdatabaseproviderconnection.cpp:345
QgsAbstractDatabaseProviderConnection::schemas
virtual QStringList schemas() const SIP_THROW(QgsProviderConnectionException)
Returns information about the existing schemas.
Definition: qgsabstractdatabaseproviderconnection.cpp:180
QgsAbstractDatabaseProviderConnection::TableProperty::operator==
bool operator==(const QgsAbstractDatabaseProviderConnection::TableProperty &other) const
Definition: qgsabstractdatabaseproviderconnection.cpp:256
QgsAbstractDatabaseProviderConnection::tableUri
virtual QString tableUri(const QString &schema, const QString &name) const SIP_THROW(QgsProviderConnectionException)
Returns the URI string for the given table and schema.
Definition: qgsabstractdatabaseproviderconnection.cpp:37
QgsAbstractDatabaseProviderConnection::capabilities
Capabilities capabilities() const
Returns connection capabilities.
Definition: qgsabstractdatabaseproviderconnection.cpp:32