private static TableResult GetTableResultFromResponse(TableOperation operation, ResourceResponse <Document> response, OperationContext context, TableRequestOptions options, List <string> selectColumns, string sessionToken)
        {
            TableResult tableResult = new TableResult();

            tableResult.Etag           = EtagHelper.ConvertFromBackEndETagFormat(response.ResponseHeaders["ETag"]);
            tableResult.HttpStatusCode = (int)response.StatusCode;
            tableResult.SessionToken   = sessionToken;
            tableResult.RequestCharge  = response.RequestCharge;
            if (operation.Entity != null && !string.IsNullOrEmpty(tableResult.Etag))
            {
                operation.Entity.ETag = tableResult.Etag;
            }
            if (operation.OperationType == TableOperationType.InsertOrReplace || operation.OperationType == TableOperationType.Replace || !operation.EchoContent)
            {
                tableResult.HttpStatusCode = 204;
            }
            if (operation.OperationType != TableOperationType.Retrieve)
            {
                if (operation.OperationType != TableOperationType.Delete)
                {
                    tableResult.Result = operation.Entity;
                    ITableEntity tableEntity = tableResult.Result as ITableEntity;
                    if (tableEntity != null)
                    {
                        if (tableResult.Etag != null)
                        {
                            tableEntity.ETag = tableResult.Etag;
                        }
                        tableEntity.Timestamp = response.Resource.Timestamp;
                    }
                }
            }
            else if (response.Resource != null)
            {
                if (operation.RetrieveResolver == null)
                {
                    tableResult.Result = EntityHelpers.GetEntityFromResourceStream(response.ResponseStream, context, operation.SelectColumns);
                }
                else
                {
                    EntityHelpers.GetPropertiesFromResourceStream(response.ResponseStream, operation.SelectColumns, out IDictionary <string, EntityProperty> entityProperties, out IDictionary <string, EntityProperty> documentDBProperties);
                    if (options.ProjectSystemProperties.Value)
                    {
                        EdmConverter.ValidateDocumentDBProperties(documentDBProperties);
                        EntityProperty entityProperty  = documentDBProperties["$pk"];
                        EntityProperty entityProperty2 = documentDBProperties["$id"];
                        EntityProperty entityProperty3 = documentDBProperties["_etag"];
                        EntityProperty entityProperty4 = documentDBProperties["_ts"];
                        string         arg             = EtagHelper.ConvertFromBackEndETagFormat(entityProperty3.StringValue);
                        DateTime       dateTime        = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(entityProperty4.DoubleValue.Value);
                        tableResult.Result = operation.RetrieveResolver(entityProperty.StringValue, entityProperty2.StringValue, dateTime, entityProperties, arg);
                    }
                    else
                    {
                        tableResult.Result = operation.RetrieveResolver(null, null, default(DateTimeOffset), entityProperties, null);
                    }
                }
            }
            return(tableResult);
        }
Esempio n. 2
0
        private TableResult GetTableResultFromDocument(TableOperation operation, Document response, OperationContext context)
        {
            TableResult result = new TableResult();

            result.Etag           = response.ETag;
            result.HttpStatusCode = GetSuccessStatusCodeFromOperationType(operation.OperationType);

            if (operation.OperationType != TableOperationType.Retrieve)
            {
                operation.Entity.ETag = response.ETag;
                result.Result         = operation.Entity;
            }
            else
            {
                if (operation.RetrieveResolver != null)
                {
                    result.Result = operation.RetrieveResolver(response.GetPropertyValue <string>(StellarConstants.PartitionKeyPropertyName),
                                                               response.Id, response.Timestamp, EntityHelpers.GetEntityFromDocument(response, context).WriteEntity(context),
                                                               response.ETag);
                }
                else
                {
                    result.Result = EntityHelpers.GetEntityFromDocument(response, context);
                }
            }

            return(result);
        }
        private static TableResult GetTableResultFromDocument(TableOperation operation, Document response, OperationContext context, TableRequestOptions requestOptions, string sessionToken, double requestCharge)
        {
            var responseETag = EtagHelper.ConvertFromBackEndETagFormat(response.ETag);

            response.SetPropertyValue("_etag", responseETag);
            TableResult tableResult = new TableResult();

            tableResult.Etag           = response.ETag;
            tableResult.HttpStatusCode = GetSuccessStatusCodeFromOperationType(operation.OperationType);
            tableResult.SessionToken   = sessionToken;
            tableResult.RequestCharge  = requestCharge;
            if (operation.OperationType != TableOperationType.Retrieve)
            {
                operation.Entity.ETag = response.ETag;
                tableResult.Result    = operation.Entity;
            }
            else if (operation.RetrieveResolver != null)
            {
                tableResult.Result = operation.RetrieveResolver(response.GetPropertyValue <string>("$pk"), response.Id, response.Timestamp, EntityTranslator.GetEntityPropertiesFromDocument(response, operation.SelectColumns), response.ETag);
            }
            else
            {
                tableResult.Result = EntityHelpers.GetEntityFromDocument(response, context, operation.SelectColumns);
            }
            return(tableResult);
        }
            public TableResult Execute(TableOperation operation)
            {
                TableOperationType     operationType = operation.OperationType;
                ITableEntity           entity        = operation.Entity;
                Tuple <string, string> key;
                IDictionary <string, EntityProperty> writeProperties;

                if (operation.OperationType != TableOperationType.Retrieve)
                {
                    key             = new Tuple <string, string>(entity.PartitionKey, entity.RowKey);
                    writeProperties = entity.WriteEntity(operationContext: null);
                }
                else
                {
                    key             = new Tuple <string, string>(operation.RetrievePartitionKey(), operation.RetrieveRowKey());
                    writeProperties = null;
                }

                switch (operation.OperationType)
                {
                case TableOperationType.Retrieve:
                    TableItem item = _entities[key];

                    // Func<string, string, DateTimeOffset, IDictionary<string, EntityProperty>, string, object>
                    var resolver = operation.RetrieveResolver();

                    return(new TableResult
                    {
                        Result = resolver(operation.RetrievePartitionKey(),
                                          operation.RetrieveRowKey(), item.Timestamp, item.CloneProperties(), item.ETag)
                    });

                case TableOperationType.Insert:
                    if (!_entities.TryAdd(key, new TableItem(writeProperties)))
                    {
                        throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                          "Entity PK='{0}',RK='{1}' already exists.", entity.PartitionKey, entity.RowKey));
                    }
                    return(new TableResult());

                case TableOperationType.Replace:
                    if (entity.ETag == null)
                    {
                        throw new InvalidOperationException("Replace requires an ETag.");
                    }
                    else if (!_entities.TryUpdate(key, new TableItem(writeProperties), new TableItem(entity.ETag)))
                    {
                        if (entity.ETag == "*")
                        {
                            throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                              "Entity PK='{0}',RK='{1}' does not exist.", entity.PartitionKey, entity.RowKey));
                        }
                        else
                        {
                            throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                              "Entity PK='{0}',RK='{1}' does not match eTag '{2}'.", entity.PartitionKey,
                                                                              entity.RowKey, entity.ETag));
                        }
                    }
                    return(new TableResult());

                default:
                    throw new InvalidOperationException(
                              "Unsupported operation type " + operationType.ToString());
                }
            }