コード例 #1
0
        /// <summary>
        /// Returns an existing table or registers the current one
        /// </summary>
        /// <param name="tableExpression"></param>
        /// <param name="context"></param>
        /// <returns>A registered table or the current newly registered one</returns>
        public virtual TableExpression RegisterTable(TableExpression tableExpression, TranslationContext context)
        {
            // 1. Find the table in current scope
            var foundTableExpression = (from t in context.EnumerateScopeTables()
                                        where t.IsEqualTo(tableExpression)
                                        select t).FirstOrDefault();

            if (foundTableExpression != null)
            {
                return(foundTableExpression);
            }
            // 2. Find it in all scopes, and promote it to current scope.
            foundTableExpression = PromoteTable(tableExpression, context);
            if (foundTableExpression != null)
            {
                return(foundTableExpression);
            }
            // 3. Add it
            context.CurrentSelect.Tables.Add(tableExpression);
            return(tableExpression);
        }
コード例 #2
0
 /// <summary>
 /// Returns an existing table or registers the current one
 /// </summary>
 /// <param name="tableExpression"></param>
 /// <param name="context"></param>
 /// <returns>A registered table or the current newly registered one</returns>
 public virtual TableExpression RegisterTable(TableExpression tableExpression, TranslationContext context)
 {
     // 1. Find the table in current scope
     var foundTableExpression = (from t in context.EnumerateScopeTables()
                             where t.IsEqualTo(tableExpression)
                             select t).FirstOrDefault();
     if (foundTableExpression != null)
     return foundTableExpression;
     // 2. Find it in all scopes, and promote it to current scope.
     foundTableExpression = PromoteTable(tableExpression, context);
     if (foundTableExpression != null)
     return foundTableExpression;
     // 3. Add it
     context.CurrentSelect.Tables.Add(tableExpression);
     return tableExpression;
 }
コード例 #3
0
        public virtual TableExpression RegisterAssociation(TableExpression tableExpression, EntityMemberInfo refMember, TranslationContext context)
        {
            IList<MemberInfo> otherKeys;
            TableJoinType joinType;
            string joinID;
            var theseKeys = GetAssociationMembers(tableExpression, refMember, out otherKeys, out joinType, out joinID);
            // if the memberInfo has no corresponding association, we get a null, that we propagate
            if (theseKeys == null)
            return null;

            // the current table has the foreign key, the other table the referenced (usually primary) key
            if (theseKeys.Count != otherKeys.Count)
            Util.Throw("S0128: Association arguments (FK and ref'd PK) don't match");

            // we first create the table, with the JoinID, and we MUST complete the table later, with the Join() method
            var otherType = refMember.DataType;
            var otherTableInfo = context.DbModel.GetTable(otherType);
            var otherTableExpression = CreateTable(otherType, context); // new TableExpression(otherType, otherTableInfo.FullName, otherTableInfo, joinID);
            otherTableExpression = RegisterTable(otherTableExpression, context);
            Expression joinExpression = null;

            var createdColumns = new List<ColumnExpression>();
            for (int keyIndex = 0; keyIndex < theseKeys.Count; keyIndex++)
            {
            // joinedKey is registered, even if unused by final select (required columns will be filtered anyway)
            Expression otherKey = RegisterColumn(otherTableExpression, otherKeys[keyIndex], context);
            // foreign is created, we will store it later if this assocation is registered too
            Expression thisKey = CreateColumn(tableExpression, theseKeys[keyIndex].Name, context);
            createdColumns.Add((ColumnExpression)thisKey);

            // the other key is set as left operand, this must be this way
            // since some vendors (SQL Server) don't support the opposite
            var referenceExpression = MakeEqual(otherKey, thisKey);

            // if we already have a join expression, then we have a double condition here, so "AND" it
            if (joinExpression != null)
                joinExpression = Expression.And(joinExpression, referenceExpression);
            else
                joinExpression = referenceExpression;
            }
            // we complete the table here, now that we have all join information
            otherTableExpression.Join(joinType, tableExpression, joinExpression);

            // our table is created, with the expressions
            // now check if we didn't register exactly the same
            var existingTable = (from t in context.EnumerateScopeTables() where t.IsEqualTo(otherTableExpression) select t).SingleOrDefault();
            if (existingTable != null)
            return existingTable;

            context.CurrentSelect.Tables.Add(otherTableExpression);
            foreach (var createdColumn in createdColumns)
            context.CurrentSelect.Columns.Add(createdColumn);
            return otherTableExpression;
        }
コード例 #4
0
        public virtual TableExpression RegisterAssociation(TableExpression tableExpression, EntityMemberInfo refMember, TranslationContext context)
        {
            IList <MemberInfo> otherKeys;
            TableJoinType      joinType;
            string             joinID;
            var theseKeys = GetAssociationMembers(tableExpression, refMember, out otherKeys, out joinType, out joinID);

            // if the memberInfo has no corresponding association, we get a null, that we propagate
            if (theseKeys == null)
            {
                return(null);
            }

            // the current table has the foreign key, the other table the referenced (usually primary) key
            if (theseKeys.Count != otherKeys.Count)
            {
                Util.Throw("S0128: Association arguments (FK and ref'd PK) don't match");
            }

            // we first create the table, with the JoinID, and we MUST complete the table later, with the Join() method
            var otherType            = refMember.DataType;
            var otherTableInfo       = context.DbModel.GetTable(otherType);
            var otherTableExpression = CreateTable(otherType, context); // new TableExpression(otherType, otherTableInfo.FullName, otherTableInfo, joinID);

            otherTableExpression = RegisterTable(otherTableExpression, context);
            Expression joinExpression = null;

            var createdColumns = new List <ColumnExpression>();

            for (int keyIndex = 0; keyIndex < theseKeys.Count; keyIndex++)
            {
                // joinedKey is registered, even if unused by final select (required columns will be filtered anyway)
                Expression otherKey = RegisterColumn(otherTableExpression, otherKeys[keyIndex], context);
                // foreign is created, we will store it later if this assocation is registered too
                Expression thisKey = CreateColumn(tableExpression, theseKeys[keyIndex].Name, context);
                createdColumns.Add((ColumnExpression)thisKey);

                // the other key is set as left operand, this must be this way
                // since some vendors (SQL Server) don't support the opposite
                var referenceExpression = MakeEqual(otherKey, thisKey);

                // if we already have a join expression, then we have a double condition here, so "AND" it
                if (joinExpression != null)
                {
                    joinExpression = Expression.And(joinExpression, referenceExpression);
                }
                else
                {
                    joinExpression = referenceExpression;
                }
            }
            // we complete the table here, now that we have all join information
            otherTableExpression.Join(joinType, tableExpression, joinExpression);

            // our table is created, with the expressions
            // now check if we didn't register exactly the same
            var existingTable = (from t in context.EnumerateScopeTables() where t.IsEqualTo(otherTableExpression) select t).SingleOrDefault();

            if (existingTable != null)
            {
                return(existingTable);
            }

            context.CurrentSelect.Tables.Add(otherTableExpression);
            foreach (var createdColumn in createdColumns)
            {
                context.CurrentSelect.Columns.Add(createdColumn);
            }
            return(otherTableExpression);
        }