Search in sources :

Example 1 with DalQueryDao

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

the class PartialQueryQueryDaoTest method findFreeFirstBigger.

// Result set is bigger than entity
public FreeEntityPartialPojo findFreeFirstBigger(String name, List<Integer> cityIds, DalHints hints) throws SQLException {
    DalQueryDao queryDao = new DalQueryDao(DATA_BASE);
    DalDefaultJpaMapper<FreeEntityPartialPojo> freeEntityPojoRowMapper = new DalDefaultJpaMapper<>(FreeEntityPartialPojo.class);
    hints = DalHints.createIfAbsent(hints);
    FreeSelectSqlBuilder<FreeEntityPartialPojo> builder = new FreeSelectSqlBuilder<>(dbCategory);
    builder.setTemplate("SELECT * FROM Person WHERE name LIKE ? and CityId in (?) ORDER BY name");
    StatementParameters parameters = new StatementParameters();
    int i = 1;
    parameters.setSensitive(i++, "name", Types.VARCHAR, name);
    i = parameters.setSensitiveInParameter(i, "cityIds", Types.INTEGER, cityIds);
    builder.mapWith(freeEntityPojoRowMapper).requireFirst().nullable();
    return (FreeEntityPartialPojo) queryDao.query(builder, parameters, hints);
}
Also used : FreeSelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalDefaultJpaMapper(com.ctrip.platform.dal.dao.helper.DalDefaultJpaMapper) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao)

Example 2 with DalQueryDao

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

the class DalColumnMapRowMapperTest method testDalColumnMapRowMapperEmpty.

@Test
public void testDalColumnMapRowMapperEmpty() {
    try {
        StatementParameters parameters = new StatementParameters();
        DalClient client = DalClientFactory.getClient(DATABASE_NAME);
        List<Map<String, Object>> result1 = client.query(sqlNoResult, parameters, new DalHints(), new DalRowMapperExtractor<Map<String, Object>>(new DalColumnMapRowMapper()));
        assertEquals(0, result1.size());
        DalQueryDao dao = new DalQueryDao(DATABASE_NAME);
        List<Map<String, Object>> result = dao.query(sqlNoResult, parameters, hints, new DalColumnMapRowMapper());
        assertEquals(0, result.size());
    } catch (Exception e) {
        fail();
    }
}
Also used : DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalColumnMapRowMapper(com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao) Map(java.util.Map) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 3 with DalQueryDao

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

the class DalColumnMapRowMapperTest method testDalColumnMapRowMapperOne.

@Test
public void testDalColumnMapRowMapperOne() {
    try {
        StatementParameters parameters = new StatementParameters();
        parameters.set(1, Types.INTEGER, 3);
        DalQueryDao dao = new DalQueryDao(DATABASE_NAME);
        List<Map<String, Object>> result = dao.query(sqlObject, parameters, hints, new DalColumnMapRowMapper());
        assertEquals(1, result.size());
    } catch (Exception e) {
        fail();
    }
}
Also used : StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalColumnMapRowMapper(com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao) Map(java.util.Map) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 4 with DalQueryDao

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

the class DalWatcherTest method testQueryObjectWhenShutdown.

@Test
public void testQueryObjectWhenShutdown() throws Exception {
    final DalQueryDao client = new DalQueryDao(DATABASE_NAME);
    Runnable shutdown = new Runnable() {

        public void run() {
            while (true) {
                DalClientFactory.shutdownFactory();
            }
        }
    };
    Runnable query = new Runnable() {

        public void run() {
            while (true) {
                String sql = "SELECT quantity FROM " + TABLE_NAME;
                StatementParameters param = new StatementParameters();
                DalHints hints = new DalHints();
                try {
                    System.out.println("query");
                    client.queryFrom(sql, param, hints, Integer.class, 0, 10);
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }
    };
    new Thread(shutdown).start();
    new Thread(query).start();
    ;
    int i = 100;
    while (i-- > 0) {
        System.out.println("waiting");
        Thread.sleep(1 * 10000);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao) Test(org.junit.Test)

Example 5 with DalQueryDao

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

the class BaseTransactionAnnoClass method testQueryFail.

private void testQueryFail(String db) {
    try {
        new DalQueryDao(db).query(query, new StatementParameters(), new DalHints(), Integer.class);
        fail();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao)

Aggregations

DalQueryDao (com.ctrip.platform.dal.dao.DalQueryDao)20 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)19 DalHints (com.ctrip.platform.dal.dao.DalHints)13 Test (org.junit.Test)9 SQLException (java.sql.SQLException)7 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)6 DalDefaultJpaMapper (com.ctrip.platform.dal.dao.helper.DalDefaultJpaMapper)4 DalColumnMapRowMapper (com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper)3 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 DalClient (com.ctrip.platform.dal.dao.DalClient)1 HashMap (java.util.HashMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BeforeClass (org.junit.BeforeClass)1