64 :
QgsVectorLayer( QString(
"%1|layername=%2" ).arg( filename, table ), QString(
"%1_auxiliarystorage" ).arg( table ),
"ogr" )
65 , mFileName( filename )
106 QString filter = QgsExpression::createFieldEqualityExpression( pkField, joinFeature.
attribute(
AS_JOINFIELD ) );
137 if ( ( definition.
name().isEmpty() && definition.
comment().isEmpty() ) ||
exists( definition ) )
150 if ( index >= 0 && auxIndex >= 0 )
161 QVector<QgsAttributeTableConfig::ColumnConfig> columns = attrCfg.
columns();
162 QVector<QgsAttributeTableConfig::ColumnConfig>::iterator it;
164 for ( it = columns.begin(); it != columns.end(); ++it )
236 for (
const QString &providerId : subProviderIds )
273 settings.setDataDefinedProperties( c );
290 if ( def.
origin().compare(
"labeling" ) == 0 )
295 if ( propName.compare( def.
name() ) == 0 )
311 if ( aDef.
origin().compare( QStringLiteral(
"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( QStringLiteral(
"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( QStringLiteral(
"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 = QString(
"%1_%2" ).arg( fieldName, def.
name().toLower() );
371 if ( !def.
comment().isEmpty() )
372 fieldName = QString(
"%1_%2" ).arg( fieldName, def.
comment() );
375 fieldName = QString(
"%1%2" ).arg(
AS_JOINPREFIX, fieldName );
384 if ( !def.
name().isEmpty() || !def.
comment().isEmpty() )
386 QVariant::Type
type = QVariant::Invalid;
388 int len( 0 ), precision( 0 );
392 type = QVariant::String;
397 type = QVariant::Double;
403 type = QVariant::Int;
404 typeName =
"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(
"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(
"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(
"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 )
476 if ( parts.size() == 3 )
488 if ( !def.
name().isEmpty() || !def.
comment().isEmpty() )
506 if ( !project.
fileInfo().fileName().isEmpty() )
508 const QFileInfo info = project.
fileInfo();
509 const QString path = info.path() + QDir::separator() + info.baseName();
511 mFileName = asFileName;
518 : mFileName( filename )
528 QFile::remove( mTmpFileName );
543 if ( mFileName.isEmpty() )
550 if ( QFile::exists( mFileName ) )
551 QFile::remove( mFileName );
553 return QFile::copy( mTmpFileName, mFileName );
568 if ( mValid && layer )
570 const QString table( layer->
id() );
574 if ( !tableExists( table, database.get() ) )
576 if ( !createTable( field.
typeName(), table, database.get() ) )
597 database = openDB( uri.
database() );
601 QString sql = QString(
"DROP TABLE %1" ).arg( uri.
table() );
602 rc = exec( sql, database.get() );
604 sql = QStringLiteral(
"VACUUM" );
605 rc = exec( sql, database.get() );
620 database = openDB( uri.
database() );
624 QString sql = QStringLiteral(
"CREATE TABLE %1 AS SELECT * FROM %2" ).arg( newTable, uri.
table() );
625 rc = exec( sql, database.get() );
634 if ( QFile::exists( filename ) )
635 QFile::remove( filename );
642 return saveAs( filenameForProject( project ) );
650 bool QgsAuxiliaryStorage::exec(
const QString &sql,
sqlite3 *handler )
656 const int err = sqlite3_exec( handler, sql.toStdString().c_str(),
nullptr,
nullptr, nullptr );
658 if ( err == SQLITE_OK )
661 debugMsg( sql, handler );
667 void QgsAuxiliaryStorage::debugMsg(
const QString &sql,
sqlite3 *handler )
670 const QString err = QString::fromUtf8( sqlite3_errmsg( handler ) );
671 const QString msg = QObject::tr(
"Unable to execute" );
672 const QString errMsg = QObject::tr(
"%1 '%2': %3" ).arg( msg, sql, err );
680 bool QgsAuxiliaryStorage::createTable(
const QString &type,
const QString &table,
sqlite3 *handler )
682 const QString sql = QStringLiteral(
"CREATE TABLE IF NOT EXISTS '%1' ( '%2' %3 )" ).arg( table,
AS_JOINFIELD, type );
684 if ( !exec( sql, handler ) )
695 rc = database.
open_v2( filename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr );
698 debugMsg( QStringLiteral(
"sqlite3_open_v2" ), database.get() );
702 exec( QStringLiteral(
"PRAGMA foreign_keys = 1" ), database.get() );
710 int rc = database.
open_v2( filename, SQLITE_OPEN_READWRITE,
nullptr );
714 debugMsg(
"sqlite3_open_v2", database.get() );
720 bool QgsAuxiliaryStorage::tableExists(
const QString &table,
sqlite3 *handler )
722 const QString sql = QStringLiteral(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='%1'" ).arg( table );
725 char **results =
nullptr;
726 const int rc = sqlite3_get_table( handler, sql.toStdString().c_str(), &results, &rows, &columns, nullptr );
727 if ( rc != SQLITE_OK )
729 debugMsg( sql, handler );
733 sqlite3_free_table( results );
744 if ( filename.isEmpty() )
749 else if ( QFile::exists( filename ) )
752 QFile::copy( filename, mTmpFileName );
768 return open( filenameForProject( project ) );
771 QString QgsAuxiliaryStorage::filenameForProject(
const QgsProject &project )
773 const QFileInfo info = project.
fileInfo();
774 const QString path = info.path() + QDir::separator() + info.baseName();
778 void QgsAuxiliaryStorage::initTmpFileName()
780 QTemporaryFile tmpFile;
783 mTmpFileName = tmpFile.fileName();
788 if ( mCopy || mFileName.isEmpty() )
800 QStringList uriParts = uri.
uri().split(
'|' );
801 if ( uriParts.count() < 2 )
804 const QString databasePath = uriParts[0].replace(
' ',
"" );
806 const QString table = uriParts[1];
807 QStringList tableParts = table.split(
' ' );
809 if ( tableParts.count() < 1 )
812 const QString tableName = tableParts[0].replace( QStringLiteral(
"layername=" ), QStringLiteral(
"" ) );
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
int lookupField(const QString &fieldName) const
Look 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.
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.
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.
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.
bool isEditable() const override
Returns true if the provider is in editing mode.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
int count() const
Return number of items.
static QString extension()
Returns the extension used for auxiliary databases.
void styleChanged()
Signal emitted whenever a change affects the layer's style.
virtual QStringList subProviders() const
Get list of sub-providers within the layer's labeling.
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 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 override
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
Get 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.
QgsWkbTypes::Type wkbType() const override
Returns the WKBType or WKBUnknown in case of error.
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.
QgsCoordinateReferenceSystem crs() const
Returns the layer's spatial reference system.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Reads and writes project states.
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.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
A store for object properties.
void setTargetFieldName(const QString &fieldName)
Sets name of the field of our layer that will be used for join.
QgsAuxiliaryLayer * auxiliaryLayer()
Returns the current auxiliary layer.
static bool deleteTable(const QgsDataSourceUri &uri)
Removes a table from the auxiliary storage.
Definition for a property.
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
QString uri(bool expandAuthConfig=true) const
return complete uri
Stores the settings for rendering of all diagrams for a layer.
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Get the list with all columns and their configuration.
QgsAttributeTableConfig attributeTableConfig() const
Get 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())
Set all data source related members at once.
void setType(QVariant::Type type)
Set variant type.
QFileInfo fileInfo() const
Returns QFileInfo object for the project's associated file.
void setPrefix(const QString &prefix)
Sets prefix of fields from the joined layer. If null, 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
Get 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.
void setCascadedDelete(bool enabled)
Sets whether a feature deleted on the target layer has to impact the joined layer by deleting the cor...
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
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.
bool saveAs(const QString &filename) const
Saves the current database to a new path.
Max scale (deprecated, for old project compatibility only)
Class for storing the component parts of a PostgreSQL/RDBMS datasource URI.
bool addAuxiliaryField(const QgsPropertyDefinition &definition)
Adds an auxiliary field for the given property.
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) ...
void setAttributeTableConfig(const QgsAttributeTableConfig &attributeTableConfig)
Set the attribute table configuration object.
QgsField field(int fieldIdx) const
Get 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.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
Adds a single feature to the sink.
QgsAuxiliaryLayer * createAuxiliaryLayer(const QgsField &field, QgsVectorLayer *layer) const
Creates an auxiliary layer for a vector layer.
void setDatabase(const QString &database)
Set database.
QString origin() const
Returns the origin of the property.
StandardPropertyTemplate standardTemplate() const
Returns the property's standard template, if applicable.
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.