|
1 | 1 | import { Config } from './config'; |
2 | 2 | import type { Column, QueryResult, Row, Rows, SQParam } from './skytable'; |
| 3 | +import { Query } from './query'; |
3 | 4 |
|
4 | 5 | const PARAMS_TYPE = { |
5 | 6 | NULL: '\x00', |
@@ -38,67 +39,60 @@ const HANDSHAKE_RESULT = { |
38 | 39 | ERROR: 'H01', |
39 | 40 | }; |
40 | 41 |
|
41 | | -function isResponsesResult(type: number): boolean { |
42 | | - return Object.values(RESPONSES_RESULT).includes(type); |
43 | | -} |
44 | | - |
45 | 42 | function isFloat(number: number | string): boolean { |
46 | 43 | return Number.isFinite(number) && !Number.isInteger(number); |
47 | 44 | } |
48 | 45 |
|
49 | | -export function encodeParams(parameters: SQParam[]): string { |
50 | | - if (!parameters.length) { |
51 | | - return ''; |
| 46 | +export function encodeParam(param: SQParam): string { |
| 47 | + // 5 A binary blob [5<size>\n<payload>] |
| 48 | + if (Buffer.isBuffer(param)) { |
| 49 | + return [PARAMS_TYPE.BINARY, param.length, '\n', param.toString()].join(''); |
52 | 50 | } |
53 | 51 |
|
54 | | - return parameters |
55 | | - .map((param) => { |
56 | | - switch (typeof param) { |
57 | | - case 'string': |
58 | | - return [PARAMS_TYPE.STRING, param.length, '\n', param].join(''); |
59 | | - case 'number': |
60 | | - // 2 Unsigned integer 64 |
61 | | - // 3 Signed integer 64 |
62 | | - // 4 Float A 64-bit |
63 | | - return [ |
64 | | - isFloat(param) |
65 | | - ? PARAMS_TYPE.FLOAT |
66 | | - : param < 0 |
67 | | - ? PARAMS_TYPE.SINT |
68 | | - : PARAMS_TYPE.UINT, |
69 | | - String(param), |
70 | | - '\n', |
71 | | - ].join(''); |
72 | | - case 'bigint': |
73 | | - return [ |
74 | | - param < 0 ? PARAMS_TYPE.SINT : PARAMS_TYPE.UINT, |
75 | | - String(param), |
76 | | - '\n', |
77 | | - ].join(''); |
78 | | - case 'boolean': |
79 | | - return [PARAMS_TYPE.BOOLEAN, Number(param) === 1 ? '\x01' : 0].join( |
80 | | - '', |
81 | | - ); |
82 | | - default: |
83 | | - // 5 A binary blob [5<size>\n<payload>] |
84 | | - if (Buffer.isBuffer(param)) { |
85 | | - return [ |
86 | | - PARAMS_TYPE.BINARY, |
87 | | - param.length, |
88 | | - '\n', |
89 | | - param.toString(), |
90 | | - ].join(''); |
91 | | - } |
92 | | - // null undefined |
93 | | - if (param == null) { |
94 | | - return '\x00'; |
95 | | - } |
96 | | - throw new TypeError( |
97 | | - `un support type: ${typeof param}, val: ${param}`, |
98 | | - ); |
99 | | - } |
100 | | - }) |
101 | | - .join(''); |
| 52 | + // null undefined |
| 53 | + if (param == null) { |
| 54 | + return '\x00'; |
| 55 | + } |
| 56 | + |
| 57 | + switch (typeof param) { |
| 58 | + case 'string': |
| 59 | + return [PARAMS_TYPE.STRING, param.length, '\n', param].join(''); |
| 60 | + case 'number': |
| 61 | + // 2 Unsigned integer 64 |
| 62 | + // 3 Signed integer 64 |
| 63 | + // 4 Float A 64-bit |
| 64 | + return [ |
| 65 | + isFloat(param) |
| 66 | + ? PARAMS_TYPE.FLOAT |
| 67 | + : param < 0 |
| 68 | + ? PARAMS_TYPE.SINT |
| 69 | + : PARAMS_TYPE.UINT, |
| 70 | + String(param), |
| 71 | + '\n', |
| 72 | + ].join(''); |
| 73 | + case 'bigint': |
| 74 | + return [ |
| 75 | + param < 0 ? PARAMS_TYPE.SINT : PARAMS_TYPE.UINT, |
| 76 | + String(param), |
| 77 | + '\n', |
| 78 | + ].join(''); |
| 79 | + case 'boolean': |
| 80 | + return [PARAMS_TYPE.BOOLEAN, Number(param) === 1 ? '\x01' : 0].join(''); |
| 81 | + default: |
| 82 | + throw new TypeError(`un support type: ${typeof param}, val: ${param}`); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export function encodeParams(parameters: SQParam[]): string { |
| 87 | + return parameters.map(encodeParam).join(''); |
| 88 | +} |
| 89 | + |
| 90 | +export function encodeQuery(query: Query): Buffer { |
| 91 | + const dataframe = `${query.getQuery()}${query.getParams().join('')}`; |
| 92 | + const data = [query.getQueryLength(), '\n', dataframe]; |
| 93 | + const requestData = ['S', data.join('').length, '\n', ...data]; |
| 94 | + |
| 95 | + return Buffer.from(requestData.join(''), 'utf-8'); |
102 | 96 | } |
103 | 97 |
|
104 | 98 | function getFirstSplitOffset(buffer: Buffer, split = '\n'): number { |
@@ -251,13 +245,12 @@ export function formatResponse(buffer: Buffer): QueryResult { |
251 | 245 | `response error code: ${buffer.subarray(1, 2).readInt8()}`, |
252 | 246 | ); |
253 | 247 | default: |
254 | | - if (isResponsesResult(type)) { |
255 | | - const [val] = parseSingleVal(buffer); |
256 | | - |
257 | | - return val; |
258 | | - } |
259 | | - throw new TypeError('unknown response type'); |
| 248 | + break; |
260 | 249 | } |
| 250 | + |
| 251 | + const [val] = parseSingleVal(buffer); |
| 252 | + |
| 253 | + return val; |
261 | 254 | } |
262 | 255 |
|
263 | 256 | export function getClientHandshake(config: Config): string { |
|
0 commit comments