Search in sources :

Example 6 with NamedSqlQuery

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

Example 7 with NamedSqlQuery

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

Example 8 with NamedSqlQuery

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

Example 9 with NamedSqlQuery

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

Example 10 with NamedSqlQuery

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