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

db.sql.api.impl.cmd.CmdFactory Maven / Gradle / Ivy

There is a newer version: 1.7.6-RC2
Show newest version
package db.sql.api.impl.cmd;


import db.sql.api.Cmd;
import db.sql.api.Getter;
import db.sql.api.cmd.GetterField;
import db.sql.api.cmd.ICmdFactory;
import db.sql.api.cmd.basic.IDataset;
import db.sql.api.cmd.basic.IDatasetField;
import db.sql.api.impl.cmd.basic.AllField;
import db.sql.api.impl.cmd.basic.DatasetField;
import db.sql.api.impl.cmd.basic.Table;
import db.sql.api.impl.cmd.basic.TableField;
import db.sql.api.impl.tookit.LambdaUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;


public class CmdFactory implements ICmdFactory {

    protected final Map tableCache = new HashMap<>(5);

    private final String tableAsPrefix;

    protected int tableNums = 0;

    public CmdFactory() {
        this("t");
    }

    public CmdFactory(String tableAsPrefix) {
        this.tableAsPrefix = tableAsPrefix;
    }

    protected String tableAs(int storey, int tableNums) {
        return this.tableAsPrefix +
                (tableNums == 1 ? "" : tableNums);
    }

    public Table cacheTable(Class entity, int storey) {
        return this.tableCache.get(String.format("%s.%s", entity.getName(), storey));
    }


    @Override
    public Table table(Class entity, int storey) {
        return tableCache.computeIfAbsent(entity.getName(), key -> {
            Table table = new Table(entity.getSimpleName());
            table.as(tableAs(storey, ++tableNums));
            return table;
        });
    }

    @Override
    public Table table(String tableName) {
        return new Table(tableName);
    }

    @Override
    public  String columnName(Getter column) {
        return LambdaUtil.getName(column);
    }

    @Override
    public  TableField field(Getter column, int storey) {
        LambdaUtil.LambdaFieldInfo fieldInfo = LambdaUtil.getFieldInfo(column);
        return this.field(fieldInfo.getType(), 1, fieldInfo.getName());
    }

    @Override
    public  TableField[] fields(int storey, Getter... columns) {
        TableField[] tableFields = new TableField[columns.length];
        for (int i = 0; i < columns.length; i++) {
            tableFields[i] = field(columns[i], storey);
        }
        return tableFields;
    }

    @Override
    @SafeVarargs
    public final TableField[] fields(GetterField... getterFields) {
        TableField[] tableFields = new TableField[getterFields.length];
        for (int i = 0; i < getterFields.length; i++) {
            GetterField getterField = getterFields[i];
            tableFields[i] = field(getterField.getGetter(), getterField.getStorey());
        }
        return tableFields;
    }

    public  TableField field(Table table, Getter column) {
        return new TableField(table, columnName(column));
    }

    @Override
    public TableField field(Class entity, String filedName, int storey) {
        return this.field(entity, storey, filedName);
    }

    public TableField field(Table table, String columnName) {
        return new TableField(table, columnName);
    }

    @Override
    public , DATASET_FIELD extends IDatasetField> DATASET_FIELD field(IDataset dataset, Getter column) {
        String filedName = LambdaUtil.getName(column);
        return (DATASET_FIELD) new DatasetField(dataset, filedName);
    }

    @Override
    public , DATASET_FIELD extends IDatasetField> DATASET_FIELD field(IDataset dataset, String columnName) {
        if (dataset instanceof Table) {
            return (DATASET_FIELD) new TableField((Table) dataset, columnName);
        }
        return (DATASET_FIELD) new DatasetField(dataset, columnName);
    }

    @Override
    public , DATASET_FIELD extends IDatasetField> DATASET_FIELD allField(IDataset dataset) {
        return (DATASET_FIELD) new AllField(dataset);
    }

    @Override
    public  R create(Getter column, int storey, Function RF) {
        return RF.apply(this.field(column, storey));
    }

    protected TableField field(Class clazz, int storey, String filedName) {
        Table table = table(clazz, storey);
        return new TableField(table, filedName);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy