Module bus.pager

Class DefaultCountSqlParser

java.lang.Object
org.miaixz.bus.pager.parsing.DefaultCountSqlParser
All Implemented Interfaces:
CountSqlParser

public class DefaultCountSqlParser extends Object implements CountSqlParser
Default implementation of CountSqlParser that provides a more intelligent way to generate count query SQL. It attempts to optimize the count query by removing unnecessary ORDER BY clauses and simplifying the SELECT statement.
Since:
Java 17+
Author:
Kimi Liu
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    protected static final net.sf.jsqlparser.expression.Alias
     

    Fields inherited from interface org.miaixz.bus.pager.parsing.CountSqlParser

    AGGREGATE_FUNCTIONS
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Retrieves a simple COUNT SQL, suitable for unparseable or complex scenarios.
    Retrieves a simple COUNT SQL with a specified COUNT column name.
    getSmartCountSql(String sql, String countColumn)
    Retrieves an intelligent COUNT SQL, automatically detecting whether to keep the ORDER BY clause.
    boolean
    isSimpleCount(net.sf.jsqlparser.statement.select.PlainSelect select)
    Determines if a simple COUNT query can be used.
    protected boolean
    Checks if the ORDER BY clause should be kept.
    protected boolean
    Checks if the ORDER BY clause of subqueries should be kept.
    boolean
    orderByHashParameters(List<net.sf.jsqlparser.statement.select.OrderByElement> orderByElements)
    Determines if the ORDER BY clause contains parameters (?).
    void
    processFromItem(net.sf.jsqlparser.statement.select.FromItem fromItem)
    Processes subqueries in the FROM clause.
    void
    processPlainSelect(net.sf.jsqlparser.statement.select.PlainSelect plainSelect)
    Processes the body of a PlainSelect type.
    void
    processSelect(net.sf.jsqlparser.statement.select.Select select)
    Processes the SELECT body to remove unnecessary ORDER BY clauses.
    void
    processWithItemsList(List<net.sf.jsqlparser.statement.select.WithItem<?>> withItemsList)
    Processes the WITH clause to remove unnecessary ORDER BY clauses.
    net.sf.jsqlparser.statement.select.Select
    sqlToCount(net.sf.jsqlparser.statement.select.Select select, String name)
    Converts a SQL statement to a count query.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.miaixz.bus.pager.parsing.CountSqlParser

    getSmartCountSql
  • Field Details

    • KEEP_ORDERBY

      public static final String KEEP_ORDERBY
      See Also:
    • TABLE_ALIAS

      protected static final net.sf.jsqlparser.expression.Alias TABLE_ALIAS
  • Constructor Details

    • DefaultCountSqlParser

      public DefaultCountSqlParser()
  • Method Details

    • getSmartCountSql

      public String getSmartCountSql(String sql, String countColumn)
      Retrieves an intelligent COUNT SQL, automatically detecting whether to keep the ORDER BY clause. This method optimizes string handling and parsing logic to reduce performance overhead.
      Specified by:
      getSmartCountSql in interface CountSqlParser
      Parameters:
      sql - the original SQL query
      countColumn - the COUNT column name, defaults to "0"
      Returns:
      the optimized COUNT SQL
    • getSimpleCountSql

      public String getSimpleCountSql(String sql)
      Retrieves a simple COUNT SQL, suitable for unparseable or complex scenarios.
      Parameters:
      sql - the original query SQL
      Returns:
      a simple COUNT SQL
    • getSimpleCountSql

      public String getSimpleCountSql(String sql, String name)
      Retrieves a simple COUNT SQL with a specified COUNT column name. This method optimizes StringBuilder capacity estimation to reduce resizing.
      Parameters:
      sql - the original query SQL
      name - the COUNT column name
      Returns:
      a simple COUNT SQL
    • sqlToCount

      public net.sf.jsqlparser.statement.select.Select sqlToCount(net.sf.jsqlparser.statement.select.Select select, String name)
      Converts a SQL statement to a count query.
      Parameters:
      select - the original query SQL
      name - the name of the count column
      Returns:
      the resulting count query SQL
    • isSimpleCount

      public boolean isSimpleCount(net.sf.jsqlparser.statement.select.PlainSelect select)
      Determines if a simple COUNT query can be used. This method avoids using the deprecated Parenthesis class, using ParenthesedExpressionList or generic Expression instead.
      Parameters:
      select - the query
      Returns:
      true if it is a simple COUNT, false otherwise
    • processSelect

      public void processSelect(net.sf.jsqlparser.statement.select.Select select)
      Processes the SELECT body to remove unnecessary ORDER BY clauses. This method optimizes recursive processing logic to reduce redundant calls.
      Parameters:
      select - the query information
    • processPlainSelect

      public void processPlainSelect(net.sf.jsqlparser.statement.select.PlainSelect plainSelect)
      Processes the body of a PlainSelect type. This method optimizes JOIN and FROM item processing logic.
      Parameters:
      plainSelect - the query
    • processWithItemsList

      public void processWithItemsList(List<net.sf.jsqlparser.statement.select.WithItem<?>> withItemsList)
      Processes the WITH clause to remove unnecessary ORDER BY clauses.
      1. Uses List<WithItem<?>> to adapt to JSqlParser 5.1's generic API.
      2. Checks for keepSubSelectOrderBy and empty lists early to reduce unnecessary loops.
      3. Uses a for-each loop to reduce iterator creation.
      4. Checks for non-null select early to reduce invalid recursion.
      Parameters:
      withItemsList - the list of WITH clauses
    • processFromItem

      public void processFromItem(net.sf.jsqlparser.statement.select.FromItem fromItem)
      Processes subqueries in the FROM clause. This method optimizes type checking and recursive logic.
      Parameters:
      fromItem - the FROM clause item
    • keepOrderBy

      protected boolean keepOrderBy()
      Checks if the ORDER BY clause should be kept. This method uses the configuration from PageMethod.
      Returns:
      true if ORDER BY should be kept, false otherwise
    • keepSubSelectOrderBy

      protected boolean keepSubSelectOrderBy()
      Checks if the ORDER BY clause of subqueries should be kept.
      Returns:
      true if subquery ORDER BY should be kept, false otherwise
    • orderByHashParameters

      public boolean orderByHashParameters(List<net.sf.jsqlparser.statement.select.OrderByElement> orderByElements)
      Determines if the ORDER BY clause contains parameters (?). This method optimizes null checks and loop efficiency.
      Parameters:
      orderByElements - the list of ORDER BY elements
      Returns:
      true if it contains parameters, false otherwise