sql_script:基于SQL脚本的动态SQL构建工具项目

使用脚本构建SQL的工具

Branch1Tags0
FilesLast commitLast update
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago

脚本构建SQL的工具

能力

示例

解析SQL脚本

package demo

import sql_script.core.*
import std.collection.{ArrayList, HashMap}
import std.time.DateTime

main(): Int64 {
    let sql = ##"select * from abcd a where 
    is_del = #{isDel}
    -- @if(field1 != null && !isEmpty(field1)) {
        and field1 = #{field1}
        -- @if(isNotEmpty(field2)) {
        or field2 = ${field2}
        -- @}
    -- @}
    and field2 = ${field2}
    and id in
    -- @for(separator=',' open='(' close=')' index="index" item='item' collection='list') {
        #{item}
    -- @}
    "##

    let params = HashMap<String, ScriptParam>()
    params.put("field1", DateTime.now())
    params.put("isDel", 0)
    params.put("field2", "'1234'")
    params.put("list", [1, 2, 4])

    let template = fromSql(sql)

    println(template.bind(params))
    return 0
}

输出

select * from abcd a where is_del = 0 and field1 = '2025-05-15 16:17:05' or field2 = '1234' and field2 = '1234'and id in( 1, 2, 4) 

扫描Markdown目录

package demo

import sql_script.core.*
import std.collection.{ArrayList, HashMap}
import sql_script.mapper.markdown.*
import std.time.DateTime

main(): Int64 {
    let mapper = MarkdownMapper().fromDir("./testdata/markdown")

    let template = mapper.getTemplate("test", "test1")

    let params = HashMap<String, ScriptParam>()
    params.put("field1", DateTime.now())
    params.put("isDel", 0)
    params.put("field2", "'1234'")
    params.put("list", [1, 2, 4])

    println(template.bind(params))
    return 0
}

输出

select * from abcd a where is_del = 0 and field1 = '2025-05-15 16:17:05' or field2 = '1234' and field2 = '1234'and id in( 1, 2, 4)  

扫描XML目录

package demo

import sql_script.core.*
import std.collection.{ArrayList, HashMap}
import sql_script.mapper.xml.*
import std.time.DateTime

main(): Int64 {
    let mapper = XMLMapper().fromDir("./testdata/xml")

    let template = mapper.getTemplate("test", "test1")

    let params = HashMap<String, ScriptParam>()
    params.put("field1", DateTime.now())
    params.put("isDel", 0)
    params.put("field2", "'1234'")
    params.put("list", [1, 2, 4])

    println(template.bind(params))
    return 0
}

输出

select * from abcd a where is_del = 0 and field1 = '2025-05-15 16:17:05' or field2 = '1234' and field2 = '1234'and id in( 1, 2, 4)  

API

基础功能

通过SQL脚本构建模板实例

public func fromSql(sql: String): ScriptTemplate 
示例
main() {
    let sql = ##"select * from abcd a where 
    is_del = #{isDel}
    -- @if(field1 != null && !isEmpty(field1)) {
        and field1 = #{field1}
        -- @if(isNotEmpty(field2)) {
        or field2 = ${field2}
        -- @}
    -- @}
    and field2 = ${field2}
    and id in
    -- @for(separator=',' open='(' close=')' index="index" item='item' collection='list') {
        #{item}
    -- @}
    "##

    let template = fromSql(sql)
}

渲染SQL

语法

占位符

普通占位符

#{paramName}

若为字符串、时间,则会自动增加'

示例 数据类型 渲染结果
#{1234} 数字 1234
#{1234} 字符串 '1234'
#{true} 布尔值 true
#{time} 时间 '2025-05-15 01:23:45'
原始数据占位符

${paramName}

无论什么数据类型,都会以用户输入的内容进行渲染

示例 数据类型 渲染结果
${1234} 数字 1234
${1234} 字符串 1234
${true} 布尔值 true
${time} 时间 2025-05-15 01:23:45
main() {
    let template: ScriptTemplate = fromSql("select id, name from user_info limit #{offset}, #{pageSize}")

    let params = HashMap<String, ScriptParam>()
    let page = 1
    let pageSize = 10
    params.put("offset", (page - 1) * pageSize)
    params.put("pageSize", pageSize)

    let sql = template.bind(params)
}

控制流

IF
-- @if(条件表达式) {
    渲染内容
-- @}
示例
-- @if(field1 != null) {
    my_field = #{field1}
-- @}
FOR
-- @for(separator='内容分割字符串(可选)' open='开始字符串(可选)' close='结束字符串(可选)' index="索引引用名(可选)" item='元素引用名' collection='集合') {
    #{item}
-- @}
示例
-- @for(separator=',' open='(' close=')' index="index" item='item' collection='list') {
    #{item}
-- @}

函数调用

只适用于IF控制流中的条件表达式

isEmpty(field1)
示例
-- @if(field1 != null && !isEmpty(field1)) {
    and field1 = #{field1}
    -- @if(isNotEmpty(field2)) {
    or field2 = ${field2}
    -- @}
-- @}
自定义函数
import sql_script.types.*
import sql_script.functions.*

let _ = register(IsEmpty())

public struct IsEmpty <: Functions {
    public prop name: String {
        get() {
            "isEmpty"
        }
    }

    public func invoke(params: Array<ScriptParam>): ScriptParam {
        if (params.isEmpty()) {
            return true
        }

        for (idx in 0..params.size) {
            let item = params[idx]
            if (let Some(result) <- isEmpty(item)) {
                if (!result) {
                    return false
                }
            } else {
                throw Exception("unsupported argument type for index [${idx}]")
            }
        }
        return true
    }
}

private func isEmpty(item: ScriptParam): ?Bool {
    match (item) {
        case v: String =>
            if (v.size > 0) {
                return false
            }

        case v: Array<ScriptParam> =>
            if (v.size > 0) {
                return false
            }

        case v: ArrayList<ScriptParam> =>
            if (v.size > 0) {
                return false
            }

        case v: ToStringable =>
            if (v.convertToString().size > 0) {
                return false
            }

        case v: Option<ScriptParam> => match (v) {
            case Some(_v) => return isEmpty(item)

            case None => return true
        }

        case _ => return None
    }

    return true
}

Introduction

使用脚本构建SQL的工具

Customize your domain