Search in sources :

Example 1 with DfOutsideSqlChecker

use of org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker in project dbflute-core by dbflute.

the class OutsideSqlCheckerTest method test_check_parameterBean.

public void test_check_parameterBean() {
    // ## Arrange ##
    DfOutsideSqlChecker ker = new DfOutsideSqlChecker();
    String fn = "test.sql";
    // ## Act ##
    try {
        ker.check(fn, "-- #df:entity#\n-- !df;pmb!\nfoo /*IF pmb.memberId != null*/bar/*END*/");
        // ## Assert ##
        fail();
    } catch (DfParameterBeanMarkInvalidException e) {
        // OK
        log(e.getMessage());
    }
    // ## Act ##
    try {
        ker.check(fn, "-- #df:entity#\n-- !df:entity!\nfoo /*IF pmb.memberId != null*/bar/*END*/");
        // ## Assert ##
        fail();
    } catch (DfParameterBeanMarkInvalidException e) {
        // OK
        log(e.getMessage());
    }
    // ## Act ##
    try {
        ker.check(fn, "-- #df:entity#\n-- !df:pnb!\nfoo /*IF pmb.memberId != null*/bar/*END*/");
        // ## Assert ##
        fail();
    } catch (DfParameterBeanMarkInvalidException e) {
        // OK
        log(e.getMessage());
    }
}
Also used : DfParameterBeanMarkInvalidException(org.dbflute.exception.DfParameterBeanMarkInvalidException) DfOutsideSqlChecker(org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker)

Example 2 with DfOutsideSqlChecker

use of org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker in project dbflute-core by dbflute.

the class OutsideSqlCheckerTest method test_check_customizeEntity.

public void test_check_customizeEntity() {
    // ## Arrange ##
    DfOutsideSqlChecker ker = new DfOutsideSqlChecker();
    String fn = "test.sql";
    // ## Act ##
    try {
        ker.check(fn, "-- #df;entity#\n-- !df:pmb!\nfoo /*IF pmb.memberId != null*/bar/*END*/");
        // ## Assert ##
        fail();
    } catch (DfCustomizeEntityMarkInvalidException e) {
        // OK
        log(e.getMessage());
    }
    // ## Act ##
    try {
        ker.check(fn, "-- #df:pmb#\n-- !df:pmb!\nfoo /*IF pmb.memberId != null*/bar/*END*/");
        // ## Assert ##
        fail();
    } catch (DfCustomizeEntityMarkInvalidException e) {
        // OK
        log(e.getMessage());
    }
    // ## Act ##
    try {
        ker.check(fn, "-- #df:emtity#\n-- !df:pmb!\nfoo /*IF pmb.memberId != null*/bar/*END*/");
        // ## Assert ##
        fail();
    } catch (DfCustomizeEntityMarkInvalidException e) {
        // OK
        log(e.getMessage());
    }
}
Also used : DfOutsideSqlChecker(org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker) DfCustomizeEntityMarkInvalidException(org.dbflute.exception.DfCustomizeEntityMarkInvalidException)

Example 3 with DfOutsideSqlChecker

use of org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker in project dbflute-core by dbflute.

the class OutsideSqlCheckerTest method test_check_basic.

public void test_check_basic() {
    // ## Arrange ##
    DfOutsideSqlChecker ker = new DfOutsideSqlChecker();
    String fn = "test.sql";
    // ## Act & Assert ##
    ker.check(fn, "-- #df:entity#\n-- !df:pmb!\nfoo /*IF pmb.memberId != null*/bar/*END*/");
    ker.check(fn, "-- #df:entity#\n-- !df:pmb!\nfoo /*IF pmb.memberId != null && pmb.existsPurchase*/bar/*END*/");
    ker.check(fn, "-- #df:entity#\nfoo /*IF pmb.getMemberId() != null || pmb.isExistsPurchase()*/bar/*END*/");
    ker.check(fn, "-- !df:pmb!\nfoo /*IF pmb.memberName == 'abc'*/bar/*END*/");
}
Also used : DfOutsideSqlChecker(org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker)

Example 4 with DfOutsideSqlChecker

use of org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker in project dbflute-core by dbflute.

the class DfOutsideSqlTestTask method getSqlFileRunner.

