Skip to content

Commit

Permalink
Added support for conditions precedence in table join conditions. Closes
Browse files Browse the repository at this point in the history
 #18.

SQLSelectTableJoinConditions objects can now be created and a collection of conditions can be added to an SQLSelectTableJoinConditions object to group a set of conditions and to ensure the sub-conditions are wrapped in parentheses and executed logically first as per the mathematical order of operations.

Removed the reference to the Friend SQLSelectTableJoinConditions.Parent property because this is not utilised by the library.

SQLSelectTableJoinConditions.Add(String, ComparisonOperator, String) is only accessible if used when the parent SQLSelectTableJoin is used and it remains only for backward compatibility reasons. Created issue #20 to indicate that this method should be marked as obsolete.
  • Loading branch information
hisystems committed Mar 4, 2012
1 parent b32b0ee commit 6c6eac6
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions SQL/Select/SQLSelectTableJoinConditions.vb
Expand Up @@ -12,38 +12,44 @@ Option Explicit On
Namespace SQL

Public Class SQLSelectTableJoinConditions
Implements Collections.Generic.IEnumerable(Of SQLSelectTableJoinCondition)

Private pobjLogicalOperators As New Collections.Generic.List(Of LogicalOperator)
Private pobjConditions As New Collections.Generic.List(Of SQLSelectTableJoinCondition)
Private pobjConditions As New ArrayList
Private pobjParent As SQLSelectTableJoin

Public Sub New()

End Sub

Friend Sub New(ByVal objParent As SQLSelectTableJoin)

MyBase.New()
pobjParent = objParent

End Sub

Friend ReadOnly Property Parent() As SQLSelectTableJoin
Get

Return pobjParent

End Get
End Property

Public Function Add() As SQLSelectTableJoinCondition

Return Add("", ComparisonOperator.EqualTo, "")

End Function

Public Sub Add(ByVal conditions As SQLSelectTableJoinConditions)

AddLogicalOperatorIfRequired()
pobjConditions.Add(conditions)

End Sub

Public Function Add( _
ByVal strLeftTableFieldName As String, _
ByVal eCompare As ComparisonOperator, _
ByVal strRightTableFieldName As String) As SQLSelectTableJoinCondition

If pobjParent Is Nothing Then
Throw New InvalidOperationException("Use overloaded Add(SQLExpression, ComparisonOperator, SQLExpression) instead")
End If

'The Add function is here basically for backward compatibility when the conditions could only accept field names and the left and right tables from the parent join were used as table aliases.
'Now that that SQLSelectTableBase is being used (which can represent a table or joined tables) we need to check that parent left and right tables are only SQLSelectTable objects.
If Not TypeOf pobjParent.LeftTable Is SQLSelectTable Then
Expand Down Expand Up @@ -110,14 +116,6 @@ Namespace SQL

End Sub

Default Public ReadOnly Property Item(ByVal intIndex As Integer) As SQLSelectTableJoinCondition
Get

Return pobjConditions.Item(intIndex)

End Get
End Property

Public ReadOnly Property IsEmpty As Boolean
Get

Expand All @@ -134,6 +132,17 @@ Namespace SQL
End Get
End Property

Public Sub Delete(ByRef objConditions As SQLSelectTableJoinConditions)

If Not pobjConditions.Contains(objConditions) Then
Throw New IndexOutOfRangeException
End If

pobjConditions.Remove(objConditions)
objConditions = Nothing

End Sub

Public Sub Delete(ByRef objOrderByField As SQLSelectTableJoinCondition)

If Not pobjConditions.Contains(objOrderByField) Then
Expand All @@ -149,36 +158,29 @@ Namespace SQL
Get

Dim strSQL As String = String.Empty
Dim objCondition As SQLSelectTableJoinCondition
Dim intIndex As Integer

With pobjConditions
For intIndex As Integer = 0 To .Count() - 1
If intIndex > 0 Then
strSQL &= " " & SQLConvertLogicalOperator(DirectCast(pobjLogicalOperators.Item(intIndex - 1), LogicalOperator)) & " "
End If
For Each objCondition As Object In pobjConditions
If intIndex > 0 Then
strSQL &= " " & SQLConvertLogicalOperator(CType(pobjLogicalOperators.Item(intIndex - 1), LogicalOperator)) & " "
End If

objCondition = DirectCast(.Item(intIndex), SQLSelectTableJoinCondition)
strSQL &= objCondition.SQL(eConnectionType)
Next
End With
If TypeOf objCondition Is SQLSelectTableJoinConditions Then
strSQL &= "(" & DirectCast(objCondition, SQLSelectTableJoinConditions).SQL(eConnectionType) & ")"
ElseIf TypeOf objCondition Is SQLSelectTableJoinCondition Then
strSQL &= DirectCast(objCondition, SQLSelectTableJoinCondition).SQL(eConnectionType)
Else
Throw New NotImplementedException(objCondition.GetType.FullName)
End If

intIndex += 1
Next

Return strSQL

End Get
End Property

Private Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator

Return pobjConditions.GetEnumerator

End Function

Private Function GetEnumerator1() As System.Collections.Generic.IEnumerator(Of SQLSelectTableJoinCondition) Implements System.Collections.Generic.IEnumerable(Of SQLSelectTableJoinCondition).GetEnumerator

Return pobjConditions.GetEnumerator

End Function

End Class

End Namespace

0 comments on commit 6c6eac6

Please sign in to comment.