Exemplo n.º 1
0
        private DmlfUpdate CompileUpdateCore(Func<TargetColumnSqlModelBase, bool> compareColumn, Func<TargetColumnSqlModelBase, bool> updateColumn, Action<DmlfUpdate> createUpdateSpecialColumns, CompareColumnContext compareContext, UpdateColumnContext updateContext)
        {
            var cmd = new DmlfUpdate();
            cmd.UpdateTarget = new DmlfSource { Alias = "target" };
            cmd.From.Add(SourceJoinModel.SourceToRefsJoin);
            cmd.From.Add(new DmlfFromItem
            {
                Source = new DmlfSource
                {
                    Alias = "target",
                    TableOrView = TargetTableSqlName,
                    LinkedInfo = TargetLinkedInfo,
                }
            });
            CreateKeyCondition(cmd, "target");
            CreateLifetimeConditions(cmd, "target");
            CreateFilterConditions(cmd);

            createUpdateSpecialColumns(cmd);

            foreach (var column in TargetColumns)
            {
                bool? update = _dbsh.LifetimeHandler.UpdateColumn(column.Name, updateContext);
                if (update == null) update = updateColumn(column);
                if (!update.Value) continue;
                cmd.Columns.Add(new DmlfUpdateField
                {
                    TargetColumn = column.Name,
                    Expr = column.CreateSourceExpression(SourceJoinModel, false),
                });
            }

            var orCondition = new DmlfOrCondition();
            foreach (var column in TargetColumns)
            {
                bool? compare = _dbsh.LifetimeHandler.CompareColumn(column.Name, compareContext);

                if (compare == null) compare = compareColumn(column);
                if (!compare.Value) continue;
                if (column.CannotBeCompared) continue;

                orCondition.Conditions.Add(new DmlfNotEqualWithNullTestCondition
                {
                    LeftExpr = column.CreateCompareExpression(column.CreateSourceExpression(SourceJoinModel, false)),
                    RightExpr = column.CreateCompareExpression(column.CreateTargetExpression("target")),
                    CollateSpec = column.UseCollate(SourceJoinModel) ? "DATABASE_DEFAULT" : null,
                });
            }
            if (orCondition.Conditions.Any()) cmd.AddAndCondition(orCondition);

            if (!cmd.Columns.Any()) return null;
            return cmd;
        }
Exemplo 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;
        }
Exemplo n.º 3
0
        private void CreateFilterConditions(DmlfCommandBase cmd)
        {
            foreach(var col in SourceJoinModel.Columns.Values)
            {
                var colFilterCondition = col.FilterCondition;

                // process additional condition
                var additional = _dbsh.AdditionalFilters.FirstOrDefault(x => x.Column == col.Alias);
                if (additional != null)
                {
                    var type = SourceColumnSqlModel.DetectFilterType(additional.FilterType, col.DbshColumns);

                    var entity = col.Entities.First();
                    var expr = new DmlfColumnRefExpression
                    {
                        Column = new DmlfColumnRef
                        {
                            ColumnName = entity.GetColumnName(col.Alias),
                            Source = entity.QuerySource,
                        }
                    };

                    var additionalCondition = FilterParser.ParseFilterExpression(type, expr, additional.Filter);

                    if (colFilterCondition != null)
                    {
                        var andCondition = new DmlfAndCondition();
                        andCondition.Conditions.Add(additionalCondition);
                        andCondition.Conditions.Add(colFilterCondition);
                        colFilterCondition = andCondition;
                    }
                    else
                    {
                        colFilterCondition = additionalCondition;
                    }
                }

                if (colFilterCondition != null)
                {
                    var orCond = new DmlfOrCondition();

                    if (col.Entities.Count == 1)
                    {
                        var entity = col.Entities.Single();
                        if (entity.SingleKeyColumnNameOrAlias != null)
                        {
                            var expr = new DmlfColumnRefExpression
                            {
                                Column = new DmlfColumnRef
                                {
                                    ColumnName = entity.GetColumnName(entity.SingleKeyColumnNameOrAlias),
                                    Source = entity.QuerySource,
                                }
                            };
                            orCond.Conditions.Add(new DmlfIsNullCondition
                            {
                                Expr = expr,
                            });
                        }
                    }

                    if (orCond.Conditions.Any())
                    {
                        orCond.Conditions.Add(colFilterCondition);
                        cmd.AddAndCondition(orCond);
                    }
                    else
                    {
                        cmd.AddAndCondition(colFilterCondition);
                    }
                }
            }
        }