Beispiel #1
0
 public TableJoinAttribute(string tableName, string rightFieldName, string leftFieldName)
 {
     this._tableName      = tableName;
     this._rightFieldName = rightFieldName;
     this._leftFieldName  = leftFieldName;
     this._joinType       = TableJoinType.InnerJoin;
 }
Beispiel #2
0
 internal JoinedTables(TableJoinType joinType, FromSource left, FromSource right, SqlBooleanExpression joinCondition)
 {
     JoinType      = joinType;
     Left          = left;
     Right         = right;
     JoinCondition = joinCondition;
 }
Beispiel #3
0
 /// <summary>
 /// Set table join
 /// </summary>
 /// <param name="joinType"></param>
 /// <param name="joinedTable"></param>
 /// <param name="joinExpression"></param>
 public void Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression)
 {
     //RI: special case - inner joins on top of outer joins should become outer joins as well
       if (joinedTable.JoinedTable != null && (joinedTable.JoinType & TableJoinType.FullOuter) != 0)
     joinType |= joinedTable.JoinType;
       JoinExpression = joinExpression;
       JoinType = joinType;
       JoinedTable = joinedTable;
 }
Beispiel #4
0
 /// <summary>
 /// Set table join
 /// </summary>
 /// <param name="joinType"></param>
 /// <param name="joinedTable"></param>
 /// <param name="joinExpression"></param>
 public void Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression)
 {
     //RI: special case - inner joins on top of outer joins should become outer joins as well
     if (joinedTable.JoinedTable != null && (joinedTable.JoinType & TableJoinType.FullOuter) != 0)
     {
         joinType |= joinedTable.JoinType;
     }
     JoinExpression = joinExpression;
     JoinType       = joinType;
     JoinedTable    = joinedTable;
 }
Beispiel #5
0
        public void Join(TableJoinType joinType, Expression joinedExpr, Expression joinExpression)
        {
            //RI: special case - inner joins on top of outer joins should become outer joins as well
            var joinedTable = joinedExpr as TableExpression;

            if (joinedTable == null)
            {
                Util.Check(false, "Joins with subqueries not supported, joined expr: {0}", joinedExpr);
            }
            if (joinedTable != null && joinedTable.JoinedTable != null && (joinedTable.JoinType & TableJoinType.FullOuter) != 0)
            {
                joinType |= joinedTable.JoinType;
            }
            JoinExpression = joinExpression;
            JoinType       = joinType;
            JoinedTable    = joinedTable;
        }
Beispiel #6
0
        /// <summary>
        /// �õ��������SQL���
        /// </summary>
        /// <param name="joinType"></param>
        /// <returns></returns>
        public static string TableJOINTypeToString(TableJoinType joinType)
        {
            switch (joinType)
            {
                case TableJoinType.InnerJoin:
                    return "INNER JOIN";

                case TableJoinType.LeftOuterJoin:
                    return "LEFT JOIN";

                case TableJoinType.RightOuterJoin:
                    return "RIGHT JOIN";

                case TableJoinType.FullOuterJoin:
                    return "FULL JOIN";
            }
            return "";
        }
        protected virtual SqlFragment BuildTableJoin(TableJoinType joinType, SqlFragment tableRef, SqlFragment joinCond)
        {
            SqlFragment joinDecl = null;

            switch (joinType)
            {
            case TableJoinType.Inner: joinDecl = SqlTerms.InnerJoin; break;

            case TableJoinType.LeftOuter: joinDecl = SqlTerms.LeftJoin; break;

            case TableJoinType.RightOuter: joinDecl = SqlTerms.RightJoin; break;

            default:
                Util.Check(false, "Join type {0} not supported.", joinType);
                break;
            }
            return(new CompositeSqlFragment(SqlTerms.NewLine, SqlTerms.Indent, joinDecl, tableRef, SqlTerms.On, joinCond));
        }
        public static string TranslateJoinType(TableJoinType joinType)
        {
            switch (joinType)
            {
            case TableJoinType.LeftJoin:
                return("LEFT JOIN");

            case TableJoinType.InnerJoin:
                return("INNER JOIN");

            case TableJoinType.RightJoin:
                return("RIGHT JOIN");

            case TableJoinType.FullJoin:
                return("FULL JOIN");

            default:
                throw new NotImplementedException($"Unsupported join type {Enum.GetName(typeof(TableJoinType), joinType)}");
            }
        }
