@@ -6,16 +6,16 @@ const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
6
6
module . exports = Backend
7
7
8
8
function Backend ( {
9
+ onparameter,
9
10
parsers,
10
11
onauth,
11
12
onready,
12
13
resolve,
13
14
reject,
15
+ transform,
14
16
onnotice,
15
17
onnotify
16
18
} ) {
17
- let result = null
18
- let columns = null
19
19
let rows = 0
20
20
21
21
const backend = Object . entries ( {
@@ -47,24 +47,15 @@ function Backend({
47
47
48
48
const state = backend . state = {
49
49
status : 'I' ,
50
- settings : { } ,
51
50
pid : null ,
52
51
secret : null
53
52
}
54
53
55
54
return backend
56
55
57
- function ParseComplete ( ) {
58
- // No handling needed
59
- }
60
-
61
- function BindComplete ( ) {
62
- // No handling needed
63
- }
64
-
65
- function CloseComplete ( ) {
66
- // No handling needed
67
- }
56
+ function ParseComplete ( ) { /* No handling needed */ }
57
+ function BindComplete ( ) { /* No handling needed */ }
58
+ function CloseComplete ( ) { /* No handling needed */ }
68
59
69
60
function NotificationResponse ( x ) {
70
61
if ( ! onnotify )
@@ -79,27 +70,34 @@ function Backend({
79
70
}
80
71
81
72
function CommandComplete ( x ) {
82
- backend . query && resolve ( backend . query . stream
83
- ? rows + 1
84
- : result
73
+ if ( ! backend . query )
74
+ return
75
+
76
+ for ( let i = x . length - 1 ; i > 0 ; i -- ) {
77
+ if ( x [ i ] === 32 && x [ i + 1 ] < 58 && backend . query . result . count === null )
78
+ backend . query . result . count = + x . utf8Slice ( i + 1 , x . length - 1 ) // eslint-disable-line
79
+ if ( x [ i - 1 ] >= 65 ) {
80
+ backend . query . result . command = x . utf8Slice ( 5 , i )
81
+ break
82
+ }
83
+ }
84
+
85
+ resolve ( backend . query . stream
86
+ ? backend . query . result . count
87
+ : backend . query . result
85
88
)
86
- result = null
87
- columns = null
88
- rows = 0
89
89
}
90
90
91
- function CopyDone ( x ) {
92
- // No handling needed
93
- }
91
+ function CopyDone ( x ) { /* No handling needed */ }
94
92
95
93
function DataRow ( x ) {
96
94
let index = 7
97
95
let length
98
96
let column
99
97
100
98
const row = { }
101
- for ( let i = 0 ; i < columns . length ; i ++ ) {
102
- column = columns [ i ]
99
+ for ( let i = 0 ; i < backend . query . statement . columns . length ; i ++ ) {
100
+ column = backend . query . statement . columns [ i ]
103
101
length = x . readInt32BE ( index )
104
102
index += 4
105
103
@@ -114,12 +112,10 @@ function Backend({
114
112
115
113
backend . query . stream
116
114
? backend . query . stream ( row , rows ++ )
117
- : result . push ( row )
115
+ : backend . query . result . push ( row )
118
116
}
119
117
120
- function CopyData ( x ) {
121
- // No handling needed until implemented
122
- }
118
+ function CopyData ( x ) { /* No handling needed until implemented */ }
123
119
124
120
function ErrorResponse ( x ) {
125
121
reject ( errors . generic ( error ( x ) ) )
@@ -133,22 +129,20 @@ function Backend({
133
129
reject ( errors . notSupported ( 'CopyOutResponse' ) )
134
130
}
135
131
136
- function EmptyQueryResponse ( ) {
137
- // No handling needed
138
- }
132
+ function EmptyQueryResponse ( ) { /* No handling needed */ }
139
133
140
134
function BackendKeyData ( x ) {
141
135
state . pid = x . readInt32BE ( 5 )
142
136
state . secret = x . readInt32BE ( 9 )
143
137
}
144
138
145
139
function NoticeResponse ( x ) {
146
- onnotice ( error ( x ) )
140
+ onnotice
141
+ ? onnotice ( error ( x ) )
142
+ : console . log ( error ( x ) )
147
143
}
148
144
149
- function NoData ( x ) {
150
- // No handling needed
151
- }
145
+ function NoData ( x ) { /* No handling needed */ }
152
146
153
147
function Authentication ( x ) {
154
148
const type = x . readInt32BE ( 5 )
@@ -157,7 +151,7 @@ function Backend({
157
151
158
152
function ParameterStatus ( x ) {
159
153
const [ k , v ] = x . utf8Slice ( 5 , x . length - 1 ) . split ( N )
160
- state . settings [ k ] = v
154
+ onparameter ( k , v )
161
155
}
162
156
163
157
function PortalSuspended ( x ) {
@@ -169,19 +163,22 @@ function Backend({
169
163
}
170
164
171
165
function RowDescription ( x ) {
166
+ rows = 0
167
+
168
+ if ( backend . query . statement . columns )
169
+ return
170
+
172
171
const length = x . readInt16BE ( 5 )
173
172
let index = 7
174
173
let start
175
174
176
- columns = Array ( length )
177
- result = [ ]
178
- rows = 0
175
+ backend . query . statement . columns = Array ( length )
179
176
180
177
for ( let i = 0 ; i < length ; ++ i ) {
181
178
start = index
182
179
while ( x [ index ++ ] !== 0 ) ;
183
- columns [ i ] = {
184
- n : x . utf8Slice ( start , index - 1 ) ,
180
+ backend . query . statement . columns [ i ] = {
181
+ n : transform ( x . utf8Slice ( start , index - 1 ) ) ,
185
182
p : parsers [ x . readInt32BE ( index + 6 ) ]
186
183
}
187
184
index += 18
0 commit comments