AddCondition() публичный Метод

Adds a condition for a specified attribute that consists of an operator and any number of values
public AddCondition ( string attributeName, QueryOperator op ) : void
attributeName string Target attribute name
op QueryOperator Comparison operator
Результат void
        private static bool TryMatchFieldWithCondition(string fieldName, QueryFilter resultFilter, SearchConditions conditions)
        {
            List<SearchCondition> conditionList;
            if (!conditions.TryGetValue(fieldName, out conditionList))
            {
                return false;
            }

            switch (conditionList.Count)
            {
                case 2: // checking for the between operator
                {
                    var lessThanOrEqualCondition = conditionList.SingleOrDefault(c => c.Operator == ScanOperator.LessThanOrEqual);
                    var greaterThanOrEqualCondition = conditionList.SingleOrDefault(c => c.Operator == ScanOperator.GreaterThanOrEqual);

                    if ((lessThanOrEqualCondition == null) || (greaterThanOrEqualCondition == null))
                    {
                        throw new InvalidOperationException("Multiple conditions for the same field are only supported for the BETWEEN case");
                    }

                    if
                    (
                        (lessThanOrEqualCondition.Values.Length != 1)
                        ||
                        (greaterThanOrEqualCondition.Values.Length != 1)
                    )
                    {
                        return false;
                    }

                    resultFilter.AddCondition(fieldName, QueryOperator.Between, greaterThanOrEqualCondition.Values[0], lessThanOrEqualCondition.Values[0]);
                }
                break;
                case 1:
                {
                    SearchCondition condition = conditionList[0];

                    // here we need to convert operators, as AWS SDK's default conversion is buggy
                    QueryOperator queryOperator;
                    if 
                    (
                        (!TryConvertScanOperatorToQueryOperator(condition.Operator, out queryOperator))
                        ||
                        (condition.Values.Length != 1)
                    )
                    {
                        return false;
                    }

                    resultFilter.AddCondition(fieldName, queryOperator, condition.Values[0]);
                }
                break;
                default:
                    throw new InvalidOperationException(string.Format("Too many conditions for field {0}", fieldName));
            }

            // removing the matched condition
            conditions.Remove(fieldName);

            return true;
        }
        private Search SearchShards(string sessionId)
        {
            QueryFilter filter = new QueryFilter();
            filter.AddCondition(ATTRIBUTE_SEQUENCE_ID, QueryOperator.GreaterThan, 0);
            filter.AddCondition(ATTRIBUTE_SESSION_ID, QueryOperator.Equal, GetHashKey(sessionId));

            QueryOperationConfig config = new QueryOperationConfig();
            config.AttributesToGet = new List<string> { ATTRIBUTE_SESSION_ID, ATTRIBUTE_SEQUENCE_ID };
            config.Select = SelectValues.SpecificAttributes;
            config.Filter = filter;

            Search search = this._table.Query(config);

            return search;
        }
Пример #3
0
        /// <summary>
        /// Initiates a Search object to Query a DynamoDB table, with the
        /// specified hash primary key and filter.
        ///
        /// No calls are made until the Search object is used.
        /// </summary>
        /// <param name="hashKey">Value of the hash key for the query operation.</param>
        /// <param name="filter">Filter to use.</param>
        /// <returns>Resultant Search container.</returns>
        public Search Query(Primitive hashKey, QueryFilter filter)
        {
            string      hashKeyName = this.HashKeys[0];
            QueryFilter fullFilter  = new QueryFilter(filter);

            fullFilter.AddCondition(hashKeyName, QueryOperator.Equal, hashKey);

            return(Query(fullFilter));
        }
Пример #4
0
        /// <summary>
        /// Initiates a Search object to Query a DynamoDB table, with the
        /// specified hash primary key and expression.
        ///
        /// No calls are made until the Search object is used.
        /// </summary>
        /// <param name="hashKey">Value of the hash key for the query operation.</param>
        /// <param name="filterExpression">Expression to use.</param>
        /// <returns>Resultant Search container.</returns>
        public Search Query(Primitive hashKey, Expression filterExpression)
        {
            string hashKeyName = this.HashKeys[0];

            QueryFilter hashKeyFilter = new QueryFilter();

            hashKeyFilter.AddCondition(hashKeyName, QueryOperator.Equal, hashKey);

            QueryOperationConfig config = new QueryOperationConfig
            {
                Filter           = hashKeyFilter,
                FilterExpression = filterExpression
            };

            return(Query(config));
        }
