-
Notifications
You must be signed in to change notification settings - Fork 11
feat: support for DECIMAL type #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cea4280
feat: support decimal
RaphDal c684440
feat: add examples for DECIMAL data type usage in README
RaphDal a130121
feat: update decimal handling in SenderBuffer classes for protocol v3…
RaphDal 88de8a7
fix: update write methods to use little-endian format for integers an…
RaphDal 5c7d3d6
feat: enhance decimal handling in SenderBufferV3 and Sender classes t…
RaphDal 16cab31
Merge branch 'main' into rd_decimal
RaphDal 8a6422c
fix: address review comments
RaphDal 0445cdb
chore: update subproject commit reference
RaphDal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // @ts-check | ||
| import { SenderOptions } from "../options"; | ||
| import { SenderBuffer } from "./index"; | ||
| import { bigintToTwosComplementBytes } from "../utils"; | ||
| import { SenderBufferV2 } from "./bufferv2"; | ||
| import { validateDecimalText } from "../validation"; | ||
|
|
||
| // Entity type constants for protocol v3. | ||
| const ENTITY_TYPE_DECIMAL: number = 23; | ||
|
|
||
| // ASCII code for equals sign used in binary protocol. | ||
| const EQUALS_SIGN: number = "=".charCodeAt(0); | ||
|
|
||
| /** | ||
| * Buffer implementation for protocol version 3. | ||
| * | ||
| * Provides support for decimals. | ||
| */ | ||
| class SenderBufferV3 extends SenderBufferV2 { | ||
| /** | ||
| * Creates a new SenderBufferV3 instance. | ||
| * | ||
| * @param {SenderOptions} options - Sender configuration object. | ||
| * | ||
| * See SenderOptions documentation for detailed description of configuration options. | ||
| */ | ||
| constructor(options: SenderOptions) { | ||
| super(options); | ||
| } | ||
|
|
||
| /** | ||
| * Writes a decimal value into the buffer using the text format. | ||
| * | ||
| * Use it to insert into DECIMAL database columns. | ||
| * | ||
| * @param {string} name - Column name. | ||
| * @param {number} value - Column value, accepts only number/string values. | ||
| * @returns {Sender} Returns with a reference to this buffer. | ||
| * @throws Error if decimals are not supported by the buffer implementation, or decimal validation fails: | ||
| * - string value is not a valid decimal representation | ||
| */ | ||
| decimalColumnText(name: string, value: string | number): SenderBuffer { | ||
| let str = ""; | ||
| if (typeof value === "string") { | ||
| validateDecimalText(value); | ||
| str = value; | ||
| } else if (typeof value === "number") { | ||
| str = value.toString(); | ||
| } else { | ||
| throw new TypeError(`Invalid decimal value type: ${typeof value}`); | ||
| } | ||
| this.writeColumn(name, str, () => { | ||
| this.checkCapacity([str], 1); | ||
| this.write(str); | ||
| this.write("d"); | ||
| }); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Writes a decimal value into the buffer using the binary format. | ||
| * | ||
| * Use it to insert into DECIMAL database columns. | ||
| * | ||
| * @param {string} name - Column name. | ||
| * @param {number} unscaled - The unscaled value of the decimal in two's | ||
| * complement representation and big-endian byte order. | ||
| * An empty array represents the NULL value. | ||
| * @param {number} scale - The scale of the decimal value. | ||
| * @returns {Sender} Returns with a reference to this buffer. | ||
| * @throws Error if decimals are not supported by the buffer implementation, or decimal validation fails: | ||
| * - unscaled value length is not between 0 and 32 bytes | ||
| * - scale is not between 0 and 76 | ||
| * - unscaled value contains invalid bytes | ||
| */ | ||
| decimalColumn( | ||
| name: string, | ||
| unscaled: Int8Array | bigint, | ||
| scale: number, | ||
| ): SenderBuffer { | ||
| if (scale < 0 || scale > 76) { | ||
| throw new RangeError("Scale must be between 0 and 76"); | ||
| } | ||
| let arr: number[]; | ||
| if (typeof unscaled === "bigint") { | ||
| arr = bigintToTwosComplementBytes(unscaled); | ||
| } else if (unscaled instanceof Int8Array) { | ||
| arr = Array.from(unscaled); | ||
| } else { | ||
| throw new TypeError( | ||
| `Invalid unscaled value type: ${typeof unscaled}, expected Int8Array or bigint`, | ||
| ); | ||
| } | ||
| if (arr.length > 32) { | ||
| throw new RangeError( | ||
| "Unscaled value length must be between 0 and 32 bytes", | ||
| ); | ||
| } | ||
| this.writeColumn(name, unscaled, () => { | ||
| this.checkCapacity([], 4 + arr.length); | ||
| this.writeByte(EQUALS_SIGN); | ||
| this.writeByte(ENTITY_TYPE_DECIMAL); | ||
| this.writeByte(scale); | ||
| this.writeByte(arr.length); | ||
| for (let i = 0; i < arr.length; i++) { | ||
| let byte = arr[i]; | ||
| if (byte > 255 || byte < -128) { | ||
| throw new RangeError( | ||
| `Unscaled value contains invalid byte [index=${i}, value=${byte}]`, | ||
| ); | ||
| } | ||
| if (byte > 127) { | ||
| byte -= 256; | ||
| } | ||
| this.writeByte(byte); | ||
| } | ||
| }); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| export { SenderBufferV3 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.