Search in sources :

Example 1 with DfSPolicyStatement

use of org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement in project dbflute-core by dbflute.

the class DfSPolicyLogicalSecretaryTest method test_isHitExp_basic.

// ===================================================================================
// isHitExp()
// ==========
public void test_isHitExp_basic() {
    // ## Arrange ##
    DfSPolicyLogicalSecretary secretary = new DfSPolicyLogicalSecretary();
    DfSPolicyStatement statement = createMockStatement();
    // ## Act ##
    // ## Assert ##
    assertTrue(secretary.isHitExp(statement, "SEA_MEMBER", "prefix:SEA_"));
    assertFalse(secretary.isHitExp(statement, "SEA_MEMBER", "prefix:LAND_"));
    assertTrue(secretary.isHitExp(statement, "SEA_MEMBER", "prefix:SEA_ and suffix:_MEMBER"));
    assertTrue(secretary.isHitExp(statement, "SEA_MEMBER", "prefix:SEA_ or prefix:LAND_"));
    assertTrue(secretary.isHitExp(statement, "LAND_MEMBER", "prefix:SEA_ or prefix:LAND_"));
    assertFalse(secretary.isHitExp(statement, "PIARI_MEMBER", "prefix:SEA_ or prefix:LAND_"));
    assertTrue(secretary.isHitExp(statement, "SEA_MEMBER", "$$ALL$$"));
    try {
        assertTrue(secretary.isHitExp(statement, "SEA_MEMBER", "$$sea$$_ID"));
        fail();
    } catch (DfSchemaPolicyCheckUnknownVariableException e) {
        log(e.getMessage());
    }
    try {
        assertTrue(secretary.isHitExp(statement, "SEA_MEMBER", "sea-mystic$$land-oneman$$piari-plaza"));
        fail();
    } catch (DfSchemaPolicyCheckUnknownVariableException e) {
        log(e.getMessage());
    }
}
Also used : DfSPolicyStatement(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement) DfSchemaPolicyCheckUnknownVariableException(org.dbflute.exception.DfSchemaPolicyCheckUnknownVariableException)

Example 2 with DfSPolicyStatement

use of org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement in project dbflute-core by dbflute.

the class DfSPolicyLogicalSecretaryTest method createMockStatement.

protected DfSPolicyStatement createMockStatement() {
    List<DfSPolicyIfPart> ifPartList = newArrayList(new DfSPolicyIfPart("columnName", "$$ALL$$", false));
    DfSPolicyIfClause ifClause = new DfSPolicyIfClause(ifPartList, false);
    DfSPolicyThenClause thenClause = new DfSPolicyThenClause("bad", false, DfCollectionUtil.emptyList(), false, null);
    return new DfSPolicyStatement("if columnName is $$ALL$$ then bad", ifClause, thenClause);
}
Also used : DfSPolicyIfPart(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfPart) DfSPolicyStatement(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement) DfSPolicyThenClause(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause) DfSPolicyIfClause(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfClause)

Example 3 with DfSPolicyStatement

use of org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement in project dbflute-core by dbflute.

the class DfSPolicyLogicalSecretary method parseStatement.

// ===================================================================================
// Statement
// =========
public DfSPolicyStatement parseStatement(String statement) {
    if (!statement.startsWith("if ")) {
        String msg = "The element of statementList should start with 'if' for SchemaPolicyCheck: " + statement;
        throw new IllegalStateException(msg);
    }
    final ScopeInfo ifScope = Srl.extractScopeFirst(statement, "if ", " then ");
    if (ifScope == null) {
        final String additional = "The statement should start with 'if' and contain 'then'.";
        throwSchemaPolicyCheckIllegalIfThenStatementException(statement, additional);
    }
    final DfSPolicyIfClause ifClause = analyzeIfClause(statement, ifScope);
    final DfSPolicyThenClause thenClause = analyzeThenClause(statement, ifScope);
    return new DfSPolicyStatement(statement, ifClause, thenClause);
}
Also used : DfSPolicyStatement(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement) DfSPolicyThenClause(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause) ScopeInfo(org.dbflute.util.Srl.ScopeInfo) DfSPolicyIfClause(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfClause)

