Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package see613.core.db;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import see613.core.base.ModelData;
- import see613.core.log.ILog;
- public class SQLDriver {
- private static final Integer QUERY_TYPE_UPDATE = 1;
- private static final Integer QUERY_TYPE_OTHERS = 2;
- public ResultSet resultSet = null;
- public ArrayList<ModelData> resultData = null;
- public ModelData rowData = null;
- public Long lastInsertId = -1L;
- public int affectedRows = 0;
- private PreparedStatement statement = null;
- private Integer queryType;
- private String sql = "";
- private int paramsCount = 0;
- private ILog log;
- private IConnectionPool connectionPool;
- /**
- * @throws Exception
- */
- public SQLDriver prepareStatement(String ... params) throws Exception {
- Connection connect = connectionPool.get();
- resultSet = null;
- statement = connect.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
- for(int i=paramsCount; i<(paramsCount + params.length); i++) {
- statement.setString(i+1, params[i]);
- }
- paramsCount += params.length;
- return this;
- }
- /**
- * @throws Exception
- */
- public SQLDriver run() throws Exception {
- Connection connect = statement.getConnection();
- try {
- if ( queryType == QUERY_TYPE_UPDATE ) {
- affectedRows = statement.executeUpdate();
- }
- else {
- resultSet = statement.executeQuery();
- }
- } catch (Exception e) {
- close();
- connectionPool.close(connect);
- throw e;
- }
- connectionPool.add(connect);
- String sqlString = statement.toString();
- sqlString = sqlString.substring( sqlString.indexOf(": ") + 2 );
- getLog().notice("query executed: "+ sqlString, 2);
- return this;
- }
- /**
- * @throws Exception
- */
- public SQLDriver query(Integer type, String sql, Boolean runImmediately, String ... params) throws Exception {
- queryType = type;
- paramsCount = 0;
- this.sql = sql;
- prepareStatement(params);
- if ( runImmediately ) {
- run();
- }
- return this;
- }
- /**
- * @throws Exception
- */
- public SQLDriver query(Integer type, String sql, String ... params) throws Exception {
- query(type, sql, true, params);
- return this;
- }
- /**
- * @throws Exception
- */
- public SQLDriver query(String sql, Boolean runImmediately, String ... params) throws Exception {
- query(QUERY_TYPE_OTHERS, sql, runImmediately, params);
- return this;
- }
- /**
- * @throws Exception
- */
- public SQLDriver query(String sql, String ... params) throws Exception {
- query(QUERY_TYPE_OTHERS, sql, params);
- return this;
- }
- /**
- * @throws Exception
- */
- public SQLDriver update(String sql, Boolean runImmediately, String ... params) throws Exception {
- query(QUERY_TYPE_UPDATE, sql, runImmediately, params);
- return this;
- }
- /**
- * @throws Exception
- */
- public SQLDriver update(String sql, String ... params) throws Exception {
- update(sql, true, params);
- return this;
- }
- /**
- * @throws SQLException
- */
- public long getInsertId() throws SQLException {
- if (affectedRows == 0) {
- throw new SQLException( "insert failed, no rows affected" );
- }
- ResultSet generatedKeys = statement.getGeneratedKeys();
- if ( generatedKeys.next() ) {
- lastInsertId = generatedKeys.getLong(1);
- return lastInsertId;
- } else {
- throw new SQLException("insert failed, no generated key obtained");
- }
- }
- /**
- * @throws Exception
- */
- protected ModelData getRow(ResultSetMetaData metaData, int columnCount) throws Exception {
- rowData = null;
- boolean rowExist = true;
- try {
- if (metaData == null) {
- metaData = resultSet.getMetaData();
- columnCount = metaData.getColumnCount();
- rowExist = resultSet.next();
- }
- rowData = new ModelData();
- if ( rowExist ) {
- for(int i=1; i<=columnCount; ++i){
- rowData.putString( metaData.getColumnLabel(i), resultSet.getString(i) );
- }
- }
- } catch (SQLException e) {
- close();
- throw e;
- }
- return rowData;
- }
- /**
- * @throws Exception
- */
- public ModelData getRow() throws Exception {
- return getRow(null, 0);
- }
- /**
- * @throws Exception
- */
- public ArrayList<ModelData> getResult() throws Exception {
- resultData = new ArrayList<ModelData>();
- try {
- ResultSetMetaData metaData = resultSet.getMetaData();
- resultData = new ArrayList<ModelData>();
- int columnCount = metaData.getColumnCount();
- while ( resultSet.next() ) {
- resultData.add( getRow( metaData, columnCount ) );
- }
- } catch (SQLException e) {
- close();
- throw e;
- }
- return resultData;
- }
- /**
- * @throws Exception
- */
- public void close() throws Exception {
- if (resultSet != null) {
- resultSet.close();
- }
- if (statement != null) {
- statement.close();
- }
- }
- public SQLDriver addBoolean(boolean value) throws SQLException {
- paramsCount++;
- statement.setBoolean(paramsCount, value);
- return this;
- }
- public SQLDriver addInt(int value) throws SQLException {
- paramsCount++;
- statement.setInt(paramsCount, value);
- return this;
- }
- public SQLDriver addFloat(float value) throws SQLException {
- paramsCount++;
- statement.setFloat(paramsCount, value);
- return this;
- }
- public SQLDriver addString(String value) throws SQLException {
- paramsCount++;
- statement.setString(paramsCount, value);
- return this;
- }
- public void setLog(ILog value) {
- log = value;
- }
- protected ILog getLog() {
- if ( log == null ) {
- throw new NullPointerException("log is null");
- }
- return log;
- }
- public void setConnectionPool(IConnectionPool value) {
- connectionPool = value;
- }
- protected IConnectionPool getConnectionPool() {
- if ( connectionPool == null ) {
- throw new NullPointerException("connectionPool is null");
- }
- return connectionPool;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement