This commit is contained in:
2023-10-27 00:11:38 +08:00
parent 31e08a2028
commit f7de0a9349
40 changed files with 431 additions and 194 deletions

View File

@@ -30,7 +30,7 @@ public class DbExecutors {
//System.out.printf("----------------%n countSql:%s%n findSql:%s%n----------------%n", sqls[0], sqls[1]);
CompletableFuture<Integer> countFuture = CompletableFuture.supplyAsync(() -> dbKit.findColumn(sqls[0], int.class));
CompletableFuture<List<Map>> listFuture = CompletableFuture.supplyAsync(() -> dbKit.findList(sqls[1], Map.class));
CompletableFuture<List<Map>> listFuture = CompletableFuture.supplyAsync(() -> dbKit.queryList(sqls[1], Map.class));
List<Map> rows = listFuture.get();
Integer total = countFuture.get();

View File

@@ -1,6 +1,7 @@
package net.tccn.base.dbq.fbean;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@@ -8,7 +9,8 @@ import java.util.List;
* 查询用实体
* @author: liangxianyou at 2018/10/25 14:49.
*/
@Data
@Getter
@Setter
public class FBean {
private String platToken; // 平台token

View File

@@ -1,6 +1,7 @@
package net.tccn.base.dbq.fbean;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@@ -8,7 +9,8 @@ import java.util.List;
* 查询条件实体
* Created by liangxianyou at 2018/12/14 15:34.
*/
@Data
@Getter
@Setter
public class Filter {
private String col;
private String value;

View File

@@ -1,11 +1,13 @@
package net.tccn.base.dbq.fbean;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/**
* Created by liangxianyou at 2018/12/14 15:36.
*/
@Data
@Getter
@Setter
public class Limit {
private int pn;
private int ps;

View File

@@ -1,13 +1,15 @@
package net.tccn.base.dbq.fbean;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
/**
* Created by liangxianyou at 2018/12/14 15:36.
*/
@Data
@Getter
@Setter
public class Order {
private String col;
private int desc;//1 or -1

View File

@@ -1,6 +1,7 @@
package net.tccn.base.dbq.jdbc.api;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import net.tccn.base.arango.Doc;
import org.redkale.convert.ConvertColumn;
@@ -12,7 +13,8 @@ import java.util.List;
*
* @author: liangxianyou at 2018/11/14 12:58.
*/
@Data
@Getter
@Setter
@Table(name = "MetaDb", catalog = "db_meta")
public class DbAccount extends Doc<DbAccount> {
public static DbAccount dao = dao(DbAccount.class);

View File

@@ -61,13 +61,13 @@ public class DbKit implements DbSource {
}
@Override
public <T> List<T> findList(String sql, Class<T> type) {
return dbSource.findList(sql, type);
public <T> List<T> queryList(String sql, Class<T> type) {
return dbSource.queryList(sql, type);
}
@Override
public <T> T findFirst(String sql, Class<T> type) {
return dbSource.findFirst(sql, type);
public <T> T find(String sql, Class<T> type) {
return dbSource.find(sql, type);
}
@Override
@@ -85,20 +85,20 @@ public class DbKit implements DbSource {
dbSource.dropTable(tableName);
}
public void exetute(String sql) {
dbSource.exetute(sql);
public boolean exetute(String sql) {
return dbSource.exetute(sql);
}
// -----------------------------------------
public <T> CompletableFuture<T> findfirstAsync(String sql, Class<T> type) {
return CompletableFuture.supplyAsync(() -> findFirst(sql, type));
public <T> CompletableFuture<T> findAsync(String sql, Class<T> type) {
return CompletableFuture.supplyAsync(() -> find(sql, type));
}
public <T> CompletableFuture<List<T>> findListAsync(String sql, Class<T> type) {
return CompletableFuture.supplyAsync(() -> findList(sql, type));
public <T> CompletableFuture<List<T>> queryListAsync(String sql, Class<T> type) {
return CompletableFuture.supplyAsync(() -> queryList(sql, type));
}
public <T> CompletableFuture<T> queryColumnAsync(String sql, Class<T> type) {
public <T> CompletableFuture<T> findColumnAsync(String sql, Class<T> type) {
return CompletableFuture.supplyAsync(() -> findColumn(sql, type));
}

View File

@@ -13,9 +13,9 @@ public interface DbSource extends IService {
void setDbAccount(DbAccount dbAccount);
void setCatalog(String catelog);
<T> List<T> findList(String sql, Class<T> type);
<T> List<T> queryList(String sql, Class<T> type);
<T> T findFirst(String sql, Class<T> type);
<T> T find(String sql, Class<T> type);
<T> T findColumn(String sql, Class<T> type);
@@ -25,21 +25,21 @@ public interface DbSource extends IService {
//待实现
default <T> void update(String tableName, T t) {}
default int queryInt(String sql) {
default int findInt(String sql) {
return findColumn(sql, int.class);
}
default long queryLong(String sql) {
default long findLong(String sql) {
return findColumn(sql, long.class);
}
default double queryDouble(String sql) {
default double findDouble(String sql) {
return findColumn(sql, double.class);
}
default Date queryDate(String sql) {
default Date findDate(String sql) {
return findColumn(sql, Date.class);
}
void createTable(String sql);
void dropTable(String tableName);
void exetute(String sql);
boolean exetute(String sql);
}

View File

@@ -55,7 +55,7 @@ public class DbSourceMysql implements DbSource {
}
@Override
public <T> List<T> findList(String sql, Class<T> type) {
public <T> List<T> queryList(String sql, Class<T> type) {
Connection connection = connection();
try (
PreparedStatement ps = connection.prepareStatement(sql);
@@ -83,7 +83,7 @@ public class DbSourceMysql implements DbSource {
}
}
}
list.add(Map.class == type ? row : Kv.toBean(row, type));
list.add(Kv.toAs(row, type));
}
return list;
@@ -97,8 +97,8 @@ public class DbSourceMysql implements DbSource {
}
@Override
public <T> T findFirst(String sql, Class<T> type) {
List<T> list = findList(sql, type);
public <T> T find(String sql, Class<T> type) {
List<T> list = queryList(sql, type);
return list.size() > 0 ? list.get(0) : null;
}
@@ -150,12 +150,12 @@ public class DbSourceMysql implements DbSource {
}
@Override
public void exetute(String sql) {
public boolean exetute(String sql) {
Connection connection = connection();
try (
PreparedStatement ps = connection.prepareStatement(sql);
) {
ps.execute();
return ps.execute();
//ps.executeUpdate();
} catch (SQLException e) {
throw new CfgException("SQL 执行失败:", sql);
@@ -166,50 +166,31 @@ public class DbSourceMysql implements DbSource {
//fixme: lxy 处理连接超过8小时失效问题
private Connection connection() {
Connection connection = connection(0);
if (connection != null && catalog != null && !catalog.isEmpty()) {
try {
Connection connection = null;
try {
connection = connection(0);
if (catalog != null && !catalog.isEmpty()) {
connection.setCatalog(catalog); //还回连接的时候是否需要重置catalog 后续观察
} catch (SQLException e) {
e.printStackTrace();
}
} catch (InterruptedException | SQLException e) {
new IllegalArgumentException("获取连接失败", e);
}
return connection;
}
private Connection connection(int n) {
private synchronized Connection connection(int n) throws InterruptedException, SQLException {
LinkedBlockingQueue<Connection> queue = conns.getOrDefault(accountKey, new LinkedBlockingQueue<>(15));
Connection conn = null;
AtomicInteger num = counter.getOrDefault(accountKey, new AtomicInteger(0));
try {
if (queue.size() == 0 && num.get() < 15) {
conn = DriverManager.getConnection(dbAccount.getUrl(), dbAccount.getUser(), dbAccount.getPwd());
int x = num.incrementAndGet();
counter.put(accountKey, num);
System.out.println("创建新的连接:" + x);
} else {
conn = queue.take();
if (conn.isClosed()) {
System.out.println("connetion had closed,");
conn = connection(n);
}
}
} catch (SQLException | InterruptedException e) {
if (e instanceof InterruptedException) {
try {
conn = DriverManager.getConnection(dbAccount.getUrl(), dbAccount.getUser(), dbAccount.getPwd());
num.getAndIncrement();
if (conn != null) {
System.out.println("获取连接异常,并重新创建成功");
}
} catch (SQLException ex) {
new IllegalArgumentException("创建连接失败", e);
}
num.getAndIncrement();
counter.put(accountKey, num);
} else {
new IllegalArgumentException("获取连接失败", e);
if (queue.size() == 0 && num.get() < 15) { // 创建总连接数小于15且暂无可用连接
conn = DriverManager.getConnection(dbAccount.getUrl(), dbAccount.getUser(), dbAccount.getPwd());
int x = num.incrementAndGet();
counter.put(accountKey, num);
System.out.println("创建新的连接:" + x);
} else {
conn = queue.take();
if (!conn.isValid(5)) {
conn = connection(++n);
}
}
conns.put(accountKey, queue);

View File

@@ -87,7 +87,7 @@ public class ParseMysql implements Parser {
//from
StringBuilder bufFrom = new StringBuilder();
bufFrom.append(" from ").append(metaTable.getCatalog()).append(".`").append(metaTable.getName()).append("` `").append(metaTable.getAlias()).append("`");
bufFrom.append(" from `").append(metaTable.getCatalog()).append("`.`").append(metaTable.getName()).append("` `").append(metaTable.getAlias()).append("`");
//left join
if (!Utils.isEmpty(links)) {
links.forEach(x -> {

View File

@@ -1,6 +1,7 @@
package net.tccn.base.dbq.qtask;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import net.tccn.base.arango.Doc;
import javax.persistence.Table;
@@ -8,7 +9,8 @@ import javax.persistence.Table;
/**
* @author: liangxianyou at 2018/11/13 14:59.
*/
@Data
@Getter
@Setter
@Table(name = "DbTask", catalog = "db_meta")
public class DbTask extends Doc<DbTask> {
public static DbTask dao = dao(DbTask.class);

View File

@@ -1,12 +1,14 @@
package net.tccn.base.dbq.table;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/**
* 数据库表的列
* @author: liangxianyou at 2018/10/8 10:59.
*/
@Data
@Getter
@Setter
public class Column {
private String field; //列名称
private String type; //列类型

View File

@@ -1,11 +1,13 @@
package net.tccn.base.dbq.table;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/**
* @author: liangxianyou at 2018/10/17 17:24.
*/
@Data
@Getter
@Setter
public class Field {
private String name;
private String label;

View File

@@ -1,7 +1,8 @@
package net.tccn.base.dbq.table;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@@ -10,7 +11,8 @@ import java.util.List;
* 数据库表.
* @author: liangxianyou at 2018/10/8 10:58.
*/
@Data
@Getter
@Setter
public class Table {
private String catalog; //库名称
private String name; //表名称