Class SqlBeanQueryContext<T>

java.lang.Object
org.apache.juneau.commons.beanquery.BeanQueryContext<T>
org.apache.juneau.beanquery.sql.SqlBeanQueryContext<T>
Type Parameters:
T - The row (bean) type.
All Implemented Interfaces:
ColumnResolver

public final class SqlBeanQueryContext<T> extends BeanQueryContext<T> implements ColumnResolver
The shared JDBC BeanQueryContext: renders validated queries into SQL for a SqlDialect and runs them on a connection from a configured source.

The engine is dialect-agnostic. Identifier quoting, the built-in operator predicates and pagination come from the SqlDialect (Juneau ships juneau-beanquery-postgres). Columns are always explicit: the class passed to create(Class) fixes T (and is returned by getType()); it is introspected only to choose the default row mapper, never to derive columns.

If no rowMapper is set, SqlBeanQueryContext.Builder.build() picks a default from the row type T: a Map type that a LinkedHashMap is assignable to gets SqlRowMapper.map(), and any other type that SqlRowMapper.bean(Class) can plan (a record, or a class with a public no-arg constructor and setters) gets SqlRowMapper.bean(Class). Object, other Map types (such as TreeMap) and unplannable types get no default; find and stream then throw IllegalStateException, with the reason appended when there is one. An explicit rowMapper(...) always wins, and findValues, streamValues and count never need one.

renderRows(ResolvedQuery) and renderCount(ResolvedQuery, boolean) open no connection, so the generated SQL and binds can be inspected and unit-tested. A guard fragment is AND-ed, in parentheses, into every statement.

Example:

SqlBeanQueryContext<Person> context = SqlBeanQueryContext .create(Person.class) .dialect(PostgresDialect.INSTANCE) .table("person") .column("name", SearchType.TEXT) .column("age", SearchType.NUMERIC) .columnSql("name", "full_name") .guard(SqlFragment.of("\"deleted\" = false")) .rowMapper((rs, cols) -> new Person(rs.getString("full_name"), rs.getInt("age"))) .dataSource(dataSource) .queryTimeout(Duration.ofSeconds(5)) .build(); try (SqlBeanQuerySession<Person> session = context.getSession()) { Page<Person> page = session.find(query); } // No rowMapper(...): Person is a record, so build() defaults to SqlRowMapper.bean(Person.class). SqlBeanQueryContext<Person> defaulted = SqlBeanQueryContext.create(Person.class) .dialect(PostgresDialect.INSTANCE) .table("person") .column("name", SearchType.TEXT) .column("age", SearchType.NUMERIC) .dataSource(dataSource) .build(); // A Map row type defaults to SqlRowMapper.map(). SqlBeanQueryContext<Map> maps = SqlBeanQueryContext.create(Map.class) // ...dialect, table, columns, connection source... .build();

Since:
10.0.0