@prisma-next/sql-contract-psl 0.3.0-dev.54 → 0.3.0-dev.63
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/LICENSE +201 -0
- package/README.md +24 -0
- package/dist/index.d.mts +40 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/interpreter-IXr5c7s7.mjs +1376 -0
- package/dist/interpreter-IXr5c7s7.mjs.map +1 -0
- package/dist/provider.d.mts +1 -1
- package/dist/provider.d.mts.map +1 -1
- package/dist/provider.mjs +4 -3
- package/dist/provider.mjs.map +1 -1
- package/package.json +8 -8
- package/src/default-function-registry.ts +510 -0
- package/src/interpreter.ts +629 -67
- package/src/provider.ts +3 -1
- package/dist/interpreter-_6-Xk1_m.mjs +0 -661
- package/dist/interpreter-_6-Xk1_m.mjs.map +0 -1
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
import type { ContractSourceDiagnostic } from '@prisma-next/config/config-types';
|
|
2
|
+
import type { ColumnDefault, ExecutionMutationDefaultValue } from '@prisma-next/contract/types';
|
|
3
|
+
import type { PslSpan } from '@prisma-next/psl-parser';
|
|
4
|
+
|
|
5
|
+
interface DefaultFunctionArgument {
|
|
6
|
+
readonly raw: string;
|
|
7
|
+
readonly span: PslSpan;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ParsedDefaultFunctionCall {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly raw: string;
|
|
13
|
+
readonly args: readonly DefaultFunctionArgument[];
|
|
14
|
+
readonly span: PslSpan;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface DefaultFunctionLoweringContext {
|
|
18
|
+
readonly sourceId: string;
|
|
19
|
+
readonly modelName: string;
|
|
20
|
+
readonly fieldName: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type LoweredDefaultValue =
|
|
24
|
+
| { readonly kind: 'storage'; readonly defaultValue: ColumnDefault }
|
|
25
|
+
| { readonly kind: 'execution'; readonly generated: ExecutionMutationDefaultValue };
|
|
26
|
+
|
|
27
|
+
type LoweredDefaultResult =
|
|
28
|
+
| { readonly ok: true; readonly value: LoweredDefaultValue }
|
|
29
|
+
| { readonly ok: false; readonly diagnostic: ContractSourceDiagnostic };
|
|
30
|
+
|
|
31
|
+
export type DefaultFunctionLoweringHandler = (input: {
|
|
32
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
33
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
34
|
+
}) => LoweredDefaultResult;
|
|
35
|
+
|
|
36
|
+
export type DefaultFunctionRegistry = ReadonlyMap<string, DefaultFunctionLoweringHandler>;
|
|
37
|
+
|
|
38
|
+
function resolveSpanPositionFromBase(
|
|
39
|
+
base: PslSpan,
|
|
40
|
+
text: string,
|
|
41
|
+
offset: number,
|
|
42
|
+
): PslSpan['start'] {
|
|
43
|
+
const safeOffset = Math.min(Math.max(0, offset), text.length);
|
|
44
|
+
let line = base.start.line;
|
|
45
|
+
let column = base.start.column;
|
|
46
|
+
|
|
47
|
+
for (let index = 0; index < safeOffset; index += 1) {
|
|
48
|
+
const character = text[index] ?? '';
|
|
49
|
+
if (character === '\r') {
|
|
50
|
+
if (text[index + 1] === '\n' && index + 1 < safeOffset) {
|
|
51
|
+
index += 1;
|
|
52
|
+
}
|
|
53
|
+
line += 1;
|
|
54
|
+
column = 1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (character === '\n') {
|
|
58
|
+
line += 1;
|
|
59
|
+
column = 1;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
column += 1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
offset: base.start.offset + safeOffset,
|
|
67
|
+
line,
|
|
68
|
+
column,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function createSpanFromBase(
|
|
73
|
+
base: PslSpan,
|
|
74
|
+
startOffset: number,
|
|
75
|
+
endOffset: number,
|
|
76
|
+
text: string,
|
|
77
|
+
): PslSpan {
|
|
78
|
+
const safeStart = Math.max(0, Math.min(startOffset, text.length));
|
|
79
|
+
const safeEnd = Math.max(safeStart, Math.min(endOffset, text.length));
|
|
80
|
+
return {
|
|
81
|
+
start: resolveSpanPositionFromBase(base, text, safeStart),
|
|
82
|
+
end: resolveSpanPositionFromBase(base, text, safeEnd),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function splitTopLevelArgs(raw: string): Array<{ raw: string; start: number; end: number }> {
|
|
87
|
+
if (raw.trim().length === 0) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const parts: Array<{ raw: string; start: number; end: number }> = [];
|
|
92
|
+
let depthParen = 0;
|
|
93
|
+
let depthBracket = 0;
|
|
94
|
+
let quote: '"' | "'" | null = null;
|
|
95
|
+
let start = 0;
|
|
96
|
+
|
|
97
|
+
for (let index = 0; index < raw.length; index += 1) {
|
|
98
|
+
const character = raw[index] ?? '';
|
|
99
|
+
if (quote) {
|
|
100
|
+
if (character === quote && raw[index - 1] !== '\\') {
|
|
101
|
+
quote = null;
|
|
102
|
+
}
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (character === '"' || character === "'") {
|
|
107
|
+
quote = character;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (character === '(') {
|
|
112
|
+
depthParen += 1;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (character === ')') {
|
|
116
|
+
depthParen = Math.max(0, depthParen - 1);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (character === '[') {
|
|
120
|
+
depthBracket += 1;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (character === ']') {
|
|
124
|
+
depthBracket = Math.max(0, depthBracket - 1);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (character === ',' && depthParen === 0 && depthBracket === 0) {
|
|
129
|
+
parts.push({
|
|
130
|
+
raw: raw.slice(start, index),
|
|
131
|
+
start,
|
|
132
|
+
end: index,
|
|
133
|
+
});
|
|
134
|
+
start = index + 1;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
parts.push({
|
|
139
|
+
raw: raw.slice(start),
|
|
140
|
+
start,
|
|
141
|
+
end: raw.length,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
return parts;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function parseDefaultFunctionCall(
|
|
148
|
+
expression: string,
|
|
149
|
+
expressionSpan: PslSpan,
|
|
150
|
+
): ParsedDefaultFunctionCall | undefined {
|
|
151
|
+
const trimmed = expression.trim();
|
|
152
|
+
const leadingWhitespace = expression.length - expression.trimStart().length;
|
|
153
|
+
const trailingWhitespace = expression.length - expression.trimEnd().length;
|
|
154
|
+
const contentEnd = expression.length - trailingWhitespace;
|
|
155
|
+
|
|
156
|
+
const openParen = trimmed.indexOf('(');
|
|
157
|
+
const closeParen = trimmed.lastIndexOf(')');
|
|
158
|
+
if (openParen <= 0 || closeParen !== trimmed.length - 1) {
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const functionName = trimmed.slice(0, openParen).trim();
|
|
163
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(functionName)) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const functionArgsRaw = trimmed.slice(openParen + 1, closeParen);
|
|
168
|
+
const parts = splitTopLevelArgs(functionArgsRaw);
|
|
169
|
+
const args: DefaultFunctionArgument[] = [];
|
|
170
|
+
for (const part of parts) {
|
|
171
|
+
const raw = part.raw.trim();
|
|
172
|
+
if (raw.length === 0) {
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
const leadingPartWhitespace = part.raw.length - part.raw.trimStart().length;
|
|
176
|
+
const argStart = leadingWhitespace + openParen + 1 + part.start + leadingPartWhitespace;
|
|
177
|
+
const argEnd = argStart + raw.length;
|
|
178
|
+
args.push({
|
|
179
|
+
raw,
|
|
180
|
+
span: createSpanFromBase(expressionSpan, argStart, argEnd, expression),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const functionStart = leadingWhitespace;
|
|
185
|
+
const functionEnd = contentEnd;
|
|
186
|
+
return {
|
|
187
|
+
name: functionName,
|
|
188
|
+
raw: trimmed,
|
|
189
|
+
args,
|
|
190
|
+
span: createSpanFromBase(expressionSpan, functionStart, functionEnd, expression),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function invalidArgumentDiagnostic(input: {
|
|
195
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
196
|
+
readonly span: PslSpan;
|
|
197
|
+
readonly message: string;
|
|
198
|
+
}): LoweredDefaultResult {
|
|
199
|
+
return {
|
|
200
|
+
ok: false,
|
|
201
|
+
diagnostic: {
|
|
202
|
+
code: 'PSL_INVALID_DEFAULT_FUNCTION_ARGUMENT',
|
|
203
|
+
message: input.message,
|
|
204
|
+
sourceId: input.context.sourceId,
|
|
205
|
+
span: input.span,
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function executionGenerator(
|
|
211
|
+
id: ExecutionMutationDefaultValue['id'],
|
|
212
|
+
params?: Record<string, unknown>,
|
|
213
|
+
): LoweredDefaultResult {
|
|
214
|
+
return {
|
|
215
|
+
ok: true,
|
|
216
|
+
value: {
|
|
217
|
+
kind: 'execution',
|
|
218
|
+
generated: {
|
|
219
|
+
kind: 'generator',
|
|
220
|
+
id,
|
|
221
|
+
...(params ? { params } : {}),
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function expectNoArgs(input: {
|
|
228
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
229
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
230
|
+
readonly usage: string;
|
|
231
|
+
}): LoweredDefaultResult | undefined {
|
|
232
|
+
if (input.call.args.length === 0) {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
return invalidArgumentDiagnostic({
|
|
236
|
+
context: input.context,
|
|
237
|
+
span: input.call.span,
|
|
238
|
+
message: `Default function "${input.call.name}" does not accept arguments. Use ${input.usage}.`,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function parseIntegerArgument(raw: string): number | undefined {
|
|
243
|
+
const trimmed = raw.trim();
|
|
244
|
+
if (!/^-?\d+$/.test(trimmed)) {
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
const value = Number(trimmed);
|
|
248
|
+
if (!Number.isInteger(value)) {
|
|
249
|
+
return undefined;
|
|
250
|
+
}
|
|
251
|
+
return value;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function parseStringLiteral(raw: string): string | undefined {
|
|
255
|
+
const match = raw.trim().match(/^(['"])(.*)\1$/s);
|
|
256
|
+
if (!match) {
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
return match[2] ?? '';
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function lowerAutoincrement(input: {
|
|
263
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
264
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
265
|
+
}): LoweredDefaultResult {
|
|
266
|
+
const maybeNoArgs = expectNoArgs({
|
|
267
|
+
call: input.call,
|
|
268
|
+
context: input.context,
|
|
269
|
+
usage: '`autoincrement()`',
|
|
270
|
+
});
|
|
271
|
+
if (maybeNoArgs) {
|
|
272
|
+
return maybeNoArgs;
|
|
273
|
+
}
|
|
274
|
+
return {
|
|
275
|
+
ok: true,
|
|
276
|
+
value: {
|
|
277
|
+
kind: 'storage',
|
|
278
|
+
defaultValue: {
|
|
279
|
+
kind: 'function',
|
|
280
|
+
expression: 'autoincrement()',
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function lowerNow(input: {
|
|
287
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
288
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
289
|
+
}): LoweredDefaultResult {
|
|
290
|
+
const maybeNoArgs = expectNoArgs({
|
|
291
|
+
call: input.call,
|
|
292
|
+
context: input.context,
|
|
293
|
+
usage: '`now()`',
|
|
294
|
+
});
|
|
295
|
+
if (maybeNoArgs) {
|
|
296
|
+
return maybeNoArgs;
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
ok: true,
|
|
300
|
+
value: {
|
|
301
|
+
kind: 'storage',
|
|
302
|
+
defaultValue: {
|
|
303
|
+
kind: 'function',
|
|
304
|
+
expression: 'now()',
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function lowerUuid(input: {
|
|
311
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
312
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
313
|
+
}): LoweredDefaultResult {
|
|
314
|
+
if (input.call.args.length === 0) {
|
|
315
|
+
return executionGenerator('uuidv4');
|
|
316
|
+
}
|
|
317
|
+
if (input.call.args.length !== 1) {
|
|
318
|
+
return invalidArgumentDiagnostic({
|
|
319
|
+
context: input.context,
|
|
320
|
+
span: input.call.span,
|
|
321
|
+
message:
|
|
322
|
+
'Default function "uuid" accepts at most one version argument: `uuid()`, `uuid(4)`, or `uuid(7)`.',
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
const version = parseIntegerArgument(input.call.args[0]?.raw ?? '');
|
|
326
|
+
if (version === 4) {
|
|
327
|
+
return executionGenerator('uuidv4');
|
|
328
|
+
}
|
|
329
|
+
if (version === 7) {
|
|
330
|
+
return executionGenerator('uuidv7');
|
|
331
|
+
}
|
|
332
|
+
return invalidArgumentDiagnostic({
|
|
333
|
+
context: input.context,
|
|
334
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
335
|
+
message:
|
|
336
|
+
'Default function "uuid" supports only `uuid()`, `uuid(4)`, or `uuid(7)` in SQL PSL provider v1.',
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function lowerCuid(input: {
|
|
341
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
342
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
343
|
+
}): LoweredDefaultResult {
|
|
344
|
+
if (input.call.args.length === 0) {
|
|
345
|
+
return {
|
|
346
|
+
ok: false,
|
|
347
|
+
diagnostic: {
|
|
348
|
+
code: 'PSL_UNKNOWN_DEFAULT_FUNCTION',
|
|
349
|
+
message:
|
|
350
|
+
'Default function "cuid()" is not supported in SQL PSL provider v1. Use `cuid(2)` instead.',
|
|
351
|
+
sourceId: input.context.sourceId,
|
|
352
|
+
span: input.call.span,
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
if (input.call.args.length !== 1) {
|
|
357
|
+
return invalidArgumentDiagnostic({
|
|
358
|
+
context: input.context,
|
|
359
|
+
span: input.call.span,
|
|
360
|
+
message: 'Default function "cuid" accepts exactly one version argument: `cuid(2)`.',
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
const version = parseIntegerArgument(input.call.args[0]?.raw ?? '');
|
|
364
|
+
if (version === 2) {
|
|
365
|
+
return executionGenerator('cuid2');
|
|
366
|
+
}
|
|
367
|
+
return invalidArgumentDiagnostic({
|
|
368
|
+
context: input.context,
|
|
369
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
370
|
+
message: 'Default function "cuid" supports only `cuid(2)` in SQL PSL provider v1.',
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function lowerUlid(input: {
|
|
375
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
376
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
377
|
+
}): LoweredDefaultResult {
|
|
378
|
+
const maybeNoArgs = expectNoArgs({
|
|
379
|
+
call: input.call,
|
|
380
|
+
context: input.context,
|
|
381
|
+
usage: '`ulid()`',
|
|
382
|
+
});
|
|
383
|
+
if (maybeNoArgs) {
|
|
384
|
+
return maybeNoArgs;
|
|
385
|
+
}
|
|
386
|
+
return executionGenerator('ulid');
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function lowerNanoid(input: {
|
|
390
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
391
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
392
|
+
}): LoweredDefaultResult {
|
|
393
|
+
if (input.call.args.length === 0) {
|
|
394
|
+
return executionGenerator('nanoid');
|
|
395
|
+
}
|
|
396
|
+
if (input.call.args.length !== 1) {
|
|
397
|
+
return invalidArgumentDiagnostic({
|
|
398
|
+
context: input.context,
|
|
399
|
+
span: input.call.span,
|
|
400
|
+
message:
|
|
401
|
+
'Default function "nanoid" accepts at most one size argument: `nanoid()` or `nanoid(<2-255>)`.',
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
const size = parseIntegerArgument(input.call.args[0]?.raw ?? '');
|
|
405
|
+
if (size !== undefined && size >= 2 && size <= 255) {
|
|
406
|
+
return executionGenerator('nanoid', { size });
|
|
407
|
+
}
|
|
408
|
+
return invalidArgumentDiagnostic({
|
|
409
|
+
context: input.context,
|
|
410
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
411
|
+
message: 'Default function "nanoid" size argument must be an integer between 2 and 255.',
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function lowerDbgenerated(input: {
|
|
416
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
417
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
418
|
+
}): LoweredDefaultResult {
|
|
419
|
+
if (input.call.args.length !== 1) {
|
|
420
|
+
return invalidArgumentDiagnostic({
|
|
421
|
+
context: input.context,
|
|
422
|
+
span: input.call.span,
|
|
423
|
+
message:
|
|
424
|
+
'Default function "dbgenerated" requires exactly one string argument: `dbgenerated("...")`.',
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
const rawExpression = parseStringLiteral(input.call.args[0]?.raw ?? '');
|
|
428
|
+
if (rawExpression === undefined) {
|
|
429
|
+
return invalidArgumentDiagnostic({
|
|
430
|
+
context: input.context,
|
|
431
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
432
|
+
message: 'Default function "dbgenerated" argument must be a string literal.',
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
if (rawExpression.trim().length === 0) {
|
|
436
|
+
return invalidArgumentDiagnostic({
|
|
437
|
+
context: input.context,
|
|
438
|
+
span: input.call.args[0]?.span ?? input.call.span,
|
|
439
|
+
message: 'Default function "dbgenerated" argument cannot be empty.',
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
ok: true,
|
|
444
|
+
value: {
|
|
445
|
+
kind: 'storage',
|
|
446
|
+
defaultValue: {
|
|
447
|
+
kind: 'function',
|
|
448
|
+
expression: rawExpression,
|
|
449
|
+
},
|
|
450
|
+
},
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const supportedFunctionUsageByName: Readonly<Record<string, readonly string[]>> = {
|
|
455
|
+
autoincrement: ['autoincrement()'],
|
|
456
|
+
now: ['now()'],
|
|
457
|
+
uuid: ['uuid()', 'uuid(4)', 'uuid(7)'],
|
|
458
|
+
cuid: ['cuid(2)'],
|
|
459
|
+
ulid: ['ulid()'],
|
|
460
|
+
nanoid: ['nanoid()', 'nanoid(n)'],
|
|
461
|
+
dbgenerated: ['dbgenerated("...")'],
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
const unknownFunctionSuggestionsByName: Readonly<Record<string, string>> = {
|
|
465
|
+
cuid2: 'Use `cuid(2)`.',
|
|
466
|
+
uuidv4: 'Use `uuid()` or `uuid(4)`.',
|
|
467
|
+
uuidv7: 'Use `uuid(7)`.',
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
function formatSupportedFunctionList(registry: DefaultFunctionRegistry): string {
|
|
471
|
+
const signatures = Array.from(registry.keys())
|
|
472
|
+
.sort()
|
|
473
|
+
.flatMap((functionName) => supportedFunctionUsageByName[functionName] ?? [`${functionName}()`]);
|
|
474
|
+
return signatures.length > 0 ? signatures.join(', ') : 'none';
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export function createBuiltinDefaultFunctionRegistry(): DefaultFunctionRegistry {
|
|
478
|
+
return new Map<string, DefaultFunctionLoweringHandler>([
|
|
479
|
+
['autoincrement', lowerAutoincrement],
|
|
480
|
+
['now', lowerNow],
|
|
481
|
+
['uuid', lowerUuid],
|
|
482
|
+
['cuid', lowerCuid],
|
|
483
|
+
['ulid', lowerUlid],
|
|
484
|
+
['nanoid', lowerNanoid],
|
|
485
|
+
['dbgenerated', lowerDbgenerated],
|
|
486
|
+
]);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
export function lowerDefaultFunctionWithRegistry(input: {
|
|
490
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
491
|
+
readonly registry: DefaultFunctionRegistry;
|
|
492
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
493
|
+
}): LoweredDefaultResult {
|
|
494
|
+
const handler = input.registry.get(input.call.name);
|
|
495
|
+
if (handler) {
|
|
496
|
+
return handler({ call: input.call, context: input.context });
|
|
497
|
+
}
|
|
498
|
+
const supportedFunctionList = formatSupportedFunctionList(input.registry);
|
|
499
|
+
const suggestion = unknownFunctionSuggestionsByName[input.call.name];
|
|
500
|
+
|
|
501
|
+
return {
|
|
502
|
+
ok: false,
|
|
503
|
+
diagnostic: {
|
|
504
|
+
code: 'PSL_UNKNOWN_DEFAULT_FUNCTION',
|
|
505
|
+
message: `Default function "${input.call.name}" is not supported in SQL PSL provider v1. Supported functions: ${supportedFunctionList}.${suggestion ? ` ${suggestion}` : ''}`,
|
|
506
|
+
sourceId: input.context.sourceId,
|
|
507
|
+
span: input.call.span,
|
|
508
|
+
},
|
|
509
|
+
};
|
|
510
|
+
}
|