use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class QueryPreparerTest method testSelectWithInList.
@Test
public void testSelectWithInList() throws SQLException {
NamedSqlQuery selectInListQuery = NamedSqlQuery.parse("select * from Table where name in (?) and id = ? ");
QueryPreparer<QueryParamList> selectInListMapper = jdbcMapperFactory.from(QueryParamList.class).to(selectInListQuery);
Connection conn = mock(Connection.class);
PreparedStatement mps = mock(PreparedStatement.class);
QueryParamList value = new QueryParamList();
value.name = Arrays.asList("name1", "name2");
value.id = 3334;
when(conn.prepareStatement("select * from Table where name in (?, ?) and id = ? ")).thenReturn(mps);
PreparedStatement ps = selectInListMapper.prepare(conn).bind(value);
assertSame(mps, ps);
verify(mps).setString(1, "name1");
verify(mps).setString(2, "name2");
verify(mps).setInt(3, 3334);
}
use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class NamedSqlQueryTest method testInsertQuestionMark.
@Test
public void testInsertQuestionMark() {
String sql = "INSERT INTO table(col1, col2) values(?, ?)";
NamedSqlQuery namedSqlQuery = NamedSqlQuery.parse(sql);
assertEquals(2, namedSqlQuery.getParametersSize());
assertEquals("col1", namedSqlQuery.getParameter(0).getName());
assertEquals("col2", namedSqlQuery.getParameter(1).getName());
assertEquals("INSERT INTO table(col1, col2) values(?, ?)", namedSqlQuery.toSqlQuery());
}
use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class SqlParameterSourceBuilder method buildFactory.
@SuppressWarnings("unchecked")
public SqlParameterSourceFactory<T> buildFactory(String sql) {
NamedSqlQuery namedSqlQuery = NamedSqlQuery.parse(sql);
for (int i = 0; i < namedSqlQuery.getParametersSize(); i++) {
NamedParameter parameter = namedSqlQuery.getParameter(i);
add(parameter.getName());
}
return buildFactory();
}
use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class QueryPreparerTest method testQueryBinderClosePsOnException.
@Test
public void testQueryBinderClosePsOnException() throws SQLException {
NamedSqlQuery query = NamedSqlQuery.parse("select id, name, email, creation_time, type_ordinal, type_name from TEST_DB_OBJECT where id = ? ");
QueryPreparer<DbObject> queryPreparer = jdbcMapperFactory.from(DbObject.class).to(query);
Connection conn = mock(Connection.class);
PreparedStatement ps = mock(PreparedStatement.class);
when(conn.prepareStatement(anyString())).thenReturn(ps);
doThrow(new IllegalStateException()).when(ps).setLong(anyInt(), anyLong());
try {
queryPreparer.prepare(conn).bind(DbObject.newInstance());
fail();
} catch (IllegalStateException e) {
// expected
}
verify(ps).close();
}
use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class QueryPreparerTest method testSelectWithInArray.
@Test
public void testSelectWithInArray() throws SQLException {
NamedSqlQuery selectInListQuery = NamedSqlQuery.parse("select * from Table where name = ? and id in (?) ");
QueryPreparer<QueryParamArray> selectInListMapper = jdbcMapperFactory.from(QueryParamArray.class).to(selectInListQuery);
Connection conn = mock(Connection.class);
PreparedStatement mps = mock(PreparedStatement.class);
QueryParamArray value = new QueryParamArray();
value.name = "nannme";
value.id = new int[] { 3334, 3336 };
when(conn.prepareStatement("select * from Table where name = ? and id in (?, ?) ")).thenReturn(mps);
PreparedStatement ps = selectInListMapper.prepare(conn).bind(value);
assertSame(mps, ps);
verify(mps).setString(1, "nannme");
verify(mps).setInt(2, 3334);
verify(mps).setInt(3, 3336);
}
Aggregations