|
1 | 1 | import { DataSourceDataType } from "./dataSourceConfig";
|
2 | 2 | import { ActionDataType } from "./queryConfig";
|
3 | 3 | import { FirebirdI18nTranslator } from "./i18n";
|
| 4 | +import { ServiceError } from "../../common/error"; |
4 | 5 | import _ from "lodash";
|
5 | 6 | import Firebird from "node-firebird";
|
6 | 7 |
|
@@ -39,11 +40,41 @@ export async function validateDataSourceConfig(dataSourceConfig: DataSourceDataT
|
39 | 40 | }
|
40 | 41 | }
|
41 | 42 |
|
| 43 | +async function prepareQueryParameters(stmt: string, parameters: object) { |
| 44 | + const re : RegExp = /(:)([_a-zA-Z0-9\[\]\.]+)/gm; |
| 45 | + |
| 46 | + const placeholdersInStmt = stmt.matchAll(re); |
| 47 | + |
| 48 | + let parametersArray = []; |
| 49 | + |
| 50 | + for(const match of placeholdersInStmt) { |
| 51 | + const paramName: string = match[2]; |
| 52 | + if (_.isNil(paramName)) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + if (!_.has(parameters, paramName)) { |
| 56 | + throw new ServiceError(`Named parameter "${paramName}" not found in Query Parameters object.`); |
| 57 | + } |
| 58 | + parametersArray.push(_.get(parameters, paramName)); |
| 59 | + } |
| 60 | + |
| 61 | + const modifiedStmt = stmt.replaceAll(re, "?"); |
| 62 | + |
| 63 | + return { |
| 64 | + stmt: modifiedStmt, |
| 65 | + parametersArray: parametersArray |
| 66 | + } |
| 67 | +} |
| 68 | + |
42 | 69 | export default async function run(action: ActionDataType, dataSourceConfig: DataSourceDataType, i18n: FirebirdI18nTranslator) {
|
43 | 70 | if (action.actionName === "Query") {
|
44 | 71 | let db = await fbdAsync.attachAsync(getFirebirdOptions(dataSourceConfig));
|
45 | 72 | promisifyAll(db);
|
46 |
| - const results = await db.queryAsync(action.sql); |
| 73 | + |
| 74 | + const { stmt, parametersArray } = await prepareQueryParameters(action.sql, action.params); |
| 75 | + |
| 76 | + const results = db.queryAsync(stmt, parametersArray); |
| 77 | + |
47 | 78 | db.detachAsync();
|
48 | 79 | return results;
|
49 | 80 | }
|
|
0 commit comments