Skip to content

Commit

Permalink
Move constraints to QgsField
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 2, 2016
1 parent f6c1bf7 commit 4efad04
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 58 deletions.
28 changes: 28 additions & 0 deletions python/core/qgsfield.sip
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ class QgsField
%End

public:

/**
* Constraints which may be present on a field.
* @note added in QGIS 3.0
*/
enum Constraint
{
ConstraintNotNull, //!< Field may not be null
ConstraintUnique, //!< Field must have a unique value
};
typedef QFlags<QgsField::Constraint> Constraints;

/** Constructor. Constructs a new QgsField object.
* @param name Field name
* @param type Field variant type, currently supported: String / Int / Double
Expand Down Expand Up @@ -162,6 +174,20 @@ class QgsField
*/
void setDefaultValueExpression( const QString& expression );

/**
* Returns any constraints which are present for the field.
* @note added in QGIS 3.0
* @see setConstraints()
*/
Constraints constraints() const;

/**
* Sets constraints which are present for the field.
* @note added in QGIS 3.0
* @see constraints()
*/
void setConstraints( Constraints constraints );

/** Returns the alias for the field (the friendly displayed name of the field ),
* or an empty string if there is no alias.
* @see setAlias()
Expand Down Expand Up @@ -254,3 +280,5 @@ class QgsField
*/
const QgsEditorWidgetSetup& editorWidgetSetup() const;
}; // class QgsField

QFlags<QgsField::Constraint> operator|(QgsField::Constraint f1, QFlags<QgsField::Constraint> f2);
14 changes: 1 addition & 13 deletions python/core/qgsvectordataprovider.sip
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,6 @@ class QgsVectorDataProvider : QgsDataProvider
/** Bitmask of all provider's editing capabilities */
static const int EditingCapabilities;

/**
* Constraints which may be present on a field.
* @note added in QGIS 3.0
*/
enum Constraint
{
ConstraintNotNull, //!< Field may not be null
ConstraintUnique, //!< Field must have a unique value
};
typedef QFlags<QgsVectorDataProvider::Constraint> Constraints;

/**
* Constructor of the vector provider
* @param uri uniform resource locator (URI) for a dataset
Expand Down Expand Up @@ -246,7 +235,7 @@ class QgsVectorDataProvider : QgsDataProvider
* field index.
* @note added in QGIS 3.0
*/
virtual QgsVectorDataProvider::Constraints fieldConstraints( int fieldIndex ) const;
virtual QgsField::Constraints fieldConstraints( int fieldIndex ) const;

/**
* Changes geometries of existing features
Expand Down Expand Up @@ -452,4 +441,3 @@ class QgsVectorDataProvider : QgsDataProvider
};

QFlags<QgsVectorDataProvider::Capability> operator|(QgsVectorDataProvider::Capability f1, QFlags<QgsVectorDataProvider::Capability> f2);
QFlags<QgsVectorDataProvider::Constraint> operator|(QgsVectorDataProvider::Constraint f1, QFlags<QgsVectorDataProvider::Constraint> f2);
6 changes: 3 additions & 3 deletions src/app/qgsattributetypedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,16 @@ bool QgsAttributeTypeDialog::fieldEditable() const
return isFieldEditableCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setProviderConstraints( QgsVectorDataProvider::Constraints constraints )
void QgsAttributeTypeDialog::setProviderConstraints( QgsField::Constraints constraints )
{
if ( constraints & QgsVectorDataProvider::ConstraintNotNull )
if ( constraints & QgsField::ConstraintNotNull )
{
notNullCheckBox->setChecked( true );
notNullCheckBox->setEnabled( false );
notNullCheckBox->setToolTip( tr( "The provider for this layer has a NOT NULL constraint set on the field." ) );
}

if ( constraints & QgsVectorDataProvider::ConstraintUnique )
if ( constraints & QgsField::ConstraintUnique )
{
mUniqueCheckBox->setChecked( true );
mUniqueCheckBox->setEnabled( false );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsattributetypedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
/**
* Sets any provider side constraints which may affect this field's behaviour.
*/
void setProviderConstraints( QgsVectorDataProvider::Constraints constraints );
void setProviderConstraints( QgsField::Constraints constraints );

