这个Java类 SqlTemplate
主要用于定义和存储SQL查询模板字符串,这些模板字符串使用StringTemplate库的语法编写。以下是类的主要功能和特点的概要描述:
QUERY_SQL
和 PREVIEW_SQL
,它们分别定义了两种不同的SQL查询模板。QUERY_SQL
模板用于生成标准的SQL查询语句,包括选择字段、表名、别名、WHERE条件、GROUP BY子句、ORDER BY子句等。PREVIEW_SQL
模板用于生成预览数据的SQL查询语句,它与 QUERY_SQL
类似,但可能包含一些特定的预览相关的逻辑,如去重(DISTINCT)等。<if(condition)>
),可以根据传入的参数值决定是否包含特定的SQL片段。<groups:{group|...}; separator=\",\">
),可以迭代传入的列表参数,为每个元素生成相应的SQL片段。SQLProvider
类中使用,通过填充具体的参数来生成完整的SQL查询语句。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 删除。