Search in sources :

Example 1 with SqlFileSelectQuery

use of org.seasar.doma.jdbc.query.SqlFileSelectQuery in project doma by domaframework.

the class DomainSingleResultHandlerTest method testHandle.

@Test
public void testHandle() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("x"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("01-2345-6789"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    DomainSingleResultHandler<String, PhoneNumber> handler = new DomainSingleResultHandler<>(_PhoneNumber.getSingletonInternal());
    PhoneNumber result = handler.handle(resultSet, query, (i, next) -> {
    }).get();
    assertEquals("01-2345-6789", result.getValue());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) SqlFileUtil(org.seasar.doma.internal.jdbc.util.SqlFileUtil) BeforeEach(org.junit.jupiter.api.BeforeEach) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) PhoneNumber(example.domain.PhoneNumber) example.domain._PhoneNumber(example.domain._PhoneNumber) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) SqlLogType(org.seasar.doma.jdbc.SqlLogType) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) Method(java.lang.reflect.Method) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) PhoneNumber(example.domain.PhoneNumber) example.domain._PhoneNumber(example.domain._PhoneNumber) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 2 with SqlFileSelectQuery

use of org.seasar.doma.jdbc.query.SqlFileSelectQuery in project doma by domaframework.

the class EntityIterationHandlerTest method testHandle.

@Test
public void testHandle() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "aaa"));
    resultSet.rows.add(new RowData(2, "bbb"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    EntityIterationHandler<Emp, Integer> handler = new EntityIterationHandler<>(_Emp.getSingletonInternal(), new IterationCallback<Emp, Integer>() {

        private int count;

        @Override
        public Integer iterate(Emp target, IterationContext iterationContext) {
            count++;
            return count;
        }
    });
    Integer result = handler.handle(resultSet, query, (i, next) -> {
    }).get();
    assertEquals(new Integer(2), result);
}
Also used : SqlFileUtil(org.seasar.doma.internal.jdbc.util.SqlFileUtil) BeforeEach(org.junit.jupiter.api.BeforeEach) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) Emp(example.entity.Emp) IterationCallback(org.seasar.doma.jdbc.IterationCallback) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) IterationContext(org.seasar.doma.jdbc.IterationContext) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) SqlLogType(org.seasar.doma.jdbc.SqlLogType) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) example.entity._Emp(example.entity._Emp) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) Method(java.lang.reflect.Method) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) RowData(org.seasar.doma.internal.jdbc.mock.RowData) IterationContext(org.seasar.doma.jdbc.IterationContext) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 3 with SqlFileSelectQuery

use of org.seasar.doma.jdbc.query.SqlFileSelectQuery in project doma by domaframework.

the class EntityIterationHandlerTest method testHandle_exits.

@Test
public void testHandle_exits() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "aaa"));
    resultSet.rows.add(new RowData(2, "bbb"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    EntityIterationHandler<Emp, Integer> handler = new EntityIterationHandler<>(_Emp.getSingletonInternal(), new IterationCallback<Emp, Integer>() {

        private int count;

        @Override
        public Integer iterate(Emp target, IterationContext iterationContext) {
            iterationContext.exit();
            count++;
            return count;
        }
    });
    Integer result = handler.handle(resultSet, query, (i, next) -> {
    }).get();
    assertEquals(new Integer(1), result);
}
Also used : SqlFileUtil(org.seasar.doma.internal.jdbc.util.SqlFileUtil) BeforeEach(org.junit.jupiter.api.BeforeEach) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) Emp(example.entity.Emp) IterationCallback(org.seasar.doma.jdbc.IterationCallback) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) IterationContext(org.seasar.doma.jdbc.IterationContext) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) SqlLogType(org.seasar.doma.jdbc.SqlLogType) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) example.entity._Emp(example.entity._Emp) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) Method(java.lang.reflect.Method) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) RowData(org.seasar.doma.internal.jdbc.mock.RowData) IterationContext(org.seasar.doma.jdbc.IterationContext) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 4 with SqlFileSelectQuery

use of org.seasar.doma.jdbc.query.SqlFileSelectQuery in project doma by domaframework.

the class EntitySingleResultHandlerTest method testHandle.

@Test
public void testHandle() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "aaa"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    EntitySingleResultHandler<Emp> handler = new EntitySingleResultHandler<>(_Emp.getSingletonInternal());
    Emp emp = handler.handle(resultSet, query, (i, next) -> {
    }).get();
    assertEquals(1, emp.getId());
    assertEquals("aaa", emp.getName());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) SqlFileUtil(org.seasar.doma.internal.jdbc.util.SqlFileUtil) BeforeEach(org.junit.jupiter.api.BeforeEach) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) AssertionUtil.assertEquals(org.seasar.doma.internal.util.AssertionUtil.assertEquals) Emp(example.entity.Emp) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) SqlLogType(org.seasar.doma.jdbc.SqlLogType) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) example.entity._Emp(example.entity._Emp) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) Method(java.lang.reflect.Method) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 5 with SqlFileSelectQuery

use of org.seasar.doma.jdbc.query.SqlFileSelectQuery in project doma by domaframework.

the class BasicResultListHandlerTest method testHandle_NonSingleColumnException.

@Test
public void testHandle_NonSingleColumnException() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("x"));
    metaData.columns.add(new ColumnMetaData("y"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData("aaa", "bbb"));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setMethod(method);
    query.setResultEnsured(true);
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    BasicResultListHandler<String> handler = new BasicResultListHandler<>(StringWrapper::new);
    try {
        handler.handle(resultSet, query, (i, next) -> {
        });
        fail();
    } catch (NonSingleColumnException expected) {
    }
}
Also used : StringWrapper(org.seasar.doma.wrapper.StringWrapper) RowData(org.seasar.doma.internal.jdbc.mock.RowData) SqlFileSelectQuery(org.seasar.doma.jdbc.query.SqlFileSelectQuery) NonSingleColumnException(org.seasar.doma.jdbc.NonSingleColumnException) MockResultSetMetaData(org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData) MockResultSet(org.seasar.doma.internal.jdbc.mock.MockResultSet) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Aggregations

SqlFileSelectQuery (org.seasar.doma.jdbc.query.SqlFileSelectQuery)22 Test (org.junit.jupiter.api.Test)18 ColumnMetaData (org.seasar.doma.internal.jdbc.mock.ColumnMetaData)17 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)17 MockResultSetMetaData (org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData)17 RowData (org.seasar.doma.internal.jdbc.mock.RowData)16 Emp (example.entity.Emp)13 example.entity._Emp (example.entity._Emp)13 Method (java.lang.reflect.Method)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 TestInfo (org.junit.jupiter.api.TestInfo)8 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)8 SqlFileUtil (org.seasar.doma.internal.jdbc.util.SqlFileUtil)8 SqlLogType (org.seasar.doma.jdbc.SqlLogType)8 List (java.util.List)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 NonUniqueResultException (org.seasar.doma.jdbc.NonUniqueResultException)6 StringWrapper (org.seasar.doma.wrapper.StringWrapper)5 BigDecimal (java.math.BigDecimal)4 Assertions.fail (org.junit.jupiter.api.Assertions.fail)4