@heyhru/business-dms-approval 0.9.1 → 0.10.0

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/dist/index.js CHANGED
@@ -190,6 +190,31 @@ function approvalExecute(encryptionKey) {
190
190
  }
191
191
  };
192
192
  }
193
+ function splitStatements(sql) {
194
+ const statements = [];
195
+ let current = "";
196
+ let inSingle = false;
197
+ let inDouble = false;
198
+ for (let i = 0; i < sql.length; i++) {
199
+ const ch = sql[i];
200
+ const prev = sql[i - 1];
201
+ if (ch === "'" && !inDouble && prev !== "\\") {
202
+ inSingle = !inSingle;
203
+ } else if (ch === '"' && !inSingle && prev !== "\\") {
204
+ inDouble = !inDouble;
205
+ }
206
+ if (ch === ";" && !inSingle && !inDouble) {
207
+ const trimmed2 = current.trim();
208
+ if (trimmed2) statements.push(trimmed2);
209
+ current = "";
210
+ } else {
211
+ current += ch;
212
+ }
213
+ }
214
+ const trimmed = current.trim();
215
+ if (trimmed) statements.push(trimmed);
216
+ return statements;
217
+ }
193
218
  async function doExecuteApproval({ id, userId, ip, encryptionKey }, log) {
194
219
  const approval = await getApprovalById(id);
195
220
  if (!approval || !["approved", "execute_failed"].includes(approval["status"])) {
@@ -205,15 +230,24 @@ async function doExecuteApproval({ id, userId, ip, encryptionKey }, log) {
205
230
  return (0, import_business_dms_datasource.getPool)({ ...ds, database: ds.database ?? "" });
206
231
  })();
207
232
  if (!pool) throw new Error("Data source not found");
208
- const rows = await pool.execute(approval["sql_text"]);
209
- const result = `${rows.length} rows affected`;
233
+ const sqlText = approval["sql_text"];
234
+ const statements = splitStatements(sqlText);
235
+ let result;
236
+ if (statements.length <= 1) {
237
+ const rows = await pool.execute(sqlText);
238
+ result = `${rows.length} rows affected`;
239
+ } else {
240
+ const results = await pool.executeInTransaction(statements);
241
+ const details = results.map((r) => `${r.rowCount} rows`).join(", ");
242
+ result = `${statements.length} statements executed (${details})`;
243
+ }
210
244
  await setExecuteResult(id, "executed", result);
211
- log.info("Approval executed (id=%s, user=%s)", id, userId);
245
+ log.info("Approval executed (id=%s, user=%s, stmts=%d)", id, userId, statements.length);
212
246
  await (0, import_business_dms_audit.writeAuditLog)({
213
247
  userId,
214
248
  dataSourceId,
215
249
  action: "DML_EXECUTE",
216
- sqlText: approval["sql_text"],
250
+ sqlText,
217
251
  resultSummary: result,
218
252
  ipAddress: ip
219
253
  });
package/dist/index.mjs CHANGED
@@ -156,6 +156,31 @@ function approvalExecute(encryptionKey) {
156
156
  }
157
157
  };
158
158
  }
159
+ function splitStatements(sql) {
160
+ const statements = [];
161
+ let current = "";
162
+ let inSingle = false;
163
+ let inDouble = false;
164
+ for (let i = 0; i < sql.length; i++) {
165
+ const ch = sql[i];
166
+ const prev = sql[i - 1];
167
+ if (ch === "'" && !inDouble && prev !== "\\") {
168
+ inSingle = !inSingle;
169
+ } else if (ch === '"' && !inSingle && prev !== "\\") {
170
+ inDouble = !inDouble;
171
+ }
172
+ if (ch === ";" && !inSingle && !inDouble) {
173
+ const trimmed2 = current.trim();
174
+ if (trimmed2) statements.push(trimmed2);
175
+ current = "";
176
+ } else {
177
+ current += ch;
178
+ }
179
+ }
180
+ const trimmed = current.trim();
181
+ if (trimmed) statements.push(trimmed);
182
+ return statements;
183
+ }
159
184
  async function doExecuteApproval({ id, userId, ip, encryptionKey }, log) {
160
185
  const approval = await getApprovalById(id);
161
186
  if (!approval || !["approved", "execute_failed"].includes(approval["status"])) {
@@ -171,15 +196,24 @@ async function doExecuteApproval({ id, userId, ip, encryptionKey }, log) {
171
196
  return getPool({ ...ds, database: ds.database ?? "" });
172
197
  })();
173
198
  if (!pool) throw new Error("Data source not found");
174
- const rows = await pool.execute(approval["sql_text"]);
175
- const result = `${rows.length} rows affected`;
199
+ const sqlText = approval["sql_text"];
200
+ const statements = splitStatements(sqlText);
201
+ let result;
202
+ if (statements.length <= 1) {
203
+ const rows = await pool.execute(sqlText);
204
+ result = `${rows.length} rows affected`;
205
+ } else {
206
+ const results = await pool.executeInTransaction(statements);
207
+ const details = results.map((r) => `${r.rowCount} rows`).join(", ");
208
+ result = `${statements.length} statements executed (${details})`;
209
+ }
176
210
  await setExecuteResult(id, "executed", result);
177
- log.info("Approval executed (id=%s, user=%s)", id, userId);
211
+ log.info("Approval executed (id=%s, user=%s, stmts=%d)", id, userId, statements.length);
178
212
  await writeAuditLog({
179
213
  userId,
180
214
  dataSourceId,
181
215
  action: "DML_EXECUTE",
182
- sqlText: approval["sql_text"],
216
+ sqlText,
183
217
  resultSummary: result,
184
218
  ipAddress: ip
185
219
  });
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.9.1",
6
+ "version": "0.10.0",
7
7
  "description": "DMS approval workflow domain logic: service, model, sql",
8
8
  "main": "./dist/index.js",
9
9
  "module": "./dist/index.mjs",
@@ -26,9 +26,9 @@
26
26
  "clean": "rm -rf dist"
27
27
  },
28
28
  "dependencies": {
29
- "@heyhru/business-dms-audit": "0.6.2",
30
- "@heyhru/business-dms-datasource": "0.8.2",
31
- "@heyhru/server-plugin-pg": "0.7.0",
29
+ "@heyhru/business-dms-audit": "0.6.3",
30
+ "@heyhru/business-dms-datasource": "0.8.3",
31
+ "@heyhru/server-plugin-pg": "0.8.0",
32
32
  "fastify": "^5.8.4"
33
33
  },
34
34
  "devDependencies": {
@@ -36,5 +36,5 @@
36
36
  "typescript": "^6.0.2",
37
37
  "vitest": "^4.1.4"
38
38
  },
39
- "gitHead": "f6013af87a27c9ad96b0e078760bd7b3d9832730"
39
+ "gitHead": "9ae6aeb27bf1baa02f538af34d55399d128af62c"
40
40
  }