コード例 #1
0
        private static string BuildSqlForTruncateTime(DateTimeOffset dateTimeOffset, SqlVersion sqlVersion)
        {
            var builder = new StringBuilder();

            var sqlGenerator = new SqlGenerator(sqlVersion);

            var functionExpression = EdmFunctions.TruncateTime(
                DbExpression.FromDateTimeOffset(dateTimeOffset));

            var sqlFragment = SqlFunctionCallHandler.GenerateFunctionCallSql(
                sqlGenerator, functionExpression);

            using (var sqlWriter = new SqlWriter(builder))
            {
                sqlFragment.WriteSql(sqlWriter, sqlGenerator);
            }

            return(builder.ToString());
        }
コード例 #2
0
 public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
 {
     if (interceptionContext.Result.DataSpace == DataSpace.SSpace &&
         interceptionContext.DbContexts.All(con => con.GetType().GetCustomAttribute <TrackDisabledAttribute>() == null))
     {
         #region Query
         var queryCommand = interceptionContext.Result as DbQueryCommandTree;
         if (queryCommand != null)
         {
             var newQuery = queryCommand.Query.Accept(new TrackingQueryVisitor());
             interceptionContext.Result = new DbQueryCommandTree(
                 queryCommand.MetadataWorkspace,
                 queryCommand.DataSpace,
                 newQuery);
         }
         #endregion
         #region Delete
         var deleteCommand = interceptionContext.Result as DbDeleteCommandTree;
         if (deleteCommand != null)
         {
             var bitColumn      = ColumnAnnotationAttribute.GetColumnName <TrackDeletedBitAttribute>(deleteCommand.Target.VariableType.EdmType);
             var dateColumn     = ColumnAnnotationAttribute.GetColumnName <TrackDeletedDateAttribute>(deleteCommand.Target.VariableType.EdmType);
             var identityColumn = ColumnAnnotationAttribute.GetColumnName <TrackDeletedIdentityAttribute>(deleteCommand.Target.VariableType.EdmType);
             var clauses        = new List <DbModificationClause>();
             if (bitColumn != null)
             {
                 clauses.Add(DbExpressionBuilder.SetClause(deleteCommand.Target.Variable.Property(bitColumn), DbExpression.FromBoolean(true)));
                 if (dateColumn != null)
                 {
                     clauses.Add(DbExpressionBuilder.SetClause(deleteCommand.Target.Variable.Property(dateColumn), DbExpression.FromDateTimeOffset(TimeZoneInfo.ConvertTime(timeProvider.NowOffsetted().Add(_dateTimeAdjustment), getCurrentTimeZoneFunction()))));
                 }
                 if (identityColumn != null)
                 {
                     clauses.Add(DbExpressionBuilder.SetClause(deleteCommand.Target.Variable.Property(identityColumn), DbExpression.FromString(getCurrentIdentityFunction())));
                 }
             }
             if (clauses.Count > 0)
             {
                 //Add this constant clause to trace deletions on update command
                 clauses.Add(DbExpressionBuilder.SetClause(DbExpressionBuilder.Constant(137), DbExpressionBuilder.Constant(137)));
                 interceptionContext.Result = new DbUpdateCommandTree(
                     deleteCommand.MetadataWorkspace,
                     deleteCommand.DataSpace,
                     deleteCommand.Target,
                     deleteCommand.Predicate,
                     clauses.AsReadOnly(),
                     null);
             }
         }
         #endregion
         #region Insert
         var insertCommand = interceptionContext.Result as DbInsertCommandTree;
         if (insertCommand != null)
         {
             var dateColumn     = ColumnAnnotationAttribute.GetColumnName <TrackCreatedDateAttribute>(insertCommand.Target.VariableType.EdmType);
             var identityColumn = ColumnAnnotationAttribute.GetColumnName <TrackCreatedIdentityAttribute>(insertCommand.Target.VariableType.EdmType);
             var clauses        = insertCommand.SetClauses.ToList();
             RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackDeletedBitAttribute>(insertCommand.Target.VariableType.EdmType));
             RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackDeletedDateAttribute>(insertCommand.Target.VariableType.EdmType));
             RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackDeletedIdentityAttribute>(insertCommand.Target.VariableType.EdmType));
             RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackModifiedDateAttribute>(insertCommand.Target.VariableType.EdmType));
             RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackModifiedIdentityAttribute>(insertCommand.Target.VariableType.EdmType));
             if (dateColumn != null)
             {
                 AddOrReplaceClause(clauses, DbExpressionBuilder.SetClause(insertCommand.Target.Variable.Property(dateColumn), DbExpression.FromDateTimeOffset(TimeZoneInfo.ConvertTime(timeProvider.NowOffsetted().Add(_dateTimeAdjustment), getCurrentTimeZoneFunction()))));
             }
             if (identityColumn != null)
             {
                 AddOrReplaceClause(clauses, DbExpressionBuilder.SetClause(insertCommand.Target.Variable.Property(identityColumn), DbExpression.FromString(getCurrentIdentityFunction())));
             }
             if (dateColumn != null || (identityColumn != null))
             {
                 interceptionContext.Result = new DbInsertCommandTree(
                     insertCommand.MetadataWorkspace,
                     insertCommand.DataSpace,
                     insertCommand.Target,
                     clauses.AsReadOnly(),
                     null);
             }
         }
         #endregion
         #region Update
         var updateCommand = interceptionContext.Result as DbUpdateCommandTree;
         if (updateCommand != null)
         {
             var dateColumn            = ColumnAnnotationAttribute.GetColumnName <TrackModifiedDateAttribute>(updateCommand.Target.VariableType.EdmType);
             var identityColumn        = ColumnAnnotationAttribute.GetColumnName <TrackModifiedIdentityAttribute>(updateCommand.Target.VariableType.EdmType);
             var clauses               = updateCommand.SetClauses.ToList();
             var traceDeleteionsClause = clauses.FirstOrDefault(c =>
                                                                ((DbSetClause)c).Property is DbConstantExpression && ((DbConstantExpression)((DbSetClause)c).Property).Value is int && ((int)((DbConstantExpression)((DbSetClause)c).Property).Value) == 137 &&
                                                                ((DbSetClause)c).Value is DbConstantExpression && ((DbConstantExpression)((DbSetClause)c).Value).Value is int && ((int)((DbConstantExpression)((DbSetClause)c).Value).Value) == 137
                                                                );
             if (traceDeleteionsClause != null)
             {
                 //This command is derived from a deletion operation
                 clauses.Remove(traceDeleteionsClause);
             }
             else
             {
                 RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackDeletedBitAttribute>(updateCommand.Target.VariableType.EdmType));
                 RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackDeletedDateAttribute>(updateCommand.Target.VariableType.EdmType));
                 RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackDeletedIdentityAttribute>(updateCommand.Target.VariableType.EdmType));
                 RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackCreatedDateAttribute>(updateCommand.Target.VariableType.EdmType));
                 RemoveClause(clauses, ColumnAnnotationAttribute.GetColumnName <TrackCreatedIdentityAttribute>(updateCommand.Target.VariableType.EdmType));
                 if (dateColumn != null)
                 {
                     AddOrReplaceClause(clauses, DbExpressionBuilder.SetClause(updateCommand.Target.Variable.Property(dateColumn), DbExpression.FromDateTimeOffset(TimeZoneInfo.ConvertTime(timeProvider.NowOffsetted().Add(_dateTimeAdjustment), getCurrentTimeZoneFunction()))));
                 }
                 if (identityColumn != null)
                 {
                     AddOrReplaceClause(clauses, DbExpressionBuilder.SetClause(updateCommand.Target.Variable.Property(identityColumn), DbExpression.FromString(getCurrentIdentityFunction())));
                 }
             }
             if (dateColumn != null || (identityColumn != null))
             {
                 interceptionContext.Result = new DbUpdateCommandTree(
                     updateCommand.MetadataWorkspace,
                     updateCommand.DataSpace,
                     updateCommand.Target,
                     updateCommand.Predicate,
                     clauses.AsReadOnly(),
                     null);
             }
         }
         #endregion
     }
 }
