db-crud-api 0.2.2 → 0.3.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/CHANGELOG.md +43 -37
- package/README.md +97 -96
- package/index.js +10 -10
- package/lib/api-batch.js +2 -2
- package/lib/api-execute.js +2 -2
- package/lib/api-factory.js +26 -26
- package/lib/api-full.js +95 -95
- package/lib/api-ro.js +37 -37
- package/lib/db-operations.js +413 -404
- package/lib/mssql.js +273 -277
- package/lib/mysql.js +278 -0
- package/lib/schema.js +3 -3
- package/package.json +22 -17
package/lib/mssql.js
CHANGED
|
@@ -1,278 +1,274 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import sql from 'mssql';
|
|
4
|
-
|
|
5
|
-
const pools = {};
|
|
6
|
-
|
|
7
|
-
// Common
|
|
8
|
-
function stringifyValue(fieldName, value, tSchema) {
|
|
9
|
-
if (value == undefined) { return 'null' }
|
|
10
|
-
if (typeof value !== 'string' || value.trimStart().charAt(0) !== '[') {
|
|
11
|
-
if (tSchema.table.fields && tSchema.table.fields[fieldName] && tSchema.table.fields[fieldName].type) {
|
|
12
|
-
if (tSchema.table.fields[fieldName].type == 'datetime') {
|
|
13
|
-
if (typeof value == 'datetime' || typeof value == 'object') return `\'${value.toISOString()}\'`;
|
|
14
|
-
if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
|
|
15
|
-
return value;
|
|
16
|
-
}
|
|
17
|
-
if (tSchema.table.fields[fieldName].type == 'boolean' && typeof value == 'boolean') return `\'${value}\'`;
|
|
18
|
-
if (tSchema.table.fields[fieldName].type == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
|
|
19
|
-
if (tSchema.table.fields[fieldName].type == 'uuid' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
// if (typeof value == 'datetime') return `\'${value.toISOString()}\'`;
|
|
23
|
-
if (typeof value == 'boolean') return `\'${value}\'`;
|
|
24
|
-
if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Create config object for pool
|
|
31
|
-
export function prepareConnection(tSchema) {
|
|
32
|
-
return {
|
|
33
|
-
id: `${tSchema.server.realName}-${tSchema.database.realName}-${tSchema.server.user}`,
|
|
34
|
-
server: ((tSchema.server.instance && tSchema.server.instance != 'DEFAULT') ? (tSchema.server.realName + '\\' + tSchema.server.instance) : tSchema.server.realName) ?? 'localhost',
|
|
35
|
-
port: tSchema.server.port,
|
|
36
|
-
user: tSchema.server.user,
|
|
37
|
-
password: tSchema.server.password,
|
|
38
|
-
options: {
|
|
39
|
-
appName: tSchema.server.options?.appName,
|
|
40
|
-
encrypt: tSchema.server.options?.encrypt ?? false, // true for azure
|
|
41
|
-
trustServerCertificate: tSchema.server.options?.trustServerCertificate ?? true // true for local dev / self-signed certs
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
pool
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
let
|
|
83
|
-
|
|
84
|
-
if
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (dbOpe.
|
|
111
|
-
if (dbOpe.
|
|
112
|
-
if (dbOpe.
|
|
113
|
-
if (dbOpe.
|
|
114
|
-
if (dbOpe.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
sqlRequest.input(key, value)
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return;
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import sql from 'mssql';
|
|
4
|
+
|
|
5
|
+
const pools = {};
|
|
6
|
+
|
|
7
|
+
// Common
|
|
8
|
+
function stringifyValue(fieldName, value, tSchema) {
|
|
9
|
+
if (value == undefined) { return 'null' }
|
|
10
|
+
if (typeof value !== 'string' || value.trimStart().charAt(0) !== '[') {
|
|
11
|
+
if (tSchema.table.fields && tSchema.table.fields[fieldName] && tSchema.table.fields[fieldName].type) {
|
|
12
|
+
if (tSchema.table.fields[fieldName].type == 'datetime') {
|
|
13
|
+
if (typeof value == 'datetime' || typeof value == 'object') return `\'${value.toISOString()}\'`;
|
|
14
|
+
if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
if (tSchema.table.fields[fieldName].type == 'boolean' && typeof value == 'boolean') return `\'${value}\'`;
|
|
18
|
+
if (tSchema.table.fields[fieldName].type == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
|
|
19
|
+
if (tSchema.table.fields[fieldName].type == 'uuid' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
// if (typeof value == 'datetime') return `\'${value.toISOString()}\'`;
|
|
23
|
+
if (typeof value == 'boolean') return `\'${value}\'`;
|
|
24
|
+
if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Create config object for pool
|
|
31
|
+
export function prepareConnection(tSchema) {
|
|
32
|
+
return {
|
|
33
|
+
id: `${tSchema.server.realName}-${tSchema.database.realName}-${tSchema.server.user}`,
|
|
34
|
+
server: ((tSchema.server.instance && tSchema.server.instance != 'DEFAULT') ? (tSchema.server.realName + '\\' + tSchema.server.instance) : tSchema.server.realName) ?? 'localhost',
|
|
35
|
+
port: tSchema.server.port,
|
|
36
|
+
user: tSchema.server.user,
|
|
37
|
+
password: tSchema.server.password,
|
|
38
|
+
options: {
|
|
39
|
+
appName: tSchema.server.options?.appName,
|
|
40
|
+
encrypt: tSchema.server.options?.encrypt ?? false, // true for azure
|
|
41
|
+
trustServerCertificate: tSchema.server.options?.trustServerCertificate ?? true // true for local dev / self-signed certs
|
|
42
|
+
},
|
|
43
|
+
pool: tSchema.server.pool,
|
|
44
|
+
database: tSchema.database.realName
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Close connection
|
|
49
|
+
export async function closeConnection(connection) {
|
|
50
|
+
let pool = pools[connection.id];
|
|
51
|
+
if (pool) {
|
|
52
|
+
await pool.close(); // wait pool is closed
|
|
53
|
+
pools[connection.id] = undefined; // remove pool from pools
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Close all connections
|
|
58
|
+
export async function closeAllConnections() {
|
|
59
|
+
for (let poolId in pools) {
|
|
60
|
+
if (!pools.hasOwnProperty(poolId)) continue;
|
|
61
|
+
let pool = pools[poolId];
|
|
62
|
+
if (pool) {
|
|
63
|
+
await pool.close(); // wait pool is closed
|
|
64
|
+
pool = undefined; // remove pool from pools
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Query
|
|
70
|
+
export async function query(connection, dbOpes) {
|
|
71
|
+
|
|
72
|
+
// Prepare pool
|
|
73
|
+
let pool = pools[connection.id]; // Try to get an existing pool
|
|
74
|
+
if (!pool) {
|
|
75
|
+
pool = new sql.ConnectionPool(connection); // Create new pool
|
|
76
|
+
try { await pool.connect(); // wait that connection are made
|
|
77
|
+
} catch(err) { throw(err); } // using original error
|
|
78
|
+
pools[connection.id] = pool; // add new pool to pools
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Prepare sql statement
|
|
82
|
+
let sqlString = '';
|
|
83
|
+
let sqlRequest = pool.request();
|
|
84
|
+
// if exists, input params will be added to request
|
|
85
|
+
if (Array.isArray(dbOpes)) dbOpes.forEach((dbOpe, index) => { sqlString += ToSql(dbOpe, sqlRequest); });
|
|
86
|
+
else sqlString += ToSql(dbOpes, sqlRequest);
|
|
87
|
+
|
|
88
|
+
// Run query
|
|
89
|
+
const sqlresult = await sqlRequest.query(sqlString);
|
|
90
|
+
|
|
91
|
+
// Return a single object if there is only 1 row
|
|
92
|
+
if (sqlresult.recordsets.length === 0) return;
|
|
93
|
+
if (sqlresult.recordsets.length === 1) return sqlresult.recordsets[0];
|
|
94
|
+
else return sqlresult.recordsets;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Normalize field name
|
|
98
|
+
//function toFieldName(s) {
|
|
99
|
+
// if ( s.trimStart().charAt(0) === '[' ) return s;
|
|
100
|
+
// else return `\[${s}\]`;
|
|
101
|
+
//}
|
|
102
|
+
|
|
103
|
+
// Compose fully qualified table name
|
|
104
|
+
function fullyQualifiedTableName(tSchema) {
|
|
105
|
+
return (tSchema.database.realName + '.' + (tSchema.table.schema ?? '') + '.' + tSchema.table.realName);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Parse oprations object to sql string
|
|
109
|
+
function ToSql(dbOpe, sqlRequest) {
|
|
110
|
+
if (dbOpe.get) return getToSql(dbOpe, sqlRequest);
|
|
111
|
+
if (dbOpe.patch) return patchToSql(dbOpe, sqlRequest);
|
|
112
|
+
if (dbOpe.put) return putToSql(dbOpe, sqlRequest);
|
|
113
|
+
if (dbOpe.delete) return deleteToSql(dbOpe, sqlRequest);
|
|
114
|
+
if (dbOpe.execute) return executeToSql(dbOpe, sqlRequest);
|
|
115
|
+
if (dbOpe.begin) return beginToSql(dbOpe, sqlRequest);
|
|
116
|
+
if (dbOpe.commit) return commitToSql(dbOpe, sqlRequest);
|
|
117
|
+
if (dbOpe.passthrough) return passthroughToSql(dbOpe, sqlRequest);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Parse operation object to sql string without any trasformation.
|
|
121
|
+
function passthroughToSql(dbOpe, sqlRequest) {
|
|
122
|
+
|
|
123
|
+
let result = "";
|
|
124
|
+
|
|
125
|
+
if (dbOpe.passthrough.command) {
|
|
126
|
+
result += dbOpe.passthrough.command;
|
|
127
|
+
}
|
|
128
|
+
else { throw new Error('command is missing.'); }
|
|
129
|
+
|
|
130
|
+
if (dbOpe.passthrough.params) addParams(dbOpe.passthrough.params, sqlRequest);
|
|
131
|
+
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Parse get operation object to sql string
|
|
136
|
+
function getToSql(dbOpe, sqlRequest) {
|
|
137
|
+
let _first = true;
|
|
138
|
+
|
|
139
|
+
let result = 'select '
|
|
140
|
+
|
|
141
|
+
if ((dbOpe.get.options) && (Object.keys(dbOpe.get.options).length > 0)) {
|
|
142
|
+
if (Array.isArray(dbOpe.get.options)) result += dbOpe.get.options.join(' ') + ' ';
|
|
143
|
+
else result += dbOpe.get.options + ' ';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if ((dbOpe.get.fields) && (Object.keys(dbOpe.get.fields).length > 0)) {
|
|
147
|
+
if (Array.isArray(dbOpe.get.fields)) result += dbOpe.get.fields.join(', ');
|
|
148
|
+
else result += dbOpe.get.fields;
|
|
149
|
+
}
|
|
150
|
+
else { throw new Error('fields is missing.'); }
|
|
151
|
+
|
|
152
|
+
result += ' from ' + fullyQualifiedTableName(dbOpe.get.schema);
|
|
153
|
+
|
|
154
|
+
if ((dbOpe.get.filters) && (Object.keys(dbOpe.get.filters).length > 0)) {
|
|
155
|
+
if (Array.isArray(dbOpe.get.filters)) result += ' where ' + dbOpe.get.filters.join(' ');
|
|
156
|
+
else result += ' where ' + dbOpe.get.filters;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if ((dbOpe.get.groups) && (Object.keys(dbOpe.get.groups).length > 0)) {
|
|
160
|
+
if (Array.isArray(dbOpe.get.groups)) result += ' group by ' + dbOpe.get.groups.join(', ');
|
|
161
|
+
else result += ' group by ' + dbOpe.get.groups;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if ((dbOpe.get.groupsFilters) && (Object.keys(dbOpe.get.groupsFilters).length > 0)) {
|
|
165
|
+
if (Array.isArray(dbOpe.get.groupsFilters)) result += ' having ' + dbOpe.get.groupsFilters.join(' ');
|
|
166
|
+
else result += ' having ' + dbOpe.get.groupsFilters;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if ((dbOpe.get.orderBy) && (Object.keys(dbOpe.get.orderBy).length > 0)) {
|
|
170
|
+
if (Array.isArray(dbOpe.get.orderBy)) result += ' order by ' + dbOpe.get.orderBy.join(', ');
|
|
171
|
+
else result += ' order by ' + dbOpe.get.orderBy;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (dbOpe.get.params) addParams(dbOpe.get.params, sqlRequest);
|
|
175
|
+
|
|
176
|
+
result += ';'
|
|
177
|
+
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Parse patch operation object to sql string
|
|
182
|
+
function patchToSql(dbOpe, sqlRequest) {
|
|
183
|
+
let result = 'update ' + fullyQualifiedTableName(dbOpe.patch.schema);
|
|
184
|
+
|
|
185
|
+
if (dbOpe.patch.sets) { result += ' set ' + Object.entries(dbOpe.patch.sets).map(e => { return e[0] + '=' + stringifyValue(e[0], e[1], dbOpe.patch.schema) }).join(', '); }
|
|
186
|
+
else { throw new Error('sets is missing.'); }
|
|
187
|
+
|
|
188
|
+
if ((dbOpe.patch.filters) && (Object.keys(dbOpe.patch.filters).length > 0)) {
|
|
189
|
+
if (Array.isArray(dbOpe.patch.filters)) result += ' where ' + dbOpe.patch.filters.join(' ');
|
|
190
|
+
else result += ' where ' + dbOpe.patch.filters;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (dbOpe.patch.params) addParams(dbOpe.patch.params, sqlRequest);
|
|
194
|
+
|
|
195
|
+
result += ';'
|
|
196
|
+
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Parse put (add) operation object to sql string
|
|
201
|
+
function putToSql(dbOpe, sqlRequest) {
|
|
202
|
+
let result = 'insert into ' + fullyQualifiedTableName(dbOpe.put.schema);
|
|
203
|
+
|
|
204
|
+
if ((dbOpe.put.sets) && (Object.keys(dbOpe.put.sets).length > 0)) {
|
|
205
|
+
let result_f = ' ('; let result_v = ' values (';
|
|
206
|
+
let _first = true;
|
|
207
|
+
for (const [key, value] of Object.entries(dbOpe.put.sets)) {
|
|
208
|
+
if (!_first) { result_f += ', '; result_v += ', '; } else { _first = false }
|
|
209
|
+
if (key || value) { result_f += key; result_v += stringifyValue(key, value, dbOpe.put.schema); }
|
|
210
|
+
}
|
|
211
|
+
result_f += ')'; result_v += ')';
|
|
212
|
+
result += result_f + result_v;
|
|
213
|
+
}
|
|
214
|
+
else { throw new Error('sets is missing.'); }
|
|
215
|
+
|
|
216
|
+
if (dbOpe.put.params) addParams(dbOpe.put.params, sqlRequest);
|
|
217
|
+
|
|
218
|
+
result += ';'
|
|
219
|
+
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Parse delete operation object to sql string
|
|
224
|
+
function deleteToSql(dbOpe, sqlRequest) {
|
|
225
|
+
let result = 'delete from ' + fullyQualifiedTableName(dbOpe.delete.schema);
|
|
226
|
+
|
|
227
|
+
if ((dbOpe.delete.filters) && (Object.keys(dbOpe.delete.filters).length > 0)) {
|
|
228
|
+
if (Array.isArray(dbOpe.delete.filters)) result += ' where ' + dbOpe.delete.filters.join(' ');
|
|
229
|
+
else result += ' where ' + dbOpe.delete.filters;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (dbOpe.delete.params) addParams(dbOpe.delete.params, sqlRequest);
|
|
233
|
+
|
|
234
|
+
result += ';'
|
|
235
|
+
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Parse execute operation object to sql string
|
|
240
|
+
function executeToSql(dbOpe, sqlRequest) {
|
|
241
|
+
let result = "execute ";
|
|
242
|
+
|
|
243
|
+
if (dbOpe.execute.schema.procedure) {
|
|
244
|
+
result += dbOpe.execute.schema.procedure.command;
|
|
245
|
+
if (dbOpe.execute.arguments) result += (' ' + dbOpe.execute.arguments);
|
|
246
|
+
}
|
|
247
|
+
else { throw new Error('missing procedure/function name.'); }
|
|
248
|
+
|
|
249
|
+
if (dbOpe.execute.params) addParams(dbOpe.execute.params, sqlRequest);
|
|
250
|
+
|
|
251
|
+
result += ';'
|
|
252
|
+
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Parse begin operation object to sql string
|
|
257
|
+
function beginToSql(dbOpe, sqlRequest) {
|
|
258
|
+
return "BEGIN TRANSACTION;";
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Parse commit operation object to sql string
|
|
262
|
+
function commitToSql(dbOpe, sqlRequest) {
|
|
263
|
+
return "IF @@TRANCOUNT > 0 COMMIT TRANSACTION;";
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Add input parameters to pool.request
|
|
267
|
+
function addParams(params, sqlRequest) {
|
|
268
|
+
if (params) {
|
|
269
|
+
for (const [key, value] of Object.entries(params)) {
|
|
270
|
+
sqlRequest.input(key, value)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return;
|
|
278
274
|
}
|