Example 4 with DfSPolicyStatement

use of org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement in project dbflute-core by dbflute.

the class DfSPolicyTableStatementChecker method doEvaluateColumnThenItemValue.

protected String doEvaluateColumnThenItemValue(DfSPolicyStatement statement, DfSPolicyThenPart thenPart, Table table, Function<String, String> violationCall) {
    final String thenItem = thenPart.getThenItem();
    final String thenValue = thenPart.getThenValue();
    final boolean notThenValue = thenPart.isNotThenValue();
    if (thenItem.equalsIgnoreCase("tableName")) {
        // e.g. tableName is prefix:CLS_
        final String tableName = toComparingTableName(table);
        if (!isHitExp(statement, tableName, toTableNameComparingThenValue(table, thenValue)) == !notThenValue) {
            return violationCall.apply(tableName);
        }
    } else if (thenItem.equalsIgnoreCase("alias")) {
        // e.g. alias is suffix:History
        final String alias = table.getAlias();
        if (!isHitExp(statement, alias, toAliasComparingThenValue(table, thenValue)) == !notThenValue) {
            return violationCall.apply(alias);
        }
    } else if (thenItem.equalsIgnoreCase("comment")) {
        // e.g. comment is contain:SEA
        final String comment = table.getComment();
        if (!isHitExp(statement, comment, thenValue) == !notThenValue) {
            return violationCall.apply(comment);
        }
    } else if (thenItem.equalsIgnoreCase("pkName")) {
        // e.g. pkName is prefix:PK_
        if (table.hasPrimaryKey()) {
            final List<Column> columnList = table.getPrimaryKey();
            // same name if compound
            final Column pk = columnList.get(0);
            final String pkName = pk.getPrimaryKeyName();
            final String comparingThenValue = toConstraintNameComparingThenValue(table, thenValue, columnList);
            if (!isHitExp(statement, pkName, comparingThenValue) == !notThenValue) {
                final String disp = pkName + (pk.isAdditionalPrimaryKey() ? ADDITIONAL_SUFFIX : "");
                return violationCall.apply(disp);
            }
        }
    } else if (thenItem.equalsIgnoreCase("fkName")) {
        // e.g. fkName is prefix:FK_
        for (ForeignKey fk : table.getForeignKeyList()) {
            final String fkName = fk.getName();
            final String comparingThenValue = toConstraintNameComparingThenValue(table, thenValue, fk.getLocalColumnList());
            if (!isHitExp(statement, fkName, comparingThenValue) == !notThenValue) {
                final String disp = fkName + (fk.isAdditionalForeignKey() ? ADDITIONAL_SUFFIX : "");
                return violationCall.apply(disp);
            }
        }
    } else if (thenItem.equalsIgnoreCase("uniqueName")) {
        // e.g. uniqueName is prefix:UQ_
        for (Unique uq : table.getUniqueList()) {
            final String uqName = uq.getName();
            final String comparingThenValue = toConstraintNameComparingThenValue(table, thenValue, uq.getColumnList());
            if (!isHitExp(statement, uqName, comparingThenValue)) {
                final String disp = uqName + (uq.isAdditional() ? ADDITIONAL_SUFFIX : "");
                return violationCall.apply(disp);
            }
        }
    } else if (thenItem.equalsIgnoreCase("indexName")) {
        // e.g. indexName is prefix:IX_
        for (Index ix : table.getIndexList()) {
            final String ixName = ix.getName();
            final String comparingThenValue = toConstraintNameComparingThenValue(table, thenValue, ix.getColumnList());
            if (!isHitExp(statement, ixName, comparingThenValue) == !notThenValue) {
                return violationCall.apply(ixName);
            }
        }
    } else if (thenItem.equalsIgnoreCase("pk_columnName")) {
        return determinePkSomethingThenValue(statement, table, violationCall, thenValue, notThenValue, pk -> toComparingColumnName(pk));
    } else if (thenItem.equalsIgnoreCase("pk_dbType") || thenItem.equalsIgnoreCase("pkDbType")) {
        // for compatible
        return determinePkSomethingThenValue(statement, table, violationCall, thenValue, notThenValue, pk -> pk.getDbType());
    } else if (thenItem.equalsIgnoreCase("pk_size")) {
        return determinePkSomethingThenValue(statement, table, violationCall, thenValue, notThenValue, pk -> pk.getColumnSize());
    } else if (thenItem.equalsIgnoreCase("pk_dbType_with_size")) {
        // e.g. char(3)
        return determinePkSomethingThenValue(statement, table, violationCall, thenValue, notThenValue, pk -> toComparingDbTypeWithSize(pk));
    } else {
        throwSchemaPolicyCheckIllegalIfThenStatementException(statement, "Unknown then-item: " + thenItem);
    }
    // no violation
    return null;
}
Also used : DfSPolicyStatement(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement) DfSPolicyThenClause(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause) Column(org.apache.torque.engine.database.model.Column) DfSPolicyThenPart(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenPart) DfSPolicyIfPart(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfPart) DfSPolicyFirstDateSecretary(org.dbflute.logic.doc.spolicy.secretary.DfSPolicyFirstDateSecretary) DfSPolicyLogicalSecretary(org.dbflute.logic.doc.spolicy.secretary.DfSPolicyLogicalSecretary) Function(java.util.function.Function) ForeignKey(org.apache.torque.engine.database.model.ForeignKey) List(java.util.List) Unique(org.apache.torque.engine.database.model.Unique) Index(org.apache.torque.engine.database.model.Index) Table(org.apache.torque.engine.database.model.Table) DfSPolicyResult(org.dbflute.logic.doc.spolicy.result.DfSPolicyResult) Srl(org.dbflute.util.Srl) Column(org.apache.torque.engine.database.model.Column) List(java.util.List) Unique(org.apache.torque.engine.database.model.Unique) Index(org.apache.torque.engine.database.model.Index) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Example 5 with DfSPolicyStatement

