Skip to content

Commit

Permalink
Cosmos: Enable all sync operations
Browse files Browse the repository at this point in the history
Switch to REST API
  • Loading branch information
AndriySvyryd committed Sep 24, 2018
1 parent e467e09 commit 3bc37f1
Show file tree
Hide file tree
Showing 39 changed files with 2,296 additions and 1,171 deletions.
2 changes: 1 addition & 1 deletion src/EFCore.Cosmos.Sql/EFCore.Cosmos.Sql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="$(StyleCopAnalyzersPackageVersion)" PrivateAssets="All" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="$(MicrosoftAzureDocumentDBCorePackageVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
</ItemGroup>

</Project>
26 changes: 12 additions & 14 deletions src/EFCore.Cosmos.Sql/Extensions/CosmosSqlLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using Microsoft.Azure.Documents;
using Microsoft.EntityFrameworkCore.Cosmos.Sql.Storage.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;

Expand All @@ -34,20 +34,19 @@ public static class CosmosSqlLoggerExtensions
warningBehavior,
FormatParameters(sqlQuerySpec.Parameters),
Environment.NewLine,
sqlQuerySpec.QueryText);
sqlQuerySpec.Query);
}

private static string FormatParameters(SqlParameterCollection parameters)
private static string FormatParameters(IReadOnlyList<SqlParameter> parameters)
{
return parameters.Count == 0
? ""
: string.Join(", ", parameters.Select(p => FormatParameter(p)));
: string.Join(", ", parameters.Select(FormatParameter));
}

