Search in sources :

Example 1 with BindValue

use of org.seasar.doma.internal.jdbc.mock.BindValue in project doma by domaframework.

the class InsertCommandTest method testExecute.

@Test
public void testExecute() throws Exception {
    Emp emp = new Emp();
    emp.setId(1);
    emp.setName("hoge");
    emp.setSalary(new BigDecimal(1000));
    emp.setVersion(10);
    AutoInsertQuery<Emp> query = new AutoInsertQuery<>(_Emp.getSingletonInternal());
    query.setMethod(getClass().getDeclaredMethod(method.getName()));
    query.setConfig(runtimeConfig);
    query.setEntity(emp);
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    int rows = new InsertCommand(query).execute();
    query.complete();
    assertEquals(1, rows);
    String sql = runtimeConfig.dataSource.connection.preparedStatement.sql;
    assertEquals("insert into EMP (ID, NAME, SALARY, VERSION) values (?, ?, ?, ?)", sql);
    List<BindValue> bindValues = runtimeConfig.dataSource.connection.preparedStatement.bindValues;
    assertEquals(1, bindValues.get(0).getValue());
    assertEquals("hoge", bindValues.get(1).getValue());
    assertEquals(new BigDecimal(1000), bindValues.get(2).getValue());
    assertEquals(10, bindValues.get(3).getValue());
}
Also used : AutoInsertQuery(org.seasar.doma.jdbc.query.AutoInsertQuery) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) BigDecimal(java.math.BigDecimal) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) Test(org.junit.jupiter.api.Test)

Example 2 with BindValue

use of org.seasar.doma.internal.jdbc.mock.BindValue in project doma by domaframework.

the class SelectCommandTest method testExecute_singleResult.

@Test
public void testExecute_singleResult() throws Exception {
    MockResultSetMetaData metaData = new MockResultSetMetaData();
    metaData.columns.add(new ColumnMetaData("id"));
    metaData.columns.add(new ColumnMetaData("name"));
    metaData.columns.add(new ColumnMetaData("salary"));
    metaData.columns.add(new ColumnMetaData("version"));
    MockResultSet resultSet = new MockResultSet(metaData);
    resultSet.rows.add(new RowData(1, "hoge", new BigDecimal(10000), 100));
    runtimeConfig.dataSource.connection = new MockConnection(new MockPreparedStatement(resultSet));
    SqlFileSelectQuery query = new SqlFileSelectQuery();
    query.setConfig(runtimeConfig);
    query.setSqlFilePath(SqlFileUtil.buildPath(getClass().getName(), method.getName()));
    query.addParameter("name", String.class, "hoge");
    query.addParameter("salary", BigDecimal.class, new BigDecimal(10000));
    query.setMethod(getClass().getMethod(method.getName()));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    SelectCommand<Emp> command = new SelectCommand<>(query, new EntitySingleResultHandler<>(_Emp.getSingletonInternal()));
    Emp entity = command.execute();
    query.complete();
    assertNotNull(entity);
    assertEquals(new Integer(1), entity.getId());
    assertEquals("hoge", entity.getName());
    assertEquals(new BigDecimal(10000), entity.getSalary());
    assertEquals(new Integer(100), entity.getVersion());
    List<BindValue> bindValues = runtimeConfig.dataSource.connection.preparedStatement.bindValues;
    BindValue bindValue = bindValues.get(0);
    assertEquals("hoge", bindValue.getValue());
    assertEquals(1, bindValue.getIndex());
    bindValue = bindValues.get(1);
    assertEquals(new BigDecimal(10000), bindValue.getValue());
    assertEquals(2, bindValue.getIndex());
}
Also used : 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) MockPreparedStatement(org.seasar.doma.internal.jdbc.mock.MockPreparedStatement) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) BigDecimal(java.math.BigDecimal) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) RowData(org.seasar.doma.internal.jdbc.mock.RowData) ColumnMetaData(org.seasar.doma.internal.jdbc.mock.ColumnMetaData) Test(org.junit.jupiter.api.Test)

Example 3 with BindValue

use of org.seasar.doma.internal.jdbc.mock.BindValue in project doma by domaframework.

the class UpdateCommandTest method testExecute_OriginalStates.

@Test
public void testExecute_OriginalStates() throws Exception {
    Emp emp = new Emp();
    emp.setId(1);
    emp.setName("hoge");
    emp.setVersion(10);
    Emp states = new Emp();
    states.setId(1);
    states.setName("foo");
    states.setVersion(10);
    emp.originalStates = states;
    AutoUpdateQuery<Emp> query = new AutoUpdateQuery<>(_Emp.getSingletonInternal());
    query.setMethod(getClass().getDeclaredMethod(method.getName()));
    query.setConfig(runtimeConfig);
    query.setEntity(emp);
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    int rows = new UpdateCommand(query).execute();
    query.complete();
    assertEquals(1, rows);
    String sql = runtimeConfig.dataSource.connection.preparedStatement.sql;
    assertEquals("update EMP set NAME = ?, VERSION = ? + 1 where ID = ? and VERSION = ?", sql);
    List<BindValue> bindValues = runtimeConfig.dataSource.connection.preparedStatement.bindValues;
    assertEquals(4, bindValues.size());
    assertEquals("hoge", bindValues.get(0).getValue());
    assertEquals(10, bindValues.get(1).getValue());
    assertEquals(1, bindValues.get(2).getValue());
    assertEquals(10, bindValues.get(3).getValue());
    Emp updatedStates = emp.originalStates;
    assertEquals(1, updatedStates.getId());
    assertEquals("hoge", updatedStates.getName());
    assertNull(updatedStates.getSalary());
    assertEquals(11, updatedStates.getVersion());
}
Also used : Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) AutoUpdateQuery(org.seasar.doma.jdbc.query.AutoUpdateQuery) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) Test(org.junit.jupiter.api.Test)

