Search in sources :

Example 6 with DfSPolicyThenClause

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

the class DfSPolicyMiscSecretary method analyzeThenClause.

// -----------------------------------------------------
// Then Clause
// -----------
protected DfSPolicyThenClause analyzeThenClause(String statement, ScopeInfo ifScope) {
    final String thenRear = ifScope.substringInterspaceToNext();
    final String thenWhole;
    // e.g. if tableName is suffix:_ID then bad => similar to column name
    final String supplement;
    if (thenRear.contains(SUPPLEMENT_DELIMITER)) {
        thenWhole = Srl.substringLastFront(thenRear, SUPPLEMENT_DELIMITER).trim();
        supplement = Srl.substringLastRear(thenRear, SUPPLEMENT_DELIMITER).trim();
    } else {
        thenWhole = thenRear;
        supplement = null;
    }
    final String thenTheme;
    final boolean notThenTheme;
    final List<DfSPolicyThenPart> thenPartList;
    final boolean connectedByOr;
    if (!thenWhole.contains(EQUALS_DELIMITER)) {
        // then [theme]
        final boolean startsWithNot = thenWhole.startsWith(NOT_PREFIX);
        if (startsWithNot) {
            thenTheme = Srl.substringFirstRear(thenWhole, NOT_PREFIX);
        } else {
            thenTheme = thenWhole;
        }
        notThenTheme = startsWithNot;
        thenPartList = Collections.emptyList();
        connectedByOr = false;
    } else {
        // then ... is ...
        thenTheme = null;
        notThenTheme = false;
        boolean byOr = false;
        List<String> thenPartStrList = splitClauseByConnector(thenWhole, " and ");
        if (thenPartStrList.size() == 1) {
            thenPartStrList = splitClauseByConnector(thenWhole, " or ");
            if (thenPartStrList.size() >= 2) {
                byOr = true;
            }
        }
        final List<DfSPolicyThenPart> makingPartList = new ArrayList<DfSPolicyThenPart>();
        for (String thenPartStr : thenPartStrList) {
            final String thenItem = Srl.substringFirstFront(thenPartStr, EQUALS_DELIMITER).trim();
            final String thenValueCandidate = Srl.substringFirstRear(thenPartStr, EQUALS_DELIMITER).trim();
            final boolean notThenValue = thenValueCandidate.startsWith(NOT_PREFIX);
            final String thenValue = notThenValue ? Srl.substringFirstRear(thenValueCandidate, NOT_PREFIX).trim() : thenValueCandidate;
            makingPartList.add(new DfSPolicyThenPart(thenItem, thenValue, notThenValue));
        }
        thenPartList = Collections.unmodifiableList(makingPartList);
        connectedByOr = byOr;
    }
    return new DfSPolicyThenClause(thenTheme, notThenTheme, thenPartList, connectedByOr, supplement);
}
Also used : DfSPolicyThenPart(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenPart) ArrayList(java.util.ArrayList) DfSPolicyThenClause(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause)

Example 7 with DfSPolicyThenClause

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

the class DfSPolicyMiscSecretary 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 8 with DfSPolicyThenClause

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

the class DfSPolicyColumnStatementChecker method evaluateColumnThenTheme.

