Java tutorial
/* * Copyright 2007-2107 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.ymate.platform.persistence.jdbc.operator; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.util.ArrayList; import java.util.List; import net.ymate.platform.base.YMP; import net.ymate.platform.commons.i18n.I18N; import net.ymate.platform.commons.util.RuntimeUtils; import net.ymate.platform.persistence.base.OperatorException; import net.ymate.platform.persistence.jdbc.IConnectionHolder; import net.ymate.platform.persistence.jdbc.JDBC; import net.ymate.platform.persistence.jdbc.base.AccessorEventContext; import net.ymate.platform.persistence.jdbc.base.IAccessor; import net.ymate.platform.persistence.jdbc.base.IAccessorCfgEvent; import net.ymate.platform.persistence.jdbc.base.SqlParameter; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.StopWatch; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * <p> * AbstractOperator * </p> * <p> * ?????? * </p> * * @author (suninformation@163.com) * @version 0.0.0 * <table style="border:1px solid gray;"> * <tr> * <th width="100px">?</th><th width="100px"></th><th * width="100px"></th><th width="100px"></th> * </tr> * <!-- Table ?? --> * <tr> * <td>0.0.0</td> * <td></td> * <td></td> * <td>2011-9-22?10:19:53</td> * </tr> * </table> */ public abstract class AbstractOperator implements IOperator { private static final Log _LOG = LogFactory.getLog(AbstractOperator.class); /** * SQL? */ private String __sql; /** * ?? */ private IConnectionHolder __currentConnection; /** * ??? */ private IAccessorCfgEvent __config; /** * SQL?? */ private List<SqlParameter> __parameters = new ArrayList<SqlParameter>(); /** * ??? */ private long __expenseTime = 0; /** * ? */ private boolean __isExecuted; /** * ? SQL ? */ private boolean __isShowSql; /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#addParameter(net.ymate.platform.persistence.jdbc.base.SqlParameter) */ public void addParameter(SqlParameter parameter) { if (parameter != null) { this.__parameters.add(parameter); } } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#addParameter(java.lang.Object) */ public void addParameter(Object parameter) { if (parameter == null) { this.__parameters.add(new SqlParameter(Types.VARCHAR, null)); } else { this.__parameters.add(new SqlParameter(parameter)); } } /** * ?? * * @return ?-1 * @throws OperatorException * @throws SQLException */ protected abstract int __execute() throws OperatorException, SQLException; /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#execute() */ public void execute() throws OperatorException { if (!this.__isExecuted) { if (StringUtils.isBlank(this.__sql)) { throw new OperatorException( I18N.formatMessage(YMP.__LSTRING_FILE, null, null, "ymp.jdbc.sql_null")); } else { try { StopWatch _time = new StopWatch(); _time.start(); int _recordSize = this.__execute(); _time.stop(); this.setExpenseTime(_time.getTime()); if (JDBC.isShowSQL) { _LOG.info(I18N.formatMessage(YMP.__LSTRING_FILE, null, null, "ymp.jdbc.show_sql", this.getSql(), __parametersToString(), _recordSize, this.getExpenseTime())); } } catch (SQLException e) { throw new OperatorException(I18N.formatMessage(YMP.__LSTRING_FILE, null, null, "ymp.jdbc.sql_exception", this.getSql(), __parametersToString()), RuntimeUtils.unwrapThrow(e)); } finally { this.__isExecuted = true; } } } } /** * @return ?? */ protected abstract String __parametersToString(); /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#execute(net.ymate.platform.persistence.jdbc.connection.ConnectionHolder) */ public void execute(IConnectionHolder conn) throws OperatorException { this.setConnection(conn); this.execute(); } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#getConnection() */ public IConnectionHolder getConnection() { return this.__currentConnection; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#setConnection(net.ymate.platform.persistence.jdbc.connection.ConnectionHolder) */ public void setConnection(IConnectionHolder conn) { this.__currentConnection = conn; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#getParameters() */ public List<SqlParameter> getParameters() { return this.__parameters; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#getSql() */ public String getSql() { return this.__sql; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#setSql(java.lang.String) */ public void setSql(String sql) { this.__sql = sql; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#isConnectionAvailable() */ public boolean isConnectionAvailable() { if (this.__currentConnection != null) { boolean _able = true; try { if (this.__currentConnection.getConnection().isClosed()) { _able = false; } } catch (SQLException e) { _able = false; } return _able; } return false; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#isExecuted() */ public boolean isExecuted() { return this.__isExecuted; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#setShowSql(boolean) */ public void setShowSql(boolean showSql) { this.__isShowSql = showSql; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#isShowSql() */ public boolean isShowSql() { return this.__isShowSql; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#getAccessorCfgEvent() */ public IAccessorCfgEvent getAccessorCfgEvent() { return this.__config; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#setAccessorCfgEvent(net.ymate.platform.persistence.jdbc.base.IAccessorCfgEvent) */ public void setAccessorCfgEvent(IAccessorCfgEvent config) { this.__config = config; } /** * ??? * * @param time */ protected void setExpenseTime(long time) { this.__expenseTime = time; } /* (non-Javadoc) * @see net.ymate.platform.persistence.jdbc.operator.IOperator#getExpenseTime() */ public long getExpenseTime() { return this.__expenseTime; } //================================= // DB Operator Base Code //================================= /** * ? * * @param accessor ? * @param conn ? * @param handler ?? * @param maxRow * @throws OperatorException * @throws SQLException */ protected void doQuery(IAccessor accessor, IConnectionHolder conn, IResultSetHandler<?> handler, int maxRow) throws OperatorException, SQLException { PreparedStatement _statement = null; ResultSet _rs = null; AccessorEventContext _context = null; try { _statement = accessor.getPreparedStatement(conn.getConnection()); if (accessor.getAccessorCfgEvent() != null) { _context = new AccessorEventContext(_statement, false, false); accessor.getAccessorCfgEvent().beforeStatementExecution(_context); } _rs = _statement.executeQuery(); handler.handle(_rs, maxRow); if (accessor.getAccessorCfgEvent() != null && _context != null) { accessor.getAccessorCfgEvent().afterStatementExecution(_context); } } finally { if (_rs != null) { _rs.close(); _rs = null; } if (_statement != null) { _statement.close(); _statement = null; } _context = null; } } /** * ? * * @param accessor ? * @param conn ? * @return ?? * @throws SQLException */ protected int doUpdate(IAccessor accessor, IConnectionHolder conn) throws SQLException { PreparedStatement _statement = null; AccessorEventContext _context = null; try { _statement = accessor.getPreparedStatement(conn.getConnection()); if (accessor.getAccessorCfgEvent() != null) { _context = new AccessorEventContext(_statement, false, false); accessor.getAccessorCfgEvent().beforeStatementExecution(_context); } int _returnValue = _statement.executeUpdate(); if (accessor.getAccessorCfgEvent() != null && _context != null) { accessor.getAccessorCfgEvent().afterStatementExecution(_context); } return _returnValue; } finally { if (_statement != null) { _statement.close(); _statement = null; } _context = null; } } /** * ? * * @param accessor ? * @param conn ? * @return ?? * @throws SQLException */ protected int[] doBatchUpdate(IAccessor accessor, IConnectionHolder conn) throws SQLException { PreparedStatement _statement = null; AccessorEventContext _context = null; try { _statement = accessor.getPreparedStatement(conn.getConnection()); if (accessor.getAccessorCfgEvent() != null) { _context = new AccessorEventContext(_statement, false, false); accessor.getAccessorCfgEvent().beforeStatementExecution(_context); } int _returnValue[] = _statement.executeBatch(); if (accessor.getAccessorCfgEvent() != null && _context != null) { accessor.getAccessorCfgEvent().afterStatementExecution(_context); } _statement.clearBatch(); return _returnValue; } finally { if (_statement != null) { _statement.close(); _statement = null; } _context = null; } } }