private static string FormatParameter(SqlParameter parameter)
{
var builder = new StringBuilder();
var clrType = parameter.Value?.GetType();
builder
.Append(parameter.Name)
.Append("=");
Expand All @@ -67,28 +66,27 @@ private static void FormatParameterValue(StringBuilder builder, object parameter

builder.Append('\'');

if (parameterValue.GetType() == typeof(DateTime))
if (parameterValue is DateTime dateTimeValue)
{
builder.Append(((DateTime)parameterValue).ToString("s"));
builder.Append(dateTimeValue.ToString("s"));
}
else if (parameterValue.GetType() == typeof(DateTimeOffset))
else if (parameterValue is DateTimeOffset dateTimeOffsetValue)
{
builder.Append(((DateTimeOffset)parameterValue).ToString("o"));
builder.Append(dateTimeOffsetValue.ToString("o"));
}
else if (parameterValue.GetType() == typeof(byte[]))
else if (parameterValue is byte[] binaryValue)
{
var buffer = (byte[])parameterValue;
builder.Append("0x");

for (var i = 0; i < buffer.Length; i++)
for (var i = 0; i < binaryValue.Length; i++)
{
if (i > 31)
{
builder.Append("...");
break;
}

builder.Append(buffer[i].ToString("X2", CultureInfo.InvariantCulture));
builder.Append(binaryValue[i].ToString("X2", CultureInfo.InvariantCulture));
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ public static IServiceCollection AddEntityFrameworkCosmosSql([NotNull] this ISer

var builder = new EntityFrameworkServicesBuilder(serviceCollection)
.TryAdd<IDatabaseProvider, DatabaseProvider<CosmosSqlDbOptionsExtension>>()
.TryAdd<IQueryContextFactory, CosmosSqlQueryContextFactory>()
.TryAdd<IDatabase, CosmosSqlDatabase>()
.TryAdd<IExecutionStrategyFactory, CosmosSqlExecutionStrategyFactory>()
.TryAdd<IDbContextTransactionManager, CosmosSqlTransactionManager>()
.TryAdd<IConventionSetBuilder, CosmosSqlConventionSetBuilder>()
.TryAdd<IDatabaseCreator, CosmosSqlDatabaseCreator>()
.TryAdd<IQueryContextFactory, CosmosSqlQueryContextFactory>()
.TryAdd<IEntityQueryModelVisitorFactory, CosmosSqlEntityQueryModelVisitorFactory>()
.TryAdd<IEntityQueryableExpressionVisitorFactory, CosmosSqlEntityQueryableExpressionVisitorFactory>()
.TryAdd<IMemberAccessBindingExpressionVisitorFactory, CosmosSqlMemberAccessBindingExpressionVisitorFactory>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.ComponentModel;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Cosmos.Sql.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Cosmos.Sql.Infrastructure
Expand All @@ -16,5 +21,53 @@ public CosmosSqlDbContextOptionsBuilder([NotNull] DbContextOptionsBuilder option
}

protected virtual DbContextOptionsBuilder OptionsBuilder { get; }

/// <summary>
/// Configures the context to use the provided <see cref="IExecutionStrategy" />.
/// </summary>
/// <param name="getExecutionStrategy"> A function that returns a new instance of an execution strategy. </param>
public virtual CosmosSqlDbContextOptionsBuilder ExecutionStrategy(
[NotNull] Func<ExecutionStrategyDependencies, IExecutionStrategy> getExecutionStrategy)
=> WithOption(e => e.WithExecutionStrategyFactory(Check.NotNull(getExecutionStrategy, nameof(getExecutionStrategy))));

/// <summary>
/// Sets an option by cloning the extension used to store the settings. This ensures the builder
/// does not modify options that are already in use elsewhere.
/// </summary>
/// <param name="setAction"> An action to set the option. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
protected virtual CosmosSqlDbContextOptionsBuilder WithOption([NotNull] Func<CosmosSqlDbOptionsExtension, CosmosSqlDbOptionsExtension> setAction)
{
((IDbContextOptionsBuilderInfrastructure)OptionsBuilder).AddOrUpdateExtension(
setAction(OptionsBuilder.Options.FindExtension<CosmosSqlDbOptionsExtension>() ?? new CosmosSqlDbOptionsExtension()));

return this;
}

#region Hidden System.Object members

/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns> A string that represents the current object. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString() => base.ToString();

/// <summary>
/// Determines whether the specified object is equal to the current object.
/// </summary>
/// <param name="obj"> The object to compare with the current object. </param>
/// <returns> true if the specified object is equal to the current object; otherwise, false. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => base.Equals(obj);

/// <summary>
/// Serves as the default hash function.
/// </summary>
/// <returns> A hash code for the current object. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => base.GetHashCode();

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System;
using System.Text;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Cosmos.Sql.Infrastructure.Internal
Expand All @@ -13,6 +15,7 @@ public class CosmosSqlDbOptionsExtension : IDbContextOptionsExtension
private Uri _serviceEndPoint;
private string _authKeyOrResourceToken;
private string _databaseName;
private Func<ExecutionStrategyDependencies, IExecutionStrategy> _executionStrategyFactory;
private string _logFragment;

public CosmosSqlDbOptionsExtension()
Expand All @@ -24,6 +27,7 @@ protected CosmosSqlDbOptionsExtension(CosmosSqlDbOptionsExtension copyFrom)
_serviceEndPoint = copyFrom._serviceEndPoint;
_authKeyOrResourceToken = copyFrom._authKeyOrResourceToken;
_databaseName = copyFrom._databaseName;
_executionStrategyFactory = copyFrom._executionStrategyFactory;
}

public virtual Uri ServiceEndPoint => _serviceEndPoint;
Expand Down Expand Up @@ -59,6 +63,28 @@ public virtual CosmosSqlDbOptionsExtension WithDatabaseName(string database)
return clone;
}

/// <summary>
/// A factory for creating the default <see cref="IExecutionStrategy" />, or <c>null</c> if none has been
/// configured.
/// </summary>
public virtual Func<ExecutionStrategyDependencies, IExecutionStrategy> ExecutionStrategyFactory => _executionStrategyFactory;

/// <summary>
/// Creates a new instance with all options the same as for this instance, but with the given option changed.
/// It is unusual to call this method directly. Instead use <see cref="DbContextOptionsBuilder" />.
/// </summary>
/// <param name="executionStrategyFactory"> The option to change. </param>
/// <returns> A new instance with the option changed. </returns>
public virtual CosmosSqlDbOptionsExtension WithExecutionStrategyFactory(
[CanBeNull] Func<ExecutionStrategyDependencies, IExecutionStrategy> executionStrategyFactory)
{
var clone = Clone();

clone._executionStrategyFactory = executionStrategyFactory;

return clone;
}

protected virtual CosmosSqlDbOptionsExtension Clone() => new CosmosSqlDbOptionsExtension(this);

public bool ApplyServices(IServiceCollection services)
Expand All @@ -85,7 +111,7 @@ public string LogFragment
{
var builder = new StringBuilder();

builder.Append("ServiceEndPoint=").Append(_serviceEndPoint.ToString()).Append(' ');
builder.Append("ServiceEndPoint=").Append(_serviceEndPoint).Append(' ');

builder.Append("Database=").Append(_databaseName).Append(' ');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.ValueGeneration.Internal;

Expand Down
25 changes: 25 additions & 0 deletions src/EFCore.Cosmos.Sql/Query/CosmosSqlQueryContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Cosmos.Sql.Storage.Internal;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.Internal;

namespace Microsoft.EntityFrameworkCore.Cosmos.Sql.Query
{
public class CosmosSqlQueryContext : QueryContext
{
public CosmosSqlQueryContext(
[NotNull] QueryContextDependencies dependencies,
[NotNull] Func<IQueryBuffer> queryBufferFactory,
[NotNull] CosmosClient cosmosClient)
: base(dependencies, queryBufferFactory)
{
CosmosClient = cosmosClient;
}

public CosmosClient CosmosClient { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Cosmos.Sql.Query.Expressions.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Sql.Query.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Sql.Storage.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors;
Expand All @@ -17,20 +16,17 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Sql.Query.ExpressionVisitors.Inte
public class CosmosSqlEntityQueryableExpressionVisitor : EntityQueryableExpressionVisitor
{
private readonly IModel _model;
private readonly CosmosClient _cosmosClient;
private readonly IQuerySource _querySource;
private readonly IEntityMaterializerSource _entityMaterializerSource;

public CosmosSqlEntityQueryableExpressionVisitor(
IModel model,
CosmosClient cosmosClient,
IEntityMaterializerSource entityMaterializerSource,
CosmosSqlQueryModelVisitor cosmosSqlQueryModelVisitor,
IQuerySource querySource)
: base(cosmosSqlQueryModelVisitor)
{
_model = model;
_cosmosClient = cosmosClient;
_querySource = querySource;
_entityMaterializerSource = entityMaterializerSource;
}
Expand All @@ -46,8 +42,7 @@ protected override Expression VisitEntityQueryable([NotNull] Type elementType)
new DocumentQueryExpression(
QueryModelVisitor.QueryCompilationContext.IsAsyncQuery,
entityType.CosmosSql().CollectionName,
new SelectExpression(entityType, _querySource),
_cosmosClient),
new SelectExpression(entityType, _querySource)),
new EntityShaper(entityType,
trackingQuery: QueryModelVisitor.QueryCompilationContext.IsTrackingQuery
&& !entityType.IsQueryType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,20 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Sql.Query.ExpressionVisitors.Inte
public class CosmosSqlEntityQueryableExpressionVisitorFactory : IEntityQueryableExpressionVisitorFactory
{
private readonly IModel _model;
private readonly CosmosClient _cosmosClient;
private readonly IEntityMaterializerSource _entityMaterializerSource;

public CosmosSqlEntityQueryableExpressionVisitorFactory(
IModel model,
CosmosClient cosmosClient,
IEntityMaterializerSource entityMaterializerSource)
{
_model = model;
_cosmosClient = cosmosClient;
_entityMaterializerSource = entityMaterializerSource;
}

public ExpressionVisitor Create(EntityQueryModelVisitor entityQueryModelVisitor, IQuerySource querySource)
{
return new CosmosSqlEntityQueryableExpressionVisitor(
_model,
_cosmosClient,
_entityMaterializerSource,
(CosmosSqlQueryModelVisitor)entityQueryModelVisitor,
querySource);
Expand Down

0 comments on commit 3bc37f1

Please sign in to comment.