use of org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement in project dbflute-core by dbflute.

the class DfSPolicyChecker method buildElementMap.

protected void buildElementMap(StringBuilder sb, String title, DfSPolicyParsedPolicyPart policyPart) {
    sb.append(ln()).append(" ").append(title).append(":");
    sb.append(ln()).append("   themeList: ").append(policyPart.getThemeList());
    sb.append(ln()).append("   statementList:");
    final List<DfSPolicyStatement> tableStatementList = policyPart.getStatementList();
    for (DfSPolicyStatement statement : tableStatementList) {
        sb.append(ln()).append("    ").append(statement.getIfClause()).append(" ").append(statement.getThenClause());
    }
}
Also used : DfSPolicyStatement(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement)

Aggregations

DfSPolicyStatement (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement)6 DfSPolicyThenClause (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause)4 DfSPolicyIfClause (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfClause)3 DfSPolicyIfPart (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfPart)2 ScopeInfo (org.dbflute.util.Srl.ScopeInfo)2 List (java.util.List)1 Function (java.util.function.Function)1 Column (org.apache.torque.engine.database.model.Column)1 ForeignKey (org.apache.torque.engine.database.model.ForeignKey)1 Index (org.apache.torque.engine.database.model.Index)1 Table (org.apache.torque.engine.database.model.Table)1 Unique (org.apache.torque.engine.database.model.Unique)1 DfSchemaPolicyCheckUnknownVariableException (org.dbflute.exception.DfSchemaPolicyCheckUnknownVariableException)1 DfSPolicyThenPart (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenPart)1 DfSPolicyResult (org.dbflute.logic.doc.spolicy.result.DfSPolicyResult)1 DfSPolicyFirstDateSecretary (org.dbflute.logic.doc.spolicy.secretary.DfSPolicyFirstDateSecretary)1 DfSPolicyLogicalSecretary (org.dbflute.logic.doc.spolicy.secretary.DfSPolicyLogicalSecretary)1 Srl (org.dbflute.util.Srl)1