private void DispatchCommands()
        {
            if (!_isLive || Interlocked.CompareExchange(ref _isDispatching, 1, 0) != 0)
            {
                return;
            }

            var candidates = Enumerable.Repeat(new { Id = default(Guid), CausationId = default(Guid) }, 0).ToList();

            using (var reader = _queryExecutor.ExecuteReader(TSql.QueryStatement(@"SELECT [Id], [CausationId] FROM [ApprovalProcess] WHERE [DispatchAcknowledged] = 0 AND ([Dispatched] IS NULL OR [Dispatched] < DATEADD(MINUTE, -5, GETDATE()))")))
            {
                candidates = reader.Cast <IDataRecord>()
                             .Select(record => new { Id = record.GetGuid(0), CausationId = record.GetGuid(1) })
                             .ToList();
            }

            foreach (var candidate in candidates)
            {
                var newCausationId = ApprovalProcessorConstants.DeterministicGuid.Create(candidate.CausationId);

                _bus.Publish(new MarkApprovalAccepted
                {
                    Id = candidate.Id,
                    ReferenceNumber = GuidEncoder.Encode(candidate.CausationId)
                }, context => context.SetHeader(Constants.CausationIdKey, newCausationId.ToString()));

                _queryExecutor.ExecuteNonQuery(TSql.NonQueryStatement(@"UPDATE [ApprovalProcess] SET [Dispatched] = GETDATE() WHERE [Id] = @P1", new { P1 = TSql.UniqueIdentifier(candidate.Id) }));
            }

            Interlocked.Exchange(ref _isDispatching, 0);
        }
Ejemplo n.º 2
0
        private static IEnumerable <TestCaseData> NonQueryStatementCases()
        {
            yield return(new TestCaseData(
                             TSql.NonQueryStatement("text"),
                             new SqlNonQueryCommand("text", new DbParameter[0], CommandType.Text)));

            yield return(new TestCaseData(
                             TSql.NonQueryStatement("text", parameters: null),
                             new SqlNonQueryCommand("text", new DbParameter[0], CommandType.Text)));

            yield return(new TestCaseData(
                             TSql.NonQueryStatement("text", new { }),
                             new SqlNonQueryCommand("text", new DbParameter[0], CommandType.Text)));

            yield return(new TestCaseData(
                             TSql.NonQueryStatement("text", new { Parameter = new TestDbParameter() }),
                             new SqlNonQueryCommand("text", new[]
            {
                new TestDbParameter().ToDbParameter("@Parameter")
            }, CommandType.Text)));

            yield return(new TestCaseData(
                             TSql.NonQueryStatement("text", new { Parameter1 = new TestDbParameter(), Parameter2 = new TestDbParameter() }),
                             new SqlNonQueryCommand("text", new[]
            {
                new TestDbParameter().ToDbParameter("@Parameter1"),
                new TestDbParameter().ToDbParameter("@Parameter2")
            }, CommandType.Text)));
        }
 public static IEnumerable <object> Handle(ItemPurchased purchased)
 {
     yield return(TSql.NonQueryStatement(@"insert into ItemsPurchased (StockKeepingUnit) values (@StockKeepingUnit)", new
     {
         purchased.StockKeepingUnit
     }));
 }
