Skip to content

apijson 支持sql语句模版, 传递参数, 执行sql语句 #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cloudAndMonkey opened this issue Feb 15, 2023 · 9 comments
Open

apijson 支持sql语句模版, 传递参数, 执行sql语句 #510

cloudAndMonkey opened this issue Feb 15, 2023 · 9 comments
Labels
Enhancement 增强 增强功能、提高性能等

Comments

@cloudAndMonkey
Copy link
Contributor

Description

apijson-router Document表 autosql字段
image
SQLAuto-智能零代码自动化测试 SQL 语句执行结果的数据库工具,任意增删改查、任意 SQL 模板变量、一键批量生成参数组合、快速构造大量测试数据。
https://github.com/TommyLemon/SQLAuto

目的:
sql模版 实现相关统计查询

@cloudAndMonkey
Copy link
Contributor Author

@raw 实现相同功能, 等空了,我整理测试用例, 如果raw支持, router方式优先级降低

@TommyLemon TommyLemon added the Enhancement 增强 增强功能、提高性能等 label Feb 16, 2023
@cloudAndMonkey
Copy link
Contributor Author

cloudAndMonkey commented Feb 27, 2023

@raw : 指定某个代码片段使用原生sql
方式一:

{
    "[]": {
        "Moment": {
            "@column": "date;left(date,10):day;sum(if(userId%2=0,1,0))", 
            "@group": "day", 
            "@having": "to_days(now())-to_days(`date`)<=7", 
            "@raw": "@column,@having"
        }
    }
}

方式二:

RAW_MAP.put("siteListWhereItem1","`Opm_site_list`.`seq` < `to`.`seq`");
"sql@": {
         "from": "Opm_site_list",
         "join": "&/Opm_site_list:to/opm_line_id@",
         "Opm_site_list": {
             "@column": "opm_line_id",
             "opm_site_id": 1, //出发站点id
            "@raw": "siteListWhereItem1"
         },
         "Opm_site_list:to": {
             "opm_line_id@": "/Opm_site_list/opm_line_id",
             "@column": "",
             "opm_site_id": 2 //到达站点id
         }
     }

@cloudAndMonkey
Copy link
Contributor Author

@TommyLemon
统计报表等复杂场景, apijson支持原生sql语句模版, 预编译执行,避免sql注入

模版

router 配置使用请参见: https://github.com/APIJSON/apijson-router

Document表字段sqlauto配置sql模版

SELECT * FROM user where username LIKE concat(?,'%') and state = ? and deleted != 1 limit ?,?

Document表字段apijson配置请求json模版

{
	"@datasource": "master",
	"paramName": ["name","state","page","count"],
	"tag": "RawUserList",
	"rawData()": "rawSQL(paramName)",
	"removeKeys+()": "removeKeys(paramName)"
}

Function表配置 远程函数 rawData()

image

Request表配置router url

image

客户端发起request请求

{
	"name": "test1",
	"state": 0,
	"page":0,
	"count":4
}

image

@TommyLemon
Copy link
Collaborator

TommyLemon commented Feb 27, 2023

@raw : 指定某个代码片段使用原生sql 方式一:

{
    "[]": {
        "Moment": {
            "@column": "date;left(date,10):day;sum(if(userId%2=0,1,0))", 
            "@group": "day", 
            "@having": "to_days(now())-to_days(`date`)<=7", 
            "@raw": "@column,@having"
        }
    }
}

方式二:

RAW_MAP.put("siteListWhereItem1","`Opm_site_list`.`seq` < `to`.`seq`");
"sql@": {
         "from": "Opm_site_list",
         "join": "&/Opm_site_list:to/opm_line_id@",
         "Opm_site_list": {
             "@column": "opm_line_id",
             "opm_site_id": 1, //出发站点id
            "@raw": "siteListWhereItem1"
         },
         "Opm_site_list:to": {
             "opm_line_id@": "/Opm_site_list/opm_line_id",
             "@column": "",
             "opm_site_id": 2 //到达站点id
         }
     }

方式一是目前唯一可用 @raw 的,不过也必须提前在 RAW_MAP 中配置允许的 SQL 片段。