// -----------------------------------------------------
// Then Theme
// ----------
protected void evaluateColumnThenTheme(DfSPolicyStatement statement, DfSPolicyResult result, Column column) {
    final String policy = toPolicy(statement);
    final DfSPolicyThenClause thenClause = statement.getThenClause();
    // already not null here
    final String thenTheme = thenClause.getThenTheme();
    final boolean notThenClause = thenClause.isNotThenTheme();
    final String notOr = notThenClause ? "not " : "";
    if (thenTheme.equalsIgnoreCase("bad")) {
        if (notThenClause) {
            throwSchemaPolicyCheckIllegalThenNotThemeException(statement, "bad");
        } else {
            result.violate(policy, "The column is no good: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("notNull")) {
        if (!column.isNotNull() == !notThenClause) {
            result.violate(policy, "The column should " + notOr + "be not-null: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("identity")) {
        if (!column.isAutoIncrement() == !notThenClause) {
            result.violate(policy, "The column should " + notOr + "be identity (auto increment): " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("pk")) {
        if (!column.isPrimaryKey() == !notThenClause) {
            result.violate(policy, "The column should " + notOr + "be primary key: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("fk")) {
        if (!column.isForeignKey() == !notThenClause) {
            result.violate(policy, "The column should " + notOr + "be foreign key: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("unique")) {
        if (!column.isUnique() == !notThenClause) {
            result.violate(policy, "The column should " + notOr + "be unique: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("index")) {
        if (!column.hasIndex() == !notThenClause) {
            result.violate(policy, "The column should " + notOr + "have index: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("classification")) {
        if (!column.hasClassification() == !notThenClause) {
            result.violate(policy, "The column should " + notOr + "be classification: " + toColumnDisp(column));
        }
    } else if (Srl.startsWithIgnoreCase(thenTheme, "classification")) {
        // e.g. classification(MemberStatus)
        final String policyClsName = analyzeSpecifiedClsName(thenTheme, statement);
        // null allowed if not related to classification
        final String clsName = column.getClassificationName();
        if (notThenClause) {
            // unsupported "not classification(MemberStatus)" because of unnecessary
            throwSchemaPolicyCheckIllegalThenNotThemeException(statement, "classification(...)");
        } else if (clsName == null || !clsName.equalsIgnoreCase(policyClsName)) {
            result.violate(policy, "The column should be classification of " + policyClsName + ": " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("upperCaseBasis")) {
        if (Srl.isLowerCaseAny(toComparingColumnName(column)) == !notThenClause) {
            result.violate(policy, "The column name should " + notOr + "be on upper case basis: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("lowerCaseBasis")) {
        if (Srl.isUpperCaseAny(toComparingColumnName(column)) == !notThenClause) {
            result.violate(policy, "The column name should " + notOr + "be on lower case basis: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("hasAlias")) {
        if (!column.hasAlias() == !notThenClause) {
            result.violate(policy, "The column should " + notOr + "have column alias: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("hasComment")) {
        if (!column.hasComment() == !notThenClause) {
            result.violate(policy, "The column should " + notOr + "have column comment: " + toColumnDisp(column));
        }
    } else if (thenTheme.equalsIgnoreCase("sameColumnAliasIfSameColumnName")) {
        final String vio = _crossDeterminer.determineSameColumnAliasIfSameColumnName(column, prepareYourTargeting(statement));
        if (vio != null) {
            result.violate(policy, vio);
        }
    } else if (thenTheme.equalsIgnoreCase("sameColumnDbTypeIfSameColumnName")) {
        final String vio = _crossDeterminer.determineSameColumnDbTypeIfSameColumnName(column, prepareYourTargeting(statement));
        if (vio != null) {
            result.violate(policy, vio);
        }
    } else if (thenTheme.equalsIgnoreCase("sameColumnSizeIfSameColumnName")) {
        final String vio = _crossDeterminer.determineSameColumnSizeIfSameColumnName(column, prepareYourTargeting(statement));
        if (vio != null) {
            result.violate(policy, vio);
        }
    } else if (thenTheme.equalsIgnoreCase("sameColumnNameIfSameColumnAlias")) {
        final String vio = _crossDeterminer.determineSameColumnNameIfSameColumnAlias(column, prepareYourTargeting(statement));
        if (vio != null) {
            result.violate(policy, vio);
        }
    } else {
        throwSchemaPolicyCheckIllegalIfThenStatementException(statement, "Unknown then-clause: " + thenClause);
    }
}
Also used : DfSPolicyThenClause(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause)

Example 9 with DfSPolicyThenClause

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

the class DfSPolicyLogicalSecretary method analyzeThenClause.

// -----------------------------------------------------
// Then Clause
// -----------
protected DfSPolicyThenClause analyzeThenClause(String statement, ScopeInfo ifScope) {
    final String thenRear = ifScope.substringInterspaceToNext();
    final String thenWhole;
    // e.g. if tableName is suffix:_ID then bad => similar to column name
    final String supplement;
    if (thenRear.contains(SUPPLEMENT_DELIMITER)) {
        thenWhole = Srl.substringLastFront(thenRear, SUPPLEMENT_DELIMITER).trim();
        supplement = Srl.substringLastRear(thenRear, SUPPLEMENT_DELIMITER).trim();
    } else {
        thenWhole = thenRear;
        supplement = null;
    }
    final String thenTheme;
    final boolean notThenTheme;
    final List<DfSPolicyThenPart> thenPartList;
    final boolean connectedByOr;
    if (!thenWhole.contains(EQUALS_DELIMITER)) {
        // then [theme]
        final boolean startsWithNot = thenWhole.startsWith(NOT_PREFIX);
        if (startsWithNot) {
            thenTheme = Srl.substringFirstRear(thenWhole, NOT_PREFIX);
        } else {
            thenTheme = thenWhole;
        }
        notThenTheme = startsWithNot;
        thenPartList = Collections.emptyList();
        connectedByOr = false;
    } else {
        // then ... is ...
        thenTheme = null;
        notThenTheme = false;
        boolean byOr = false;
        List<String> thenPartStrList = splitClauseByConnector(thenWhole, " and ");
        if (thenPartStrList.size() == 1) {
            thenPartStrList = splitClauseByConnector(thenWhole, " or ");
            if (thenPartStrList.size() >= 2) {
                byOr = true;
            }
        }
        final List<DfSPolicyThenPart> makingPartList = new ArrayList<DfSPolicyThenPart>();
        for (String thenPartStr : thenPartStrList) {
            final String thenItem = Srl.substringFirstFront(thenPartStr, EQUALS_DELIMITER).trim();
            final String thenValueCandidate = Srl.substringFirstRear(thenPartStr, EQUALS_DELIMITER).trim();
            final boolean notThenValue = thenValueCandidate.startsWith(NOT_PREFIX);
            final String thenValue = notThenValue ? Srl.substringFirstRear(thenValueCandidate, NOT_PREFIX).trim() : thenValueCandidate;
            makingPartList.add(new DfSPolicyThenPart(thenItem, thenValue, notThenValue));
        }
        thenPartList = Collections.unmodifiableList(makingPartList);
        connectedByOr = byOr;
    }
    return new DfSPolicyThenClause(thenTheme, notThenTheme, thenPartList, connectedByOr, supplement);
}
Also used : DfSPolicyThenPart(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenPart) ArrayList(java.util.ArrayList) DfSPolicyThenClause(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause)

Aggregations

DfSPolicyThenClause (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause)9 DfSPolicyStatement (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement)3 DfSPolicyIfClause (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfClause)3 ArrayList (java.util.ArrayList)2 DfSPolicyThenPart (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenPart)2 ScopeInfo (org.dbflute.util.Srl.ScopeInfo)2 DfSPolicyIfPart (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfPart)1