Creating column specifications

The column selection arguments that you pass to the select method for a row query are objects that implement IQuerySelectColumn. You use methods on the QuerySelectColumns class to create column selection arguments. The columns that you select must be columns in the database. You cannot specify virtual properties, enhancement methods, or other entity methods as column selections. To create a column that you reference by entity.property syntax or by position, starting from 0, you use the path method on QuerySelectColumns. To create a named column, you use the pathWithAlias method.

You use methods on the gw.api.path.Paths class to create column paths, which are gw.api.path.PersistentPath objects. To specify the path to an individual column, you use the make method on Paths. To specify the path to a column in a related entity, you provide additional parameters to the make method. For example, the first two of the following Gosu expressions select the ID column from the Contact table. The second expression sets the alias ContactID on the column. The third expression creates a column for the user name of the user that created the Contact entity instance:

QuerySelectColumns.path(Paths.make(Contact#ID))
QuerySelectColumns.pathWithAlias("ContactID", Paths.make(Contact#ID))
QuerySelectColumns.path(Paths.make(Contact#CreateUser, User#Credential, Credential#UserName))

The following Gosu code sets up a row query that returns the columns Subtype, FirstName, and LastName for all Person entity instances and prints the values for each row.

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

var query = Query.make(Person)

var results = query.select({
  QuerySelectColumns.path(Paths.make(Person#Subtype)),
  QuerySelectColumns.pathWithAlias("FName", Paths.make(Person#FirstName)),
  QuerySelectColumns.pathWithAlias("LName", Paths.make(Person#LastName))
})

for (row in results) {
  // Access the first column by entity.property, the second column by alias, the third by position
  print(row.getColumn("Person.Subtype") + ":\t" + row.getColumn("FName") + " " + row.getColumn(2))
}