public PreparedCommand PrepareUpdate(object document, UpdateOptions options = null)
        {
            options ??= UpdateOptions.Default;

            var mapping = mappings.Resolve(document.GetType());

            var updateStatements = mapping.WritableIndexedColumns().Select(c => $"[{c.ColumnName}] = @{c.ColumnName}").ToList();

            switch (mapping.JsonStorageFormat)
            {
            case JsonStorageFormat.TextOnly:
                updateStatements.Add($"[{JsonVariableName}] = @{JsonVariableName}");
                break;

            case JsonStorageFormat.CompressedOnly:
                updateStatements.Add($"[{JsonBlobVariableName}] = @{JsonBlobVariableName}");
                break;

            case JsonStorageFormat.MixedPreferCompressed:
                updateStatements.Add($"[{JsonVariableName}] = @{JsonVariableName}");
                updateStatements.Add($"[{JsonBlobVariableName}] = @{JsonBlobVariableName}");
                break;

            case JsonStorageFormat.MixedPreferText:
                updateStatements.Add($"[{JsonVariableName}] = @{JsonVariableName}");
                updateStatements.Add($"[{JsonBlobVariableName}] = @{JsonBlobVariableName}");
                break;

            case JsonStorageFormat.NoJson:
                // Nothing to set
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var updates = string.Join(", ", updateStatements);

            var statement = $"UPDATE [{configuration.GetSchemaNameOrDefault(mapping)}].[{mapping.TableName}] {options.Hint ?? ""} SET {updates} WHERE [{mapping.IdColumn.ColumnName}] = @{mapping.IdColumn.ColumnName}";

            var parameters = GetDocumentParameters(
                m => throw new Exception("Cannot update a document if it does not have an ID"),
                null,
                null,
                document,
                mapping
                );

            statement = AppendRelatedDocumentStatementsForUpdate(statement, parameters, mapping, document);
            return(new PreparedCommand(statement, parameters, RetriableOperation.Update, mapping, options.CommandTimeout));
        }
示例#2
0
        public DeleteQueryBuilderFixture()
        {
            query      = null;
            parameters = null;

            mappings = Substitute.For <IDocumentMapRegistry>();
            mappings.Resolve <object>().Returns(c => new EmptyMap());
            mappings.Resolve(Arg.Any <Type>()).Returns(c => new EmptyMap());

            queryExecutor = Substitute.For <IWriteQueryExecutor>();
            queryExecutor.ExecuteNonQuery(Arg.Any <PreparedCommand>()).Returns(info =>
            {
                query      = info.Arg <PreparedCommand>().Statement;
                parameters = info.Arg <PreparedCommand>().ParameterValues;
                return(1);
            });
        }