Building a simple query

Consider a simple query in SQL that returns information from a single database table. The SQL SELECT statement specifies the table. Without further restrictions, the database returns all rows and columns of information from the table.

For example, you submit the following SQL statement to a relational database.

SELECT * FROM addresses;

In response, the relational database returns a result set that contains fetched information. A result set is like a database table, with columns and rows, that contains the information that you specified with the SELECT statement. In response to the preceding SQL example code, the relational database returns a result set that has the same columns and rows as the addresses table.

The following Gosu code constructs and executes a functionally equivalent query to the preceding SQL example.
uses gw.api.database.Query     // Import the query builder APIs.

var query = Query.make(Address)
var select = query.select()
var result = select.iterator() // Execute the query and access the results with an iterator. 
Important: Using the Query Builder API to create a query is different in Java and Gosu. In Java, you must use the gw.api.database.Queries.createQuery method rather than gw.api.database.Query.make.
The following Java code is equivalent to the preceding Gosu code.
import entity.Address;
import gw.api.database.IQueryBeanResult;
import gw.api.database.Queries;
import gw.api.database.Query;
import java.util.Iterator;
...
Query<Address> query = Queries.createQuery(Address.TYPE);
IQueryBeanResult<Address> select = query.select();
Iterator result = select.iterator();

In response, the PolicyCenter application database returns a result object that contains fetched entity instances. A result object is like a Gosu collection that contains entity instances of the type that you specified with the make method and that meets any restrictions that you added. Calling the iterator method in the preceding Gosu code causes the application database to fetch all Address instances from the application database into the result object.

The query builder API can use a view entity as the primary entity type in the same way as a standard entity. A view entity can provide straightforward access to a subset of columns on primary and related entities.

See also