Module bus.pager

Class DefaultSqlServerSqlParser

java.lang.Object
org.miaixz.bus.pager.parsing.DefaultSqlServerSqlParser
All Implemented Interfaces:
SqlServerSqlParser

public class DefaultSqlServerSqlParser extends Object implements SqlServerSqlParser
Default implementation of SqlServerSqlParser for converting SQL Server queries to paginated statements. Important considerations:
  1. Ensure your SQL is executable first.
  2. It's best to include an ORDER BY clause directly in the SQL, which can be automatically extracted.
  3. If there is no ORDER BY, you can provide it as a parameter, but you must ensure its correctness.
  4. If the SQL has an ORDER BY, it can be overridden by the `orderby` parameter.
  5. Column names in the ORDER BY clause cannot be aliases.
  6. Do not use single quotes (') for table and column aliases.
This class is designed as a standalone utility, dependent on JSqlParser, and can be used independently.
Since:
Java 17+
Author:
Kimi Liu
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final String
    Alias prefix for generated columns.
    protected static final String
    Row number column name.
    protected static final net.sf.jsqlparser.schema.Column
    Row number column object.
    static final String
    Page size placeholder.
    static final net.sf.jsqlparser.expression.Alias
    Alias for the page table.
    protected static final String
    Table alias name.
    static final String
    Start row number placeholder.
    protected static final net.sf.jsqlparser.statement.select.Top
    TOP 100 PERCENT clause.
    protected static final String
    Outer wrapper table name.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected net.sf.jsqlparser.statement.select.SelectItem<?>
    addRowNumber(net.sf.jsqlparser.statement.select.PlainSelect plainSelect, List<net.sf.jsqlparser.statement.select.SelectItem<?>> autoItems)
    Gets the ROW_NUMBER() column.
    protected net.sf.jsqlparser.statement.select.OrderByElement
    cloneOrderByElement(net.sf.jsqlparser.statement.select.OrderByElement orig, String alias)
    Clones an OrderByElement.
    protected net.sf.jsqlparser.statement.select.OrderByElement
    cloneOrderByElement(net.sf.jsqlparser.statement.select.OrderByElement orig, net.sf.jsqlparser.expression.Expression expression)
    Clones an OrderByElement.
    Converts a SQL statement to a paginated statement.
    convertToPageSql(String sql, Integer offset, Integer limit)
    Converts a SQL statement to a paginated statement.
    protected List<net.sf.jsqlparser.statement.select.OrderByElement>
    getOrderByElements(net.sf.jsqlparser.statement.select.PlainSelect plainSelect, List<net.sf.jsqlparser.statement.select.SelectItem<?>> autoItems)
    Gets the new sorting list.
    protected net.sf.jsqlparser.statement.select.Select
    getPageSelect(net.sf.jsqlparser.statement.select.Select select)
    Gets an outer wrapped TOP query.
    protected List<net.sf.jsqlparser.statement.select.SelectItem<?>>
    getSelectItems(net.sf.jsqlparser.statement.select.PlainSelect plainSelect)
    Gets the query columns.
    boolean
    Checks if a list is not empty.
    protected void
    processFromItem(net.sf.jsqlparser.statement.select.FromItem fromItem, int level)
    Processes a subquery.
    protected void
    processPlainSelect(net.sf.jsqlparser.statement.select.PlainSelect plainSelect, int level)
    Processes a PlainSelect type of select.
    protected void
    processSelectBody(Object select, int level)
    Processes the selectBody to remove Order by.
    protected net.sf.jsqlparser.statement.select.Select
    wrapSetOperationList(net.sf.jsqlparser.statement.select.SetOperationList setOperationList)
    Wraps a SetOperationList.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • START_ROW

      public static final String START_ROW
      Start row number placeholder.
    • PAGE_SIZE

      public static final String PAGE_SIZE
      Page size placeholder.
    • WRAP_TABLE

      protected static final String WRAP_TABLE
      Outer wrapper table name.
      See Also:
    • PAGE_TABLE_NAME

      protected static final String PAGE_TABLE_NAME
      Table alias name.
      See Also:
    • PAGE_TABLE_ALIAS

      public static final net.sf.jsqlparser.expression.Alias PAGE_TABLE_ALIAS
      Alias for the page table.
    • PAGE_ROW_NUMBER

      protected static final String PAGE_ROW_NUMBER
      Row number column name.
      See Also:
    • PAGE_ROW_NUMBER_COLUMN

      protected static final net.sf.jsqlparser.schema.Column PAGE_ROW_NUMBER_COLUMN
      Row number column object.
    • TOP100_PERCENT

      protected static final net.sf.jsqlparser.statement.select.Top TOP100_PERCENT
      TOP 100 PERCENT clause.
    • PAGE_COLUMN_ALIAS_PREFIX

      protected static final String PAGE_COLUMN_ALIAS_PREFIX
      Alias prefix for generated columns.
      See Also:
  • Constructor Details

    • DefaultSqlServerSqlParser

      public DefaultSqlServerSqlParser()
  • Method Details

    • convertToPageSql

      public String convertToPageSql(String sql)
      Converts a SQL statement to a paginated statement.
      Parameters:
      sql - the SQL statement
      Returns:
      the paginated SQL string
    • convertToPageSql

      public String convertToPageSql(String sql, Integer offset, Integer limit)
      Converts a SQL statement to a paginated statement.
      Specified by:
      convertToPageSql in interface SqlServerSqlParser
      Parameters:
      sql - the SQL statement
      offset - the starting position
      limit - the ending position
      Returns:
      the paginated SQL string
    • getPageSelect

      protected net.sf.jsqlparser.statement.select.Select getPageSelect(net.sf.jsqlparser.statement.select.Select select)
      Gets an outer wrapped TOP query.
      Parameters:
      select - the Select statement
      Returns:
      the wrapped Select statement
    • wrapSetOperationList

      protected net.sf.jsqlparser.statement.select.Select wrapSetOperationList(net.sf.jsqlparser.statement.select.SetOperationList setOperationList)
      Wraps a SetOperationList.
      Parameters:
      setOperationList - the SetOperationList
      Returns:
      the wrapped Select statement
    • getSelectItems

      protected List<net.sf.jsqlparser.statement.select.SelectItem<?>> getSelectItems(net.sf.jsqlparser.statement.select.PlainSelect plainSelect)
      Gets the query columns.
      Parameters:
      plainSelect - the PlainSelect statement
      Returns:
      the list of select items
    • addRowNumber

      protected net.sf.jsqlparser.statement.select.SelectItem<?> addRowNumber(net.sf.jsqlparser.statement.select.PlainSelect plainSelect, List<net.sf.jsqlparser.statement.select.SelectItem<?>> autoItems)
      Gets the ROW_NUMBER() column.
      Parameters:
      plainSelect - the original query
      autoItems - auto-generated query columns
      Returns:
      the ROW_NUMBER() column
    • processSelectBody

      protected void processSelectBody(Object select, int level)
      Processes the selectBody to remove Order by.
      Parameters:
      select - the query
      level - the level of nesting
    • processPlainSelect

      protected void processPlainSelect(net.sf.jsqlparser.statement.select.PlainSelect plainSelect, int level)
      Processes a PlainSelect type of select.
      Parameters:
      plainSelect - the query
      level - the level of nesting
    • processFromItem

      protected void processFromItem(net.sf.jsqlparser.statement.select.FromItem fromItem, int level)
      Processes a subquery.
      Parameters:
      fromItem - the FromItem
      level - the level of nesting
    • isNotEmptyList

      public boolean isNotEmptyList(List<?> list)
      Checks if a list is not empty.
      Parameters:
      list - the list
      Returns:
      true if the list is not empty, false otherwise
    • cloneOrderByElement

      protected net.sf.jsqlparser.statement.select.OrderByElement cloneOrderByElement(net.sf.jsqlparser.statement.select.OrderByElement orig, String alias)
      Clones an OrderByElement.
      Parameters:
      orig - the original OrderByElement
      alias - the new sorting element for the OrderByElement
      Returns:
      the cloned new OrderByElement
    • cloneOrderByElement

      protected net.sf.jsqlparser.statement.select.OrderByElement cloneOrderByElement(net.sf.jsqlparser.statement.select.OrderByElement orig, net.sf.jsqlparser.expression.Expression expression)
      Clones an OrderByElement.
      Parameters:
      orig - the original OrderByElement
      expression - the new sorting element for the OrderByElement
      Returns:
      the cloned new OrderByElement
    • getOrderByElements

      protected List<net.sf.jsqlparser.statement.select.OrderByElement> getOrderByElements(net.sf.jsqlparser.statement.select.PlainSelect plainSelect, List<net.sf.jsqlparser.statement.select.SelectItem<?>> autoItems)
      Gets the new sorting list.
      Parameters:
      plainSelect - the original query
      autoItems - the newly generated query elements
      Returns:
      the new sorting list