Using standard query filters in Gosu code

You often use standard query filters in Gosu code to separate code that constructs a general purpose query from code in subroutines that process different subsets of the result. Query builder code that constructs a query can be complex if joins, subselects, and compound predicate expressions are involved. Placing this part of your query builder code in one location improves the process of debugging and maintaining your code.

The following Gosu code builds a query for addresses in the state of Illinois, and then passes the query result to three subroutines for processing. Each subroutine creates and adds a standard query filter for a city in Illinois: Bloomington, Chicago, or Evanston. Then, the subroutines iterate their own subset of the main query result.

uses gw.api.database.Query
uses gw.api.database.IQueryBeanResult
uses gw.api.filters.StandardQueryFilter
uses gw.api.database.QuerySelectColumns
uses gw.api.path.Paths

// Select addresses from the state of Illinois.
var query = Query.make(Address)
query.compare(Address#State, Equals, typekey.State.TC_IL) 

// Get a result and apply ordering
var result = query.select() 
result.orderBy(QuerySelectColumns.path(Paths.make(Address#City)))
       .thenBy(QuerySelectColumns.path(Paths.make(Address#PostalCode)))

// Pass the ordered result to subroutines to process Illinois addresses by city.
processBloomington(result)
result.clearFilters() // Remove any filters added by the subroutine from the result.

processChicago(result)
result.clearFilters() // Remove any filters added by the subroutine from the result.

processEvanston(result)
result.clearFilters() // Remove any filters added by the subroutine from the result.

// Subroutines

// Add Bloomington filter and process results.
function processBloomington(aResult : IQueryBeanResult<Address>) { 
  var queryFilterBloomington = new StandardQueryFilter("QueryFilterBloomington",
          \ filterQuery -> {filterQuery.compare(Address#City, Equals, "Bloomington")})
  aResult.addFilter(queryFilterBloomington)
  for (address in aResult) {
    print (address.City + ", " + address.State + "  " + address.PostalCode)
  }
}

// Add Chicago filter and process results.
function processChicago(aResult : IQueryBeanResult<Address>) { 
  var queryFilterChicago = new StandardQueryFilter("QueryFilterChicago",
          \ filterQuery -> {filterQuery.compare(Address#City, Equals, "Chicago")})
  aResult.addFilter(queryFilterChicago)
  for (address in result) {
    print (address.City + ", " + address.State + "  " + address.PostalCode)
  }
}

// Add Evanston filter and process results.
function processEvanston(aResult : IQueryBeanResult<Address>) { 
  var queryFilterEvanston = new StandardQueryFilter("QueryFilterEvanston",
          \ filterQuery -> {filterQuery.compare(Address#City, Equals, "Evanston")})
  aResult.addFilter(queryFilterEvanston)
  for (address in result) {
    print (address.City + ", " + address.State + "  " + address.PostalCode)
  }
}