Пример #1
0
        /// <summary>
        /// Get the skip query.
        /// </summary>
        /// <returns>The skip query string.</returns>
        public string GetSkipQuery()
        {
            // Select the skip type.
            switch (typeof(T).Name.ToLower())
            {
            case "system.boolean":
            case "boolean":
            case "bool":
                return(TableQuery.GenerateFilterConditionForBool(
                           _skipPropertyName, QueryComparisons.GreaterThanOrEqual,
                           (Boolean)Convert.ChangeType(_skipValue, typeof(Boolean))));

            case "system.byte[]":
            case "byte[]":
                return(TableQuery.GenerateFilterConditionForBinary(
                           _skipPropertyName, QueryComparisons.GreaterThanOrEqual,
                           (Byte[])Convert.ChangeType(_skipValue, typeof(Byte[]))));

            case "system.datetimeoffset":
            case "datetimeoffset":
                return(TableQuery.GenerateFilterConditionForDate(
                           _skipPropertyName, QueryComparisons.GreaterThanOrEqual,
                           (DateTimeOffset)Convert.ChangeType(_skipValue, typeof(DateTimeOffset))));

            case "system.double":
            case "double":
                return(TableQuery.GenerateFilterConditionForDouble(
                           _skipPropertyName, QueryComparisons.GreaterThanOrEqual,
                           (Double)Convert.ChangeType(_skipValue, typeof(Double))));

            case "system.guid":
            case "guid":
                return(TableQuery.GenerateFilterConditionForGuid(
                           _skipPropertyName, QueryComparisons.GreaterThanOrEqual,
                           (Guid)Convert.ChangeType(_skipValue, typeof(Guid))));

            case "system.int32":
            case "int32":
            case "int":
                return(TableQuery.GenerateFilterConditionForInt(
                           _skipPropertyName, QueryComparisons.GreaterThanOrEqual,
                           (Int32)Convert.ChangeType(_skipValue, typeof(Int32))));

            case "system.int64":
            case "int64":
            case "long":
                return(TableQuery.GenerateFilterConditionForLong(
                           _skipPropertyName, QueryComparisons.GreaterThanOrEqual,
                           (Int64)Convert.ChangeType(_skipValue, typeof(Int64))));

            case "system.string":
            case "string":
                return(TableQuery.GenerateFilterCondition(
                           _skipPropertyName, QueryComparisons.GreaterThanOrEqual,
                           (String)Convert.ChangeType(_skipValue, typeof(String))));

            default:
                throw new Exception("The skip value type is not supported.");
            }
        }
Пример #2
0
        /// <summary>
        /// Get async.
        /// </summary>
        /// <typeparam name="ST">The skip type.</typeparam>
        /// <param name="value">The value.</param>
        /// <param name="propertyName">The property name.</param>
        /// <param name="operation">The operation.</param>
        /// <param name="take">The number of items to return.</param>
        /// <param name="skip">The number of items to skip.</param>
        /// <returns>The array or items.</returns>
        public async Task <T[]> GetAsync <ST>(byte[] value, string propertyName = "PartitionKey",
                                              string operation = QueryComparisons.Equal, int take = 100, SkipCount <ST> skip = null)
        {
            TableQuery <T> scanQuery = null;

            if (skip == null)
            {
                // Create the query.
                scanQuery = new TableQuery <T>().Where
                                (TableQuery.GenerateFilterConditionForBinary(propertyName, operation, value));
            }
            else
            {
                // Create the query.
                scanQuery = new TableQuery <T>().Where
                                (TableQuery.CombineFilters(
                                    TableQuery.GenerateFilterConditionForBinary(propertyName, operation, value),
                                    TableOperators.And,
                                    skip.GetSkipQuery()
                                    ));
            }

            // Return the array
            return(await ScanAsyncEx(scanQuery, take));
        }
Пример #3
0
        /// <summary>
        ///     Shortcut method that queries the table based on a given PartitionKey and given property with
        ///     the same property name. Handles the continuation token scenario as well. Overloaded to accept
        ///     all appropriate table entity types.
        /// </summary>
        /// <param name="partitionKey"></param>
        /// <param name="propertyName"></param>
        /// <param name="property"></param>
        /// <returns></returns>
        public IEnumerable <TAzureTableEntity> WherePropertyEquals(string partitionKey, string propertyName, byte[] property)
        {
            var propertyFilter = TableQuery.GenerateFilterConditionForBinary(propertyName, QueryComparisons.Equal, property);
            var query          = PartitionKeyAndPropertyFilterQuery(partitionKey, propertyFilter);

            return(RunQuerySegment(query));
        }
