前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SqlTemplate类

SqlTemplate类

原创
作者头像
用户4161295
发布2024-09-24 09:12:53
540
发布2024-09-24 09:12:53
举报
文章被收录于专栏:dataease

这个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" +
            "SELECT\n" +
            "<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" +
            "FROM\n" +
            "    <table.tableName>   <table.tableAlias>\n" +
            "<if(filters)>\n" +
            "WHERE\n" +
            "    <filters:{filter|<if(filter)><filter><endif>}; separator=\"\\nAND \">\n" +
            "<endif>\n" +
            "<if(groups && !useAliasForGroup)>\n" +
            "GROUP BY\n" +
            "    <groups:{group|<if(group)><group.fieldName><endif>}; separator=\",\\n\">\n" +
            "<endif>\n" +
            "<if(groups && useAliasForGroup)>\n" +
            "GROUP BY\n" +
            "    <groups:{group|<if(group)><group.fieldAlias><endif>}; separator=\",\\n\">\n" +
            "<endif>\n" +
            "<if(orders)>\n" +
            "ORDER BY\n" +
            "    <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" +
            "SELECT\n" +
            "<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" +
            "FROM\n" +
            "    <table.tableName>   <table.tableAlias>\n" +
            "<if(filters)>\n" +
            "WHERE\n" +
            "    <filters:{filter|<if(filter)><filter><endif>}; separator=\"\\nAND \">\n" +
            "<endif>\n" +
            "<if(isGroup && groups && !useAliasForGroup)>\n" +
            "GROUP BY\n" +
            "    <groups:{group|<if(group)><group.fieldName><endif>}; separator=\",\\n\">\n" +
            "<endif>\n" +
            "<if(isGroup && groups && useAliasForGroup)>\n" +
            "GROUP BY\n" +
            "    <groups:{group|<if(group)><group.fieldAlias><endif>}; separator=\",\\n\">\n" +
            "<endif>\n" +
            "<if(orders)>\n" +
            "ORDER BY\n" +
            "    <orders:{order|<if(order)><order.orderAlias> <order.orderDirection><endif>}; separator=\",\\n\">\n" +
            "<endif>\n" +
            ">>";
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档