SqlTemplate类

2024-09-24 09:12:53 浏览数 (1)

这个Java类 SqlTemplate 主要用于定义和存储SQL查询模板字符串,这些模板字符串使用StringTemplate库的语法编写。以下是类的主要功能和特点的概要描述:

  • 类包含两个静态常量字符串:QUERY_SQLPREVIEW_SQL,它们分别定义了两种不同的SQL查询模板。
  • QUERY_SQL 模板用于生成标准的SQL查询语句,包括选择字段、表名、别名、WHERE条件、GROUP BY子句、ORDER BY子句等。
  • PREVIEW_SQL 模板用于生成预览数据的SQL查询语句,它与 QUERY_SQL 类似,但可能包含一些特定的预览相关的逻辑,如去重(DISTINCT)等。
  • 模板使用StringTemplate的语法,允许通过传入参数动态地构建SQL查询语句。
  • 模板中包含了条件判断(如 <if(condition)>),可以根据传入的参数值决定是否包含特定的SQL片段。
  • 模板支持循环(如 <groups:{group|...}; separator=",">),可以迭代传入的列表参数,为每个元素生成相应的SQL片段。
  • 模板字符串是硬编码的,意味着它们在编译时就已经确定,并且不会在运行时改变。
  • 这些模板可以在 SQLProvider 类中使用,通过填充具体的参数来生成完整的SQL查询语句。
  • 模板的设计使得SQL语句的构建更加灵活和可维护,同时也减少了代码重复,并提高了生成SQL语句的效率。

代码语言:java复制
package io.dataease.engine.sql;

/**
 * @Author Junjun
 */
public class SqlTemplate {
    public static String QUERY_SQL = "querySql(limitFiled, groups, aggregators, filters, orders, table, notUseAs, useAliasForGroup)n"  
            "::=<<n"  
            "SELECTn"  
            "<if(limitFiled)>n"  
            "   <limitFiled.limitFiled>n"  
            "<endif>n"  
            "<if(!groups && !aggregators)>n"  
            "    *n"  
            "<endif>n"  
            "<if(groups && notUseAs)>n"  
            "    <groups:{group|<if(group)><group.fieldName> <endif>}; separator=",\n">n"  
            "<endif>n"  
            "<if(groups && !notUseAs)>n"  
            "    <groups:{group|<if(group)><group.fieldName> AS <group.fieldAlias><endif>}; separator=",\n">n"  
            "<endif>n"  
            "    <if(groups && aggregators)>,<endif>n"  
            "<if(aggregators)>n"  
            "    <aggregators:{agg|<if(agg)><agg.fieldName> AS <agg.fieldAlias><endif>}; separator=",\n">n"  
            "<endif>n"  
            "FROMn"  
            "    <table.tableName>   <table.tableAlias>n"  
            "<if(filters)>n"  
            "WHEREn"  
            "    <filters:{filter|<if(filter)><filter><endif>}; separator="\nAND ">n"  
            "<endif>n"  
            "<if(groups && !useAliasForGroup)>n"  
            "GROUP BYn"  
            "    <groups:{group|<if(group)><group.fieldName><endif>}; separator=",\n">n"  
            "<endif>n"  
            "<if(groups && useAliasForGroup)>n"  
            "GROUP BYn"  
            "    <groups:{group|<if(group)><group.fieldAlias><endif>}; separator=",\n">n"  
            "<endif>n"  
            "<if(orders)>n"  
            "ORDER BYn"  
            "    <orders:{order|<if(order)><order.orderAlias> <order.orderDirection><endif>}; separator=",\n">n"  
            "<endif>n"  
            ">>";

    public static String PREVIEW_SQL = "previewSql(limitFiled, groups, aggregators, filters, orders, table, isGroup, notUseAs, useAliasForGroup, distinct)n"  
            "::=<<n"  
            "SELECTn"  
            "<if(limitFiled)>n"  
            "   <limitFiled.limitFiled>n"  
            "<endif>n"  
            "<if(!groups && !aggregators)>n"  
            "    *n"  
            "<endif>n"  
            "<if(groups && notUseAs)>n"  
            "    <groups:{group|<if(group)><group.fieldName> <endif>}; separator=",\n">n"  
            "<endif>n"  
            "<if(groups && !notUseAs)>n"  
            "    <groups:{group|<if(group)><if(distinct)> DISTINCT <endif> <group.fieldName> AS <group.fieldAlias><endif>}; separator=",\n">n"  
            "<endif>n"  
            "    <if(groups && aggregators)>,<endif>n"  
            "<if(aggregators)>n"  
            "    <aggregators:{agg|<if(agg)><agg.fieldName> AS <agg.fieldAlias><endif>}; separator=",\n">n"  
            "<endif>n"  
            "FROMn"  
            "    <table.tableName>   <table.tableAlias>n"  
            "<if(filters)>n"  
            "WHEREn"  
            "    <filters:{filter|<if(filter)><filter><endif>}; separator="\nAND ">n"  
            "<endif>n"  
            "<if(isGroup && groups && !useAliasForGroup)>n"  
            "GROUP BYn"  
            "    <groups:{group|<if(group)><group.fieldName><endif>}; separator=",\n">n"  
            "<endif>n"  
            "<if(isGroup && groups && useAliasForGroup)>n"  
            "GROUP BYn"  
            "    <groups:{group|<if(group)><group.fieldAlias><endif>}; separator=",\n">n"  
            "<endif>n"  
            "<if(orders)>n"  
            "ORDER BYn"  
            "    <orders:{order|<if(order)><order.orderAlias> <order.orderDirection><endif>}; separator=",\n">n"  
            "<endif>n"  
            ">>";
}

0 人点赞