protected DfSqlFileRunnerExecute getSqlFileRunner(final DfRunnerInformation runInfo) {
    final String nonTargetMark = "df:x";
    final DBDef currentDBDef = getDatabaseTypeFacadeProp().getCurrentDBDef();
    return new DfSqlFileRunnerExecute(runInfo, getDataSource()) {

        protected DfOutsideSqlChecker _outsideSqlChecker;

        @Override
        protected String filterSql(String sql) {
            // /- - - - - - - - - - - - - - - - - - - - - - - - - -
            // check parameter comments in the SQL before filtering
            // - - - - - - - - - -/
            checkParameterComment(_sqlFile, sql);
            // filter comments if it needs.
            if (!currentDBDef.dbway().isBlockCommentSupported()) {
                sql = removeBlockComment(sql);
            }
            if (!currentDBDef.dbway().isLineCommentSupported()) {
                sql = removeLineComment(sql);
            }
            return super.filterSql(sql);
        }

        protected String removeBlockComment(final String sql) {
            return Srl.removeBlockComment(sql);
        }

        protected String removeLineComment(final String sql) {
            return Srl.removeLineComment(sql);
        }

        @Override
        protected boolean isTargetSql(String sql) {
            final String entityName = getEntityName(sql);
            if (entityName != null && nonTargetMark.equalsIgnoreCase(entityName)) {
                // non-target SQL
                _nonTargetSqlFileSet.add(_sqlFile);
                _log.info("...Skipping the SQL by non-target mark '" + nonTargetMark + "'");
                return false;
            }
            return super.isTargetSql(sql);
        }

        @Override
        protected void traceSql(String sql) {
            _log.info("SQL:" + ln() + sql);
        }

        @Override
        protected void traceResult(int goodSqlCount, int totalSqlCount) {
            _log.info(" -> success=" + goodSqlCount + " failure=" + (totalSqlCount - goodSqlCount) + ln());
        }

        protected String getEntityName(final String sql) {
            return getTargetString(sql, "#");
        }

        protected String getTargetString(final String sql, final String mark) {
            final List<String> targetList = getTargetList(sql, mark);
            return !targetList.isEmpty() ? targetList.get(0) : null;
        }

        protected List<String> getTargetList(final String sql, final String mark) {
            if (sql == null || sql.trim().length() == 0) {
                String msg = "The sql is invalid: " + sql;
                throw new IllegalArgumentException(msg);
            }
            final List<String> betweenBeginEndMarkList = getListBetweenBeginEndMark(sql, "--" + mark, mark);
            if (!betweenBeginEndMarkList.isEmpty()) {
                return betweenBeginEndMarkList;
            } else {
                // basically for MySQL
                return getListBetweenBeginEndMark(sql, "-- " + mark, mark);
            }
        }

        protected List<String> getListBetweenBeginEndMark(String targetStr, String beginMark, String endMark) {
            final List<ScopeInfo> scopeList = Srl.extractScopeList(targetStr, beginMark, endMark);
            final List<String> resultList = DfCollectionUtil.newArrayList();
            for (ScopeInfo scope : scopeList) {
                resultList.add(scope.getContent());
            }
            return resultList;
        }

        protected void checkParameterComment(File sqlFile, String sql) {
            final DfOutsideSqlProperties outsideSqlProp = getOutsideSqlProperties();
            if (outsideSqlProp.isSuppressParameterCommentCheck()) {
                return;
            }
            if (_outsideSqlChecker == null) {
                _outsideSqlChecker = createOutsideSqlChecker(outsideSqlProp);
            }
            _outsideSqlChecker.check(sqlFile.getName(), sql);
        }
    };
}
Also used : DfOutsideSqlProperties(org.dbflute.properties.DfOutsideSqlProperties) DfSqlFileRunnerExecute(org.dbflute.helper.jdbc.sqlfile.DfSqlFileRunnerExecute) DfOutsideSqlChecker(org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker) DBDef(org.dbflute.dbway.DBDef) ScopeInfo(org.dbflute.util.Srl.ScopeInfo) File(java.io.File) DfSpecifiedSqlFile(org.dbflute.task.bs.assistant.DfSpecifiedSqlFile)

Example 5 with DfOutsideSqlChecker

use of org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker in project dbflute-core by dbflute.

the class OutsideSqlCheckerTest method test_check_parameterComment.

public void test_check_parameterComment() {
    // ## Arrange ##
    DfOutsideSqlChecker ker = new DfOutsideSqlChecker();
    String fn = "test.sql";
    // ## Act ##
    try {
        ker.check(fn, "-- #df:entity#\n-- !df:pmb!\nfoo /*IF pmb.memberId != null*/bar");
        // ## Assert ##
        fail();
    } catch (EndCommentNotFoundException e) {
        // OK
        log(e.getMessage());
    }
    // ## Act ##
    try {
        ker.check(fn, "-- #df:entity#\n-- !df:pmb!\nfoo /*IF */bar/*END*/");
        // ## Assert ##
        fail();
    } catch (IfCommentConditionEmptyException e) {
        // OK
        log(e.getMessage());
    }
}
Also used : IfCommentConditionEmptyException(org.dbflute.twowaysql.exception.IfCommentConditionEmptyException) DfOutsideSqlChecker(org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker) EndCommentNotFoundException(org.dbflute.twowaysql.exception.EndCommentNotFoundException)

Aggregations

DfOutsideSqlChecker (org.dbflute.logic.outsidesqltest.DfOutsideSqlChecker)8 File (java.io.File)1 DBDef (org.dbflute.dbway.DBDef)1 DfCustomizeEntityMarkInvalidException (org.dbflute.exception.DfCustomizeEntityMarkInvalidException)1 DfParameterBeanMarkInvalidException (org.dbflute.exception.DfParameterBeanMarkInvalidException)1 DfSqlFileRunnerExecute (org.dbflute.helper.jdbc.sqlfile.DfSqlFileRunnerExecute)1 DfLanguageImplStyle (org.dbflute.logic.generate.language.implstyle.DfLanguageImplStyle)1 DfOutsideSqlProperties (org.dbflute.properties.DfOutsideSqlProperties)1 DfSpecifiedSqlFile (org.dbflute.task.bs.assistant.DfSpecifiedSqlFile)1 EndCommentNotFoundException (org.dbflute.twowaysql.exception.EndCommentNotFoundException)1 IfCommentConditionEmptyException (org.dbflute.twowaysql.exception.IfCommentConditionEmptyException)1 IfCommentUnsupportedExpressionException (org.dbflute.twowaysql.exception.IfCommentUnsupportedExpressionException)1 ScopeInfo (org.dbflute.util.Srl.ScopeInfo)1