數據開發_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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM