All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenshoo.jooq.AbstractDataTable Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.jooq;

import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.jooq.*;
import org.jooq.impl.AbstractKeys;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;

import java.util.*;

public abstract class AbstractDataTable> extends TableImpl implements DataTable {

    private final List> primaryKeyFields = Lists.newArrayList();
    private final Supplier> primaryKeySupplier = Suppliers.memoize(new PrimaryKeySupplier());
    private final Multimap, FieldsPair> foreignKeyFields = ArrayListMultimap.create();
    private final Supplier>> foreignKeysSupplier = Suppliers.memoize(new ForeignKeysSupplier());
    private final Supplier> identitySupplier = Suppliers.memoize(new IdentitySupplier());
    private TableField identityField;

    protected AbstractDataTable(String tableName) {
        super(tableName);
    }

    protected AbstractDataTable(T aliased, String alias) {
        super(alias, null, aliased);
    }

    protected AbstractDataTable(String tableName, Schema schema) {
        super(tableName, schema);
    }

    public abstract T as(String alias);

    protected final  TableField createPKField(String name, DataType type) {
        TableField field = createField(name, type);
        primaryKeyFields.add(field);
        if(type.identity()) {
            this.identityField = field;
        }
        return field;
    }

    protected final  TableField createPKAndFKField(String name, DataType type, TableField referenceField) {
        final TableField field = createPKField(name, type);
        foreignKeyFields.put(referenceField.getTable(), new FieldsPair(field, referenceField));
        return field;
    }

    protected final  TableField createFKField(String name, TableField referenceField) {
        TableField field = createField(name, referenceField.getDataType().identity(false));
        foreignKeyFields.put(referenceField.getTable(), new FieldsPair(field, referenceField));
        return field;
    }

    @Override
    public Collection> getVirtualPartition() {
        return Collections.emptyList();
    }

    @Override
    public UniqueKey getPrimaryKey() {
        return primaryKeySupplier.get();
    }

    @Override
    public List> getReferences() {
        return foreignKeysSupplier.get();
    }

    @Override
    public Identity getIdentity() {
        return identitySupplier.get();
    }

    private class Keys extends AbstractKeys {
        UniqueKey createPK() {
            if(primaryKeyFields.isEmpty()) {
                return null;
            }
            //noinspection unchecked
            TableField[] pkFields = primaryKeyFields.toArray(new TableField[primaryKeyFields.size()]);
            return createKey(pkFields);
        }

        UniqueKey createKey(TableField[] pkFields) {
            return createUniqueKey(AbstractDataTable.this, pkFields);
        }

        ForeignKey createFK(UniqueKey targetKey, Table table, TableField[] fields) {
            return createForeignKey(targetKey, table, fields);
        }

        Identity createPKIdentity() {
            return identityField != null ? createIdentity(AbstractDataTable.this, identityField) : null;
        }


    }

    private static class FieldsPair {
        final TableField source;
        final TableField target;
        FieldsPair(TableField source, TableField target) {
            this.source = source;
            this.target = target;
        }
    }

    private class IdentitySupplier implements Supplier> {
        @Override
        public Identity get() {
            return new Keys().createPKIdentity();
        }
    }

    private class PrimaryKeySupplier implements Supplier> {
        @Override
        public UniqueKey get() {
            return new Keys().createPK();
        }
    }

    private class ForeignKeysSupplier implements Supplier>> {
        private final Function> FIELDS_PAIR_SOURCE = fieldsPair -> fieldsPair.source;
        private final Function> FIELDS_PAIR_TARGET = fieldsPair -> fieldsPair.target;

        @Override
        public List> get() {
            return Lists.newArrayList(Collections2.transform(foreignKeyFields.asMap().entrySet(), (Function, Collection>, ForeignKey>) entry -> {
                //noinspection unchecked
                TableField[] sourceFields = getTableFields(entry, FIELDS_PAIR_SOURCE);
                //noinspection unchecked
                TableField[] targetFields = getTableFields(entry, FIELDS_PAIR_TARGET);
                AbstractDataTable targetTable = (AbstractDataTable) entry.getKey();
                UniqueKey targetKey = targetTable.new Keys().createKey(targetFields);
                return new Keys().createFK(targetKey, entry.getKey(), sourceFields);
            }));
        }

        private TableField[] getTableFields(Map.Entry, Collection> entry, Function> extractor) {
            return Iterables.toArray(Collections2.transform(entry.getValue(), extractor), TableField.class);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy