Search in sources :

Example 6 with Database

use of org.apache.torque.engine.database.model.Database in project dbflute-core by dbflute.

the class DfSql2EntityTask method processDomainHandling.

// -----------------------------------------------------
// Result Handling
// ---------------
protected void processDomainHandling(DfCustomizeEntityInfo entityInfo, Table tbl) {
    final DfPmbMetaData pmbMetaData = entityInfo.getPmbMetaData();
    if (pmbMetaData == null || !pmbMetaData.isTypedReturnEntityPmb()) {
        final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
        br.addNotice("The 'domain' option was not related to a typed parameter-bean.");
        br.addItem("Advice");
        br.addElement("A 'domain' option should be defined with a typed parameter-bean");
        br.addElement("that is typed to things returning an entity.");
        br.addElement("For example:");
        br.addElement("  (x): selectDomainMember.sql");
        br.addElement("  (o): MemberBhv_selectDomainMember.sql");
        br.addElement("  (x):");
        br.addElement("    -- #df:entity#");
        br.addElement("    -- +domain+");
        br.addElement("");
        br.addElement("    select MEMBER_ID, MEMBER_NAME, ... from MEMBER");
        br.addElement("  (o):");
        br.addElement("    -- #df:entity#");
        br.addElement("    -- +domain+");
        br.addElement("");
        br.addElement("    -- !df:pmb!");
        br.addElement("");
        br.addElement("    select MEMBER_ID, MEMBER_NAME, ... from MEMBER");
        br.addItem("SQL File");
        br.addElement(entityInfo.getSqlFile());
        final String msg = br.buildExceptionMessage();
        throw new DfCustomizeEntityMarkInvalidException(msg);
    }
    final String entityClassName = pmbMetaData.getEntityClassName();
    if (Srl.is_Null_or_TrimmedEmpty(entityClassName)) {
        String msg = "The entity class name should not be null: " + entityInfo.getSqlFile();
        // no way
        throw new IllegalStateException(msg);
    }
    final Database database = _schemaData.getDatabase();
    Table domainTable = database.getTable(entityClassName);
    if (domainTable == null) {
        // retry without project-prefix for a class name
        final String projectPrefix = getBasicProperties().getProjectPrefix();
        domainTable = database.getTable(Srl.substringFirstFront(entityClassName, projectPrefix));
    }
    if (domainTable == null) {
        final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
        br.addNotice("The table of the behavior query was not found.");
        br.addItem("Advice");
        br.addElement("A 'domain' option should be defined under behavior query.");
        br.addElement("And behavior query should have an existing table.");
        br.addElement("For example:");
        br.addElement("  (x): MembooBhv_selectDomainMember.sql");
        br.addElement("  (o): MemberBhv_selectDomainMember.sql");
        br.addItem("SQL File");
        br.addElement(entityInfo.getSqlFile());
        final String msg = br.buildExceptionMessage();
        // basically no way
        throw new DfCustomizeEntityMarkInvalidException(msg);
    }
    final List<Column> columnList = tbl.getColumnList();
    for (Column column : columnList) {
        final Column found = domainTable.getColumn(column.getName());
        if (found == null) {
            final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
            br.addNotice("The selected column was not a column of domain table.");
            br.addItem("Advice");
            br.addElement("A selected column with a 'domain' option");
            br.addElement("should be one of domain table.");
            br.addElement("For example:");
            br.addElement("  (x):");
            br.addElement("    select MEMBER_ID, 'noexist' as NO_EXIST from MEMBER");
            br.addElement("  (o):");
            br.addElement("    select MEMBER_ID, MEMBER_NAME from MEMBER");
            br.addElement("  (o):");
            br.addElement("    select member.* from MEMBER member");
            br.addItem("SQL File");
            br.addElement(entityInfo.getSqlFile());
            br.addItem("Unknown Column");
            br.addElement(column.getName());
            br.addItem("Domain Table");
            br.addElement(domainTable.getTableDbName());
            final String msg = br.buildExceptionMessage();
            throw new DfCustomizeEntityMarkInvalidException(msg);
        }
    }
    entityInfo.setEntityClassName(domainTable.getExtendedEntityClassName());
    entityInfo.setEntityClassName(domainTable.getImmutableExtendedEntityClassName());
}
Also used : Table(org.apache.torque.engine.database.model.Table) ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder) Column(org.apache.torque.engine.database.model.Column) Database(org.apache.torque.engine.database.model.Database) DfPmbMetaData(org.dbflute.logic.sql2entity.pmbean.DfPmbMetaData) DfCustomizeEntityMarkInvalidException(org.dbflute.exception.DfCustomizeEntityMarkInvalidException)

Example 7 with Database

use of org.apache.torque.engine.database.model.Database in project dbflute-core by dbflute.

the class DfSPolicyInRepsChecker method checkSchemaPolicyInRepsIfNeeds.

// ===================================================================================
// Check
// =====
public boolean checkSchemaPolicyInRepsIfNeeds() {
    // returns check executed or not
    final DfReplaceSchemaProperties repsProp = getReplaceSchemaProperties();
    if (!repsProp.isCheckSchemaPolicyInReps()) {
        return false;
    }
    final DfSchemaPolicyProperties policyProp = getSchemaPolicyProperties();
    if (!policyProp.hasPolicy()) {
        return false;
    }
    _log.info("");
    _log.info("* * * * * * * * * * * *");
    _log.info("*                     *");
    _log.info("*    Schema Policy    *");
    _log.info("*                     *");
    _log.info("* * * * * * * * * * * *");
    final String schemaXml = repsProp.getSchemaPolicyInRepsSchemaXml();
    deleteTemporarySchemaXmlIfExists(schemaXml);
    final DfSchemaXmlSerializer serializer = createSchemaXmlSerializer(schemaXml);
    serializer.serialize();
    try {
        final DfSchemaXmlReader reader = createSchemaXmlReader(schemaXml);
        final AppData appData = reader.read();
        final Database database = appData.getDatabase();
        // for e.g. "then classification"
        initializeSupplementaryMetaData(database);
        final DfSPolicyChecker checker = createChecker(policyProp, database);
        final DfSPolicyResult policyResult = checker.checkPolicyIfNeeds();
        if (policyResult == null) {
            // no way already checked, just in case
            return false;
        }
        // immediately handles violation (may be throw)
        policyResult.ending();
    } finally {
        deleteTemporarySchemaXmlIfExists(schemaXml);
    }
    return true;
}
Also used : DfSPolicyChecker(org.dbflute.logic.doc.spolicy.DfSPolicyChecker) DfReplaceSchemaProperties(org.dbflute.properties.DfReplaceSchemaProperties) AppData(org.apache.torque.engine.database.model.AppData) DfSPolicyResult(org.dbflute.logic.doc.spolicy.result.DfSPolicyResult) Database(org.apache.torque.engine.database.model.Database) DfSchemaXmlSerializer(org.dbflute.logic.jdbc.schemaxml.DfSchemaXmlSerializer) DfSchemaPolicyProperties(org.dbflute.properties.DfSchemaPolicyProperties) DfSchemaXmlReader(org.dbflute.logic.jdbc.schemaxml.DfSchemaXmlReader)

Example 8 with Database

use of org.apache.torque.engine.database.model.Database in project dbflute-core by dbflute.

the class DfSPolicyCrossSecretary method getSameAliasColumnListMap.

protected Map<String, List<Column>> getSameAliasColumnListMap(Database database) {
    if (_sameAliasColumnListMap != null) {
        return _sameAliasColumnListMap;
    }
    // case insensitive (not flexible), alias handling rule does not exist in DBFlute
    // but flexible is hard to implement and small merit
    final Map<String, List<Column>> sameAliasColumnListMap = StringKeyMap.createAsCaseInsensitive();
    final List<Table> targetTableList = database.getTableList().stream().filter(tbl -> isTargetTable(tbl)).collect(Collectors.toList());
    for (Table myTable : targetTableList) {
        final List<Column> columnList = myTable.getColumnList();
        for (Column myColumn : columnList) {
            if (!isTargetColumn(myColumn)) {
                // non-target
                continue;
            }
            if (!myColumn.hasAlias()) {
                // cannot determine
                continue;
            }
            final String myAlias = myColumn.getAlias();
            if (sameAliasColumnListMap.containsKey(myAlias)) {
                // registered by the other same-name column
                continue;
            }
            for (Table yourTable : targetTableList) {
                List<Column> yourColumnList = yourTable.getColumnList();
                Column yourColumn = null;
                for (Column currentColumn : yourColumnList) {
                    if (currentColumn.hasAlias() && myAlias.equalsIgnoreCase(currentColumn.getAlias())) {
                        yourColumn = currentColumn;
                        break;
                    }
                }
                if (yourColumn != null) {
                    if (!isTargetColumn(yourColumn)) {
                        // non-target
                        continue;
                    }
                    if (!yourColumn.hasAlias()) {
                        // cannot determine
                        continue;
                    }
                    List<Column> sameColumnList = sameAliasColumnListMap.get(myAlias);
                    if (sameColumnList == null) {
                        sameColumnList = new ArrayList<Column>();
                        sameColumnList.add(myColumn);
                        sameAliasColumnListMap.put(myAlias, sameColumnList);
                    }
                    sameColumnList.add(yourColumn);
                }
            }
        }
    }
    _sameAliasColumnListMap = sameAliasColumnListMap;
    return _sameAliasColumnListMap;
}
Also used : Column(org.apache.torque.engine.database.model.Column) DfCollectionUtil(org.dbflute.util.DfCollectionUtil) Predicate(java.util.function.Predicate) DfBuildProperties(org.dbflute.DfBuildProperties) DfBasicProperties(org.dbflute.properties.DfBasicProperties) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Database(org.apache.torque.engine.database.model.Database) List(java.util.List) Table(org.apache.torque.engine.database.model.Table) Map(java.util.Map) StringKeyMap(org.dbflute.helper.StringKeyMap) Table(org.apache.torque.engine.database.model.Table) Column(org.apache.torque.engine.database.model.Column) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with Database

