Search in sources :

Example 46 with SelectSqlBuilder

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

the class SelectSqlBuilderTest method testMySQLSelectCount.

@Test
public void testMySQLSelectCount() throws SQLException {
    List<String> in = new ArrayList<String>();
    in.add("12");
    in.add("12");
    SelectSqlBuilder builder = new SelectSqlBuilder();
    builder.selectCount();
    builder.equal("a", "paramValue", Types.INTEGER);
    builder.and().in("b", in, Types.INTEGER);
    builder.and().like("b", "in", Types.INTEGER);
    builder.and().betweenNullable("c", "paramValue1", "paramValue2", Types.INTEGER);
    builder.and().betweenNullable("d", null, "paramValue2", Types.INTEGER);
    builder.and().isNull("sss");
    builder.orderBy("PeopleID", false);
    builder.requireFirst();
    builder.from("People").setDatabaseCategory(DatabaseCategory.MySql);
    String sql = builder.build();
    String expect_sql = "SELECT COUNT(1) FROM `People` " + "WHERE `a` = ? AND `b` in ( ? ) AND `b` LIKE ? AND `c` BETWEEN ? AND ? " + "AND `sss` IS NULL ORDER BY `PeopleID` DESC LIMIT 1";
    Assert.assertEquals(expect_sql, sql);
}
Also used : ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) Test(org.junit.Test)

Example 47 with SelectSqlBuilder

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

the class AbstractBuilderTest method validate.

public void validate(String exp, String expected) throws SQLException {
    SelectSqlBuilder builder = new SelectSqlBuilder();
    builder.from("People").setDatabaseCategory(DatabaseCategory.SqlServer);
    // 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", "", Types.INTEGER);
                break;
            case "equalNull":
                builder.equalNullable("a", null, Types.INTEGER);
                break;
            case "like":
                builder.like("a", "", Types.INTEGER);
                break;
            case "likeNull":
                builder.likeNullable("a", null, Types.INTEGER);
                break;
            case "isNull":
                builder.isNull("a");
                break;
            case "isNotNull":
                builder.isNotNull("a");
                break;
            case "in":
                List<?> l = new ArrayList<>();
                builder.in("a", l, Types.INTEGER);
                break;
            case "between":
                builder.between("a", "", "", Types.INTEGER);
                break;
            case "inNull":
                builder.inNullable("a", null, Types.INTEGER);
                break;
            case "betweenNull":
                builder.betweenNullable("a", null, null, Types.INTEGER);
                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.getWhereExp());
}
Also used : ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)

Example 48 with SelectSqlBuilder

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

the class SelectSqlBuilderTest method testMySQLFirstNew.

@Test
public void testMySQLFirstNew() throws SQLException {
    List<String> in = new ArrayList<String>();
    in.add("12");
    in.add("12");
    SelectSqlBuilder builder = new SelectSqlBuilder();
    builder.select("PeopleID", "Name", "CityID");
    builder.equal("a", "paramValue", Types.INTEGER);
    builder.and().in("b", in, Types.INTEGER);
    builder.and().like("b", "in", Types.INTEGER);
    builder.and().betweenNullable("c", "paramValue1", "paramValue2", Types.INTEGER);
    builder.and().betweenNullable("d", null, "paramValue2", Types.INTEGER);
    builder.and().isNull("sss");
    builder.orderBy("PeopleID", false);
    builder.requireFirst();
    builder.from("People").setDatabaseCategory(DatabaseCategory.MySql);
    String sql = builder.build();
    String expect_sql = "SELECT `PeopleID`, `Name`, `CityID` FROM `People` " + "WHERE `a` = ? AND `b` in ( ? ) AND `b` LIKE ? AND `c` BETWEEN ? AND ? " + "AND `sss` IS NULL ORDER BY `PeopleID` DESC LIMIT 1";
    Assert.assertEquals(expect_sql, sql);
}
Also used : ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) Test(org.junit.Test)

Example 49 with SelectSqlBuilder

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

the class SelectSqlBuilderTest method testSQLServerNew.

@Test
public void testSQLServerNew() throws SQLException {
    List<String> in = new ArrayList<String>();
    in.add("12");
    in.add("12");
    SelectSqlBuilder builder = new SelectSqlBuilder();
    builder.select("PeopleID", "Name", "CityID");
    builder.equal("a", "paramValue", Types.INTEGER);
    builder.and().in("b", in, Types.INTEGER);
    builder.and().like("b", "in", Types.INTEGER);
    builder.and().betweenNullable("c", "paramValue1", "paramValue2", Types.INTEGER);
    builder.and().betweenNullable("d", null, "paramValue2", Types.INTEGER);
    builder.and().isNull("sss");
    builder.orderBy("PeopleID", true);
    builder.from("People").setDatabaseCategory(DatabaseCategory.SqlServer);
    String sql = builder.build();
    String expect_sql = "SELECT [PeopleID], [Name], [CityID] FROM [People] WITH (NOLOCK) " + "WHERE [a] = ? AND [b] in ( ? ) AND [b] LIKE ? AND [c] BETWEEN ? AND ? " + "AND [sss] IS NULL ORDER BY [PeopleID] ASC";
    Assert.assertEquals(expect_sql, sql);
}
Also used : ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) Test(org.junit.Test)

Example 50 with SelectSqlBuilder

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

the class SelectSqlBuilderTest method testMySQLPagination.

@Test
public void testMySQLPagination() throws SQLException {
    List<String> in = new ArrayList<String>();
    in.add("12");
    in.add("12");
    SelectSqlBuilder builder = new SelectSqlBuilder("People", DatabaseCategory.MySql, true);
    builder.select("PeopleID", "Name", "CityID");
    builder.equal("a", "paramValue", Types.INTEGER);
    builder.and().in("b", in, Types.INTEGER);
    builder.and().like("b", "in", Types.INTEGER);
    builder.and().betweenNullable("c", "paramValue1", "paramValue2", Types.INTEGER);
    builder.and().betweenNullable("d", null, "paramValue2", Types.INTEGER);
    builder.and().isNull("sss");
    builder.orderBy("PeopleID", false);
    String sql = builder.build();
    String expect_sql = "SELECT `PeopleID`, `Name`, `CityID` FROM `People` " + "WHERE `a` = ? AND `b` in ( ?, ? ) AND `b` LIKE ? AND `c` BETWEEN ? AND ? " + "AND `sss` IS NULL ORDER BY `PeopleID` DESC limit ?, ?";
    Assert.assertEquals(expect_sql, sql);
}
Also used : ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) Test(org.junit.Test)

Aggregations

SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)93 Test (org.junit.Test)71 ArrayList (java.util.ArrayList)35 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)28 DalHints (com.ctrip.platform.dal.dao.DalHints)15 SQLException (java.sql.SQLException)15 ClientTestModel (com.ctrip.platform.dal.dao.unitbase.ClientTestModel)5 HashSet (java.util.HashSet)5 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)4 DalException (com.ctrip.platform.dal.exceptions.DalException)4 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)3 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)3 LocalDalPropertiesProvider (com.ctrip.platform.dal.dao.configure.LocalDalPropertiesProvider)1 GenTaskByFreeSql (com.ctrip.platform.dal.daogen.entity.GenTaskByFreeSql)1 GenTaskBySqlBuilder (com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)1