/**
* Setter for checkbox for not null
Expand Down
16 changes: 14 additions & 2 deletions src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ void QgsField::setDefaultValueExpression( const QString& expression )
d->defaultValueExpression = expression;
}

QgsField::Constraints QgsField::constraints() const
{
return d->constraints;
}

void QgsField::setConstraints( Constraints constraints )
{
d->constraints = constraints;
}

QString QgsField::alias() const
{
return d->alias;
Expand Down Expand Up @@ -303,15 +313,16 @@ QDataStream& operator<<( QDataStream& out, const QgsField& field )
out << field.comment();
out << field.alias();
out << field.defaultValueExpression();
out << field.constraints();
out << static_cast< quint32 >( field.subType() );
return out;
}

QDataStream& operator>>( QDataStream& in, QgsField& field )
{
quint32 type, subType, length, precision;
quint32 type, subType, length, precision, constraints;
QString name, typeName, comment, alias, defaultValueExpression;
in >> name >> type >> typeName >> length >> precision >> comment >> alias >> defaultValueExpression >> subType;
in >> name >> type >> typeName >> length >> precision >> comment >> alias >> defaultValueExpression >> constraints >> subType;
field.setName( name );
field.setType( static_cast< QVariant::Type >( type ) );
field.setTypeName( typeName );
Expand All @@ -320,6 +331,7 @@ QDataStream& operator>>( QDataStream& in, QgsField& field )
field.setComment( comment );
field.setAlias( alias );
field.setDefaultValueExpression( defaultValueExpression );
field.setConstraints( static_cast< QgsField::Constraints>( constraints ) );
field.setSubType( static_cast< QVariant::Type >( subType ) );
return in;
}
29 changes: 29 additions & 0 deletions src/core/qgsfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,21 @@ class CORE_EXPORT QgsField
Q_PROPERTY( QString name READ name WRITE setName )
Q_PROPERTY( QString alias READ alias WRITE setAlias )
Q_PROPERTY( QString defaultValueExpression READ defaultValueExpression WRITE setDefaultValueExpression )
Q_PROPERTY( Constraints constraints READ constraints WRITE setConstraints )

public:

/**
* Constraints which may be present on a field.
* @note added in QGIS 3.0
*/
enum Constraint
{
ConstraintNotNull = 1, //!< Field may not be null
ConstraintUnique = 1 << 1, //!< Field must have a unique value
};
Q_DECLARE_FLAGS( Constraints, Constraint )

/** Constructor. Constructs a new QgsField object.
* @param name Field name
* @param type Field variant type, currently supported: String / Int / Double
Expand Down Expand Up @@ -208,6 +221,20 @@ class CORE_EXPORT QgsField
*/
void setDefaultValueExpression( const QString& expression );

/**
* Returns any constraints which are present for the field.
* @note added in QGIS 3.0
* @see setConstraints()
*/
Constraints constraints() const;

/**
* Sets constraints which are present for the field.
* @note added in QGIS 3.0
* @see constraints()
*/
void setConstraints( Constraints constraints );

/** Returns the alias for the field (the friendly displayed name of the field ),
* or an empty string if there is no alias.
* @see setAlias()
Expand Down Expand Up @@ -261,6 +288,8 @@ class CORE_EXPORT QgsField

}; // class QgsField

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsField::Constraints )

Q_DECLARE_METATYPE( QgsField )

//! Writes the field to stream out. QGIS version compatibility is not guaranteed.
Expand Down
8 changes: 7 additions & 1 deletion src/core/qgsfield_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class QgsFieldPrivate : public QSharedData
, length( len )
, precision( prec )
, comment( comment )
, constraints( 0 )
{
}

Expand All @@ -69,6 +70,7 @@ class QgsFieldPrivate : public QSharedData
, comment( other.comment )
, alias( other.alias )
, defaultValueExpression( other.defaultValueExpression )
, constraints( other.constraints )
{
}

Expand All @@ -78,7 +80,8 @@ class QgsFieldPrivate : public QSharedData
{
return (( name == other.name ) && ( type == other.type ) && ( subType == other.subType )
&& ( length == other.length ) && ( precision == other.precision )
&& ( alias == other.alias ) && ( defaultValueExpression == other.defaultValueExpression ) );
&& ( alias == other.alias ) && ( defaultValueExpression == other.defaultValueExpression )
&& ( constraints == other.constraints ) );
}

//! Name
Expand Down Expand Up @@ -108,6 +111,9 @@ class QgsFieldPrivate : public QSharedData
//! Default value expression
QString defaultValueExpression;

//! Constraints
QgsField::Constraints constraints;

QgsEditorWidgetSetup editorWidgetSetup;
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectordataprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ QVariant QgsVectorDataProvider::defaultValue( int fieldId ) const
return QVariant();
}

QgsVectorDataProvider::Constraints QgsVectorDataProvider::fieldConstraints( int fieldIndex ) const
QgsField::Constraints QgsVectorDataProvider::fieldConstraints( int fieldIndex ) const
{
Q_UNUSED( fieldIndex );
return 0;
Expand Down
15 changes: 1 addition & 14 deletions src/core/qgsvectordataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,6 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
ChangeAttributeValues | ChangeGeometries | AddAttributes | DeleteAttributes |
RenameAttributes;

/**
* Constraints which may be present on a field.
* @note added in QGIS 3.0
*/
enum Constraint
{
ConstraintNotNull = 1, //!< Field may not be null
ConstraintUnique = 1 << 1, //!< Field must have a unique value
};
Q_DECLARE_FLAGS( Constraints, Constraint )

