EntityInfo浼樺寲
This commit is contained in:
@@ -148,7 +148,7 @@ public class DataJdbcSource extends DataSqlSource {
|
|||||||
c = c1;
|
c = c1;
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
if (!isTableNotExist(info, se.getSQLState())) throw se;
|
if (!isTableNotExist(info, se.getSQLState())) throw se;
|
||||||
if (info.getTableStrategy() == null) {
|
if (info.getTableStrategy() == null) { //单表
|
||||||
String[] tablesqls = createTableSqls(info);
|
String[] tablesqls = createTableSqls(info);
|
||||||
if (tablesqls == null) throw se;
|
if (tablesqls == null) throw se;
|
||||||
Statement st = conn.createStatement();
|
Statement st = conn.createStatement();
|
||||||
@@ -161,7 +161,7 @@ public class DataJdbcSource extends DataSqlSource {
|
|||||||
st.executeBatch();
|
st.executeBatch();
|
||||||
}
|
}
|
||||||
st.close();
|
st.close();
|
||||||
} else {
|
} else { //分库分表
|
||||||
synchronized (info.disTableLock()) {
|
synchronized (info.disTableLock()) {
|
||||||
final String catalog = conn.getCatalog();
|
final String catalog = conn.getCatalog();
|
||||||
final String newTable = info.getTable(entitys[0]);
|
final String newTable = info.getTable(entitys[0]);
|
||||||
@@ -175,9 +175,9 @@ public class DataJdbcSource extends DataSqlSource {
|
|||||||
info.addDisTable(tableKey);
|
info.addDisTable(tableKey);
|
||||||
} catch (SQLException sqle) { //多进程并发时可能会出现重复建表
|
} catch (SQLException sqle) { //多进程并发时可能会出现重复建表
|
||||||
if (isTableNotExist(info, sqle.getSQLState())) {
|
if (isTableNotExist(info, sqle.getSQLState())) {
|
||||||
if (newTable.indexOf('.') < 0) {
|
if (newTable.indexOf('.') < 0) { //分表的原始表不存在
|
||||||
String[] tablesqls = createTableSqls(info);
|
String[] tablesqls = createTableSqls(info);
|
||||||
if (tablesqls != null) {
|
if (tablesqls != null) { //创建原始表
|
||||||
Statement st = conn.createStatement();
|
Statement st = conn.createStatement();
|
||||||
if (tablesqls.length == 1) {
|
if (tablesqls.length == 1) {
|
||||||
st.execute(tablesqls[0]);
|
st.execute(tablesqls[0]);
|
||||||
@@ -188,7 +188,7 @@ public class DataJdbcSource extends DataSqlSource {
|
|||||||
st.executeBatch();
|
st.executeBatch();
|
||||||
}
|
}
|
||||||
st.close();
|
st.close();
|
||||||
//再执行一遍复制表操作
|
//再执行一遍创建分表操作
|
||||||
st = conn.createStatement();
|
st = conn.createStatement();
|
||||||
st.execute(getTableCopySQL(info, newTable));
|
st.execute(getTableCopySQL(info, newTable));
|
||||||
st.close();
|
st.close();
|
||||||
@@ -204,7 +204,7 @@ public class DataJdbcSource extends DataSqlSource {
|
|||||||
logger.log(Level.SEVERE, "create database(" + newTable.substring(0, newTable.indexOf('.')) + ") error", sqle1);
|
logger.log(Level.SEVERE, "create database(" + newTable.substring(0, newTable.indexOf('.')) + ") error", sqle1);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
//再执行一遍复制表操作
|
//再执行一遍创建分表操作
|
||||||
st = conn.createStatement();
|
st = conn.createStatement();
|
||||||
st.execute(getTableCopySQL(info, newTable));
|
st.execute(getTableCopySQL(info, newTable));
|
||||||
st.close();
|
st.close();
|
||||||
@@ -212,7 +212,7 @@ public class DataJdbcSource extends DataSqlSource {
|
|||||||
} catch (SQLException sqle2) {
|
} catch (SQLException sqle2) {
|
||||||
if (isTableNotExist(info, sqle2.getSQLState())) {
|
if (isTableNotExist(info, sqle2.getSQLState())) {
|
||||||
String[] tablesqls = createTableSqls(info);
|
String[] tablesqls = createTableSqls(info);
|
||||||
if (tablesqls != null) {
|
if (tablesqls != null) { //创建原始表
|
||||||
st = conn.createStatement();
|
st = conn.createStatement();
|
||||||
if (tablesqls.length == 1) {
|
if (tablesqls.length == 1) {
|
||||||
st.execute(tablesqls[0]);
|
st.execute(tablesqls[0]);
|
||||||
@@ -223,7 +223,7 @@ public class DataJdbcSource extends DataSqlSource {
|
|||||||
st.executeBatch();
|
st.executeBatch();
|
||||||
}
|
}
|
||||||
st.close();
|
st.close();
|
||||||
//再执行一遍复制表操作
|
//再执行一遍创建分表操作
|
||||||
st = conn.createStatement();
|
st = conn.createStatement();
|
||||||
st.execute(getTableCopySQL(info, newTable));
|
st.execute(getTableCopySQL(info, newTable));
|
||||||
st.close();
|
st.close();
|
||||||
|
|||||||
@@ -939,6 +939,26 @@ public final class EntityInfo<T> {
|
|||||||
return table;
|
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的表名
|
* 根据主键值获取Entity的表名
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user