@Override public void createLockTable() { try { queryRunner.execute(txcSql.lockTableSql()); } catch (SQLException e) { log.error("txc > sql executor > create lock table error.", e); } }
@Override public void createUndoLogTable() { try { queryRunner.execute(txcSql.undoLogTableSql()); } catch (SQLException e) { log.error("txc > sql executor > create undo_log table error.", e); } }
/** * Execute an SQL statement, including a stored procedure call, which does * not return any result sets. * Any parameters which are instances of {@link OutParameter} will be * registered as OUT parameters. * <p> * Use this method when invoking a stored procedure with OUT parameters * that does not return any result sets. If you are not invoking a stored * procedure, or the stored procedure has no OUT parameters, consider using * {@link #update(java.sql.Connection, java.lang.String, java.lang.Object...) }. * If the stored procedure returns result sets, use * {@link #execute(java.sql.Connection, java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }. * * @param conn The connection to use to run the query. * @param sql The SQL to execute. * @param params The query replacement parameters. * @return The number of rows updated. * @throws SQLException if a database access error occurs */ public int execute(Connection conn, String sql, Object... params) throws SQLException { return this.execute(conn, false, sql, params); }
/** * Execute an SQL statement, including a stored procedure call, which * returns one or more result sets. * Any parameters which are instances of {@link OutParameter} will be * registered as OUT parameters. * <p> * Use this method when: a) running SQL statements that return multiple * result sets; b) invoking a stored procedure that return result * sets and OUT parameters. Otherwise you may wish to use * {@link #query(java.sql.Connection, java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) } * (if there are no OUT parameters) or * {@link #execute(java.sql.Connection, java.lang.String, java.lang.Object...) } * (if there are no result sets). * * @param <T> The type of object that the handler returns * @param conn The connection to use to run the query. * @param sql The SQL to execute. * @param rsh The result set handler * @param params The query replacement parameters. * @return A list of objects generated by the handler * @throws SQLException if a database access error occurs */ public <T> List<T> execute(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException { return this.execute(conn, false, sql, rsh, params); }
/** * Execute an SQL statement, including a stored procedure call, which does * not return any result sets. * Any parameters which are instances of {@link OutParameter} will be * registered as OUT parameters. * <p> * Use this method when invoking a stored procedure with OUT parameters * that does not return any result sets. If you are not invoking a stored * procedure, or the stored procedure has no OUT parameters, consider using * {@link #update(java.lang.String, java.lang.Object...) }. * If the stored procedure returns result sets, use * {@link #execute(java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }. * <p> * The <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the update will not be saved. * * @param sql The SQL statement to execute. * @param params Initializes the CallableStatement's parameters (i.e. '?'). * @throws SQLException if a database access error occurs * @return The number of rows updated. */ public int execute(String sql, Object... params) throws SQLException { Connection conn = this.prepareConnection(); return this.execute(conn, true, sql, params); }
/** * Execute an SQL statement, including a stored procedure call, which * returns one or more result sets. * Any parameters which are instances of {@link OutParameter} will be * registered as OUT parameters. * <p> * Use this method when: a) running SQL statements that return multiple * result sets; b) invoking a stored procedure that return result * sets and OUT parameters. Otherwise you may wish to use * {@link #query(java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) } * (if there are no OUT parameters) or * {@link #execute(java.lang.String, java.lang.Object...) } * (if there are no result sets). * * @param <T> The type of object that the handler returns * @param sql The SQL to execute. * @param rsh The result set handler * @param params The query replacement parameters. * @return A list of objects generated by the handler * @throws SQLException if a database access error occurs */ public <T> List<T> execute(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException { Connection conn = this.prepareConnection(); return this.execute(conn, true, sql, rsh, params); }
@Test(expected=SQLException.class) public void testNullConnectionExecute() throws Exception { when(meta.getParameterCount()).thenReturn(2); when(dataSource.getConnection()).thenReturn(null); runner.execute("{call my_proc(?, ?)}", "unit", "test"); }
@Test(expected=SQLException.class) public void testNullSqlExecute() throws Exception { when(meta.getParameterCount()).thenReturn(2); runner.execute(null); }
@Test(expected=SQLException.class) public void testNullSqlExecuteWithResultSet() throws Exception { when(meta.getParameterCount()).thenReturn(2); runner.execute(null, handler); }
@Test(expected=SQLException.class) public void testNullConnectionExecuteWithResultSet() throws Exception { when(meta.getParameterCount()).thenReturn(2); when(dataSource.getConnection()).thenReturn(null); runner.execute("{call my_proc(?, ?)}", handler, "unit", "test"); }
@Test(expected=SQLException.class) public void testNullHandlerExecute() throws Exception { when(meta.getParameterCount()).thenReturn(2); runner.execute("{call my_proc(?, ?)}"); }
@Test(expected=SQLException.class) public void testNullHandlerExecuteWithResultSet() throws Exception { when(meta.getParameterCount()).thenReturn(2); runner.execute("{call my_proc(?, ?)}", (ResultSetHandler)null); }
@Test public void testExecuteWithMultipleResultSets() throws Exception { when(call.execute()).thenReturn(true); when(call.getMoreResults()).thenAnswer(new Answer<Boolean>() { int count = 1; @Override public Boolean answer(InvocationOnMock invocation) { return ++count <= 3; } }); when(meta.getParameterCount()).thenReturn(0); List<Object[]> objects = runner.execute("{call my_proc()}", handler); Assert.assertEquals(3, objects.size()); verify(call, times(1)).execute(); verify(results, times(3)).close(); verify(call, times(1)).close(); // make sure we closed the statement verify(conn, times(1)).close(); // make sure we close the connection }
int result = runner.execute(conn, "{call my_proc(?, ?)}", "unit", "test"); result = runner.execute(conn, "{call my_proc()}"); OutParameter<Integer> intParam = new OutParameter<Integer>(Types.INTEGER, Integer.class); result = runner.execute(conn, "{?= call my_proc()}", intParam); when(call.getObject(1)).thenReturn(4242); intParam.setValue(null); result = runner.execute(conn, "{?= call my_proc(?, ?)}", intParam, "unit", "test"); OutParameter<String> stringParam = new OutParameter<String>(Types.VARCHAR, String.class, "in"); result = runner.execute(conn, "{?= call my_proc(?, ?)}", intParam, "test", stringParam);
runner.execute(conn, "{call my_proc(?, ?)}", handler, "unit", "test"); runner.execute(conn, "{call my_proc()}", handler); OutParameter<Integer> intParam = new OutParameter<Integer>(Types.INTEGER, Integer.class); runner.execute(conn, "{?= call my_proc()}", handler, intParam); when(call.getObject(1)).thenReturn(4242); intParam.setValue(null); runner.execute(conn, "{?= call my_proc(?, ?)}", handler, intParam, "unit", "test"); OutParameter<String> stringParam = new OutParameter<String>(Types.VARCHAR, String.class, "in"); runner.execute(conn, "{?= call my_proc(?, ?)}", handler, intParam, "test", stringParam);
runner.execute("{call my_proc(?, ?)}", handler, "unit", "test"); runner.execute("{call my_proc()}", handler); OutParameter<Integer> intParam = new OutParameter<Integer>(Types.INTEGER, Integer.class); runner.execute("{?= call my_proc()}", handler, intParam); when(call.getObject(1)).thenReturn(4242); intParam.setValue(null); runner.execute("{?= call my_proc(?, ?)}", handler, intParam, "unit", "test"); OutParameter<String> stringParam = new OutParameter<String>(Types.VARCHAR, String.class, "in"); runner.execute("{?= call my_proc(?, ?)}", handler, intParam, "test", stringParam);
int result = runner.execute("{call my_proc(?, ?)}", "unit", "test"); result = runner.execute("{call my_proc()}"); OutParameter<Integer> intParam = new OutParameter<Integer>(Types.INTEGER, Integer.class); result = runner.execute("{?= call my_proc()}", intParam); when(call.getObject(1)).thenReturn(4242); intParam.setValue(null); result = runner.execute("{?= call my_proc(?, ?)}", intParam, "unit", "test"); OutParameter<String> stringParam = new OutParameter<String>(Types.VARCHAR, String.class, "in"); result = runner.execute("{?= call my_proc(?, ?)}", intParam, "test", stringParam);