query-core 0.0.4 → 0.0.5
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/index.js +1 -0
- package/lib/services.js +276 -0
- package/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/services.ts +309 -0
package/lib/index.js
CHANGED
package/lib/services.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function log(db, isLog, logger, q, result, r, duration) {
|
|
4
|
+
if (!isLog) {
|
|
5
|
+
return db;
|
|
6
|
+
}
|
|
7
|
+
if (q !== undefined && q != null && q.length > 0) {
|
|
8
|
+
if (!logger.isDebugEnabled()) {
|
|
9
|
+
return db;
|
|
10
|
+
}
|
|
11
|
+
return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
|
|
12
|
+
}
|
|
13
|
+
if (!logger.isInfoEnabled()) {
|
|
14
|
+
return db;
|
|
15
|
+
}
|
|
16
|
+
return new LogManager(db, logger.error, logger.info, q, result, r, duration);
|
|
17
|
+
}
|
|
18
|
+
exports.log = log;
|
|
19
|
+
function useLog(db, isLog, err, lg, q, result, r, duration) {
|
|
20
|
+
if (!isLog) {
|
|
21
|
+
return db;
|
|
22
|
+
}
|
|
23
|
+
if (err) {
|
|
24
|
+
return new LogManager(db, err, lg, q, result, r, duration);
|
|
25
|
+
}
|
|
26
|
+
return db;
|
|
27
|
+
}
|
|
28
|
+
exports.useLog = useLog;
|
|
29
|
+
var LogManager = (function () {
|
|
30
|
+
function LogManager(db, err, lg, q, result, r, duration) {
|
|
31
|
+
this.db = db;
|
|
32
|
+
this.driver = db.driver;
|
|
33
|
+
this.duration = (duration && duration.length > 0 ? duration : 'duration');
|
|
34
|
+
this.sql = (q === undefined ? '' : q);
|
|
35
|
+
this.return = (r !== undefined && r != null ? r : 'count');
|
|
36
|
+
this.result = (result !== undefined && result != null ? result : '');
|
|
37
|
+
this.log = lg;
|
|
38
|
+
this.error = err;
|
|
39
|
+
this.param = this.param.bind(this);
|
|
40
|
+
this.exec = this.exec.bind(this);
|
|
41
|
+
this.execBatch = this.execBatch.bind(this);
|
|
42
|
+
this.query = this.query.bind(this);
|
|
43
|
+
this.queryOne = this.queryOne.bind(this);
|
|
44
|
+
this.execScalar = this.execScalar.bind(this);
|
|
45
|
+
this.count = this.count.bind(this);
|
|
46
|
+
}
|
|
47
|
+
LogManager.prototype.param = function (i) {
|
|
48
|
+
return this.db.param(i);
|
|
49
|
+
};
|
|
50
|
+
LogManager.prototype.exec = function (sql, args, ctx) {
|
|
51
|
+
var _this = this;
|
|
52
|
+
var t1 = new Date();
|
|
53
|
+
return this.db.exec(sql, args, ctx).then(function (v) {
|
|
54
|
+
setTimeout(function () {
|
|
55
|
+
if (_this.log) {
|
|
56
|
+
var d = diff(t1);
|
|
57
|
+
var obj = {};
|
|
58
|
+
if (_this.sql.length > 0) {
|
|
59
|
+
obj[_this.sql] = getString(sql, args);
|
|
60
|
+
}
|
|
61
|
+
if (_this.return.length > 0) {
|
|
62
|
+
obj[_this.return] = v;
|
|
63
|
+
}
|
|
64
|
+
obj[_this.duration] = d;
|
|
65
|
+
_this.log('query', obj);
|
|
66
|
+
}
|
|
67
|
+
}, 0);
|
|
68
|
+
return v;
|
|
69
|
+
}).catch(function (er) {
|
|
70
|
+
setTimeout(function () {
|
|
71
|
+
var d = diff(t1);
|
|
72
|
+
var obj = {};
|
|
73
|
+
if (_this.sql.length > 0) {
|
|
74
|
+
obj[_this.sql] = getString(sql, args);
|
|
75
|
+
}
|
|
76
|
+
obj[_this.duration] = d;
|
|
77
|
+
_this.error('error query: ' + buildString(er));
|
|
78
|
+
}, 0);
|
|
79
|
+
throw er;
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
LogManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
|
|
83
|
+
var _this = this;
|
|
84
|
+
var t1 = new Date();
|
|
85
|
+
return this.db.execBatch(statements, firstSuccess, ctx).then(function (v) {
|
|
86
|
+
setTimeout(function () {
|
|
87
|
+
if (_this.log) {
|
|
88
|
+
var d = diff(t1);
|
|
89
|
+
var obj = {};
|
|
90
|
+
if (_this.sql.length > 0) {
|
|
91
|
+
obj[_this.sql] = JSON.stringify(statements);
|
|
92
|
+
}
|
|
93
|
+
if (_this.return.length > 0) {
|
|
94
|
+
obj[_this.return] = v;
|
|
95
|
+
}
|
|
96
|
+
obj[_this.duration] = d;
|
|
97
|
+
_this.log('exec batch', obj);
|
|
98
|
+
}
|
|
99
|
+
}, 0);
|
|
100
|
+
return v;
|
|
101
|
+
}).catch(function (er) {
|
|
102
|
+
setTimeout(function () {
|
|
103
|
+
var d = diff(t1);
|
|
104
|
+
var obj = {};
|
|
105
|
+
if (_this.sql.length > 0) {
|
|
106
|
+
obj[_this.sql] = JSON.stringify(statements);
|
|
107
|
+
}
|
|
108
|
+
obj[_this.duration] = d;
|
|
109
|
+
_this.error('error exec batch: ' + buildString(er));
|
|
110
|
+
}, 0);
|
|
111
|
+
throw er;
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
LogManager.prototype.query = function (sql, args, m, bools, ctx) {
|
|
115
|
+
var _this = this;
|
|
116
|
+
var t1 = new Date();
|
|
117
|
+
return this.db.query(sql, args, m, bools, ctx).then(function (v) {
|
|
118
|
+
setTimeout(function () {
|
|
119
|
+
if (_this.log) {
|
|
120
|
+
var d = diff(t1);
|
|
121
|
+
var obj = {};
|
|
122
|
+
if (_this.sql.length > 0) {
|
|
123
|
+
obj[_this.sql] = getString(sql, args);
|
|
124
|
+
}
|
|
125
|
+
if (_this.result.length > 0) {
|
|
126
|
+
if (v && v.length > 0) {
|
|
127
|
+
obj[_this.result] = JSON.stringify(v);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (_this.return.length > 0) {
|
|
131
|
+
obj[_this.return] = v ? v.length : 0;
|
|
132
|
+
}
|
|
133
|
+
obj[_this.duration] = d;
|
|
134
|
+
_this.log('query', obj);
|
|
135
|
+
}
|
|
136
|
+
}, 0);
|
|
137
|
+
return v;
|
|
138
|
+
}).catch(function (er) {
|
|
139
|
+
setTimeout(function () {
|
|
140
|
+
var d = diff(t1);
|
|
141
|
+
var obj = {};
|
|
142
|
+
if (_this.sql.length > 0) {
|
|
143
|
+
obj[_this.sql] = getString(sql, args);
|
|
144
|
+
}
|
|
145
|
+
obj[_this.duration] = d;
|
|
146
|
+
_this.error('error query: ' + buildString(er));
|
|
147
|
+
}, 0);
|
|
148
|
+
throw er;
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
LogManager.prototype.queryOne = function (sql, args, m, bools, ctx) {
|
|
152
|
+
var _this = this;
|
|
153
|
+
var t1 = new Date();
|
|
154
|
+
return this.db.queryOne(sql, args, m, bools, ctx).then(function (v) {
|
|
155
|
+
setTimeout(function () {
|
|
156
|
+
if (_this.log) {
|
|
157
|
+
var d = diff(t1);
|
|
158
|
+
var obj = {};
|
|
159
|
+
if (_this.sql.length > 0) {
|
|
160
|
+
obj[_this.sql] = getString(sql, args);
|
|
161
|
+
}
|
|
162
|
+
if (_this.result.length > 0) {
|
|
163
|
+
obj[_this.result] = v ? JSON.stringify(v) : 'null';
|
|
164
|
+
}
|
|
165
|
+
if (_this.return.length > 0) {
|
|
166
|
+
obj[_this.return] = v ? 1 : 0;
|
|
167
|
+
}
|
|
168
|
+
obj[_this.duration] = d;
|
|
169
|
+
_this.log('query one', obj);
|
|
170
|
+
}
|
|
171
|
+
}, 0);
|
|
172
|
+
return v;
|
|
173
|
+
}).catch(function (er) {
|
|
174
|
+
setTimeout(function () {
|
|
175
|
+
var d = diff(t1);
|
|
176
|
+
var obj = {};
|
|
177
|
+
if (_this.sql.length > 0) {
|
|
178
|
+
obj[_this.sql] = getString(sql, args);
|
|
179
|
+
}
|
|
180
|
+
obj[_this.duration] = d;
|
|
181
|
+
_this.error('error query one: ' + buildString(er));
|
|
182
|
+
}, 0);
|
|
183
|
+
throw er;
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
LogManager.prototype.execScalar = function (sql, args, ctx) {
|
|
187
|
+
var _this = this;
|
|
188
|
+
var t1 = new Date();
|
|
189
|
+
return this.db.execScalar(sql, args, ctx).then(function (v) {
|
|
190
|
+
setTimeout(function () {
|
|
191
|
+
if (_this.log) {
|
|
192
|
+
var d = diff(t1);
|
|
193
|
+
var obj = {};
|
|
194
|
+
if (_this.sql.length > 0) {
|
|
195
|
+
obj[_this.sql] = getString(sql, args);
|
|
196
|
+
}
|
|
197
|
+
if (_this.result.length > 0) {
|
|
198
|
+
obj[_this.result] = v ? buildString(v) : 'null';
|
|
199
|
+
}
|
|
200
|
+
if (_this.return.length > 0) {
|
|
201
|
+
obj[_this.return] = v ? 1 : 0;
|
|
202
|
+
}
|
|
203
|
+
obj[_this.duration] = d;
|
|
204
|
+
_this.log('exec scalar', obj);
|
|
205
|
+
}
|
|
206
|
+
}, 0);
|
|
207
|
+
return v;
|
|
208
|
+
}).catch(function (er) {
|
|
209
|
+
setTimeout(function () {
|
|
210
|
+
var d = diff(t1);
|
|
211
|
+
var obj = {};
|
|
212
|
+
if (_this.sql.length > 0) {
|
|
213
|
+
obj[_this.sql] = getString(sql, args);
|
|
214
|
+
}
|
|
215
|
+
obj[_this.duration] = d;
|
|
216
|
+
_this.error('error exec scalar: ' + buildString(er));
|
|
217
|
+
}, 0);
|
|
218
|
+
throw er;
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
LogManager.prototype.count = function (sql, args, ctx) {
|
|
222
|
+
var _this = this;
|
|
223
|
+
var t1 = new Date();
|
|
224
|
+
return this.db.count(sql, args).then(function (v) {
|
|
225
|
+
setTimeout(function () {
|
|
226
|
+
if (_this.log) {
|
|
227
|
+
var d = diff(t1);
|
|
228
|
+
var obj = {};
|
|
229
|
+
if (_this.sql.length > 0) {
|
|
230
|
+
obj[_this.sql] = getString(sql, args);
|
|
231
|
+
}
|
|
232
|
+
if (_this.return.length > 0) {
|
|
233
|
+
obj[_this.return] = v;
|
|
234
|
+
}
|
|
235
|
+
obj[_this.duration] = d;
|
|
236
|
+
_this.log('count', obj);
|
|
237
|
+
}
|
|
238
|
+
}, 0);
|
|
239
|
+
return v;
|
|
240
|
+
}).catch(function (er) {
|
|
241
|
+
setTimeout(function () {
|
|
242
|
+
var d = diff(t1);
|
|
243
|
+
var obj = {};
|
|
244
|
+
if (_this.sql.length > 0) {
|
|
245
|
+
obj[_this.sql] = getString(sql, args);
|
|
246
|
+
}
|
|
247
|
+
obj[_this.duration] = d;
|
|
248
|
+
_this.error('error count: ' + buildString(er));
|
|
249
|
+
}, 0);
|
|
250
|
+
throw er;
|
|
251
|
+
});
|
|
252
|
+
};
|
|
253
|
+
return LogManager;
|
|
254
|
+
}());
|
|
255
|
+
exports.LogManager = LogManager;
|
|
256
|
+
function buildString(v) {
|
|
257
|
+
if (typeof v === 'string') {
|
|
258
|
+
return v;
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
return JSON.stringify(v);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function getString(sql, args) {
|
|
265
|
+
if (args && args.length > 0) {
|
|
266
|
+
return sql + ' ' + JSON.stringify(args);
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
return sql;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
function diff(d1) {
|
|
273
|
+
var d2 = new Date();
|
|
274
|
+
return d2.getTime() - d1.getTime();
|
|
275
|
+
}
|
|
276
|
+
exports.diff = diff;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
package/src/services.ts
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import {Attribute, Statement, StringMap} from './metadata';
|
|
2
|
+
|
|
3
|
+
export interface Manager {
|
|
4
|
+
driver: string;
|
|
5
|
+
param(i: number): string;
|
|
6
|
+
exec(sql: string, args?: any[], ctx?: any): Promise<number>;
|
|
7
|
+
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
|
|
8
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
|
|
9
|
+
}
|
|
10
|
+
export type DB = Manager;
|
|
11
|
+
export interface ExtManager {
|
|
12
|
+
driver: string;
|
|
13
|
+
param(i: number): string;
|
|
14
|
+
exec(sql: string, args?: any[], ctx?: any): Promise<number>;
|
|
15
|
+
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
|
|
16
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
|
|
17
|
+
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T|null>;
|
|
18
|
+
execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>;
|
|
19
|
+
count(sql: string, args?: any[], ctx?: any): Promise<number>;
|
|
20
|
+
}
|
|
21
|
+
export interface SimpleMap {
|
|
22
|
+
[key: string]: string|number|boolean|Date;
|
|
23
|
+
}
|
|
24
|
+
export interface Logger {
|
|
25
|
+
level: number;
|
|
26
|
+
debug(msg: string, m?: SimpleMap, ctx?: any): void;
|
|
27
|
+
info(msg: string, m?: SimpleMap, ctx?: any): void;
|
|
28
|
+
error(msg: string, m?: SimpleMap, ctx?: any): void;
|
|
29
|
+
isDebugEnabled(): boolean;
|
|
30
|
+
isInfoEnabled(): boolean;
|
|
31
|
+
}
|
|
32
|
+
export function log(db: ExtManager, isLog: boolean|undefined|null, logger: Logger, q?: string, result?: string, r?: string, duration?: string): ExtManager {
|
|
33
|
+
if (!isLog) {
|
|
34
|
+
return db;
|
|
35
|
+
}
|
|
36
|
+
if (q !== undefined && q != null && q.length > 0) {
|
|
37
|
+
if (!logger.isDebugEnabled()) {
|
|
38
|
+
return db;
|
|
39
|
+
}
|
|
40
|
+
return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
|
|
41
|
+
}
|
|
42
|
+
if (!logger.isInfoEnabled()) {
|
|
43
|
+
return db;
|
|
44
|
+
}
|
|
45
|
+
return new LogManager(db, logger.error, logger.info, q, result, r, duration);
|
|
46
|
+
}
|
|
47
|
+
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 {
|
|
48
|
+
if (!isLog) {
|
|
49
|
+
return db;
|
|
50
|
+
}
|
|
51
|
+
if (err) {
|
|
52
|
+
return new LogManager(db, err, lg, q, result, r, duration);
|
|
53
|
+
}
|
|
54
|
+
return db;
|
|
55
|
+
}
|
|
56
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
57
|
+
export class LogManager implements ExtManager {
|
|
58
|
+
constructor(public db: ExtManager, err: (msg: string, m?: SimpleMap) => void, lg?: (msg: string, m?: SimpleMap) => void, q?: string, result?: string, r?: string, duration?: string) {
|
|
59
|
+
this.driver = db.driver;
|
|
60
|
+
this.duration = (duration && duration.length > 0 ? duration : 'duration');
|
|
61
|
+
this.sql = (q === undefined ? '' : q);
|
|
62
|
+
this.return = (r !== undefined && r != null ? r : 'count');
|
|
63
|
+
this.result = (result !== undefined && result != null ? result : '');
|
|
64
|
+
// this.err = (er ? er : 'error');
|
|
65
|
+
this.log = lg;
|
|
66
|
+
this.error = err;
|
|
67
|
+
this.param = this.param.bind(this);
|
|
68
|
+
this.exec = this.exec.bind(this);
|
|
69
|
+
this.execBatch = this.execBatch.bind(this);
|
|
70
|
+
this.query = this.query.bind(this);
|
|
71
|
+
this.queryOne = this.queryOne.bind(this);
|
|
72
|
+
this.execScalar = this.execScalar.bind(this);
|
|
73
|
+
this.count = this.count.bind(this);
|
|
74
|
+
}
|
|
75
|
+
log?: (msg: string, m?: SimpleMap, ctx?: any) => void;
|
|
76
|
+
error: (msg: string, m?: SimpleMap, ctx?: any) => void;
|
|
77
|
+
driver: string;
|
|
78
|
+
duration: string;
|
|
79
|
+
sql: string;
|
|
80
|
+
return: string;
|
|
81
|
+
result: string;
|
|
82
|
+
// err: string;
|
|
83
|
+
param(i: number): string {
|
|
84
|
+
return this.db.param(i);
|
|
85
|
+
}
|
|
86
|
+
exec(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
87
|
+
const t1 = new Date();
|
|
88
|
+
return this.db.exec(sql, args, ctx).then(v => {
|
|
89
|
+
setTimeout(() => {
|
|
90
|
+
if (this.log) {
|
|
91
|
+
const d = diff(t1);
|
|
92
|
+
const obj: SimpleMap = {} ;
|
|
93
|
+
if (this.sql.length > 0) {
|
|
94
|
+
obj[this.sql] = getString(sql, args);
|
|
95
|
+
}
|
|
96
|
+
if (this.return.length > 0) {
|
|
97
|
+
obj[this.return] = v;
|
|
98
|
+
}
|
|
99
|
+
obj[this.duration] = d;
|
|
100
|
+
this.log('query', obj);
|
|
101
|
+
}
|
|
102
|
+
}, 0);
|
|
103
|
+
return v;
|
|
104
|
+
}).catch(er => {
|
|
105
|
+
setTimeout(() => {
|
|
106
|
+
const d = diff(t1);
|
|
107
|
+
const obj: SimpleMap = {};
|
|
108
|
+
if (this.sql.length > 0) {
|
|
109
|
+
obj[this.sql] = getString(sql, args);
|
|
110
|
+
}
|
|
111
|
+
obj[this.duration] = d;
|
|
112
|
+
this.error('error query: ' + buildString(er));
|
|
113
|
+
}, 0);
|
|
114
|
+
throw er;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
|
|
118
|
+
const t1 = new Date();
|
|
119
|
+
return this.db.execBatch(statements, firstSuccess, ctx).then(v => {
|
|
120
|
+
setTimeout(() => {
|
|
121
|
+
if (this.log) {
|
|
122
|
+
const d = diff(t1);
|
|
123
|
+
const obj: SimpleMap = {} ;
|
|
124
|
+
if (this.sql.length > 0) {
|
|
125
|
+
obj[this.sql] = JSON.stringify(statements);
|
|
126
|
+
}
|
|
127
|
+
if (this.return.length > 0) {
|
|
128
|
+
obj[this.return] = v;
|
|
129
|
+
}
|
|
130
|
+
obj[this.duration] = d;
|
|
131
|
+
this.log('exec batch', obj);
|
|
132
|
+
}
|
|
133
|
+
}, 0);
|
|
134
|
+
return v;
|
|
135
|
+
}).catch(er => {
|
|
136
|
+
setTimeout(() => {
|
|
137
|
+
const d = diff(t1);
|
|
138
|
+
const obj: SimpleMap = {};
|
|
139
|
+
if (this.sql.length > 0) {
|
|
140
|
+
obj[this.sql] = JSON.stringify(statements);
|
|
141
|
+
}
|
|
142
|
+
obj[this.duration] = d;
|
|
143
|
+
this.error('error exec batch: ' + buildString(er));
|
|
144
|
+
}, 0);
|
|
145
|
+
throw er;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
|
|
149
|
+
const t1 = new Date();
|
|
150
|
+
return this.db.query<T>(sql, args, m, bools, ctx).then(v => {
|
|
151
|
+
setTimeout(() => {
|
|
152
|
+
if (this.log) {
|
|
153
|
+
const d = diff(t1);
|
|
154
|
+
const obj: SimpleMap = {} ;
|
|
155
|
+
if (this.sql.length > 0) {
|
|
156
|
+
obj[this.sql] = getString(sql, args);
|
|
157
|
+
}
|
|
158
|
+
if (this.result.length > 0) {
|
|
159
|
+
if (v && v.length > 0) {
|
|
160
|
+
obj[this.result] = JSON.stringify(v);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (this.return.length > 0) {
|
|
164
|
+
obj[this.return] = v ? v.length : 0;
|
|
165
|
+
}
|
|
166
|
+
obj[this.duration] = d;
|
|
167
|
+
this.log('query', obj);
|
|
168
|
+
}
|
|
169
|
+
}, 0);
|
|
170
|
+
return v;
|
|
171
|
+
}).catch(er => {
|
|
172
|
+
setTimeout(() => {
|
|
173
|
+
const d = diff(t1);
|
|
174
|
+
const obj: SimpleMap = {};
|
|
175
|
+
if (this.sql.length > 0) {
|
|
176
|
+
obj[this.sql] = getString(sql, args);
|
|
177
|
+
}
|
|
178
|
+
obj[this.duration] = d;
|
|
179
|
+
this.error('error query: ' + buildString(er));
|
|
180
|
+
}, 0);
|
|
181
|
+
throw er;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T|null> {
|
|
185
|
+
const t1 = new Date();
|
|
186
|
+
return this.db.queryOne<T>(sql, args, m, bools, ctx).then(v => {
|
|
187
|
+
setTimeout(() => {
|
|
188
|
+
if (this.log) {
|
|
189
|
+
const d = diff(t1);
|
|
190
|
+
const obj: SimpleMap = {} ;
|
|
191
|
+
if (this.sql.length > 0) {
|
|
192
|
+
obj[this.sql] = getString(sql, args);
|
|
193
|
+
}
|
|
194
|
+
if (this.result.length > 0) {
|
|
195
|
+
obj[this.result] = v ? JSON.stringify(v) : 'null';
|
|
196
|
+
}
|
|
197
|
+
if (this.return.length > 0) {
|
|
198
|
+
obj[this.return] = v ? 1 : 0;
|
|
199
|
+
}
|
|
200
|
+
obj[this.duration] = d;
|
|
201
|
+
this.log('query one', obj);
|
|
202
|
+
}
|
|
203
|
+
}, 0);
|
|
204
|
+
return v;
|
|
205
|
+
}).catch(er => {
|
|
206
|
+
setTimeout(() => {
|
|
207
|
+
const d = diff(t1);
|
|
208
|
+
const obj: SimpleMap = {};
|
|
209
|
+
if (this.sql.length > 0) {
|
|
210
|
+
obj[this.sql] = getString(sql, args);
|
|
211
|
+
}
|
|
212
|
+
obj[this.duration] = d;
|
|
213
|
+
this.error('error query one: ' + buildString(er));
|
|
214
|
+
}, 0);
|
|
215
|
+
throw er;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
|
|
219
|
+
const t1 = new Date();
|
|
220
|
+
return this.db.execScalar<T>(sql, args, ctx).then(v => {
|
|
221
|
+
setTimeout(() => {
|
|
222
|
+
if (this.log) {
|
|
223
|
+
const d = diff(t1);
|
|
224
|
+
const obj: SimpleMap = {} ;
|
|
225
|
+
if (this.sql.length > 0) {
|
|
226
|
+
obj[this.sql] = getString(sql, args);
|
|
227
|
+
}
|
|
228
|
+
if (this.result.length > 0) {
|
|
229
|
+
obj[this.result] = v ? buildString(v) : 'null';
|
|
230
|
+
}
|
|
231
|
+
if (this.return.length > 0) {
|
|
232
|
+
obj[this.return] = v ? 1 : 0;
|
|
233
|
+
}
|
|
234
|
+
obj[this.duration] = d;
|
|
235
|
+
this.log('exec scalar', obj);
|
|
236
|
+
}
|
|
237
|
+
}, 0);
|
|
238
|
+
return v;
|
|
239
|
+
}).catch(er => {
|
|
240
|
+
setTimeout(() => {
|
|
241
|
+
const d = diff(t1);
|
|
242
|
+
const obj: SimpleMap = {};
|
|
243
|
+
if (this.sql.length > 0) {
|
|
244
|
+
obj[this.sql] = getString(sql, args);
|
|
245
|
+
}
|
|
246
|
+
obj[this.duration] = d;
|
|
247
|
+
this.error('error exec scalar: ' + buildString(er));
|
|
248
|
+
}, 0);
|
|
249
|
+
throw er;
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
count(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
253
|
+
const t1 = new Date();
|
|
254
|
+
return this.db.count(sql, args).then(v => {
|
|
255
|
+
setTimeout(() => {
|
|
256
|
+
if (this.log) {
|
|
257
|
+
const d = diff(t1);
|
|
258
|
+
const obj: SimpleMap = {} ;
|
|
259
|
+
if (this.sql.length > 0) {
|
|
260
|
+
obj[this.sql] = getString(sql, args);
|
|
261
|
+
}
|
|
262
|
+
if (this.return.length > 0) {
|
|
263
|
+
obj[this.return] = v;
|
|
264
|
+
}
|
|
265
|
+
obj[this.duration] = d;
|
|
266
|
+
this.log('count', obj);
|
|
267
|
+
}
|
|
268
|
+
}, 0);
|
|
269
|
+
return v;
|
|
270
|
+
}).catch(er => {
|
|
271
|
+
setTimeout(() => {
|
|
272
|
+
const d = diff(t1);
|
|
273
|
+
const obj: SimpleMap = {};
|
|
274
|
+
if (this.sql.length > 0) {
|
|
275
|
+
obj[this.sql] = getString(sql, args);
|
|
276
|
+
}
|
|
277
|
+
obj[this.duration] = d;
|
|
278
|
+
this.error('error count: ' + buildString(er));
|
|
279
|
+
}, 0);
|
|
280
|
+
throw er;
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
function buildString(v: any): string {
|
|
285
|
+
if (typeof v === 'string') {
|
|
286
|
+
return v;
|
|
287
|
+
} else {
|
|
288
|
+
return JSON.stringify(v);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function getString(sql: string, args?: any[]): string {
|
|
292
|
+
if (args && args.length > 0) {
|
|
293
|
+
return sql + ' ' + JSON.stringify(args);
|
|
294
|
+
} else {
|
|
295
|
+
return sql;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
export function diff(d1: Date): number {
|
|
299
|
+
const d2 = new Date();
|
|
300
|
+
return d2.getTime() - d1.getTime();
|
|
301
|
+
}
|
|
302
|
+
/*
|
|
303
|
+
const NS_PER_SEC = 1e9;
|
|
304
|
+
const NS_TO_MS = 1e6;
|
|
305
|
+
const getDurationInMilliseconds = (start: [number, number] | undefined) => {
|
|
306
|
+
const diff = process.hrtime(start);
|
|
307
|
+
return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
|
|
308
|
+
};
|
|
309
|
+
*/
|