plugin-data-source-postgres 2.0.0 → 2.1.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/client/index.js +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/constants.js +3 -0
- package/dist/externalVersion.js +10 -8
- package/dist/locale/en-US.json +8 -1
- package/dist/locale/zh-CN.json +8 -1
- 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 +727 -23
- 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/client/ConfigurationForm.tsx +312 -133
- package/src/client/__tests__/client-v1.test.tsx +80 -0
- package/src/client-v2/PostgresConfigForm.tsx +133 -6
- package/src/client-v2/__tests__/plugin.test.tsx +69 -0
- package/src/client-v2/plugin.tsx +2 -2
- package/src/constants.ts +15 -0
- package/src/locale/en-US.json +8 -1
- package/src/locale/zh-CN.json +8 -1
- package/src/server/__tests__/postgres-datasource.unit.test.ts +1038 -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 +912 -18
- 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
- package/dist/client-v2/985.34da95acdf7743ab.js +0 -10
|
@@ -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
|
/**
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
"use strict";(self.webpackChunkplugin_data_source_postgres_client_v2=self.webpackChunkplugin_data_source_postgres_client_v2||[]).push([["985"],{184:function(e,t,r){r.r(t),r.d(t,{default:function(){return y}});var n=r(155),o=r.n(n),l=r(59),a=r(694),s=r(953),u=r(981),i=r(582);function c(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function p(e,t,r,n,o,l,a){try{var s=e[l](a),u=s.value}catch(e){r(e);return}s.done?t(u):Promise.resolve(u).then(n,o)}function m(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r,n,o=null==e?null:"u">typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=o){var l=[],a=!0,s=!1;try{for(o=o.call(e);!(a=(r=o.next()).done)&&(l.push(r.value),!t||l.length!==t);a=!0);}catch(e){s=!0,n=e}finally{try{a||null==o.return||o.return()}finally{if(s)throw n}}return l}}(e,t)||d(e,t)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function d(e,t){if(e){if("string"==typeof e)return c(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);if("Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r)return Array.from(r);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return c(e,t)}}var f=["host","port","database","schema","username","password","ssl","sslCa"];function b(e,t){var r=null==t?void 0:t[e];return"port"===e?Number(null!=r?r:u.Wg):"schema"===e?"string"==typeof r&&r?r:u.my:"ssl"===e?!!r:"sslCa"===e?null!=r?r:"":r}function y(e){var t,r,y,h,v=e.mode,g=e.initialValues,F=e.loadCollections,w=(0,s.useTranslation)(u.CU).t,E=(0,a.useFlowContext)(),S=l.Form.useFormInstance(),I=l.Form.useWatch(["options","addAllCollections"],S),O=l.Form.useWatch(["options","host"],S),P=l.Form.useWatch(["options","port"],S),C=l.Form.useWatch(["options","database"],S),j=l.Form.useWatch(["options","schema"],S),A=l.Form.useWatch(["options","username"],S),k=l.Form.useWatch(["options","password"],S),_=l.Form.useWatch(["options","ssl"],S),V=l.Form.useWatch(["options","sslCa"],S),x=m((0,n.useState)([]),2),W=x[0],D=x[1],L=m((0,n.useState)(!1),2),R=L[0],T=L[1],U=(0,n.useRef)(0),q=(0,n.useRef)(),N=(0,n.useRef)(!1),M=(0,n.useRef)(!1),z=null==g||null==(r=g.options)?void 0:r.selectedCollections,G=null!=(t=null==g||null==(y=g.options)?void 0:y.addAllCollections)?t:(null==g||null==(h=g.options)?void 0:h.autoDiscover)!==!1,H=null!=I?I:G,K="edit"===v&&!G&&!Array.isArray(z);(0,n.useEffect)(function(){void 0===S.getFieldValue(["options","host"])&&S.setFieldValue(["options","host"],"localhost"),void 0===S.getFieldValue(["options","port"])&&S.setFieldValue(["options","port"],u.Wg),void 0===S.getFieldValue(["options","schema"])&&S.setFieldValue(["options","schema"],u.my),void 0===S.getFieldValue(["options","ssl"])&&S.setFieldValue(["options","ssl"],!1),void 0===S.getFieldValue(["options","addAllCollections"])&&S.setFieldValue(["options","addAllCollections"],G),void 0===S.getFieldValue("__postgresPreserveLegacySelection")&&S.setFieldValue("__postgresPreserveLegacySelection",K),"edit"===v&&Array.isArray(z)&&void 0===S.getFieldValue("collections")&&S.setFieldValue("collections",z)},[S,G,v,K,z]),(0,n.useEffect)(function(){U.current+=1,q.current=void 0,N.current=!1,M.current=!1,D([]),T(!1)},[O,P,C,j,A,k,_,V]);var $=(0,n.useCallback)(function(){var e;return(e=function(){var e,t,r,n,o,l,a,s;return function(e,t){var r,n,o,l={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype),s=Object.defineProperty;return s(a,"next",{value:u(0)}),s(a,"throw",{value:u(1)}),s(a,"return",{value:u(2)}),"function"==typeof Symbol&&s(a,Symbol.iterator,{value:function(){return this}}),a;function u(s){return function(u){var i=[s,u];if(r)throw TypeError("Generator is already executing.");for(;a&&(a=0,i[0]&&(l=0)),l;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return l.label++,{value:i[1],done:!1};case 5:l.label++,n=i[1],i=[0];continue;case 7:i=l.ops.pop(),l.trys.pop();continue;default:if(!(o=(o=l.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){l=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){l.label=i[1];break}if(6===i[0]&&l.label<o[1]){l.label=o[1],o=i;break}if(o&&l.label<o[2]){l.label=o[2],l.ops.push(i);break}o[2]&&l.ops.pop(),l.trys.pop();continue}i=t.call(e,l)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}}}(this,function(p){var m,y,h;switch(p.label){case 0:if(r=[(t=S.getFieldsValue(!0)).key].concat(function(e){if(Array.isArray(e))return c(e)}(m=f.map(function(e){return b(e,t.options)}))||function(e){if("u">typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(m)||d(m)||function(){throw TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),(n=null==(e=q.current)?void 0:e.every(function(e,t){return e===r[t]}))&&(N.current||M.current))return[2];n||(U.current+=1,q.current=r,N.current=!1),o=U.current,M.current=!0,T(!0),p.label=1;case 1:if(p.trys.push([1,6,7,8]),l=f.some(function(e){return b(e,t.options)!==b(e,null==g?void 0:g.options)}),!("create"===v||l))return[3,3];return[4,E.api.resource("dataSources").readTables({values:{dataSourceKey:t.key,dbOptions:(y=function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),n.forEach(function(t){var n;n=r[t],t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n})}return e}({},t.options),h=h={type:u.uy},Object.getOwnPropertyDescriptors?Object.defineProperties(y,Object.getOwnPropertyDescriptors(h)):(function(e){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t.push.apply(t,r)}return t})(Object(h)).forEach(function(e){Object.defineProperty(y,e,Object.getOwnPropertyDescriptor(h,e))}),y)}})];case 2:return s=p.sent().data,[3,5];case 3:return[4,F(String(null==g?void 0:g.key))];case 4:s=p.sent(),p.label=5;case 5:return a=s,o===U.current&&(D((0,i.I)(a)),N.current=!0),[3,8];case 6:return p.sent(),o===U.current&&E.message.error(String(w("Unable to load collections"))),[3,8];case 7:return o===U.current&&(M.current=!1,T(!1)),[7];case 8:return[2]}})},function(){var t=this,r=arguments;return new Promise(function(n,o){var l=e.apply(t,r);function a(e){p(l,n,o,a,s,"next",e)}function s(e){p(l,n,o,a,s,"throw",e)}a(void 0)})})()},[E,S,g,F,v,w]);return o().createElement(o().Fragment,null,o().createElement(l.Form.Item,{name:["options","host"],label:w("Host"),rules:[{required:!0}]},o().createElement(l.Input,null)),o().createElement(l.Form.Item,{name:["options","port"],label:w("Port"),rules:[{required:!0}]},o().createElement(l.InputNumber,{min:1,max:65535,style:{width:"100%"}})),o().createElement(l.Form.Item,{name:["options","database"],label:w("Database"),rules:[{required:!0}]},o().createElement(l.Input,null)),o().createElement(l.Form.Item,{name:["options","schema"],label:w("Schema")},o().createElement(l.Input,null)),o().createElement(l.Form.Item,{name:["options","username"],label:w("Username"),rules:[{required:!0}]},o().createElement(l.Input,null)),o().createElement(l.Form.Item,{name:["options","password"],label:w("Password")},o().createElement(l.Input.Password,null)),o().createElement(l.Form.Item,{name:["options","ssl"],label:w("Use SSL"),valuePropName:"checked"},o().createElement(l.Checkbox,null)),_?o().createElement(l.Form.Item,{name:["options","sslCa"],label:w("CA certificate (PEM)")},o().createElement(l.Input.TextArea,{autoSize:{minRows:3,maxRows:8}})):null,o().createElement(l.Form.Item,{name:["options","addAllCollections"],label:w("Load all collections"),valuePropName:"checked"},o().createElement(l.Checkbox,null)),o().createElement(l.Form.Item,{name:"__postgresPreserveLegacySelection",hidden:!0},o().createElement(l.Input,null)),H?null:o().createElement(l.Form.Item,{name:"collections",label:w("Collections")},o().createElement(l.Select,{mode:"multiple",allowClear:!0,loading:R,options:W,optionFilterProp:"label",onFocus:$,onChange:function(){return S.setFieldValue("__postgresPreserveLegacySelection",!1)}})))}}}]);
|