|
| 1 | +// @ts-check |
| 2 | +import { SenderOptions } from "../options"; |
| 3 | +import { SenderBuffer } from "./index"; |
| 4 | +import { bigintToTwosComplementBytes } from "../utils"; |
| 5 | +import { SenderBufferV2 } from "./bufferv2"; |
| 6 | +import { validateDecimalText } from "../validation"; |
| 7 | + |
| 8 | +// Entity type constants for protocol v3. |
| 9 | +const ENTITY_TYPE_DECIMAL: number = 23; |
| 10 | + |
| 11 | +// ASCII code for equals sign used in binary protocol. |
| 12 | +const EQUALS_SIGN: number = "=".charCodeAt(0); |
| 13 | + |
| 14 | +/** |
| 15 | + * Buffer implementation for protocol version 3. |
| 16 | + * |
| 17 | + * Provides support for decimals. |
| 18 | + */ |
| 19 | +class SenderBufferV3 extends SenderBufferV2 { |
| 20 | + /** |
| 21 | + * Creates a new SenderBufferV3 instance. |
| 22 | + * |
| 23 | + * @param {SenderOptions} options - Sender configuration object. |
| 24 | + * |
| 25 | + * See SenderOptions documentation for detailed description of configuration options. |
| 26 | + */ |
| 27 | + constructor(options: SenderOptions) { |
| 28 | + super(options); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Writes a decimal value into the buffer using the text format. |
| 33 | + * |
| 34 | + * Use it to insert into DECIMAL database columns. |
| 35 | + * |
| 36 | + * @param {string} name - Column name. |
| 37 | + * @param {number} value - Column value, accepts only number/string values. |
| 38 | + * @returns {Sender} Returns with a reference to this buffer. |
| 39 | + * @throws Error if decimals are not supported by the buffer implementation, or decimal validation fails: |
| 40 | + * - string value is not a valid decimal representation |
| 41 | + */ |
| 42 | + decimalColumnText(name: string, value: string | number): SenderBuffer { |
| 43 | + let str = ""; |
| 44 | + if (typeof value === "string") { |
| 45 | + validateDecimalText(value); |
| 46 | + str = value; |
| 47 | + } else if (typeof value === "number") { |
| 48 | + str = value.toString(); |
| 49 | + } else { |
| 50 | + throw new TypeError(`Invalid decimal value type: ${typeof value}`); |
| 51 | + } |
| 52 | + this.writeColumn(name, str, () => { |
| 53 | + this.checkCapacity([str], 1); |
| 54 | + this.write(str); |
| 55 | + this.write("d"); |
| 56 | + }); |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Writes a decimal value into the buffer using the binary format. |
| 62 | + * |
| 63 | + * Use it to insert into DECIMAL database columns. |
| 64 | + * |
| 65 | + * @param {string} name - Column name. |
| 66 | + * @param {number} unscaled - The unscaled value of the decimal in two's |
| 67 | + * complement representation and big-endian byte order. |
| 68 | + * An empty array represents the NULL value. |
| 69 | + * @param {number} scale - The scale of the decimal value. |
| 70 | + * @returns {Sender} Returns with a reference to this buffer. |
| 71 | + * @throws Error if decimals are not supported by the buffer implementation, or decimal validation fails: |
| 72 | + * - unscaled value length is not between 0 and 32 bytes |
| 73 | + * - scale is not between 0 and 76 |
| 74 | + * - unscaled value contains invalid bytes |
| 75 | + */ |
| 76 | + decimalColumn( |
| 77 | + name: string, |
| 78 | + unscaled: Int8Array | bigint, |
| 79 | + scale: number, |
| 80 | + ): SenderBuffer { |
| 81 | + if (scale < 0 || scale > 76) { |
| 82 | + throw new RangeError("Scale must be between 0 and 76"); |
| 83 | + } |
| 84 | + let arr: number[]; |
| 85 | + if (typeof unscaled === "bigint") { |
| 86 | + arr = bigintToTwosComplementBytes(unscaled); |
| 87 | + } else if (unscaled instanceof Int8Array) { |
| 88 | + arr = Array.from(unscaled); |
| 89 | + } else { |
| 90 | + throw new TypeError( |
| 91 | + `Invalid unscaled value type: ${typeof unscaled}, expected Int8Array or bigint`, |
| 92 | + ); |
| 93 | + } |
| 94 | + if (arr.length > 32) { |
| 95 | + throw new RangeError( |
| 96 | + "Unscaled value length must be between 0 and 32 bytes", |
| 97 | + ); |
| 98 | + } |
| 99 | + this.writeColumn(name, unscaled, () => { |
| 100 | + this.checkCapacity([], 4 + arr.length); |
| 101 | + this.writeByte(EQUALS_SIGN); |
| 102 | + this.writeByte(ENTITY_TYPE_DECIMAL); |
| 103 | + this.writeByte(scale); |
| 104 | + this.writeByte(arr.length); |
| 105 | + for (let i = 0; i < arr.length; i++) { |
| 106 | + let byte = arr[i]; |
| 107 | + if (byte > 255 || byte < -128) { |
| 108 | + throw new RangeError( |
| 109 | + `Unscaled value contains invalid byte [index=${i}, value=${byte}]`, |
| 110 | + ); |
| 111 | + } |
| 112 | + if (byte > 127) { |
| 113 | + byte -= 256; |
| 114 | + } |
| 115 | + this.writeByte(byte); |
| 116 | + } |
| 117 | + }); |
| 118 | + return this; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export { SenderBufferV3 }; |
0 commit comments