Esempio n. 1
0
        public void MaterializeIfNeeded()
        {
            if (IsExternal) return;
            if (!_dbsh.Materialize) return;

            _materializedName = new NameWithSchema("#" + SqlAlias.Replace(".", "_"));
            _materializeSelect = new DmlfSelect();
            _materializeSelect.SingleFrom.Source = QuerySource;
            _materializeSelect.SelectIntoTable = _materializedName;
            _materializeSelect.SelectAll = true;

            QuerySource = new DmlfSource
            {
                Alias = SqlAlias,
                TableOrView = _materializedName,
            };
        }
Esempio n. 2
0
        private DmlfSelect BuildSelect()
        {
            var select = new DmlfSelect();
            var from = select.SingleFrom;
            from.Source = new DmlfSource
            {
                TableOrView = EntityFullName(_rootEntity),
                Alias = EntityAlias(_rootEntity),
            };

            var added = new List<SourceEntitySqlModel>();
            added.Add(_rootEntity);

            foreach (var entity in _filteredEntities)
            {
                if (added.Contains(entity)) continue;
                var relation = new DmlfRelation
                {
                    JoinType = DmlfJoinType.Left,
                    Reference = new DmlfSource
                    {
                        TableOrView = EntityFullName(entity),
                        Alias = EntityAlias(entity),
                    },
                };


                foreach (var column in entity.Columns)
                {
                    foreach (var ent2 in added)
                    {
                        if (ent2.Columns.Any(x => x.Alias == column.Alias))
                        {
                            relation.Conditions.Add(new DmlfEqualCondition
                            {
                                LeftExpr = new DmlfColumnRefExpression
                                {
                                    Column = new DmlfColumnRef
                                    {
                                        ColumnName = entity.GetColumnName(column.Alias),
                                        Source = new DmlfSource { Alias = EntityAlias(entity) },
                                    }
                                },
                                RightExpr = new DmlfColumnRefExpression
                                {
                                    Column = new DmlfColumnRef
                                    {
                                        ColumnName = ent2.GetColumnName(column.Alias),
                                        Source = new DmlfSource { Alias = EntityAlias(ent2) },
                                    }
                                },
                            });
                        }
                    }
                }

                added.Add(entity);
                from.Relations.Add(relation);
            }

            foreach(var col in _rootEntity.Dbsh.Columns)
            {
                select.Columns.Add(DmlfResultField.BuildFromColumn(col.Name, new DmlfSource { Alias = EntityAlias(_rootEntity) }));
            }

            foreach(var entity in _filteredEntities)
            {
                foreach(var col in entity.Dbsh.Columns)
                {
                    if (col.Filter == null) continue;
                    var orCond = new DmlfOrCondition();
                    orCond.Conditions.Add(SourceColumnSqlModel.CompileSingleFilter(col, EntityAlias(entity)));
                    if (entity.SingleKeyColumnOriginalName != null)
                    {
                        orCond.Conditions.Add(new DmlfIsNullCondition
                        {
                            Expr = new DmlfColumnRefExpression
                            {
                                Column = new DmlfColumnRef
                                {
                                    ColumnName = entity.SingleKeyColumnOriginalName,
                                    Source = new DmlfSource { Alias = EntityAlias(entity) },
                                }
                            }
                        });
                    }
                    select.AddAndCondition(orCond);
                }
            }

            return select;
        }
Esempio n. 3
0
 private DmlfUpdate CompileMarkDeleted()
 {
     var res = new DmlfUpdate();
     res.UpdateTarget = new DmlfSource { Alias = "target" };
     res.SingleFrom.Source = new DmlfSource
     {
         Alias = "target",
         TableOrView = TargetTableSqlName,
         LinkedInfo = TargetLinkedInfo,
     };
     var existSelect = new DmlfSelect();
     res.AddAndCondition(new DmlfNotExistCondition
     {
         Select = existSelect,
     });
     existSelect.SelectAll = true;
     existSelect.From.Add(SourceJoinModel.SourceToRefsJoin);
     CreateKeyCondition(existSelect, "target");
     CreateRestrictionCondition(res, "target");
     CreateLifetimeConditions(res, "target");
     CreateFilterConditions(existSelect);
     _dbsh.LifetimeHandler.CreateSetDeletedUpdateFields(res, "target", this);
     return res;
 }
Esempio n. 4
0
        private DmlfDelete CompileDelete()
        {
            var res = new DmlfDelete();
            res.DeleteTarget = new DmlfSource { Alias = "target" };
            res.SingleFrom.Source = new DmlfSource
            {
                Alias = "target",
                TableOrView = TargetTableSqlName,
                LinkedInfo = TargetLinkedInfo,
            };
            var existSelect = new DmlfSelect();
            res.AddAndCondition(new DmlfNotExistCondition
            {
                Select = existSelect,
            });
            existSelect.SelectAll = true;
            existSelect.From.Add(SourceJoinModel.SourceToRefsJoin);
            CreateKeyCondition(existSelect, "target");
            CreateRestrictionCondition(res, "target", false);
            CreateLifetimeConditions(res, "target");
            CreateReferenceRestrictionsCodition(res, "target");

            CreateFilterConditions(existSelect);
            return res;
        }
Esempio n. 5
0
        private DmlfInsertSelect CompileInsert()
        {
            var res = new DmlfInsertSelect();
            res.TargetTable = TargetTableSqlName;
            res.LinkedInfo = TargetLinkedInfo;
            res.Select = new DmlfSelect();
            res.Select.From.Add(SourceJoinModel.SourceToRefsJoin);

            foreach (var col in TargetColumns)
            {
                res.TargetColumns.Add(col.Name);
                var expr = col.CreateSourceExpression(SourceJoinModel, RequiresGrouping && !col.IsKey && !col.IsRestriction);
                res.Select.Columns.Add(new DmlfResultField
                {
                    Expr = expr,
                });
            }

            if (RequiresGrouping)
            {
                res.Select.GroupBy = new DmlfGroupByCollection();
                foreach (var col in TargetColumns.Where(x => x.IsKey || (x.IsRestriction && x.IsReference)))
                {
                    var expr = col.CreateSourceExpression(SourceJoinModel, false);
                    res.Select.GroupBy.Add(new DmlfGroupByItem
                    {
                        Expr = expr,
                    });
                }
            }

            CreateNotNullConditions(res.Select);

            var existSelect = new DmlfSelect();
            existSelect.SingleFrom.Source = new DmlfSource
            {
                TableOrView = TargetTableSqlName,
                Alias = "tested",
                LinkedInfo = TargetLinkedInfo,
            };
            existSelect.SelectAll = true;
            CreateKeyCondition(existSelect, "tested");
            CreateLifetimeConditions(existSelect, "tested");
            CreateFilterConditions(res.Select);

            res.Select.AddAndCondition(new DmlfNotExistCondition { Select = existSelect });

            return res;
        }