metal-orm 1.0.80 → 1.0.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +460 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +99 -24
- package/dist/index.d.ts +99 -24
- package/dist/index.js +458 -71
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
- package/src/codegen/typescript.ts +40 -29
- package/src/core/ast/expression-nodes.ts +32 -21
- package/src/core/ast/expression-visitor.ts +6 -1
- package/src/core/ast/expression.ts +1 -0
- package/src/core/ast/param-proxy.ts +50 -0
- package/src/core/dialect/abstract.ts +12 -10
- package/src/core/execution/db-executor.ts +5 -4
- package/src/core/execution/executors/mysql-executor.ts +9 -7
- package/src/openapi/index.ts +1 -0
- package/src/openapi/query-parameters.ts +207 -0
- package/src/openapi/schema-extractor.ts +290 -122
- package/src/openapi/schema-types.ts +72 -6
- package/src/openapi/type-mappers.ts +28 -8
- package/src/orm/unit-of-work.ts +25 -13
- package/src/query-builder/relation-filter-utils.ts +1 -0
- package/src/query-builder/select.ts +17 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metal-orm",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.82",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"engines": {
|
|
@@ -59,11 +59,13 @@
|
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"@electric-sql/pglite": "^0.3.14",
|
|
62
|
+
"@types/express": "^5.0.6",
|
|
62
63
|
"@typescript-eslint/eslint-plugin": "^8.20.0",
|
|
63
64
|
"@typescript-eslint/parser": "^8.20.0",
|
|
64
65
|
"@vitest/ui": "^4.0.14",
|
|
65
66
|
"eslint": "^8.57.0",
|
|
66
67
|
"eslint-plugin-deprecation": "^3.0.0",
|
|
68
|
+
"express": "^5.2.1",
|
|
67
69
|
"mysql-memory-server": "^1.13.0",
|
|
68
70
|
"mysql2": "^3.15.3",
|
|
69
71
|
"pg": "^8.16.3",
|
|
@@ -17,13 +17,14 @@ import {
|
|
|
17
17
|
LiteralNode,
|
|
18
18
|
FunctionNode,
|
|
19
19
|
AliasRefNode,
|
|
20
|
-
CastExpressionNode,
|
|
21
|
-
CollateExpressionNode,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
CastExpressionNode,
|
|
21
|
+
CollateExpressionNode,
|
|
22
|
+
ParamNode,
|
|
23
|
+
ExpressionVisitor,
|
|
24
|
+
OperandVisitor,
|
|
25
|
+
visitExpression,
|
|
26
|
+
visitOperand
|
|
27
|
+
} from '../core/ast/expression.js';
|
|
27
28
|
import { SQL_OPERATOR_REGISTRY } from '../core/sql/sql-operator-config.js';
|
|
28
29
|
import { SqlOperator } from '../core/sql/sql.js';
|
|
29
30
|
import { isRelationAlias } from '../query-builder/relation-alias.js';
|
|
@@ -190,13 +191,14 @@ export class TypeScriptGenerator implements ExpressionVisitor<string>, OperandVi
|
|
|
190
191
|
case 'ScalarSubquery':
|
|
191
192
|
case 'CaseExpression':
|
|
192
193
|
case 'WindowFunction':
|
|
193
|
-
case 'Cast':
|
|
194
|
-
case 'Collate':
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
194
|
+
case 'Cast':
|
|
195
|
+
case 'Collate':
|
|
196
|
+
case 'Param':
|
|
197
|
+
return this.printOperand(term);
|
|
198
|
+
default:
|
|
199
|
+
return this.printExpression(term);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
200
202
|
|
|
201
203
|
private getSelectionKey(selection: SelectionColumn, index: number): string {
|
|
202
204
|
if (selection.alias) {
|
|
@@ -244,13 +246,17 @@ export class TypeScriptGenerator implements ExpressionVisitor<string>, OperandVi
|
|
|
244
246
|
return this.printColumnOperand(node);
|
|
245
247
|
}
|
|
246
248
|
|
|
247
|
-
public visitLiteral(node: LiteralNode): string {
|
|
248
|
-
return this.printLiteralOperand(node);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
public
|
|
252
|
-
return this.
|
|
253
|
-
}
|
|
249
|
+
public visitLiteral(node: LiteralNode): string {
|
|
250
|
+
return this.printLiteralOperand(node);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
public visitParam(node: ParamNode): string {
|
|
254
|
+
return this.printParamOperand(node);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
public visitFunction(node: FunctionNode): string {
|
|
258
|
+
return this.printFunctionOperand(node);
|
|
259
|
+
}
|
|
254
260
|
|
|
255
261
|
public visitJsonPath(node: JsonPathNode): string {
|
|
256
262
|
return this.printJsonPathOperand(node);
|
|
@@ -379,14 +385,19 @@ export class TypeScriptGenerator implements ExpressionVisitor<string>, OperandVi
|
|
|
379
385
|
* @param literal - Literal node
|
|
380
386
|
* @returns TypeScript code representation
|
|
381
387
|
*/
|
|
382
|
-
private printLiteralOperand(literal: LiteralNode): string {
|
|
383
|
-
if (literal.value === null) return 'null';
|
|
384
|
-
return typeof literal.value === 'string' ? `'${literal.value}'` : String(literal.value);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
388
|
+
private printLiteralOperand(literal: LiteralNode): string {
|
|
389
|
+
if (literal.value === null) return 'null';
|
|
390
|
+
return typeof literal.value === 'string' ? `'${literal.value}'` : String(literal.value);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private printParamOperand(param: ParamNode): string {
|
|
394
|
+
const name = param.name.replace(/'/g, "\\'");
|
|
395
|
+
return `{ type: 'Param', name: '${name}' }`;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Prints a function operand to TypeScript code
|
|
400
|
+
* @param fn - Function node
|
|
390
401
|
* @returns TypeScript code representation
|
|
391
402
|
*/
|
|
392
403
|
private printFunctionOperand(fn: FunctionNode): string {
|
|
@@ -2,14 +2,23 @@ import type { SelectQueryNode, OrderByNode } from './query.js';
|
|
|
2
2
|
import { SqlOperator, BitwiseOperator } from '../sql/sql.js';
|
|
3
3
|
import { ColumnRef } from './types.js';
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* AST node representing a literal value
|
|
7
|
-
*/
|
|
5
|
+
/**
|
|
6
|
+
* AST node representing a literal value
|
|
7
|
+
*/
|
|
8
8
|
export interface LiteralNode {
|
|
9
9
|
type: 'Literal';
|
|
10
10
|
/** The literal value (string, number, boolean, Date, or null) */
|
|
11
11
|
value: string | number | boolean | Date | null;
|
|
12
12
|
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* AST node representing a named parameter placeholder
|
|
16
|
+
*/
|
|
17
|
+
export interface ParamNode {
|
|
18
|
+
type: 'Param';
|
|
19
|
+
/** Stable parameter name */
|
|
20
|
+
name: string;
|
|
21
|
+
}
|
|
13
22
|
|
|
14
23
|
/**
|
|
15
24
|
* AST node representing a reference to a SELECT alias (for ORDER BY / GROUP BY).
|
|
@@ -149,29 +158,31 @@ export interface ArithmeticExpressionNode {
|
|
|
149
158
|
/**
|
|
150
159
|
* Union type representing any operand that can be used in expressions
|
|
151
160
|
*/
|
|
152
|
-
export type OperandNode =
|
|
153
|
-
| AliasRefNode
|
|
154
|
-
| ColumnNode
|
|
155
|
-
| LiteralNode
|
|
156
|
-
|
|
|
157
|
-
|
|
|
158
|
-
|
|
|
159
|
-
|
|
|
160
|
-
|
|
|
161
|
+
export type OperandNode =
|
|
162
|
+
| AliasRefNode
|
|
163
|
+
| ColumnNode
|
|
164
|
+
| LiteralNode
|
|
165
|
+
| ParamNode
|
|
166
|
+
| FunctionNode
|
|
167
|
+
| JsonPathNode
|
|
168
|
+
| ScalarSubqueryNode
|
|
169
|
+
| CaseExpressionNode
|
|
170
|
+
| CastExpressionNode
|
|
161
171
|
| WindowFunctionNode
|
|
162
172
|
| ArithmeticExpressionNode
|
|
163
173
|
| BitwiseExpressionNode
|
|
164
174
|
| CollateExpressionNode;
|
|
165
175
|
|
|
166
|
-
const operandTypes = new Set<OperandNode['type']>([
|
|
167
|
-
'AliasRef',
|
|
168
|
-
'Column',
|
|
169
|
-
'Literal',
|
|
170
|
-
'
|
|
171
|
-
'
|
|
172
|
-
'
|
|
173
|
-
'
|
|
174
|
-
'
|
|
176
|
+
const operandTypes = new Set<OperandNode['type']>([
|
|
177
|
+
'AliasRef',
|
|
178
|
+
'Column',
|
|
179
|
+
'Literal',
|
|
180
|
+
'Param',
|
|
181
|
+
'Function',
|
|
182
|
+
'JsonPath',
|
|
183
|
+
'ScalarSubquery',
|
|
184
|
+
'CaseExpression',
|
|
185
|
+
'Cast',
|
|
175
186
|
'WindowFunction',
|
|
176
187
|
'ArithmeticExpression',
|
|
177
188
|
'BitwiseExpression',
|
|
@@ -18,7 +18,8 @@ import {
|
|
|
18
18
|
WindowFunctionNode,
|
|
19
19
|
CollateExpressionNode,
|
|
20
20
|
AliasRefNode,
|
|
21
|
-
BitwiseExpressionNode
|
|
21
|
+
BitwiseExpressionNode,
|
|
22
|
+
ParamNode
|
|
22
23
|
} from './expression-nodes.js';
|
|
23
24
|
|
|
24
25
|
/**
|
|
@@ -42,6 +43,7 @@ export interface ExpressionVisitor<R> {
|
|
|
42
43
|
export interface OperandVisitor<R> {
|
|
43
44
|
visitColumn?(node: ColumnNode): R;
|
|
44
45
|
visitLiteral?(node: LiteralNode): R;
|
|
46
|
+
visitParam?(node: ParamNode): R;
|
|
45
47
|
visitFunction?(node: FunctionNode): R;
|
|
46
48
|
visitJsonPath?(node: JsonPathNode): R;
|
|
47
49
|
visitScalarSubquery?(node: ScalarSubqueryNode): R;
|
|
@@ -187,6 +189,9 @@ export const visitOperand = <R>(node: OperandNode, visitor: OperandVisitor<R>):
|
|
|
187
189
|
case 'Literal':
|
|
188
190
|
if (visitor.visitLiteral) return visitor.visitLiteral(node);
|
|
189
191
|
break;
|
|
192
|
+
case 'Param':
|
|
193
|
+
if (visitor.visitParam) return visitor.visitParam(node);
|
|
194
|
+
break;
|
|
190
195
|
case 'Function':
|
|
191
196
|
if (visitor.visitFunction) return visitor.visitFunction(node);
|
|
192
197
|
break;
|
|
@@ -9,6 +9,7 @@ export * from './expression-builders.js';
|
|
|
9
9
|
export * from './window-functions.js';
|
|
10
10
|
export * from './aggregate-functions.js';
|
|
11
11
|
export * from './expression-visitor.js';
|
|
12
|
+
export * from './param-proxy.js';
|
|
12
13
|
export type { ColumnRef, TableRef as AstTableRef } from './types.js';
|
|
13
14
|
export * from './adapters.js';
|
|
14
15
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ParamNode } from './expression-nodes.js';
|
|
2
|
+
|
|
3
|
+
export type ParamProxy = ParamNode & {
|
|
4
|
+
[key: string]: ParamProxy;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type ParamProxyRoot = {
|
|
8
|
+
[key: string]: ParamProxy;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const buildParamProxy = (name: string): ParamProxy => {
|
|
12
|
+
const target: ParamNode = { type: 'Param', name };
|
|
13
|
+
return new Proxy(target, {
|
|
14
|
+
get(t, prop, receiver) {
|
|
15
|
+
if (prop === 'then') return undefined;
|
|
16
|
+
if (typeof prop === 'symbol') {
|
|
17
|
+
return Reflect.get(t, prop, receiver);
|
|
18
|
+
}
|
|
19
|
+
if (typeof prop === 'string' && prop.startsWith('$')) {
|
|
20
|
+
const trimmed = prop.slice(1);
|
|
21
|
+
const nextName = name ? `${name}.${trimmed}` : trimmed;
|
|
22
|
+
return buildParamProxy(nextName);
|
|
23
|
+
}
|
|
24
|
+
if (prop in t) {
|
|
25
|
+
return (t as unknown as Record<string, unknown>)[prop];
|
|
26
|
+
}
|
|
27
|
+
const nextName = name ? `${name}.${prop}` : prop;
|
|
28
|
+
return buildParamProxy(nextName);
|
|
29
|
+
}
|
|
30
|
+
}) as ParamProxy;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const createParamProxy = (): ParamProxyRoot => {
|
|
34
|
+
const target: Record<string, unknown> = {};
|
|
35
|
+
return new Proxy(target, {
|
|
36
|
+
get(t, prop, receiver) {
|
|
37
|
+
if (prop === 'then') return undefined;
|
|
38
|
+
if (typeof prop === 'symbol') {
|
|
39
|
+
return Reflect.get(t, prop, receiver);
|
|
40
|
+
}
|
|
41
|
+
if (typeof prop === 'string' && prop.startsWith('$')) {
|
|
42
|
+
return buildParamProxy(prop.slice(1));
|
|
43
|
+
}
|
|
44
|
+
if (prop in t) {
|
|
45
|
+
return (t as unknown as Record<string, unknown>)[prop];
|
|
46
|
+
}
|
|
47
|
+
return buildParamProxy(String(prop));
|
|
48
|
+
}
|
|
49
|
+
}) as ParamProxyRoot;
|
|
50
|
+
};
|
|
@@ -26,10 +26,11 @@ import {
|
|
|
26
26
|
BetweenExpressionNode,
|
|
27
27
|
ArithmeticExpressionNode,
|
|
28
28
|
BitwiseExpressionNode,
|
|
29
|
-
CollateExpressionNode,
|
|
30
|
-
AliasRefNode,
|
|
31
|
-
isOperandNode
|
|
32
|
-
|
|
29
|
+
CollateExpressionNode,
|
|
30
|
+
AliasRefNode,
|
|
31
|
+
isOperandNode,
|
|
32
|
+
ParamNode
|
|
33
|
+
} from '../ast/expression.js';
|
|
33
34
|
import { DialectName } from '../sql/sql.js';
|
|
34
35
|
import type { FunctionStrategy } from '../functions/types.js';
|
|
35
36
|
import { StandardFunctionStrategy } from '../functions/standard-strategy.js';
|
|
@@ -453,12 +454,13 @@ export abstract class Dialect
|
|
|
453
454
|
}
|
|
454
455
|
|
|
455
456
|
private registerDefaultOperandCompilers(): void {
|
|
456
|
-
this.registerOperandCompiler('Literal', (literal: LiteralNode, ctx) => ctx.addParameter(literal.value));
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
457
|
+
this.registerOperandCompiler('Literal', (literal: LiteralNode, ctx) => ctx.addParameter(literal.value));
|
|
458
|
+
this.registerOperandCompiler('Param', (_param: ParamNode, ctx) => ctx.addParameter(null));
|
|
459
|
+
|
|
460
|
+
this.registerOperandCompiler('AliasRef', (alias: AliasRefNode, _ctx) => {
|
|
461
|
+
void _ctx;
|
|
462
|
+
return this.quoteIdentifier(alias.name);
|
|
463
|
+
});
|
|
462
464
|
|
|
463
465
|
this.registerOperandCompiler('Column', (column: ColumnNode, _ctx) => {
|
|
464
466
|
void _ctx;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// src/core/execution/db-executor.ts
|
|
2
2
|
|
|
3
3
|
// low-level canonical shape
|
|
4
|
-
export type QueryResult = {
|
|
5
|
-
columns: string[];
|
|
6
|
-
values: unknown[][];
|
|
7
|
-
|
|
4
|
+
export type QueryResult = {
|
|
5
|
+
columns: string[];
|
|
6
|
+
values: unknown[][];
|
|
7
|
+
insertId?: number;
|
|
8
|
+
};
|
|
8
9
|
|
|
9
10
|
export interface DbExecutor {
|
|
10
11
|
/** Capability flags so the runtime can make correct decisions without relying on optional methods. */
|
|
@@ -31,13 +31,15 @@ export function createMysqlExecutor(
|
|
|
31
31
|
capabilities: {
|
|
32
32
|
transactions: supportsTransactions,
|
|
33
33
|
},
|
|
34
|
-
async executeSql(sql, params) {
|
|
35
|
-
const [rows] = await client.query(sql, params);
|
|
36
|
-
|
|
37
|
-
if (!Array.isArray(rows)) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
async executeSql(sql, params) {
|
|
35
|
+
const [rows] = await client.query(sql, params);
|
|
36
|
+
|
|
37
|
+
if (!Array.isArray(rows)) {
|
|
38
|
+
const insertId = (rows as { insertId?: number } | null)?.insertId;
|
|
39
|
+
const normalized = typeof insertId === 'number' && insertId > 0 ? insertId : undefined;
|
|
40
|
+
// e.g. insert/update returning only headers, treat as no rows
|
|
41
|
+
return [{ columns: [], values: [], insertId: normalized }];
|
|
42
|
+
}
|
|
41
43
|
|
|
42
44
|
const result = rowsToQueryResult(
|
|
43
45
|
rows as Array<Record<string, unknown>>
|
package/src/openapi/index.ts
CHANGED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import type { TableDef } from '../schema/table.js';
|
|
2
|
+
import {
|
|
3
|
+
isOperandNode,
|
|
4
|
+
type ExpressionNode,
|
|
5
|
+
type OperandNode,
|
|
6
|
+
type ColumnNode,
|
|
7
|
+
type FunctionNode,
|
|
8
|
+
type JsonPathNode,
|
|
9
|
+
type CaseExpressionNode,
|
|
10
|
+
type CastExpressionNode,
|
|
11
|
+
type WindowFunctionNode,
|
|
12
|
+
type ArithmeticExpressionNode,
|
|
13
|
+
type BitwiseExpressionNode,
|
|
14
|
+
type CollateExpressionNode
|
|
15
|
+
} from '../core/ast/expression.js';
|
|
16
|
+
import type { TableSourceNode, OrderByNode, OrderingTerm } from '../core/ast/query.js';
|
|
17
|
+
import type { ColumnSchemaOptions, JsonSchemaProperty, OpenApiParameter } from './schema-types.js';
|
|
18
|
+
import { mapColumnType } from './type-mappers.js';
|
|
19
|
+
|
|
20
|
+
const FILTER_PARAM_NAME = 'filter';
|
|
21
|
+
|
|
22
|
+
const buildRootTableNames = (table: TableDef, from?: TableSourceNode): Set<string> => {
|
|
23
|
+
const names = new Set<string>([table.name]);
|
|
24
|
+
if (from?.type === 'Table') {
|
|
25
|
+
names.add(from.name);
|
|
26
|
+
if (from.alias) names.add(from.alias);
|
|
27
|
+
}
|
|
28
|
+
return names;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const collectFilterColumns = (
|
|
32
|
+
expr: ExpressionNode,
|
|
33
|
+
table: TableDef,
|
|
34
|
+
rootTables: Set<string>
|
|
35
|
+
): Set<string> => {
|
|
36
|
+
const columns = new Set<string>();
|
|
37
|
+
|
|
38
|
+
const recordColumn = (node: ColumnNode): void => {
|
|
39
|
+
if (!rootTables.has(node.table)) return;
|
|
40
|
+
if (node.name in table.columns) {
|
|
41
|
+
columns.add(node.name);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const visitOrderingTerm = (term: OrderingTerm): void => {
|
|
46
|
+
if (!term || typeof term !== 'object') return;
|
|
47
|
+
if (isOperandNode(term)) {
|
|
48
|
+
visitOperand(term as OperandNode);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if ('type' in term) {
|
|
52
|
+
visitExpression(term as ExpressionNode);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const visitOrderBy = (orderBy: OrderByNode[] | undefined): void => {
|
|
57
|
+
if (!orderBy) return;
|
|
58
|
+
orderBy.forEach(node => visitOrderingTerm(node.term));
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const visitOperand = (node: OperandNode): void => {
|
|
62
|
+
switch (node.type) {
|
|
63
|
+
case 'Column':
|
|
64
|
+
recordColumn(node as ColumnNode);
|
|
65
|
+
return;
|
|
66
|
+
case 'Function': {
|
|
67
|
+
const fn = node as FunctionNode;
|
|
68
|
+
fn.args?.forEach(visitOperand);
|
|
69
|
+
visitOrderBy(fn.orderBy);
|
|
70
|
+
if (fn.separator) visitOperand(fn.separator);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
case 'JsonPath': {
|
|
74
|
+
const jp = node as JsonPathNode;
|
|
75
|
+
recordColumn(jp.column);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
case 'ScalarSubquery':
|
|
79
|
+
return;
|
|
80
|
+
case 'CaseExpression': {
|
|
81
|
+
const cs = node as CaseExpressionNode;
|
|
82
|
+
cs.conditions.forEach(condition => {
|
|
83
|
+
visitExpression(condition.when);
|
|
84
|
+
visitOperand(condition.then);
|
|
85
|
+
});
|
|
86
|
+
if (cs.else) visitOperand(cs.else);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
case 'Cast': {
|
|
90
|
+
const cast = node as CastExpressionNode;
|
|
91
|
+
visitOperand(cast.expression);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
case 'WindowFunction': {
|
|
95
|
+
const windowFn = node as WindowFunctionNode;
|
|
96
|
+
windowFn.args?.forEach(visitOperand);
|
|
97
|
+
windowFn.partitionBy?.forEach(recordColumn);
|
|
98
|
+
visitOrderBy(windowFn.orderBy);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
case 'ArithmeticExpression': {
|
|
102
|
+
const arith = node as ArithmeticExpressionNode;
|
|
103
|
+
visitOperand(arith.left);
|
|
104
|
+
visitOperand(arith.right);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
case 'BitwiseExpression': {
|
|
108
|
+
const bitwise = node as BitwiseExpressionNode;
|
|
109
|
+
visitOperand(bitwise.left);
|
|
110
|
+
visitOperand(bitwise.right);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
case 'Collate': {
|
|
114
|
+
const collate = node as CollateExpressionNode;
|
|
115
|
+
visitOperand(collate.expression);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
case 'AliasRef':
|
|
119
|
+
case 'Literal':
|
|
120
|
+
case 'Param':
|
|
121
|
+
return;
|
|
122
|
+
default:
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const visitExpression = (node: ExpressionNode): void => {
|
|
128
|
+
switch (node.type) {
|
|
129
|
+
case 'BinaryExpression':
|
|
130
|
+
visitOperand(node.left);
|
|
131
|
+
visitOperand(node.right);
|
|
132
|
+
if (node.escape) visitOperand(node.escape);
|
|
133
|
+
return;
|
|
134
|
+
case 'LogicalExpression':
|
|
135
|
+
node.operands.forEach(visitExpression);
|
|
136
|
+
return;
|
|
137
|
+
case 'NullExpression':
|
|
138
|
+
visitOperand(node.left);
|
|
139
|
+
return;
|
|
140
|
+
case 'InExpression':
|
|
141
|
+
visitOperand(node.left);
|
|
142
|
+
if (Array.isArray(node.right)) {
|
|
143
|
+
node.right.forEach(visitOperand);
|
|
144
|
+
}
|
|
145
|
+
return;
|
|
146
|
+
case 'ExistsExpression':
|
|
147
|
+
return;
|
|
148
|
+
case 'BetweenExpression':
|
|
149
|
+
visitOperand(node.left);
|
|
150
|
+
visitOperand(node.lower);
|
|
151
|
+
visitOperand(node.upper);
|
|
152
|
+
return;
|
|
153
|
+
case 'ArithmeticExpression':
|
|
154
|
+
visitOperand(node.left);
|
|
155
|
+
visitOperand(node.right);
|
|
156
|
+
return;
|
|
157
|
+
case 'BitwiseExpression':
|
|
158
|
+
visitOperand(node.left);
|
|
159
|
+
visitOperand(node.right);
|
|
160
|
+
return;
|
|
161
|
+
default:
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
visitExpression(expr);
|
|
167
|
+
return columns;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export const buildFilterParameters = (
|
|
171
|
+
table: TableDef,
|
|
172
|
+
where: ExpressionNode | undefined,
|
|
173
|
+
from: TableSourceNode | undefined,
|
|
174
|
+
options: ColumnSchemaOptions = {}
|
|
175
|
+
): OpenApiParameter[] => {
|
|
176
|
+
if (!where) return [];
|
|
177
|
+
|
|
178
|
+
const rootTables = buildRootTableNames(table, from);
|
|
179
|
+
const columnNames = collectFilterColumns(where, table, rootTables);
|
|
180
|
+
|
|
181
|
+
let schema: JsonSchemaProperty;
|
|
182
|
+
if (columnNames.size) {
|
|
183
|
+
const properties: Record<string, JsonSchemaProperty> = {};
|
|
184
|
+
for (const name of columnNames) {
|
|
185
|
+
const column = table.columns[name];
|
|
186
|
+
if (!column) continue;
|
|
187
|
+
properties[name] = mapColumnType(column, options);
|
|
188
|
+
}
|
|
189
|
+
schema = {
|
|
190
|
+
type: 'object',
|
|
191
|
+
properties
|
|
192
|
+
};
|
|
193
|
+
} else {
|
|
194
|
+
schema = {
|
|
195
|
+
type: 'object',
|
|
196
|
+
additionalProperties: true
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return [{
|
|
201
|
+
name: FILTER_PARAM_NAME,
|
|
202
|
+
in: 'query',
|
|
203
|
+
style: 'deepObject',
|
|
204
|
+
explode: true,
|
|
205
|
+
schema
|
|
206
|
+
}];
|
|
207
|
+
};
|