Beispiel #1
0
        public string GetAlias(string desiredAlias, string defaultAlias)
        {
            if (_aliases == null)
            {
                _aliases = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            }

            var alias = desiredAlias;

            if (string.IsNullOrEmpty(desiredAlias) || desiredAlias.Length > 25)
            {
                desiredAlias = defaultAlias;
                alias        = defaultAlias + "1";
            }

            for (var i = 1; ; i++)
            {
                if (!_aliases.Contains(alias) && !ReservedWords.IsReserved(alias))
                {
                    _aliases.Add(alias);
                    break;
                }

                alias = desiredAlias + i;
            }

            return(alias);
        }
Beispiel #2
0
        internal void SetAliases()
        {
            _aliases = null;

            Parameters.Clear();

            var allAliases    = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            var paramsVisited = new HashSet <SqlParameter>();
            var tablesVisited = new HashSet <SqlTableSource>();

            new QueryVisitor().VisitAll(this, expr =>
            {
                switch (expr.ElementType)
                {
                case QueryElementType.MergeSourceTable:
                    {
                        var source = (SqlMergeSourceTable)expr;

                        Utils.MakeUniqueNames(
                            source.SourceFields,
                            null,
                            (n, a) => !ReservedWords.IsReserved(n),
                            f => f.PhysicalName,
                            (f, n, a) => { f.PhysicalName = n; },
                            f =>
                        {
                            var a = f.PhysicalName;
                            return(a.IsNullOrEmpty()
                                                                                ? "c1"
                                                                                : a + (a !.EndsWith("_") ? string.Empty : "_") + "1");
                        },
        public string GetAlias(string desiredAlias, string defaultAlias)
        {
            if (_aliases == null)
            {
                _aliases = new Dictionary <string, object>();
            }

            var alias = desiredAlias;

            if (string.IsNullOrEmpty(desiredAlias) || desiredAlias.Length > 25)
            {
                desiredAlias = defaultAlias;
                alias        = defaultAlias + "1";
            }

            for (var i = 1; ; i++)
            {
                var s = alias.ToUpper();

                if (!_aliases.ContainsKey(s) && !ReservedWords.IsReserved(s))
                {
                    _aliases.Add(s, s);
                    break;
                }

                alias = desiredAlias + i;
            }

            return(alias);
        }
Beispiel #4
0
        public static void PrepareQueryAndAliases(SqlStatement statement, AliasesContext?prevAliasContext, out AliasesContext newAliasContext)
        {
            var allAliases = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            HashSet <string>?allParameterNames = null;

            HashSet <SqlParameter>?  paramsVisited = null;
            HashSet <SqlTableSource>?tablesVisited = null;

            var newAliases = new AliasesContext();

            new QueryVisitor().VisitAll(statement, expr =>
            {
                if (prevAliasContext != null && prevAliasContext.IsAliased(expr))
                {
                    // Copy aliased from previous run
                    //
                    newAliases.RegisterAliased(expr);

                    // Remember already used aliases from previous run
                    if (expr.ElementType == QueryElementType.TableSource)
                    {
                        var alias = ((SqlTableSource)expr).Alias;
                        if (!string.IsNullOrEmpty(alias))
                        {
                            allAliases.Add(alias !);
                        }
                    }
                    else if (expr.ElementType == QueryElementType.SqlParameter)
                    {
                        var alias = ((SqlParameter)expr).Name;
                        if (!string.IsNullOrEmpty(alias))
                        {
                            allParameterNames ??= new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                            allParameterNames.Add(alias !);
                        }
                    }

                    return;
                }

                switch (expr.ElementType)
                {
                case QueryElementType.MergeSourceTable:
                    {
                        var source = (SqlTableLikeSource)expr;

                        Utils.MakeUniqueNames(
                            source.SourceFields,
                            null,
                            (n, a) => !ReservedWords.IsReserved(n),
                            f => f.PhysicalName,
                            (f, n, a) => { f.PhysicalName = n; },
                            f =>
                        {
                            var a = f.PhysicalName;
                            return(a.IsNullOrEmpty()
                                                                                ? "c1"
                                                                                : a + (a !.EndsWith("_") ? string.Empty : "_") + "1");
                        },
Beispiel #5
0
        internal void SetAliases()
        {
            _aliases = null;

            Parameters.Clear();

            var allAliases    = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            var paramsVisited = new HashSet <SqlParameter>();
            var tablesVisited = new HashSet <SqlTableSource>();

            new QueryVisitor().VisitAll(this, expr =>
            {
                switch (expr.ElementType)
                {
                case QueryElementType.SqlQuery:
                    {
                        var query = (SelectQuery)expr;
                        if (query.IsParameterDependent)
                        {
                            IsParameterDependent = true;
                        }

                        if (query.Select.Columns.Count > 0)
                        {
                            var isRootQuery = query.ParentSelect == null;

                            Utils.MakeUniqueNames(query.Select.Columns.Where(c => c.Alias != "*"),
                                                  n => !ReservedWords.IsReserved(n), c => c.Alias, (c, n) =>
                            {
                                if (isRootQuery)
                                {
                                    allAliases.Add(n);
                                }
                                c.Alias = n;
                            },
                                                  c =>
                            {
                                var a = c.Alias;
                                return(a.IsNullOrEmpty()
                                                                                        ? "c1"
                                                                                        : a + (a.EndsWith("_") ? string.Empty : "_") + "1");
                            },
                                                  StringComparer.OrdinalIgnoreCase);

                            if (query.HasUnion)
                            {
                                for (var i = 0; i < query.Select.Columns.Count; i++)
                                {
                                    var col = query.Select.Columns[i];

                                    foreach (var t in query.Unions)
                                    {
                                        var union = t.SelectQuery.Select;
                                        union.Columns[i].Alias = col.Alias;
                                    }
                                }
                            }
                        }

                        break;
                    }

                case QueryElementType.SqlParameter:
                    {
                        var p = (SqlParameter)expr;

                        if (p.IsQueryParameter)
                        {
                            if (paramsVisited.Add(p))
                            {
                                Parameters.Add(p);
                            }
                        }
                        else
                        {
                            IsParameterDependent = true;
                        }

                        break;
                    }

                case QueryElementType.TableSource:
                    {
                        var table = (SqlTableSource)expr;
                        tablesVisited.Add(table);
                        break;
                    }
                }
            });

            Utils.MakeUniqueNames(tablesVisited,
                                  n => !ReservedWords.IsReserved(n), ts => ts.Alias, (ts, n) =>
            {
                allAliases.Add(n);
                ts.Alias = n;
            },
                                  ts =>
            {
                var a = ts.Alias;
                return(a.IsNullOrEmpty() ? "t1" : a + (a.EndsWith("_") ? string.Empty : "_") + "1");
            },
                                  StringComparer.OrdinalIgnoreCase);

            Utils.MakeUniqueNames(Parameters,
                                  n => !allAliases.Contains(n) && !ReservedWords.IsReserved(n), p => p.Name, (p, n) =>
            {
                allAliases.Add(n);
                p.Name = n;
            },
                                  ts => ts.Name ?? "p1",
                                  StringComparer.OrdinalIgnoreCase);

            _aliases = allAliases;
        }