Beispiel #9
0
        protected virtual string GetJoinOperator(TableJoinType joinType)
        {
            switch (joinType)
            {
            case TableJoinType.CrossJoin:
                return("CROSS JOIN");

            case TableJoinType.InnerJoin:
                return("INNER JOIN");

            case TableJoinType.LeftOuterJoin:
                return("LEFT OUTER JOIN");

            case TableJoinType.RightOuterJoin:
                return("RIGHT OUTER JOIN");

            case TableJoinType.FullOuterJoin:
                return("FULL OUTER JOIN");

            default:
                throw new InvalidOperationException();
            }
        }
 private Expression AnalyzeJoin(IList<Expression> parameters, TableJoinType joinType, TranslationContext context)
 {
     if (parameters.Count == 5)
     {
         var outerExpr = Analyze(parameters[0], context);
         var innerExpr = Analyze(parameters[1], context);
         var innerTable = innerExpr as TableExpression;
         // TODO: fix this. When joined table is a subquery, we have this exception
         if (innerTable == null)
            Util.Throw("Join with sub-query is not supported. Sub-query: {0}.", innerExpr);
         // RI: check if key selectors return Entity: if yes, change to PK
         var outerSel = CheckJoinKeySelector(parameters[2], context);
         var innerSel = CheckJoinKeySelector(parameters[3], context);
         var outerKeySelector = Analyze(outerSel, outerExpr, context);
         var innerKeySelector = Analyze(innerSel, innerTable, context);
         // from here, we have two options to join:
         // 1. left and right are tables, we can use generic expressions (most common)
         // 2. left is something else (a meta table)
         var outerTable = outerExpr as TableExpression;
         if (outerTable == null)
         {
             var outerKeyColumn = outerKeySelector as ColumnExpression;
             if (outerKeyColumn == null)
                 Util.Throw("S0701: No way to find left table for Join");
             outerTable = outerKeyColumn.Table;
         }
         var joinExpr = ExpressionUtil.MakeBinary(ExpressionType.Equal, outerKeySelector, innerKeySelector);
         innerTable.Join(joinType, outerTable, joinExpr,
                         string.Format("join{0}", context.EnumerateAllTables().Count()));
         // last part is lambda, with two tables as parameters
         var metaTableDefinitionBuilderContext = new TranslationContext(context);
         //metaTableDefinitionBuilderContext.ExpectMetaTableDefinition = true;
         var expression = Analyze(parameters[4], new[] { outerExpr, innerTable }, metaTableDefinitionBuilderContext);
         return expression;
     }
     Util.Throw("S0530: Don't know how to handle GroupJoin() with {0} parameters", parameters.Count);
     return null;
 }
        /// <summary>
        /// Returns association definition, if any
        /// </summary>
        /// <param name="thisTableExpression">The table referenced by the assocation (the type holding the member)</param>
        /// <param name="memberInfo">The memberInfo related to association</param>
        /// <param name="thisKey">The keys in the joined table</param>
        /// <param name="otherKey">The keys in the associated table</param>
        /// <param name="joinType"></param>
        /// <param name="joinID"></param>
        /// <param name="dataContext"></param>
        /// <returns></returns>
        public virtual Type GetAssociation(TableExpression thisTableExpression, MemberInfo memberInfo,
                                           out IList<MemberInfo> thisKey, out IList<MemberInfo> otherKey,
                                           out TableJoinType joinType, out string joinID, DataContext dataContext)
        {
            var thisTableDescription = dataContext.Mapping.GetTable(thisTableExpression.Type);
            var thisAssociation =
                (from association in thisTableDescription.RowType.Associations
                 where association.ThisMember.Member == memberInfo
                 select association).SingleOrDefault();
            if (thisAssociation != null)
            {
                joinType = TableJoinType.Inner;
                joinID = thisAssociation.ThisMember.MappedName;
                if (string.IsNullOrEmpty(joinID))
                    throw Error.BadArgument("S0108: Association name is required to ensure join uniqueness");

                var otherType = thisAssociation.ThisMember.Type;
                if (otherType.IsGenericType) // TODO: something serious here
                    otherType = otherType.GetGenericArguments()[0];

                var otherTableDescription = dataContext.Mapping.GetTable(otherType);
                thisKey = GetAssociationKeys(thisTableDescription, thisAssociation.ThisKey, dataContext);
                otherKey = GetAssociationKeys(otherTableDescription, thisAssociation.OtherKey, dataContext);

                return otherType;
            }
            thisKey = null;
            otherKey = null;
            joinType = TableJoinType.Default;
            joinID = null;
            return null;
        }
