Skip to content

Commit

Permalink
Added constraint KeyFieldIsUniqueInCollectionConstraint that ensures …
Browse files Browse the repository at this point in the history
…that a key field is unique in a collection. Closes #13.
  • Loading branch information
hisystems committed Mar 10, 2012
1 parent cf7d095 commit 7ff02b6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Contraints/KeyFieldIsUniqueInCollectionConstraint.vb
@@ -0,0 +1,77 @@
' ___________________________________________________
'
' (c) Hi-Integrity Systems 2010. All rights reserved.
' www.hisystems.com.au - Toby Wicks
' ___________________________________________________
'

Option Strict On
Option Explicit On

Namespace Constraints

''' <summary>
''' Ensures that the key field (KeyFieldName for parent collection class) value is unique within the collection.
''' The value passed to the ConstraintBinding must by the key field value.
''' The constraint ensure s that if the object is new then the key field value must be unique within the entire collection (the collection's SubSet applies).
''' Otherwise, if the new object is already saved and the key field value has changed then it is ensured to be unique within the collection (the collection's SubSet applies).
''' </summary>
''' <remarks></remarks>
''' <typeparam name="T">The key field data type (typically string)</typeparam>
Public Class KeyFieldIsUniqueInCollectionConstraint(Of T)
Implements IConstraint(Of T)

Private pobjDatabaseObject As DatabaseObject

''' <summary>
''' Indicates the object that will be ensured to be unique within the collection based on the key field value.
''' </summary>
''' <param name="objDatabaseObject">
''' The object that must be unique based on the key field value.
''' </param>
Public Sub New(ByVal objDatabaseObject As DatabaseObject)

If objDatabaseObject Is Nothing Then
Throw New ArgumentNullException
End If

pobjDatabaseObject = objDatabaseObject

End Sub

Private Function ValueSatisfiesConstraint(value As T) As Boolean Implements IConstraint(Of T).ValueSatisfiesConstraint

Dim objParentCollection As IDatabaseObjects = pobjDatabaseObject.ParentCollection

Dim objSelect As New SQL.SQLSelect
objSelect.Fields.Add(objParentCollection.DistinctFieldName)
objSelect.Tables.Add(objParentCollection.TableName)

Dim objSubset As SQL.SQLConditions = objParentCollection.Subset
If Not objSubset Is Nothing AndAlso Not objSubset.IsEmpty Then
objSelect.Where.Add(objSubset)
End If
objSelect.Where.Add(objParentCollection.KeyFieldName, SQL.ComparisonOperator.EqualTo, value)

'objFoundItem is nothing if the unique item is not found
Dim objExistingObjectDistinctValue As Object = pobjDatabaseObject.ParentDatabase.Connection.ExecuteScalarWithConnect(objSelect)

'If a value was not found in the database then the value is unique
If objExistingObjectDistinctValue Is Nothing Then
Return True
Else
'If a value was found in the database then it should only be for this object (i.e. this field was not changed)
If DirectCast(pobjDatabaseObject, IDatabaseObject).IsSaved Then
Return objExistingObjectDistinctValue.Equals(DirectCast(pobjDatabaseObject, IDatabaseObject).DistinctValue)
Else
'If value was found in the database but the object has not been saved then the item found cannot be for this object
'and therefore it is being used by another object
Return False
End If
End If

End Function

End Class

End Namespace
1 change: 1 addition & 0 deletions DatabaseObjects.vbproj
Expand Up @@ -114,6 +114,7 @@
<Compile Include="Attributes\SubsetAttribute.vb" />
<Compile Include="Attributes\TableAttribute.vb" />
<Compile Include="Attributes\TableJoinAttribute.vb" />
<Compile Include="Contraints\KeyFieldIsUniqueInCollectionConstraint.vb" />
<Compile Include="Contraints\Constraint.vb" />
<Compile Include="Contraints\RegExConstraint.vb" />
<Compile Include="Contraints\DateIsTodayOrFutureForNewObjectConstraint.vb" />
Expand Down

0 comments on commit 7ff02b6

Please sign in to comment.