67 :
QgsVectorLayer( QStringLiteral(
"%1|layername=%2" ).arg( filename, table ),
68 QStringLiteral(
"%1_auxiliarystorage" ).arg( table ), QStringLiteral(
"ogr" ) )
69 , mFileName( filename )
141 if ( ( definition.
name().isEmpty() && definition.
comment().isEmpty() ) ||
exists( definition ) )
154 if ( index >= 0 && auxIndex >= 0 )
165 QVector<QgsAttributeTableConfig::ColumnConfig> columns = attrCfg.
columns();
166 QVector<QgsAttributeTableConfig::ColumnConfig>::iterator it;
168 for ( it = columns.begin(); it != columns.end(); ++it )
240 for (
const QString &providerId : subProviderIds )
274 c.setProperty( property, prop );
275 settings.setDataDefinedProperties( c );
290 if ( def.
origin().compare( QLatin1String(
"labeling" ) ) == 0 )
295 if ( propName.compare( def.
name() ) == 0 )
311 if ( aDef.
origin().compare( QLatin1String(
"labeling" ) ) == 0 )
314 QgsPropertiesDefinition::const_iterator it = defs.constBegin();
315 for ( ; it != defs.constEnd(); ++it )
317 if ( it->name().compare( aDef.
name(), Qt::CaseInsensitive ) == 0 )
324 else if ( aDef.
origin().compare( QLatin1String(
"symbol" ) ) == 0 )
327 QgsPropertiesDefinition::const_iterator it = defs.constBegin();
328 for ( ; it != defs.constEnd(); ++it )
330 if ( it->name().compare( aDef.
name(), Qt::CaseInsensitive ) == 0 )
337 else if ( aDef.
origin().compare( QLatin1String(
"diagram" ) ) == 0 )
340 QgsPropertiesDefinition::const_iterator it = defs.constBegin();
341 for ( ; it != defs.constEnd(); ++it )
343 if ( it->name().compare( aDef.
name(), Qt::CaseInsensitive ) == 0 )
366 QString fieldName = def.
origin();
368 if ( !def.
name().isEmpty() )
369 fieldName = QStringLiteral(
"%1_%2" ).arg( fieldName, def.
name().toLower() );
371 if ( !def.
comment().isEmpty() )
372 fieldName = QStringLiteral(
"%1_%2" ).arg( fieldName, def.
comment() );
375 fieldName = QStringLiteral(
"%1%2" ).arg(
AS_JOINPREFIX, fieldName );
384 if ( !def.
name().isEmpty() || !def.
comment().isEmpty() )
386 QVariant::Type
type = QVariant::Invalid;
392 type = QVariant::String;
394 typeName = QStringLiteral(
"String" );
397 type = QVariant::Double;
400 typeName = QStringLiteral(
"Real" );
403 type = QVariant::Int;
404 typeName = QStringLiteral(
"Integer" );
421 const QStringList parts = f.
name().split(
'_' );
423 if ( parts.size() <= 1 )
426 const QString origin = parts[0];
427 const QString propertyName = parts[1];
429 if ( origin.compare( QLatin1String(
"labeling" ), Qt::CaseInsensitive ) == 0 )
432 for (
auto it = props.constBegin(); it != props.constEnd(); ++it )
434 if ( it.value().name().compare( propertyName, Qt::CaseInsensitive ) == 0 )
437 if ( parts.size() >= 3 )
443 else if ( origin.compare( QLatin1String(
"symbol" ), Qt::CaseInsensitive ) == 0 )
446 for (
auto it = props.constBegin(); it != props.constEnd(); ++it )
448 if ( it.value().name().compare( propertyName, Qt::CaseInsensitive ) == 0 )
451 if ( parts.size() >= 3 )
457 else if ( origin.compare( QLatin1String(
"diagram" ), Qt::CaseInsensitive ) == 0 )
460 for (
auto it = props.constBegin(); it != props.constEnd(); ++it )
462 if ( it.value().name().compare( propertyName, Qt::CaseInsensitive ) == 0 )
465 if ( parts.size() >= 3 )
477 case QVariant::Double:
485 case QVariant::String:
491 if ( parts.size() >= 3 )
503 if ( !def.
name().isEmpty() || !def.
comment().isEmpty() )
523 mFileName = filenameForProject( project );
530 : mFileName( filename )
540 QFile::remove( mTmpFileName );
555 if ( mFileName.isEmpty() )
562 if ( QFile::exists( mFileName ) )
563 QFile::remove( mFileName );
565 return QFile::copy( mTmpFileName, mFileName );
580 if ( mValid && layer )
582 const QString table( layer->
id() );
586 if ( !tableExists( table, database.get() ) )
588 if ( !createTable( field.
typeName(), table, database.get() ) )
609 database = openDB( uri.
database() );
613 QString sql = QStringLiteral(
"DROP TABLE %1" ).arg( uri.
table() );
614 rc = exec( sql, database.get() );
616 sql = QStringLiteral(
"VACUUM" );
617 rc = exec( sql, database.get() );
632 database = openDB( uri.
database() );
636 QString sql = QStringLiteral(
"CREATE TABLE %1 AS SELECT * FROM %2" ).arg( newTable, uri.
table() );
637 rc = exec( sql, database.get() );
651 mErrorString.clear();
653 QFile dest( filename );
654 if ( dest.exists() && !dest.remove() )
656 mErrorString = dest.errorString();
661 if ( !origin.copy( filename ) )
663 mErrorString = origin.errorString();
672 return saveAs( filenameForProject( project ) );
682 const QFileInfo fileinfo( filenameForProject( project ) );
683 return fileinfo.exists() && fileinfo.isFile();
686 bool QgsAuxiliaryStorage::exec(
const QString &sql,
sqlite3 *handler )
692 const int err = sqlite3_exec( handler, sql.toStdString().c_str(),
nullptr,
nullptr, nullptr );
694 if ( err == SQLITE_OK )
697 debugMsg( sql, handler );
703 void QgsAuxiliaryStorage::debugMsg(
const QString &sql,
sqlite3 *handler )
706 const QString err = QString::fromUtf8( sqlite3_errmsg( handler ) );
707 const QString msg = QObject::tr(
"Unable to execute" );
708 const QString errMsg = QObject::tr(
"%1 '%2': %3" ).arg( msg, sql, err );
716 bool QgsAuxiliaryStorage::createTable(
const QString &type,
const QString &table,
sqlite3 *handler )
718 const QString sql = QStringLiteral(
"CREATE TABLE IF NOT EXISTS '%1' ( '%2' %3 )" ).arg( table,
AS_JOINFIELD, type );
720 if ( !exec( sql, handler ) )
731 rc = database.
open_v2( filename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr );
734 debugMsg( QStringLiteral(
"sqlite3_open_v2" ), database.get() );
738 exec( QStringLiteral(
"PRAGMA foreign_keys = 1" ), database.get() );
746 int rc = database.
open_v2( filename, SQLITE_OPEN_READWRITE,
nullptr );
750 debugMsg( QStringLiteral(
"sqlite3_open_v2" ), database.get() );
756 bool QgsAuxiliaryStorage::tableExists(
const QString &table,
sqlite3 *handler )
758 const QString sql = QStringLiteral(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='%1'" ).arg( table );
761 char **results =
nullptr;
762 const int rc = sqlite3_get_table( handler, sql.toStdString().c_str(), &results, &rows, &columns, nullptr );
763 if ( rc != SQLITE_OK )
765 debugMsg( sql, handler );
769 sqlite3_free_table( results );
780 if ( filename.isEmpty() )
785 else if ( QFile::exists( filename ) )
788 QFile::copy( filename, mTmpFileName );
804 return open( filenameForProject( project ) );
807 QString QgsAuxiliaryStorage::filenameForProject(
const QgsProject &project )
810 const QString path = info.path() + QDir::separator() + info.baseName();
814 void QgsAuxiliaryStorage::initTmpFileName()
816 QTemporaryFile tmpFile;
819 mTmpFileName = tmpFile.fileName();
824 if ( mCopy || mFileName.isEmpty() )
836 QStringList uriParts = uri.
uri().split(
'|' );
837 if ( uriParts.count() < 2 )
840 const QString databasePath = uriParts[0].replace(
' ', QString() );
842 const QString table = uriParts[1];
843 QStringList tableParts = table.split(
' ' );
845 if ( tableParts.count() < 1 )
848 const QString tableName = tableParts[0].replace( QStringLiteral(
"layername=" ), QString() );
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
bool isValid() const
Returns the validity of this feature.
void updateFields()
Will regenerate the fields property of this layer by obtaining all fields from the dataProvider...
void setJoinLayer(QgsVectorLayer *layer)
Sets weak reference to the joined layer.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the labeling property definitions.
Wrapper for iterator of features from vector data provider or vector layer.
static QgsField createAuxiliaryField(const QgsPropertyDefinition &definition)
Creates a new auxiliary field from a property definition.
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
void setDiagramLayerSettings(const QgsDiagramLayerSettings &s)
QString table() const
Returns the table name stored in the URI.
void update(const QgsFields &fields)
Update the configuration with the given fields.
Property
Data definable properties.
bool isValid() const
Returns the status of the auxiliary storage currently defined.
QgsMapLayerType type() const
Returns the type of the layer.
bool save()
Commits changes and starts editing then.
QgsPropertyDefinition propertyDefinitionFromIndex(int index) const
Returns the property definition for the underlying field index.
bool exists(const QgsPropertyDefinition &definition) const
Returns true if the property is stored in the layer already, false otherwise.
int propertyFromIndex(int index) const
Returns the underlying property key for the field index.
void setEditable(bool enabled)
Sets whether the form of the target layer allows editing joined fields.
QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
void setPrecision(int precision)
Set the field precision.
Class allowing to manage the auxiliary storage for a vector layer.
bool commitChanges()
Attempts to commit to the underlying data provider any buffered changes made since the last to call t...
int indexOfPropertyDefinition(const QgsPropertyDefinition &definition) const
Returns the index of the auxiliary field for a specific property definition.
const QString AS_JOINFIELD
bool startEditing()
Makes the layer editable.
virtual void setSettings(QgsPalLayerSettings *settings, const QString &providerId=QString())=0
Set pal settings for a specific provider (takes ownership).
static QgsProperty fromField(const QString &fieldName, bool isActive=true)
Returns a new FieldBasedProperty created from the specified field name.
bool deleteFeatures(const QgsFeatureIds &fids)
Deletes a set of features from the layer (but does not commit it)
X-coordinate data defined label position.
void setJoinFieldName(const QString &fieldName)
Sets name of the field of joined layer that will be used for join.
Min scale (deprecated, for old project compatibility only)
Container of fields for a vector layer.
void setName(const QString &name)
Set the field name.
const QString AS_EXTENSION
Color with alpha channel.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QString currentFileName() const
Returns the path of the current database used.
QgsVectorLayer * toSpatialLayer() const
An auxiliary layer is not spatial.
void setName(const QString &name)
Sets the name of the property.
int count() const
Returns number of items.
static QString extension()
Returns the extension used for auxiliary databases.
virtual QStringList subProviders() const
Gets list of sub-providers within the layer's labeling.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
static int createProperty(QgsPalLayerSettings::Property property, QgsVectorLayer *vlayer)
Creates if necessary a new auxiliary field for a PAL property and activates this property in settings...
void setLength(int len)
Set the field length.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
bool save() const
Saves the current database.
Property
Data definable properties.
void setUpsertOnEdit(bool enabled)
Sets whether a feature created on the target layer has to impact the joined layer by creating a new f...
void setEditorWidgetSetup(int index, const QgsEditorWidgetSetup &setup)
The editor widget setup defines which QgsFieldFormatter and editor widget will be used for the field ...
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
DataType dataType() const
Returns the allowable field/value data type for the property.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Unique pointer for spatialite databases, which automatically closes the database when the pointer goe...
const QVector< QgsPalLayerSettings::Property > palHiddenProperties
QString fileName() const
Returns the target filename of the database.
virtual QgsPalLayerSettings settings(const QString &providerId=QString()) const =0
Gets associated label settings.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the label's property collection, used for data defined overrides.
Property requires a boolean value.
QString typeName() const
Gets the field type.
void setTypeName(const QString &typeName)
Set the field type.
Defines left outer join from our vector layer to some other vector layer.
virtual bool deleteAttribute(int attr)
Deletes an attribute field (but does not commit it).
const QString AS_JOINPREFIX
virtual ~QgsAuxiliaryStorage()
Destructor.
Horizontal alignment for data defined label position (Left, Center, Right)
Property requires a numeric value.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsVectorLayerJoinInfo joinInfo() const
Returns information to use for joining with primary key and so on.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false) ...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Encapsulate a field in an attribute table or data source.
bool clear()
Deletes all features from the layer.
QgsAuxiliaryLayer(const QString &pkField, const QString &filename, const QString &table, QgsVectorLayer *vlayer)
Constructor.
const QgsAbstractVectorLayerLabeling * labeling() const
Access to const labeling configuration.
A store for object properties.
void setTargetFieldName(const QString &fieldName)
Sets name of the field of our layer that will be used for join.
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value)
Create an expression allowing to evaluate if a field is equal to a value.
QgsAuxiliaryLayer * auxiliaryLayer()
Returns the current auxiliary layer.
static bool deleteTable(const QgsDataSourceUri &uri)
Removes a table from the auxiliary storage.
void setDataType(DataType type)
Sets the data type.
Definition for a property.
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
static bool exists(const QgsProject &project)
Returns true if the auxiliary database yet exists for a project, false otherwise. ...
Stores the settings for rendering of all diagrams for a layer.
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Gets the list with all columns and their configuration.
QgsAttributeTableConfig attributeTableConfig() const
Returns the attribute table configuration object.
QString comment() const
Returns the comment of the property.
void setDataSource(const QString &aSchema, const QString &aTable, const QString &aGeometryColumn, const QString &aSql=QString(), const QString &aKeyColumn=QString())
Sets all data source related members at once.
void setType(QVariant::Type type)
Set variant type.
void setPrefix(const QString &prefix)
Sets prefix of fields from the joined layer. If nullptr, joined layer's name will be used...
QgsEditorWidgetSetup editorWidgetSetup(int index) const
The editor widget setup defines which QgsFieldFormatter and editor widget will be used for the field ...
void setColumns(const QVector< QgsAttributeTableConfig::ColumnConfig > &columns)
Set the list of columns visible in the attribute table.
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Property requires a string value.
QString source() const
Returns the source for the layer.
QgsAuxiliaryStorage(const QgsProject &project, bool copy=true)
Constructor.
const QgsDiagramLayerSettings * diagramLayerSettings() const
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the diagram property definitions.
void setOrigin(const QString &origin)
Sets the origin of the property.
static QString nameFromProperty(const QgsPropertyDefinition &def, bool joined=false)
Returns the name of the auxiliary field for a property definition.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
void setComment(const QString &comment)
Sets comment of the property.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) FINAL
Adds a single feature to the sink.
void setCascadedDelete(bool enabled)
Sets whether a feature deleted on the target layer has to impact the joined layer by deleting the cor...
QString errorString() const
Returns the underlying error string describing potential errors happening in saveAs().
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
void setJoinFieldNamesBlackList(const QStringList &blackList)
Sets a list of fields to ignore whatever happens.
bool deleteAttribute(int attr) override
Removes attribute from the layer and commits changes.
static bool duplicateTable(const QgsDataSourceUri &uri, const QString &newTable)
Duplicates a table and its content.
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, QgsWkbTypes::Type geometryType=QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Creates a new memory layer using the specified parameters.
QgsFields auxiliaryFields() const
Returns a list of all auxiliary fields currently managed by the layer.
Y-coordinate data defined label position.
bool nextFeature(QgsFeature &f)
This is a container for configuration of the attribute table.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the symbol layer property definitions.
Max scale (deprecated, for old project compatibility only)
Class for storing the component parts of a RDBMS data source URI (e.g.
bool addAuxiliaryField(const QgsPropertyDefinition &definition)
Adds an auxiliary field for the given property.
QString absoluteFilePath() const
Returns full absolute path to the project file if the project is stored in a file system - derived fr...
static QgsPropertyDefinition propertyDefinitionFromField(const QgsField &field)
Returns the property definition from an auxiliary field.
QString name() const
Returns the name of the property.
Represents a vector layer which manages a vector based data sets.
bool addAttribute(const QgsField &field)
Add an attribute field (but does not commit it) returns true if the field was added.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
Vertical alignment for data defined label position (Bottom, Base, Half, Cap, Top) ...
Whether all parts of multi-part features should be labeled.
void setAttributeTableConfig(const QgsAttributeTableConfig &attributeTableConfig)
Sets the attribute table configuration object.
QgsField field(int fieldIdx) const
Gets field at particular index (must be in range 0..N-1)
bool isHiddenProperty(int index) const
Returns true if the underlying field has to be hidden from editing tools like attribute table...
QString database() const
Returns the database name stored in the URI.
QgsAuxiliaryLayer * createAuxiliaryLayer(const QgsField &field, QgsVectorLayer *layer) const
Creates an auxiliary layer for a vector layer.
bool saveAs(const QString &filename)
Saves the current database to a new path.
void setDatabase(const QString &database)
Sets the URI database name.
QString origin() const
Returns the origin of the property.
StandardPropertyTemplate standardTemplate() const
Returns the property's standard template, if applicable.
QgsCoordinateReferenceSystem crs
QgsVectorLayer * clone() const override
Returns a new instance equivalent to this one.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the label's property collection, used for data defined overrides.
Color with no alpha channel.