Пример #4
0
        /// <summary>
        ///     Async shortcut method that queries the table based on a given PartitionKey and given property with
        ///     the same property name. Handles the continuation token scenario as well. Overloaded to accept
        ///     all appropriate table entity types.
        /// </summary>
        /// <param name="partitionKey"></param>
        /// <param name="propertyName"></param>
        /// <param name="property"></param>
        /// <returns></returns>
        public async Task <List <TAzureTableEntity> > WherePropertyEqualsAsync(string partitionKey, string propertyName, byte[] property)
        {
            var propertyFilter = TableQuery.GenerateFilterConditionForBinary(propertyName, QueryComparisons.Equal, property);
            var query          = PartitionKeyAndPropertyFilterQuery(partitionKey, propertyFilter);

            return(await RunQuerySegmentAsync(query).ConfigureAwait(false));
        }
 public static string Column(string columnName, byte[] value, ColumnOperator op = ColumnOperator.Equal)
 {
     return(TableQuery.GenerateFilterConditionForBinary(
                columnName,
                ToQueryComparison(op),
                value));
 }
Пример #6
0
        /// <summary>
        /// Get async.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="propertyName">The property name.</param>
        /// <param name="operation">The operation.</param>
        /// <param name="take">The number of items to return.</param>
        /// <returns>The array or items.</returns>
        public async Task <T[]> GetAsync(byte[] value, string propertyName = "PartitionKey",
                                         string operation = QueryComparisons.Equal, int take = 100)
        {
            // Create the query.
            TableQuery <T> scanQuery = new TableQuery <T>().Where
                                           (TableQuery.GenerateFilterConditionForBinary(propertyName, operation, value));

            // Return the array
            return(await ScanAsyncEx(scanQuery, take));
        }
Пример #7
0
        DictionaryTableEntity GetDictionaryTableEntity(Type type, string property, object value)
        {
            var tableName = type.Name;
            var table     = client.GetTableReference(tableName);

            TableQuery <DictionaryTableEntity> query;

            var propertyInfo = type.GetProperty(property);

            if (propertyInfo.PropertyType == typeof(byte[]))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForBinary(property, QueryComparisons.Equal, (byte[])value));
            }
            else if (propertyInfo.PropertyType == typeof(bool))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForBool(property, QueryComparisons.Equal, (bool)value));
            }
            else if (propertyInfo.PropertyType == typeof(DateTime))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForDate(property, QueryComparisons.Equal, (DateTime)value));
            }
            else if (propertyInfo.PropertyType == typeof(Guid))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForGuid(property, QueryComparisons.Equal, (Guid)value));
            }
            else if (propertyInfo.PropertyType == typeof(Int32))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForInt(property, QueryComparisons.Equal, (int)value));
            }
            else if (propertyInfo.PropertyType == typeof(Int64))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForLong(property, QueryComparisons.Equal, (long)value));
            }
            else if (propertyInfo.PropertyType == typeof(Double))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForDouble(property, QueryComparisons.Equal, (double)value));
            }
            else if (propertyInfo.PropertyType == typeof(string))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterCondition(property, QueryComparisons.Equal, (string)value));
            }
            else
            {
                throw new NotSupportedException(
                          string.Format("The property type '{0}' is not supported in windows azure table storage",
                                        propertyInfo.PropertyType.Name));
            }
            var tableEntity = table.ExecuteQuery(query).SafeFirstOrDefault();

            return(tableEntity);
        }
