Skip to content
Permalink
Browse files

Move constraints to QgsField

  • Loading branch information
nyalldawson committed Nov 2, 2016
1 parent f6c1bf7 commit 4efad04bd821da61d79f3501cd3d1b64a4587a04
@@ -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
@@ -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()
@@ -254,3 +280,5 @@ class QgsField
*/
const QgsEditorWidgetSetup& editorWidgetSetup() const;
}; // class QgsField

QFlags<QgsField::Constraint> operator|(QgsField::Constraint f1, QFlags<QgsField::Constraint> f2);
@@ -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
@@ -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
@@ -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);
@@ -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 );
@@ -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
@@ -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;
@@ -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 );
@@ -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;
}
@@ -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
@@ -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()
@@ -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.
@@ -55,6 +55,7 @@ class QgsFieldPrivate : public QSharedData
, length( len )
, precision( prec )
, comment( comment )
, constraints( 0 )
{
}

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

@@ -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
@@ -108,6 +111,9 @@ class QgsFieldPrivate : public QSharedData
//! Default value expression
QString defaultValueExpression;

//! Constraints
QgsField::Constraints constraints;

QgsEditorWidgetSetup editorWidgetSetup;
};

@@ -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;
@@ -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
@@ -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
@@ -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
@@ -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;
}
@@ -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
@@ -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();
@@ -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
@@ -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*/
@@ -494,7 +494,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
void setTransaction( QgsTransaction* transaction ) override;

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


0 comments on commit 4efad04

Please sign in to comment.
You can’t perform that action at this time.