コード例 #1
0
ファイル: SourceJoinSqlModel.cs プロジェクト: dbshell/dbshell
        public SourceJoinSqlModel(TargetEntitySqlModel targetEntitySqlModel, SourceGraphSqlModel sourceGraph)
        {
            _targetEntitySqlModel = targetEntitySqlModel;
            _sourceGraph = sourceGraph;

            SourceToRefsJoin = new DmlfFromItem();


            //foreach (var column in _targetEntitySqlModel.RequiredSourceColumns)
            //{
            //    if (Columns.ContainsKey(column.Alias)) continue;

            //    Columns[column.Alias] = new SourceColumnSqlModel
            //    {
            //        Alias = column.Alias,
            //    };

            //    bool addedEntity = false;
            //    foreach (var ent in column.Entities)
            //    {
            //        if (ent.SingleKeyColumn == column.Alias)
            //        {
            //            AddEntity(ent);
            //            addedEntity = true;
            //            break;
            //        }
            //    }
            //    if (addedEntity) continue;

            //    AddEntity(column.Entities.First());
            //}

            if (_sourceGraph == null)
            {
                // flat variant
                PrimarySource = _targetEntitySqlModel.DataSync.FlatSources.FirstOrDefault(x => x.Match(_targetEntitySqlModel.Dbsh.PrimarySource));
                if (PrimarySource == null)
                {
                    throw new IncorrectRdsDefinitionException($"DBSH-00000 Source not found for entity {_targetEntitySqlModel.LogName}");
                }
                SourceToRefsJoin.Source = PrimarySource.QuerySource;
                foreach (var col in PrimarySource.Columns)
                {
                    Columns[col.Alias] = col;
                }
            }
            else
            {
                _entityQueue.AddRange(_sourceGraph.Entities);

                DetectPrimarySource();

                DetectUnusedEntities();

                RebuildEntityList();
                //queue.Add(_targetEntitySqlModel.KeySourceColumns.First().Entities.First());
                //foreach (var ent in _targetEntitySqlModel.RequiredSourceColumns.SelectMany(x => x.Entities))
                //{
                //    if (queue.Contains(ent)) continue;
                //    queue.Add(ent);
                //}

                if (!_entityQueue.Any() && targetEntitySqlModel.Dbsh.Columns.Any(x => x.IsKey))
                {
                    throw new IncorrectRdsDefinitionException($"LGM-00000 None of source entities is used in {_targetEntitySqlModel.LogName} (try to set source column)");
                }

                if (_entityQueue.Any())
                {
                    CreateSourceJoin();
                    AddRefsToJoin();
                }

                foreach (var col in Columns.Values) col.CompileFilter();
            }
        }
コード例 #2
0
        public DmlfSource GetRefSource(DmlfFromItem from, SourceJoinSqlModel joinModel)
        {
            var res = from.FindSourceWithAlias(SqlAlias);
            if (res != null) return res;
            res = new DmlfSource
            {
                Alias = SqlAlias,
                TableOrView = TargetTableSqlName,
                LinkedInfo = TargetLinkedInfo,
            };
            var rel = new DmlfRelation
            {
                JoinType = DmlfJoinType.Left,
                Reference = res,
            };

            foreach (var keycol in TargetColumns.Where(x => x.IsKey || x.IsRestriction))
            {
                rel.Conditions.Add(new DmlfEqualCondition
                {
                    LeftExpr = keycol.CreateSourceExpression(joinModel, false),
                    RightExpr = keycol.CreateTargetExpression(res),
                    CollateSpec = keycol.UseCollate(joinModel) ? "DATABASE_DEFAULT" : null,
                });
            }

            from.Relations.Add(rel);

            return res;
        }