Ejemplo n.º 4
0
 public CustomerProjection()
 {
     When <CustomerCreated>(@event =>
                            TSql.NonQueryStatement(
                                "INSERT INTO [Customer] ([Id], [Name]) VALUES (@P1, @P2)",
                                new { P1 = TSql.UniqueIdentifier(@event.Id), P2 = TSql.NVarChar(@event.CustomerName, 40) }
                                ));
     When <CustomerNameChanged>(@event =>
                                TSql.NonQueryStatement(
                                    "UPDATE [Customer] SET [Name] = @P2 WHERE [Id] = @P1",
                                    new { P1 = TSql.UniqueIdentifier(@event.Id), P2 = TSql.NVarChar(@event.NewCustomerName, 40) }
                                    ));
     When <NewUserAdded>(@event =>
                         TSql.NonQueryStatement(
                             "INSERT INTO [CustomerUser] ([Id], [CustomerId], [Name]) VALUES (@P1, @P2, @P3)",
                             new { P1 = TSql.UniqueIdentifier(@event.UserId),
                                   P2 = TSql.UniqueIdentifier(@event.CustomerId),
                                   P3 = TSql.NVarChar(@event.UserName, 40) }
                             ));
     When <UserNameChanged>(@event =>
                            TSql.NonQueryStatement(
                                "UPDATE [CustomerUser] SET [Name] = @P2 WHERE [Id] = @P1",
                                new { P1 = TSql.UniqueIdentifier(@event.UserId), P2 = TSql.NVarChar(@event.NewUserName, 40) }
                                ));
     When <CreateSchema>(_ =>
                         TSql.NonQueryStatement(
                             @"IF NOT EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='Customer' AND XTYPE='U')
                 BEGIN
                     CREATE TABLE [Customer] (
                         [Id] UNIQUEIDENTIFIER NOT NULL CONSTRAINT PK_Customer PRIMARY KEY, 
                         [Name] NVARCHAR(MAX) NOT NULL)
                 END
                 IF NOT EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='CustomerUser' AND XTYPE='U')
                 BEGIN
                     CREATE TABLE [CustomerUser] (
                         [Id] UNIQUEIDENTIFIER NOT NULL CONSTRAINT PK_CustomerUser PRIMARY KEY, 
                         [CustomerId] UNIQUEIDENTIFIER NOT NULL,
                         [Name] NVARCHAR(MAX) NOT NULL)
                 END"));
     When <DropSchema>(_ => new[] {
         TSql.NonQueryStatement(
             @"IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='Customer' AND XTYPE='U')
                 DROP TABLE [Customer]"),
         TSql.NonQueryStatement(
             @"IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='CustomerUser' AND XTYPE='U')
                 DROP TABLE [CustomerUser]")
     });
     When <DeleteData>(_ => new [] {
         TSql.NonQueryStatement(
             @"IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='Customer' AND XTYPE='U')
                 DELETE FROM [Customer]"),
         TSql.NonQueryStatement(
             @"IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='CustomerUser' AND XTYPE='U')
                 DELETE FROM [CustomerUser]")
     });
 }
        public static IEnumerable <object> Project(ItemPurchased purchased, Guid nextEventId, int checkpointPosition)
        {
            yield return(TSql.NonQueryStatement(@"insert into ItemsPurchased (StockKeepingUnit) values (@StockKeepingUnit)", new
            {
                StockKeepingUnit = TSql.VarCharMax(purchased.StockKeepingUnit)
            }));

            yield return(TSql.NonQueryStatement(@"insert into StreamCheckpoint (Position) values (@Position)", new
            {
                Position = TSql.Int(checkpointPosition)
            }));

            yield return(new WriteToStream(nextEventId, "egress", new CheckpointEvent(checkpointPosition)));
        }
 public PortfolioProjection()
 {
     When <PortfolioAdded>(@event =>
                           TSql.NonQueryStatement(
                               "INSERT INTO [Portfolio] (Id, Name) VALUES (@P1, @P2)",
                               new { P1 = TSql.Int(@event.Id), P2 = TSql.NVarChar(@event.Name, 40) }
                               ));
     When <PortfolioRemoved>(@event =>
                             TSql.NonQueryStatement(
                                 "DELETE FROM [Portfolio] WHERE Id = @P1",
                                 new { P1 = TSql.Int(@event.Id) }
                                 ));
     When <PortfolioRenamed>(@event =>
                             TSql.NonQueryStatement(
                                 "UPDATE [Portfolio] SET Name = @P2 WHERE Id = @P1",
                                 new { P1 = TSql.Int(@event.Id), P2 = TSql.NVarChar(@event.Name, 40) }
                                 ));
 }
 public void NonQueryStatementParameterCountLimitedTo2098()
 {
     Assert.Throws <ArgumentException>(() => TSql.NonQueryStatement("", ParameterCountLimitedExceeded.Instance));
 }
 public static IEnumerable <SqlNonQueryCommand> CreateSchema()
 {
     yield return(TSql.NonQueryStatement(ScriptContents.CreateSchema));
 }