createLimitSQL

This commit is contained in:
redkale
2024-06-05 23:14:12 +08:00
parent 698e6c6ee7
commit 5a8557bf63
4 changed files with 27 additions and 36 deletions

View File

@@ -231,8 +231,8 @@ public abstract class AbstractDataSource extends AbstractService implements Data
* @param flipper 翻页参数
* @return SQL
*/
protected <T> String createSQLOrderby(EntityInfo<T> info, Flipper flipper) {
return info.createSQLOrderby(flipper);
protected <T> String createOrderbySql(EntityInfo<T> info, Flipper flipper) {
return info.createOrderbySql(flipper);
}
/**

View File

@@ -5,10 +5,6 @@
*/
package org.redkale.source;
import static org.redkale.boot.Application.*;
import static org.redkale.source.DataSources.*;
import static org.redkale.util.Utility.isEmpty;
import java.io.Serializable;
import java.math.*;
import java.sql.SQLException;
@@ -21,13 +17,16 @@ import java.util.stream.Stream;
import org.redkale.annotation.*;
import org.redkale.annotation.AutoLoad;
import org.redkale.annotation.ResourceType;
import static org.redkale.boot.Application.*;
import org.redkale.convert.ConvertDisabled;
import org.redkale.inject.ResourceEvent;
import org.redkale.net.AsyncGroup;
import org.redkale.persistence.Table;
import org.redkale.service.Local;
import static org.redkale.source.DataSources.*;
import org.redkale.source.EntityInfo.EntityColumn;
import org.redkale.util.*;
import static org.redkale.util.Utility.isEmpty;
/**
* DataSource的SQL抽象实现类 <br>
@@ -157,6 +156,17 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
readConfProps.getProperty(DATA_SOURCE_SLOWMS_ERROR, "3000").trim());
}
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);
}
@Override
@ResourceChanged
public void onResourceChange(ResourceEvent[] events) {
@@ -1458,7 +1468,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
String sql = "DELETE FROM " + table + " a" + (join1 == null ? "" : (", " + join1))
+ " WHERE " + info.getPrimarySQLColumn() + " IN (SELECT " + info.getPrimaryField() + " FROM "
+ table
+ join2AndWhere + info.createSQLOrderby(flipper) + " OFFSET 0 LIMIT " + flipper.getLimit()
+ join2AndWhere + info.createOrderbySql(flipper) + " OFFSET 0 LIMIT " + flipper.getLimit()
+ ")";
sqls.add(sql);
}
@@ -1468,7 +1478,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
List<String> sqls = new ArrayList<>();
for (String table : tables) {
String sql = "DELETE " + (mysql ? "a" : "") + " FROM " + table + " a"
+ (join1 == null ? "" : (", " + join1)) + join2AndWhere + info.createSQLOrderby(flipper)
+ (join1 == null ? "" : (", " + join1)) + join2AndWhere + info.createOrderbySql(flipper)
+ ((mysql && flipper != null && flipper.getLimit() > 0)
? (" LIMIT " + flipper.getLimit())
: "");
@@ -2144,14 +2154,14 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql
+ " WHERE " + info.getPrimarySQLColumn() + " IN (SELECT " + info.getPrimaryField() + " FROM "
+ tables[0]
+ wherestr + info.createSQLOrderby(flipper) + " OFFSET 0 LIMIT " + flipper.getLimit() + ")";
+ wherestr + info.createOrderbySql(flipper) + " OFFSET 0 LIMIT " + flipper.getLimit() + ")";
} else {
sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql
+ ((where == null || where.length() == 0)
? (join2 == null ? "" : (" WHERE " + join2))
: (" WHERE " + where + (join2 == null ? "" : (" AND " + join2))))
+ info.createSQLOrderby(flipper)
+ info.createOrderbySql(flipper)
+ (("mysql".equals(dbtype()) && flipper != null && flipper.getLimit() > 0)
? (" LIMIT " + flipper.getLimit())
: "");

View File

@@ -5,8 +5,6 @@
*/
package org.redkale.source;
import static org.redkale.source.DataSources.*;
import java.io.Serializable;
import java.sql.*;
import java.util.*;
@@ -20,6 +18,7 @@ import org.redkale.annotation.AutoLoad;
import org.redkale.annotation.ResourceType;
import org.redkale.inject.ResourceEvent;
import org.redkale.service.Local;
import static org.redkale.source.DataSources.*;
import org.redkale.util.*;
/**
@@ -2570,7 +2569,6 @@ public class DataJdbcSource extends AbstractDataSqlSource {
listSubSql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getQueryColumns("a", selects) + " FROM "
+ tables[0] + " a" + joinAndWhere;
} else {
int b = 0;
for (String table : tables) {
if (union.length() > 0) {
union.append(" UNION ALL ");
@@ -2585,21 +2583,10 @@ public class DataJdbcSource extends AbstractDataSqlSource {
listSubSql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getQueryColumns("a", selects) + " FROM ("
+ (union) + ") a";
}
listSql = listSubSql + createSQLOrderby(info, flipper);
if (mysqlOrPgsql) {
listSql += (flipper == null || flipper.getLimit() < 1
? ""
: (" LIMIT " + flipper.getLimit() + " OFFSET " + flipper.getOffset()));
if (readCache && info.isLoggable(logger, Level.FINEST, listSql)) {
logger.finest(info.getType().getSimpleName() + " query sql=" + listSql);
}
} else {
if (readCache && info.isLoggable(logger, Level.FINEST, listSql)) {
logger.finest(info.getType().getSimpleName() + " query sql=" + listSql
+ (flipper == null || flipper.getLimit() < 1
? ""
: (" LIMIT " + flipper.getLimit() + " OFFSET " + flipper.getOffset())));
}
listSql = listSubSql + createOrderbySql(info, flipper);
listSql = createLimitSQL(listSql, flipper);
if (readCache && info.isLoggable(logger, Level.FINEST, listSql)) {
logger.finest(info.getType().getSimpleName() + " query sql=" + listSql);
}
if (mysqlOrPgsql && needTotal) {
String countSubSql;
@@ -2840,7 +2827,6 @@ public class DataJdbcSource extends AbstractDataSqlSource {
}
public <V> Sheet<V> nativeQuerySheet(Class<V> type, String sql, Flipper flipper, Map<String, Object> params) {
final boolean mysqlOrPgsql = "mysql".equals(dbtype()) || "postgresql".equals(dbtype());
DataNativeSqlStatement sinfo = super.nativeParse(sql, true, params);
final long s = System.currentTimeMillis();
final JdbcConnection conn = readPool.pollConnection();
@@ -2876,12 +2862,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
}
slowLog(s, countSql);
if (total > 0) {
String listSql = sinfo.getNativeSql();
if (mysqlOrPgsql) {
listSql += (flipper == null || flipper.getLimit() < 1
? ""
: (" LIMIT " + flipper.getLimit() + " OFFSET " + flipper.getOffset()));
}
String listSql = createLimitSQL(sinfo.getNativeSql(), flipper);
if (sinfo.isEmptyNamed()) {
Statement stmt = conn.createQueryStatement();
ResultSet set = stmt.executeQuery(listSql);

View File

@@ -1415,7 +1415,7 @@ public final class EntityInfo<T> {
* @param flipper 翻页对象
* @return String
*/
protected String createSQLOrderby(Flipper flipper) {
protected String createOrderbySql(Flipper flipper) {
if (flipper == null || flipper.getSort() == null) {
return "";
}