/**
* Constructor of the vector provider
* @param uri uniform resource locator (URI) for a dataset
Expand Down Expand Up @@ -297,7 +286,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
* field index.
* @note added in QGIS 3.0
*/
virtual Constraints fieldConstraints( int fieldIndex ) const;
virtual QgsField::Constraints fieldConstraints( int fieldIndex ) const;

/**
* Changes geometries of existing features
Expand Down Expand Up @@ -538,7 +527,5 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Capabilities )
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorDataProvider::Constraints )


#endif
16 changes: 16 additions & 0 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4189,3 +4189,19 @@ bool QgsVectorLayer::setDependencies( const QSet<QgsMapLayerDependency>& oDeps )

return true;
}

QgsField::Constraints QgsVectorLayer::fieldConstraints( int fieldIndex ) const
{
if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
return 0;

QgsField::Constraints constraints = mFields.at( fieldIndex ).constraints();

// make sure provider constraints are always present!
if ( mFields.fieldOrigin( fieldIndex ) == QgsFields::OriginProvider )
{
constraints |= mDataProvider->fieldConstraints( mFields.fieldOriginIndex( fieldIndex ) );
}

return constraints;
}
8 changes: 8 additions & 0 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,14 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
QString defaultValueExpression( int index ) const;

/**
* Returns any constraints which are present for a specified
* field index. These constraints may be inherited from the layer's data provider
* or may be set manually on the vector layer from within QGIS.
* @note added in QGIS 3.0
*/
QgsField::Constraints fieldConstraints( int fieldIndex ) const;

/** Calculates a list of unique values contained within an attribute in the layer. Note that
* in some circumstances when unsaved changes are present for the layer then the returned list
* may contain outdated values (for instance when the attribute value in a saved feature has
Expand Down
19 changes: 12 additions & 7 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,14 +998,16 @@ bool QgsPostgresProvider::loadFields()
mAttrPalIndexName.insert( i, fieldName );
mDefaultValues.insert( mAttributeFields.size(), defValMap[tableoid][attnum] );

Constraints constraints = 0;
QgsField newField = QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldPrec, fieldComment, fieldSubType );

QgsField::Constraints constraints = 0;
if ( notNullMap[tableoid][attnum] )
constraints |= ConstraintNotNull;
constraints |= QgsField::ConstraintNotNull;
if ( uniqueMap[tableoid][attnum] )
constraints |= ConstraintUnique;
mFieldConstraints.insert( mAttributeFields.size(), constraints );
constraints |= QgsField::ConstraintUnique;
newField.setConstraints( constraints );

mAttributeFields.append( QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldPrec, fieldComment, fieldSubType ) );
mAttributeFields.append( newField );
}

setEditorWidgets();
Expand Down Expand Up @@ -1736,9 +1738,12 @@ QVariant QgsPostgresProvider::defaultValue( int fieldId ) const
return defVal;
}

QgsVectorDataProvider::Constraints QgsPostgresProvider::fieldConstraints( int fieldIndex ) const
QgsField::Constraints QgsPostgresProvider::fieldConstraints( int fieldIndex ) const
{
return mFieldConstraints.value( fieldIndex, 0 );
if ( fieldIndex < 0 || fieldIndex >= mAttributeFields.count() )
return 0;

return mAttributeFields.at( fieldIndex ).constraints();
}

QString QgsPostgresProvider::paramValue( const QString& fieldValue, const QString &defaultValue ) const
Expand Down
3 changes: 1 addition & 2 deletions src/providers/postgres/qgspostgresprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
QgsAttributeList attributeIndexes() const override;
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
QVariant defaultValue( int fieldId ) const override;
Constraints fieldConstraints( int fieldIndex ) const override;
QgsField::Constraints fieldConstraints( int fieldIndex ) const override;

/** Adds a list of features
@return true in case of success and false in case of failure*/
Expand Down Expand Up @@ -494,7 +494,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
void setTransaction( QgsTransaction* transaction ) override;

QHash<int, QString> mDefaultValues;
QHash<int, QgsVectorDataProvider::Constraints > mFieldConstraints;
};


Expand Down
Loading

0 comments on commit 4efad04

Please sign in to comment.