Hi,

I would like to create programmatically some tables (without using the code 
generator) and then execute CRUD operations on them.

My initial approach was (see the whole code here 
<https://gist.github.com/siordache/c89777eb69756b2b1295840625846cbf#file-jooqupsertv1-java>
):

    final static Table<Record> PRODUCT = table(name("PRODUCT"));
    final static Field<String> NAME = field(name("NAME"), String.class);
    final static Field<String> COLOR = field(name("COLOR"), String.class);

    public void create() {
         dsl.createTable(PRODUCT)
                .column(NAME, NAME.getDataType().nullable(false))
                .column(COLOR, COLOR.getDataType().nullable(false))
                .constraints(
                    constraint(name("PK_PRODUCT")).primaryKey(NAME)
                )
                .execute();
    }

    int upsert(String name, String color) {
        return dsl.insertInto(PRODUCT)
                .columns(NAME, COLOR)
                .values(name, color)
                .onDuplicateKeyUpdate()
                .set(COLOR, color)
                .execute();
    }

Calling the upsert() method results in an IllegalStateException: The ON 
DUPLICATE KEY IGNORE/UPDATE clause cannot be emulated when inserting into 
non-updatable tables : "PRODUCT".

The PRODUCT table is not updatable because DSL.table() creates a TableImpl 
instance and TableInstance.getPrimaryKey() always returns null.

I was able to make the code work by subclassing TableImpl and overriding 
getPrimaryKey() (see the whole code here 
<https://gist.github.com/siordache/c89777eb69756b2b1295840625846cbf#file-jooqupsertv2-java>
):  

    private static class UpsertTable extends TableImpl<Record> {
        private UniqueKey<Record> primaryKey;

        protected UpsertTable(Name name) {
            super(name);
        }

        public void setPrimaryKey(String keyName, TableField<Record,?> 
field) {
            this.primaryKey = Internal.createUniqueKey(this, keyName, 
field);
        }

        @Override
        public UniqueKey<Record> getPrimaryKey() {
            return primaryKey;
        }
    }


However, I feel uncomfortable using classes that are marked "for JOOQ 
INTERNAL USE only". Is there a better way to create updatable tables 
programmatically?

Thanks,
Serban

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