Types and methods for building queries

The following sections describe the major classes, interfaces, and methods that you use to build queries.

Types for building queries

The following Gosu types in the gw.api.database package provide methods for building a query. Query, Restriction, and Table all implement the ISelectQueryBuilder interface. IQueryBeanResult implements IQueryResult.

Type                           

Description

Query

A class that represents a query that fetches entities or rows from the application database.

Restriction

An interface that represents a Boolean condition or conditions that restricts the set of items that a query fetches from the application database.

Table

An interface that represents an entity type that you add to the query with a join or a subselect.

IQueryBeanResult

An interface that represents the results of a query that fetches entities from the application database. Adding sorting or filters to this item affects the query.

IQueryResult

An interface that represents the results of a query that fetches rows from the application database. Adding sorting or filters to this item affects the query.

Methods on Query type for building queries

The following methods provide fundamental functionality for building queries. Other methods provide filtering to the query. To see information about methods that provide additional functionality, see the Javadoc. You can chain many of these methods to create concise code. For example, you can specify a query on an entity type, join to another entity type, and create the result set object by using a line like the following one:

var result = Query.make(Company).join(Company#PrimaryAddress).select()

Method                      

Description

Returned object type

join

Returns a table object with information from several entity types joined together in advanced ways.

Table

make

Static method on the Query class that creates a new query object.

Query

select

Defines the items to fetch from the application database, according to restrictions added to the query. Returns a result object that provides one of the following types of item:

  • Items fetched from a single, root entity type
  • Data rows containing specified fields from one or more entity types

IQueryBeanResult or IQueryResult

subselect

Joins a dependent source. For example:

var outerQuery = Query.make(User) // Returns User Query

var innerQuery = Query.make(Note) // Returns Note Query

// Filter the inner query

noteQuery.compareIn(Note#Topic, {NoteTopicType.TC_GENERAL, NoteTopicType.TC_LEGAL})

// Filter the outer query by using a subselect

userQuery.subselect(User#ID, InOperation.CompareIn, noteQuery, Note#Author )

Table

union

Combines all results from two queries into a single result.

GroupingQuery

withDistinct

Whether to remove duplicate entity instances from the result.

Query

Methods on Result types for building queries

The following methods provide ordering functionality for building queries.

Other methods provide filtering to the result set. To see information about methods that provide additional functionality, see the Javadoc.

Method

Description

orderBy

Clears all previous ordering, and then orders results by the specified column in ascending order.

orderByDescending

Clears all previous ordering, and then orders results by the specified column in descending order.

thenBy

Orders by the specified column in ascending order, without clearing previous ordering.

thenByDescending

Orders by the specified column in descending order, without clearing previous ordering.

These ordering methods all take an object that implements the IQuerySelectColumn interface as their one argument.

See also