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

org.zodiac.fastorm.rdb.metadata.RDBSchemaMetadata Maven / Gradle / Ivy

The newest version!
package org.zodiac.fastorm.rdb.metadata;

import org.zodiac.fastorm.core.meta.AbstractSchemaMetadata;
import org.zodiac.fastorm.core.meta.ObjectMetadata;
import org.zodiac.fastorm.core.meta.ObjectType;
import org.zodiac.fastorm.rdb.metadata.dialect.Dialect;
import org.zodiac.fastorm.rdb.operator.builder.DefaultQuerySqlBuilder;
import org.zodiac.fastorm.rdb.operator.builder.fragments.NotFillOrNullFragmentBuilder;
import org.zodiac.fastorm.rdb.operator.builder.fragments.ddl.CommonAlterTableSqlBuilder;
import org.zodiac.fastorm.rdb.operator.builder.fragments.ddl.CommonCreateIndexSqlBuilder;
import org.zodiac.fastorm.rdb.operator.builder.fragments.ddl.CommonCreateTableSqlBuilder;
import org.zodiac.fastorm.rdb.operator.builder.fragments.ddl.CommonDropIndexSqlBuilder;
import org.zodiac.fastorm.rdb.operator.builder.fragments.term.DefaultForeignKeyTermFragmentBuilder;
import org.zodiac.fastorm.rdb.utils.FeatureUtils;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class RDBSchemaMetadata extends AbstractSchemaMetadata {

    private final List allObjectType = Arrays.asList(RDBObjectType.table, RDBObjectType.view);

    public RDBSchemaMetadata(String name) {
        {
            /*Query*/
            addFeature(DefaultQuerySqlBuilder.of(this));


            /* General query conditions. */
            addFeature(RDBFeatures.eq);
            addFeature(RDBFeatures.is);
            addFeature(RDBFeatures.gt);
            addFeature(RDBFeatures.gte);
            addFeature(RDBFeatures.lt);
            addFeature(RDBFeatures.lte);
            addFeature(RDBFeatures.like);

            addFeature(RDBFeatures.in);
            addFeature(RDBFeatures.between);
            addFeature(RDBFeatures.isNull);
            addFeature(RDBFeatures.notNull);
            addFeature(RDBFeatures.isEmpty);
            addFeature(RDBFeatures.notEmpty);
            addFeature(RDBFeatures.nEmpty);

            /*Mismatch queries support including null values.*/
            addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.not));
            addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.notEq));
            addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.nlike));
            addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.notIn));
            addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.notBetween));


            /*Automatically associate foreign key conditions.*/
            addFeature(DefaultForeignKeyTermFragmentBuilder.getInstance());


            /*Functions.*/
            addFeature(RDBFeatures.count);
            addFeature(RDBFeatures.sum);
            addFeature(RDBFeatures.max);
            addFeature(RDBFeatures.min);
            addFeature(RDBFeatures.avg);


            /* DDL */

            addFeature(CommonCreateTableSqlBuilder.INSTANCE);
            addFeature(CommonAlterTableSqlBuilder.getInstance());
            addFeature(CommonCreateIndexSqlBuilder.getInstance());
            addFeature(CommonDropIndexSqlBuilder.getInstance());

            /* Encoder and decoder factory. */
            addFeature(DefaultValueCodecFactory.getCommons());
        }

        this.setName(name);
    }

    public String getQuoteName() {
        return getDatabase()
            .getDialect()
            .quote(getName(), false);
    }

    @Override
    @SuppressWarnings("all")
    public RDBDatabaseMetadata getDatabase() {
        return ((RDBDatabaseMetadata) super.getDatabase());
    }

    public Optional getTable(String name, boolean autoLoad) {
        if (name.contains(".")) {
            return this.getDatabase()
                       .getObject(name, (schema, _name) -> schema.getTable(_name, autoLoad))
                       .map(RDBTableMetadata.class::cast);
        }
        return getObject(RDBObjectType.table, name, autoLoad);
    }

    public Optional getTable(String name) {
        return getTable(name, true);
    }

    public Mono getTableReactive(String name) {
        return getTableReactive(name, true);
    }

    public Mono getTableReactive(String name, boolean autoLoad) {
        if (name.contains(".")) {
            return this
                .getDatabase()
                .getObjectReactive(name, (schema, _name) -> schema.getTableReactive(_name, autoLoad));
        }
        return getObjectReactive(RDBObjectType.table, name, autoLoad);
    }

    public Mono getTableOrViewReactive(String name, boolean autoLoad) {
        return getTableReactive(name, autoLoad)
            .cast(TableOrViewMetadata.class)
            .switchIfEmpty(Mono.defer(() -> getViewReactive(name, autoLoad).cast(TableOrViewMetadata.class)));
    }

    public Mono getTableOrViewReactive(String name) {
        return getTableOrViewReactive(name, true);
    }

    public Mono getViewReactive(String name) {
        return getObjectReactive(RDBObjectType.view, name);
    }

    public Mono getViewReactive(String name, boolean autoLoad) {
        return getObjectReactive(RDBObjectType.view, name, autoLoad);
    }

    public Optional getView(String name, boolean autoLoad) {
        return getObject(RDBObjectType.view, name, autoLoad);
    }

    public Optional getView(String name) {
        return getView(name, true);
    }

    public void addTable(RDBTableMetadata metadata) {
        metadata.setSchema(this);
        addObject(metadata);
    }

    public Mono findTableOrViewReactive(String name) {
        return this
            .getTableOrViewReactive(name, false)
            .switchIfEmpty(getDatabase().getTableOrViewReactive(name, false));
    }

    public Optional findTableOrView(String name) {
        Optional current = getTableOrView(name, false);
        if (current.isPresent()) {
            return current;
        }
        return getDatabase().getTableOrView(name, false);
    }

    public Optional getTableOrView(String name, boolean autoLoad) {
        return Optional
            .of(getTable(name, autoLoad).map(AbstractTableOrViewMetadata.class::cast))
            .filter(Optional::isPresent)
            .orElseGet(() -> getView(name, autoLoad).map(AbstractTableOrViewMetadata.class::cast))
            .map(TableOrViewMetadata.class::cast);
    }

    public Optional getTableOrView(String name) {
        return getTableOrView(name, true);
    }

    public RDBTableMetadata newTable(String name) {
        RDBTableMetadata tableMetadata = new RDBTableMetadata(name);
        tableMetadata.setSchema(this);
        return tableMetadata;
    }

    public void loadAllTable() {
        loadMetadata(RDBObjectType.table)
            .forEach(table -> addTable(((RDBTableMetadata) table)));
    }

    public Mono loadAllTableReactive() {
        return loadMetadataReactive(RDBObjectType.table)
            .doOnNext(table -> addTable(((RDBTableMetadata) table)))
            .then();
    }

    @Override
    public List getAllObjectType() {
        return allObjectType;
    }

    public Dialect getDialect() {
        return Optional.ofNullable(getDatabase())
                       .map(RDBDatabaseMetadata::getDialect)
                       .orElseGet(() -> this
                           .getFeatures(RDBFeatureType.dialect)
                           .stream()
                           .findFirst()
                           .orElse(null));
    }

    public Optional removeTableOrView(String name) {
        return this.removeObject(RDBObjectType.table, name)
                   .map(Optional::of)
                   .orElseGet(() -> removeObject(RDBObjectType.view, name));
    }

    @Override
    public RDBSchemaMetadata clone() {
        return (RDBSchemaMetadata) super.clone();
    }

    @Override
    public String toString() {
        return "schema " +
            getName() +
            " (" + getClass().getSimpleName() + ")" +
            "\n" +
            FeatureUtils.featureToString(getFeatureList());

    }

    @Override
    protected  List loadMetadata(ObjectType type) {
        return super.loadMetadata(type)
                    .stream()
                    .map(this::metadataParsed)
                    .collect(Collectors.toList());
    }

    @Override
    protected  Flux loadMetadataReactive(ObjectType type) {
        return super.loadMetadataReactive(type)
                    .map(this::metadataParsed);
    }

    protected  T metadataParsed(T metadata) {
        if (metadata instanceof AbstractTableOrViewMetadata) {
            ((AbstractTableOrViewMetadata) metadata).setSchema(this);
        }
        return metadata;
    }

    @Override
    protected  T loadMetadata(ObjectType type, String name) {
        T metadata = super.loadMetadata(type, name);

        return this.metadataParsed(metadata);
    }

    @Override
    protected  Mono loadMetadataReactive(ObjectType type, String name) {
        return super
            .loadMetadataReactive(type, name)
            .map(this::metadataParsed);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy