64 :
QgsVectorLayer( QStringLiteral(
"%1|layername=%2" ).arg( filename, table ),
65 QStringLiteral(
"%1_auxiliarystorage" ).arg( table ), QStringLiteral(
"ogr" ) )
66 , mFileName( filename )
138 if ( ( definition.
name().isEmpty() && definition.
comment().isEmpty() ) ||
exists( definition ) )
151 if ( index >= 0 && auxIndex >= 0 )
162 QVector<QgsAttributeTableConfig::ColumnConfig> columns = attrCfg.
columns();
163 QVector<QgsAttributeTableConfig::ColumnConfig>::iterator it;
165 for ( it = columns.begin(); it != columns.end(); ++it )
237 for (
const QString &providerId : subProviderIds )
273 c.setProperty( property, prop );
274 settings.setDataDefinedProperties( c );
291 if ( def.
origin().compare(
"labeling" ) == 0 )
296 if ( propName.compare( def.
name() ) == 0 )
312 if ( aDef.
origin().compare( QLatin1String(
"labeling" ) ) == 0 )
315 QgsPropertiesDefinition::const_iterator it = defs.constBegin();
316 for ( ; it != defs.constEnd(); ++it )
318 if ( it->name().compare( aDef.
name(), Qt::CaseInsensitive ) == 0 )
325 else if ( aDef.
origin().compare( QLatin1String(
"symbol" ) ) == 0 )
328 QgsPropertiesDefinition::const_iterator it = defs.constBegin();
329 for ( ; it != defs.constEnd(); ++it )
331 if ( it->name().compare( aDef.
name(), Qt::CaseInsensitive ) == 0 )
338 else if ( aDef.
origin().compare( QLatin1String(
"diagram" ) ) == 0 )
341 QgsPropertiesDefinition::const_iterator it = defs.constBegin();
342 for ( ; it != defs.constEnd(); ++it )
344 if ( it->name().compare( aDef.
name(), Qt::CaseInsensitive ) == 0 )
367 QString fieldName = def.
origin();
369 if ( !def.
name().isEmpty() )
370 fieldName = QString(
"%1_%2" ).arg( fieldName, def.
name().toLower() );
372 if ( !def.
comment().isEmpty() )
373 fieldName = QString(
"%1_%2" ).arg( fieldName, def.
comment() );
376 fieldName = QString(
"%1%2" ).arg(
AS_JOINPREFIX, fieldName );
385 if ( !def.
name().isEmpty() || !def.
comment().isEmpty() )
387 QVariant::Type
type = QVariant::Invalid;
393 type = QVariant::String;
398 type = QVariant::Double;
404 type = QVariant::Int;
405 typeName =
"Integer";
422 const QStringList parts = f.
name().split(
'_' );
424 if ( parts.size() <= 1 )
427 const QString origin = parts[0];
428 const QString propertyName = parts[1];
430 if ( origin.compare(
"labeling", Qt::CaseInsensitive ) == 0 )
433 for (
auto it = props.constBegin(); it != props.constEnd(); ++it )
435 if ( it.value().name().compare( propertyName, Qt::CaseInsensitive ) == 0 )
438 if ( parts.size() == 3 )
444 else if ( origin.compare(
"symbol", Qt::CaseInsensitive ) == 0 )
447 for (
auto it = props.constBegin(); it != props.constEnd(); ++it )
449 if ( it.value().name().compare( propertyName, Qt::CaseInsensitive ) == 0 )
452 if ( parts.size() == 3 )
458 else if ( origin.compare(
"diagram", Qt::CaseInsensitive ) == 0 )
461 for (
auto it = props.constBegin(); it != props.constEnd(); ++it )
463 if ( it.value().name().compare( propertyName, Qt::CaseInsensitive ) == 0 )
466 if ( parts.size() == 3 )
477 if ( parts.size() == 3 )
489 if ( !def.
name().isEmpty() || !def.
comment().isEmpty() )
509 mFileName = filenameForProject( project );
516 : mFileName( filename )
526 QFile::remove( mTmpFileName );
541 if ( mFileName.isEmpty() )
548 if ( QFile::exists( mFileName ) )
549 QFile::remove( mFileName );
551 return QFile::copy( mTmpFileName, mFileName );
566 if ( mValid && layer )
568 const QString table( layer->
id() );
572 if ( !tableExists( table, database.get() ) )
574 if ( !createTable( field.
typeName(), table, database.get() ) )
595 database = openDB( uri.
database() );
599 QString sql = QString(
"DROP TABLE %1" ).arg( uri.
table() );
600 rc = exec( sql, database.get() );
602 sql = QStringLiteral(
"VACUUM" );
603 rc = exec( sql, database.get() );
618 database = openDB( uri.
database() );
622 QString sql = QStringLiteral(
"CREATE TABLE %1 AS SELECT * FROM %2" ).arg( newTable, uri.
table() );
623 rc = exec( sql, database.get() );
637 mErrorString.clear();
639 QFile dest( filename );
640 if ( dest.exists() && !dest.remove() )
642 mErrorString = dest.errorString();
647 if ( !origin.copy( filename ) )
649 mErrorString = origin.errorString();
658 return saveAs( filenameForProject( project ) );
668 const QFileInfo fileinfo( filenameForProject( project ) );
669 return fileinfo.exists() && fileinfo.isFile();
672 bool QgsAuxiliaryStorage::exec(
const QString &sql,
sqlite3 *handler )
678 const int err = sqlite3_exec( handler, sql.toStdString().c_str(),
nullptr,
nullptr, nullptr );
680 if ( err == SQLITE_OK )
683 debugMsg( sql, handler );
689 void QgsAuxiliaryStorage::debugMsg(
const QString &sql,
sqlite3 *handler )
692 const QString err = QString::fromUtf8( sqlite3_errmsg( handler ) );
693 const QString msg = QObject::tr(
"Unable to execute" );
694 const QString errMsg = QObject::tr(
"%1 '%2': %3" ).arg( msg, sql, err );
702 bool QgsAuxiliaryStorage::createTable(
const QString &type,
const QString &table,
sqlite3 *handler )
704 const QString sql = QStringLiteral(
"CREATE TABLE IF NOT EXISTS '%1' ( '%2' %3 )" ).arg( table,
AS_JOINFIELD, type );
706 if ( !exec( sql, handler ) )
717 rc = database.
open_v2( filename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr );
720 debugMsg( QStringLiteral(
"sqlite3_open_v2" ), database.get() );
724 exec( QStringLiteral(
"PRAGMA foreign_keys = 1" ), database.get() );
732 int rc = database.
open_v2( filename, SQLITE_OPEN_READWRITE,
nullptr );
736 debugMsg(
"sqlite3_open_v2", database.get() );
742 bool QgsAuxiliaryStorage::tableExists(
const QString &table,
sqlite3 *handler )
744 const QString sql = QStringLiteral(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='%1'" ).arg( table );
747 char **results =
nullptr;
748 const int rc = sqlite3_get_table( handler, sql.toStdString().c_str(), &results, &rows, &columns, nullptr );
749 if ( rc != SQLITE_OK )
751 debugMsg( sql, handler );
755 sqlite3_free_table( results );
766 if ( filename.isEmpty() )
771 else if ( QFile::exists( filename ) )
774 QFile::copy( filename, mTmpFileName );
790 return open( filenameForProject( project ) );
793 QString QgsAuxiliaryStorage::filenameForProject(
const QgsProject &project )
796 const QString path = info.path() + QDir::separator() + info.baseName();
800 void QgsAuxiliaryStorage::initTmpFileName()
802 QTemporaryFile tmpFile;
805 mTmpFileName = tmpFile.fileName();
810 if ( mCopy || mFileName.isEmpty() )
822 QStringList uriParts = uri.
uri().split(
'|' );
823 if ( uriParts.count() < 2 )
826 const QString databasePath = uriParts[0].replace(
' ',
"" );
828 const QString table = uriParts[1];
829 QStringList tableParts = table.split(
' ' );
831 if ( tableParts.count() < 1 )
834 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.
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 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) ...
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.