Search in sources :

Example 1 with AbstractFreeSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder in project dal by ctripcorp.

the class AbstractFreeSqlBuilderMeltdownTest method validate.

public void validate(String exp, String expected) throws SQLException {
    AbstractFreeSqlBuilder builder = new AbstractFreeSqlBuilder().setLogicDbName(logicDbName);
    // equal equalNull between betweenNull in inNull like likeNull isNull isNotNull AND OR NOT ( )
    String[] tokens = exp.split(" ");
    for (String token : tokens) {
        switch(token) {
            case "equal":
                builder.equal("a");
                break;
            case "equalNull":
                builder.equal("a").when(false);
                break;
            case "like":
                builder.like("a");
                break;
            case "likeNull":
                builder.like("a").when(false);
                break;
            case "isNull":
                builder.isNull("a");
                break;
            case "isNotNull":
                builder.isNotNull("a");
                break;
            case "in":
                builder.in("a");
                break;
            case "notIn":
                builder.notIn("a");
                break;
            case "inNull":
                builder.in("a").when(false);
                break;
            case "notInNull":
                builder.notIn("a").when(false);
                break;
            case "between":
                builder.between("a");
                break;
            case "betweenNull":
                builder.between("a").when(false);
                break;
            case "AND":
                builder.and();
                break;
            case "OR":
                builder.or();
                break;
            case "NOT":
                builder.not();
                break;
            case "(":
                builder.leftBracket();
                break;
            case ")":
                builder.rightBracket();
                break;
            default:
                Assert.fail("Unknown token: " + token);
        }
    }
    Assert.assertEquals(expected, builder.build());
}
Also used : AbstractFreeSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder)

Example 2 with AbstractFreeSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder in project dal by ctripcorp.

the class AbstractFreeSqlBuilderMeltdownWithSpaceSkipDisabledTest method validate.

public void validate(String exp, String expected) throws SQLException {
    AbstractFreeSqlBuilder builder = new AbstractFreeSqlBuilder().setLogicDbName(logicDbName);
    builder.disableSpaceSkipping();
    // equal equalNull between betweenNull in inNull like likeNull isNull isNotNull AND OR NOT ( )
    String[] tokens = exp.split(" ");
    for (String token : tokens) {
        switch(token) {
            case "equal":
                builder.equal("a");
                break;
            case "equalNull":
                builder.equal("a").ignoreNull(null);
                break;
            case "like":
                builder.like("a");
                break;
            case "likeNull":
                builder.like("a").ignoreNull(null);
                break;
            case "isNull":
                builder.isNull("a");
                break;
            case "isNotNull":
                builder.isNotNull("a");
                break;
            case "in":
                builder.in("a");
                break;
            case "notIn":
                builder.notIn("a");
                break;
            case "inNull":
                builder.in("a").ignoreNull(null);
                break;
            case "notInNull":
                builder.notIn("a").ignoreNull(null);
                break;
            case "between":
                builder.between("a");
                break;
            case "betweenNull":
                builder.between("a").ignoreNull(null);
                break;
            case "AND":
                builder.and();
                break;
            case "OR":
                builder.or();
                break;
            case "NOT":
                builder.not();
                break;
            case "(":
                builder.leftBracket();
                break;
            case ")":
                builder.rightBracket();
                break;
            default:
                Assert.fail("Unknown token: " + token);
        }
    }
    Assert.assertEquals(expected, builder.build());
}
Also used : AbstractFreeSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder)

Example 3 with AbstractFreeSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder in project dal by ctripcorp.

the class AbstractFreeSqlBuilderTest method testAppendCondition.

@Test
public void testAppendCondition() {
    AbstractFreeSqlBuilder test = create();
    test.appendWhen(true, template);
    assertEquals(template, test.build());
    test = create();
    test.appendWhen(false, template);
    assertEquals(EMPTY, test.build());
}
Also used : AbstractFreeSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder) Test(org.junit.Test)

Example 4 with AbstractFreeSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder in project dal by ctripcorp.

the class AbstractFreeSqlBuilderTest method testAppendTable.

@Test
public void testAppendTable() {
    String noShardTable = "noShard";
    AbstractFreeSqlBuilder test = create();
    test.appendTable(noShardTable);
    test.setLogicDbName(logicDbName);
    test.setHints(new DalHints());
    assertEquals("[" + noShardTable + "]", test.build());
    test = create();
    test.appendTable(tableName);
    test.setLogicDbName(logicDbName);
    test.setHints(new DalHints().inTableShard(1));
    assertEquals("[" + tableName + "_1]", test.build());
    test = create();
    test.appendTable(tableName, template);
    test.setLogicDbName(logicDbName);
    test.setHints(new DalHints().inTableShard(1));
    assertEquals("[" + tableName + "_1] AS " + template, test.build());
    test = create();
    test.append(table(tableName).as(template));
    test.setLogicDbName(logicDbName);
    test.setHints(new DalHints().inTableShard(1));
    assertEquals("[" + tableName + "_1] AS " + template, test.build());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) AbstractFreeSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder) Test(org.junit.Test)

Example 5 with AbstractFreeSqlBuilder

use of com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder in project dal by ctripcorp.

the class AbstractFreeSqlBuilderTest method testIsNotNull.

@Test
public void testIsNotNull() {
    AbstractFreeSqlBuilder test = create();
    test.isNotNull(template);
    test.setLogicDbName(logicDbName);
    test.setHints(new DalHints());
    assertEquals(wrappedTemplate + " IS NOT NULL", test.build());
    assertEquals(0, test.buildParameters().size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) AbstractFreeSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder) Test(org.junit.Test)

Aggregations

AbstractFreeSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder)55 Test (org.junit.Test)51 DalHints (com.ctrip.platform.dal.dao.DalHints)17 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)8 SQLException (java.sql.SQLException)5 Text (com.ctrip.platform.dal.dao.sqlbuilder.AbstractFreeSqlBuilder.Text)4 ArrayList (java.util.ArrayList)3 Expression (com.ctrip.platform.dal.dao.sqlbuilder.Expressions.Expression)2 Clause (com.ctrip.platform.dal.dao.sqlbuilder.Clause)1