Transforming row query results to Gosu or Java objects

You can use the transformQueryRow method on the result object of a row query to obtain a query result of Gosu or Java objects. For example, you can transform multiple columns to a Gosu data transfer object or a single column to a Java String. The return value of transformQueryRow is an instance of a class that implements IQueryResult. This object provides zero or more objects of the transformed type. For example, the following Gosu code returns an IQueryResult object that contains objects of type ShortContact.

uses gw.api.database.Query
uses gw.api.database.QuerySelectColumns
uses gw.api.path.Paths

var query = Query.make(Person).withLogSQL(true)

var results = query.select({
        QuerySelectColumns.pathWithAlias("Subtype", Paths.make(Person#Subtype)),
        QuerySelectColumns.pathWithAlias("PName", Paths.make(Person#FirstName))
  }).transformQueryRow(\row -> {
    // Create a ShortContact object for each row
    return new ShortContact (
            row.getColumn("Subtype") as typekey.Contact, row.getColumn("PName") as String)
})

for (p in results index i) {
  print ((i +1) + ": " + p.Subtype.Description + " " + p.Name) 
}

class ShortContact {
  var _subType : typekey.Contact as Subtype
  var _name : String as Name
  construct(st : typekey.Contact, n : String){
    _subType = st
    _name = n
  }
}

Sometimes you only need a single column from each entity. In these cases, you use a simpler syntax for the argument to transformQueryRow. For example, to change the preceding code to retrieve just the PName column instead of a ShortContact object, use the following line that returns an IQueryResult<Person, String> object.

transformQueryRow(\row -> row.getColumn("PName") as String)