Update docs
Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/b1f9ad5b Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/b1f9ad5b Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/b1f9ad5b Branch: refs/heads/master Commit: b1f9ad5b333442e6e51286fa1a3379f83919411c Parents: 18c72d3 Author: Arseni Bulatski <ancars...@gmail.com> Authored: Wed Oct 24 12:57:26 2018 +0300 Committer: Arseni Bulatski <ancars...@gmail.com> Committed: Wed Oct 24 13:23:25 2018 +0300 ---------------------------------------------------------------------- .../asciidoc/_cayenne-guide/part2/queries.adoc | 31 ++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/b1f9ad5b/docs/asciidoc/cayenne-guide/src/docs/asciidoc/_cayenne-guide/part2/queries.adoc ---------------------------------------------------------------------- diff --git a/docs/asciidoc/cayenne-guide/src/docs/asciidoc/_cayenne-guide/part2/queries.adoc b/docs/asciidoc/cayenne-guide/src/docs/asciidoc/_cayenne-guide/part2/queries.adoc index c93076f..0230a54 100644 --- a/docs/asciidoc/cayenne-guide/src/docs/asciidoc/_cayenne-guide/part2/queries.adoc +++ b/docs/asciidoc/cayenne-guide/src/docs/asciidoc/_cayenne-guide/part2/queries.adoc @@ -296,8 +296,9 @@ Artist artistWithId1 = SelectById.query(Artist.class, 1) `SQLSelect` and `SQLExec` are essentially a "fluent" versions of older `SQLTemplate` query. `SQLSelect` can be used (as name suggests) to select custom data in form of entities, -separate columns or collection of `DataRow`. `SQLExec` is designed to just execute any raw SQL code -(e.g. updates, deletes, DDLs, etc.) This queries support all directives described in <<SQLTemplate>> section. +separate columns, collection of `DataRow` or Object[]. `SQLExec` is designed to just execute any raw SQL code +(e.g. updates, deletes, DDLs, etc.) This queries support all directives described in <<SQLTemplate>> section. Also you can predefine +result type of columns. Here is example of how to use SQLSelect: @@ -317,6 +318,16 @@ List<String> paintingNames = SQLSelect .scalarQuery(String.class, "SELECT PAINTING_TITLE FROM PAINTING WHERE ESTIMATED_PRICE > #bind($price)") .params("price", 100000) .select(context); + +// Selecting DataRow with predefined types +List<DataRow> result = SQLSelect + .dataRowQuery("SELECT * FROM ARTIST_CT", Integer.class, String.class, LocalDateTime.class) + .select(context); + +// Selecting Object[] with predefined types +List<Object[]> result = SQLSelect + .scalarQuery("SELECT * FROM ARTIST_CT", Integer.class, String.class, LocalDateTime.class) + .select(context); ---- @@ -873,6 +884,22 @@ query.setResult(resultDescriptor); List<Object[]> data = context.performQuery(query); ---- +You can fetch list of scalars, list of Object[] or list of DataRow with predefined result column types or using default types. +[source, Java] +---- +// Selecting Object[] with predefined types +SQLTemplate q3 = new SQLTemplate(Artist.class, "SELECT ARTIST_ID, ARTIST_NAME FROM ARTIST"); + q3.setResultColumnsTypes(Double.class, String.class); + q3.setUseScalar(true); +List<Object[]> result = context.performQuery(q3); + +// Selecting DataRow with predefined types +SQLTemplate q3 = new SQLTemplate(Artist.class, "SELECT ARTIST_ID, ARTIST_NAME FROM ARTIST"); + q3.setResultColumnsTypes(Double.class, String.class); + q3.setFetchingDataRows(true); +List<DataRow> result = context.performQuery(q3); +---- + Another trick related to mapping result sets is making Cayenne recognize prefetched entities in the result set. This emulates "joint" prefetching of `SelectQuery`, and is achieved by special column naming. Columns belonging to the "root" entity of the query should use unqualified names corresponding to the root `DbEntity` columns.