All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blade.jdbc.ARC Maven / Gradle / Ivy

There is a newer version: 0.2.2-RELEASE
Show newest version
package com.blade.jdbc;

import java.util.List;

import org.sql2o.Connection;
import org.sql2o.Query;

import blade.kit.logging.Logger;
import blade.kit.logging.LoggerFactory;

public class ARC {

	private static final Logger LOGGER = LoggerFactory.getLogger(ARC.class);
	
	private Connection connection;
	private Object[] args;
	
	private String executeSql;
	
	public ARC(Connection connection, String sql) {
		this.connection = connection;
		this.executeSql = sql;
	}
	
	public ARC(Connection connection, String sql, Object...args) {
		this.connection = connection;
		this.args = args;
		this.executeSql = sql;
		
		for(int i=1, len = args.length; i<=len; i++){
			this.executeSql = this.executeSql.replaceFirst("\\?", ":p" + i); 
		}
	}
	
	private Query buildQuery(){
		Query query = connection.createQuery(executeSql);
		LOGGER.info("execute sql: {}", executeSql);
		if(null != args && args.length > 0){
			query.withParams(args);
		}
		
		return query;
	}
	
	private  void autoAdd(OptType optType, Class type){
		if(optType == OptType.QUERY){
			// 没有select * from xxx
			if(this.executeSql.indexOf("select") == -1){
				String prfix = "select * from " + ARKit.tableName(type) + " ";
				this.executeSql = prfix + this.executeSql;
			}
		}
	}
	
	public  List list(Class type) {
		autoAdd(OptType.QUERY, type);
		Query query = buildQuery();
		List result = null;
		try {
			result = query.executeAndFetch(type);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(false);
		}
		return result;
	}
	
	public  T first(Class type) {
		autoAdd(OptType.QUERY, type);
		Query query = this.buildQuery();
		T result = null;
		try {
			result = query.executeAndFetchFirst(type);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(false);
		}
		return result;
	}
	
	public long count() {
		Query query = this.buildQuery();
		long result = 0;
		try {
			result = query.executeAndFetchFirst(Long.class);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(false);
		}
		return result;
	}
	
	public Connection next() {
		Query query = this.buildQuery();
		return query.executeUpdate();
	}
	
	public int commit() {
		try {
			Query query = this.buildQuery();
			int result = query.executeUpdate().getResult();
			return result;
		} catch (Exception e) {
			if(null != connection){
				connection.rollback();
			}
			e.printStackTrace();
		} finally {
			close(true);
		}
		return 0;
	}
	
	public Object key() {
		try {
			Query query = this.buildQuery();
			Object result = query.executeUpdate().getKey();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(true);
		}
		return null;
	}
	
	private void close(boolean isCommit){
		if(null != connection){
			if(isCommit){
				connection.commit();
			}
			connection.close();
		}
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy