Search in sources :

Example 1 with NamedSqlQuery

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);
}
Also used : Connection(java.sql.Connection) NamedSqlQuery(org.simpleflatmapper.jdbc.named.NamedSqlQuery) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 2 with NamedSqlQuery

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());
}
Also used : NamedSqlQuery(org.simpleflatmapper.jdbc.named.NamedSqlQuery) Test(org.junit.Test)

Example 3 with NamedSqlQuery

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();
}
Also used : NamedParameter(org.simpleflatmapper.jdbc.named.NamedParameter) NamedSqlQuery(org.simpleflatmapper.jdbc.named.NamedSqlQuery)

Example 4 with NamedSqlQuery

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();
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) NamedSqlQuery(org.simpleflatmapper.jdbc.named.NamedSqlQuery) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 5 with NamedSqlQuery

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);
}
Also used : Connection(java.sql.Connection) NamedSqlQuery(org.simpleflatmapper.jdbc.named.NamedSqlQuery) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Aggregations

NamedSqlQuery (org.simpleflatmapper.jdbc.named.NamedSqlQuery)12 Test (org.junit.Test)10 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 NamedParameter (org.simpleflatmapper.jdbc.named.NamedParameter)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 ResultSet (java.sql.ResultSet)1 SqlParameter (org.springframework.jdbc.core.SqlParameter)1