Пример #5
0
        /// <summary>
        /// Initiates a Search object to Query a DynamoDB table, with the
        /// specified hash primary key and expression.
        /// 
        /// No calls are made until the Search object is used.
        /// </summary>
        /// <param name="hashKey">Value of the hash key for the query operation.</param>
        /// <param name="filterExpression">Expression to use.</param>
        /// <returns>Resultant Search container.</returns>
        public Search Query(Primitive hashKey, Expression filterExpression)
        {
            string hashKeyName = this.HashKeys[0];

            QueryFilter hashKeyFilter = new QueryFilter();
            hashKeyFilter.AddCondition(hashKeyName, QueryOperator.Equal, hashKey);

            QueryOperationConfig config = new QueryOperationConfig
            {
                Filter = hashKeyFilter,
                FilterExpression = filterExpression
            };

            return Query(config);
        }
Пример #6
0
        /// <summary>
        /// Initiates a Search object to Query a DynamoDB table, with the
        /// specified hash primary key and filter.
        /// 
        /// No calls are made until the Search object is used.
        /// </summary>
        /// <param name="hashKey">Value of the hash key for the query operation.</param>
        /// <param name="filter">Filter to use.</param>
        /// <returns>Resultant Search container.</returns>
        public Search Query(Primitive hashKey, QueryFilter filter)
        {
            string hashKeyName = this.HashKeys[0];
            QueryFilter fullFilter = new QueryFilter(filter);
            fullFilter.AddCondition(hashKeyName, QueryOperator.Equal, hashKey);

            return Query(fullFilter);
        }
        public static void RunDocumentModelSample()
        {
            Console.WriteLine("Loading Businesses table");
            Table table = Table.LoadTable(client, "Businesses");

            Console.WriteLine("Creating and saving first item");
            Document chainStore2 = new Document();
            chainStore2["Name"] = "Big Sales Inc";
            chainStore2["Id"] = 2;
            chainStore2["Owner"] = "Big Sales Corp";
            chainStore2["Managers"] = new List<string> { "Samantha Jones", "Richard Frost" };
            chainStore2["FoundedDate"] = new DateTime(1980, 7, 4);
            chainStore2["Address"] = "123 Main Street, New York, New York";
            chainStore2["Employees"] = 46;
            chainStore2["State"] = "NY";
            table.PutItem(chainStore2);

            Console.WriteLine("Creating and saving first item");
            Document chainStore13 = new Document();
            chainStore13["Name"] = "Big Sales Inc";
            chainStore13["Id"] = 13;
            chainStore13["Owner"] = "Big Sales Corp";
            chainStore13["Managers"] = new List<string> { "Anil Garg", "Alex Short" };
            chainStore13["FoundedDate"] = new DateTime(1999, 3, 15);
            chainStore13["Address"] = "1999 West Ave, Chicago, Illinois";
            chainStore13["Employees"] = 54;
            chainStore13["State"] = "IL";
            table.PutItem(chainStore13);

            Console.WriteLine("Creating and saving second item");
            Document tinyDiner = new Document();
            tinyDiner["Name"] = "Tiny Map-themed Diner";
            tinyDiner["Id"] = 0;
            tinyDiner["Owner"] = "John Doe";
            tinyDiner["FoundedDate"] = new DateTime(1974, 12, 10);
            tinyDiner["Address"] = "800 Lincoln Ave, Seattle, Washington";
            tinyDiner["State"] = "WA";
            table.PutItem(tinyDiner);


            Console.WriteLine("Creating and saving third item");
            Document internetStore = new Document();
            internetStore["Name"] = "Best Online Store Ever";
            internetStore["Id"] = 0;
            internetStore["Owner"] = "Jane Doe";
            internetStore["FoundedDate"] = new DateTime(1994, 2, 19);
            internetStore["Employees"] = 5;
            internetStore["Url"] = "http://www.best-online-store-ever.fake";
            internetStore["Phone"] = "425-555-1234";
            table.PutItem(internetStore);


            Console.WriteLine("Loading item");
            Document doc1 = table.GetItem("Big Sales Inc", 2);
            Console.WriteLine("Attribute counts match (should be true): " +
                (chainStore2.GetAttributeNames().Count == doc1.GetAttributeNames().Count));

            Console.WriteLine("Loading item...");
            Document doc2 = table.GetItem("Best Online Store Ever", 0);
            Console.WriteLine("Attribute counts match (should be true): " +
                (chainStore2.GetAttributeNames().Count == doc1.GetAttributeNames().Count));
            Console.WriteLine("Change item: remove one attribute, add one, modify one attribute");
            doc2["Phone"] = null;
            doc2["Twitter"] = "best-online-store-ever";
            doc2["Employees"] = 4;
            Console.WriteLine("Updating item");
            table.UpdateItem(doc2);

            Console.WriteLine("Reloading item");
            doc2 = table.GetItem("Best Online Store Ever", 0);
            Console.WriteLine("Phone attribute present (should be false): " + doc2.Contains("Phone"));
            Console.WriteLine("Twitter attribute present (should be true): " + doc2.Contains("Twitter"));
            Console.WriteLine("Employees attribute equals 4: " + (object.Equals(doc2["Employees"].AsPrimitive().Value, "4")));

            Console.WriteLine("Loading nonexistent item");
            Document doc3 = table.GetItem("Big Sales Inc", 3);
            Console.WriteLine("Returned document == null (should be true): " + (doc3 == null));



            Search query;
            Console.WriteLine();
            Console.WriteLine("Querying for items (Equals)");
            query = table.Query("Big Sales Inc", new QueryFilter("Id", QueryOperator.Equal, 2));
            List<Document> queryItems1 = query.GetRemaining();
            Console.WriteLine("Number of items returned (should be 1): " + queryItems1.Count);

            Console.WriteLine();
            Console.WriteLine("Querying for items (Between)");
            QueryFilter filter = new QueryFilter();
            filter.AddCondition("Name", QueryOperator.Equal, "Big Sales Inc");
            filter.AddCondition("Id", QueryOperator.Between, 0, 15);
            QueryOperationConfig queryConfig = new QueryOperationConfig
            {
                Filter = filter,
                Limit = 1
            };
            query = table.Query(queryConfig);
            int totalItems = 0;
            while (!query.IsDone)
            {
                Console.WriteLine("Retrieving next set (page) of items");
                List<Document> querySet = query.GetNextSet();
                Console.WriteLine("Number of items returned in set (should be 1, unless last set, which will be 0): " + querySet.Count);

                foreach (Document doc in querySet)
                {
                    Console.WriteLine("Retrieving individual properties");
                    Primitive name = doc["Name"].AsPrimitive();
                    Primitive id = doc["Id"].AsPrimitive();
                    PrimitiveList managers = doc["Managers"].AsPrimitiveList();
                    Console.WriteLine("Name = {0}, Id = {1}, # of managers = {2}", name.Value, id.Value, managers.Entries.Count);
                    totalItems++;
                }
            }
            Console.WriteLine("Total items found (should be 2): " + totalItems);



            Search scan;
            ScanFilter scanFilter;

            Console.WriteLine();
            Console.WriteLine("Scanning for items (GreaterThan)");
            scanFilter = new ScanFilter();
            scanFilter.AddCondition("Employees", ScanOperator.GreaterThan, 50);
            scan = table.Scan(scanFilter);
            List<Document> scanItems1 = scan.GetRemaining();
            Console.WriteLine("Number of items returned (should be 1): " + scanItems1.Count);

            Console.WriteLine();
            Console.WriteLine("Scanning for items (GreaterThan and LessThan)");
            scanFilter = new ScanFilter();
            scanFilter.AddCondition("Employees", ScanOperator.GreaterThan, 2);
            scanFilter.AddCondition("FoundedDate", ScanOperator.LessThan, new DateTime(1993, 1, 1));
            scan = table.Scan(scanFilter);
            List<Document> scanItems2 = scan.GetRemaining();
            Console.WriteLine("Number of items returned (should be 1): " + scanItems2.Count);


            Console.WriteLine();
            Console.WriteLine("Retrieving an item");
            Document existingDoc = table.GetItem("Big Sales Inc", 13);
            Console.WriteLine("Returned document == null (should be false): " + (existingDoc == null));
            Console.WriteLine("Deleting item");
            table.DeleteItem("Big Sales Inc", 13);
            Console.WriteLine("Retrieving same item");
            existingDoc = table.GetItem("Big Sales Inc", 13);
            Console.WriteLine("Returned document == null (should be true): " + (existingDoc == null));


            Console.WriteLine();
            Console.WriteLine("Scanning for items (no filter) and deleting all");
            scanFilter = new ScanFilter();
            scan = table.Scan(scanFilter);
            List<Document> scanItems3 = scan.GetRemaining();
            Console.WriteLine("Number of items returned (should be 3): " + scanItems3.Count);
            for (int i = 0; i < scanItems3.Count; i++)
            {
                Document item = scanItems3[i];
                Console.WriteLine("Deleting item {0} of {1}", i + 1, scanItems3.Count);
                table.DeleteItem(item);
            }

            Console.WriteLine("Scanning table again");
            scan = table.Scan(scanFilter);
            scanItems3 = scan.GetRemaining();
            Console.WriteLine("Number of items returned (should be 0): " + scanItems3.Count);
        }