Search in sources :

Example 11 with NamedSqlQuery

use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.

the class NamedSqlQueryTest method testParseComplexSql.

@Test
public void testParseComplexSql() {
    String sql = "SELECT a, b, c FROM table " + "where " + "a in (?, :futon) " + "and b=lower(?) " + "and c = :c " + "and upper(lower(d)) = ? " + "and upper(lower(e)) = upper(lower(?)) " + "and lower(f) = upper(lower(?))";
    NamedSqlQuery namedSqlQuery = NamedSqlQuery.parse(sql);
    assertEquals(7, namedSqlQuery.getParametersSize());
    assertEquals("a", namedSqlQuery.getParameter(0).getName());
    assertEquals("futon", namedSqlQuery.getParameter(1).getName());
    assertEquals("b", namedSqlQuery.getParameter(2).getName());
    assertEquals("c", namedSqlQuery.getParameter(3).getName());
    assertEquals("d", namedSqlQuery.getParameter(4).getName());
    assertEquals("e", namedSqlQuery.getParameter(5).getName());
    assertEquals("f", namedSqlQuery.getParameter(6).getName());
    assertEquals("SELECT a, b, c FROM table " + "where a in (?, ?) " + "and b=lower(?) " + "and c = ? " + "and upper(lower(d)) = ? " + "and upper(lower(e)) = upper(lower(?)) " + "and lower(f) = upper(lower(?))", namedSqlQuery.toSqlQuery());
}
Also used : NamedSqlQuery(org.simpleflatmapper.jdbc.named.NamedSqlQuery) Test(org.junit.Test)

Example 12 with NamedSqlQuery

use of org.simpleflatmapper.jdbc.named.NamedSqlQuery in project SimpleFlatMapper by arnaudroger.

the class MappingSqlQuery method extractParameters.

private SqlParameter[] extractParameters(String sql) {
    NamedSqlQuery query = NamedSqlQuery.parse(sql);
    SqlParameter[] params = new SqlParameter[query.getParametersSize()];
    for (int i = 0; i < params.length; i++) {
        NamedParameter namedParameter = query.getParameter(i);
        params[i] = new SqlParameter(namedParameter.getName(), 0);
    }
    return params;
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) NamedParameter(org.simpleflatmapper.jdbc.named.NamedParameter) NamedSqlQuery(org.simpleflatmapper.jdbc.named.NamedSqlQuery)

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