query-core 0.1.17 → 0.1.18
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/lib/SearchBuilder.js +1 -1
- package/lib/query.js +1 -1
- package/lib/services.js +246 -0
- package/package.json +1 -1
- package/src/SearchBuilder.ts +3 -3
- package/src/query.ts +1 -1
- package/src/services.ts +271 -2
package/lib/SearchBuilder.js
CHANGED
|
@@ -54,7 +54,7 @@ var SearchBuilder = (function () {
|
|
|
54
54
|
var sn = s[st];
|
|
55
55
|
delete s[st];
|
|
56
56
|
var x = (this.provider === exports.postgres ? 'ilike' : this.buildParam);
|
|
57
|
-
var q2 = this.buildQuery(s, x, this.
|
|
57
|
+
var q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
|
|
58
58
|
if (!q2) {
|
|
59
59
|
throw new Error('Cannot build query');
|
|
60
60
|
}
|
package/lib/query.js
CHANGED
|
@@ -56,7 +56,7 @@ function buildDollarParam(i) {
|
|
|
56
56
|
return '$' + i;
|
|
57
57
|
}
|
|
58
58
|
exports.buildDollarParam = buildDollarParam;
|
|
59
|
-
function buildQuery(filter, bparam,
|
|
59
|
+
function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq, strExcluding) {
|
|
60
60
|
if (!table || !attrs) {
|
|
61
61
|
return undefined;
|
|
62
62
|
}
|
package/lib/services.js
CHANGED
|
@@ -91,6 +91,252 @@ var SqlSearchLoader = (function (_super) {
|
|
|
91
91
|
return SqlSearchLoader;
|
|
92
92
|
}(SqlLoader));
|
|
93
93
|
exports.SqlSearchLoader = SqlSearchLoader;
|
|
94
|
+
function log(db, isLog, logger, q, result, r, duration) {
|
|
95
|
+
if (!isLog) {
|
|
96
|
+
return db;
|
|
97
|
+
}
|
|
98
|
+
if (q !== undefined && q != null && q.length > 0) {
|
|
99
|
+
if (!logger.isDebugEnabled()) {
|
|
100
|
+
return db;
|
|
101
|
+
}
|
|
102
|
+
return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
|
|
103
|
+
}
|
|
104
|
+
if (!logger.isInfoEnabled()) {
|
|
105
|
+
return db;
|
|
106
|
+
}
|
|
107
|
+
return new LogManager(db, logger.error, logger.info, q, result, r, duration);
|
|
108
|
+
}
|
|
109
|
+
exports.log = log;
|
|
110
|
+
function useLog(db, isLog, err, lg, q, result, r, duration) {
|
|
111
|
+
if (!isLog) {
|
|
112
|
+
return db;
|
|
113
|
+
}
|
|
114
|
+
if (err) {
|
|
115
|
+
return new LogManager(db, err, lg, q, result, r, duration);
|
|
116
|
+
}
|
|
117
|
+
return db;
|
|
118
|
+
}
|
|
119
|
+
exports.useLog = useLog;
|
|
120
|
+
var LogManager = (function () {
|
|
121
|
+
function LogManager(db, err, lg, q, result, r, duration) {
|
|
122
|
+
this.db = db;
|
|
123
|
+
this.driver = db.driver;
|
|
124
|
+
this.duration = (duration && duration.length > 0 ? duration : 'duration');
|
|
125
|
+
this.sql = (q === undefined ? '' : q);
|
|
126
|
+
this.return = (r !== undefined && r != null ? r : 'count');
|
|
127
|
+
this.result = (result !== undefined && result != null ? result : '');
|
|
128
|
+
this.log = lg;
|
|
129
|
+
this.error = err;
|
|
130
|
+
this.param = this.param.bind(this);
|
|
131
|
+
this.exec = this.exec.bind(this);
|
|
132
|
+
this.execBatch = this.execBatch.bind(this);
|
|
133
|
+
this.query = this.query.bind(this);
|
|
134
|
+
this.queryOne = this.queryOne.bind(this);
|
|
135
|
+
this.execScalar = this.execScalar.bind(this);
|
|
136
|
+
this.count = this.count.bind(this);
|
|
137
|
+
}
|
|
138
|
+
LogManager.prototype.param = function (i) {
|
|
139
|
+
return this.db.param(i);
|
|
140
|
+
};
|
|
141
|
+
LogManager.prototype.exec = function (sql, args, ctx) {
|
|
142
|
+
var t1 = new Date();
|
|
143
|
+
return this.db.exec(sql, args, ctx);
|
|
144
|
+
};
|
|
145
|
+
LogManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
|
|
146
|
+
var _this = this;
|
|
147
|
+
var t1 = new Date();
|
|
148
|
+
return this.db.execBatch(statements, firstSuccess, ctx).then(function (v) {
|
|
149
|
+
setTimeout(function () {
|
|
150
|
+
if (_this.log) {
|
|
151
|
+
var d = diff(t1);
|
|
152
|
+
var obj = {};
|
|
153
|
+
if (_this.sql.length > 0) {
|
|
154
|
+
obj[_this.sql] = JSON.stringify(statements);
|
|
155
|
+
}
|
|
156
|
+
if (_this.return.length > 0) {
|
|
157
|
+
obj[_this.return] = v;
|
|
158
|
+
}
|
|
159
|
+
obj[_this.duration] = d;
|
|
160
|
+
_this.log('exec batch', obj);
|
|
161
|
+
}
|
|
162
|
+
}, 0);
|
|
163
|
+
return v;
|
|
164
|
+
}).catch(function (er) {
|
|
165
|
+
setTimeout(function () {
|
|
166
|
+
var d = diff(t1);
|
|
167
|
+
var obj = {};
|
|
168
|
+
if (_this.sql.length > 0) {
|
|
169
|
+
obj[_this.sql] = JSON.stringify(statements);
|
|
170
|
+
}
|
|
171
|
+
obj[_this.duration] = d;
|
|
172
|
+
_this.error('error exec batch: ' + buildString(er));
|
|
173
|
+
}, 0);
|
|
174
|
+
throw er;
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
LogManager.prototype.query = function (sql, args, m, bools, ctx) {
|
|
178
|
+
var _this = this;
|
|
179
|
+
var t1 = new Date();
|
|
180
|
+
return this.db.query(sql, args, m, bools, ctx).then(function (v) {
|
|
181
|
+
setTimeout(function () {
|
|
182
|
+
if (_this.log) {
|
|
183
|
+
var d = diff(t1);
|
|
184
|
+
var obj = {};
|
|
185
|
+
if (_this.sql.length > 0) {
|
|
186
|
+
obj[_this.sql] = getString(sql, args);
|
|
187
|
+
}
|
|
188
|
+
if (_this.result.length > 0) {
|
|
189
|
+
if (v && v.length > 0) {
|
|
190
|
+
obj[_this.result] = JSON.stringify(v);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (_this.return.length > 0) {
|
|
194
|
+
obj[_this.return] = v ? v.length : 0;
|
|
195
|
+
}
|
|
196
|
+
obj[_this.duration] = d;
|
|
197
|
+
_this.log('query', obj);
|
|
198
|
+
}
|
|
199
|
+
}, 0);
|
|
200
|
+
return v;
|
|
201
|
+
}).catch(function (er) {
|
|
202
|
+
setTimeout(function () {
|
|
203
|
+
var d = diff(t1);
|
|
204
|
+
var obj = {};
|
|
205
|
+
if (_this.sql.length > 0) {
|
|
206
|
+
obj[_this.sql] = getString(sql, args);
|
|
207
|
+
}
|
|
208
|
+
obj[_this.duration] = d;
|
|
209
|
+
_this.error('error query: ' + buildString(er));
|
|
210
|
+
}, 0);
|
|
211
|
+
throw er;
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
LogManager.prototype.queryOne = function (sql, args, m, bools, ctx) {
|
|
215
|
+
var _this = this;
|
|
216
|
+
var t1 = new Date();
|
|
217
|
+
return this.db.queryOne(sql, args, m, bools, ctx).then(function (v) {
|
|
218
|
+
setTimeout(function () {
|
|
219
|
+
if (_this.log) {
|
|
220
|
+
var d = diff(t1);
|
|
221
|
+
var obj = {};
|
|
222
|
+
if (_this.sql.length > 0) {
|
|
223
|
+
obj[_this.sql] = getString(sql, args);
|
|
224
|
+
}
|
|
225
|
+
if (_this.result.length > 0) {
|
|
226
|
+
obj[_this.result] = v ? JSON.stringify(v) : 'null';
|
|
227
|
+
}
|
|
228
|
+
if (_this.return.length > 0) {
|
|
229
|
+
obj[_this.return] = v ? 1 : 0;
|
|
230
|
+
}
|
|
231
|
+
obj[_this.duration] = d;
|
|
232
|
+
_this.log('query one', obj);
|
|
233
|
+
}
|
|
234
|
+
}, 0);
|
|
235
|
+
return v;
|
|
236
|
+
}).catch(function (er) {
|
|
237
|
+
setTimeout(function () {
|
|
238
|
+
var d = diff(t1);
|
|
239
|
+
var obj = {};
|
|
240
|
+
if (_this.sql.length > 0) {
|
|
241
|
+
obj[_this.sql] = getString(sql, args);
|
|
242
|
+
}
|
|
243
|
+
obj[_this.duration] = d;
|
|
244
|
+
_this.error('error query one: ' + buildString(er));
|
|
245
|
+
}, 0);
|
|
246
|
+
throw er;
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
LogManager.prototype.execScalar = function (sql, args, ctx) {
|
|
250
|
+
var _this = this;
|
|
251
|
+
var t1 = new Date();
|
|
252
|
+
return this.db.execScalar(sql, args, ctx).then(function (v) {
|
|
253
|
+
setTimeout(function () {
|
|
254
|
+
if (_this.log) {
|
|
255
|
+
var d = diff(t1);
|
|
256
|
+
var obj = {};
|
|
257
|
+
if (_this.sql.length > 0) {
|
|
258
|
+
obj[_this.sql] = getString(sql, args);
|
|
259
|
+
}
|
|
260
|
+
if (_this.result.length > 0) {
|
|
261
|
+
obj[_this.result] = v ? buildString(v) : 'null';
|
|
262
|
+
}
|
|
263
|
+
if (_this.return.length > 0) {
|
|
264
|
+
obj[_this.return] = v ? 1 : 0;
|
|
265
|
+
}
|
|
266
|
+
obj[_this.duration] = d;
|
|
267
|
+
_this.log('exec scalar', obj);
|
|
268
|
+
}
|
|
269
|
+
}, 0);
|
|
270
|
+
return v;
|
|
271
|
+
}).catch(function (er) {
|
|
272
|
+
setTimeout(function () {
|
|
273
|
+
var d = diff(t1);
|
|
274
|
+
var obj = {};
|
|
275
|
+
if (_this.sql.length > 0) {
|
|
276
|
+
obj[_this.sql] = getString(sql, args);
|
|
277
|
+
}
|
|
278
|
+
obj[_this.duration] = d;
|
|
279
|
+
_this.error('error exec scalar: ' + buildString(er));
|
|
280
|
+
}, 0);
|
|
281
|
+
throw er;
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
LogManager.prototype.count = function (sql, args, ctx) {
|
|
285
|
+
var _this = this;
|
|
286
|
+
var t1 = new Date();
|
|
287
|
+
return this.db.count(sql, args).then(function (v) {
|
|
288
|
+
setTimeout(function () {
|
|
289
|
+
if (_this.log) {
|
|
290
|
+
var d = diff(t1);
|
|
291
|
+
var obj = {};
|
|
292
|
+
if (_this.sql.length > 0) {
|
|
293
|
+
obj[_this.sql] = getString(sql, args);
|
|
294
|
+
}
|
|
295
|
+
if (_this.return.length > 0) {
|
|
296
|
+
obj[_this.return] = v;
|
|
297
|
+
}
|
|
298
|
+
obj[_this.duration] = d;
|
|
299
|
+
_this.log('count', obj);
|
|
300
|
+
}
|
|
301
|
+
}, 0);
|
|
302
|
+
return v;
|
|
303
|
+
}).catch(function (er) {
|
|
304
|
+
setTimeout(function () {
|
|
305
|
+
var d = diff(t1);
|
|
306
|
+
var obj = {};
|
|
307
|
+
if (_this.sql.length > 0) {
|
|
308
|
+
obj[_this.sql] = getString(sql, args);
|
|
309
|
+
}
|
|
310
|
+
obj[_this.duration] = d;
|
|
311
|
+
_this.error('error count: ' + buildString(er));
|
|
312
|
+
}, 0);
|
|
313
|
+
throw er;
|
|
314
|
+
});
|
|
315
|
+
};
|
|
316
|
+
return LogManager;
|
|
317
|
+
}());
|
|
318
|
+
exports.LogManager = LogManager;
|
|
319
|
+
function buildString(v) {
|
|
320
|
+
if (typeof v === 'string') {
|
|
321
|
+
return v;
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
return JSON.stringify(v);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
function getString(sql, args) {
|
|
328
|
+
if (args && args.length > 0) {
|
|
329
|
+
return sql + ' ' + JSON.stringify(args);
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
return sql;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function diff(d1) {
|
|
336
|
+
var d2 = new Date();
|
|
337
|
+
return d2.getTime() - d1.getTime();
|
|
338
|
+
}
|
|
339
|
+
exports.diff = diff;
|
|
94
340
|
var SqlWriter = (function (_super) {
|
|
95
341
|
__extends(SqlWriter, _super);
|
|
96
342
|
function SqlWriter(manager, table, attrs, toDB, fromDB) {
|
package/package.json
CHANGED
package/src/SearchBuilder.ts
CHANGED
|
@@ -10,7 +10,7 @@ export const sqlite = 'sqlite';
|
|
|
10
10
|
export class SearchBuilder<T, S> {
|
|
11
11
|
map?: StringMap;
|
|
12
12
|
bools?: Attribute[];
|
|
13
|
-
buildQuery: (s: S, bparam: LikeType|((i: number ) => string),
|
|
13
|
+
buildQuery: (s: S, bparam: LikeType|((i: number ) => string), sort?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string, attrs?: Attributes, table?: string, fields?: string[], sq?: string, strExcluding?: string) => Statement|undefined;
|
|
14
14
|
q?: string;
|
|
15
15
|
excluding?: string;
|
|
16
16
|
buildSort?: (sort?: string, map?: Attributes|StringMap) => string;
|
|
@@ -19,7 +19,7 @@ export class SearchBuilder<T, S> {
|
|
|
19
19
|
constructor(public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>, public table: string,
|
|
20
20
|
public attributes?: Attributes,
|
|
21
21
|
public provider?: string,
|
|
22
|
-
buildQ?: (s: S, bparam: LikeType|((i: number ) => string),
|
|
22
|
+
buildQ?: (s: S, bparam: LikeType|((i: number ) => string), sort?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string, attrs?: Attributes, table?: string, fields?: string[], sq?: string, strExcluding?: string) => Statement|undefined,
|
|
23
23
|
public fromDB?: (v: T) => T,
|
|
24
24
|
public sort?: string,
|
|
25
25
|
q?: string,
|
|
@@ -62,7 +62,7 @@ export class SearchBuilder<T, S> {
|
|
|
62
62
|
const sn = (s as any)[st] as string;
|
|
63
63
|
delete (s as any)[st];
|
|
64
64
|
const x = (this.provider === postgres ? 'ilike' : this.buildParam);
|
|
65
|
-
const q2 = this.buildQuery(s, x, this.
|
|
65
|
+
const q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
|
|
66
66
|
if (!q2) {
|
|
67
67
|
throw new Error('Cannot build query');
|
|
68
68
|
}
|
package/src/query.ts
CHANGED
|
@@ -52,7 +52,7 @@ export function buildOracleParam(i: number): string {
|
|
|
52
52
|
export function buildDollarParam(i: number): string {
|
|
53
53
|
return '$' + i;
|
|
54
54
|
}
|
|
55
|
-
export function buildQuery<S>(filter: S, bparam: LikeType|((i: number ) => string),
|
|
55
|
+
export function buildQuery<S>(filter: S, bparam: LikeType|((i: number ) => string), sort?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string, attrs?: Attributes, table?: string, fields?: string[], sq?: string, strExcluding?: string): Statement|undefined {
|
|
56
56
|
if (!table || !attrs) {
|
|
57
57
|
return undefined;
|
|
58
58
|
}
|
package/src/services.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {attributes, buildToDelete, buildToInsert, buildToUpdate, exist, metadata, select, version} from './build';
|
|
1
|
+
import {attributes, buildToDelete, buildToInsert, buildToUpdate, exist, metadata, select, toString, version} from './build';
|
|
2
2
|
import {Attribute, Attributes, Statement, StringMap} from './metadata';
|
|
3
3
|
import {SearchResult} from './search';
|
|
4
4
|
|
|
@@ -69,6 +69,7 @@ export class SqlLoader<T, ID> {
|
|
|
69
69
|
return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(res => (!res || res.length === 0) ? false : true);
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
72
73
|
export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
|
|
73
74
|
constructor(
|
|
74
75
|
protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
|
|
@@ -91,13 +92,280 @@ export interface Manager {
|
|
|
91
92
|
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
|
|
92
93
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
|
|
93
94
|
}
|
|
94
|
-
export
|
|
95
|
+
export type DB = Manager;
|
|
96
|
+
export interface ExtManager {
|
|
95
97
|
driver: string;
|
|
96
98
|
param(i: number): string;
|
|
97
99
|
exec(sql: string, args?: any[], ctx?: any): Promise<number>;
|
|
98
100
|
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
|
|
99
101
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
|
|
102
|
+
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T|null>;
|
|
103
|
+
execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>;
|
|
104
|
+
count(sql: string, args?: any[], ctx?: any): Promise<number>;
|
|
100
105
|
}
|
|
106
|
+
export interface SimpleMap {
|
|
107
|
+
[key: string]: string|number|boolean|Date;
|
|
108
|
+
}
|
|
109
|
+
export interface Logger {
|
|
110
|
+
level: number;
|
|
111
|
+
debug(msg: string, m?: SimpleMap, ctx?: any): void;
|
|
112
|
+
info(msg: string, m?: SimpleMap, ctx?: any): void;
|
|
113
|
+
error(msg: string, m?: SimpleMap, ctx?: any): void;
|
|
114
|
+
isDebugEnabled(): boolean;
|
|
115
|
+
isInfoEnabled(): boolean;
|
|
116
|
+
}
|
|
117
|
+
export function log(db: ExtManager, isLog: boolean|undefined|null, logger: Logger, q?: string, result?: string, r?: string, duration?: string): ExtManager {
|
|
118
|
+
if (!isLog) {
|
|
119
|
+
return db;
|
|
120
|
+
}
|
|
121
|
+
if (q !== undefined && q != null && q.length > 0) {
|
|
122
|
+
if (!logger.isDebugEnabled()) {
|
|
123
|
+
return db;
|
|
124
|
+
}
|
|
125
|
+
return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
|
|
126
|
+
}
|
|
127
|
+
if (!logger.isInfoEnabled()) {
|
|
128
|
+
return db;
|
|
129
|
+
}
|
|
130
|
+
return new LogManager(db, logger.error, logger.info, q, result, r, duration);
|
|
131
|
+
}
|
|
132
|
+
export function useLog(db: ExtManager, isLog: boolean|undefined|null, err: ((msg: string, m?: SimpleMap) => void)|undefined, lg?: (msg: string, m?: SimpleMap) => void, q?: string, result?: string, r?: string, duration?: string): ExtManager {
|
|
133
|
+
if (!isLog) {
|
|
134
|
+
return db;
|
|
135
|
+
}
|
|
136
|
+
if (err) {
|
|
137
|
+
return new LogManager(db, err, lg, q, result, r, duration);
|
|
138
|
+
}
|
|
139
|
+
return db;
|
|
140
|
+
}
|
|
141
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
142
|
+
export class LogManager implements ExtManager {
|
|
143
|
+
constructor(public db: ExtManager, err: (msg: string, m?: SimpleMap) => void, lg?: (msg: string, m?: SimpleMap) => void, q?: string, result?: string, r?: string, duration?: string) {
|
|
144
|
+
this.driver = db.driver;
|
|
145
|
+
this.duration = (duration && duration.length > 0 ? duration : 'duration');
|
|
146
|
+
this.sql = (q === undefined ? '' : q);
|
|
147
|
+
this.return = (r !== undefined && r != null ? r : 'count');
|
|
148
|
+
this.result = (result !== undefined && result != null ? result : '');
|
|
149
|
+
// this.err = (er ? er : 'error');
|
|
150
|
+
this.log = lg;
|
|
151
|
+
this.error = err;
|
|
152
|
+
this.param = this.param.bind(this);
|
|
153
|
+
this.exec = this.exec.bind(this);
|
|
154
|
+
this.execBatch = this.execBatch.bind(this);
|
|
155
|
+
this.query = this.query.bind(this);
|
|
156
|
+
this.queryOne = this.queryOne.bind(this);
|
|
157
|
+
this.execScalar = this.execScalar.bind(this);
|
|
158
|
+
this.count = this.count.bind(this);
|
|
159
|
+
}
|
|
160
|
+
log?: (msg: string, m?: SimpleMap, ctx?: any) => void;
|
|
161
|
+
error: (msg: string, m?: SimpleMap, ctx?: any) => void;
|
|
162
|
+
driver: string;
|
|
163
|
+
duration: string;
|
|
164
|
+
sql: string;
|
|
165
|
+
return: string;
|
|
166
|
+
result: string;
|
|
167
|
+
// err: string;
|
|
168
|
+
param(i: number): string {
|
|
169
|
+
return this.db.param(i);
|
|
170
|
+
}
|
|
171
|
+
exec(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
172
|
+
const t1 = new Date();
|
|
173
|
+
return this.db.exec(sql, args, ctx);
|
|
174
|
+
}
|
|
175
|
+
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
|
|
176
|
+
const t1 = new Date();
|
|
177
|
+
return this.db.execBatch(statements, firstSuccess, ctx).then(v => {
|
|
178
|
+
setTimeout(() => {
|
|
179
|
+
if (this.log) {
|
|
180
|
+
const d = diff(t1);
|
|
181
|
+
const obj: SimpleMap = {} ;
|
|
182
|
+
if (this.sql.length > 0) {
|
|
183
|
+
obj[this.sql] = JSON.stringify(statements);
|
|
184
|
+
}
|
|
185
|
+
if (this.return.length > 0) {
|
|
186
|
+
obj[this.return] = v;
|
|
187
|
+
}
|
|
188
|
+
obj[this.duration] = d;
|
|
189
|
+
this.log('exec batch', obj);
|
|
190
|
+
}
|
|
191
|
+
}, 0);
|
|
192
|
+
return v;
|
|
193
|
+
}).catch(er => {
|
|
194
|
+
setTimeout(() => {
|
|
195
|
+
const d = diff(t1);
|
|
196
|
+
const obj: SimpleMap = {};
|
|
197
|
+
if (this.sql.length > 0) {
|
|
198
|
+
obj[this.sql] = JSON.stringify(statements);
|
|
199
|
+
}
|
|
200
|
+
obj[this.duration] = d;
|
|
201
|
+
this.error('error exec batch: ' + buildString(er));
|
|
202
|
+
}, 0);
|
|
203
|
+
throw er;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
|
|
207
|
+
const t1 = new Date();
|
|
208
|
+
return this.db.query<T>(sql, args, m, bools, ctx).then(v => {
|
|
209
|
+
setTimeout(() => {
|
|
210
|
+
if (this.log) {
|
|
211
|
+
const d = diff(t1);
|
|
212
|
+
const obj: SimpleMap = {} ;
|
|
213
|
+
if (this.sql.length > 0) {
|
|
214
|
+
obj[this.sql] = getString(sql, args);
|
|
215
|
+
}
|
|
216
|
+
if (this.result.length > 0) {
|
|
217
|
+
if (v && v.length > 0) {
|
|
218
|
+
obj[this.result] = JSON.stringify(v);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (this.return.length > 0) {
|
|
222
|
+
obj[this.return] = v ? v.length : 0;
|
|
223
|
+
}
|
|
224
|
+
obj[this.duration] = d;
|
|
225
|
+
this.log('query', obj);
|
|
226
|
+
}
|
|
227
|
+
}, 0);
|
|
228
|
+
return v;
|
|
229
|
+
}).catch(er => {
|
|
230
|
+
setTimeout(() => {
|
|
231
|
+
const d = diff(t1);
|
|
232
|
+
const obj: SimpleMap = {};
|
|
233
|
+
if (this.sql.length > 0) {
|
|
234
|
+
obj[this.sql] = getString(sql, args);
|
|
235
|
+
}
|
|
236
|
+
obj[this.duration] = d;
|
|
237
|
+
this.error('error query: ' + buildString(er));
|
|
238
|
+
}, 0);
|
|
239
|
+
throw er;
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T|null> {
|
|
243
|
+
const t1 = new Date();
|
|
244
|
+
return this.db.queryOne<T>(sql, args, m, bools, ctx).then(v => {
|
|
245
|
+
setTimeout(() => {
|
|
246
|
+
if (this.log) {
|
|
247
|
+
const d = diff(t1);
|
|
248
|
+
const obj: SimpleMap = {} ;
|
|
249
|
+
if (this.sql.length > 0) {
|
|
250
|
+
obj[this.sql] = getString(sql, args);
|
|
251
|
+
}
|
|
252
|
+
if (this.result.length > 0) {
|
|
253
|
+
obj[this.result] = v ? JSON.stringify(v) : 'null';
|
|
254
|
+
}
|
|
255
|
+
if (this.return.length > 0) {
|
|
256
|
+
obj[this.return] = v ? 1 : 0;
|
|
257
|
+
}
|
|
258
|
+
obj[this.duration] = d;
|
|
259
|
+
this.log('query one', obj);
|
|
260
|
+
}
|
|
261
|
+
}, 0);
|
|
262
|
+
return v;
|
|
263
|
+
}).catch(er => {
|
|
264
|
+
setTimeout(() => {
|
|
265
|
+
const d = diff(t1);
|
|
266
|
+
const obj: SimpleMap = {};
|
|
267
|
+
if (this.sql.length > 0) {
|
|
268
|
+
obj[this.sql] = getString(sql, args);
|
|
269
|
+
}
|
|
270
|
+
obj[this.duration] = d;
|
|
271
|
+
this.error('error query one: ' + buildString(er));
|
|
272
|
+
}, 0);
|
|
273
|
+
throw er;
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
|
|
277
|
+
const t1 = new Date();
|
|
278
|
+
return this.db.execScalar<T>(sql, args, ctx).then(v => {
|
|
279
|
+
setTimeout(() => {
|
|
280
|
+
if (this.log) {
|
|
281
|
+
const d = diff(t1);
|
|
282
|
+
const obj: SimpleMap = {} ;
|
|
283
|
+
if (this.sql.length > 0) {
|
|
284
|
+
obj[this.sql] = getString(sql, args);
|
|
285
|
+
}
|
|
286
|
+
if (this.result.length > 0) {
|
|
287
|
+
obj[this.result] = v ? buildString(v) : 'null';
|
|
288
|
+
}
|
|
289
|
+
if (this.return.length > 0) {
|
|
290
|
+
obj[this.return] = v ? 1 : 0;
|
|
291
|
+
}
|
|
292
|
+
obj[this.duration] = d;
|
|
293
|
+
this.log('exec scalar', obj);
|
|
294
|
+
}
|
|
295
|
+
}, 0);
|
|
296
|
+
return v;
|
|
297
|
+
}).catch(er => {
|
|
298
|
+
setTimeout(() => {
|
|
299
|
+
const d = diff(t1);
|
|
300
|
+
const obj: SimpleMap = {};
|
|
301
|
+
if (this.sql.length > 0) {
|
|
302
|
+
obj[this.sql] = getString(sql, args);
|
|
303
|
+
}
|
|
304
|
+
obj[this.duration] = d;
|
|
305
|
+
this.error('error exec scalar: ' + buildString(er));
|
|
306
|
+
}, 0);
|
|
307
|
+
throw er;
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
count(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
311
|
+
const t1 = new Date();
|
|
312
|
+
return this.db.count(sql, args).then(v => {
|
|
313
|
+
setTimeout(() => {
|
|
314
|
+
if (this.log) {
|
|
315
|
+
const d = diff(t1);
|
|
316
|
+
const obj: SimpleMap = {} ;
|
|
317
|
+
if (this.sql.length > 0) {
|
|
318
|
+
obj[this.sql] = getString(sql, args);
|
|
319
|
+
}
|
|
320
|
+
if (this.return.length > 0) {
|
|
321
|
+
obj[this.return] = v;
|
|
322
|
+
}
|
|
323
|
+
obj[this.duration] = d;
|
|
324
|
+
this.log('count', obj);
|
|
325
|
+
}
|
|
326
|
+
}, 0);
|
|
327
|
+
return v;
|
|
328
|
+
}).catch(er => {
|
|
329
|
+
setTimeout(() => {
|
|
330
|
+
const d = diff(t1);
|
|
331
|
+
const obj: SimpleMap = {};
|
|
332
|
+
if (this.sql.length > 0) {
|
|
333
|
+
obj[this.sql] = getString(sql, args);
|
|
334
|
+
}
|
|
335
|
+
obj[this.duration] = d;
|
|
336
|
+
this.error('error count: ' + buildString(er));
|
|
337
|
+
}, 0);
|
|
338
|
+
throw er;
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function buildString(v: any): string {
|
|
343
|
+
if (typeof v === 'string') {
|
|
344
|
+
return v;
|
|
345
|
+
} else {
|
|
346
|
+
return JSON.stringify(v);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function getString(sql: string, args?: any[]): string {
|
|
350
|
+
if (args && args.length > 0) {
|
|
351
|
+
return sql + ' ' + JSON.stringify(args);
|
|
352
|
+
} else {
|
|
353
|
+
return sql;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
export function diff(d1: Date): number {
|
|
357
|
+
const d2 = new Date();
|
|
358
|
+
return d2.getTime() - d1.getTime();
|
|
359
|
+
}
|
|
360
|
+
/*
|
|
361
|
+
const NS_PER_SEC = 1e9;
|
|
362
|
+
const NS_TO_MS = 1e6;
|
|
363
|
+
const getDurationInMilliseconds = (start: [number, number] | undefined) => {
|
|
364
|
+
const diff = process.hrtime(start);
|
|
365
|
+
return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
|
|
366
|
+
};
|
|
367
|
+
*/
|
|
368
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
101
369
|
export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
|
|
102
370
|
version?: string;
|
|
103
371
|
exec: (sql: string, args?: any[], ctx?: any) => Promise<number>;
|
|
@@ -160,6 +428,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
|
|
|
160
428
|
}
|
|
161
429
|
}
|
|
162
430
|
}
|
|
431
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
163
432
|
export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
|
|
164
433
|
constructor(
|
|
165
434
|
protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
|