Пример #8
0
        public static TableQuery <DictionaryTableEntity> BuildWherePropertyQuery(Type type, string property, object value)
        {
            TableQuery <DictionaryTableEntity> query;

            var propertyInfo = type.GetProperty(property);

            if (propertyInfo == null)
            {
                return(null);
            }

            if (propertyInfo.PropertyType == typeof(byte[]))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForBinary(property, QueryComparisons.Equal, (byte[])value));
            }
            else if (propertyInfo.PropertyType == typeof(bool))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForBool(property, QueryComparisons.Equal, (bool)value));
            }
            else if (propertyInfo.PropertyType == typeof(DateTime))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForDate(property, QueryComparisons.Equal, (DateTime)value));
            }
            else if (propertyInfo.PropertyType == typeof(Guid))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForGuid(property, QueryComparisons.Equal, (Guid)value));
            }
            else if (propertyInfo.PropertyType == typeof(int))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForInt(property, QueryComparisons.Equal, (int)value));
            }
            else if (propertyInfo.PropertyType == typeof(long))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForLong(property, QueryComparisons.Equal, (long)value));
            }
            else if (propertyInfo.PropertyType == typeof(double))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForDouble(property, QueryComparisons.Equal, (double)value));
            }
            else if (propertyInfo.PropertyType == typeof(string))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterCondition(property, QueryComparisons.Equal, (string)value));
            }
            else
            {
                throw new NotSupportedException($"The property type '{propertyInfo.PropertyType.Name}' is not supported in Azure Table Storage");
            }

            return(query);
        }
Пример #9
0
        protected override string ExpressionFilterConverter(IFilterExpression <T> expression)
        {
            if (expression.PropertyType == typeof(byte[]))
            {
                return(TableQuery.GenerateFilterConditionForBinary(expression.PropertyName, GetInstruction(expression.Comparator), (byte[])expression.PropertyValue));
            }

            if (expression.PropertyType == typeof(bool) || expression.PropertyType == typeof(bool?))
            {
                return(TableQuery.GenerateFilterConditionForBool(expression.PropertyName, GetInstruction(expression.Comparator), (bool)expression.PropertyValue));
            }

            if (expression.PropertyType == typeof(DateTime) || expression.PropertyType == typeof(DateTime?))
            {
                return(TableQuery.GenerateFilterConditionForDate(expression.PropertyName, GetInstruction(expression.Comparator), (DateTime)expression.PropertyValue));
            }

            if (expression.PropertyType == typeof(DateTimeOffset) || expression.PropertyType == typeof(DateTimeOffset?))
            {
                return(TableQuery.GenerateFilterConditionForDate(expression.PropertyName, GetInstruction(expression.Comparator), (DateTimeOffset)expression.PropertyValue));
            }

            if (expression.PropertyType == typeof(double) || expression.PropertyType == typeof(double?))
            {
                return(TableQuery.GenerateFilterConditionForDouble(expression.PropertyName, GetInstruction(expression.Comparator), (double)expression.PropertyValue));
            }

            if (expression.PropertyType == typeof(Guid) || expression.PropertyType == typeof(Guid?))
            {
                return(TableQuery.GenerateFilterConditionForGuid(expression.PropertyName, GetInstruction(expression.Comparator), (Guid)expression.PropertyValue));
            }

            if (expression.PropertyType == typeof(int) || expression.PropertyType == typeof(int?))
            {
                return(TableQuery.GenerateFilterConditionForInt(expression.PropertyName, GetInstruction(expression.Comparator), (int)expression.PropertyValue));
            }

            if (expression.PropertyType == typeof(long) || expression.PropertyType == typeof(long?))
            {
                return(TableQuery.GenerateFilterConditionForLong(expression.PropertyName, GetInstruction(expression.Comparator), (long)expression.PropertyValue));
            }

            return(TableQuery.GenerateFilterCondition(expression.PropertyName, GetInstruction(expression.Comparator), expression.PropertyValue?.ToString() ?? string.Empty));
        }
Пример #10
0
        public string GenerateFilter(string propertyName, object propertyValue, string operatorString)
        {
            var propertyType = propertyValue.GetType();

            if (propertyType == typeof(string))
            {
                return(TableQuery.GenerateFilterCondition(propertyName, operatorString, propertyValue as string));
            }

            if (propertyType == typeof(DateTime))
            {
                return(TableQuery.GenerateFilterConditionForDate(propertyName, operatorString, (DateTime)propertyValue));
            }

            if (propertyType == typeof(bool))
            {
                return(TableQuery.GenerateFilterConditionForBool(propertyName, operatorString, (bool)propertyValue));
            }

            if (propertyType == typeof(double))
            {
                return(TableQuery.GenerateFilterConditionForDouble(propertyName, operatorString, (double)propertyValue));
            }

            if (propertyType == typeof(int))
            {
                return(TableQuery.GenerateFilterConditionForInt(propertyName, operatorString, (int)propertyValue));
            }

            if (propertyType == typeof(long))
            {
                return(TableQuery.GenerateFilterConditionForLong(propertyName, operatorString, (long)propertyValue));
            }

            if (propertyType == typeof(Guid))
            {
                return(TableQuery.GenerateFilterConditionForGuid(propertyName, operatorString, (Guid)propertyValue));
            }

            return(propertyType == typeof(byte[]) ?
                   TableQuery.GenerateFilterConditionForBinary(propertyName, operatorString, (byte[])propertyValue) :
                   TableQuery.GenerateFilterCondition(propertyName, operatorString, propertyValue.ToString()));
        }
