コード例 #1
0
ファイル: SqlQuery_Join.cs プロジェクト: xcao104621/Serenity
        private void JoinToString(Join join, StringBuilder sb, bool modifySelf)
        {
            sb.Append(join.GetKeyword());
            sb.Append(' ');
            sb.Append(SqlSyntax.AutoBracketValid(join.Table));

            // joinAlias belirtilmişse ekle
            if (!string.IsNullOrEmpty(join.Name))
            {
                sb.Append(' ');
                sb.Append(join.Name);
            }

            if (!ReferenceEquals(null, join.OnCriteria) &&
                !join.OnCriteria.IsEmpty)
            {
                sb.Append(" ON ");
                if (!(join.OnCriteria is BinaryCriteria))
                {
                    sb.Append('(');
                }

                if (modifySelf)
                {
                    sb.Append(join.OnCriteria.ToString(this));
                }
                else
                {
                    sb.Append(join.OnCriteria.ToStringIgnoreParams());
                }

                if (!(join.OnCriteria is BinaryCriteria))
                {
                    sb.Append(')');
                }
            }
        }
コード例 #2
0
        internal string PrefixAliases(string expression, string alias,
                                      DialectExpressionSelector expressionSelector, List <Attribute> extraJoins)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                return(expression);
            }

            Check.NotNullOrWhiteSpace(alias, "alias");

            var aliasPrefix = alias + "_";

            var mappedJoins = new Dictionary <string, ISqlJoin>();

            Func <string, string> mapAlias = null;

            Func <string, string> mapExpression = x =>
            {
                if (x == null)
                {
                    return(null);
                }

                return(JoinAliasLocator.ReplaceAliases(x, mapAlias));
            };

            mapAlias = x =>
            {
                if (x == "t0" || x == "T0")
                {
                    return(alias);
                }

                ISqlJoin sqlJoin;
                if (mappedJoins.TryGetValue(x, out sqlJoin))
                {
                    return(sqlJoin.Alias);
                }

                Tuple <string, ForeignKeyAttribute, ISqlJoin> propJoin;
                if (joinPropertyByAlias.TryGetValue(x, out propJoin))
                {
                    var    propertyInfo = propertyByName[propJoin.Item1];
                    string leftExpression;
                    var    newAlias   = aliasPrefix + x;
                    var    columnAttr = propertyInfo.GetCustomAttribute <ColumnAttribute>();
                    if (columnAttr != null)
                    {
                        leftExpression = alias + "." + SqlSyntax.AutoBracket(columnAttr.Name);
                    }
                    else
                    {
                        var expressionAttr = propertyInfo.GetCustomAttribute <ExpressionAttribute>();
                        if (expressionAttr != null)
                        {
                            leftExpression = mapExpression(expressionAttr.Value);
                        }
                        else
                        {
                            var origin = propertyInfo.GetCustomAttribute <OriginAttribute>();
                            if (origin != null)
                            {
                                leftExpression = OriginExpression(propertyInfo.Name, origin, expressionSelector, alias, extraJoins);
                            }
                            else
                            {
                                leftExpression = alias + "." + SqlSyntax.AutoBracket(propertyInfo.Name);
                            }
                        }
                    }

                    ISqlJoin srcJoin   = propJoin.Item3;
                    var      criteriax = leftExpression + " = " + newAlias + "." + SqlSyntax.AutoBracket(propJoin.Item2.Field);

                    if (srcJoin is LeftJoinAttribute)
                    {
                        srcJoin = new LeftJoinAttribute(newAlias, propJoin.Item2.Table, criteriax);
                    }
                    else if (srcJoin is InnerJoinAttribute)
                    {
                        srcJoin = new InnerJoinAttribute(newAlias, propJoin.Item2.Table, criteriax);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException("joinType");
                    }

                    srcJoin.RowType = propJoin.Item2.RowType ?? propJoin.Item3.RowType;
                    mappedJoins[x]  = srcJoin;
                    extraJoins.Add((Attribute)srcJoin);
                    return(newAlias);
                }

                if (rowJoinByAlias.TryGetValue(x, out sqlJoin))
                {
                    var mappedCriteria = mapExpression(sqlJoin.OnCriteria);
                    var newAlias       = aliasPrefix + x;
                    var rowType        = sqlJoin.RowType;

                    var lja = sqlJoin as LeftJoinAttribute;
                    if (lja != null)
                    {
                        sqlJoin = new LeftJoinAttribute(lja.Alias, lja.ToTable, mappedCriteria);
                    }
                    else
                    {
                        var ija = sqlJoin as InnerJoinAttribute;
                        if (ija != null)
                        {
                            sqlJoin = new InnerJoinAttribute(ija.Alias, ija.ToTable, mappedCriteria);
                        }
                        else
                        {
                            var oaa = sqlJoin as OuterApplyAttribute;
                            if (oaa != null)
                            {
                                sqlJoin = new OuterApplyAttribute(ija.Alias, mappedCriteria);
                            }
                        }
                    }

                    sqlJoin.RowType = rowType;
                    mappedJoins[x]  = sqlJoin;
                    extraJoins.Add((Attribute)sqlJoin);
                    return(newAlias);
                }

                return(x);
            };

            return(mapExpression(expression));
        }