This commit is contained in:
@@ -334,19 +334,16 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
try {
|
try {
|
||||||
if (!info.isVirtualEntity()) {
|
if (!info.isVirtualEntity()) {
|
||||||
final String sql = info.getInsertSQL(values[0]);
|
final String sql = info.getInsertSQL(values[0]);
|
||||||
final PreparedStatement prestmt = info.autoGenerated
|
|
||||||
? conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) : conn.prepareStatement(sql);
|
|
||||||
final Class primaryType = info.getPrimary().type();
|
final Class primaryType = info.getPrimary().type();
|
||||||
final Attribute primary = info.getPrimary();
|
final Attribute primary = info.getPrimary();
|
||||||
final boolean distributed = info.distributed;
|
final boolean distributed = info.distributed;
|
||||||
Attribute<T, Serializable>[] attrs = info.insertAttributes;
|
Attribute<T, Serializable>[] attrs = info.insertAttributes;
|
||||||
String[] sqls = null;
|
String[] sqls = new String[values.length];
|
||||||
if (distributed && !info.initedPrimaryValue && primaryType.isPrimitive()) { //由DataSource生成主键
|
if (distributed && !info.initedPrimaryValue && primaryType.isPrimitive()) { //由DataSource生成主键
|
||||||
synchronized (info) {
|
synchronized (info) {
|
||||||
if (!info.initedPrimaryValue) { //初始化最大主键值
|
if (!info.initedPrimaryValue) { //初始化最大主键值
|
||||||
try {
|
try {
|
||||||
Statement stmt = conn.createStatement();
|
Statement stmt = conn.createStatement();
|
||||||
|
|
||||||
ResultSet rs = stmt.executeQuery("SELECT MAX(" + info.getPrimarySQLColumn() + ") FROM " + info.getTable(values[0]));
|
ResultSet rs = stmt.executeQuery("SELECT MAX(" + info.getPrimarySQLColumn() + ") FROM " + info.getTable(values[0]));
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
if (primaryType == int.class) {
|
if (primaryType == int.class) {
|
||||||
@@ -366,42 +363,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (writeListener == null) {
|
PreparedStatement prestmt = createInsertPreparedStatement(conn, sql, info, sqls, values);
|
||||||
for (final T value : values) {
|
|
||||||
int i = 0;
|
|
||||||
if (distributed) info.createPrimaryValue(value);
|
|
||||||
for (Attribute<T, Serializable> attr : attrs) {
|
|
||||||
prestmt.setObject(++i, attr.get(value));
|
|
||||||
}
|
|
||||||
prestmt.addBatch();
|
|
||||||
}
|
|
||||||
} else { //调用writeListener回调接口
|
|
||||||
char[] sqlchars = sql.toCharArray();
|
|
||||||
sqls = new String[values.length];
|
|
||||||
CharSequence[] ps = new CharSequence[attrs.length];
|
|
||||||
int index = 0;
|
|
||||||
for (final T value : values) {
|
|
||||||
int i = 0;
|
|
||||||
if (distributed) info.createPrimaryValue(value);
|
|
||||||
for (Attribute<T, Serializable> attr : attrs) {
|
|
||||||
Object a = attr.get(value);
|
|
||||||
ps[i] = FilterNode.formatToString(a);
|
|
||||||
prestmt.setObject(++i, a);
|
|
||||||
}
|
|
||||||
prestmt.addBatch();
|
|
||||||
//-----------------------------
|
|
||||||
StringBuilder sb = new StringBuilder(128);
|
|
||||||
i = 0;
|
|
||||||
for (char ch : sqlchars) {
|
|
||||||
if (ch == '?') {
|
|
||||||
sb.append(ps[i++]);
|
|
||||||
} else {
|
|
||||||
sb.append(ch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sqls[index++] = sb.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
prestmt.executeBatch();
|
prestmt.executeBatch();
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
@@ -416,6 +378,8 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
info.tables.add(newTable);
|
info.tables.add(newTable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
prestmt.close();
|
||||||
|
prestmt = createInsertPreparedStatement(conn, sql, info, sqls, values);
|
||||||
prestmt.executeBatch();
|
prestmt.executeBatch();
|
||||||
}
|
}
|
||||||
if (writeListener != null) writeListener.insert(sqls);
|
if (writeListener != null) writeListener.insert(sqls);
|
||||||
@@ -469,6 +433,49 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <T> PreparedStatement createInsertPreparedStatement(final Connection conn, final String sql,
|
||||||
|
final EntityInfo<T> info, final String[] sqls, T... values) throws SQLException {
|
||||||
|
Attribute<T, Serializable>[] attrs = info.insertAttributes;
|
||||||
|
final PreparedStatement prestmt = info.autoGenerated
|
||||||
|
? conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) : conn.prepareStatement(sql);
|
||||||
|
if (writeListener == null) {
|
||||||
|
for (final T value : values) {
|
||||||
|
int i = 0;
|
||||||
|
if (info.distributed) info.createPrimaryValue(value);
|
||||||
|
for (Attribute<T, Serializable> attr : attrs) {
|
||||||
|
prestmt.setObject(++i, attr.get(value));
|
||||||
|
}
|
||||||
|
prestmt.addBatch();
|
||||||
|
}
|
||||||
|
} else { //调用writeListener回调接口
|
||||||
|
char[] sqlchars = sql.toCharArray();
|
||||||
|
CharSequence[] ps = new CharSequence[attrs.length];
|
||||||
|
int index = 0;
|
||||||
|
for (final T value : values) {
|
||||||
|
int i = 0;
|
||||||
|
if (info.distributed) info.createPrimaryValue(value);
|
||||||
|
for (Attribute<T, Serializable> attr : attrs) {
|
||||||
|
Object a = attr.get(value);
|
||||||
|
ps[i] = FilterNode.formatToString(a);
|
||||||
|
prestmt.setObject(++i, a);
|
||||||
|
}
|
||||||
|
prestmt.addBatch();
|
||||||
|
//-----------------------------
|
||||||
|
StringBuilder sb = new StringBuilder(128);
|
||||||
|
i = 0;
|
||||||
|
for (char ch : sqlchars) {
|
||||||
|
if (ch == '?') {
|
||||||
|
sb.append(ps[i++]);
|
||||||
|
} else {
|
||||||
|
sb.append(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sqls[index++] = sb.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return prestmt;
|
||||||
|
}
|
||||||
|
|
||||||
public <T> void insertCache(Class<T> clazz, T... values) {
|
public <T> void insertCache(Class<T> clazz, T... values) {
|
||||||
if (values.length == 0) return;
|
if (values.length == 0) return;
|
||||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||||
|
|||||||
Reference in New Issue
Block a user