python sql模板_数据开发_SQL模板引擎

本文介绍了Python中用于动态拼接SQL的模板引擎,包括StringTemplate和内置的string.Template。详细讲解了如何使用这些模板引擎来解析和构建SQL语句,以及ANTLR作为SQL解析器的作用。还提到了模板引擎在数据分离、代码重用方面的优势,并提供了具体的使用示例。
摘要由CSDN通过智能技术生成

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的模板引擎库

org.antlr

ST4

4.3.1

如果自动下载不能的话,到 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

org.antlr

antlr4-runtime

4.9

ANTLR 4 Tool

org.antlr

antlr4

4.9

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值