Esempio n. 1
0
        public void InsertOrReplace(AzureEntity entity)
        {
            Validate
            .Begin()
            .ParameterIsNotNull(entity, "entity")
            .Check();

            m_proxy.InsertOrReplaceEntity(this.TableName, entity);
        }
Esempio n. 2
0
        public void Delete(AzureEntity entity)
        {
            Validate
            .Begin()
            .ParameterIsNotNull(entity, "entity")
            .Check();

            Delete(entity.PartitionKey, entity.RowKey);
        }
Esempio n. 3
0
        public void Update(AzureEntity entity)
        {
            Validate
            .Begin()
            .ParameterIsNotNull(entity, "entity")
            .Check();

            m_proxy.UpdateEntity(this.TableName, entity);
        }
Esempio n. 4
0
        internal IEnumerable <AzureEntity> GetEntities(string tableName, string partitionKey, int maxRows)
        {
            var uri         = string.Format("{0}()", tableName);
            var queryString = string.Empty;

            if (maxRows > 0)
            {
                queryString = string.Format("$top={0}", maxRows);
            }
            if (!partitionKey.IsNullOrEmpty())
            {
                if (!queryString.IsNullOrEmpty())
                {
                    queryString += "&";
                }

                queryString += BuildQueryExpression("PartitionKey", ComparisonOperators.Equal, partitionKey);
            }


            var request = GenerateRequest("GET", uri, queryString);

            using (var response = (HttpWebResponse)request.GetResponse(false))
            {
                var dataStream = response.GetResponseStream();

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    using (var reader = new StreamReader(dataStream))
                    {
                        var doc = XDocument.Load(reader);

                        throw new StorageException(response.StatusCode, doc);
                    }
                }

                using (var reader = new StreamReader(dataStream))
                {
                    var doc = XDocument.Load(reader);

                    foreach (var entry in doc.Element(Namespaces.Atom + "feed").Elements(Namespaces.Atom + "entry"))
                    {
                        var entity = AzureEntity.FromATOMFeed(entry);
                        yield return(entity);
                    }
                }
            }
        }
Esempio n. 5
0
        internal static AzureEntity FromATOMFeed(XElement entryElement)
        {
            var id       = entryElement.Element(Namespaces.Atom + "id").Value;
            var updated  = DateTime.Parse(entryElement.Element(Namespaces.Atom + "updated").Value);
            var category = entryElement.Element(Namespaces.Atom + "category").Attribute("term").Value;

            var linkElement = entryElement.Element(Namespaces.Atom + "link");
            var link        = new Link();

            link.HRef  = (string)linkElement.Attribute("href");
            link.Rel   = (string)linkElement.Attribute("rel");
            link.Title = (string)linkElement.Attribute("title");

            var properties = entryElement.Element(Namespaces.Atom + "content").Element(Namespaces.DataServicesMeta + "properties");

            string partitionKey = null, rowKey = null;
            AzureFieldCollection fields = new AzureFieldCollection();

            foreach (var prop in properties.Elements())
            {
                if (prop.Name == Namespaces.DataServices + "PartitionKey")
                {
                    partitionKey = prop.Value;
                }
                else if (prop.Name == Namespaces.DataServices + "RowKey")
                {
                    rowKey = prop.Value;
                }
                else
                {
                    fields.Add(AzureField.FromATOMFeed(prop));
                }
            }

            var entity = new AzureEntity(partitionKey, rowKey, fields)
            {
                ID          = id,
                LastUpdated = updated,
                Category    = category,
                Link        = link
            };

            return(entity);
        }
Esempio n. 6
0
        public void InsertOrReplaceEntity(string tableName, AzureEntity entity, bool setIfMatchHeader)
        {
            Validate
            .Begin()
            .ParameterIsNotNull(tableName, "tableName")
            .ParameterIsNotNull(entity, "entity")
            .Check();

            var entry = entity.AsATOMEntry();

            var request = GenerateRequest(
                "PUT",
                string.Format("{0}(PartitionKey='{1}',RowKey='{2}')", tableName, entity.PartitionKey, entity.RowKey),
                null,
                entry);

            if (setIfMatchHeader)
            {
                request.Headers.Add("If-Match", "*");
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.NoContent:     // SUCCESS
                    return;

                default:
                    var dataStream = response.GetResponseStream();
                    using (var reader = new StreamReader(dataStream))
                    {
                        var doc = XDocument.Load(reader);
                        throw new StorageException(response.StatusCode, doc);
                    }
                }
            }
        }
Esempio n. 7
0
 public void InsertOrReplaceEntity(string tableName, AzureEntity entity)
 {
     InsertOrReplaceEntity(tableName, entity, false);
 }
Esempio n. 8
0
 public void UpdateEntity(string tableName, AzureEntity entity)
 {
     InsertOrReplaceEntity(tableName, entity, true);
 }