예제 #1
0
        public static Guid Execute(DocumentTransaction tx, InsertCommand command)
        {
            var values = ConvertAnonymousToProjections(command.Table, command.Projections);

            values[DocumentTable.IdColumn]            = command.Id;
            values[DocumentTable.EtagColumn]          = tx.CommitId;
            values[DocumentTable.CreatedAtColumn]     = DateTimeOffset.Now;
            values[DocumentTable.ModifiedAtColumn]    = DateTimeOffset.Now;
            values[DocumentTable.LastOperationColumn] = Operation.Inserted;

            var sql = $@"
                insert into {tx.Store.Database.FormatTableNameAndEscape(command.Table.Name)}
                ({string.Join(", ", from column in values.Keys select column.Name)})
                values ({string.Join(", ", from column in values.Keys select "@" + column.Name)});";

            var parameters = Parameters.FromProjections(values);

            DocumentWriteCommand.Execute(tx, new SqlDatabaseCommand
            {
                Sql              = sql,
                Parameters       = parameters,
                ExpectedRowCount = 1
            });

            return(tx.CommitId);
        }
예제 #2
0
        public static Guid Execute(DocumentTransaction tx, UpdateCommand command)
        {
            var values = ConvertAnonymousToProjections(command.Table, command.Projections);

            values[DocumentTable.EtagColumn]          = tx.CommitId;
            values[DocumentTable.ModifiedAtColumn]    = DateTimeOffset.Now;
            values[DocumentTable.LastOperationColumn] = Operation.Updated;

            var sql = new SqlBuilder()
                      .Append($"update {tx.Store.Database.FormatTableNameAndEscape(command.Table.Name)}")
                      .Append($"set {string.Join(", ", from column in values.Keys select column.Name + " = @" + column.Name)}")
                      .Append($"where {DocumentTable.IdColumn.Name}=@Id")
                      .Append(!command.LastWriteWins, $"and {DocumentTable.EtagColumn.Name}=@ExpectedEtag")
                      .ToString();

            var parameters = Parameters.FromProjections(values);

            parameters.Add("@Id", command.Id, SqlTypeMap.Convert(DocumentTable.IdColumn).DbType, null);

            if (!command.LastWriteWins)
            {
                parameters.Add("@ExpectedEtag", command.ExpectedEtag, SqlTypeMap.Convert(DocumentTable.EtagColumn).DbType, null);
            }

            DocumentWriteCommand.Execute(tx, new SqlDatabaseCommand
            {
                Sql              = sql,
                Parameters       = parameters,
                ExpectedRowCount = 1
            });

            return(tx.CommitId);
        }
예제 #3
0
        public static Guid Execute(DocumentTransaction tx, DeleteCommand command)
        {
            // Note that last write wins can actually still produce a ConcurrencyException if the
            // row was already deleted, which would result in 0 resulting rows changed

            var sql        = new SqlBuilder();
            var parameters = new Parameters();

            if (tx.Store.Configuration.SoftDelete)
            {
                sql
                .Append($"update {tx.Store.Database.FormatTableNameAndEscape(command.Table.Name)}")
                .Append($"set {DocumentTable.IdColumn.Name} = @NewId")
                .Append($", {DocumentTable.LastOperationColumn.Name} = {(byte) Operation.Deleted}")
                .Append($"where {DocumentTable.IdColumn.Name} = @Id")
                .Append(!command.LastWriteWins, $"and {DocumentTable.EtagColumn.Name} = @ExpectedEtag");

                parameters.Add("@NewId", $"{command.Key}/{Guid.NewGuid()}", SqlTypeMap.Convert(DocumentTable.IdColumn).DbType, null);
            }
            else
            {
                sql
                .Append($"delete from {tx.Store.Database.FormatTableNameAndEscape(command.Table.Name)}")
                .Append($"where {DocumentTable.IdColumn.Name} = @Id")
                .Append(!command.LastWriteWins, $"and {DocumentTable.EtagColumn.Name} = @ExpectedEtag");
            }

            parameters.Add("@Id", command.Key, SqlTypeMap.Convert(DocumentTable.IdColumn).DbType, null);

            if (!command.LastWriteWins)
            {
                parameters.Add("@ExpectedEtag", command.ExpectedEtag, SqlTypeMap.Convert(DocumentTable.EtagColumn).DbType, null);
            }

            DocumentWriteCommand.Execute(tx, new SqlDatabaseCommand
            {
                Sql              = sql.ToString(),
                Parameters       = parameters,
                ExpectedRowCount = 1
            });

            return(tx.CommitId);
        }