rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
55 lines • 1.77 kB
JavaScript
const SERIAL_PSEUDO_TYPE_MAP = {
serial: 'integer',
serial4: 'integer',
bigserial: 'bigint',
serial8: 'bigint',
smallserial: 'smallint',
serial2: 'smallint',
};
/**
* Converts Postgres serial pseudo-type names into their real storage types so ZTD
* cast expressions never emit non-existent types.
*
* @param typeName Optional type name (possibly qualified) that is trimmed before normalization.
* @returns The normalized type name when a pseudo-type matches, the trimmed input when no match is found, or `undefined` when the argument is `undefined`.
* @example
* normalizeSerialPseudoType('serial') // 'integer'
* normalizeSerialPseudoType('public.BIGSERIAL') // 'public.bigint'
*/
export function normalizeSerialPseudoType(typeName) {
if (!typeName) {
return typeName;
}
const trimmed = typeName.trim();
if (!trimmed) {
return trimmed;
}
// Preserve schema or catalog qualifiers while normalizing the terminal segment.
const segments = trimmed.split('.');
const base = segments.pop();
const normalizedBase = SERIAL_PSEUDO_TYPE_MAP[base.toLowerCase()];
if (!normalizedBase) {
return trimmed;
}
segments.push(normalizedBase);
return segments.join('.');
}
/**
* Returns true when the provided type name resolves to a known serial pseudo-type.
*/
export function isSerialPseudoType(typeName) {
if (!typeName) {
return false;
}
const trimmed = typeName.trim();
if (!trimmed) {
return false;
}
const segments = trimmed.split('.');
const base = segments.pop();
if (!base) {
return false;
}
return Boolean(SERIAL_PSEUDO_TYPE_MAP[base.toLowerCase()]);
}
//# sourceMappingURL=serialTypeNormalization.js.map