nativePageSql
This commit is contained in:
@@ -159,7 +159,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
|
|||||||
readConfProps.getProperty(DATA_SOURCE_SLOWMS_ERROR, "3000").trim());
|
readConfProps.getProperty(DATA_SOURCE_SLOWMS_ERROR, "3000").trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T> PageCountSql createPageCountSql(
|
protected <T> PageCountSql filterPageCountSql(
|
||||||
EntityInfo<T> info,
|
EntityInfo<T> info,
|
||||||
final boolean readCache,
|
final boolean readCache,
|
||||||
boolean needTotal,
|
boolean needTotal,
|
||||||
@@ -199,12 +199,18 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
|
|||||||
listSubSql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getQueryColumns("a", selects) + " FROM ("
|
listSubSql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getQueryColumns("a", selects) + " FROM ("
|
||||||
+ (union) + ") a";
|
+ (union) + ") a";
|
||||||
}
|
}
|
||||||
|
// pageSql
|
||||||
pageSql = listSubSql + createOrderbySql(info, flipper);
|
pageSql = listSubSql + createOrderbySql(info, flipper);
|
||||||
if (mysqlOrPgsql) {
|
if (Flipper.validLimit(flipper)) {
|
||||||
pageSql = createLimitSql(pageSql, flipper);
|
if (mysqlOrPgsql) {
|
||||||
containsLimit = true;
|
pageSql += " LIMIT " + flipper.getLimit() + " OFFSET " + flipper.getOffset();
|
||||||
|
containsLimit = true;
|
||||||
|
} else if ("oracle".equals(dbtype())) {
|
||||||
|
// to do
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (mysqlOrPgsql && needTotal) {
|
// countSql
|
||||||
|
if (needTotal) {
|
||||||
String countSubSql;
|
String countSubSql;
|
||||||
if (tables.length == 1) {
|
if (tables.length == 1) {
|
||||||
countSubSql = "SELECT "
|
countSubSql = "SELECT "
|
||||||
@@ -233,24 +239,6 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
|
|||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成page sql
|
|
||||||
*
|
|
||||||
* @param listSql listSql
|
|
||||||
* @param flipper 翻页对象
|
|
||||||
* @return String
|
|
||||||
*/
|
|
||||||
protected String createLimitSql(String listSql, Flipper flipper) {
|
|
||||||
if (flipper == null || flipper.getLimit() < 1) {
|
|
||||||
return listSql;
|
|
||||||
}
|
|
||||||
final boolean mysqlOrPgsql = "mysql".equals(dbtype()) || "postgresql".equals(dbtype());
|
|
||||||
if (mysqlOrPgsql) {
|
|
||||||
return listSql + " LIMIT " + flipper.getLimit() + " OFFSET " + flipper.getOffset();
|
|
||||||
}
|
|
||||||
throw new SourceException("Not support page sql: " + listSql);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据Flipper获取ORDER BY的SQL语句,不存在Flipper或sort字段返回空字符串
|
* 根据Flipper获取ORDER BY的SQL语句,不存在Flipper或sort字段返回空字符串
|
||||||
*
|
*
|
||||||
@@ -922,12 +910,13 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
|
|||||||
return getSQLAttrValue(info, attr, val);
|
return getSQLAttrValue(info, attr, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected DataNativeSqlStatement nativeParse(String nativeSql, boolean countable, Map<String, Object> params) {
|
protected DataNativeSqlStatement nativeParse(
|
||||||
|
String nativeSql, boolean countable, Flipper flipper, Map<String, Object> params) {
|
||||||
if (nativeSqlParser == null) {
|
if (nativeSqlParser == null) {
|
||||||
throw new SourceException("not found " + DataNativeSqlParser.class.getSimpleName() + " instance");
|
throw new SourceException("not found " + DataNativeSqlParser.class.getSimpleName() + " instance");
|
||||||
}
|
}
|
||||||
return nativeSqlParser.parse(
|
return nativeSqlParser.parse(
|
||||||
signFunc, dbtype(), nativeSql, countable, params == null ? Collections.emptyMap() : params);
|
signFunc, dbtype(), nativeSql, countable, flipper, params == null ? Collections.emptyMap() : params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConvertDisabled
|
@ConvertDisabled
|
||||||
|
|||||||
@@ -2390,7 +2390,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
|
|||||||
try {
|
try {
|
||||||
conn = readPool.pollConnection();
|
conn = readPool.pollConnection();
|
||||||
conn.setAutoCommit(true);
|
conn.setAutoCommit(true);
|
||||||
PageCountSql sqls = createPageCountSql(info, readCache, needTotal, distinct, sels, tables, flipper, node);
|
PageCountSql sqls = filterPageCountSql(info, readCache, needTotal, distinct, sels, tables, flipper, node);
|
||||||
try {
|
try {
|
||||||
return executeQuerySheet(info, needTotal, flipper, sels, s, conn, sqls);
|
return executeQuerySheet(info, needTotal, flipper, sels, s, conn, sqls);
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
@@ -2429,7 +2429,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 重新查询一次
|
// 重新查询一次
|
||||||
sqls = createPageCountSql(info, readCache, needTotal, distinct, sels, tables, flipper, node);
|
sqls = filterPageCountSql(info, readCache, needTotal, distinct, sels, tables, flipper, node);
|
||||||
return executeQuerySheet(info, needTotal, flipper, sels, s, conn, sqls);
|
return executeQuerySheet(info, needTotal, flipper, sels, s, conn, sqls);
|
||||||
} else {
|
} else {
|
||||||
throw new SourceException(se);
|
throw new SourceException(se);
|
||||||
@@ -2494,7 +2494,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
|
|||||||
if (flipper != null && flipper.getOffset() > 0) {
|
if (flipper != null && flipper.getOffset() > 0) {
|
||||||
set.absolute(flipper.getOffset());
|
set.absolute(flipper.getOffset());
|
||||||
}
|
}
|
||||||
final int limit = flipper == null || flipper.getLimit() < 1 ? Integer.MAX_VALUE : flipper.getLimit();
|
final int limit = Flipper.validLimit(flipper) ? flipper.getLimit() : Integer.MAX_VALUE;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
final DataResultSet rr = createDataResultSet(info, set);
|
final DataResultSet rr = createDataResultSet(info, set);
|
||||||
EntityBuilder<T> builder = info.getBuilder();
|
EntityBuilder<T> builder = info.getBuilder();
|
||||||
@@ -2626,7 +2626,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
|
|||||||
@Local
|
@Local
|
||||||
@Override
|
@Override
|
||||||
public int nativeUpdate(String sql, Map<String, Object> params) {
|
public int nativeUpdate(String sql, Map<String, Object> params) {
|
||||||
DataNativeSqlStatement sinfo = super.nativeParse(sql, false, params);
|
DataNativeSqlStatement sinfo = super.nativeParse(sql, false, null, params);
|
||||||
final long s = System.currentTimeMillis();
|
final long s = System.currentTimeMillis();
|
||||||
JdbcConnection conn = writePool.pollConnection();
|
JdbcConnection conn = writePool.pollConnection();
|
||||||
Statement stmt = null;
|
Statement stmt = null;
|
||||||
@@ -2703,7 +2703,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
|
|||||||
BiConsumer<Object, Object> consumer,
|
BiConsumer<Object, Object> consumer,
|
||||||
Function<DataResultSet, V> handler,
|
Function<DataResultSet, V> handler,
|
||||||
Map<String, Object> params) {
|
Map<String, Object> params) {
|
||||||
DataNativeSqlStatement sinfo = super.nativeParse(sql, false, params);
|
DataNativeSqlStatement sinfo = super.nativeParse(sql, false, null, params);
|
||||||
final long s = System.currentTimeMillis();
|
final long s = System.currentTimeMillis();
|
||||||
final JdbcConnection conn = readPool.pollConnection();
|
final JdbcConnection conn = readPool.pollConnection();
|
||||||
try {
|
try {
|
||||||
@@ -2746,7 +2746,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <V> Sheet<V> nativeQuerySheet(Class<V> type, String sql, Flipper flipper, Map<String, Object> params) {
|
public <V> Sheet<V> nativeQuerySheet(Class<V> type, String sql, Flipper flipper, Map<String, Object> params) {
|
||||||
DataNativeSqlStatement sinfo = super.nativeParse(sql, true, params);
|
DataNativeSqlStatement sinfo = super.nativeParse(sql, true, flipper, params);
|
||||||
final long s = System.currentTimeMillis();
|
final long s = System.currentTimeMillis();
|
||||||
final JdbcConnection conn = readPool.pollConnection();
|
final JdbcConnection conn = readPool.pollConnection();
|
||||||
try {
|
try {
|
||||||
@@ -2781,7 +2781,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
|
|||||||
}
|
}
|
||||||
slowLog(s, countSql);
|
slowLog(s, countSql);
|
||||||
if (total > 0) {
|
if (total > 0) {
|
||||||
String pageSql = createLimitSql(sinfo.getNativePageSql(), flipper);
|
String pageSql = sinfo.getNativePageSql();
|
||||||
if (sinfo.isEmptyNamed()) {
|
if (sinfo.isEmptyNamed()) {
|
||||||
Statement stmt = conn.createQueryStatement();
|
Statement stmt = conn.createQueryStatement();
|
||||||
ResultSet set = stmt.executeQuery(pageSql);
|
ResultSet set = stmt.executeQuery(pageSql);
|
||||||
|
|||||||
@@ -24,7 +24,12 @@ public interface DataNativeSqlParser {
|
|||||||
public DataNativeSqlInfo parse(IntFunction<String> signFunc, String dbType, String rawSql);
|
public DataNativeSqlInfo parse(IntFunction<String> signFunc, String dbType, String rawSql);
|
||||||
|
|
||||||
public DataNativeSqlStatement parse(
|
public DataNativeSqlStatement parse(
|
||||||
IntFunction<String> signFunc, String dbType, String rawSql, boolean countable, Map<String, Object> params);
|
IntFunction<String> signFunc,
|
||||||
|
String dbType,
|
||||||
|
String rawSql,
|
||||||
|
boolean countable,
|
||||||
|
Flipper flipper,
|
||||||
|
Map<String, Object> params);
|
||||||
|
|
||||||
public static DataNativeSqlParser loadFirst() {
|
public static DataNativeSqlParser loadFirst() {
|
||||||
if (DataNativeSqlStatement._first_parser != DataNativeSqlStatement.PARSER_NIL) {
|
if (DataNativeSqlStatement._first_parser != DataNativeSqlStatement.PARSER_NIL) {
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ public class DataNativeSqlStatement {
|
|||||||
String dbType,
|
String dbType,
|
||||||
String rawSql,
|
String rawSql,
|
||||||
boolean countable,
|
boolean countable,
|
||||||
|
Flipper flipper,
|
||||||
Map<String, Object> params) {
|
Map<String, Object> params) {
|
||||||
throw new UnsupportedOperationException("No available instances found");
|
throw new UnsupportedOperationException("No available instances found");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,6 +191,13 @@ public final class Flipper implements Serializable, Cloneable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Flipper sortIfAbsent(String sort) {
|
||||||
|
if (this.sort == null || this.sort.isEmpty()) {
|
||||||
|
this.sort = sort;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public static Flipper sortIfAbsent(Flipper flipper, String sort) {
|
public static Flipper sortIfAbsent(Flipper flipper, String sort) {
|
||||||
if (flipper != null) {
|
if (flipper != null) {
|
||||||
return flipper.sortIfAbsent(sort);
|
return flipper.sortIfAbsent(sort);
|
||||||
@@ -198,10 +205,7 @@ public final class Flipper implements Serializable, Cloneable {
|
|||||||
return flipper;
|
return flipper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Flipper sortIfAbsent(String sort) {
|
public static boolean validLimit(Flipper flipper) {
|
||||||
if (this.sort == null || this.sort.isEmpty()) {
|
return flipper != null && flipper.getLimit() > 0;
|
||||||
this.sort = sort;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user