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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations