Skip to content

Commit

Permalink
Added support for multiple TableJoinAttribute definitions. Closes #44.
Browse files Browse the repository at this point in the history
  • Loading branch information
hisystems committed Apr 1, 2012
1 parent 959fe37 commit 4600a4c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Attributes/TableJoinAttribute.vb
Expand Up @@ -28,7 +28,7 @@ Option Explicit On
''' </code>
''' </example>
''' --------------------------------------------------------------------------------
<AttributeUsage(AttributeTargets.Class, AllowMultiple:=False, Inherited:=True)> _
<AttributeUsage(AttributeTargets.Class, AllowMultiple:=True, Inherited:=True)> _
Public Class TableJoinAttribute
Inherits Attribute

Expand Down
21 changes: 14 additions & 7 deletions Database/DatabaseObjectsUsingAttributesHelper.vb
Expand Up @@ -8,6 +8,7 @@

Option Strict On
Option Explicit On
Option Infer On

Imports System.Linq
Imports System.Reflection
Expand All @@ -30,7 +31,7 @@ Friend Class DatabaseObjectsUsingAttributesHelper
Private pobjKeyField As KeyFieldAttribute = Nothing
Private pobjOrderByAttributes As New System.Collections.Generic.List(Of OrderByFieldAttribute)
Private pobjSubset As SubsetAttribute = Nothing
Private pobjTableJoin As TableJoinAttribute = Nothing
Private pobjTableJoins As New System.Collections.Generic.List(Of TableJoinAttribute)
Private pobjItemInstance As ItemInstanceAttribute = Nothing

Public Sub New(ByVal objDatabaseObjects As DatabaseObjects)
Expand All @@ -56,7 +57,7 @@ Friend Class DatabaseObjectsUsingAttributesHelper
ElseIf TypeOf objAttribute Is TableAttribute Then
pobjTable = DirectCast(objAttribute, TableAttribute)
ElseIf TypeOf objAttribute Is TableJoinAttribute Then
pobjTableJoin = DirectCast(objAttribute, TableJoinAttribute)
pobjTableJoins.Add(DirectCast(objAttribute, TableJoinAttribute))
ElseIf TypeOf objAttribute Is ItemInstanceAttribute Then
pobjItemInstance = DirectCast(objAttribute, ItemInstanceAttribute)
End If
Expand Down Expand Up @@ -185,14 +186,20 @@ Friend Class DatabaseObjectsUsingAttributesHelper
Public Function TableJoins(ByVal objPrimaryTable As SQL.SQLSelectTable, ByVal objTables As SQL.SQLSelectTables) As SQL.SQLSelectTableJoins

'If attribute was not specified
If pobjTableJoin Is Nothing Then
If pobjTableJoins.Count = 0 Then
Return Nothing
Else
Dim objTableJoins As SQL.SQLSelectTableJoins = New SQL.SQLSelectTableJoins

With objTableJoins.Add(objPrimaryTable, SQL.SQLSelectTableJoin.Type.Inner, objTables.Add(pobjTableJoin.ToTableName))
.Where.Add(pobjTableJoin.FieldName, SQL.ComparisonOperator.EqualTo, pobjTableJoin.ToFieldName)
End With
Dim leftTable As SQL.SQLSelectTableBase = objPrimaryTable
Dim leftTableName As String = objPrimaryTable.Name

For Each tableJoinAttribute In pobjTableJoins
Dim rightTable As SQL.SQLSelectTableBase = objTables.Add(tableJoinAttribute.ToTableName)
Dim tableJoin = objTableJoins.Add(leftTable, SQL.SQLSelectTableJoin.Type.Inner, rightTable)
tableJoin.Where.Add(New SQL.SQLFieldExpression(New SQL.SQLSelectTable(leftTableName), tableJoinAttribute.FieldName), SQL.ComparisonOperator.EqualTo, New SQL.SQLFieldExpression(New SQL.SQLSelectTable(tableJoinAttribute.ToTableName), tableJoinAttribute.ToFieldName))
leftTable = tableJoin
leftTableName = tableJoinAttribute.ToTableName
Next

Return objTableJoins
End If
Expand Down

0 comments on commit 4600a4c

Please sign in to comment.