This commit is contained in:
@@ -5,19 +5,19 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.source;
|
package org.redkale.source;
|
||||||
|
|
||||||
import static org.redkale.source.FilterNode.*;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.Method;
|
||||||
import java.net.*;
|
import java.net.URL;
|
||||||
import java.nio.channels.*;
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.*;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import javax.annotation.*;
|
import javax.annotation.Resource;
|
||||||
import javax.sql.*;
|
import javax.sql.ConnectionPoolDataSource;
|
||||||
import javax.xml.stream.*;
|
import javax.xml.stream.*;
|
||||||
|
import static org.redkale.source.FilterNode.formatToString;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -81,8 +81,8 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
|
|
||||||
public DataDefaultSource(final String unitName) throws IOException {
|
public DataDefaultSource(final String unitName) throws IOException {
|
||||||
this(unitName, System.getProperty(DATASOURCE_CONFPATH) == null
|
this(unitName, System.getProperty(DATASOURCE_CONFPATH) == null
|
||||||
? DataDefaultSource.class.getResource("/META-INF/persistence.xml")
|
? DataDefaultSource.class.getResource("/META-INF/persistence.xml")
|
||||||
: new File(System.getProperty(DATASOURCE_CONFPATH)).toURI().toURL());
|
: new File(System.getProperty(DATASOURCE_CONFPATH)).toURI().toURL());
|
||||||
}
|
}
|
||||||
|
|
||||||
public DataDefaultSource(final String unitName, URL url) throws IOException {
|
public DataDefaultSource(final String unitName, URL url) throws IOException {
|
||||||
@@ -186,7 +186,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
static ConnectionPoolDataSource createDataSource(Properties property) {
|
static ConnectionPoolDataSource createDataSource(Properties property) {
|
||||||
try {
|
try {
|
||||||
return createDataSource(property.getProperty(JDBC_SOURCE, property.getProperty(JDBC_DRIVER)),
|
return createDataSource(property.getProperty(JDBC_SOURCE, property.getProperty(JDBC_DRIVER)),
|
||||||
property.getProperty(JDBC_URL), property.getProperty(JDBC_USER), property.getProperty(JDBC_PWD));
|
property.getProperty(JDBC_URL), property.getProperty(JDBC_USER), property.getProperty(JDBC_PWD));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new RuntimeException("(" + property + ") have no jdbc parameters", ex);
|
throw new RuntimeException("(" + property + ") have no jdbc parameters", ex);
|
||||||
}
|
}
|
||||||
@@ -339,15 +339,15 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
if (!info.isVirtualEntity()) {
|
if (!info.isVirtualEntity()) {
|
||||||
final String sql = info.insertSQL;
|
final String sql = info.insertSQL;
|
||||||
final PreparedStatement prestmt = info.autoGenerated
|
final PreparedStatement prestmt = info.autoGenerated
|
||||||
? conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) : conn.prepareStatement(sql);
|
? 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 = null;
|
||||||
if (distributed && !info.initedPrimaryValue && primaryType.isPrimitive()) {
|
if (distributed && !info.initedPrimaryValue && primaryType.isPrimitive()) { //由DataSource生成主键
|
||||||
synchronized (info) {
|
synchronized (info) {
|
||||||
if (!info.initedPrimaryValue) {
|
if (!info.initedPrimaryValue) { //初始化最大主键值
|
||||||
Statement stmt = conn.createStatement();
|
Statement stmt = conn.createStatement();
|
||||||
ResultSet rs = stmt.executeQuery("SELECT MAX(" + info.getPrimarySQLColumn() + ") FROM " + info.getTable());
|
ResultSet rs = stmt.executeQuery("SELECT MAX(" + info.getPrimarySQLColumn() + ") FROM " + info.getTable());
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
@@ -361,7 +361,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
}
|
}
|
||||||
rs.close();
|
rs.close();
|
||||||
stmt.close();
|
stmt.close();
|
||||||
if (info.distributeTables != null) {
|
if (info.distributeTables != null) { //是否还有其他表
|
||||||
for (final Class t : info.distributeTables) {
|
for (final Class t : info.distributeTables) {
|
||||||
EntityInfo<T> infox = loadEntityInfo(t);
|
EntityInfo<T> infox = loadEntityInfo(t);
|
||||||
stmt = conn.createStatement();
|
stmt = conn.createStatement();
|
||||||
@@ -392,7 +392,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
}
|
}
|
||||||
prestmt.addBatch();
|
prestmt.addBatch();
|
||||||
}
|
}
|
||||||
} else {
|
} else { //调用writeListener回调接口
|
||||||
char[] sqlchars = sql.toCharArray();
|
char[] sqlchars = sql.toCharArray();
|
||||||
sqls = new String[values.length];
|
sqls = new String[values.length];
|
||||||
CharSequence[] ps = new CharSequence[attrs.length];
|
CharSequence[] ps = new CharSequence[attrs.length];
|
||||||
@@ -421,7 +421,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
}
|
}
|
||||||
prestmt.executeBatch();
|
prestmt.executeBatch();
|
||||||
if (writeListener != null) writeListener.insert(sqls);
|
if (writeListener != null) writeListener.insert(sqls);
|
||||||
if (info.autoGenerated) {
|
if (info.autoGenerated) { //由数据库自动生成主键值
|
||||||
ResultSet set = prestmt.getGeneratedKeys();
|
ResultSet set = prestmt.getGeneratedKeys();
|
||||||
int i = -1;
|
int i = -1;
|
||||||
while (set.next()) {
|
while (set.next()) {
|
||||||
@@ -437,7 +437,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
}
|
}
|
||||||
prestmt.close();
|
prestmt.close();
|
||||||
//------------------------------------------------------------
|
//------------------------------------------------------------
|
||||||
if (debug.get()) {
|
if (debug.get()) { //打印调试信息
|
||||||
char[] sqlchars = sql.toCharArray();
|
char[] sqlchars = sql.toCharArray();
|
||||||
for (final T value : values) {
|
for (final T value : values) {
|
||||||
//-----------------------------
|
//-----------------------------
|
||||||
@@ -457,10 +457,10 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
}
|
}
|
||||||
logger.finest(info.getType().getSimpleName() + " insert sql=" + sb.toString().replaceAll("(\r|\n)", "\\n"));
|
logger.finest(info.getType().getSimpleName() + " insert sql=" + sb.toString().replaceAll("(\r|\n)", "\\n"));
|
||||||
}
|
}
|
||||||
}
|
} //打印结束
|
||||||
}
|
}
|
||||||
final EntityCache<T> cache = info.getCache();
|
final EntityCache<T> cache = info.getCache();
|
||||||
if (cache != null) {
|
if (cache != null) { //更新缓存
|
||||||
for (final T value : values) {
|
for (final T value : values) {
|
||||||
cache.insert(value);
|
cache.insert(value);
|
||||||
}
|
}
|
||||||
@@ -492,7 +492,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
public <T> void delete(T... values) {
|
public <T> void delete(T... values) {
|
||||||
if (values.length == 0) return;
|
if (values.length == 0) return;
|
||||||
final EntityInfo<T> info = loadEntityInfo((Class<T>) values[0].getClass());
|
final EntityInfo<T> info = loadEntityInfo((Class<T>) values[0].getClass());
|
||||||
if (info.isVirtualEntity()) {
|
if (info.isVirtualEntity()) { //虚拟表只更新缓存Cache
|
||||||
delete(null, info, values);
|
delete(null, info, values);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -524,7 +524,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
@Override
|
@Override
|
||||||
public <T> void delete(Class<T> clazz, Serializable... ids) {
|
public <T> void delete(Class<T> clazz, Serializable... ids) {
|
||||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||||
if (info.isVirtualEntity()) {
|
if (info.isVirtualEntity()) { //虚拟表只更新缓存Cache
|
||||||
delete(null, info, ids);
|
delete(null, info, ids);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -743,7 +743,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
try {
|
try {
|
||||||
if (!info.isVirtualEntity()) {
|
if (!info.isVirtualEntity()) {
|
||||||
String sql = "UPDATE " + info.getTable() + " SET " + info.getSQLColumn(null, column) + " = "
|
String sql = "UPDATE " + info.getTable() + " SET " + info.getSQLColumn(null, column) + " = "
|
||||||
+ formatToString(value) + " WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
+ formatToString(value) + " WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
||||||
final Statement stmt = conn.createStatement();
|
final Statement stmt = conn.createStatement();
|
||||||
stmt.execute(sql);
|
stmt.execute(sql);
|
||||||
@@ -798,7 +798,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
if (!info.isVirtualEntity()) {
|
if (!info.isVirtualEntity()) {
|
||||||
String col = info.getSQLColumn(null, column);
|
String col = info.getSQLColumn(null, column);
|
||||||
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " + (" + incvalue
|
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " + (" + incvalue
|
||||||
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
||||||
final Statement stmt = conn.createStatement();
|
final Statement stmt = conn.createStatement();
|
||||||
stmt.execute(sql);
|
stmt.execute(sql);
|
||||||
@@ -854,7 +854,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
if (!info.isVirtualEntity()) {
|
if (!info.isVirtualEntity()) {
|
||||||
String col = info.getSQLColumn(null, column);
|
String col = info.getSQLColumn(null, column);
|
||||||
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " & (" + andvalue
|
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " & (" + andvalue
|
||||||
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
||||||
final Statement stmt = conn.createStatement();
|
final Statement stmt = conn.createStatement();
|
||||||
stmt.execute(sql);
|
stmt.execute(sql);
|
||||||
@@ -910,7 +910,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
if (!info.isVirtualEntity()) {
|
if (!info.isVirtualEntity()) {
|
||||||
String col = info.getSQLColumn(null, column);
|
String col = info.getSQLColumn(null, column);
|
||||||
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " | (" + orvalue
|
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " | (" + orvalue
|
||||||
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
||||||
final Statement stmt = conn.createStatement();
|
final Statement stmt = conn.createStatement();
|
||||||
stmt.execute(sql);
|
stmt.execute(sql);
|
||||||
@@ -1052,7 +1052,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
|
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
|
||||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
|
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
|
||||||
final String sql = "SELECT " + func.getColumn((column == null || column.isEmpty() ? "*" : ("a." + column))) + " FROM " + info.getTable() + " a"
|
final String sql = "SELECT " + func.getColumn((column == null || column.isEmpty() ? "*" : ("a." + column))) + " FROM " + info.getTable() + " a"
|
||||||
+ (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
|
+ (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
|
||||||
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql);
|
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql);
|
||||||
final PreparedStatement prestmt = conn.prepareStatement(sql);
|
final PreparedStatement prestmt = conn.prepareStatement(sql);
|
||||||
Number rs = null;
|
Number rs = null;
|
||||||
@@ -1114,7 +1114,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
|
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
|
||||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
|
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
|
||||||
final String sql = "SELECT a." + sqlkey + ", " + func.getColumn((funcColumn == null || funcColumn.isEmpty() ? "*" : ("a." + funcColumn)))
|
final String sql = "SELECT a." + sqlkey + ", " + func.getColumn((funcColumn == null || funcColumn.isEmpty() ? "*" : ("a." + funcColumn)))
|
||||||
+ " FROM " + info.getTable() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + " GROUP BY a." + sqlkey;
|
+ " FROM " + info.getTable() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + " GROUP BY a." + sqlkey;
|
||||||
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql);
|
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql);
|
||||||
final PreparedStatement prestmt = conn.prepareStatement(sql);
|
final PreparedStatement prestmt = conn.prepareStatement(sql);
|
||||||
Map<K, N> rs = new LinkedHashMap<>();
|
Map<K, N> rs = new LinkedHashMap<>();
|
||||||
@@ -1690,7 +1690,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
|
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
|
||||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
|
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
|
||||||
final String sql = "SELECT a.* FROM " + info.getTable() + " a" + (join == null ? "" : join)
|
final String sql = "SELECT a.* FROM " + info.getTable() + " a" + (join == null ? "" : join)
|
||||||
+ ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + info.createSQLOrderby(flipper);
|
+ ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + info.createSQLOrderby(flipper);
|
||||||
if (debug.get() && info.isLoggable(Level.FINEST))
|
if (debug.get() && info.isLoggable(Level.FINEST))
|
||||||
logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null ? "" : (" LIMIT " + flipper.index() + "," + flipper.getSize())));
|
logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null ? "" : (" LIMIT " + flipper.index() + "," + flipper.getSize())));
|
||||||
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||||
|
|||||||
@@ -5,11 +5,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.source;
|
package org.redkale.source;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.Serializable;
|
||||||
import java.nio.channels.*;
|
import java.nio.channels.CompletionHandler;
|
||||||
import java.sql.*;
|
import java.sql.ResultSet;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.*;
|
import java.util.function.Consumer;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -94,6 +94,9 @@ public interface DataSource {
|
|||||||
|
|
||||||
public <T> void updateColumns(final CompletionHandler<Void, T> handler, final T value, final String... columns);
|
public <T> void updateColumns(final CompletionHandler<Void, T> handler, final T value, final String... columns);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//############################################# 查询接口 #############################################
|
||||||
//-----------------------getXXXXResult-----------------------------
|
//-----------------------getXXXXResult-----------------------------
|
||||||
public Number getNumberResult(final Class entityClass, final FilterFunc func, final String column);
|
public Number getNumberResult(final Class entityClass, final FilterFunc func, final String column);
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class FilterKey implements java.io.Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "$." + getColumn();
|
return "a." + getColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user