Example 4 with BindValue

use of org.seasar.doma.internal.jdbc.mock.BindValue in project doma by domaframework.

the class CallableSqlParameterBinderTest method testBind.

@Test
public void testBind() throws Exception {
    MockCallableStatement callableStatement = new MockCallableStatement();
    List<SqlParameter> parameters = new ArrayList<>();
    parameters.add(new BasicSingleResultParameter<>(IntegerWrapper::new));
    parameters.add(new BasicInParameter<>(() -> new org.seasar.doma.wrapper.StringWrapper("aaa")));
    parameters.add(new BasicInOutParameter<>(BigDecimalWrapper::new, new Reference<>(new BigDecimal(10))));
    parameters.add(new BasicOutParameter<>(() -> new org.seasar.doma.wrapper.StringWrapper("bbb"), new Reference<>()));
    CallableSqlParameterBinder binder = new CallableSqlParameterBinder(new MyQuery());
    binder.bind(callableStatement, parameters);
    List<BindValue> bindValues = callableStatement.bindValues;
    assertEquals(2, bindValues.size());
    BindValue bindValue = bindValues.get(0);
    assertEquals(2, bindValue.getIndex());
    assertEquals("aaa", bindValue.getValue());
    bindValue = bindValues.get(1);
    assertEquals(3, bindValue.getIndex());
    assertEquals(new BigDecimal(10), bindValue.getValue());
    List<RegisterOutParameter> registerOutParameters = callableStatement.registerOutParameters;
    assertEquals(3, registerOutParameters.size());
}
Also used : MockCallableStatement(org.seasar.doma.internal.jdbc.mock.MockCallableStatement) SqlParameter(org.seasar.doma.jdbc.SqlParameter) Reference(org.seasar.doma.jdbc.Reference) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) RegisterOutParameter(org.seasar.doma.internal.jdbc.mock.RegisterOutParameter) Test(org.junit.jupiter.api.Test)

Example 5 with BindValue

use of org.seasar.doma.internal.jdbc.mock.BindValue in project doma by domaframework.

the class PreparedSqlParameterBinderTest method testBind.

@Test
public void testBind() throws Exception {
    MockPreparedStatement preparedStatement = new MockPreparedStatement();
    List<BasicInParameter<?>> parameters = new ArrayList<>();
    parameters.add(new BasicInParameter<>(() -> new org.seasar.doma.wrapper.StringWrapper("aaa")));
    parameters.add(new BasicInParameter<>(() -> new org.seasar.doma.wrapper.BigDecimalWrapper(new BigDecimal(10))));
    PreparedSqlParameterBinder binder = new PreparedSqlParameterBinder(new MyQuery());
    binder.bind(preparedStatement, parameters);
    List<BindValue> bindValues = preparedStatement.bindValues;
    assertEquals(2, bindValues.size());
    BindValue bindValue = bindValues.get(0);
    assertEquals(1, bindValue.getIndex());
    assertEquals("aaa", bindValue.getValue());
    bindValue = bindValues.get(1);
    assertEquals(2, bindValue.getIndex());
    assertEquals(new BigDecimal(10), bindValue.getValue());
}
Also used : ArrayList(java.util.ArrayList) MockPreparedStatement(org.seasar.doma.internal.jdbc.mock.MockPreparedStatement) BigDecimal(java.math.BigDecimal) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) BasicInParameter(org.seasar.doma.internal.jdbc.sql.BasicInParameter) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)10 BindValue (org.seasar.doma.internal.jdbc.mock.BindValue)10 Emp (example.entity.Emp)8 example.entity._Emp (example.entity._Emp)8 BigDecimal (java.math.BigDecimal)8 MockPreparedStatement (org.seasar.doma.internal.jdbc.mock.MockPreparedStatement)4 ColumnMetaData (org.seasar.doma.internal.jdbc.mock.ColumnMetaData)3 MockConnection (org.seasar.doma.internal.jdbc.mock.MockConnection)3 MockResultSet (org.seasar.doma.internal.jdbc.mock.MockResultSet)3 MockResultSetMetaData (org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData)3 RowData (org.seasar.doma.internal.jdbc.mock.RowData)3 SqlFileSelectQuery (org.seasar.doma.jdbc.query.SqlFileSelectQuery)3 ArrayList (java.util.ArrayList)2 AutoInsertQuery (org.seasar.doma.jdbc.query.AutoInsertQuery)2 AutoUpdateQuery (org.seasar.doma.jdbc.query.AutoUpdateQuery)2 List (java.util.List)1 Stream (java.util.stream.Stream)1 MockCallableStatement (org.seasar.doma.internal.jdbc.mock.MockCallableStatement)1 RegisterOutParameter (org.seasar.doma.internal.jdbc.mock.RegisterOutParameter)1 BasicInParameter (org.seasar.doma.internal.jdbc.sql.BasicInParameter)1