Apache Calcite教程-SQL解析-FMPP

  • fmpp 具体代码实例
    • 1.添加Maven依赖
    • fmpp配置文件
    • freemarker模板1
    • freemarker模板2
    • 执行maven插件
    • 生成文件
  • 代码示例

Github

fmpp 具体代码实例

FMPP是以freemarker为模板的模板生成器

1.添加Maven依赖

<plugin>
    <configuration>
        
        <cfgFile>src/main/codegen/config.fmppcfgFile>
        
        <outputDirectory>target/generated-sources/fmpp/outputDirectory>
        
        <templateDirectory>src/main/codegen/templatestemplateDirectory>
    configuration>
    <groupId>com.googlecode.fmpp-maven-plugingroupId>
    <artifactId>fmpp-maven-pluginartifactId>
    <version>1.0version>
    <executions>
        <execution>
            <phase>generate-sourcesphase>
            <goals>
                <goal>generategoal>
            goals>
        execution>
    executions>
    <dependencies>
        <dependency>
            <groupId>org.freemarkergroupId>
            <artifactId>freemarkerartifactId>
            <version>2.3.28version>
        dependency>
        <dependency>
            <groupId>net.sourceforge.fmppgroupId>
            <artifactId>fmppartifactId>
            <version>0.9.16version>
            <exclusions>
                <exclusion>
                <groupId>org.freemarkergroupId>
                <artifactId>freemarkerartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>
plugin>

fmpp配置文件

fmpp配置文件 config.fmpp

# 用data标示为变量
# 一般变量替换为 ${one} or ${two.three} ,具体语法请参考freemarker语法
# include 指令插入另个freemarker模板
data: {
     one:1,
     two: {
        three: 3
     }
     implementationFiles: [
             "parserImpls.ftl"
      ]
}

#
freemarkerLinks: {
    includes: includes/
}

freemarker模板1

模板1 Main.ftl

public class Main {
    public static void main(String[] args){
        System.out.println(${one} + ${two.three});
    }
    /**
     * 额外附加代码
     */
    <#list implementationFiles as file>
        <#include "/@includes/"+file />
    
}

freemarker模板2

模板2 parserImpls.ftl

static {
    System.out.println(${one});
    System.out.println(${two.three});
}

执行maven插件

执行maven命令

mvn fmpp:generate

生成文件

生成如下文件

public class Main {
    public static void main(String[] args){
        System.out.println(1 + 3);
    }
    /**
     * 额外附加代码
     */
static {
    System.out.println(1);
    System.out.println(3);
}}

代码示例

https://github.com/quxiucheng/apache-calcite-tutorial/tree/master/calcite-tutorial-2-parser/parser-1-fmpp-tutorial

你可能感兴趣的:(calcite)