2017-08-09 14 views
6
public class DestinationCustomBinding implements Binding<Object, Destination>{ 


    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private final Converter<Object, Destination> converter = new DestinationConverter(); 

    public Converter<Object, Destination> converter() { 
     // TODO Auto-generated method stub 

     return converter; 
    } 

    public void sql(BindingSQLContext<Destination> ctx) throws SQLException { 
     // TODO Auto-generated method stub 
     Param<Integer> param = DSL.val(ctx.convert(converter).value(),Integer.class); 

     ctx.render().visit(DSL.val(ctx.convert(converter).value(),Integer.class)); 
    } 

    public void register(BindingRegisterContext<Destination> ctx) throws SQLException { 
     // TODO Auto-generated method stub 
     ctx.statement().registerOutParameter(ctx.index(), Types.JAVA_OBJECT); 
    } 

    public void set(BindingSetStatementContext<Destination> ctx) throws SQLException { 


     ctx.statement().setObject(ctx.index(), ctx.convert(converter).value(), null); 
    //  ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter).value(), null)); 
    } 

    public void set(BindingSetSQLOutputContext<Destination> ctx) throws SQLException { 
     // TODO Auto-generated method stub 
     throw new SQLFeatureNotSupportedException(); 
    } 

    public void get(BindingGetResultSetContext<Destination> ctx) throws SQLException { 
     // TODO Auto-generated method stub 
     ctx.convert(converter).value(ctx.resultSet().getObject(ctx.index())); 
    } 

    public void get(BindingGetStatementContext<Destination> ctx) throws SQLException { 
     // TODO Auto-generated method stub 
     ctx.convert(converter).value(ctx.statement().getObject(ctx.index())); 
    } 

    public void get(BindingGetSQLInputContext<Destination> ctx) throws SQLException { 
     // TODO Auto-generated method stub 
     throw new SQLFeatureNotSupportedException(); 
    } 

} 

tôi muốn sử dụng setObject thay vì setString trong public void set (BindingSetStatementContext ctx), Nhưng tôi nhận được lỗi saukhông thể sử dụng setobject cho tùy chỉnh ràng buộc trong jooq

Caused by: java.sql.SQLFeatureNotSupportedException: setObject not implemented 
    at java.sql.PreparedStatement.setObject(PreparedStatement.java:1291) 
    at org.jooq.tools.jdbc.DefaultPreparedStatement.setObject(DefaultPreparedStatement.java:371) 
    at com.shn.analytics.db.connection.utils.DestinationCustomBinding.set(DestinationCustomBinding.java:53) 
    at org.jooq.impl.DefaultBindContext.bindValue0(DefaultBindContext.java:62) 
    at org.jooq.impl.AbstractBindContext.bindValue(AbstractBindContext.java:127) 
    ... 11 more 

Sử dụng trường hợp: tôi đang sử dụng db thùng mà chấp nhận một Json tương tự (không Json) các đối tượng mà không có dấu ngoặc kép Ex:

create table test_table ( 
    Id Integer, 
    name STRING, 
    test_Object OBJECT 
); 

insert into test_table(Id, test_Object) 
values (10, 'test_Name', {city = 'random_city'}); 

Làm thế nào có thể thực hiện trường hợp sử dụng này trong jooq

01.235.
+0

Bạn đang sử dụng cơ sở dữ liệu/trình điều khiển JDBC nào? –

+0

tôi đang sử dụng crate db (https://crate.io/). Trình điều khiển jdbc là (https://crate.io/docs/clients/jdbc/) postgres và tôi đang sử dụng nguồn dữ liệu HikariCP để nhận các kết nối – Pradeep

+0

Định nghĩa của 'DestinationConverter' là gì? – aristotll

Trả lời

3

Trong nhiều trình điều khiển JDBC, bạn không thể sử dụng PreparedStatement.setObject(index, null) vì trình điều khiển JDBC cần biết những gì loại của NULL nó phải nối tiếp qua dây tới máy chủ. Tôi không biết những gì crate.io hy vọng đây (nó không phải là một cơ sở dữ liệu hỗ trợ chính thức trong jOOQ), nhưng các tùy chọn thông thường là:

// Using String 
stmt.setString(index, null); 

// Use setNull() 
stmt.setNull(index, Types.OTHER); 

Trong trường hợp của phương pháp setNull(), nó cũng có thể là có một giá trị Types dành riêng cho nhà cung cấp, cụ thể hơn Types.OTHER.

Các vấn đề liên quan