@prisma-next/sql-contract-psl 0.0.1 → 0.3.0-dev.114
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 +34 -8
- package/dist/default-function-registry-DUMRIhJH.d.mts +71 -0
- package/dist/default-function-registry-DUMRIhJH.d.mts.map +1 -0
- package/dist/index.d.mts +11 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/interpreter-D7gLmaHz.mjs +1412 -0
- package/dist/interpreter-D7gLmaHz.mjs.map +1 -0
- package/dist/provider.d.mts +10 -11
- package/dist/provider.d.mts.map +1 -1
- package/dist/provider.mjs +18 -12
- package/dist/provider.mjs.map +1 -1
- package/package.json +23 -23
- package/src/default-function-registry.ts +262 -0
- package/src/exports/index.ts +8 -0
- package/src/interpreter.ts +1055 -102
- package/src/provider.ts +34 -21
- package/dist/interpreter-_6-Xk1_m.mjs +0 -661
- package/dist/interpreter-_6-Xk1_m.mjs.map +0 -1
|
@@ -0,0 +1,262 @@
|
|
|
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
|
+
readonly columnCodecId?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type LoweredDefaultValue =
|
|
25
|
+
| { readonly kind: 'storage'; readonly defaultValue: ColumnDefault }
|
|
26
|
+
| { readonly kind: 'execution'; readonly generated: ExecutionMutationDefaultValue };
|
|
27
|
+
|
|
28
|
+
export type LoweredDefaultResult =
|
|
29
|
+
| { readonly ok: true; readonly value: LoweredDefaultValue }
|
|
30
|
+
| { readonly ok: false; readonly diagnostic: ContractSourceDiagnostic };
|
|
31
|
+
|
|
32
|
+
export type DefaultFunctionLoweringHandler = (input: {
|
|
33
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
34
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
35
|
+
}) => LoweredDefaultResult;
|
|
36
|
+
|
|
37
|
+
export interface DefaultFunctionRegistryEntry {
|
|
38
|
+
readonly lower: DefaultFunctionLoweringHandler;
|
|
39
|
+
readonly usageSignatures?: readonly string[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type DefaultFunctionRegistry = ReadonlyMap<string, DefaultFunctionRegistryEntry>;
|
|
43
|
+
|
|
44
|
+
export interface MutationDefaultGeneratorDescriptor {
|
|
45
|
+
readonly id: string;
|
|
46
|
+
readonly applicableCodecIds: readonly string[];
|
|
47
|
+
readonly resolveGeneratedColumnDescriptor?: (input: {
|
|
48
|
+
readonly generated: ExecutionMutationDefaultValue;
|
|
49
|
+
}) =>
|
|
50
|
+
| {
|
|
51
|
+
readonly codecId: string;
|
|
52
|
+
readonly nativeType: string;
|
|
53
|
+
readonly typeRef?: string;
|
|
54
|
+
readonly typeParams?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
| undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ControlMutationDefaultEntry {
|
|
60
|
+
readonly lower: (input: {
|
|
61
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
62
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
63
|
+
}) => LoweredDefaultResult;
|
|
64
|
+
readonly usageSignatures?: readonly string[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type ControlMutationDefaultRegistry = ReadonlyMap<string, ControlMutationDefaultEntry>;
|
|
68
|
+
|
|
69
|
+
export interface ControlMutationDefaults {
|
|
70
|
+
readonly defaultFunctionRegistry: ControlMutationDefaultRegistry;
|
|
71
|
+
readonly generatorDescriptors: readonly MutationDefaultGeneratorDescriptor[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function resolveSpanPositionFromBase(
|
|
75
|
+
base: PslSpan,
|
|
76
|
+
text: string,
|
|
77
|
+
offset: number,
|
|
78
|
+
): PslSpan['start'] {
|
|
79
|
+
const safeOffset = Math.min(Math.max(0, offset), text.length);
|
|
80
|
+
let line = base.start.line;
|
|
81
|
+
let column = base.start.column;
|
|
82
|
+
|
|
83
|
+
for (let index = 0; index < safeOffset; index += 1) {
|
|
84
|
+
const character = text[index] ?? '';
|
|
85
|
+
if (character === '\r') {
|
|
86
|
+
if (text[index + 1] === '\n' && index + 1 < safeOffset) {
|
|
87
|
+
index += 1;
|
|
88
|
+
}
|
|
89
|
+
line += 1;
|
|
90
|
+
column = 1;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (character === '\n') {
|
|
94
|
+
line += 1;
|
|
95
|
+
column = 1;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
column += 1;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
offset: base.start.offset + safeOffset,
|
|
103
|
+
line,
|
|
104
|
+
column,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function createSpanFromBase(
|
|
109
|
+
base: PslSpan,
|
|
110
|
+
startOffset: number,
|
|
111
|
+
endOffset: number,
|
|
112
|
+
text: string,
|
|
113
|
+
): PslSpan {
|
|
114
|
+
const safeStart = Math.max(0, Math.min(startOffset, text.length));
|
|
115
|
+
const safeEnd = Math.max(safeStart, Math.min(endOffset, text.length));
|
|
116
|
+
return {
|
|
117
|
+
start: resolveSpanPositionFromBase(base, text, safeStart),
|
|
118
|
+
end: resolveSpanPositionFromBase(base, text, safeEnd),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function splitTopLevelArgs(raw: string): Array<{ raw: string; start: number; end: number }> {
|
|
123
|
+
if (raw.trim().length === 0) {
|
|
124
|
+
return [];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const parts: Array<{ raw: string; start: number; end: number }> = [];
|
|
128
|
+
let depthParen = 0;
|
|
129
|
+
let depthBracket = 0;
|
|
130
|
+
let quote: '"' | "'" | null = null;
|
|
131
|
+
let start = 0;
|
|
132
|
+
|
|
133
|
+
for (let index = 0; index < raw.length; index += 1) {
|
|
134
|
+
const character = raw[index] ?? '';
|
|
135
|
+
if (quote) {
|
|
136
|
+
if (character === quote && raw[index - 1] !== '\\') {
|
|
137
|
+
quote = null;
|
|
138
|
+
}
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (character === '"' || character === "'") {
|
|
143
|
+
quote = character;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (character === '(') {
|
|
148
|
+
depthParen += 1;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (character === ')') {
|
|
152
|
+
depthParen = Math.max(0, depthParen - 1);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (character === '[') {
|
|
156
|
+
depthBracket += 1;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (character === ']') {
|
|
160
|
+
depthBracket = Math.max(0, depthBracket - 1);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (character === ',' && depthParen === 0 && depthBracket === 0) {
|
|
165
|
+
parts.push({
|
|
166
|
+
raw: raw.slice(start, index),
|
|
167
|
+
start,
|
|
168
|
+
end: index,
|
|
169
|
+
});
|
|
170
|
+
start = index + 1;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
parts.push({
|
|
175
|
+
raw: raw.slice(start),
|
|
176
|
+
start,
|
|
177
|
+
end: raw.length,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
return parts;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function parseDefaultFunctionCall(
|
|
184
|
+
expression: string,
|
|
185
|
+
expressionSpan: PslSpan,
|
|
186
|
+
): ParsedDefaultFunctionCall | undefined {
|
|
187
|
+
const trimmed = expression.trim();
|
|
188
|
+
const leadingWhitespace = expression.length - expression.trimStart().length;
|
|
189
|
+
const trailingWhitespace = expression.length - expression.trimEnd().length;
|
|
190
|
+
const contentEnd = expression.length - trailingWhitespace;
|
|
191
|
+
|
|
192
|
+
const openParen = trimmed.indexOf('(');
|
|
193
|
+
const closeParen = trimmed.lastIndexOf(')');
|
|
194
|
+
if (openParen <= 0 || closeParen !== trimmed.length - 1) {
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const functionName = trimmed.slice(0, openParen).trim();
|
|
199
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(functionName)) {
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const functionArgsRaw = trimmed.slice(openParen + 1, closeParen);
|
|
204
|
+
const parts = splitTopLevelArgs(functionArgsRaw);
|
|
205
|
+
const args: DefaultFunctionArgument[] = [];
|
|
206
|
+
for (const part of parts) {
|
|
207
|
+
const raw = part.raw.trim();
|
|
208
|
+
if (raw.length === 0) {
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
const leadingPartWhitespace = part.raw.length - part.raw.trimStart().length;
|
|
212
|
+
const argStart = leadingWhitespace + openParen + 1 + part.start + leadingPartWhitespace;
|
|
213
|
+
const argEnd = argStart + raw.length;
|
|
214
|
+
args.push({
|
|
215
|
+
raw,
|
|
216
|
+
span: createSpanFromBase(expressionSpan, argStart, argEnd, expression),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const functionStart = leadingWhitespace;
|
|
221
|
+
const functionEnd = contentEnd;
|
|
222
|
+
return {
|
|
223
|
+
name: functionName,
|
|
224
|
+
raw: trimmed,
|
|
225
|
+
args,
|
|
226
|
+
span: createSpanFromBase(expressionSpan, functionStart, functionEnd, expression),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function formatSupportedFunctionList(registry: ControlMutationDefaultRegistry): string {
|
|
231
|
+
const signatures = Array.from(registry.entries())
|
|
232
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
233
|
+
.flatMap(([functionName, entry]) => {
|
|
234
|
+
const usageSignatures = entry.usageSignatures?.filter((signature) => signature.length > 0);
|
|
235
|
+
return usageSignatures && usageSignatures.length > 0
|
|
236
|
+
? usageSignatures
|
|
237
|
+
: [`${functionName}()`];
|
|
238
|
+
});
|
|
239
|
+
return signatures.length > 0 ? signatures.join(', ') : 'none';
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function lowerDefaultFunctionWithRegistry(input: {
|
|
243
|
+
readonly call: ParsedDefaultFunctionCall;
|
|
244
|
+
readonly registry: ControlMutationDefaultRegistry;
|
|
245
|
+
readonly context: DefaultFunctionLoweringContext;
|
|
246
|
+
}): LoweredDefaultResult {
|
|
247
|
+
const entry = input.registry.get(input.call.name);
|
|
248
|
+
if (entry) {
|
|
249
|
+
return entry.lower({ call: input.call, context: input.context });
|
|
250
|
+
}
|
|
251
|
+
const supportedFunctionList = formatSupportedFunctionList(input.registry);
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
ok: false,
|
|
255
|
+
diagnostic: {
|
|
256
|
+
code: 'PSL_UNKNOWN_DEFAULT_FUNCTION',
|
|
257
|
+
message: `Default function "${input.call.name}" is not supported in SQL PSL provider v1. Supported functions: ${supportedFunctionList}.`,
|
|
258
|
+
sourceId: input.context.sourceId,
|
|
259
|
+
span: input.call.span,
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
}
|
package/src/exports/index.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ControlMutationDefaults,
|
|
3
|
+
DefaultFunctionLoweringContext,
|
|
4
|
+
DefaultFunctionLoweringHandler,
|
|
5
|
+
DefaultFunctionRegistry,
|
|
6
|
+
DefaultFunctionRegistryEntry,
|
|
7
|
+
MutationDefaultGeneratorDescriptor,
|
|
8
|
+
} from '../default-function-registry';
|
|
1
9
|
export {
|
|
2
10
|
type InterpretPslDocumentToSqlContractIRInput,
|
|
3
11
|
interpretPslDocumentToSqlContractIR,
|