protected virtual Expression VisitAsOfTable(TemporalTableExpression tableExpression)
        {
            // This method was modeled on "SqlServerQuerySqlGenerator.VisitTable".
            // Where we deviate, is after printing the table name, we check if temporal constraints
            // need to be applied.

            Sql.Append(_sqlGenerationHelper.DelimitIdentifier(tableExpression.Name, tableExpression.Schema));

            if (tableExpression.AsOfDate != null)
            {
                var name = TEMPORAL_PARAMETER_PREFIX + tableExpression.AsOfDate.Name;
                Sql.Append($" FOR SYSTEM_TIME AS OF @{name}"); //2020-02-28T11:00:00

                if (!_commandbuilder.Parameters.Any(x => x.InvariantName == tableExpression.AsOfDate.Name))
                {
                    _commandbuilder.AddParameter(tableExpression.AsOfDate.Name, name);
                }
            }

            Sql
            .Append(AliasSeparator)
            .Append(_sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));

            return(tableExpression);
        }
        protected virtual Expression VisitAllTable(TemporalTableExpression tableExpression)
        {
            // This method was modeled on "SqlServerQuerySqlGenerator.VisitTable".
            // Where we deviate, is after printing the table name, we apply the temporal constraints
            Sql.Append(_sqlGenerationHelper.DelimitIdentifier(tableExpression.Name, tableExpression.Schema))
            .Append(" FOR SYSTEM_TIME ALL")
            .Append(AliasSeparator)
            .Append(_sqlGenerationHelper.DelimitIdentifier(tableExpression.Alias));

            return(tableExpression);
        }
Example #3
0
        public override SelectExpression Select(IEntityType entityType)
        {
            if (entityType.FindAnnotation(SqlServerEntityTypeBuilderExtensions.ANNOTATION_TEMPORAL) != null)
            {
                var temporalTableExpression = new TemporalTableExpression(
                    entityType.GetTableName(),
                    entityType.GetSchema(),
                    entityType.GetTableName().ToLower().Substring(0, 1));

                var selectContructor = typeof(SelectExpression).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(IEntityType), typeof(TableExpressionBase) }, null);
                var select           = (SelectExpression)selectContructor.Invoke(new object[] { entityType, temporalTableExpression });

                var privateInitializer = typeof(SqlExpressionFactory).GetMethod("AddConditions", BindingFlags.NonPublic | BindingFlags.Instance);
                privateInitializer.Invoke(this, new object[] { select, entityType, null, null });

                return(select);
            }

            return(base.Select(entityType));
        }