• 数据开发_SQL模板引擎


    SQL模板引擎

       动态拼接SQL,替换变量,填充数据
       解析SQL命令
    

    动态拼接sql字符

    方案一:
          String/StringBuffer/StringBuilder
              缺点:Java不支持多行字符串,也不能自动解析字符串里的变量 
    		  一种是利用%操作符实现,另外一种是格式化字符串format实现
    
    方案二:
       基于XML配置文件生成SQL语句
       Yaml is in many ways similar to JSON
    
    方案三:
        采用一种模板引擎,把SQL语句写在模板中,其中的变化部分用模板引擎来填充及控制是否输出。
    	指定模板内容(字符串)中的特定标记(子字符串)替换一下便生成了最终需要的业务数据(比如网页)
    	数据分离(动态数据与静态数据),还可以实现代码单元共享(代码重用)
    	Templating engines
    
    	模板引擎:
    	    StringTemplate    https://www.stringtemplate.org/
    	   Freemarker
    	   Thymeleaf 3
    	   Beetl
    	    jOOQ with MyBatis.
    

    具体案例

     使用: StringTemplate(简称ST)是一个基于Java的模板引擎库
          <!-- https://mvnrepository.com/artifact/org.antlr/ST4 -->
          <dependency>
              <groupId>org.antlr</groupId>
              <artifactId>ST4</artifactId>
              <version>4.3.1</version>
          </dependency>
            <!-- stringtemplate 这个不用 -->
    		如果自动下载不能的话,到 https://www.stringtemplate.org/download.html 下载Jar包,然后project 加到工程里去
    		算是provided 包
         包内容:
    	   org.stringtemplate.v4	 
           org.stringtemplate.v4.compiler	 
           org.stringtemplate.v4.debug	 
           org.stringtemplate.v4.gui	 
           org.stringtemplate.v4.misc
    	常用的类:
    	     ST st=new ST(StringFormat); // ST 是 StringTemplate的一个实例
    	    模板:
    	       STGroupString
    	       public class STGroupString extends STGroup {
    	       public class STGroupFile   extends STGroup {
    	       public class STGroupDir    extends STGroup {
    	       public class STRawGroupDir extends STGroupDir {	       	 
    	语法:
    	   StringTemplate语法有两种组成元素,
    	      一种是属性(attribute),另一种是普通字符(plain text)。
    		  在$…$中包围的是属性,其余的都是普通字符。
    		    属性: 间接属性(indirect property names) 多值属性(multi-valued attribute)
    			属性的呈现(attribute render)
    		保留字:
    		   attribute中的property和这些保留字重复了,StringTemplate就会报错,解决方案是使用间接属性
           模板:
    	      命名模板(Group Files)
    		  匿名子模板(anonymous subtemplate)
    		  ST中默认的模板文件后缀名为st
    		  Group file names must end in .stg:
    		  拆分成多个模板,以模板组的方式使用更加方便
    

    SQL解析

       ANTLR 实现的 SQL解析器  
         Plugins中搜“ANTLR v4 grammar plugin”插件,重启IDEA即可使
        ANTLR 4 antlr4-runtime 
           <!-- https://mvnrepository.com/artifact/org.antlr/antlr4-runtime -->
            <dependency>
                <groupId>org.antlr</groupId>
                <artifactId>antlr4-runtime</artifactId>
                <version>4.9</version>
            </dependency>
    
        ANTLR 4 Tool 
           <!-- https://mvnrepository.com/artifact/org.antlr/antlr4 -->
            <dependency>
                <groupId>org.antlr</groupId>
                <artifactId>antlr4</artifactId>
                <version>4.9</version>
            </dependency>
    

    Python 中的字符替换

     # string.Template,将一个string设置为模板,通过替换变量的方法,最终得到想要的string
     Template是python中的string库的一部分
    1.变量是 $xxx,  其中xxx是满足python命名规则的字符串,即不能以数字开头,不能为关键字
       delimiter 字段 可以将$字符改变为其他字符,如“#”
       idpattern 字段 可以修改key的命名规则
       可用{}将xxx包裹起来
       两个重要的方法:substitute和safe_substitute. 差别对于参数缺少时的处理方式
    2.使用方式:
      首先通过Template初始化一个字符串。这些字符串中包含了一个个key。通过调用substitute或safe_subsititute,
      将key值与方法中传递过来的参数对应上,从而实现在指定的位置导入字符串
    3.注意事项:
       各个参数的顺序必须固定
    
    4.示例代码
    from string import Template
    
    tempTemplate = Template('$who likes $what')
    print(tempTemplate.substitute(who='Jac', what='letour'))
    tempTemplate.safe_substitute(who='Jac', what='letour')
    

    参考:

     Simple ETL generation series – an overview http://roelantvos.com/blog/simple-etl-generation-series-an-overview/
     SQL Templating with jOOQ or MyBatis https://blog.jooq.org/tag/template-engine/
     https://www.antlr.org/api/Java/index.html
     https://www.antlr.org/api/JavaTool/index.html
     https://github.com/antlr/stringtemplate4/blob/master/doc/introduction.md
    StringTemplate模板引擎用法 https://my.oschina.net/648885471/blog/2251858
  • 相关阅读:
    Android 自定义RadioButton的样式
    利用ListView批量删除item
    android中checkbox自定义样式
    Android 限定符
    Android Fragment的用法(二)
    Android Fragment的用法(一)
    Android 自定义控件
    Android 外部启动activity,自定义action,action常量大全
    Android之编写测试用例
    Android 定制自己的日志工具
  • 原文地址:https://www.cnblogs.com/ytwang/p/14110717.html