Sending the DEFAULT placeholder via JDBC?

前端 未结 2 1103
余生分开走
余生分开走 2020-12-20 16:07

Is there any way, via JDBC, to send the DEFAULT placeholder explicitly, like in INSERT INTO sometables VALUES (blah, DEFAULT)? (I\'m almost certain

相关标签:
2条回答
  • 2020-12-20 16:27

    default isn't a value literal - it's a pseudocolumn (or at least I hope that's what they're called in postgres). Since it isn't a value but a part of insert's syntax, it can't be bound to a placeholder, just like the table name cannot.

    0 讨论(0)
  • 2020-12-20 16:45

    There is no standard way of doing this. As far as I know the SQL standard does not support a mechanism for declaring to use the DEFAULT through parameters. The SQL standard seems to assume that each INSERT is crafted for its specific purpose. So declaring DEFAULT can only be done in the insert statement itself and not as a value for a parameter. In these kinds of decisions, the JDBC specification usually follows the SQL standard.

    The only method you currently have is to create a vendor-specific method in the JDBC driver, for example as you specified in your question itself, but you could also think of something like:

    To clarify: below is an example of how a driver implementation could solve it, it doesn't actually work like this.

    Using setNull(int, int)

    setNull(idx, PostgresTypes.DEFAULT_VALUE)
    

    or using setNull(int, int, String)

    setNull(idx, Types.<correct-field-type>, Postgres.DEFAULT_VALUE_MARKER)
    

    However this assumes that PostgreSQL actually has a method of specifying DEFAULT through parameters, or that the driver implementation will parse and recreate the statement for each set of received parameters so that it can declare a literal DEFAULT.

    I don't know if there is any driver that currently supports such a workaround.

    0 讨论(0)
提交回复
热议问题