Skip to content

Commit 4a8c254

Browse files
committed
f
1 parent 3a0d1c4 commit 4a8c254

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

core/dal-runtime/src/TableSqlMap.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { TemplateUtil } from './TemplateUtil';
55
import { SqlType } from '@eggjs/tegg-types';
66
import type { SqlMap } from '@eggjs/tegg-types';
77

8+
const SQL_PARAMS = '$$__sql_params';
9+
810
export interface SqlGenerator {
911
type: SqlType;
1012
template: Template,
@@ -49,8 +51,8 @@ export class TableSqlMap {
4951

5052
// Add param filter for parameterized queries
5153
env.addFilter('param', function(this: Template, value: any) {
52-
if ((this as any).env.$$params) {
53-
(this as any).env.$$params.push(value);
54+
if ((this as any).ctx[SQL_PARAMS]) {
55+
(this as any).ctx[SQL_PARAMS].push(value);
5456
}
5557
return '?';
5658
});
@@ -101,12 +103,13 @@ export class TableSqlMap {
101103

102104
// Set timezone and params collector on env
103105
(template as any).env.timezone = timezone;
104-
(template as any).env.$$params = params;
105106

106-
const sql = template.render(data);
107+
const context = {
108+
...data,
109+
[SQL_PARAMS]: params,
110+
};
107111

108-
// Clean up params collector
109-
delete (template as any).env.$$params;
112+
const sql = template.render(context);
110113

111114
return { sql, params };
112115
}

plugin/dal/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ export default class FooDAO extends BaseFooDAO {
612612
613613
**param 过滤器**
614614
615-
`param` 过滤器用于将值作为参数化查询参数,而不是直接拼接到 SQL 字符串中。这可以有效防止 SQL 注入攻击,并支持使用数据库的预编译语句功能
615+
`param` 过滤器用于将值作为参数化查询参数,而不是直接拼接到 SQL 字符串中。可以有效利用到 sql parameters 的能力,小幅提升 db 性能与观测能力
616616
617617
使用示例:
618618
@@ -633,8 +633,6 @@ export default {
633633
生成的 SQL:`SELECT ... FROM egg_foo WHERE name = ? AND age > ?`
634634
参数数组:`['John', 18]`
635635

636-
**注意**:建议在所有用户输入的值上使用 `param` 过滤器,以确保安全性。
637-
638636
支持自定义 block 来简化 sql, 如内置的 allColumns
639637

640638
```ts

0 commit comments

Comments
 (0)