Exemple #1
0
        public SqlQuery EnsureJoinsInExpression(string expression)
        {
            if (string.IsNullOrEmpty(expression))
            {
                return(this);
            }

            string referencedJoin;
            var    referencedJoins = JoinAliasLocator.LocateOptimized(expression, out referencedJoin);

            if (referencedJoin != null)
            {
                EnsureJoin(referencedJoin);
            }

            if (referencedJoins != null)
            {
                foreach (var alias in referencedJoins)
                {
                    EnsureJoin(alias);
                }
            }

            return(this);
        }
Exemple #2
0
        public static TFields Get <TFields>(string alias)
            where TFields : RowFieldsBase, new()
        {
            Check.NotNullOrEmpty(alias, "alias");

            var key = new Tuple <Type, string>(typeof(TFields), alias);

            RowFieldsBase cached;

            if (cache.TryGetValue(key, out cached))
            {
                return((TFields)cached);
            }

            var result = new TFields();

            result.Initialize();
            const string error = "$$!!OnlyTableFieldsCanBeUsedAliased!!$$";

            foreach (var field in result)
            {
                if ((field.Flags & FieldFlags.Foreign) == FieldFlags.Foreign |
                    (field.Flags & FieldFlags.ClientSide) == FieldFlags.ClientSide |
                    field.expression == null)
                {
                    field.expression = error;
                }
                else
                {
                    field.expression = JoinAliasLocator.ReplaceAliases(field.expression,
                                                                       x =>
                    {
                        if (x == "t0" || x == "T0")
                        {
                            return(alias);
                        }

                        return(error);
                    });
                }

                field.referencedAliases = null;
                field.join      = null;
                field.joinAlias = null;
                field.origin    = null;
            }
            result.alias    = alias;
            result.aliasDot = alias + ".";

            cache[key] = result;
            return(result);
        }
Exemple #3
0
        protected Join(IDictionary <string, Join> joins, string toTable, string alias, ICriteria onCriteria)
            : base(toTable, alias)
        {
            this.joins      = joins;
            this.onCriteria = onCriteria;

            if (!ReferenceEquals(this.onCriteria, null))
            {
                var aliases = JoinAliasLocator.Locate(this.onCriteria.ToStringIgnoreParams());
                if (aliases != null && aliases.Count > 0)
                {
                    referencedAliases = aliases;
                }
            }

            var toTableAliases = JoinAliasLocator.Locate(this.Table);

            if (toTableAliases != null && toTableAliases.Count > 0)
            {
                if (referencedAliases == null)
                {
                    referencedAliases = toTableAliases;
                }
                else
                {
                    foreach (var x in toTableAliases)
                    {
                        referencedAliases.Add(x);
                    }
                }
            }

            if (joins != null)
            {
                if (joins.ContainsKey(this.Name))
                {
                    throw new ArgumentException(String.Format(
                                                    "There is already a join with alias '{0}'", this.Name));
                }

                joins.Add(this.Name, this);
            }
        }
        static void EnsureJoinsInExpression(SqlQuery query, string expression)
        {
            if (string.IsNullOrEmpty(expression))
            {
                return;
            }

            var ext = (ISqlQueryExtensible)query;

            var intoRow = ext.FirstIntoRow as IEntityWithJoins;

            if (intoRow == null)
            {
                return;
            }

            string referencedJoin;
            Join   join;
            var    referencedJoins = JoinAliasLocator.LocateOptimized(expression, out referencedJoin);

            if (referencedJoin != null)
            {
                if (intoRow.Joins.TryGetValue(referencedJoin, out join))
                {
                    EnsureJoin(query, join);
                }
            }

            if (referencedJoins != null)
            {
                foreach (var alias in referencedJoins)
                {
                    if (intoRow.Joins.TryGetValue(alias, out join))
                    {
                        EnsureJoin(query, join);
                    }
                }
            }
        }
Exemple #5
0
        partial void EnsureJoinsInExpression(string expression)
        {
            if (expression.IsNullOrEmpty())
            {
                return;
            }

            var intoRow = this.FirstIntoRow as IEntityWithJoins;

            if (intoRow == null)
            {
                return;
            }

            string referencedJoin;
            Join   join;
            var    referencedJoins = JoinAliasLocator.LocateOptimized(expression, out referencedJoin);

            if (referencedJoin != null)
            {
                if (intoRow.Joins.TryGetValue(referencedJoin, out join))
                {
                    EnsureJoin(join);
                }
            }

            if (referencedJoins != null)
            {
                foreach (var alias in referencedJoins)
                {
                    if (intoRow.Joins.TryGetValue(alias, out join))
                    {
                        EnsureJoin(join);
                    }
                }
            }
        }
        public static TFields As <TFields>(this TFields fields, string alias)
            where TFields : RowFieldsBase, new()
        {
            Check.NotNullOrWhiteSpace(alias, "alias");

            if ((alias == "t0" || alias == "T0") &&
                (fields.alias == "t0" || fields.alias == "T0"))
            {
                return(fields);
            }

            if (fields.AliasName != "t0" && fields.AliasName != "T0")
            {
                throw new ArgumentException("You can't alias fields that are already aliased once!");
            }

            var key = new Tuple <Type, string>(typeof(TFields), alias);

            RowFieldsBase cached;

            if (cache.TryGetValue(key, out cached))
            {
                return((TFields)cached);
            }

            var result = new TFields();

            result.Initialize();

            var aliasPrefix = alias + "_";

            var joinByKey = new HashSet <string>(result.joins.Keys, StringComparer.OrdinalIgnoreCase);

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

                if (!joinByKey.Contains(x))
                {
                    return(x);
                }

                return(aliasPrefix + x);
            };

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

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

            foreach (var field in result)
            {
                field.expression = mapExpression(field.expression);

                if (field.referencedAliases != null && field.ReferencedAliases.Count > 0)
                {
                    var old = field.ReferencedAliases.ToArray();
                    field.ReferencedAliases.Clear();
                    foreach (var x in old)
                    {
                        field.ReferencedAliases.Add(mapAlias(x));
                    }
                }

                field.join      = null;
                field.joinAlias = field.joinAlias == null ? null : mapAlias(field.joinAlias);
            }

            var oldJoins = result.joins.ToArray();

            result.joins.Clear();
            foreach (var join in oldJoins)
            {
                BaseCriteria onCriteria;
                var          bc = join.Value.OnCriteria as BinaryCriteria;
                if (!ReferenceEquals(null, bc))
                {
                    onCriteria = new BinaryCriteria(
                        new Criteria(mapExpression(bc.LeftOperand.ToString())),
                        bc.Operator,
                        new Criteria(mapExpression(bc.RightOperand.ToString())));
                }
                else
                {
                    if (ReferenceEquals(null, join.Value.OnCriteria))
                    {
                        onCriteria = null;
                    }
                    else
                    {
                        onCriteria = new Criteria(mapExpression(join.Value.OnCriteria.ToString()));
                    }
                }

                new ReplacedJoin(result.Joins,
                                 mapExpression(join.Value.Table),
                                 mapAlias(join.Value.Name),
                                 onCriteria,
                                 join.Value.GetKeyword());
            }

            result.alias    = alias;
            result.aliasDot = alias + ".";

            cache[key] = result;
            return(result);
        }
        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));
        }