Beispiel #12
0
 public void Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression, string joinID)
 {
     Join(joinType, joinedTable, joinExpression);
     JoinID = joinID;
 }
 /// <summary>
 /// Set table join
 /// </summary>
 /// <param name="joinType"></param>
 /// <param name="joinedTable"></param>
 /// <param name="joinExpression"></param>
 public void Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression)
 {
     JoinExpression = joinExpression;
     JoinType = joinType;
     JoinedTable = joinedTable;
 }
 /// <summary>
 /// Set table join
 /// </summary>
 /// <param name="joinType"></param>
 /// <param name="joinedTable"></param>
 /// <param name="joinExpression"></param>
 /// <param name="joinID"></param>
 public void Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression, string joinID)
 {
     Join(joinType, joinedTable, joinExpression);
     JoinID = joinID;
 }
        private IList <EntityMemberInfo> GetAssociationMembers(TableExpression thisTableExpression, EntityMemberInfo member,
                                                               out IList <EntityMemberInfo> otherKey, out TableJoinType joinType, out string joinID)
        {
            switch (member.Kind)
            {
            case EntityMemberKind.EntityRef:
                // by default, join is inner
                joinType = TableJoinType.Inner;
                joinID   = member.MemberName;
                var otherType = member.ReferenceInfo.ToKey.Entity.ClassInfo.Type;
                otherKey = member.ReferenceInfo.ToKey.ExpandedKeyMembers.Select(km => km.Member).ToList();
                var thisKey = member.ReferenceInfo.FromKey.ExpandedKeyMembers.Select(km => km.Member).ToList();
                if (member.Flags.IsSet(EntityMemberFlags.Nullable))
                {
                    joinType |= TableJoinType.LeftOuter;
                }
                return(thisKey);

            case EntityMemberKind.Transient:
                if (!member.Flags.IsSet(EntityMemberFlags.FromOneToOneRef))
                {
                    break;
                }
                joinType = TableJoinType.LeftOuter;
                joinID   = member.MemberName;
                var targetEnt = _dbModel.EntityModel.GetEntityInfo(member.DataType, throwIfNotFound: true);
                otherKey = targetEnt.PrimaryKey.ExpandedKeyMembers.Select(km => km.Member).ToList();
                var thisPk = member.Entity.PrimaryKey.ExpandedKeyMembers.Select(km => km.Member).ToList();
                return(thisPk);
            }
            Util.Throw("Cannot create JOIN expression for property {0}, property not supported in LINQ.", member);
            //just to satisfy
            otherKey = null;
            joinType = TableJoinType.Default;
            joinID   = null;
            return(null);
        }
Beispiel #16
0
        /// <summary>
        /// Returns association definition, if any
        /// </summary>
        /// <param name="thisTableExpression">The table referenced by the assocation (the type holding the member)</param>
        /// <param name="memberInfo">The memberInfo related to association</param>
        /// <param name="otherType"></param>
        /// <param name="otherKey">The keys in the associated table</param>
        /// <param name="joinType"></param>
        /// <param name="joinID"></param>
        /// <param name="dataContext"></param>
        /// <returns>ThisKey</returns>
        public virtual IList<MemberInfo> GetAssociation(TableExpression thisTableExpression, MemberInfo memberInfo, Type otherType, out IList<MemberInfo> otherKey, out TableJoinType joinType, out string joinID, DataContext dataContext)
        {
            var thisTableDescription = dataContext.Mapping.GetTable(thisTableExpression.Type);
            var thisAssociation =
                (from association in thisTableDescription.RowType.Associations
                 where association.ThisMember.Member == memberInfo
                 select association).SingleOrDefault();
            if (thisAssociation != null)
            {
                // by default, join is inner
                joinType = TableJoinType.Inner;
                joinID = thisAssociation.ThisMember.MappedName;
                if (string.IsNullOrEmpty(joinID))
                    throw Error.BadArgument("S0108: Association name is required to ensure join uniqueness");

                var otherTableDescription = dataContext.Mapping.GetTable(otherType);
                bool thisKeyHasNullables, otherKeyHasNullables;
                var thisKey = GetAssociationKeys(thisTableDescription, thisAssociation.ThisKey, dataContext,
                                                 out thisKeyHasNullables);
                otherKey = GetAssociationKeys(otherTableDescription, thisAssociation.OtherKey, dataContext,
                                              out otherKeyHasNullables);

                // we just test here the left join (since associations are symmetric,
                //        we can only find left joins here, and the otherKeyHasNullables is
                //        always equal to thisKeyHasNullables)
                if (thisKeyHasNullables)
                    joinType |= TableJoinType.LeftOuter;

                return thisKey;
            }
            otherKey = null;
            joinType = TableJoinType.Default;
            joinID = null;
            return null;
        }
Beispiel #17
0
 /// <summary>
 /// Set table join
 /// </summary>
 /// <param name="joinType"></param>
 /// <param name="joinedTable"></param>
 /// <param name="joinExpression"></param>
 public void Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression)
 {
     JoinExpression = joinExpression;
     JoinType = joinType;
     JoinedTable = joinedTable;
 }