コード例 #3
0
        protected override Expression VisitConstant(ConstantExpression node)
        {
            var expression = base.VisitConstant(node);

            var type = node.Type;

            if (IsNullableType(type))
            {
                var genericArgs = type.GetGenericArguments();
                if ((genericArgs != null) && (genericArgs.Length == 1))
                {
                    type = genericArgs[0];
                }
            }

            if (type == typeof(byte[]))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromBinary((byte[])node.Value));
            }
            else if (type == typeof(bool))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromBoolean((bool?)node.Value));
            }
            else if (type == typeof(byte))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromByte((byte?)node.Value));
            }
            else if (type == typeof(DateTime))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromDateTime((DateTime?)node.Value));
            }
            else if (type == typeof(DateTimeOffset))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromDateTimeOffset((DateTimeOffset?)node.Value));
            }
            else if (type == typeof(decimal))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromDecimal((decimal?)node.Value));
            }
            else if (type == typeof(double))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromDouble((double?)node.Value));
            }
            else if (type == typeof(Guid))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromGuid((Guid?)node.Value));
            }
            else if (type == typeof(Int16))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromInt16((Int16?)node.Value));
            }
            else if (type == typeof(Int32))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromInt32((Int32?)node.Value));
            }
            else if (type.IsEnum)
            {
                MapExpressionToDbExpression(expression, DbExpression.FromInt32((Int32)node.Value));
            }
            else if (type == typeof(Int64))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromInt64((Int64?)node.Value));
            }
            else if (type == typeof(float))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromSingle((float?)node.Value));
            }
            else if (type == typeof(string))
            {
                MapExpressionToDbExpression(expression, DbExpression.FromString((string)node.Value));
            }
            else
            {
                throw new NotImplementedException(string.Format("Unhandled Type of {0} for Constant value {1} in LambdaToDbExpressionVisitor.VisitConstant", node.Type.Name, node.Value ?? "null"));
            }

            return(expression);
        }