public class DataProvidersKt
| Modifier and Type | Method and Description |
|---|---|
static <T extends KEntity<?>> |
asDataProvider(com.github.mvysny.vokdataloader.DataLoader<T> $receiver)
Returns a VokDataProvider which loads data from this DataLoader.
|
static <T extends KEntity<?>> |
setDataLoader(com.vaadin.flow.component.grid.Grid<T> $receiver,
com.github.mvysny.vokdataloader.DataLoader<T> dataLoader)
Sets given data loader to this Grid, by the means of wrapping the data loader via DataLoaderAdapter and setting it
as a (configurable) Grid.getDataProvider.
|
static <T> com.vaadin.flow.data.provider.ConfigurableFilterDataProvider<T,com.github.mvysny.vokdataloader.Filter,com.github.mvysny.vokdataloader.Filter> |
sqlDataProvider(java.lang.Class<T> clazz,
java.lang.String sql,
java.util.Map<java.lang.String,? extends java.lang.Object> params,
kotlin.jvm.functions.Function1<? super T,? extends java.lang.Object> idMapper)
Allows the coder to write any SQL he wishes. This provider must be simple enough to not to get in the way by smart (complex) Kotlin language features.
It should support any SQL select, but should allow for adding custom filters and orderings (since this is plugged into Grid after all).
|
public static <T> com.vaadin.flow.data.provider.ConfigurableFilterDataProvider<T,com.github.mvysny.vokdataloader.Filter,com.github.mvysny.vokdataloader.Filter> sqlDataProvider(java.lang.Class<T> clazz,
java.lang.String sql,
java.util.Map<java.lang.String,? extends java.lang.Object> params,
kotlin.jvm.functions.Function1<? super T,? extends java.lang.Object> idMapper)
Allows the coder to write any SQL he wishes. This provider must be simple enough to not to get in the way by smart (complex) Kotlin language features. It should support any SQL select, but should allow for adding custom filters and orderings (since this is plugged into Grid after all).
The provider is bound to a holder class which holds the values (any POJO). vok-orm is used to map the result set to the class. For example:
data class CustomerAddress(val customerName: String, val address: String)
val provider = sqlDataProvider(CustomerAddress::class.java, """select c.name as customerName, a.street || ' ' || a.city as address
from Customer c inner join Address a on c.address_id=a.id where 1=1 {{WHERE}} order by 1=1{{ORDER}} {{PAGING}}""", idMapper = { it })
(Note how select column names must correspond to field names in the CustomerAddress class)
Now sqlDataProvider can hot-patch the where clause computed from Grid's filters into {{WHERE}} (as a simple string replacement),
and the order by and offset/limit into the {{ORDER}} and {{PAGING}}, as follows:
{{WHERE}} will be replaced by something like this: "and name=:pqw5as and age>:p123a" - note the auto-generated parameter
names starting with p. If there are no filters, will be replaced by an empty string.
{{ORDER}} will be replaced by ", customerName ASC, street ASC" or by an empty string if there is no ordering requirement.
{{PAGING}} will be replaced by "offset 0 limit 100" or by an empty string if there are no limitations.
Note that the Grid will display fields present in the CustomerAddress holder class and will also auto-generate filters
for them, based on the type of the field.
No bloody annotations! Work in progress. It is expected that a holder class is written for every select, tailored to show the outcome of that particular select.
T - the type of the holder class.clazz - the type of the holder class which will hold the resultsql - the select which can map into the holder class (that is, it selects columns whose names match the holder class fields). It should contain
{{WHERE}}, {{ORDER}} and {{PAGING}} strings which will be replaced by a simple substring replacement.params - the sql may be parametrized; this map holds all the parameters present in the sql itself.idMapper - returns the primary key which must be unique for every row returned. If the holder class is a data class and/or has proper equals/hashcode, the class itself may act as the key; in such case
just pass in identity here: { it }public static <T extends KEntity<?>> void setDataLoader(com.vaadin.flow.component.grid.Grid<T> $receiver, com.github.mvysny.vokdataloader.DataLoader<T> dataLoader)
Sets given data loader to this Grid, by the means of wrapping the data loader via DataLoaderAdapter and setting it as a (configurable) Grid.getDataProvider.
public static <T extends KEntity<?>> com.vaadin.flow.data.provider.ConfigurableFilterDataProvider<T,com.github.mvysny.vokdataloader.Filter,com.github.mvysny.vokdataloader.Filter> asDataProvider(com.github.mvysny.vokdataloader.DataLoader<T> $receiver)
Returns a VokDataProvider which loads data from this DataLoader.