use of org.apache.torque.engine.database.model.Database in project dbflute-core by dbflute.

the class DfSPolicyCrossSecretary method getSameNameColumnListMap.

protected Map<String, List<Column>> getSameNameColumnListMap(Database database) {
    if (_sameNameColumnListMap != null) {
        return _sameNameColumnListMap;
    }
    // flexible map because column name is treated as flexible in DBFlute (so MEMBER_NAME and MemberName are same-name)
    final Map<String, List<Column>> sameNameColumnListMap = StringKeyMap.createAsFlexible();
    final List<Table> targetTableList = database.getTableList().stream().filter(tbl -> isTargetTable(tbl)).collect(Collectors.toList());
    for (Table myTable : targetTableList) {
        final List<Column> columnList = myTable.getColumnList();
        for (Column myColumn : columnList) {
            if (!isTargetColumn(myColumn)) {
                // non-target
                continue;
            }
            final String myName = myColumn.getName();
            if (sameNameColumnListMap.containsKey(myName)) {
                // registered by the other same-name column
                continue;
            }
            for (Table yourTable : targetTableList) {
                final Column yourColumn = yourTable.getColumn(myName);
                if (yourColumn != null) {
                    if (!isTargetColumn(yourColumn)) {
                        // non-target
                        continue;
                    }
                    List<Column> sameColumnList = sameNameColumnListMap.get(myName);
                    if (sameColumnList == null) {
                        sameColumnList = new ArrayList<Column>();
                        sameColumnList.add(myColumn);
                        sameNameColumnListMap.put(myName, sameColumnList);
                    }
                    sameColumnList.add(yourColumn);
                }
            }
        }
    }
    _sameNameColumnListMap = sameNameColumnListMap;
    return _sameNameColumnListMap;
}
Also used : Column(org.apache.torque.engine.database.model.Column) DfCollectionUtil(org.dbflute.util.DfCollectionUtil) Predicate(java.util.function.Predicate) DfBuildProperties(org.dbflute.DfBuildProperties) DfBasicProperties(org.dbflute.properties.DfBasicProperties) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Database(org.apache.torque.engine.database.model.Database) List(java.util.List) Table(org.apache.torque.engine.database.model.Table) Map(java.util.Map) StringKeyMap(org.dbflute.helper.StringKeyMap) Table(org.apache.torque.engine.database.model.Table) Column(org.apache.torque.engine.database.model.Column) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with Database

use of org.apache.torque.engine.database.model.Database in project dbflute-core by dbflute.

the class DfSchemaDiff method loadPreviousSchema.

// ===================================================================================
// Load Schema
// ===========
public void loadPreviousSchema() {
    // before loading next schema
    final DfSchemaXmlReader reader = _previousReader;
    if (!reader.exists()) {
        _firstTime = true;
        // to make history of the first time @since 1.1.9
        // dummy database (has no object, can return empty)
        _previousDb = new Database();
        // fixedly no table
        _previousTableCount = 0;
        return;
    }
    try {
        _previousDb = reader.read().getDatabase();
    } catch (RuntimeException e) {
        _loadingFailure = true;
        handleReadingException(e, reader);
    }
    _previousTableCount = _previousDb.getTableList().size();
}
Also used : Database(org.apache.torque.engine.database.model.Database) DfSchemaXmlReader(org.dbflute.logic.jdbc.schemaxml.DfSchemaXmlReader)

Aggregations

Database (org.apache.torque.engine.database.model.Database)11 Table (org.apache.torque.engine.database.model.Table)6 Column (org.apache.torque.engine.database.model.Column)5 List (java.util.List)3 Map (java.util.Map)3 AppData (org.apache.torque.engine.database.model.AppData)3 StringKeyMap (org.dbflute.helper.StringKeyMap)3 DfSchemaXmlReader (org.dbflute.logic.jdbc.schemaxml.DfSchemaXmlReader)3 ArrayList (java.util.ArrayList)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2 Collectors (java.util.stream.Collectors)2 DfBuildProperties (org.dbflute.DfBuildProperties)2 DfBasicProperties (org.dbflute.properties.DfBasicProperties)2 DfCollectionUtil (org.dbflute.util.DfCollectionUtil)2 File (java.io.File)1 LinkedHashMap (java.util.LinkedHashMap)1 VelocityContext (org.apache.velocity.VelocityContext)1 DfCustomizeEntityMarkInvalidException (org.dbflute.exception.DfCustomizeEntityMarkInvalidException)1 DfIllegalPropertySettingException (org.dbflute.exception.DfIllegalPropertySettingException)1