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

com.easy.query.core.proxy.AbstractBaseProxyEntity Maven / Gradle / Ivy

There is a newer version: 2.0.106
Show newest version
package com.easy.query.core.proxy;

import com.easy.query.api.proxy.entity.select.impl.EasyEntityQueryable;
import com.easy.query.core.annotation.Nullable;
import com.easy.query.core.basic.api.select.ClientQueryable;
import com.easy.query.core.context.EmptyQueryRuntimeContext;
import com.easy.query.core.context.QueryRuntimeContext;
import com.easy.query.core.enums.RelationTypeEnum;
import com.easy.query.core.exception.EasyQueryInvalidOperationException;
import com.easy.query.core.expression.parser.core.available.TableAvailable;
import com.easy.query.core.expression.parser.core.base.SimpleEntitySQLTableOwner;
import com.easy.query.core.expression.sql.builder.EntityExpressionBuilder;
import com.easy.query.core.metadata.NavigateMetadata;
import com.easy.query.core.proxy.available.EntitySQLContextAvailable;
import com.easy.query.core.proxy.columns.SQLAnyColumn;
import com.easy.query.core.proxy.columns.SQLBooleanColumn;
import com.easy.query.core.proxy.columns.SQLDateTimeColumn;
import com.easy.query.core.proxy.columns.SQLManyQueryable;
import com.easy.query.core.proxy.columns.SQLNavigateColumn;
import com.easy.query.core.proxy.columns.SQLNumberColumn;
import com.easy.query.core.proxy.columns.SQLQueryable;
import com.easy.query.core.proxy.columns.SQLStringColumn;
import com.easy.query.core.proxy.columns.impl.EasySQLManyQueryable;
import com.easy.query.core.proxy.columns.impl.EasySQLQueryable;
import com.easy.query.core.proxy.columns.impl.EmptySQLManyQueryable;
import com.easy.query.core.proxy.columns.impl.EmptySQLQueryable;
import com.easy.query.core.proxy.columns.impl.SQLAnyColumnImpl;
import com.easy.query.core.proxy.columns.impl.SQLBooleanColumnImpl;
import com.easy.query.core.proxy.columns.impl.SQLDateTimeColumnImpl;
import com.easy.query.core.proxy.columns.impl.SQLNavigateColumnImpl;
import com.easy.query.core.proxy.columns.impl.SQLNumberColumnImpl;
import com.easy.query.core.proxy.columns.impl.SQLStringColumnImpl;
import com.easy.query.core.proxy.columns.types.SQLAnyTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLBigDecimalTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLBooleanTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLByteTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLDateTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLDoubleTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLFloatTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLIntegerTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLLocalDateTimeTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLLocalDateTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLLocalTimeTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLLongTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLShortTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLStringTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLTimeTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLTimestampTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLUUIDTypeColumn;
import com.easy.query.core.proxy.columns.types.SQLUtilDateTypeColumn;
import com.easy.query.core.proxy.columns.types.impl.SQLAnyTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLBigDecimalTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLBooleanTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLByteTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLDateTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLDoubleTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLFloatTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLIntegerTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLLocalDateTimeTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLLocalDateTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLLocalTimeTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLLongTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLShortTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLStringTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLTimeTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLTimestampTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLUUIDTypeColumnImpl;
import com.easy.query.core.proxy.columns.types.impl.SQLUtilDateTypeColumnImpl;
import com.easy.query.core.proxy.core.ColumnSelectSQLContext;
import com.easy.query.core.proxy.core.EntitySQLContext;
import com.easy.query.core.proxy.impl.SQLColumnImpl;
import com.easy.query.core.util.EasyObjectUtil;
import com.easy.query.core.util.EasyRelationalUtil;

import java.util.Objects;

/**
 * create time 2023/6/25 12:39
 * 文件说明
 *
 * @author xuejiaming
 */
public abstract class AbstractBaseProxyEntity, TEntity> implements ProxyEntity, EntitySQLContextAvailable {

    protected TableAvailable table;
    protected EntitySQLContext entitySQLContext = new ColumnSelectSQLContext();

    private String propValue;

    @Override
    public String getNavValue() {
        return propValue;
    }

