Search in sources :

Example 11 with DaoStatement

use of org.nutz.dao.sql.DaoStatement 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

DaoStatement (org.nutz.dao.sql.DaoStatement)11 CallableStatement (java.sql.CallableStatement)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 DaoInterceptorChain (org.nutz.dao.DaoInterceptorChain)2 Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)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 ValueAdaptor (org.nutz.dao.jdbc.ValueAdaptor)1 Pager (org.nutz.dao.pager.Pager)1 Atom (org.nutz.trans.Atom)1 Transaction (org.nutz.trans.Transaction)1