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 void UniqueIdentifierReturnsExpectedInstance() { Assert.That(TSql.UniqueIdentifier(Guid.Empty), Is.EqualTo(new TSqlUniqueIdentifierValue(Guid.Empty))); }
public void UniqueIdentifierNullReturnsExpectedInstance() { Assert.That(TSql.UniqueIdentifier(null), Is.EqualTo(TSqlUniqueIdentifierNullValue.Instance)); }
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); }