gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 2.38 kB
JavaScript
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */
import{ColType as e}from"./ColType.js";import{ACudColType as t}from"./ACudColType.js";import{FromBinResult as n}from"../../primitive/FromBinResult.js";import{TypeProblem as s}from"../../error/probs/TypeProblem.js";import{RangeProblem as r}from"../../error/probs/RangeProblem.js";import{sLen as i}from"../../safe/safe.js";import{U64 as o}from"../../primitive/number/U64.js";const I=o.zero,m=o.fromI32s(4294967295,4294967295);class T extends t{constructor(){super(!1)}valid(e){let t;if(e instanceof o)t=e;else{if(!Number.isInteger(e))return s.UnexpVal("input",e,"integer or Int64");t=o.fromInt(e)}if(t.lt(I)||t.gt(this._max64))return r.IncInc("Id",e,I,this._max64)}cudByteSize(){return this._maxByteLen}unknownBin(e){let t;if(e instanceof o)t=e;else{if("number"!=typeof e||!Number.isInteger(e))throw new TypeError("Integer or U64 required");t=o.fromInt(e)}const n=t.toBytesBE();i("i64-bytes",n).atMost(this._maxByteLen).throwNot();const s=new Uint8Array(1+n.length);return s[0]=n.length,s.set(n,1),s}binUnknown(e,t){if(t+1>e.length)return new n(0,void 0,"Id.binUnknown unable to find length");const s=e[t++];if(0===s)return new n(0,void 0,"Id.binUnknown cannot be null");if(s>this._maxByteLen)return new n(0,void 0,`Id.binUnknown invalid length (0<${this._maxByteLen} got ${s})`);return t+s>e.length?new n(0,void 0,"Id.binUnknown missing data"):new n(s+1,o.fromBytesBE(e,t))}}export class Id2 extends T{constructor(){super(...arguments),this._colType=e.Id2,this._maxByteLen=2,this._max64=o.fromI32s(32767,0),this.mysqlType="SMALLINT AUTO_INCREMENT PRIMARY KEY",this.sqliteType="INT2 PRIMARY KEY NOT NULL",this.postgresType="smallint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY",this.cudType="id2"}}export class Id4 extends T{constructor(){super(...arguments),this._colType=e.Id4,this._maxByteLen=4,this._max64=o.fromI32s(2147483647,0),this.mysqlType="INT AUTO_INCREMENT PRIMARY KEY",this.sqliteType="INT PRIMARY KEY NOT NULL",this.postgresType="int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY",this.cudType="id4"}}export class Id8 extends T{constructor(){super(...arguments),this._colType=e.Id8,this._maxByteLen=8,this._max64=m,this.mysqlType="BIGINT AUTO_INCREMENT PRIMARY KEY",this.sqliteType="BIGINT PRIMARY KEY NOT NULL",this.postgresType="bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY",this.cudType="id8"}}