Пример #1
0
        public void SimplifyFromAliases()
        {
            if (IsSingleFromTable)
            {
                var source = From[0].Source;
                ReplaceSimpleSource(source, null);
            }
            else
            {
                var sources = new HashSet <DmlfSource>();
                From.ForEach(x => x.GetSimpleSources(sources));

                var usedAliases = new HashSet <string>();

                foreach (var source in sources)
                {
                    if (sources.Count(x => x.TableOrView == source.TableOrView) == 1)
                    {
                        // use table name as qualifier, remove alias
                        ReplaceSimpleSource(source, null);
                    }
                    else
                    {
                        var    aliasBase = DmlfSource.GetAliasBase(source.TableOrView.Name);
                        string alias     = DmlfSource.AllocateAlias(usedAliases, aliasBase);
                        ReplaceSimpleSource(source, alias);
                    }
                }
            }
        }
Пример #2
0
 public static DmlfColumnRef[] BuildFromArray(string[] cols, DmlfSource source)
 {
     return((
                from c in cols select new DmlfColumnRef {
         ColumnName = c, Source = source
     }
                ).ToArray());
 }
Пример #3
0
 public void ReplaceSimpleSource(DmlfSource source, string newAlias)
 {
     source = source.GetSimpleSourceCopy();
     ForEachChild(x =>
     {
         var src = x as DmlfSource;
         if (src != null && src == source)
         {
             src.Alias = newAlias;
         }
     });
 }
Пример #4
0
        public List <DmlfColumnRef> GetPrimaryKey(DmlfSource src)
        {
            var res = new List <DmlfColumnRef>();

            foreach (var fld in this)
            {
                var col = fld.Column;
                if (col != null && fld.ResultInfo != null && fld.ResultInfo.IsKey && col.Source == src)
                {
                    res.Add(col);
                }
            }
            return(res);
        }
Пример #5
0
 public static DmlfResultField BuildFromColumn(string colname, DmlfSource src)
 {
     return(new DmlfResultField
     {
         Expr = new DmlfColumnRefExpression
         {
             Column = new DmlfColumnRef
             {
                 ColumnName = colname,
                 Source = src,
             }
         },
     });
 }
Пример #6
0
        public DmlfColumnRef GetColumnRef(DmlfSource baseSource, NameWithSchema baseTable, StructuredIdentifier columnId, DatabaseInfo db, DmlfJoinType joinType)
        {
            var    relationId = columnId.WithoutLast;
            string column     = columnId.Last;
            var    source     = AddOrFindRelation(baseSource, baseTable, relationId, db, joinType);

            if (source == null)
            {
                return(null);
            }
            return(new DmlfColumnRef
            {
                ColumnName = column,
                Source = source,
            });
        }
Пример #7
0
        private DmlfSource DoAddOrFindRelation(DmlfSource baseSource, NameWithSchema baseTable, StructuredIdentifier relationJoined, StructuredIdentifier relationToJoin, DatabaseInfo db, DmlfJoinType joinType)
        {
            if (relationToJoin.IsEmpty)
            {
                return(baseSource);
            }
            string relName = relationToJoin.First;
            string alias   = String.Format("_REF{0}_{1}", relationJoined.NameItems.Select(x => "_" + x).CreateDelimitedText(""), relName);
            var    source  = FindSourceByAlias(alias);

            if (source == null)
            {
                var baseTableInfo = db.FindTable(baseTable);
                var fk            = baseTableInfo.ForeignKeys.FirstOrDefault(x => System.String.Compare(x.ConstraintName, relName, StringComparison.OrdinalIgnoreCase) == 0);
                if (fk == null)
                {
                    var column = baseTableInfo.FindColumn(relName);
                    if (column != null)
                    {
                        fk = column.GetForeignKeys().FirstOrDefault(x => x.Columns.Count == 1);
                    }
                }
                if (fk == null)
                {
                    return(null);
                }

                source = new DmlfSource
                {
                    TableOrView = fk.RefTableFullName,
                    Alias       = alias,
                };
                var relation = new DmlfRelation
                {
                    Reference = source,
                    JoinType  = joinType,
                };
                for (int i = 0; i < fk.Columns.Count; i++)
                {
                    relation.Conditions.Add(new DmlfEqualCondition
                    {
                        LeftExpr = new DmlfColumnRefExpression
                        {
                            Column = new DmlfColumnRef
                            {
                                ColumnName = fk.Columns[0].RefColumnName,
                                Source     = baseSource,
                            }
                        },
                        RightExpr = new DmlfColumnRefExpression
                        {
                            Column = new DmlfColumnRef
                            {
                                ColumnName = fk.RefColumns[0].RefColumnName,
                                Source     = source,
                            }
                        },
                    });
                    Relations.Add(relation);
                }
            }
            if (relationToJoin.IsEmpty)
            {
                return(source);
            }
            return(DoAddOrFindRelation(source, source.TableOrView, relationJoined / relationToJoin.First, relationToJoin.WithoutFirst, db, joinType));
        }
Пример #8
0
 public DmlfSource AddOrFindRelation(DmlfSource baseSource, NameWithSchema baseTable, StructuredIdentifier relationId, DatabaseInfo db, DmlfJoinType joinType)
 {
     return(DoAddOrFindRelation(baseSource, baseTable, new StructuredIdentifier(), relationId, db, joinType));
 }