QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
38 void QgsAbstractDatabaseProviderConnection::checkCapability( QgsAbstractDatabaseProviderConnection::Capability capability ) const
39 {
40  if ( ! mCapabilities.testFlag( capability ) )
41  {
42  static QMetaEnum metaEnum = QMetaEnum::fromType<QgsAbstractDatabaseProviderConnection::Capability>();
43  const QString capName { metaEnum.valueToKey( capability ) };
44  throw QgsProviderConnectionException( QObject::tr( "Operation '%1' is not supported for this connection" ).arg( capName ) );
45  }
46 }
48 
50  const QString &name,
51  const QgsFields &fields,
52  QgsWkbTypes::Type wkbType,
54  bool overwrite,
55  const QMap<QString, QVariant> *
56  options ) const
57 {
58  Q_UNUSED( schema );
59  Q_UNUSED( name );
60  Q_UNUSED( fields );
61  Q_UNUSED( srs );
62  Q_UNUSED( overwrite );
63  Q_UNUSED( options );
64  Q_UNUSED( wkbType );
65  throw QgsProviderConnectionException( QObject::tr( "Operation 'createVectorTable' is not supported" ) );
66 }
67 
68 void QgsAbstractDatabaseProviderConnection::renameVectorTable( const QString &, const QString &, const QString & ) const
69 {
70  checkCapability( Capability::RenameVectorTable );
71 }
72 
73 void QgsAbstractDatabaseProviderConnection::renameRasterTable( const QString &, const QString &, const QString & ) const
74 {
75  checkCapability( Capability::RenameRasterTable );
76 }
77 
78 void QgsAbstractDatabaseProviderConnection::dropVectorTable( const QString &, const QString & ) const
79 {
80  checkCapability( Capability::DropVectorTable );
81 }
82 
83 bool QgsAbstractDatabaseProviderConnection::tableExists( const QString &schema, const QString &name ) const
84 {
85  checkCapability( Capability::TableExists );
86  const QList<QgsAbstractDatabaseProviderConnection::TableProperty> constTables { tables( schema ) };
87  for ( const auto &t : constTables )
88  {
89  if ( t.tableName() == name )
90  {
91  return true;
92  }
93  }
94  return false;
95 }
96 
97 void QgsAbstractDatabaseProviderConnection::dropRasterTable( const QString &, const QString & ) const
98 {
99  checkCapability( Capability::DropRasterTable );
100 }
101 
103 {
104  checkCapability( Capability::CreateSchema );
105 }
106 
107 void QgsAbstractDatabaseProviderConnection::dropSchema( const QString &, bool ) const
108 {
109  checkCapability( Capability::DropSchema );
110 }
111 
112 void QgsAbstractDatabaseProviderConnection::renameSchema( const QString &, const QString & ) const
113 {
114  checkCapability( Capability::RenameSchema );
115 }
116 
117 QList<QList<QVariant>> QgsAbstractDatabaseProviderConnection::executeSql( const QString & ) const
118 {
119  checkCapability( Capability::ExecuteSql );
120  return QList<QList<QVariant>>();
121 }
122 
123 void QgsAbstractDatabaseProviderConnection::vacuum( const QString &, const QString & ) const
124 {
125  checkCapability( Capability::Vacuum );
126 }
127 
128 QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsAbstractDatabaseProviderConnection::tables( const QString &, const QgsAbstractDatabaseProviderConnection::TableFlags & ) const
129 {
130  checkCapability( Capability::Tables );
131  return QList<QgsAbstractDatabaseProviderConnection::TableProperty>();
132 }
133 
134 QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsAbstractDatabaseProviderConnection::tablesInt( const QString &schema, const int flags ) const
135 {
136  return tables( schema, static_cast<QgsAbstractDatabaseProviderConnection::TableFlags>( flags ) );
137 }
138 
139 
141 {
142  checkCapability( Capability::Schemas );
143  return QStringList();
144 }
145 
147 {
148  return mTableName;
149 }
150 
152 {
153  mTableName = name;
154 }
155 
157 {
158  // Do not add the type if it's already present
160  for ( const auto &t : qgis::as_const( mGeometryColumnTypes ) )
161  {
162  if ( t == toAdd )
163  {
164  return;
165  }
166  }
167  mGeometryColumnTypes.push_back( toAdd );
168 }
169 
170 QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> QgsAbstractDatabaseProviderConnection::TableProperty::geometryColumnTypes() const
171 {
172  return mGeometryColumnTypes;
173 }
174 
175 
177 {
178  QString n = mTableName;
179  if ( mGeometryColumnCount > 1 ) n += '.' + mGeometryColumn;
180  return n;
181 }
182 
184 {
185  TableProperty property;
186 
187  Q_ASSERT( index >= 0 && index < mGeometryColumnTypes.size() );
188 
189  property.mGeometryColumnTypes << mGeometryColumnTypes[ index ];
190  property.mSchema = mSchema;
191  property.mTableName = mTableName;
192  property.mGeometryColumn = mGeometryColumn;
193  property.mPkColumns = mPkColumns;
194  property.mGeometryColumnCount = mGeometryColumnCount;
195  property.mFlags = mFlags;
196  property.mComment = mComment;
197  property.mInfo = mInfo;
198  return property;
199 }
200 
202 {
203  mFlags.setFlag( flag );
204 }
205 
207 {
208  int res = 0;
209  for ( const TableProperty::GeometryColumnType &ct : qgis::as_const( mGeometryColumnTypes ) )
210  {
211  res = std::max( res, QgsWkbTypes::coordDimensions( ct.wkbType ) );
212  }
213  return res;
214 }
215 
216 
217 void QgsAbstractDatabaseProviderConnection::TableProperty::setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &columnTypes )
218 {
219  mGeometryColumnTypes = columnTypes;
220 }
221 
222 
224 {
225  return mGeometryColumnCount;
226 }
227 
229 {
230  mGeometryColumnCount = geometryColumnCount;
231 }
232 
234 {
235  return mInfo;
236 }
237 
239 {
240  mInfo = info;
241 }
242 
244 {
245  return mComment;
246 }
247 
249 {
250  mComment = comment;
251 }
252 
253 QgsAbstractDatabaseProviderConnection::TableFlags QgsAbstractDatabaseProviderConnection::TableProperty::flags() const
254 {
255  return mFlags;
256 }
257 
258 void QgsAbstractDatabaseProviderConnection::TableProperty::setFlags( const QgsAbstractDatabaseProviderConnection::TableFlags &flags )
259 {
260  mFlags = flags;
261 }
262 
263 QList<QgsCoordinateReferenceSystem> QgsAbstractDatabaseProviderConnection::TableProperty::crsList() const
264 {
265  QList<QgsCoordinateReferenceSystem> crss;
266  for ( const auto &t : qgis::as_const( mGeometryColumnTypes ) )
267  {
268  crss.push_back( t.crs );
269  }
270  return crss;
271 }
272 
274 {
275  return mPkColumns;
276 }
277 
279 {
280  mPkColumns = pkColumns;
281 }
282 
284 {
285  return mGeometryColumn;
286 }
287 
289 {
290  mGeometryColumn = geometryColumn;
291 }
292 
294 {
295  return mSchema;
296 }
297 
299 {
300  mSchema = schema;
301 }
302 
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 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 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.