Skip to content

Commit

Permalink
Updated per code review comments
Browse files Browse the repository at this point in the history
Also added message for split query, as discussed in triage.
  • Loading branch information
ajcvickers committed Aug 17, 2020
1 parent b6d1cd1 commit 0334a23
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 137 deletions.
8 changes: 5 additions & 3 deletions src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/EFCore.Cosmos/Properties/CosmosStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<value>Cosmos-specific methods can only be used when the context is using the Cosmos provider.</value>
</data>
<data name="NoReadItemQueryString" xml:space="preserve">
<value>There is no string-based representation of this query.</value>
<value>There is no string-based representation of this query as it's executed using 'ReadItemQueryAsync({resourceId}, {partitionKey})'.</value>
</data>
<data name="DuplicateDiscriminatorValue" xml:space="preserve">
<value>The discriminator value for '{entityType1}' is '{discriminatorValue}' which is the same for '{entityType2}'. Every concrete entity type mapped to the container '{container}' needs to have a unique discriminator value.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,110 @@ public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToke
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public string ToQueryString()
=> CosmosStrings.NoReadItemQueryString;
{
TryGetResourceId(out var resourceId);
TryGetPartitionId(out var partitionKey);
return CosmosStrings.NoReadItemQueryString(resourceId, partitionKey);
}

private bool TryGetPartitionId(out string partitionKey)
{
partitionKey = null;

var partitionKeyPropertyName = _readItemExpression.EntityType.GetPartitionKeyPropertyName();
if (partitionKeyPropertyName == null)
{
return true;
}

var partitionKeyProperty = _readItemExpression.EntityType.FindProperty(partitionKeyPropertyName);

if (TryGetParameterValue(partitionKeyProperty, out var value))
{
partitionKey = GetString(partitionKeyProperty, value);

return !string.IsNullOrEmpty(partitionKey);
}

return false;
}

private bool TryGetResourceId(out string resourceId)
{
var idProperty = _readItemExpression.EntityType.GetProperties()
.FirstOrDefault(p => p.GetJsonPropertyName() == StoreKeyConvention.IdPropertyJsonName);

if (TryGetParameterValue(idProperty, out var value))
{
resourceId = GetString(idProperty, value);

if (string.IsNullOrEmpty(resourceId))
{
throw new InvalidOperationException(CosmosStrings.InvalidResourceId);
}

return true;
}

if (TryGenerateIdFromKeys(idProperty, out var generatedValue))
{
resourceId = GetString(idProperty, generatedValue);

return true;
}

resourceId = null;
return false;
}

private bool TryGetParameterValue(IProperty property, out object value)
{
value = null;
return _readItemExpression.PropertyParameters.TryGetValue(property, out var parameterName)
&& _cosmosQueryContext.ParameterValues.TryGetValue(parameterName, out value);
}

private static string GetString(IProperty property, object value)
{
var converter = property.GetTypeMapping().Converter;

return converter is null
? (string)value
: (string)converter.ConvertToProvider(value);
}

private bool TryGenerateIdFromKeys(IProperty idProperty, out object value)
{
var entityEntry = Activator.CreateInstance(_readItemExpression.EntityType.ClrType);

#pragma warning disable EF1001 // Internal EF Core API usage.
var internalEntityEntry = new InternalEntityEntryFactory().Create(
_cosmosQueryContext.Context.GetDependencies().StateManager, _readItemExpression.EntityType, entityEntry);
#pragma warning restore EF1001 // Internal EF Core API usage.

foreach (var keyProperty in _readItemExpression.EntityType.FindPrimaryKey().Properties)
{
var property = _readItemExpression.EntityType.FindProperty(keyProperty.Name);

if (TryGetParameterValue(property, out var parameterValue))
{
#pragma warning disable EF1001 // Internal EF Core API usage.
internalEntityEntry[property] = parameterValue;
#pragma warning restore EF1001 // Internal EF Core API usage.
}
}

#pragma warning disable EF1001 // Internal EF Core API usage.
internalEntityEntry.SetEntityState(EntityState.Added);

value = internalEntityEntry[idProperty];

internalEntityEntry.SetEntityState(EntityState.Detached);
#pragma warning restore EF1001 // Internal EF Core API usage.

return value != null;
}


private sealed class Enumerator : IEnumerator<T>, IAsyncEnumerator<T>
{
Expand All @@ -69,6 +172,7 @@ private sealed class Enumerator : IEnumerator<T>, IAsyncEnumerator<T>
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
private readonly ReadItemQueryingEnumerable<T> _readItemEnumerable;
private readonly CancellationToken _cancellationToken;

private JObject _item;
Expand All @@ -82,6 +186,7 @@ public Enumerator(ReadItemQueryingEnumerable<T> readItemEnumerable, Cancellation
_contextType = readItemEnumerable._contextType;
_queryLogger = readItemEnumerable._queryLogger;
_standAloneStateManager = readItemEnumerable._standAloneStateManager;
_readItemEnumerable = readItemEnumerable;
_cancellationToken = cancellationToken;
}

Expand All @@ -97,12 +202,12 @@ public bool MoveNext()
{
if (!_hasExecuted)
{
if (!TryGetResourceId(out var resourceId))
if (!_readItemEnumerable.TryGetResourceId(out var resourceId))
{
throw new InvalidOperationException(CosmosStrings.ResourceIdMissing);
}

if (!TryGetPartitionId(out var partitionKey))
if (!_readItemEnumerable.TryGetPartitionId(out var partitionKey))
{
throw new InvalidOperationException(CosmosStrings.ParitionKeyMissing);
}
Expand Down Expand Up @@ -137,12 +242,12 @@ public async ValueTask<bool> MoveNextAsync()
if (!_hasExecuted)
{

if (!TryGetResourceId(out var resourceId))
if (!_readItemEnumerable.TryGetResourceId(out var resourceId))
{
throw new InvalidOperationException(CosmosStrings.ResourceIdMissing);
}

if (!TryGetPartitionId(out var partitionKey))
if (!_readItemEnumerable.TryGetPartitionId(out var partitionKey))
{
throw new InvalidOperationException(CosmosStrings.ParitionKeyMissing);
}
Expand Down Expand Up @@ -200,104 +305,6 @@ private bool ShapeResult()

return hasNext;
}

private bool TryGetPartitionId(out string partitionKey)
{
partitionKey = null;

var partitionKeyPropertyName = _readItemExpression.EntityType.GetPartitionKeyPropertyName();
if (partitionKeyPropertyName == null)
{
return true;
}

var partitionKeyProperty = _readItemExpression.EntityType.FindProperty(partitionKeyPropertyName);

if (TryGetParameterValue(partitionKeyProperty, out var value))
{
partitionKey = GetString(partitionKeyProperty, value);

return !string.IsNullOrEmpty(partitionKey);
}

return false;
}

private bool TryGetResourceId(out string resourceId)
{
var idProperty = _readItemExpression.EntityType.GetProperties()
.FirstOrDefault(p => p.GetJsonPropertyName() == StoreKeyConvention.IdPropertyJsonName);

if (TryGetParameterValue(idProperty, out var value))
{
resourceId = GetString(idProperty, value);

if (string.IsNullOrEmpty(resourceId))
{
throw new InvalidOperationException(CosmosStrings.InvalidResourceId);
}

return true;
}

if (TryGenerateIdFromKeys(idProperty, out var generatedValue))
{
resourceId = GetString(idProperty, generatedValue);

return true;
}

resourceId = null;
return false;
}

private bool TryGenerateIdFromKeys(IProperty idProperty, out object value)
{
var entityEntry = Activator.CreateInstance(_readItemExpression.EntityType.ClrType);

#pragma warning disable EF1001 // Internal EF Core API usage.
var internalEntityEntry = new InternalEntityEntryFactory().Create(
_cosmosQueryContext.Context.GetDependencies().StateManager, _readItemExpression.EntityType, entityEntry);
#pragma warning restore EF1001 // Internal EF Core API usage.

foreach (var keyProperty in _readItemExpression.EntityType.FindPrimaryKey().Properties)
{
var property = _readItemExpression.EntityType.FindProperty(keyProperty.Name);

if (TryGetParameterValue(property, out var parameterValue))
{
#pragma warning disable EF1001 // Internal EF Core API usage.
internalEntityEntry[property] = parameterValue;
#pragma warning restore EF1001 // Internal EF Core API usage.
}
}

#pragma warning disable EF1001 // Internal EF Core API usage.
internalEntityEntry.SetEntityState(EntityState.Added);

value = internalEntityEntry[idProperty];

internalEntityEntry.SetEntityState(EntityState.Detached);
#pragma warning restore EF1001 // Internal EF Core API usage.

return value != null;
}

private bool TryGetParameterValue(IProperty property, out object value)
{
value = null;
return _readItemExpression.PropertyParameters.TryGetValue(property, out var parameterName)
&& _cosmosQueryContext.ParameterValues.TryGetValue(parameterName, out value);
}

private static string GetString(IProperty property, object value)
{
var converter = property.GetTypeMapping().Converter;

return converter is null
? (string)value
: (string)converter.ConvertToProvider(value);
}
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 30 additions & 27 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -123,6 +123,9 @@
<data name="NoDbCommand" xml:space="preserve">
<value>Cannot create a 'DbCommand' for a non-relational query.</value>
</data>
<data name="SplitQueryString" xml:space="preserve">
<value>This LINQ query is being executed in split-query mode. The SQL shown is for the first query to be executed. Additional queries may also be executed depending on the results of the first query.</value>
</data>
<data name="CannotChangeWhenOpen" xml:space="preserve">
<value>The 'DbConnection' is currently in use. The connection can only be changed when the existing connection is not being used.</value>
</data>
Expand Down
Loading

0 comments on commit 0334a23

Please sign in to comment.