EntityInfo浼樺寲

This commit is contained in:
Redkale
2022-12-26 11:12:55 +08:00
parent bcb2a89388
commit ef6b90c2f6
2 changed files with 28 additions and 8 deletions

View File

@@ -148,7 +148,7 @@ public class DataJdbcSource extends DataSqlSource {
c = c1;
} catch (SQLException se) {
if (!isTableNotExist(info, se.getSQLState())) throw se;
if (info.getTableStrategy() == null) {
if (info.getTableStrategy() == null) { //单表
String[] tablesqls = createTableSqls(info);
if (tablesqls == null) throw se;
Statement st = conn.createStatement();
@@ -161,7 +161,7 @@ public class DataJdbcSource extends DataSqlSource {
st.executeBatch();
}
st.close();
} else {
} else { //分库分表
synchronized (info.disTableLock()) {
final String catalog = conn.getCatalog();
final String newTable = info.getTable(entitys[0]);
@@ -175,9 +175,9 @@ public class DataJdbcSource extends DataSqlSource {
info.addDisTable(tableKey);
} catch (SQLException sqle) { //多进程并发时可能会出现重复建表
if (isTableNotExist(info, sqle.getSQLState())) {
if (newTable.indexOf('.') < 0) {
if (newTable.indexOf('.') < 0) { //分表的原始表不存在
String[] tablesqls = createTableSqls(info);
if (tablesqls != null) {
if (tablesqls != null) { //创建原始表
Statement st = conn.createStatement();
if (tablesqls.length == 1) {
st.execute(tablesqls[0]);
@@ -188,7 +188,7 @@ public class DataJdbcSource extends DataSqlSource {
st.executeBatch();
}
st.close();
//再执行一遍复制表操作
//再执行一遍创建分表操作
st = conn.createStatement();
st.execute(getTableCopySQL(info, newTable));
st.close();
@@ -204,7 +204,7 @@ public class DataJdbcSource extends DataSqlSource {
logger.log(Level.SEVERE, "create database(" + newTable.substring(0, newTable.indexOf('.')) + ") error", sqle1);
}
try {
//再执行一遍复制表操作
//再执行一遍创建分表操作
st = conn.createStatement();
st.execute(getTableCopySQL(info, newTable));
st.close();
@@ -212,7 +212,7 @@ public class DataJdbcSource extends DataSqlSource {
} catch (SQLException sqle2) {
if (isTableNotExist(info, sqle2.getSQLState())) {
String[] tablesqls = createTableSqls(info);
if (tablesqls != null) {
if (tablesqls != null) { //创建原始表
st = conn.createStatement();
if (tablesqls.length == 1) {
st.execute(tablesqls[0]);
@@ -223,7 +223,7 @@ public class DataJdbcSource extends DataSqlSource {
st.executeBatch();
}
st.close();
//再执行一遍复制表操作
//再执行一遍创建分表操作
st = conn.createStatement();
st.execute(getTableCopySQL(info, newTable));
st.close();

View File

@@ -939,6 +939,26 @@ public final class EntityInfo<T> {
return table;
}
public Map<String, List<Serializable>> getTableMap(Serializable... pks) {
if (tableStrategy == null) return Utility.ofMap(table, Utility.ofList(pks));
Map<String, List<Serializable>> map = new LinkedHashMap<>();
for (Serializable pk : pks) {
String t = getTable(pk);
map.computeIfAbsent(t, k -> new ArrayList()).add(pk);
}
return map;
}
public Map<String, List<T>> getTableMap(T... entitys) {
if (tableStrategy == null) return Utility.ofMap(table, Utility.ofList(entitys));
Map<String, List<T>> map = new LinkedHashMap<>();
for (T entity : entitys) {
String t = getTable(entity);
map.computeIfAbsent(t, k -> new ArrayList()).add(entity);
}
return map;
}
/**
* 根据主键值获取Entity的表名
*