Class DataTablesQuery
request bean onto a BeanQuery, runs it against a
BeanQuerySession, and copies the result onto a DataTablesResults response bean.
This adapter is the only place the two worlds meet: the DataTables JSON beans
(org.apache.juneau.console.datatables) know nothing about the query engine, and the engine knows
nothing about DataTables JSON. The mapping (toBeanQuery(DataTablesRequest, BeanQueryContext)):
- Each
columns[i].dataresolves againstcontext.columns(): a declared name is used as is, an all-digits string is the n-th declared column, andnull/""is skipped — resolution only happens for a column something actually uses (a non-blank per-column search, anorder[]entry, or global-search eligibility), so a render-only column with an unresolvabledatanever errors. - Each searchable column's non-blank
search.valuebecomes one rawcolumn=expressioncondition (BeanQuery.Builder#search(String, String)); two DataTables columns resolving to the same key merge into one$and. - A non-blank global
search.valuebecomes a top-level$orof one leaf per eligible column (searchable, resolvable), the leaf chosen by that column'sSearchType:$containsforSearchType.TEXT/SearchType.ID,$prefixforSearchType.VERSION,$eqicforSearchType.ENUM, and$eqforSearchType.NUMERIC/SearchType.TIMESTAMP/SearchType.BOOLEANwhensearch.valueparses for that type — a parse failure excludes just that column from this request's$or, it never fails the request. Resolved keys are de-duplicated in column order; one eligible column collapses to a plain leaf (a single-item group is unwrapped); an eligible key that also has a per-column search merges into that column's$and. - Each
order[i]resolves throughcolumns[i]to asort/sortDescentry; duplicate keys keep the first occurrence. start/lengthbecome thepagewindow (length=-1means all rows; a negativestartis clamped to0).- Counts are always requested (
CountRequest.BOTH); the context's ownCountPolicystill wins.
columns.size() and order.size() are capped by BeanQueryContext.getMaxSearchClauses() /
BeanQueryContext.getMaxSortKeys() before any column is resolved. A caller mistake — an
unknown column, too many search clauses, too many sort keys, a malformed expression, a disallowed operator, a
restricted column — raises BeanQuerySyntaxException, which run / runArrays do not catch:
it propagates to RestContext.convertThrowable, which maps it to HTTP 400 with the server's message
in the X-BeanQuery-Error response header (BeanQueryRequest.ERROR_HEADER) — the same contract as
every other queryable endpoint. (It is not a 200 response carrying an error field.)
The bundled juneau-datatables.js reads that header and shows the message through DataTables' errMode
instead of a generic "Ajax error". A
BeanQueryExecutionException (a genuine server fault) propagates unchanged to the REST layer's 500 mapping.
Example:
A caller that needs to add its own condition (e.g. a tenant guard) builds the query directly and merges it with
BeanQuery.copy():
BeanQuery
- Since:
- 10.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> DataTablesResults<T>run(DataTablesRequest request, BeanQuerySession<T> session) Runs the DataTables request against the session and builds the response envelope.static DataTablesResults<List<Object>>runArrays(DataTablesRequest request, BeanQuerySession<?> session) Runs the DataTables request against the session and builds the response envelope with array rows.static BeanQuerytoBeanQuery(DataTablesRequest request, BeanQueryContext<?> context) Translates a DataTables request into an engineBeanQuery(search, sort, page window, counts) against the given context's declared columns.
-
Method Details
-
run
Runs the DataTables request against the session and builds the response envelope.The caller owns the session (open it in a try-with-resources block); this method neither opens nor closes it, so the adapter works identically over an in-memory or a SQL engine. Rows come back as beans (
T); userunArrayswhencolumns[i].dataare integers and the table expects array rows.- Type Parameters:
T- The row type.- Parameters:
request- The DataTables request bean. Must not benull .session- The open bean-query session. Must not benull .- Returns:
- The populated DataTables response envelope.
- Throws:
BeanQuerySyntaxException- Ifrequestdescribes an invalid query. Mapped centrally to HTTP 400 with anX-BeanQuery-Errorheader.
-
runArrays
public static DataTablesResults<List<Object>> runArrays(DataTablesRequest request, BeanQuerySession<?> session) Runs the DataTables request against the session and builds the response envelope with array rows.For a table whose
columns[i].dataare integers (so there is no bean property to put named fields on): the view is set to every column incontext.columns()order, rows come back fromfindValues, and each rowMapis flattened to aListin that same order, sodata: nand the n-th array cell refer to the same column.- Parameters:
request- The DataTables request bean. Must not benull .session- The open bean-query session. Must not benull .- Returns:
- The populated DataTables response envelope.
- Throws:
BeanQuerySyntaxException- Ifrequestdescribes an invalid query (seerun).
-
toBeanQuery
Translates a DataTables request into an engineBeanQuery(search, sort, page window, counts) against the given context's declared columns.- Parameters:
request- The DataTables request bean. Must not benull .context- The context the query will run against. Must not benull . Itscolumns()/getColumnType(String)/getMaxSearchClauses()/getMaxSortKeys()drive every resolution and cap below. A session that narrows its columns (restrictColumns) is not visible here — the query pipeline still rejects a restricted column, reported through the sameBeanQuerySyntaxExceptionpath.- Returns:
- The equivalent query.
- Throws:
BeanQuerySyntaxException- Ifcolumns/orderexceed the context's caps, or a search/sort target names an unresolvable column.
-