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

com.javajy.api.component.DBManager Maven / Gradle / Ivy

The newest version!
package com.javajy.api.component;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON;
import com.javajy.api.e.b;
import com.javajy.api.entity.Database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DBManager {
    private T d;
    private Database e;
    public static DruidDataSource dataSource = null;
    public static Set ext = new HashSet();

    public DBManager(T var1, Database var2) {
        this.d = var1;
        this.e = var2;
        if (ext.size() == 0) {
            ext.add("ip");
            ext.add("port");
            ext.add("db");
        }

        if (dataSource == null) {
            try {
                dataSource = new DruidDataSource();
                dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
                dataSource.setUrl("jdbc:mysql://" + var2.getIp() + ":" + var2.getPort() + "/" + var2.getDb() + "?characterEncoding=UTF8&useSSL=false&autoReconnect=true&failOverReadOnly=false");
                dataSource.setUsername(var2.getUsername());
                dataSource.setPassword(var2.getPassword());
                dataSource.setInitialSize(10);
                dataSource.setMaxActive(15);
                dataSource.setMinIdle(10);
                dataSource.setMaxWait(5000L);
                dataSource.setPoolPreparedStatements(false);
                dataSource.setValidationQuery("select 1");
                dataSource.setKeepAlive(true);
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

    }

    public DruidPooledConnection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (Exception var2) {
            dataSource = null;
            System.out.println(var2.getMessage());
            return null;
        }
    }

    private String b(String var1, String key) {
        Statement var2 = null;
        ResultSet var3 = null;
        DruidPooledConnection var4 = this.getConnection();

        try {
            var2 = var4.createStatement();
            var3 = var2.executeQuery(var1);
            String var5 = null;
            if(key != null) {
            	var5 = b.a(var3, key);
            }else {
            	var5 = b.a(var3, ext);
            }
            return var5;
        } catch (Exception var9) {
            System.out.println(var9.getMessage() + ", connection disconnected");
        } finally {
        	a(var4, var2, var3);
        }
        return null;
    }

    private Integer c(String var1) {
        Statement var2 = null;
        DruidPooledConnection var3 = this.getConnection();

        try {
            var2 = var3.createStatement();
            Integer var4 = var2.executeUpdate(var1);
            return var4;
        } catch (Exception var8) {
        	System.out.println(var1);
            var8.printStackTrace();
        } finally {
            a(var3, var2, (ResultSet)null);
        }

        return null;
    }
    
    private Integer c(List sqlList) {
		Statement stat = null;
		DruidPooledConnection connection = getConnection();
		try {
			connection.setAutoCommit(false);
			stat = getConnection().createStatement();
			Statement stmt = connection.createStatement();
			for(String sql : sqlList) {
				stmt.executeUpdate(sql);
			}
			connection.commit();
		} catch (Exception e) {
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			a(connection, stat, (ResultSet)null);
		}
		return null;
	}

    public List list() {
    	String str1 = "select * from " + b.l(this.d.getClass().getSimpleName());
        String str2 = b.b(this.d);
        String str3 = b.g(this.d);
        String str4 = b.h(this.d);
        if (str2 != null && !str2.equals("end"))
          str1 = str1 + " where " + str2; 
        str1 = str1 + str3;
        str1 = str1 + str4;
        //System.out.println("EXECUTE SQL:" + str1);
        String str5 = b(str1, null); 
        return (List) JSON.parseArray(str5, this.d.getClass());
    }

    public int listCount() {
        String var1 = "select count(id) as totalCount from " + b.l(this.d.getClass().getSimpleName());
        String var2 = b.b(this.d);
        if (var2 != null && !var2.equals("end")) {
            var1 = var1 + " where " + var2;
        }
        String var6 = this.b(var1, "totalCount");
        return Integer.parseInt(var6);
    }

    public List query(String var1) {
        String var2 = this.b(var1, null);
        return (List) JSON.parseArray(var2, this.d.getClass());
    }

    public Integer update() {
        String var1 = b.c(this.d);
        return this.c(var1);
    }

    public Integer save() {
        String var1 = b.d(this.d);
        return this.c(var1);
    }
    
    public Integer saveList(List dataList) {
		System.out.println("data list count:"+dataList.size());
		List sqlList = new ArrayList();
		for(T t : dataList) {
			sqlList.add(b.c(t));
		}
		if(sqlList != null && sqlList.size() > 0) {
			System.out.println("sql:"+sqlList.get(0));
		}else {
			System.out.println("No sql found");
		}
		return c(sqlList);
	}

    public Integer delete() {
        String var1 = b.e(this.d);
        return this.c(var1);
    }

    private static void a(DruidPooledConnection var0, Statement var1, ResultSet var2) {
        try {
            if (var2 != null) {
                var2.close();
            }

            if (var1 != null) {
                var1.close();
            }

            if (var0 != null) {
                var0.close();
            }
        } catch (SQLException var4) {
            var4.printStackTrace();
        }

    }

    public List getById() {
        String var1 = "select * from " + b.l(this.d.getClass().getSimpleName());
        String var2 = b.b(this.d);
        if (var2 != null && !var2.equals("end")) {
            var1 = var1 + " where " + var2;
        }

        String var6 = this.b(var1, null);
        return (List) JSON.parseArray(var6, this.d.getClass());
    }

    public List getOptionList() {
        String var1 = "select * from " + b.l(this.d.getClass().getSimpleName());
        String var2 = b.b(this.d);
        if (var2 != null && !var2.equals("end")) {
            var1 = var1 + " where " + var2;
        }

        String var6 = this.b(var1, null);
        return (List) JSON.parseArray(var6, this.d.getClass());
    }
}