You can pass in a data type to your fields when you're creating them.
I haven't tested this in a compiler so might be some syntax mishaps here
and there, but I've used this pattern before. Just pass in the class type
explicitly and the Result you get back should be typed based on the fields
you're requesting.
ie.
Result*<Record3<String, Integer, Integer>>* result = context.select(
userInput.field("name", *String.class*),
userInput.field("a", *Integer.class*),
userInput.field("b", *Integer.class*),
MY_TABLE.SOME_FIELD
).from(MY_TABLE).crossJoin(userInput).where(
MY_TABLE.ID_FIELD.equal(userInput.field("name"))
).fetch();
On Tue, Apr 7, 2015 at 5:35 PM, Ed Erwin <[email protected]> wrote:
> I would like to perform a join between a database table and a table of
> user-given custom values. What is a good way to do this in JOOQ?
>
> I have figured out how to do it, but the code does not maintain all the
> type safety that I would prefer.
>
> DSLContext context = createDSLContext();
>
> // add the user data to a List (in reality this is done in a loop)
> List<Row3<String, Integer, Integer>> rowList = new ArrayList<>();
> rowList.add(DSL.row("A", 35, 120));
> rowList.add(DSL.row("B", 52, 375));
> rowList.add(DSL.row("C", 19, 85));
>
> // convert that data to a table
> Row3[] rows = rowList.toArray(new Row3[rowList.size()]);
> Table userInput = createDSLContext().select().from(values(rows))
> .asTable("userInput", "name", "a", "b");
>
> // join user input with data in table MY_TABLE
> Result result = context.select(
> userInput.field("name"),
> userInput.field("a"),
> userInput.field("b"),
> MY_TABLE.SOME_FIELD
> ).from(MY_TABLE).crossJoin(userInput).where(
> MY_TABLE.ID_FIELD.equal(userInput.field("name"))
> ).fetch();
>
>
> This code works, but it bothers me that if I try to grab a field of my
> temporary table, it does not know anything about the data types. It does
> not know, for example, that userInput.field("name") is a String data field.
>
> Is there a more recommended way of adding-in user input values?
>
> --
> You received this message because you are subscribed to the Google Groups
> "jOOQ User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
Thank you
Samir Faci
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.