예제 #1
0
 private SqlLink BuildLink(SqlExpression item, MetaDataMember member, Expression source)
 {
     if (member.IsAssociation)
     {
         SqlExpression[] exprs = new SqlExpression[member.Association.ThisKey.Count];
         for (int i = 0, n = exprs.Length; i < n; i++)
         {
             MetaDataMember mm = member.Association.ThisKey[i];
             exprs[i] = sql.Member(item, mm.Member);
         }
         MetaType otherType = member.Association.OtherType;
         return(new SqlLink(new object(), otherType, member.Type, typeProvider.From(member.Type), item, member, exprs, null, source));
     }
     else
     {
         // if not association link is always based on primary key
         MetaType thisType = member.DeclaringType;
         System.Diagnostics.Debug.Assert(thisType.IsEntity);
         List <SqlExpression> exprs = new List <SqlExpression>();
         foreach (MetaDataMember mm in thisType.IdentityMembers)
         {
             exprs.Add(sql.Member(item, mm.Member));
         }
         SqlExpression expansion = sql.Member(item, member.Member);
         return(new SqlLink(new object(), thisType, member.Type, typeProvider.From(member.Type), item, member, exprs, expansion, source));
     }
 }
예제 #2
0
        private SqlLink BuildLink(SqlExpression item, MetaDataMember member, Expression source)
        {
            if (member.IsAssociation)
            {
                var array = new SqlExpression[member.Association.ThisKey.Count];
                var i     = 0;
                for (var num = array.Length; i < num; i++)
                {
                    var metaDataMember = member.Association.ThisKey[i];
                    array[i] = sql.Member(item, metaDataMember.Member);
                }
                var otherType = member.Association.OtherType;
                return(new SqlLink(new object(), otherType, member.Type, typeProvider.From(member.Type), item, member, array, null, source));
            }
            var declaringType = member.DeclaringType;
            var list          = new List <SqlExpression>();

            foreach (var identityMember in declaringType.IdentityMembers)
            {
                list.Add(sql.Member(item, identityMember.Member));
            }
            SqlExpression expansion = sql.Member(item, member.Member);

            return(new SqlLink(new object(), declaringType, member.Type, typeProvider.From(member.Type), item, member, list, expansion, source));
        }
예제 #3
0
            internal override SqlAlias VisitAlias(SqlAlias a)
            {
                SqlTable tab = a.Node as SqlTable;
                SqlTableValuedFunctionCall tvf = a.Node as SqlTableValuedFunctionCall;

                if (this.addPrimaryKeys && (tab != null || tvf != null))
                {
                    List <SqlOrderExpression> list = new List <SqlOrderExpression>();

                    bool     isTable = tab != null;
                    MetaType rowType = isTable ? tab.RowType : tvf.RowType;
                    foreach (MetaDataMember mm in rowType.IdentityMembers)
                    {
                        string           name = mm.MappedName;
                        SqlColumn        col;
                        Expression       sourceExpression;
                        List <SqlColumn> columns;

                        if (isTable)
                        {
                            col = tab.Find(name);
                            sourceExpression = tab.SourceExpression;
                            columns          = tab.Columns;
                        }
                        else
                        {
                            col = tvf.Find(name);
                            sourceExpression = tvf.SourceExpression;
                            columns          = tvf.Columns;
                        }

                        if (col == null)
                        {
                            col       = new SqlColumn(mm.MemberAccessor.Type, typeProvider.From(mm.MemberAccessor.Type), name, mm, null, sourceExpression);
                            col.Alias = a;
                            columns.Add(col);
                        }
                        list.Add(new SqlOrderExpression(SqlOrderType.Ascending, new SqlColumnRef(col)));
                    }

                    this.PrependOrderExpressions(list);

                    return(a);
                }
                else
                {
                    return(base.VisitAlias(a));
                }
            }
        internal SqlExpression ConvertTo(Type clrType, SqlExpression expr)
        {
            //
            // In SQL Server 2008, the new TIME data type cannot be converted to BIGINT, or FLOAT,
            // or a bunch of other SQL types.
            //
            if (clrType.IsGenericType && clrType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                clrType = clrType.GetGenericArguments()[0];
            }

            bool isClrTimeSpanType = clrType == typeof(TimeSpan);

            if (IsSqlTimeType(expr))
            {
                if (isClrTimeSpanType)
                {
                    // no conversion necessary
                    return(expr);
                }
                else
                {
                    expr = ConvertToDateTime(expr);
                }
            }

            return(UnaryConvert(clrType, typeProvider.From(clrType), expr, expr.SourceExpression));
        }