方式二 "@raw": "siteListWhereItem1" 在最近比较新的版本都不可用,@raw 原始 SQL 片段功能是 @zhoulingfengofcd 新增的:
#126

他分享的这篇文章提到了 @raw 的使用:APIJSON复杂业务深入实践(类似12306订票系统)
https://blog.csdn.net/aa330233789/article/details/105309571

不过我改成了值里面的所有 key 必须为当前 JSON 对象的 key,可以更加灵活地支撑各种相关需求。

@TommyLemon
Copy link
Collaborator

TommyLemon commented Feb 27, 2023

@cloudAndMonkey
感谢你的多次重要贡献,你已经是 APIJSON 的重要贡献者之一了,我们将向你发送加入 APIJSON 开源组织的邀请,有兴趣的话可以通过 GitHub Notifications 或 GitHub 发送的加入 APIJSON 邮件 来接受邀请哦~
APIJSON 组织首页:
https://github.com/APIJSON

@cloudAndMonkey
Copy link
Contributor Author

@cloudAndMonkey 感谢你的多次重要贡献,你已经是 APIJSON 的重要贡献者之一了,我们将向你发送加入 APIJSON 开源组织的邀请,有兴趣的话可以通过 GitHub Notifications 或 GitHub 发送的加入 APIJSON 邮件 来接受邀请哦~ APIJSON 组织首页: https://github.com/APIJSON

@TommyLemon 谢谢, 链接过期了,重新发一下嘛 😄

@cloudAndMonkey
Copy link
Contributor Author

@raw : 指定某个代码片段使用原生sql 方式一:

{
    "[]": {
        "Moment": {
            "@column": "date;left(date,10):day;sum(if(userId%2=0,1,0))", 
            "@group": "day", 
            "@having": "to_days(now())-to_days(`date`)<=7", 
            "@raw": "@column,@having"
        }
    }
}

方式二:

RAW_MAP.put("siteListWhereItem1","`Opm_site_list`.`seq` < `to`.`seq`");
"sql@": {
         "from": "Opm_site_list",
         "join": "&/Opm_site_list:to/opm_line_id@",
         "Opm_site_list": {
             "@column": "opm_line_id",
             "opm_site_id": 1, //出发站点id
            "@raw": "siteListWhereItem1"
         },
         "Opm_site_list:to": {
             "opm_line_id@": "/Opm_site_list/opm_line_id",
             "@column": "",
             "opm_site_id": 2 //到达站点id
         }
     }

方式一是目前唯一可用 @raw 的,不过也必须提前在 RAW_MAP 中配置允许的 SQL 片段。

方式二 "@raw": "siteListWhereItem1" 在最近比较新的版本都不可用,@raw 原始 SQL 片段功能是 @zhoulingfengofcd 新增的: #126

他分享的这篇文章提到了 @raw 的使用:APIJSON复杂业务深入实践(类似12306订票系统) https://blog.csdn.net/aa330233789/article/details/105309571

不过我改成了值里面的所有 key 必须为当前 JSON 对象的 key,可以更加灵活地支撑各种相关需求。

嗯嗯,我来测一下

@TommyLemon
Copy link
Collaborator

@cloudAndMonkey 感谢你的多次重要贡献,你已经是 APIJSON 的重要贡献者之一了,我们将向你发送加入 APIJSON 开源组织的邀请,有兴趣的话可以通过 GitHub Notifications 或 GitHub 发送的加入 APIJSON 邮件 来接受邀请哦~ APIJSON 组织首页: https://github.com/APIJSON

@TommyLemon 谢谢, 链接过期了,重新发一下嘛 😄

@cloudAndMonkey
已邀请

@cloudAndMonkey
Copy link
Contributor Author

@TommyLemon
等我空了把这些功能使用, 放到 APIJSON文档里面 , 测试案例整理到 http://apijson.cn/api/
我先把 redis 测完, 和 raw原生sql统计 一起提交demo, 然后再搞这个

cloudAndMonkey added a commit to cloudAndMonkey/APIJSON-Demo that referenced this issue Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement 增强 增强功能、提高性能等
Projects
None yet
Development

No branches or pull requests

2 participants