Search in sources :

Example 1 with DaoExecutor

use of org.nutz.dao.impl.DaoExecutor in project nutz by nutzam.

the class SimpleDaoTest method test_get_sql.

@Test
public void test_get_sql() throws Throwable {
    NutDao dao = new NutDao(ioc.get(javax.sql.DataSource.class));
    final List<String> sqls = new ArrayList<String>();
    final Method m = NutStatement.class.getDeclaredMethod("toStatement", Object[][].class, String.class);
    m.setAccessible(true);
    dao.setExecutor(new DaoExecutor() {

        public void exec(Connection conn, DaoStatement st) {
            String psql = st.toPreparedStatement();
            sqls.add(psql);
            // 事实上根据参数矩阵(getParamMatrix)和Sql语句(toPreparedStatement)就能逐一替换生成
            try {
                String ssql = (String) m.invoke(st, st.getParamMatrix(), psql);
                sqls.add(ssql);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    dao.insert(Pet.create("hi"));
    for (String sql : sqls) {
        System.out.println(sql);
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) DaoStatement(org.nutz.dao.sql.DaoStatement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) DaoExecutor(org.nutz.dao.impl.DaoExecutor) Method(java.lang.reflect.Method) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) Test(org.junit.Test)

Aggregations

Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 Test (org.junit.Test)1 DaoException (org.nutz.dao.DaoException)1 DaoExecutor (org.nutz.dao.impl.DaoExecutor)1 NutDao (org.nutz.dao.impl.NutDao)1 SimpleDataSource (org.nutz.dao.impl.SimpleDataSource)1 DaoStatement (org.nutz.dao.sql.DaoStatement)1