Пример #11
0
        static string GenerateFilterCondition(BinaryExpression binary)
        {
            var left = binary.Left as MemberExpression;

            if (left == null)
            {
                throw new InvalidOperationException();
            }

            var op         = ToComparisonOperator(binary.NodeType);
            var rightValue = binary.Right.Invoke();

            return
                (left.Type == typeof(byte[]) ? TableQuery.GenerateFilterConditionForBinary(left.Member.Name, op, (byte[])rightValue) :
                 left.Type == typeof(bool) ? TableQuery.GenerateFilterConditionForBool(left.Member.Name, op, (bool)rightValue) :
                 left.Type == typeof(DateTime) ? TableQuery.GenerateFilterConditionForDate(left.Member.Name, op, (DateTime)rightValue) :
                 left.Type == typeof(DateTimeOffset) ? TableQuery.GenerateFilterConditionForDate(left.Member.Name, op, (DateTimeOffset)rightValue) :
                 left.Type == typeof(double) ? TableQuery.GenerateFilterConditionForDouble(left.Member.Name, op, (double)rightValue) :
                 left.Type == typeof(Guid) ? TableQuery.GenerateFilterConditionForGuid(left.Member.Name, op, (Guid)rightValue) :
                 left.Type == typeof(int) ? TableQuery.GenerateFilterConditionForInt(left.Member.Name, op, (int)rightValue) :
                 left.Type == typeof(long) ? TableQuery.GenerateFilterConditionForLong(left.Member.Name, op, (long)rightValue) :
                 TableQuery.GenerateFilterCondition(left.Member.Name, op, rightValue.To <string>()));
        }
