2015-04-08 2:35 GMT+02:00 Ed Erwin <[email protected]>:

>     Table userInput = createDSLContext().select().from(values(rows))
>         .asTable("userInput", "name", "a", "b");


On a side-note: you could also write this as

Table userInput = values(rows).as("userInput", "name", "a", "b");


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?
>

Unfortunately, there is no way in Java to associate generic type
information with a string literal, the way you were using it. One option
would of course be to redundantly apply the type information when
dereferencing the fields, as Samir suggested:

 Result*<Record3<String, Integer, Integer>>* result = context.select(
>         userInput.field("name", *String.class*),
>         userInput.field("a", *Integer.class*),
>         userInput.field("b", *Integer.class*)


Another option would be to keep a hold of a Row3<String, Integer, Integer>
reference, and use its field1(), field2() and field3() methods to get the
DataType<T> or Class<T> reference. The drawback of this approach is the
usual drawback of using positional column references, rather than named
ones.

A third option would be to declare actual Fields:

Table<?> userInput = DSL.tableByName("userInput");

Field<String> name = DSL.fieldByName(String.class, userInput.getName(),
"name");
Field<Integer> a = DSL.fieldByName(Integer.class, userInput.getName(), "a");
Field<Integer> b = DSL.fieldByName(Integer.class, userInput.getName(), "b");


You could then write


// join user input with data in table MY_TABLE
    Result result = context.select(
        name,
        a,
        b,
        MY_TABLE.SOME_FIELD
    ).from(MY_TABLE).crossJoin(values(rows).as(userInput.getName(),
name.getName(), a.getName(), b.getName())).where(
        MY_TABLE.ID_FIELD.equal(name)
    ).fetch();


We have a pending feature request that aims for improving writing views in
jOOQ, but we're not quite sure how to design that API yet:
https://github.com/jOOQ/jOOQ/issues/1969

-- 
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.

Reply via email to