QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 
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 
135 QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsAbstractDatabaseProviderConnection::tables( const QString &, const QgsAbstractDatabaseProviderConnection::TableFlags & ) const
136 {
137  checkCapability( Capability::Tables );
138  return QList<QgsAbstractDatabaseProviderConnection::TableProperty>();
139 }
140 
141 
143 {
144  checkCapability( Capability::Tables );
145  const QList<QgsAbstractDatabaseProviderConnection::TableProperty> constTables { tables( schema ) };
146  for ( const auto &t : constTables )
147  {
148  if ( t.tableName() == name )
149  {
150  return t;
151  }
152  }
153  throw QgsProviderConnectionException( QObject::tr( "Table '%1' was not found in schema '%2'" )
154  .arg( name )
155  .arg( schema ) );
156 }
157 
158 QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsAbstractDatabaseProviderConnection::tablesInt( const QString &schema, const int flags ) const
159 {
160  return tables( schema, static_cast<QgsAbstractDatabaseProviderConnection::TableFlags>( flags ) );
161 }
162 
163 
165 {
166  checkCapability( Capability::Schemas );
167  return QStringList();
168 }
169 
171 {
172  return mTableName;
173 }
174 
176 {
177  mTableName = name;
178 }
179 
181 {
182  // Do not add the type if it's already present
184  for ( const auto &t : qgis::as_const( mGeometryColumnTypes ) )
185  {
186  if ( t == toAdd )
187  {
188  return;
189  }
190  }
191  mGeometryColumnTypes.push_back( toAdd );
192 }
193 
194 QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> QgsAbstractDatabaseProviderConnection::TableProperty::geometryColumnTypes() const
195 {
196  return mGeometryColumnTypes;
197 }
198 
199 
201 {
202  QString n = mTableName;
203  if ( mGeometryColumnCount > 1 ) n += '.' + mGeometryColumn;
204  return n;
205 }
206 
208 {
209  TableProperty property;
210 
211  Q_ASSERT( index >= 0 && index < mGeometryColumnTypes.size() );
212 
213  property.mGeometryColumnTypes << mGeometryColumnTypes[ index ];
214  property.mSchema = mSchema;
215  property.mTableName = mTableName;
216  property.mGeometryColumn = mGeometryColumn;
217  property.mPkColumns = mPkColumns;
218  property.mGeometryColumnCount = mGeometryColumnCount;
219  property.mFlags = mFlags;
220  property.mComment = mComment;
221  property.mInfo = mInfo;
222  return property;
223 }
224 
226 {
227  mFlags.setFlag( flag );
228 }
229 
231 {
232  int res = 0;
233  for ( const TableProperty::GeometryColumnType &ct : qgis::as_const( mGeometryColumnTypes ) )
234  {
235  res = std::max( res, QgsWkbTypes::coordDimensions( ct.wkbType ) );
236  }
237  return res;
238 }
239 
240 
241 void QgsAbstractDatabaseProviderConnection::TableProperty::setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &columnTypes )
242 {
243  mGeometryColumnTypes = columnTypes;
244 }
245 
246 
248 {
249  return mGeometryColumnCount;
250 }
251 
253 {
254  mGeometryColumnCount = geometryColumnCount;
255 }
256 
258 {
259  return mInfo;
260 }
261 
263 {
264  mInfo = info;
265 }
266 
268 {
269  return mComment;
270 }
271 
273 {
274  mComment = comment;
275 }
276 
277 QgsAbstractDatabaseProviderConnection::TableFlags QgsAbstractDatabaseProviderConnection::TableProperty::flags() const
278 {
279  return mFlags;
280 }
281 
282 void QgsAbstractDatabaseProviderConnection::TableProperty::setFlags( const QgsAbstractDatabaseProviderConnection::TableFlags &flags )
283 {
284  mFlags = flags;
285 }
286 
287 QList<QgsCoordinateReferenceSystem> QgsAbstractDatabaseProviderConnection::TableProperty::crsList() const
288 {
289  QList<QgsCoordinateReferenceSystem> crss;
290  for ( const auto &t : qgis::as_const( mGeometryColumnTypes ) )
291  {
292  crss.push_back( t.crs );
293  }
294  return crss;
295 }
296 
298 {
299  return mPkColumns;
300 }
301 
303 {
304  mPkColumns = pkColumns;
305 }
306 
308 {
309  return mGeometryColumn;
310 }
311 
313 {
314  mGeometryColumn = geometryColumn;
315 }
316 
318 {
319  return mSchema;
320 }
321 
323 {
324  mSchema = schema;
325 }
326 
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.
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:42
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.
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 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...
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.
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.
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.
Capabilities capabilities() const
Returns connection capabilities.
void setComment(const QString &comment)
Sets the table comment.
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.
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.
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:799
QVariantMap info() const
Returns additional information about the table.