Пример #12
0
        static AzureConditionBuilderExtention()
        {
            ToAzureType = new Dictionary <Type, Func <object, string> >();
            ToAzureType[typeof(string)]   = (x) => TableQuery.GenerateFilterCondition("", "", x.ToString());
            ToAzureType[typeof(bool)]     = (x) => TableQuery.GenerateFilterConditionForBool("", "", (bool)x);
            ToAzureType[typeof(DateTime)] = (x) => TableQuery.GenerateFilterConditionForDate("", "", ((DateTime)x));
            ToAzureType[typeof(double)]   = (x) => TableQuery.GenerateFilterConditionForDouble("", "", (double)x);
            ToAzureType[typeof(Guid)]     = (x) => TableQuery.GenerateFilterConditionForGuid("", "", (Guid)x);
            ToAzureType[typeof(int)]      = (x) => TableQuery.GenerateFilterConditionForInt("", "", (int)x);
            ToAzureType[typeof(long)]     = (x) => TableQuery.GenerateFilterConditionForLong("", "", (long)x);
            ToAzureType[typeof(byte[])]   = (x) => TableQuery.GenerateFilterConditionForBinary("", "", (byte[])x);

            ToAzureCondition            = new Dictionary <string, string>();
            ToAzureCondition["=="]      = QueryComparisons.Equal;
            ToAzureCondition[">"]       = QueryComparisons.GreaterThan;
            ToAzureCondition[">="]      = QueryComparisons.GreaterThanOrEqual;
            ToAzureCondition["<"]       = QueryComparisons.LessThan;
            ToAzureCondition["<="]      = QueryComparisons.LessThanOrEqual;
            ToAzureCondition["!="]      = QueryComparisons.NotEqual;
            ToAzureCondition["AndAlso"] = TableOperators.And;
            ToAzureCondition["OrElse"]  = TableOperators.Or;
            ToAzureCondition["Not"]     = TableOperators.Not;
        }
        public void TableGenericQueryOnSupportedTypes()
        {
            // 1. Filter on String
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterCondition("String", QueryComparisons.GreaterThanOrEqual, "0050"), Midpoint);

            // 2. Filter on Guid
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForGuid("Guid", QueryComparisons.Equal, this.middleRef.Guid), 1);

            // 3. Filter on Long
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForLong("Int64", QueryComparisons.GreaterThanOrEqual,
                                                                                   this.middleRef.LongPrimitive), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForLong("LongPrimitive",
                                                                                                      QueryComparisons.GreaterThanOrEqual, this.middleRef.LongPrimitive), Midpoint);

            // 4. Filter on Double
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForDouble("Double", QueryComparisons.GreaterThanOrEqual,
                                                                                     this.middleRef.Double), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForDouble("DoublePrimitive",
                                                                                                        QueryComparisons.GreaterThanOrEqual, this.middleRef.DoublePrimitive), Midpoint);

            // 5. Filter on Integer
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForInt("Int32", QueryComparisons.GreaterThanOrEqual,
                                                                                  this.middleRef.Int32), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForInt("IntegerPrimitive",
                                                                                                     QueryComparisons.GreaterThanOrEqual, middleRef.IntegerPrimitive), Midpoint);

            // 6. Filter on Date
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForDate("DateTimeOffset", QueryComparisons.GreaterThanOrEqual,
                                                                                   this.middleRef.DateTimeOffset), Midpoint);

            // 7. Filter on Boolean
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBool("Bool", QueryComparisons.Equal, this.middleRef.Bool), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBool("BoolPrimitive", QueryComparisons.Equal, this.middleRef.BoolPrimitive),
                                         Midpoint);

            // 8. Filter on Binary
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBinary("Binary", QueryComparisons.Equal, this.middleRef.Binary), 1);

            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBinary("BinaryPrimitive", QueryComparisons.Equal,
                                                                                     middleRef.BinaryPrimitive), 1);

            // 9. Filter on Binary GTE
            ExecuteQueryAndAssertResults(this.currentTable,
                                         TableQuery.GenerateFilterConditionForBinary("Binary", QueryComparisons.GreaterThanOrEqual,
                                                                                     this.middleRef.Binary), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForBinary("BinaryPrimitive",
                                                                                                        QueryComparisons.GreaterThanOrEqual, this.middleRef.BinaryPrimitive), Midpoint);

            // 10. Complex Filter on Binary GTE
            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.CombineFilters(
                                             TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,
                                                                                this.middleRef.PartitionKey),
                                             TableOperators.And,
                                             TableQuery.GenerateFilterConditionForBinary("Binary", QueryComparisons.GreaterThanOrEqual,
                                                                                         this.middleRef.Binary)), Midpoint);

            ExecuteQueryAndAssertResults(this.currentTable, TableQuery.GenerateFilterConditionForBinary("BinaryPrimitive",
                                                                                                        QueryComparisons.GreaterThanOrEqual, this.middleRef.BinaryPrimitive), Midpoint);
        }
Пример #14
0
        public static string FilterString(string propertyName, string queryComparison, object value)
        {
            var filterString = "";

            var val = "";

            if (value != null)
            {
                val = value.GetType().ToString().Replace("System.", "");
            }

            switch (val)
            {
            //case "":
            //    filterString = TableQuery.GenerateFilterCondition(property, queryComparison, "");
            //    break;

            case "Byte[]":
                filterString = TableQuery.GenerateFilterConditionForBinary(propertyName, queryComparison, (byte[])value);
                break;

            case "Boolean":
                filterString = TableQuery.GenerateFilterConditionForBool(propertyName, queryComparison, (bool)value);
                break;

            case "DateTime":
                filterString = TableQuery.GenerateFilterConditionForDate(propertyName, queryComparison, (DateTime)value);
                break;

            case "Double":
                filterString = TableQuery.GenerateFilterConditionForDouble(propertyName, queryComparison, (double)value);
                break;

            case "Guid":
                filterString = TableQuery.GenerateFilterConditionForGuid(propertyName, queryComparison, (Guid)value);
                break;

            case "Int32":
                filterString = TableQuery.GenerateFilterConditionForInt(propertyName, queryComparison, (int)value);
                break;

            case "Int64":
                filterString = TableQuery.GenerateFilterConditionForLong(propertyName, queryComparison, (long)value);
                break;

            default:
                //if (Utils.IsRowKey(property))
                //    filterString = TableQuery.GenerateFilterCondition("RowKey", queryComparison, (String)value);
                //else

                //if (Utils.IsPartitionKey(property))
                //    filterString = TableQuery.GenerateFilterCondition("PartitionKey", queryComparison, (String)value);
                //else
                filterString = TableQuery.GenerateFilterCondition(propertyName, queryComparison, (String)value);
                break;
            }

            //Event("Type Not Handled error on QueryOn");

            return(filterString);
        }
