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.
@@ -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 sanitizeSQL(sql: string): string {
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
- while (i < sql.length) {
70
- const ch = sql[i];
71
- const next = sql[i + 1];
70
+ try {
71
+ while (i < sql.length) {
72
+ const ch = sql[i];
73
+ const next = sql[i + 1];
72
74
 
73
- if (ch === '-' && next === '-') {
74
- i += 2;
75
- while (i < sql.length && sql[i] !== '\n') i += 1;
76
- out.push(' ');
77
- continue;
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
- if (ch === '/' && next === '*') {
81
- let depth = 1;
82
- i += 2;
83
- while (i < sql.length && depth > 0) {
84
- if (sql[i] === '/' && sql[i + 1] === '*') {
85
- depth += 1;
86
- i += 2;
87
- } else if (sql[i] === '*' && sql[i + 1] === '/') {
88
- depth -= 1;
89
- i += 2;
90
- } else {
91
- i += 1;
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
- if ((ch === 'u' || ch === 'U') && next === '&' && (sql[i + 2] === "'" || sql[i + 2] === '"')) {
100
- throw new PostgresValidationError('Invalid or unsupported SQL syntax');
101
- }
101
+ if (/\s/.test(ch)) {
102
+ out.push(ch);
103
+ i += 1;
104
+ continue;
105
+ }
102
106
 
103
- if ((ch === 'e' || ch === 'E') && next === "'" && (i === 0 || !/[A-Za-z0-9\x80-\uffff_$]/.test(sql[i - 1]))) {
104
- i = consumeQuotedLiteral(sql, i + 2, true);
105
- out.push("''");
106
- continue;
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
- if (/[A-Za-z\x80-\uffff_]/.test(ch)) {
110
- const start = i;
111
- i += 1;
112
- while (i < sql.length && /[A-Za-z0-9\x80-\uffff_$]/.test(sql[i])) i += 1;
113
- out.push(sql.slice(start, i));
114
- continue;
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
- if (ch === "'") {
118
- i = consumeQuotedLiteral(sql, i + 1, false);
119
- out.push("''");
120
- continue;
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
- if (ch === '"') {
124
- const parsed = consumeDoubleQuotedIdentifier(sql, i);
125
- out.push(parsed.sanitized);
126
- i = parsed.nextIndex;
127
- continue;
128
- }
131
+ if (ch === "'") {
132
+ i = consumeQuotedLiteral(sql, i + 1, false);
133
+ out.push("''");
134
+ nonTrivia.push('code');
135
+ continue;
136
+ }
129
137
 
130
- if (ch === '$') {
131
- const tail = sql.slice(i);
132
- const paramMatch = tail.match(/^\$[0-9]+/);
133
- if (paramMatch) {
134
- out.push(paramMatch[0]);
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
- const tagMatch = tail.match(/^\$(?:[A-Za-z\x80-\uffff_][A-Za-z0-9\x80-\uffff_]*)?\$/);
139
- if (tagMatch) {
140
- const tag = tagMatch[0];
141
- const end = sql.indexOf(tag, i + tag.length);
142
- if (end === -1) throw new PostgresValidationError('Invalid or unsupported SQL syntax');
143
- i = end + tag.length;
144
- out.push("''");
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
- out.push(ch);
150
- i += 1;
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
- return out.join('').trim();
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
  /**