Beispiel #18
0
 private JoinWithoutOnExpression(TableJoinType joinType, TableSource left, TableSource right)
 {
     this.joinType = joinType;
     this.left     = left;
     this.right    = right;
 }
Beispiel #19
0
        /// <summary>
        /// Returns association definition, if any
        /// </summary>
        /// <param name="thisTableExpression">The table referenced by the assocation (the type holding the member)</param>
        /// <param name="memberInfo">The memberInfo related to association</param>
        /// <param name="otherType"></param>
        /// <param name="otherKey">The keys in the associated table</param>
        /// <param name="joinType"></param>
        /// <param name="joinID"></param>
        /// <param name="dataContext"></param>
        /// <returns>ThisKey</returns>
        public virtual IList <MemberInfo> GetAssociation(TableExpression thisTableExpression, MemberInfo memberInfo, Type otherType, out IList <MemberInfo> otherKey, out TableJoinType joinType, out string joinID, DataContext dataContext)
        {
            var thisTableDescription = dataContext.Mapping.GetTable(thisTableExpression.Type);
            var thisAssociation      =
                (from association in thisTableDescription.RowType.Associations
                 where association.ThisMember.Member == memberInfo
                 select association).SingleOrDefault();

            if (thisAssociation != null)
            {
                // by default, join is inner
                joinType = TableJoinType.Inner;
                joinID   = thisAssociation.ThisMember.MappedName;
                if (string.IsNullOrEmpty(joinID))
                {
                    throw Error.BadArgument("S0108: Association name is required to ensure join uniqueness");
                }

                var  otherTableDescription = dataContext.Mapping.GetTable(otherType);
                bool thisKeyHasNullables, otherKeyHasNullables;
                var  thisKey = GetAssociationKeys(thisTableDescription, thisAssociation.ThisKey, dataContext,
                                                  out thisKeyHasNullables);
                otherKey = GetAssociationKeys(otherTableDescription, thisAssociation.OtherKey, dataContext,
                                              out otherKeyHasNullables);

                // we just test here the left join (since associations are symmetric,
                //        we can only find left joins here, and the otherKeyHasNullables is
                //        always equal to thisKeyHasNullables)
                if (thisKeyHasNullables)
                {
                    joinType |= TableJoinType.LeftOuter;
                }

                return(thisKey);
            }
            otherKey = null;
            joinType = TableJoinType.Default;
            joinID   = null;
            return(null);
        }
Beispiel #20
0
 // Methods
 public RelationReflectMapping(string tableName, string sourceTableName)
 {
     this.tableName = tableName;
     this.tableAlias = tableName;
     this.sourceTableName = sourceTableName;
     this.sourceTableAlias = sourceTableName;
     this.joinType = TableJoinType.InnerJoin;
 }
 private IList<MemberInfo> GetAssociationMembers(TableExpression thisTableExpression, EntityMemberInfo member,
     out IList<MemberInfo> otherKey, out TableJoinType joinType, out string joinID)
 {
     switch(member.Kind) {
       case MemberKind.EntityRef:
     // by default, join is inner
     joinType = TableJoinType.Inner;
     joinID = member.MemberName;
     var otherType = member.ReferenceInfo.ToKey.Entity.ClassInfo.Type;
     otherKey = member.ReferenceInfo.ToKey.ExpandedKeyMembers.Select(km => (MemberInfo) km.Member.ClrClassMemberInfo).ToList();
     var thisKey = member.ReferenceInfo.FromKey.ExpandedKeyMembers.Select(km => (MemberInfo)km.Member.ClrClassMemberInfo).ToList();
     if(member.Flags.IsSet(EntityMemberFlags.Nullable))
       joinType |= TableJoinType.LeftOuter;
     return thisKey;
       case MemberKind.Transient:
     if(!member.Flags.IsSet(EntityMemberFlags.FromOneToOneRef))
       break;
     joinType = TableJoinType.LeftOuter;
     joinID = member.MemberName;
     var targetEnt = this._dbModel.EntityApp.Model.GetEntityInfo(member.DataType, throwIfNotFound: true);
     otherKey = targetEnt.PrimaryKey.ExpandedKeyMembers.Select(km => km.Member.ClrClassMemberInfo).ToList();
     var thisPk = member.Entity.PrimaryKey.ExpandedKeyMembers.Select(km => km.Member.ClrClassMemberInfo).ToList();
     return thisPk;
     }
     Util.Throw("Cannot create JOIN expression for property {0}, property not supported in LINQ.", member);
     otherKey = null;
     joinType = TableJoinType.Default;
     joinID = null;
     return null;
 }