Skip to content

Commit

Permalink
Supports early binding for all references which also require other ta…
Browse files Browse the repository at this point in the history
…ble joins. Closes #81.

Traverses "horizontally" across the class hierarchy to determine
whether a referenced item also requires another table join. For
example, an purchase order which refers to a supplier which refers to a
branch.

Refactored GetTableJoins into two functions for recursive calls when
traversing "horizontally" across the object heirarchy.

Also removed the first superfluous IDatabaseObjects argument to
GetTableJoins.
  • Loading branch information
hisystems committed Apr 15, 2013
1 parent 4b06fa5 commit 3267f6f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Database/DatabaseObjectsUsingAttributesHelper.cs
Expand Up @@ -190,7 +190,7 @@ public SQL.SQLSelectTableJoins TableJoins(SQL.SQLSelectTable objPrimaryTable, SQ
else
{
//If the ObjectReferenceEarlyBindingAttribute is specified on the item instance then create the table joins that will be required
var earlyBindingTableJoins = ObjectReferenceEarlyBinding.GetTableJoins(pobjDatabaseObjects, objPrimaryTable, this.GetItemInstanceType());
var earlyBindingTableJoins = ObjectReferenceEarlyBinding.GetTableJoins(objPrimaryTable, this.GetItemInstanceType());

if (earlyBindingTableJoins.Count > 0)
tableJoinsCollection = earlyBindingTableJoins;
Expand Down
33 changes: 25 additions & 8 deletions Database/ObjectReferenceEarlyBinding.cs
Expand Up @@ -131,12 +131,26 @@ private class CollectionType
/// </summary>
/// <returns>Returns an empty list if there are no early binding required for the item instance type, otherwise the table joins required.</returns>
private static CollectionTypesInAssemblies collectionTypesInAssemblies = new CollectionTypesInAssemblies();

internal static SQL.SQLSelectTableJoins GetTableJoins(IDatabaseObjects collection, SQL.SQLSelectTable collectionTable, Type itemInstanceType)

internal static SQL.SQLSelectTableJoins GetTableJoins(SQL.SQLSelectTable collectionTable, Type itemInstanceType)
{
var tableJoins = new SQL.SQLSelectTableJoins();

GetTableJoins(collectionTable, itemInstanceType, collectionTable, tableJoins);

return tableJoins;
}

/// <summary>
/// Creates all of the table joins by traversing the item instance type and finding ObjectReferenceEarlyBinding and FieldMapping attributed fields
/// and then finding the corresponding item instance type and the associated collection / table that it should be joined to.
/// This applies to all of the fields in the item instance type and also to any other table joins that the fields refer to.
/// Essentially, ensuring that all table joins are created for the collection.
/// </summary>
/// <param name="leftTable">The joined table which contains all of the calculated table joins</param>
/// <returns>The joined table which contains all of the calculated table joins</returns>
internal static SQL.SQLSelectTableBase GetTableJoins(SQL.SQLSelectTable collectionTable, Type itemInstanceType, SQL.SQLSelectTableBase leftTable, SQL.SQLSelectTableJoins tableJoins)
{
SQL.SQLSelectTableJoins tableJoins = new SQL.SQLSelectTableJoins();
SQL.SQLSelectTableBase leftTable = collectionTable;

foreach (var fieldInfo in GetObjectReferenceEarlyBindingFieldsAndForBaseTypes(itemInstanceType))
{
var fieldTypeAssembly = fieldInfo.ObjectReferenceFieldType.Assembly; // assume that the collection type is defined in the same assembly as the item instance type
Expand All @@ -150,11 +164,14 @@ internal static SQL.SQLSelectTableJoins GetTableJoins(IDatabaseObjects collectio
var tableJoin = tableJoins.Add(leftTable, SQL.SQLSelectTableJoin.Type.Inner, rightTable);
tableJoin.Where.Add(new SQL.SQLFieldExpression(collectionTable, fieldInfo.FieldMappingName), SQL.ComparisonOperator.EqualTo, new SQL.SQLFieldExpression(new SQL.SQLSelectTable(fieldAssociatedCollectionInfo.TableName), fieldAssociatedCollectionInfo.DistinctFieldName));
leftTable = tableJoin;

// Also traverse "horizontally" / to a greater depth along the reference chain to ensure that referenced objects which also reference other objects are added to the table joins
leftTable = GetTableJoins(new SQL.SQLSelectTable(fieldAssociatedCollectionInfo.TableName), fieldAssociatedCollectionInfo.ItemInstanceType, leftTable, tableJoins);
}
return tableJoins;

return leftTable;
}

/// <summary>
/// Traverses the item instance type and any base classes for reference to fields marked with the
/// ObjectReferenceEarlyBindingAttribute.
Expand Down

0 comments on commit 3267f6f

Please sign in to comment.