64 :
QgsVectorLayer( QString(
"%1|layername=%2" ).arg( filename, table ), QString(
"%1_auxiliarystorage" ).arg( table ),
"ogr" )
65 , mFileName( filename )
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 )
272 c.setProperty( property, prop );
273 settings.setDataDefinedProperties( c );
290 if ( def.
origin().compare(
"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 = 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;
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() )
508 mFileName = filenameForProject( project );
515 : mFileName( filename )
525 QFile::remove( mTmpFileName );
540 if ( mFileName.isEmpty() )
547 if ( QFile::exists( mFileName ) )
548 QFile::remove( mFileName );
550 return QFile::copy( mTmpFileName, mFileName );
565 if ( mValid && layer )
567 const QString table( layer->
id() );
571 if ( !tableExists( table, database.get() ) )
573 if ( !createTable( field.
typeName(), table, database.get() ) )
594 database = openDB( uri.
database() );
598 QString sql = QString(
"DROP TABLE %1" ).arg( uri.
table() );
599 rc = exec( sql, database.get() );
601 sql = QStringLiteral(
"VACUUM" );
602 rc = exec( sql, database.get() );
617 database = openDB( uri.
database() );
621 QString sql = QStringLiteral(
"CREATE TABLE %1 AS SELECT * FROM %2" ).arg( newTable, uri.
table() );
622 rc = exec( sql, database.get() );
636 mErrorString.clear();
638 QFile dest( filename );
639 if ( dest.exists() && !dest.remove() )
641 mErrorString = dest.errorString();
646 if ( !origin.copy( filename ) )
648 mErrorString = origin.errorString();
657 return saveAs( filenameForProject( project ) );
667 const QFileInfo fileinfo( filenameForProject( project ) );
668 return fileinfo.exists() && fileinfo.isFile();
671 bool QgsAuxiliaryStorage::exec(
const QString &sql,
sqlite3 *handler )
677 const int err = sqlite3_exec( handler, sql.toStdString().c_str(),
nullptr,
nullptr, nullptr );
679 if ( err == SQLITE_OK )
682 debugMsg( sql, handler );
688 void QgsAuxiliaryStorage::debugMsg(
const QString &sql,
sqlite3 *handler )
691 const QString err = QString::fromUtf8( sqlite3_errmsg( handler ) );
692 const QString msg = QObject::tr(
"Unable to execute" );
693 const QString errMsg = QObject::tr(
"%1 '%2': %3" ).arg( msg, sql, err );
701 bool QgsAuxiliaryStorage::createTable(
const QString &type,
const QString &table,
sqlite3 *handler )
703 const QString sql = QStringLiteral(
"CREATE TABLE IF NOT EXISTS '%1' ( '%2' %3 )" ).arg( table,
AS_JOINFIELD, type );
705 if ( !exec( sql, handler ) )
716 rc = database.
open_v2( filename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr );
719 debugMsg( QStringLiteral(
"sqlite3_open_v2" ), database.get() );
723 exec( QStringLiteral(
"PRAGMA foreign_keys = 1" ), database.get() );
731 int rc = database.
open_v2( filename, SQLITE_OPEN_READWRITE,
nullptr );
735 debugMsg(
"sqlite3_open_v2", database.get() );
741 bool QgsAuxiliaryStorage::tableExists(
const QString &table,
sqlite3 *handler )
743 const QString sql = QStringLiteral(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='%1'" ).arg( table );
746 char **results =
nullptr;
747 const int rc = sqlite3_get_table( handler, sql.toStdString().c_str(), &results, &rows, &columns, nullptr );
748 if ( rc != SQLITE_OK )
750 debugMsg( sql, handler );
754 sqlite3_free_table( results );
765 if ( filename.isEmpty() )
770 else if ( QFile::exists( filename ) )
773 QFile::copy( filename, mTmpFileName );
789 return open( filenameForProject( project ) );
792 QString QgsAuxiliaryStorage::filenameForProject(
const QgsProject &project )
795 const QString path = info.path() + QDir::separator() + info.baseName();
799 void QgsAuxiliaryStorage::initTmpFileName()
801 QTemporaryFile tmpFile;
804 mTmpFileName = tmpFile.fileName();
809 if ( mCopy || mFileName.isEmpty() )
821 QStringList uriParts = uri.
uri().split(
'|' );
822 if ( uriParts.count() < 2 )
825 const QString databasePath = uriParts[0].replace(
' ',
"" );
827 const QString table = uriParts[1];
828 QStringList tableParts = table.split(
' ' );
830 if ( tableParts.count() < 1 )
833 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.
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.
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.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
int count() const
Returns 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
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) ...
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.
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.
Definition for a property.
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
QString uri(bool expandAuthConfig=true) const
Returns complete uri.
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 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
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
Query 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 PostgreSQL/RDBMS datasource URI.
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) ...
void setAttributeTableConfig(const QgsAttributeTableConfig &attributeTableConfig)
Set 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.
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 database.
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.