Hi Ross,

Thanks a lot for your enquiry. This is an interesting topic and we've given
this some thought in the past. We had abandoned the idea at some point,
because automatic data type validation is one of those things that looks
easy at first but opens a can of worms when going further. There are a lot
of questions, like, how to do...


   - Correct validation of DOMAINs (see #681
   <https://github.com/jOOQ/jOOQ/issues/681>)
   - Correct validation in the presence of triggers
   - Correct validation in the case of expressions, such as concatenation,
   arithmetic, etc

Specifically, the latter is very hard to get "right". What happens when you
do:

UPDATE my_table
SET my_varchar_10_column = lpad(? || (SELECT substring(abc || ?, 3, 7) FROM
my_other_table), 5)


Should we go through the AST and evaluate the resulting VARCHAR length of
the expression? Or, should we just ignore these cases and provide
functionality only in the "easy" case:

UPDATE my_table
SET my_varchar_10_column = ?

Is this still an "easy" case?


UPDATE my_table
SET my_varchar_10_column = CAST(? AS VARCHAR(10))


However, I do like the idea of having at least a way to compare a DataType
with an actual value. While jOOQ may not be able to provide general
automatic validation, you as a user might implement *some* validation at
the client side... We'll track these ideas here:
https://github.com/jOOQ/jOOQ/issues/4543


For the record, here's also my answer that I have given to your question on
Stack Overflow (http://stackoverflow.com/a/32558080/521799):

In jOOQ, is there any way to determine if a value can be used for a Field /
DataType?

No such feature exists as of jOOQ 3.6. There's a new feature request for
this, now: #4543 <https://github.com/jOOQ/jOOQ/issues/4543>.

Also, is it possible for UpdateSetStep#set(Field field, T value)
<http://www.jooq.org/javadoc/latest/org/jooq/UpdateSetStep.html#set-org.jooq.Field-org.jooq.Field->
(and
similar methods) to throw an exception if the field cannot accept the value?

You won't be able to override the behaviour of set(). But you could add
your own data type Binding implementations to all data types, and implement
your validation when binding variables to the underlying JDBC statement.

For more info about data type bindings, see:
http://www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings/

2015-09-12 19:47 GMT+02:00 Ross Goldberg <[email protected]>:

> Is there any way to determine if a value can be used for a Field /
> DataType?
>
> e.g.:
>
> Field f = <a non-nullable varchar(2)>;
>
> assert f.accepts("");
> assert f.accepts("a");
> assert f.accepts("ab");
>
> assert ! f.accepts("abc");
> assert ! f.accepts(null);
> assert ! f.accepts();
>
> Field g = <a nullable unsigned byte>;
>
> assert g.accepts(null);
> assert g.accepts(0);
> assert g.accepts(1);
>
> assert ! g.accepts("");
> assert ! g.accepts("a");
> assert ! g.accepts(-1);
> assert ! g.accepts(999999);
>
> If something like this exists, is it strict, or converting?
>
> e.g.:
>
> if strict, a varchar(2) column would not accept an Integer 1, but would
> accept a String "1"
>
> if converting, a varchar(2) column would accept an Integer 1
>
> Also, is it possible for UpdateSetStep#set(Field field, T value) (and
> similar methods) to throw an exception if the field cannot accept the
> value?  (for any reason, like nullability, data type, length, precision,
> scale, etc.)
>
> --
> 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.
>

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