Пример #15
0
        T ISagaPersister.Get <T>(string property, object value)
        {
            var type      = typeof(T);
            var tableName = type.Name;
            var table     = client.GetTableReference(tableName);

            TableQuery <DictionaryTableEntity> query;

            var propertyInfo = type.GetProperty(property);

            if (propertyInfo.PropertyType == typeof(byte[]))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForBinary(property, QueryComparisons.Equal, (byte[])value));
            }
            else if (propertyInfo.PropertyType == typeof(bool))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForBool(property, QueryComparisons.Equal, (bool)value));
            }
            else if (propertyInfo.PropertyType == typeof(DateTime))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForDate(property, QueryComparisons.Equal, (DateTime)value));
            }
            else if (propertyInfo.PropertyType == typeof(Guid))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForGuid(property, QueryComparisons.Equal, (Guid)value));
            }
            else if (propertyInfo.PropertyType == typeof(Int32))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForInt(property, QueryComparisons.Equal, (int)value));
            }
            else if (propertyInfo.PropertyType == typeof(Int64))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForLong(property, QueryComparisons.Equal, (long)value));
            }
            else if (propertyInfo.PropertyType == typeof(Double))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterConditionForDouble(property, QueryComparisons.Equal, (double)value));
            }
            else if (propertyInfo.PropertyType == typeof(string))
            {
                query = new TableQuery <DictionaryTableEntity>().Where(TableQuery.GenerateFilterCondition(property, QueryComparisons.Equal, (string)value));
            }
            else
            {
                throw new NotSupportedException(
                          string.Format("The property type '{0}' is not supported in windows azure table storage",
                                        propertyInfo.PropertyType.Name));
            }

            try
            {
                return(ToEntity <T>(table.ExecuteQuery(query).FirstOrDefault()));
            }
            catch (WebException ex)
            {
                // occurs when table has not yet been created, but already looking for absence of instance
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var response = (HttpWebResponse)ex.Response;
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(default(T));
                    }
                }

                throw;
            }
        }
Пример #16
0
        public override string ToString()
        {
            var filterOperation = QueryComparisons.Equal;

            switch (Operator)
            {
            case QueryFilterOperator.Equal:
                filterOperation = QueryComparisons.Equal;
                break;

            case QueryFilterOperator.NotEqual:
                filterOperation = QueryComparisons.NotEqual;
                break;

            case QueryFilterOperator.Lower:
                filterOperation = QueryComparisons.LessThan;
                break;

            case QueryFilterOperator.Greater:
                filterOperation = QueryComparisons.GreaterThan;
                break;

            case QueryFilterOperator.LowerEqual:
                filterOperation = QueryComparisons.LessThanOrEqual;
                break;

            case QueryFilterOperator.GreaterEqual:
                filterOperation = QueryComparisons.GreaterThanOrEqual;
                break;
            }

            if (Value is string)
            {
                return(TableQuery.GenerateFilterCondition(Property, filterOperation, (string)Value));
            }

            if (Value is bool)
            {
                return(TableQuery.GenerateFilterConditionForBool(Property, filterOperation, (bool)Value));
            }

            if (Value is byte[])
            {
                return(TableQuery.GenerateFilterConditionForBinary(Property, filterOperation, (byte[])Value));
            }

            if (Value is DateTimeOffset)
            {
                return(TableQuery.GenerateFilterConditionForDate(Property, filterOperation, (DateTimeOffset)Value));
            }

            if (Value is double)
            {
                return(TableQuery.GenerateFilterConditionForDouble(Property, filterOperation, (double)Value));
            }

            if (Value is Guid)
            {
                return(TableQuery.GenerateFilterConditionForGuid(Property, filterOperation, (Guid)Value));
            }

            if (Value is int)
            {
                return(TableQuery.GenerateFilterConditionForInt(Property, filterOperation, (int)Value));
            }

            if (Value is long)
            {
                return(TableQuery.GenerateFilterConditionForLong(Property, filterOperation, (long)Value));
            }

            throw new NotSupportedException($"QueryFilter of Type \"{Value?.GetType().FullName}\" is not supported.");
        }