Query Filters
Earlier in this chapter, we went through retrieving data with methods like allItemsOfType. While useful, often times you'll probably a set of criteria you'd like met. Query filters help construct the "where" clause of a SQL statements. An LDMQueryFilter is composed of an array of LDMQueryFilterClauses, which in turn is composed of an array of LDMFilters. Let's look at the following example.
LDMQueryFilter *queryFilter = [[LDMQueryFilter alloc] init];
LDMQueryFilterClause *clause = [[LDMQueryFilterClause alloc] init];
[clause addFilter:[[LDMFilter alloc] initWithField:@"accountId" value:@"testAccount123" comparison:LDMFilterComparisonEquals resolvedDimension:NO]];
[queryFilter addClause:clause];
NSArray *results = [[LDMDataManager sharedInstance] allItemsOfType:@"Account" withQueryFilter:queryFilter];
The code above creates a query filter, with one clause, which has one filter. The SQL in turn will be generated.
select t.* from <communityName>_entity_Account t where t.accountId = 'testAccount123' order by t.h_clientId
Every LDMFilter you add to an LDMQueryFilterClause will append an additional OR clause to the final SQL statement. Every LDMQueryFilterClause you add to the LDMQueryFilter will append an additional AND clause to the final SQL statement. Let's take a look at another example.
LDMQueryFilter *queryFilter = [[LDMQueryFilter alloc] init];
LDMQueryFilterClause *idClause = [[LDMQueryFilterClause alloc] init];
[idClause addFilter:[[LDMFilter alloc] initWithField:@"accountId" value:@"testAccount123" comparison:LDMFilterComparisonEquals resolvedDimension:NO]];
[idClause addFilter:[[LDMFilter alloc] initWithField:@"accountId" value:@"testAccount124" comparison:LDMFilterComparisonEquals resolvedDimension:NO]];
LDMQueryFilterClause *typeClause = [[LDMQueryFilterClause alloc] init];
[typeClause addFilter:[[LDMFilter alloc] initWithField:@"accountType" value:@"domesticAccount" comparison:LDMFilterComparisonEquals resolvedDimension:NO]];
[typeClause addFilter:[[LDMFilter alloc] initWithField:@"accountType" value:@"foreignAccount" comparison:LDMFilterComparisonEquals resolvedDimension:NO]];
[queryFilter initWithQueryFilterClauses:@[idClause, typeClause]];
NSArray *results = [[LDMDataManager sharedInstance] allItemsOfType:@"Account" withQueryFilter:queryFilter];
The code above would give the SQL below:
select t.* from <communityName>_entity_Account t where ((t.accountId = 'testAccount123' OR t.accountId = 'testAccount124') AND (t.accountType = 'domesticAccount' OR t.accountType = 'foreignAccount')) order by t.h_clientId