Module bus.pager

Class TenantHandler

java.lang.Object
org.miaixz.bus.mapper.handler.AbstractSqlHandler
All Implemented Interfaces:
Serializable, org.miaixz.bus.core.Handler, org.miaixz.bus.mapper.handler.MapperHandler

public class TenantHandler extends ConditionHandler implements org.miaixz.bus.mapper.handler.MapperHandler
Multi-tenancy handler, responsible for adding tenant conditions to SQL statements. This handler intercepts various SQL operations (SELECT, INSERT, UPDATE, DELETE) and modifies them to include tenant-specific filtering or data manipulation based on the configured TenantProvider.
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Summary

    Fields inherited from class org.miaixz.bus.mapper.handler.AbstractSqlHandler

    DEFAULT_REFLECTOR_FACTORY, DELEGATE_BOUNDSQL, DELEGATE_BOUNDSQL_SQL, DELEGATE_MAPPEDSTATEMENT, MAPPEDSTATEMENT
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    appendSelectItem(List<net.sf.jsqlparser.statement.select.SelectItem<?>> selectItems)
    Appends the tenant column to the SELECT clause of a SELECT statement.
    net.sf.jsqlparser.expression.Expression
    buildTableExpression(net.sf.jsqlparser.schema.Table table, net.sf.jsqlparser.expression.Expression where, String segment)
    Builds the tenant condition expression (e.g., `tenant_id = ?
    protected net.sf.jsqlparser.schema.Column
    getAliasColumn(net.sf.jsqlparser.schema.Table table)
    Retrieves the tenant column name, optionally prefixed with the table alias.
    Retrieves the tenant service provider.
    void
    prepare(org.apache.ibatis.executor.statement.StatementHandler statementHandler)
    Pre-processes INSERT, UPDATE, and DELETE statements to add tenant conditions.
    protected void
    processDelete(net.sf.jsqlparser.statement.delete.Delete delete, int index, String sql, Object object)
    Processes a DELETE statement, adding tenant conditions to its WHERE clause.
    protected void
    processInsert(net.sf.jsqlparser.statement.insert.Insert insert, int index, String sql, Object object)
    Processes an INSERT statement, adding tenant columns and values.
    protected void
    processInsertSelect(net.sf.jsqlparser.statement.select.Select select, String segment)
    Processes an INSERT INTO ...
    protected void
    processSelect(net.sf.jsqlparser.statement.select.Select select, int index, String sql, Object object)
    Processes a SELECT statement, adding tenant conditions to its WHERE clause.
    protected void
    processUpdate(net.sf.jsqlparser.statement.update.Update update, int index, String sql, Object object)
    Processes an UPDATE statement, adding tenant conditions to its WHERE clause.
    void
    query(Object object, org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement mappedStatement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler resultHandler, org.apache.ibatis.mapping.BoundSql boundSql)
    Handles query operations by adding tenant conditions to SELECT statements.
    boolean
    Sets the properties for the handler, initializing the tenant service provider.
    void
    Sets the tenant service provider.

    Methods inherited from class org.miaixz.bus.pager.handler.SqlParserHandler

    parse, parserMulti, parserSingle, parseStatements, processParser, validateSql

    Methods inherited from class org.miaixz.bus.mapper.handler.AbstractSqlHandler

    getMappedStatement, getMappedStatement, getMetaObject, getSqlParserInfo, mapperBoundSql, mapperStatementHandler, realTarget, setAdditionalParameter

    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.core.Handler

    after, before

    Methods inherited from interface org.miaixz.bus.mapper.handler.MapperHandler

    getBoundSql, isQuery, isUpdate, update
  • Constructor Details

    • TenantHandler

      public TenantHandler()
  • Method Details

    • query

      public void query(Object object, org.apache.ibatis.executor.Executor executor, org.apache.ibatis.mapping.MappedStatement mappedStatement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler resultHandler, org.apache.ibatis.mapping.BoundSql boundSql)
      Handles query operations by adding tenant conditions to SELECT statements. The SQL is parsed and modified to include tenant-specific WHERE clauses.
      Specified by:
      query in interface org.miaixz.bus.mapper.handler.MapperHandler
      Parameters:
      object - The result object (unused).
      executor - The MyBatis executor.
      mappedStatement - The MappedStatement.
      parameter - The query parameters.
      rowBounds - The pagination parameters.
      resultHandler - The result handler.
      boundSql - The BoundSql object.
    • prepare

      public void prepare(org.apache.ibatis.executor.statement.StatementHandler statementHandler)
      Pre-processes INSERT, UPDATE, and DELETE statements to add tenant conditions. This method is called before the statement is executed.
      Specified by:
      prepare in interface org.miaixz.bus.mapper.handler.MapperHandler
      Parameters:
      statementHandler - The StatementHandler.
    • processSelect

      protected void processSelect(net.sf.jsqlparser.statement.select.Select select, int index, String sql, Object object)
      Processes a SELECT statement, adding tenant conditions to its WHERE clause.
      Overrides:
      processSelect in class SqlParserHandler
      Parameters:
      select - The SELECT statement.
      index - The statement index (unused).
      sql - The original SQL string.
      object - The WHERE clause segment (as a string).
    • processInsert

      protected void processInsert(net.sf.jsqlparser.statement.insert.Insert insert, int index, String sql, Object object)
      Processes an INSERT statement, adding tenant columns and values.
      Overrides:
      processInsert in class SqlParserHandler
      Parameters:
      insert - The INSERT statement.
      index - The statement index (unused).
      sql - The original SQL string.
      object - The WHERE clause segment (unused).
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if multi-table update is attempted without proper exclusion.
    • processUpdate

      protected void processUpdate(net.sf.jsqlparser.statement.update.Update update, int index, String sql, Object object)
      Processes an UPDATE statement, adding tenant conditions to its WHERE clause.
      Overrides:
      processUpdate in class SqlParserHandler
      Parameters:
      update - The UPDATE statement.
      index - The statement index (unused).
      sql - The original SQL string.
      object - The WHERE clause segment (as a string).
    • processDelete

      protected void processDelete(net.sf.jsqlparser.statement.delete.Delete delete, int index, String sql, Object object)
      Processes a DELETE statement, adding tenant conditions to its WHERE clause.
      Overrides:
      processDelete in class SqlParserHandler
      Parameters:
      delete - The DELETE statement.
      index - The statement index (unused).
      sql - The original SQL string.
      object - The WHERE clause segment (as a string).
    • processInsertSelect

      protected void processInsertSelect(net.sf.jsqlparser.statement.select.Select select, String segment)
      Processes an INSERT INTO ... SELECT statement, ensuring the SELECT subquery includes tenant conditions.
      Parameters:
      select - The SELECT statement body.
      segment - The WHERE clause segment (as a string).
    • appendSelectItem

      protected void appendSelectItem(List<net.sf.jsqlparser.statement.select.SelectItem<?>> selectItems)
      Appends the tenant column to the SELECT clause of a SELECT statement.
      Parameters:
      selectItems - The list of select items in the SELECT clause.
    • getAliasColumn

      protected net.sf.jsqlparser.schema.Column getAliasColumn(net.sf.jsqlparser.schema.Table table)
      Retrieves the tenant column name, optionally prefixed with the table alias.
      Parameters:
      table - The Table object.
      Returns:
      The tenant column as a Column object (e.g., tenantId or tableAlias.tenantId).
    • setProperties

      public boolean setProperties(Properties properties)
      Sets the properties for the handler, initializing the tenant service provider.
      Specified by:
      setProperties in interface org.miaixz.bus.core.Handler
      Parameters:
      properties - The configuration properties.
      Returns:
      true if properties were set successfully.
    • buildTableExpression

      public net.sf.jsqlparser.expression.Expression buildTableExpression(net.sf.jsqlparser.schema.Table table, net.sf.jsqlparser.expression.Expression where, String segment)
      Builds the tenant condition expression (e.g., `tenant_id = ?`).
      Specified by:
      buildTableExpression in class ConditionHandler
      Parameters:
      table - The Table object.
      where - The current WHERE condition.
      segment - The full Mapper path (unused).
      Returns:
      The tenant condition expression, or null if the table is ignored.
    • getProvider

      public TenantProvider getProvider()
      Retrieves the tenant service provider.
      Returns:
      The TenantProvider instance.
    • setProvider

      public void setProvider(TenantProvider provider)
      Sets the tenant service provider.
      Parameters:
      provider - The TenantProvider instance to set.