plugin-data-source-postgres 2.0.0 → 2.0.1
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/README.md +1 -1
- package/dist/externalVersion.js +1 -0
- package/dist/server/collections/sql-collection.js +2 -2
- package/dist/server/collections/sql-model.js +4 -2
- package/dist/server/hooks/field-hooks.js +20 -0
- package/dist/server/plugin.js +603 -20
- package/dist/server/postgres-collection-manager.js +192 -25
- package/dist/server/postgres-datasource.js +47 -13
- package/dist/server/utils/sql-validator.js +119 -69
- package/package.json +2 -2
- package/src/server/__tests__/postgres-datasource.unit.test.ts +885 -0
- package/src/server/__tests__/sql-model.test.ts +47 -3
- package/src/server/__tests__/sql-validator.test.ts +8 -1
- package/src/server/collections/sql-collection.ts +2 -2
- package/src/server/collections/sql-model.ts +2 -2
- package/src/server/hooks/field-hooks.ts +36 -2
- package/src/server/plugin.ts +752 -17
- package/src/server/postgres-collection-manager.ts +228 -28
- package/src/server/postgres-datasource.ts +57 -12
- package/src/server/utils/sql-validator.ts +123 -68
|
@@ -62,95 +62,150 @@ function consumeDoubleQuotedIdentifier(sql: string, start: number): { nextIndex:
|
|
|
62
62
|
return { nextIndex: i, sanitized: `"${raw.replace(/[^A-Za-z0-9_]/g, ' ')}"` };
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
function
|
|
65
|
+
function scanSqlTokens(sql: string, strict: boolean): { sanitized: string; trailingSemicolonIndices: number[] } | null {
|
|
66
66
|
const out: string[] = [];
|
|
67
|
+
const nonTrivia: Array<'code' | number> = [];
|
|
67
68
|
let i = 0;
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
try {
|
|
71
|
+
while (i < sql.length) {
|
|
72
|
+
const ch = sql[i];
|
|
73
|
+
const next = sql[i + 1];
|
|
72
74
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
if (ch === '-' && next === '-') {
|
|
76
|
+
i += 2;
|
|
77
|
+
while (i < sql.length && sql[i] !== '\n') i += 1;
|
|
78
|
+
out.push(' ');
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
79
81
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
82
|
+
if (ch === '/' && next === '*') {
|
|
83
|
+
let depth = 1;
|
|
84
|
+
i += 2;
|
|
85
|
+
while (i < sql.length && depth > 0) {
|
|
86
|
+
if (sql[i] === '/' && sql[i + 1] === '*') {
|
|
87
|
+
depth += 1;
|
|
88
|
+
i += 2;
|
|
89
|
+
} else if (sql[i] === '*' && sql[i + 1] === '/') {
|
|
90
|
+
depth -= 1;
|
|
91
|
+
i += 2;
|
|
92
|
+
} else {
|
|
93
|
+
i += 1;
|
|
94
|
+
}
|
|
92
95
|
}
|
|
96
|
+
if (depth > 0) throw new PostgresValidationError('Invalid or unsupported SQL syntax');
|
|
97
|
+
out.push(' ');
|
|
98
|
+
continue;
|
|
93
99
|
}
|
|
94
|
-
if (depth > 0) throw new PostgresValidationError('Invalid or unsupported SQL syntax');
|
|
95
|
-
out.push(' ');
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
100
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
101
|
+
if (/\s/.test(ch)) {
|
|
102
|
+
out.push(ch);
|
|
103
|
+
i += 1;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
102
106
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
107
|
+
if ((ch === 'u' || ch === 'U') && next === '&' && (sql[i + 2] === "'" || sql[i + 2] === '"')) {
|
|
108
|
+
if (strict) throw new PostgresValidationError('Invalid or unsupported SQL syntax');
|
|
109
|
+
out.push(sql.slice(i, i + 2));
|
|
110
|
+
nonTrivia.push('code');
|
|
111
|
+
i += 2;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
108
114
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
115
|
+
if ((ch === 'e' || ch === 'E') && next === "'" && (i === 0 || !/[A-Za-z0-9\x80-\uffff_$]/.test(sql[i - 1]))) {
|
|
116
|
+
i = consumeQuotedLiteral(sql, i + 2, true);
|
|
117
|
+
out.push("''");
|
|
118
|
+
nonTrivia.push('code');
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
116
121
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
+
if (/[A-Za-z\x80-\uffff_]/.test(ch)) {
|
|
123
|
+
const start = i;
|
|
124
|
+
i += 1;
|
|
125
|
+
while (i < sql.length && /[A-Za-z0-9\x80-\uffff_$]/.test(sql[i])) i += 1;
|
|
126
|
+
out.push(sql.slice(start, i));
|
|
127
|
+
nonTrivia.push('code');
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
122
130
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
131
|
+
if (ch === "'") {
|
|
132
|
+
i = consumeQuotedLiteral(sql, i + 1, false);
|
|
133
|
+
out.push("''");
|
|
134
|
+
nonTrivia.push('code');
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
129
137
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
i += paramMatch[0].length;
|
|
138
|
+
if (ch === '"') {
|
|
139
|
+
const parsed = consumeDoubleQuotedIdentifier(sql, i);
|
|
140
|
+
out.push(parsed.sanitized);
|
|
141
|
+
nonTrivia.push('code');
|
|
142
|
+
i = parsed.nextIndex;
|
|
136
143
|
continue;
|
|
137
144
|
}
|
|
138
|
-
|
|
139
|
-
if (
|
|
140
|
-
const
|
|
141
|
-
const
|
|
142
|
-
if (
|
|
143
|
-
|
|
144
|
-
|
|
145
|
+
|
|
146
|
+
if (ch === '$') {
|
|
147
|
+
const tail = sql.slice(i);
|
|
148
|
+
const paramMatch = tail.match(/^\$[0-9]+/);
|
|
149
|
+
if (paramMatch) {
|
|
150
|
+
out.push(paramMatch[0]);
|
|
151
|
+
nonTrivia.push('code');
|
|
152
|
+
i += paramMatch[0].length;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const tagMatch = tail.match(/^\$(?:[A-Za-z\x80-\uffff_][A-Za-z0-9\x80-\uffff_]*)?\$/);
|
|
156
|
+
if (tagMatch) {
|
|
157
|
+
const tag = tagMatch[0];
|
|
158
|
+
const end = sql.indexOf(tag, i + tag.length);
|
|
159
|
+
if (end === -1) throw new PostgresValidationError('Invalid or unsupported SQL syntax');
|
|
160
|
+
i = end + tag.length;
|
|
161
|
+
out.push("''");
|
|
162
|
+
nonTrivia.push('code');
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (ch === ';') {
|
|
168
|
+
out.push(';');
|
|
169
|
+
nonTrivia.push(i);
|
|
170
|
+
i += 1;
|
|
145
171
|
continue;
|
|
146
172
|
}
|
|
173
|
+
|
|
174
|
+
out.push(ch);
|
|
175
|
+
nonTrivia.push('code');
|
|
176
|
+
i += 1;
|
|
147
177
|
}
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (strict) throw error;
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
148
182
|
|
|
149
|
-
|
|
150
|
-
|
|
183
|
+
const trailingSemicolonIndices: number[] = [];
|
|
184
|
+
for (let idx = nonTrivia.length - 1; idx >= 0; idx -= 1) {
|
|
185
|
+
const token = nonTrivia[idx];
|
|
186
|
+
if (typeof token !== 'number') break;
|
|
187
|
+
trailingSemicolonIndices.push(token);
|
|
151
188
|
}
|
|
189
|
+
return { sanitized: out.join('').trim(), trailingSemicolonIndices };
|
|
190
|
+
}
|
|
152
191
|
|
|
153
|
-
|
|
192
|
+
function sanitizeSQL(sql: string): string {
|
|
193
|
+
const scanned = scanSqlTokens(sql, true);
|
|
194
|
+
if (!scanned) throw new PostgresValidationError('Invalid or unsupported SQL syntax');
|
|
195
|
+
return scanned.sanitized;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Strip trailing statement semicolons after any comments/whitespace so subquery wrapping stays valid.
|
|
200
|
+
*/
|
|
201
|
+
export function normalizeSqlCollectionQuery(sql: string): string {
|
|
202
|
+
const scanned = scanSqlTokens(sql, false);
|
|
203
|
+
if (!scanned) return sql.trim();
|
|
204
|
+
let working = sql;
|
|
205
|
+
for (const semiIndex of scanned.trailingSemicolonIndices) {
|
|
206
|
+
working = working.slice(0, semiIndex) + working.slice(semiIndex + 1);
|
|
207
|
+
}
|
|
208
|
+
return working.trim();
|
|
154
209
|
}
|
|
155
210
|
|
|
156
211
|
/**
|