@@ -13,6 +13,10 @@ export class Query extends Promise {
13
13
reject = b
14
14
} )
15
15
16
+ this . resolver = resolve
17
+ this . rejecter = reject
18
+
19
+ this . statistics = handler . stats || handler . debug ? { started : - 1 , executed : - 1 } : undefined
16
20
this . tagged = Array . isArray ( strings . raw )
17
21
this . strings = strings
18
22
this . args = args
@@ -23,19 +27,30 @@ export class Query extends Promise {
23
27
this . state = null
24
28
this . statement = null
25
29
26
- this . resolve = x => ( this . active = false , resolve ( x ) )
27
- this . reject = x => ( this . active = false , reject ( x ) )
28
-
29
30
this . active = false
30
31
this . cancelled = null
31
32
this . executed = false
32
33
this . signature = ''
33
34
34
- this [ originError ] = this . handler . debug
35
+ this [ originError ] = handler . debug
35
36
? new Error ( )
36
37
: this . tagged && cachedError ( this . strings )
37
38
}
38
39
40
+ resolve ( x ) {
41
+ this . active = false
42
+ this . statistics && addStats ( this , x )
43
+ this . handler . onquery && ( this . handler . onquery = this . handler . onquery ( x ) )
44
+ this . resolver ( x )
45
+ }
46
+
47
+ reject ( x ) {
48
+ this . active = false
49
+ this . statistics && addStats ( this , x )
50
+ this . handler . onquery && ( this . handler . onquery = this . handler . onquery ( x ) )
51
+ this . rejecter ( x )
52
+ }
53
+
39
54
get origin ( ) {
40
55
return ( this . handler . debug
41
56
? this [ originError ] . stack
@@ -131,13 +146,25 @@ export class Query extends Promise {
131
146
return this
132
147
}
133
148
149
+ stats ( ) {
150
+ this . statistics = { started : - 1 , executed : - 1 }
151
+ return this
152
+ }
153
+
134
154
values ( ) {
135
155
this . isRaw = 'values'
136
156
return this
137
157
}
138
158
139
159
async handle ( ) {
140
- ! this . executed && ( this . executed = true ) && await 1 && this . handler ( this )
160
+ if ( this . executed )
161
+ return
162
+
163
+ this . executed = true
164
+ await 1
165
+ this . statistics && ( this . statistics . started = performance . now ( ) )
166
+ this . handler . onquery && ( this . handler . onquery = this . handler . onquery ( this ) )
167
+ this . handler ( this )
141
168
}
142
169
143
170
execute ( ) {
@@ -171,3 +198,10 @@ function cachedError(xs) {
171
198
Error . stackTraceLimit = x
172
199
return originCache . get ( xs )
173
200
}
201
+
202
+
203
+ function addStats ( query , result ) {
204
+ result . waiting = query . statistics . executed - query . statistics . started
205
+ result . duration = performance . now ( ) - query . statistics . started
206
+ result . execution = performance . now ( ) - query . statistics . executed
207
+ }
0 commit comments