MCPcopy Create free account
hub / github.com/denodrivers/postgres / decode

Function decode

query/decode.ts:211–259  ·  view source on GitHub ↗
(
  value: Uint8Array,
  column: Column,
  controls?: ClientControls,
)

Source from the content-addressed store, hash-verified

209}
210
211export function decode(
212 value: Uint8Array,
213 column: Column,
214 controls?: ClientControls,
215) {
216 const strValue = decoder.decode(value);
217
218 // check if there is a custom decoder
219 if (controls?.decoders) {
220 const oidType = OidTypes[column.typeOid as OidValue];
221 // check if there is a custom decoder by oid (number) or by type name (string)
222 const decoderFunc = controls.decoders?.[column.typeOid] ||
223 controls.decoders?.[oidType];
224
225 if (decoderFunc) {
226 return decoderFunc(strValue, column.typeOid, parseArray);
227 } // if no custom decoder is found and the oid is for an array type, check if there is
228 // a decoder for the base type and use that with the array parser
229 else if (oidType?.includes("_array")) {
230 const baseOidType = oidType.replace("_array", "") as OidType;
231 // check if the base type is in the Oid object
232 if (baseOidType in Oid) {
233 // check if there is a custom decoder for the base type by oid (number) or by type name (string)
234 const decoderFunc = controls.decoders?.[Oid[baseOidType]] ||
235 controls.decoders?.[baseOidType];
236 if (decoderFunc) {
237 return parseArray(
238 strValue,
239 (value: string) => decoderFunc(value, column.typeOid, parseArray),
240 );
241 }
242 }
243 }
244 }
245
246 // check if the decode strategy is `string`
247 if (controls?.decodeStrategy === "string") {
248 return strValue;
249 }
250
251 // else, default to 'auto' mode, which uses the typeOid to determine the decoding strategy
252 if (column.format === Format.BINARY) {
253 return decodeBinary();
254 } else if (column.format === Format.TEXT) {
255 return decodeText(strValue, column.typeOid);
256 } else {
257 throw new Error(`Unknown column format: ${column.format}`);
258 }
259}

Callers 3

insertRowMethod · 0.90
insertRowMethod · 0.90
decode_test.tsFile · 0.90

Calls 3

parseArrayFunction · 0.90
decodeBinaryFunction · 0.85
decodeTextFunction · 0.85

Tested by

no test coverage detected