gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 3.39 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{TableName as n}from"../TableName.js";import{ColName as r}from"../ColName.js";import{FromBinResult as s}from"../../primitive/FromBinResult.js";import{TypeProblem as i}from"../../error/probs/TypeProblem.js";import{RangeProblem as o}from"../../error/probs/RangeProblem.js";import{ContentError as l}from"../../error/ContentError.js";import{sLen as f}from"../../safe/safe.js";import{I64 as m}from"../../primitive/number/I64.js";const u=m.fromI32s(0,2147483648),a=m.fromI32s(4294967295,4294967295);export class ARef extends t{constructor(e,t,n=!1){super(n),this.table=e,this.column=t}get cudType(){return this._cudPrefix+"("+this.table.name+"."+this.column.name+")"}cudByteSize(){return this._maxByteLen}valid(e){let t;if(null==e)return this.nullable?void 0:i.Null("Ref");if(e instanceof m)t=e;else{if(!Number.isInteger(e))return i.UnexpVal("Ref",e,"integer or Int64");t=m.fromInt(e)}return t.lt(u)||t.gt(this._max64)?o.IncInc("Ref",e,u,this._max64):void 0}toBin(){const e=super.toBin(),t=this.table.toBin(),n=this.column.toBin(),r=new Uint8Array(e.length+t.length+n.length);r.set(e);let s=e.length;return r.set(t,s),s+=t.length,r.set(n,s),r}unknownBin(e){let t;if(null==e){if(!this.nullable)throw new l("cannot be null","Ref",void 0);return new Uint8Array([0])}if(e instanceof m)t=e;else{if("number"!=typeof e||!Number.isInteger(e))throw new TypeError("Integer or Int64 required");t=m.fromInt(e)}const n=t.toBytesBE();f("i64-bytes",n).atMost(this._maxByteLen).throwNot();const r=new Uint8Array(1+n.length);return r[0]=n.length,r.set(n,1),r}binUnknown(e,t){if(t+1>e.length)return new s(0,void 0,"Ref.binUnknown unable to find length");const n=e[t++];if(0===n)return this.nullable?new s(1,void 0):new s(0,void 0,"Ref.binUnknown cannot be null");if(n>this._maxByteLen)return new s(0,void 0,`Ref.binUnknown invalid length (0<${this._maxByteLen} got ${n})`);return t+n>e.length?new s(0,void 0,"Ref.binUnknown missing data"):new s(n+1,m.fromBytesBE(e.subarray(t,t+1)))}static fromBinSub(t,i,o,l,f){switch(t){case e.Ref2:case e.Ref4:case e.Ref8:break;default:return new s(0,void 0,`ARef.fromBin invalid colType ${t}`)}let m=f;const u=n.fromBin(l,m);if(!u.success)return new s(0,void 0,"ARef.fromBinSub missing table: "+u.reason);m+=u.byteLen;const a=r.fromBin(l,m);if(!a.success)return new s(0,void 0,"ARef.fromBinSub missing column: "+a.reason);switch(m+=a.byteLen,t){case e.Ref2:return new s(o+m-f,new Ref2(u.value,a.value,i));case e.Ref4:return new s(o+m-f,new Ref4(u.value,a.value,i));case e.Ref8:return new s(o+m-f,new Ref8(u.value,a.value,i))}}}export class Ref2 extends ARef{constructor(){super(...arguments),this._colType=e.Ref2,this._maxByteLen=2,this._max64=m.fromI32s(32767,0),this._cudPrefix="ref2",this.mysqlType="SMALLINT",this.sqliteType="INT2",this.postgresType="smallint"}}export class Ref4 extends ARef{constructor(){super(...arguments),this._colType=e.Ref4,this._maxByteLen=4,this._max64=m.fromI32s(2147483647,0),this._cudPrefix="ref4",this.mysqlType="INT",this.sqliteType="INT",this.postgresType="integer"}}export class Ref8 extends ARef{constructor(){super(...arguments),this._colType=e.Ref8,this._maxByteLen=8,this._max64=a,this._cudPrefix="ref8",this.mysqlType="BIGINT",this.sqliteType="BIGINT",this.postgresType="bigint"}}