forked from porsager/postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.js
173 lines (143 loc) · 3.52 KB
/
query.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
111
112
113
114
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
const originCache = new Map()
, originStackCache = new Map()
, originError = Symbol('OriginError')
export const CLOSE = {}
export class Query extends Promise {
constructor(strings, args, handler, canceller, options = {}) {
let resolve
, reject
super((a, b) => {
resolve = a
reject = b
})
this.tagged = Array.isArray(strings.raw)
this.strings = strings
this.args = args
this.handler = handler
this.canceller = canceller
this.options = options
this.state = null
this.statement = null
this.resolve = x => (this.active = false, resolve(x))
this.reject = x => (this.active = false, reject(x))
this.active = false
this.cancelled = null
this.executed = false
this.signature = ''
this[originError] = this.handler.debug
? new Error()
: this.tagged && cachedError(this.strings)
}
get origin() {
return (this.handler.debug
? this[originError].stack
: this.tagged && originStackCache.has(this.strings)
? originStackCache.get(this.strings)
: originStackCache.set(this.strings, this[originError].stack).get(this.strings)
) || ''
}
static get [Symbol.species]() {
return Promise
}
cancel() {
return this.canceller && (this.canceller(this), this.canceller = null)
}
simple() {
this.options.simple = true
this.options.prepare = false
return this
}
async readable() {
this.simple()
this.streaming = true
return this
}
async writable() {
this.simple()
this.streaming = true
return this
}
cursor(rows = 1, fn) {
this.options.simple = false
if (typeof rows === 'function') {
fn = rows
rows = 1
}
this.cursorRows = rows
if (typeof fn === 'function')
return (this.cursorFn = fn, this)
let prev
return {
[Symbol.asyncIterator]: () => ({
next: () => {
if (this.executed && !this.active)
return { done: true }
prev && prev()
const promise = new Promise((resolve, reject) => {
this.cursorFn = value => {
resolve({ value, done: false })
return new Promise(r => prev = r)
}
this.resolve = () => (this.active = false, resolve({ done: true }))
this.reject = x => (this.active = false, reject(x))
})
this.execute()
return promise
},
return() {
prev && prev(CLOSE)
return { done: true }
}
})
}
}
describe() {
this.options.simple = false
this.onlyDescribe = this.options.prepare = true
return this
}
stream() {
throw new Error('.stream has been renamed to .forEach')
}
forEach(fn) {
this.forEachFn = fn
this.handle()
return this
}
raw() {
this.isRaw = true
return this
}
values() {
this.isRaw = 'values'
return this
}
async handle() {
!this.executed && (this.executed = true) && await 1 && this.handler(this)
}
execute() {
this.handle()
return this
}
then() {
this.handle()
return super.then.apply(this, arguments)
}
catch() {
this.handle()
return super.catch.apply(this, arguments)
}
finally() {
this.handle()
return super.finally.apply(this, arguments)
}
}
function cachedError(xs) {
if (originCache.has(xs))
return originCache.get(xs)
const x = Error.stackTraceLimit
Error.stackTraceLimit = 4
originCache.set(xs, new Error())
Error.stackTraceLimit = x
return originCache.get(xs)
}