use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class QueryPreparerTest method testInsertAndSelectWithPreparedStatementMapper.
@Test
public void testInsertAndSelectWithPreparedStatementMapper() throws SQLException {
NamedSqlQuery insertQuery = NamedSqlQuery.parse("INSERT INTO test_db_object(id, name, email, creation_time, type_ordinal, type_name) values(?, ?, ?, ?, ?, ?) ");
NamedSqlQuery selectQuery = NamedSqlQuery.parse("select id, name, email, creation_time, type_ordinal, type_name from TEST_DB_OBJECT where id = ? ");
QueryPreparer<DbObject> insertQueryPreparer = jdbcMapperFactory.from(DbObject.class).to(insertQuery);
QueryPreparer<DbObject> selectQueryPreparer = jdbcMapperFactory.from(DbObject.class).to(selectQuery);
DbObject dbObject = DbObject.newInstance();
Connection connection = DbHelper.objectDb();
try {
PreparedStatement ps = insertQueryPreparer.prepare(connection).bind(dbObject);
try {
ps.execute();
} finally {
ps.close();
}
final QueryBinder<DbObject> queryBinder = selectQueryPreparer.prepare(connection);
ps = selectQueryPreparer.prepareStatement(connection);
try {
queryBinder.bindTo(dbObject, ps);
ResultSet resultSet = ps.executeQuery();
assertTrue(resultSet.next());
assertEquals(dbObject, JdbcMapperFactory.newInstance().newMapper(DbObject.class).map(resultSet));
} finally {
ps.close();
}
} finally {
connection.close();
}
}
use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class NamedSqlQueryTest method testParseSqlWithQuestionMarks.
@Test
public void testParseSqlWithQuestionMarks() {
String sql = "SELECT * FROM table where field = ? and field2 = ?";
NamedSqlQuery namedSqlQuery = NamedSqlQuery.parse(sql);
assertEquals(2, namedSqlQuery.getParametersSize());
assertEquals("field", namedSqlQuery.getParameter(0).getName());
assertEquals("field2", namedSqlQuery.getParameter(1).getName());
assertEquals("SELECT * FROM table where field = ? and field2 = ?", namedSqlQuery.toSqlQuery());
}
use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class NamedSqlQueryTest method testParseSqlWithOnlyNamedParams.
@Test
public void testParseSqlWithOnlyNamedParams() {
String sql = ":field2";
NamedSqlQuery namedSqlQuery = NamedSqlQuery.parse(sql);
assertEquals(1, namedSqlQuery.getParametersSize());
assertEquals("field2", namedSqlQuery.getParameter(0).getName());
assertEquals("?", namedSqlQuery.toSqlQuery());
}
use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class NamedSqlQueryTest method testParseSqlWithNamedParam.
@Test
public void testParseSqlWithNamedParam() {
String sql = "SELECT * FROM table where field = :field and field = :field2";
NamedSqlQuery namedSqlQuery = NamedSqlQuery.parse(sql);
assertEquals(2, namedSqlQuery.getParametersSize());
assertEquals("field", namedSqlQuery.getParameter(0).getName());
assertEquals("field2", namedSqlQuery.getParameter(1).getName());
assertEquals("SELECT * FROM table where field = ? and field = ?", namedSqlQuery.toSqlQuery());
}
use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.
the class NamedSqlQueryTest method testParseSqlWithNoParams.
@Test
public void testParseSqlWithNoParams() {
String sql = "select 1 from dual";
NamedSqlQuery namedSqlQuery = NamedSqlQuery.parse(sql);
assertEquals(0, namedSqlQuery.getParametersSize());
assertEquals("select 1 from dual", namedSqlQuery.toSqlQuery());
}
Aggregations