    @Override
    public void setNavValue(String val) {
        this.propValue = val;
    }

    @Override
    public @Nullable TableAvailable getTable() {
        return table;
    }

    @Override
    public TableAvailable getTableOrNull() {
        return table;
    }

    @Override
    public EntitySQLContext getEntitySQLContext() {
        Objects.requireNonNull(entitySQLContext, "cant found entitySQLContext in sql context");
        return entitySQLContext;
    }


    @Override
    public TProxy create(TableAvailable table, EntitySQLContext entitySQLContext) {
        this.table = table;
        this.entitySQLContext = entitySQLContext;
        return EasyObjectUtil.typeCastNullable(this);
    }

    @Deprecated
    protected  SQLColumn get(String property) {
        return get(property, null);
    }

    protected  SQLColumn get(String property, @Nullable Class propType) {
        SQLColumn column = new SQLColumnImpl<>(entitySQLContext, table, property, propType);
        column._setProxy(castChain());
        return column;
    }

    protected  SQLDateTimeColumn getDateTimeColumn(String property, @Nullable Class propType) {
        SQLDateTimeColumn column = new SQLDateTimeColumnImpl<>(entitySQLContext, table, property, propType);
        column._setProxy(castChain());
        return column;
    }

    protected  SQLNumberColumn getNumberColumn(String property, @Nullable Class propType) {
        SQLNumberColumn column = new SQLNumberColumnImpl<>(entitySQLContext, table, property, propType);
        column._setProxy(castChain());
        return column;
    }

    protected  SQLStringColumn getStringColumn(String property, @Nullable Class propType) {
        SQLStringColumn column = new SQLStringColumnImpl<>(entitySQLContext, table, property, propType);
        column._setProxy(castChain());
        return column;
    }

    protected  SQLBooleanColumn getBooleanColumn(String property, @Nullable Class propType) {
        SQLBooleanColumn column = new SQLBooleanColumnImpl<>(entitySQLContext, table, property, propType);
        column._setProxy(castChain());
        return column;
    }

    protected  SQLAnyColumn getAnyColumn(String property, @Nullable Class propType) {
        SQLAnyColumn column = new SQLAnyColumnImpl<>(entitySQLContext, table, property, propType);
        column._setProxy(castChain());
        return column;
    }


