internal static string ReadAndUpdateTableEntity(ITableEntity entity, ODataEntry entry, OperationContext ctx)
        {
            entity.ETag = entry.ETag;


            Dictionary <string, EntityProperty> entityProperties = new Dictionary <string, EntityProperty>();

            foreach (ODataProperty prop in entry.Properties)
            {
                if (prop.Name == "PartitionKey")
                {
                    entity.PartitionKey = (string)prop.Value;
                }
                else if (prop.Name == "RowKey")
                {
                    entity.RowKey = (string)prop.Value;
                }
                else if (prop.Name == "Timestamp")
                {
                    entity.Timestamp = (DateTime)prop.Value;
                }
                else
                {
                    entityProperties.Add(prop.Name, EntityProperty.CreateEntityPropertyFromObject(prop.Value));
                }
            }


            entity.ReadEntity(entityProperties, ctx);



            return(entry.ETag);
        }
        // returns etag
        internal static string ReadAndUpdateTableEntity(ITableEntity entity, ODataEntry entry, EntityReadFlags flags, OperationContext ctx)
        {
            if ((flags & EntityReadFlags.Etag) > 0)
            {
                entity.ETag = entry.ETag;
            }

            Dictionary <string, EntityProperty> entityProperties = (flags & EntityReadFlags.Properties) > 0 ? new Dictionary <string, EntityProperty>() : null;

            if (flags > 0)
            {
                foreach (ODataProperty prop in entry.Properties)
                {
                    if (prop.Name == TableConstants.PartitionKey)
                    {
                        if ((flags & EntityReadFlags.PartitionKey) == 0)
                        {
                            continue;
                        }

                        entity.PartitionKey = (string)prop.Value;
                    }
                    else if (prop.Name == TableConstants.RowKey)
                    {
                        if ((flags & EntityReadFlags.RowKey) == 0)
                        {
                            continue;
                        }

                        entity.RowKey = (string)prop.Value;
                    }
                    else if (prop.Name == TableConstants.Timestamp)
                    {
                        if ((flags & EntityReadFlags.Timestamp) == 0)
                        {
                            continue;
                        }

                        entity.Timestamp = (DateTime)prop.Value;
                    }
                    else if ((flags & EntityReadFlags.Properties) > 0)
                    {
                        entityProperties.Add(prop.Name, EntityProperty.CreateEntityPropertyFromObject(prop.Value));
                    }
                }

                if ((flags & EntityReadFlags.Properties) > 0)
                {
                    entity.ReadEntity(entityProperties, ctx);
                }
            }

            return(entry.ETag);
        }
Beispiel #3
0
        internal static TElement ResolveEntityByType <TElement>(string partitionKey, string rowKey, DateTimeOffset timestamp, IDictionary <string, EntityProperty> properties, string etag)
        {
            ITableEntity obj = (ITableEntity)InstantiateEntityFromType(typeof(TElement));

            obj.PartitionKey = partitionKey;
            obj.RowKey       = rowKey;
            obj.Timestamp    = timestamp;
            obj.ReadEntity(properties, null);
            obj.ETag = etag;
            return((TElement)obj);
        }
        internal static TElement ResolveEntityByType <TElement>(string partitionKey, string rowKey, DateTimeOffset timestamp, IDictionary <string, EntityProperty> properties, string etag) where TElement : ITableEntity
        {
            ITableEntity entity = (ITableEntity)InstantiateEntityFromGenericType <TElement>();

            entity.PartitionKey = partitionKey;
            entity.RowKey       = rowKey;
            entity.Timestamp    = timestamp;
            entity.ReadEntity(properties, null);
            entity.ETag = etag;

            return((TElement)entity);
        }
        internal static string ReadAndUpdateTableEntity(ITableEntity entity, string etag, Dictionary <string, object> props, EntityReadFlags flags, OperationContext ctx)
        {
            if ((flags & EntityReadFlags.Etag) > (EntityReadFlags)0)
            {
                entity.ETag = etag;
            }
            Dictionary <string, EntityProperty> dictionary = ((flags & EntityReadFlags.Properties) > (EntityReadFlags)0) ? new Dictionary <string, EntityProperty>() : null;

            if (flags > (EntityReadFlags)0)
            {
                foreach (KeyValuePair <string, object> prop in props)
                {
                    if (prop.Key == "PartitionKey")
                    {
                        if ((flags & EntityReadFlags.PartitionKey) != 0)
                        {
                            entity.PartitionKey = (string)prop.Value;
                        }
                    }
                    else if (prop.Key == "RowKey")
                    {
                        if ((flags & EntityReadFlags.RowKey) != 0)
                        {
                            entity.RowKey = (string)prop.Value;
                        }
                    }
                    else if (prop.Key == "Timestamp")
                    {
                        if ((flags & EntityReadFlags.Timestamp) != 0)
                        {
                            entity.Timestamp = (DateTime)prop.Value;
                        }
                    }
                    else if ((flags & EntityReadFlags.Properties) > (EntityReadFlags)0)
                    {
                        dictionary.Add(prop.Key, EntityProperty.CreateEntityPropertyFromObject(prop.Value));
                    }
                }
                if ((flags & EntityReadFlags.Properties) > (EntityReadFlags)0)
                {
                    entity.ReadEntity(dictionary, ctx);
                }
            }
            return(etag);
        }
        internal static void ReadAndUpdateTableEntityWithEdmTypeResolver(ITableEntity entity, Dictionary <string, string> entityAttributes, EntityReadFlags flags, Func <string, string, string, string, EdmType> propertyResolver, OperationContext ctx)
        {
            Dictionary <string, EntityProperty> entityProperties           = (flags & EntityReadFlags.Properties) > 0 ? new Dictionary <string, EntityProperty>() : null;
            Dictionary <string, EdmType>        propertyResolverDictionary = null;

            // Try to add the dictionary to the cache only if it is not a DynamicTableEntity. If DisablePropertyResolverCache is true, then just use reflection and generate dictionaries for each entity.
            if (entity.GetType() != typeof(DynamicTableEntity))
            {
#if WINDOWS_DESKTOP && !WINDOWS_PHONE
                if (!TableEntity.DisablePropertyResolverCache)
                {
                    propertyResolverDictionary = TableEntity.PropertyResolverCache.GetOrAdd(entity.GetType(), TableOperationHttpResponseParsers.CreatePropertyResolverDictionary);
                }
                else
                {
                    Logger.LogVerbose(ctx, SR.PropertyResolverCacheDisabled);
                    propertyResolverDictionary = TableOperationHttpResponseParsers.CreatePropertyResolverDictionary(entity.GetType());
                }
#else
                propertyResolverDictionary = TableOperationHttpResponseParsers.CreatePropertyResolverDictionary(entity.GetType());
#endif
            }

            if (flags > 0)
            {
                foreach (KeyValuePair <string, string> prop in entityAttributes)
                {
                    if (prop.Key == TableConstants.PartitionKey)
                    {
                        entity.PartitionKey = (string)prop.Value;
                    }
                    else if (prop.Key == TableConstants.RowKey)
                    {
                        entity.RowKey = (string)prop.Value;
                    }
                    else if (prop.Key == TableConstants.Timestamp)
                    {
                        if ((flags & EntityReadFlags.Timestamp) == 0)
                        {
                            continue;
                        }

                        entity.Timestamp = DateTime.Parse(prop.Value, CultureInfo.InvariantCulture);
                    }
                    else if ((flags & EntityReadFlags.Properties) > 0)
                    {
                        if (propertyResolver != null)
                        {
                            Logger.LogVerbose(ctx, SR.UsingUserProvidedPropertyResolver);
                            try
                            {
                                EdmType type = propertyResolver(entity.PartitionKey, entity.RowKey, prop.Key, prop.Value);
                                Logger.LogVerbose(ctx, SR.AttemptedEdmTypeForTheProperty, prop.Key, type.GetType().ToString());
                                try
                                {
                                    entityProperties.Add(prop.Key, EntityProperty.CreateEntityPropertyFromObject(prop.Value, type.GetType()));
                                }
                                catch (FormatException ex)
                                {
                                    throw new StorageException(string.Format(CultureInfo.InvariantCulture, SR.FailParseProperty, prop.Key, prop.Value, type.ToString()), ex)
                                          {
                                              IsRetryable = false
                                          };
                                }
                            }
                            catch (StorageException)
                            {
                                throw;
                            }
                            catch (Exception ex)
                            {
                                throw new StorageException(SR.PropertyResolverThrewError, ex)
                                      {
                                          IsRetryable = false
                                      };
                            }
                        }
                        else if (entity.GetType() != typeof(DynamicTableEntity))
                        {
                            EdmType edmType;
                            Logger.LogVerbose(ctx, SR.UsingDefaultPropertyResolver);

                            if (propertyResolverDictionary != null)
                            {
                                propertyResolverDictionary.TryGetValue(prop.Key, out edmType);
                                Logger.LogVerbose(ctx, SR.AttemptedEdmTypeForTheProperty, prop.Key, edmType);
                                entityProperties.Add(prop.Key, EntityProperty.CreateEntityPropertyFromObject(prop.Value, edmType));
                            }
                        }
                        else
                        {
                            Logger.LogVerbose(ctx, SR.NoPropertyResolverAvailable);
                            entityProperties.Add(prop.Key, EntityProperty.CreateEntityPropertyFromObject(prop.Value, typeof(string)));
                        }
                    }
                }

                if ((flags & EntityReadFlags.Properties) > 0)
                {
                    entity.ReadEntity(entityProperties, ctx);
                }
            }
        }
Beispiel #7
0
        internal ExecutionInfo Bind()
        {
            ExecutionInfo retVal = new ExecutionInfo();

            // IQueryable impl
            if (this.Expression != null)
            {
                Dictionary <Expression, Expression> normalizerRewrites = new Dictionary <Expression, Expression>(ReferenceEqualityComparer <Expression> .Instance);

                // Step 1. Evaluate any local evaluatable expressions ( lambdas etc)
                Expression partialEvaluatedExpression = Evaluator.PartialEval(this.Expression);

                // Step 2. Normalize expression, replace String Comparisons etc.
                Expression normalizedExpression = ExpressionNormalizer.Normalize(partialEvaluatedExpression, normalizerRewrites);

                // Step 3. Bind Expression, Analyze predicates and create query option expressions. End result is a single ResourceSetExpression
                Expression boundExpression = ResourceBinder.Bind(normalizedExpression);

                // Step 4. Parse the Bound expression into sub components, i.e. take count, filter, select columns, request options, opcontext, etc.
                ExpressionParser parser = new ExpressionParser();
                parser.Translate(boundExpression);

                // Step 5. Store query components & params
                this.TakeCount          = parser.TakeCount;
                this.FilterString       = parser.FilterString;
                this.SelectColumns      = parser.SelectColumns;
                retVal.RequestOptions   = parser.RequestOptions;
                retVal.OperationContext = parser.OperationContext;

                // Step 6. If projection & no resolver then generate a resolver to perform the projection
                if (parser.Resolver == null)
                {
                    if (parser.Projection != null && parser.Projection.Selector != ProjectionQueryOptionExpression.DefaultLambda)
                    {
                        Type intermediateType = parser.Projection.Selector.Parameters[0].Type;

                        // Convert Expression to take type object as input to allow for direct invocation.
                        ParameterExpression paramExpr = Expression.Parameter(typeof(object));

                        Func <object, TElement> projectorFunc = Expression.Lambda <Func <object, TElement> >(
                            Expression.Invoke(parser.Projection.Selector, Expression.Convert(paramExpr, intermediateType)), paramExpr).Compile();

                        // Generate a resolver to do the projection.
                        retVal.Resolver = (pk, rk, ts, props, etag) =>
                        {
                            // Parse to intermediate type
                            ITableEntity intermediateObject = (ITableEntity)EntityUtilities.InstantiateEntityFromType(intermediateType);
                            intermediateObject.PartitionKey = pk;
                            intermediateObject.RowKey       = rk;
                            intermediateObject.Timestamp    = ts;
                            intermediateObject.ReadEntity(props, parser.OperationContext);
                            intermediateObject.ETag = etag;

                            // Invoke lambda expression
                            return(projectorFunc(intermediateObject));
                        };
                    }
                    else
                    {
                        // No op - No resolver or projection specified.
                    }
                }
                else
                {
                    retVal.Resolver = (EntityResolver <TElement>)parser.Resolver.Value;
                }
            }

            retVal.RequestOptions   = TableRequestOptions.ApplyDefaults(retVal.RequestOptions, this.queryProvider.Table.ServiceClient);
            retVal.OperationContext = retVal.OperationContext ?? new OperationContext();
            return(retVal);
        }
        internal static void ReadAndUpdateTableEntityWithEdmTypeResolver(ITableEntity entity, Dictionary <string, string> entityAttributes, EntityReadFlags flags, Func <string, string, string, string, EdmType> propertyResolver, OperationContext ctx)
        {
            Dictionary <string, EntityProperty> dictionary  = ((flags & EntityReadFlags.Properties) > (EntityReadFlags)0) ? new Dictionary <string, EntityProperty>() : null;
            Dictionary <string, EdmType>        dictionary2 = null;

            if (entity.GetType() != typeof(DynamicTableEntity))
            {
                if (!TableEntity.DisablePropertyResolverCache)
                {
                    dictionary2 = TableEntity.PropertyResolverCache.GetOrAdd(entity.GetType(), CreatePropertyResolverDictionary);
                }
                else
                {
                    Logger.LogVerbose(ctx, "Property resolver cache is disabled.");
                    dictionary2 = CreatePropertyResolverDictionary(entity.GetType());
                }
            }
            if (flags > (EntityReadFlags)0)
            {
                foreach (KeyValuePair <string, string> entityAttribute in entityAttributes)
                {
                    if (entityAttribute.Key == "PartitionKey")
                    {
                        entity.PartitionKey = entityAttribute.Value;
                    }
                    else if (entityAttribute.Key == "RowKey")
                    {
                        entity.RowKey = entityAttribute.Value;
                    }
                    else if (entityAttribute.Key == "Timestamp")
                    {
                        if ((flags & EntityReadFlags.Timestamp) != 0)
                        {
                            entity.Timestamp = DateTime.Parse(entityAttribute.Value, CultureInfo.InvariantCulture);
                        }
                    }
                    else if ((flags & EntityReadFlags.Properties) > (EntityReadFlags)0)
                    {
                        if (propertyResolver != null)
                        {
                            Logger.LogVerbose(ctx, "Using the property resolver provided via TableRequestOptions to deserialize the entity.");
                            try
                            {
                                EdmType edmType = propertyResolver(entity.PartitionKey, entity.RowKey, entityAttribute.Key, entityAttribute.Value);
                                Logger.LogVerbose(ctx, "Attempting to deserialize '{0}' as type '{1}'", entityAttribute.Key, edmType.GetType().ToString());
                                try
                                {
                                    dictionary.Add(entityAttribute.Key, EntityProperty.CreateEntityPropertyFromObject(entityAttribute.Value, edmType.GetType()));
                                }
                                catch (FormatException innerException)
                                {
                                    throw new StorageException(string.Format(CultureInfo.InvariantCulture, "Failed to parse property '{0}' with value '{1}' as type '{2}'", entityAttribute.Key, entityAttribute.Value, edmType.ToString()), innerException)
                                          {
                                              IsRetryable = false
                                          };
                                }
                            }
                            catch (StorageException)
                            {
                                throw;
                            }
                            catch (Exception innerException2)
                            {
                                throw new StorageException("The custom property resolver delegate threw an exception. Check the inner exception for more details.", innerException2)
                                      {
                                          IsRetryable = false
                                      };
                            }
                        }
                        else if (entity.GetType() != typeof(DynamicTableEntity))
                        {
                            Logger.LogVerbose(ctx, "Using the default property resolver to deserialize the entity.");
                            if (dictionary2 != null)
                            {
                                dictionary2.TryGetValue(entityAttribute.Key, out EdmType value);
                                Logger.LogVerbose(ctx, "Attempting to deserialize '{0}' as type '{1}'", entityAttribute.Key, value);
                                dictionary.Add(entityAttribute.Key, EntityProperty.CreateEntityPropertyFromObject(entityAttribute.Value, value));
                            }
                        }
                        else
                        {
                            Logger.LogVerbose(ctx, "No property resolver available. Deserializing the entity properties as strings.");
                            dictionary.Add(entityAttribute.Key, EntityProperty.CreateEntityPropertyFromObject(entityAttribute.Value, typeof(string)));
                        }
                    }
                }
                if ((flags & EntityReadFlags.Properties) > (EntityReadFlags)0)
                {
                    entity.ReadEntity(dictionary, ctx);
                }
            }
        }