예제 #1
0
 protected override string GetSourceDataType(SourceJoinSqlModel sourceJoinModel)
 {
     switch (_dbsh.RealValueType)
     {
         case TargetColumnValueType.Source:
             var entity = sourceJoinModel[_dbsh.Source].Entities.First();
             return sourceJoinModel[_dbsh.Source].DbshColumns.First().DataType;
     }
     return null;
 }
예제 #2
0
 public override DmlfExpression CreateSourceExpression(SourceJoinSqlModel sourceJoinModel, bool aggregate)
 {
     var expr = new DmlfColumnRefExpression
     {
         Column = new DmlfColumnRef
         {
             ColumnName = _col.RefName,
             Source = _targetSqlModel.GetRefSource(sourceJoinModel.SourceToRefsJoin, sourceJoinModel),
         }
     };
     return GetExprOrAggregate(expr, aggregate);
 }
예제 #3
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;
        }
예제 #4
0
        public void CreateJoinModel()
        {
            _dbsh.LifetimeHandler.AddTargetColumns(this);

            //if (!KeySourceColumns.Any())
            //{
            //    throw new Exception($"DBSH-00220 Entity {dbsh.TableName} has no source for key");
            //}

            SourceJoinModel = new SourceJoinSqlModel(this, _dataSyncSqlModel.SourceGraphModel);

            RequiresGrouping = DetectGrouping();
        }
예제 #5
0
 public abstract DmlfExpression CreateSourceExpression(SourceJoinSqlModel sourceJoinModel, bool aggregate);
예제 #6
0
 public bool UseCollate(SourceJoinSqlModel sourceJoinModel)
 {
     if (Info == null) return false;
     string srcType = GetSourceDataType(sourceJoinModel);
     if (srcType == null) return false;
     if (srcType.ToLower().Contains("char") && (Info.DataType?.ToLower()?.Contains("char") ?? false)) return true;
     return false;
 }
예제 #7
0
 protected virtual string GetSourceDataType(SourceJoinSqlModel sourceJoinModel) => null;
예제 #8
0
        public override DmlfExpression CreateSourceExpression(SourceJoinSqlModel sourceJoinModel, bool aggregate)
        {
            switch (_dbsh.RealValueType)
            {
                case TargetColumnValueType.Source:
                    var entity = sourceJoinModel[_dbsh.Source].Entities.First();
                    return
                        GetExprOrAggregate(new DmlfColumnRefExpression
                        {
                            Column = new DmlfColumnRef
                            {
                                ColumnName = entity.GetColumnName(sourceJoinModel[_dbsh.Source].Alias),
                                Source = entity.QuerySource,
                            }
                        }, aggregate);
                case TargetColumnValueType.Value:
                    return new DmlfLiteralExpression
                    {
                        Value = _dbsh.Value,
                    };
                case TargetColumnValueType.Expression:
                    if (Regex.Match(_dbsh.Expression, TargetEntitySqlModel.ExpressionColumnRegex).Success)
                    {
                        string expr = Regex.Replace(
                            _dbsh.Expression, 
                            TargetEntitySqlModel.ExpressionColumnRegex, 
                            m => GetColumnExpression(sourceJoinModel, m.Groups[1].Value, aggregate));

                        return new DmlfSqlValueExpression
                        {
                            Value = expr,
                        };

                        //return GetExprOrAggregate(new DmlfSqlValueExpression
                        //{
                        //    Value = expr,
                        //}, aggregate);
                    }
                    else
                    {
                        return new DmlfSqlValueExpression
                        {
                            Value = _dbsh.Expression,
                        };
                    }
                case TargetColumnValueType.Template:
                    var addExpr = new DmlfAddOperatorExpression();
                    string rest = _dbsh.Template;
                    while (!String.IsNullOrEmpty(rest))
                    {
                        var match = Regex.Match(rest, TargetEntitySqlModel.ExpressionColumnRegex);
                        if (!match.Success) break;
                        if (match.Index > 0) addExpr.Items.Add(new DmlfLiteralExpression { Value = rest.Substring(0, match.Index) });
                        rest = rest.Substring(match.Index + match.Length);
                        string colExpr = GetColumnExpression(sourceJoinModel, match.Groups[1].Value, aggregate);
                        string allExpr = $"ISNULL(CONVERT(NVARCHAR,{colExpr}),'')";
                        addExpr.Items.Add(new DmlfSqlValueExpression { Value = allExpr });
                    }
                    if (!String.IsNullOrEmpty(rest))
                    {
                        addExpr.Items.Add(new DmlfLiteralExpression { Value = rest });
                    }
                    if (!addExpr.Items.Any()) addExpr.Items.Add(new DmlfLiteralExpression { Value = "" });
                    return addExpr;

                case TargetColumnValueType.Special:
                    switch (_dbsh.SpecialValue)
                    {
                        case TargetColumnSpecialValue.Null:
                            return new DmlfLiteralExpression
                            {
                                Value = null,
                            };
                        case TargetColumnSpecialValue.CurrentDateTime:
                            return new DmlfFuncCallExpression
                            {
                                FuncName = "GETDATE",
                            };
                        case TargetColumnSpecialValue.CurrentDate:
                            return new DmlfSqlValueExpression
                            {
                                Value = "DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))",
                            };
                        case TargetColumnSpecialValue.CurrentUtcDateTime:
                            return new DmlfFuncCallExpression
                            {
                                FuncName = "GETUTCDATE",
                            };
                        case TargetColumnSpecialValue.CurrentUtcDate:
                            return new DmlfSqlValueExpression
                            {
                                Value = "DATEADD(dd, 0, DATEDIFF(dd, 0, GETUTCDATE()))",
                            };
                        case TargetColumnSpecialValue.NewGUID:
                            return new DmlfFuncCallExpression
                            {
                                FuncName = "NEWID",
                            };
                        case TargetColumnSpecialValue.ImportDateTime:
                            {
                                return SqlScriptCompiler.ImportDateTimeExpression;
                            }
                        case TargetColumnSpecialValue.ImportDate:
                            return new DmlfSqlValueExpression
                            {
                                Value = SqlScriptCompiler.ImportDateVariableName,
                            };
                    }
                    break;

            }
            
            throw new IncorrectRdsDefinitionException($"DBSH-00221 Cannot create expression from column {Info?.OwnerTable?.Name}.{Name}. Try to set value type and corrent expression.");
        }
예제 #9
0
 private string GetColumnExpression(SourceJoinSqlModel sourceJoinModel, string colname, bool aggregate)
 {
     var entity = sourceJoinModel[colname].Entities.First();
     string res = $"[{entity.SqlAlias}].[{entity.GetColumnName(sourceJoinModel[colname].Alias)}]";
     if (aggregate) res = $"MAX({res})";
     return res;
 }
 public override DmlfExpression CreateSourceExpression(SourceJoinSqlModel sourceJoinModel, bool aggregate)
 {
     return SqlScriptCompiler.ImportDateTimeExpression;
 }