-
-
Notifications
You must be signed in to change notification settings - Fork 629
/
Copy pathtest-binary-multiple-results.js
207 lines (185 loc) · 5.53 KB
/
test-binary-multiple-results.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// This file was modified by Oracle on June 2, 2021.
// The test has been updated to remove all expectations with regards to the
// "columnLength" metadata field.
// Modifications copyright (c) 2021, Oracle and/or its affiliates.
'use strict';
const mysql = require('../../common.js').createConnection({
multipleStatements: true
});
const assert = require('assert-diff');
mysql.query('CREATE TEMPORARY TABLE no_rows (test int)');
mysql.query('CREATE TEMPORARY TABLE some_rows (test int)');
mysql.query('INSERT INTO some_rows values(0)');
mysql.query('INSERT INTO some_rows values(42)');
mysql.query('INSERT INTO some_rows values(314149)');
const clone = function(obj) {
return JSON.parse(JSON.stringify(obj));
};
const rs1 = {
affectedRows: 0,
fieldCount: 0,
insertId: 0,
serverStatus: 10,
warningStatus: 0,
info: ''
};
const rs2 = clone(rs1);
rs2.serverStatus = 2;
const rs3 = clone(rs1);
rs3.serverStatus = 34;
const select1 = [{ '1': '1' }];
const select2 = [{ '2': '2' }];
const fields1 = [
{
catalog: 'def',
characterSet: 63,
columnType: 8,
decimals: 0,
flags: 129,
name: '1',
orgName: '',
orgTable: '',
schema: '',
table: ''
}
];
const nr_fields = [
{
catalog: 'def',
characterSet: 63,
columnType: 3,
decimals: 0,
flags: 0,
name: 'test',
orgName: 'test',
orgTable: 'no_rows',
schema: mysql.config.database,
table: 'no_rows'
}
];
const sr_fields = clone(nr_fields);
sr_fields[0].orgTable = 'some_rows';
sr_fields[0].table = 'some_rows';
const select3 = [{ test: 0 }, { test: 42 }, { test: 314149 }];
const fields2 = clone(fields1);
fields2[0].name = '2';
const tests = [
['select * from some_rows', [[select3, rs3], [sr_fields, undefined], 2]], // select 3 rows
[
'SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT; SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS',
[rs2, undefined, 1]
],
['set @a = 1', [rs2, undefined, 1]],
['set @a = 1; set @b = 2', [rs2, undefined, 1]],
[
'select 1; select 2',
[[select1, select2, rs2], [fields1, fields2, undefined], 3]
],
['set @a = 1; select 1', [[select1, rs2], [fields1, undefined], 2]],
['select 1; set @a = 1', [[select1, rs2], [fields1, undefined], 2]],
['select * from no_rows', [[[], rs3], [nr_fields, undefined], 2]], // select 0 rows"
['set @a = 1; select * from no_rows', [[[], rs3], [nr_fields, undefined], 2]], // insert + select 0 rows
['select * from no_rows; set @a = 1', [[[], rs3], [nr_fields, undefined], 2]], // select 0 rows + insert
[
'set @a = 1; select * from some_rows',
[[select3, rs3], [sr_fields, undefined], 2]
], // insert + select 3 rows
[
'select * from some_rows; set @a = 1',
[[select3, rs3], [sr_fields, undefined], 2]
] // select 3 rows + insert
];
function procedurise(sql) {
return [
'DROP PROCEDURE IF EXISTS _as_sp_call;',
'CREATE PROCEDURE _as_sp_call()',
'BEGIN',
`${sql};`,
'END'
].join('\n');
}
function do_test(testIndex) {
const next = function() {
if (testIndex + 1 < tests.length) {
do_test(testIndex + 1);
} else {
mysql.end();
}
};
const entry = tests[testIndex];
let sql = entry[0];
const expectation = entry[1];
// prepared statements do not support multiple statements itself, we need to wrap quey in a stored procedure
const sp = procedurise(sql);
mysql.query(sp, err => {
if (err) {
throw err;
}
sql = 'CALL _as_sp_call()'; // this call is allowed with prepared statements, and result contain multiple statements
let _numResults = 0;
const textCmd = mysql.query(sql, (err, _rows, _columns) => {
if (err) {
throw err;
}
const arrOrColumn = function(c) {
if (Array.isArray(c)) {
return c.map(arrOrColumn);
}
if (typeof c === 'undefined') {
return void 0;
}
const column = c.inspect();
// "columnLength" is non-deterministic and the display width for integer
// data types was deprecated on MySQL 8.0.17.
// https://dev.mysql.com/doc/refman/8.0/en/numeric-type-syntax.html
delete column.columnLength;
return column;
};
assert.deepEqual(expectation[0], _rows);
assert.deepEqual(expectation[1], arrOrColumn(_columns));
const q = mysql.execute(sql);
let resIndex = 0;
let rowIndex = 0;
let fieldIndex = -1;
function checkRow(row) {
const index = fieldIndex;
if (_numResults === 1) {
assert.equal(index, 0);
if (row.constructor.name === 'ResultSetHeader') {
assert.deepEqual(_rows, row);
} else {
assert.deepEqual(_rows[rowIndex], row);
}
} else {
if (resIndex !== index) {
rowIndex = 0;
resIndex = index;
}
if (row.constructor.name === 'ResultSetHeader') {
assert.deepEqual(_rows[index], row);
} else {
assert.deepEqual(_rows[index][rowIndex], row);
}
}
rowIndex++;
}
function checkFields(fields) {
fieldIndex++;
const index = fieldIndex;
if (_numResults === 1) {
assert.equal(index, 0);
assert.deepEqual(arrOrColumn(_columns), arrOrColumn(fields));
} else {
assert.deepEqual(arrOrColumn(_columns[index]), arrOrColumn(fields));
}
}
q.on('result', checkRow);
q.on('fields', checkFields);
q.on('end', next);
});
textCmd.on('fields', () => {
_numResults++;
});
});
}
do_test(0);