    protected  SQLAnyTypeColumn getAnyTypeColumn(String property, @Nullable Class propType) {
        SQLAnyTypeColumn column = new SQLAnyTypeColumnImpl<>(entitySQLContext, table, property, propType);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLBigDecimalTypeColumn getBigDecimalTypeColumn(String property) {
        SQLBigDecimalTypeColumn column = new SQLBigDecimalTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLBooleanTypeColumn getBooleanTypeColumn(String property) {
        SQLBooleanTypeColumn column = new SQLBooleanTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLByteTypeColumn getByteTypeColumn(String property) {
        SQLByteTypeColumn column = new SQLByteTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLDateTypeColumn getSQLDateTypeColumn(String property) {
        SQLDateTypeColumn column = new SQLDateTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLDoubleTypeColumn getDoubleTypeColumn(String property) {
        SQLDoubleTypeColumn column = new SQLDoubleTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLFloatTypeColumn getFloatTypeColumn(String property) {
        SQLFloatTypeColumn column = new SQLFloatTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLIntegerTypeColumn getIntegerTypeColumn(String property) {
        SQLIntegerTypeColumn column = new SQLIntegerTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLLocalDateTimeTypeColumn getLocalDateTimeTypeColumn(String property) {
        SQLLocalDateTimeTypeColumn column = new SQLLocalDateTimeTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLLocalDateTypeColumn getLocalDateTypeColumn(String property) {
        SQLLocalDateTypeColumn column = new SQLLocalDateTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLLocalTimeTypeColumn getLocalTimeTypeColumn(String property) {
        SQLLocalTimeTypeColumn column = new SQLLocalTimeTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLLongTypeColumn getLongTypeColumn(String property) {
        SQLLongTypeColumn column = new SQLLongTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLShortTypeColumn getShortTypeColumn(String property) {
        SQLShortTypeColumn column = new SQLShortTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLStringTypeColumn getStringTypeColumn(String property) {
        SQLStringTypeColumn column = new SQLStringTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLTimestampTypeColumn getTimestampTypeColumn(String property) {
        SQLTimestampTypeColumn column = new SQLTimestampTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLTimeTypeColumn getTimeTypeColumn(String property) {
        SQLTimeTypeColumn column = new SQLTimeTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLUtilDateTypeColumn getUtilDateTypeColumn(String property) {
        SQLUtilDateTypeColumn column = new SQLUtilDateTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }

    protected SQLUUIDTypeColumn getUUIDTypeColumn(String property) {
        SQLUUIDTypeColumn column = new SQLUUIDTypeColumnImpl<>(entitySQLContext, table, property);
        column._setProxy(castChain());
        column.setNavValue(getFullNavValue(property));
        return column;
    }


    @Deprecated
    protected  SQLNavigateColumn getNavigate(String property, Class propType) {
        SQLNavigateColumnImpl column = new SQLNavigateColumnImpl<>(this.getEntitySQLContext(), table, property, propType);
        column._setProxy(castChain());
        return column;
    }

    protected , TProperty> TPropertyProxy getNavigate(String property, TPropertyProxy propertyProxy) {
        Objects.requireNonNull(this.entitySQLContext, "entitySQLContext is null");
        EntityExpressionBuilder entityExpressionBuilder = entitySQLContext.getEntityExpressionBuilder();
        //vo
        if (entityExpressionBuilder == null || entitySQLContext.methodIsInclude() || entityExpressionBuilder.getRuntimeContext() instanceof EmptyQueryRuntimeContext) {
            TPropertyProxy tPropertyProxy = propertyProxy.create(getTable(), this.getEntitySQLContext());
            tPropertyProxy.setNavValue(getFullNavValue(property));
            return tPropertyProxy;
        } else {
            TableAvailable leftTable = getTable();
            if (leftTable == null) {
                throw new EasyQueryInvalidOperationException(String.format("getNavigate %s cant not found table", property));
            }
            String fullName = getFullNavValue(property);
            TableAvailable relationTable = EasyRelationalUtil.getRelationTable(entityExpressionBuilder, leftTable, property,fullName);
            TPropertyProxy tPropertyProxy = propertyProxy.create(relationTable, this.getEntitySQLContext());
            tPropertyProxy.setNavValue(fullName);
            return tPropertyProxy;
        }
    }

    protected , TProperty> SQLQueryable getNavigates(String property, TPropertyProxy propertyProxy) {
        Objects.requireNonNull(this.entitySQLContext, "entitySQLContext is null");
        QueryRuntimeContext runtimeContext = this.entitySQLContext.getRuntimeContext();
        EntityExpressionBuilder entityExpressionBuilder = entitySQLContext.getEntityExpressionBuilder();
        if (entityExpressionBuilder == null || runtimeContext instanceof EmptyQueryRuntimeContext) {
            propertyProxy.setNavValue(getFullNavValue(property));
            return new EmptySQLQueryable<>(this.getEntitySQLContext(), propertyProxy);
        } else {
            TableAvailable leftTable = getTable();
            if (leftTable == null) {
                throw new EasyQueryInvalidOperationException(String.format("getNavigate %s cant not found table", property));
            }
            NavigateMetadata navigateMetadata = leftTable.getEntityMetadata().getNavigateNotNull(property);
            ClientQueryable clientQueryable = runtimeContext.getSQLClientApiFactory().createQueryable(propertyProxy.getEntityClass(), runtimeContext);
            if (navigateMetadata.getRelationType() == RelationTypeEnum.ManyToMany && navigateMetadata.getMappingClass() != null) {
                ClientQueryable mappingQueryable = runtimeContext.getSQLClientApiFactory().createQueryable(navigateMetadata.getMappingClass(), runtimeContext);
                clientQueryable.where(x -> {
                    x.and(() -> {
                        ClientQueryable subMappingQueryable = mappingQueryable.where(m -> {
                            m.multiEq(true, x, navigateMetadata.getTargetMappingProperties(), navigateMetadata.getTargetPropertiesOrPrimary(runtimeContext));
                            m.multiEq(true, new SimpleEntitySQLTableOwner<>(leftTable), navigateMetadata.getSelfMappingProperties(), navigateMetadata.getSelfPropertiesOrPrimary());
                            navigateMetadata.predicateMappingClassFilterApply(m);
                        }).limit(1);
                        x.exists(subMappingQueryable);
                    });
                });
            } else {
                clientQueryable.where(t -> {
                    t.and(() -> {
                        t.multiEq(true, new SimpleEntitySQLTableOwner<>(leftTable), navigateMetadata.getTargetPropertiesOrPrimary(runtimeContext), navigateMetadata.getSelfPropertiesOrPrimary());
                        navigateMetadata.predicateFilterApply(t);
                    });
                });
            }
            EasyEntityQueryable queryable = new EasyEntityQueryable<>(propertyProxy, clientQueryable);
            queryable.get1Proxy().setNavValue(getFullNavValue(property));
            return new EasySQLQueryable<>(this.getEntitySQLContext(), queryable, leftTable);
        }
    }

    protected , TProperty> SQLManyQueryable getNavigateMany(String property, TPropertyProxy propertyProxy) {
        Objects.requireNonNull(this.entitySQLContext, "entitySQLContext is null");
        EntityExpressionBuilder entityExpressionBuilder = entitySQLContext.getEntityExpressionBuilder();
        QueryRuntimeContext runtimeContext = this.entitySQLContext.getRuntimeContext();
        if (entityExpressionBuilder == null || runtimeContext instanceof EmptyQueryRuntimeContext) {
            propertyProxy.setNavValue(getFullNavValue(property));
            SQLManyQueryable query = new EmptySQLManyQueryable<>(this.getEntitySQLContext(), propertyProxy);
            query._setProxy(castChain());
            return query;
        } else {
            TableAvailable leftTable = getTable();
            if (leftTable == null) {
                throw new EasyQueryInvalidOperationException(String.format("getNavigate %s cant not found table", property));
            }
            NavigateMetadata navigateMetadata = leftTable.getEntityMetadata().getNavigateNotNull(property);
            ClientQueryable clientQueryable = runtimeContext.getSQLClientApiFactory().createQueryable(propertyProxy.getEntityClass(), runtimeContext);
            if (navigateMetadata.getRelationType() == RelationTypeEnum.ManyToMany && navigateMetadata.getMappingClass() != null) {
                ClientQueryable mappingQueryable = runtimeContext.getSQLClientApiFactory().createQueryable(navigateMetadata.getMappingClass(), runtimeContext);
                clientQueryable.where(x -> {
                    x.and(() -> {
                        ClientQueryable subMappingQueryable = mappingQueryable.where(m -> {
                            m.multiEq(true,x, navigateMetadata.getTargetMappingProperties(), navigateMetadata.getTargetPropertiesOrPrimary(runtimeContext));
                            m.multiEq(true,new SimpleEntitySQLTableOwner<>(leftTable), navigateMetadata.getSelfMappingProperties(), navigateMetadata.getSelfPropertiesOrPrimary());
                            navigateMetadata.predicateMappingClassFilterApply(m);
                        }).limit(1);
                        x.exists(subMappingQueryable);
                    });
                });
            } else {
                clientQueryable.where(t -> {
                    t.and(() -> {
                        t.multiEq(true,new SimpleEntitySQLTableOwner<>(leftTable), navigateMetadata.getTargetPropertiesOrPrimary(runtimeContext), navigateMetadata.getSelfPropertiesOrPrimary());
                        navigateMetadata.predicateFilterApply(t);
                    });
                });
            }
            EasyEntityQueryable queryable = new EasyEntityQueryable<>(propertyProxy, clientQueryable);
            queryable.get1Proxy().setNavValue(getFullNavValue(property));
            EasySQLManyQueryable query = new EasySQLManyQueryable<>(this.getEntitySQLContext(), queryable, leftTable);
            query._setProxy(castChain());
            return query;
        }
    }

    private String getFullNavValue(String navValue) {
        String parentNavValue = getNavValue();
        if (parentNavValue == null) {
            return navValue;
        }
        return parentNavValue + "." + navValue;
    }

    protected TProxy castChain() {
        return (TProxy) this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy