private void AddParentToChildJoins(DbJoin arg)
        {
            TableKey childTable = arg.Destination;
            TableKey parent     = null;
            Dictionary <int, string> localGeneratedJoins = new Dictionary <int, string> ( );


            while ((parent = childTable.Parent) != null && (arg.Source != childTable))
            {
                DbJoin join = new DbJoin(parent, childTable);

                if (!_generatedJoins.ContainsKey(join))
                {
                    var joins = _cachedJoins.GetOrAdd(join, GenerateAndAddJoinWithChild);
                    localGeneratedJoins.Add(join, joins.First( ));
                }

                childTable = parent;
            }

            if (arg.Source == childTable)
            {
                Merge(_generatedJoins, localGeneratedJoins);
            }
        }
 private void CreateJoin(DbJoin arg)
 {
     if (!AddChildToParentJoins(arg))
     {
         AddParentToChildJoins(arg);
     }
 }
        public void AddJoins(TableKey source, TableKey destination)
        {
            if (source == destination)
            {
                return;
            }

            DbJoin join = new DbJoin(source, destination);

            CreateJoin(join);
        }
        private bool AddChildToParentJoins(DbJoin arg)
        {
            TableKey childTable = arg.Source;
            TableKey parent     = null;

            while ((parent = childTable.Parent) != null && (arg.Destination != childTable))
            {
                DbJoin join = new DbJoin(childTable, parent);

                if (!_generatedJoins.ContainsKey(join))
                {
                    var joins = _cachedJoins.GetOrAdd(join, GenerateAndAddNewJoin);
                    _generatedJoins.Add(join, joins.First( ));
                }

                childTable = parent;
            }

            return(childTable == arg.Destination);
        }
        private List <string> GenerateAndAddJoinWithChild(DbJoin arg)
        {
            string joinText = GetJoinWithChild(arg.Destination);

            return(new List <string> (new string[] { joinText }));
        }
        private List <string> GenerateAndAddNewJoin(DbJoin arg)
        {
            string joinText = GetJoinWithParent(arg.Source);

            return(new List <string> (new string[] { joinText }));
        }