/*------------------------------------------------------------------------- * * raw.c * Oracle-compatible RAW type support. * *------------------------------------------------------------------------- */ #include "postgres.h" #include "fmgr.h" #include "utils/array.h" #include "utils/builtins.h" #include "utils/varlena.h" extern Datum byteain(PG_FUNCTION_ARGS); extern Datum byteaout(PG_FUNCTION_ARGS); extern Datum bytearecv(PG_FUNCTION_ARGS); extern Datum byteasend(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(raw_input); PG_FUNCTION_INFO_V1(raw_out); PG_FUNCTION_INFO_V1(raw_recv); PG_FUNCTION_INFO_V1(raw_send); PG_FUNCTION_INFO_V1(raw_typmodin); PG_FUNCTION_INFO_V1(raw_typmodout); PG_FUNCTION_INFO_V1(raw); static void raw_check_length(const struct varlena *value, int32 typmod) { if (typmod >= 0 && VARSIZE_ANY_EXHDR(value) > typmod) ereport(ERROR, (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION), errmsg("value too long for type RAW(%d)", typmod))); } Datum raw_input(PG_FUNCTION_ARGS) { Datum result; int32 typmod = PG_GETARG_INT32(2); result = DirectFunctionCall1(byteain, PG_GETARG_DATUM(0)); raw_check_length((struct varlena *) DatumGetPointer(result), typmod); PG_RETURN_DATUM(result); } Datum raw_out(PG_FUNCTION_ARGS) { return byteaout(fcinfo); } Datum raw_recv(PG_FUNCTION_ARGS) { Datum result; int32 typmod = PG_GETARG_INT32(2); result = DirectFunctionCall1(bytearecv, PG_GETARG_DATUM(0)); raw_check_length((struct varlena *) DatumGetPointer(result), typmod); PG_RETURN_DATUM(result); } Datum raw_send(PG_FUNCTION_ARGS) { return byteasend(fcinfo); } Datum raw_typmodin(PG_FUNCTION_ARGS) { ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0); int32 *tl; int n; int32 typmod; tl = ArrayGetIntegerTypmods(ta, &n); if (n != 1) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("RAW type modifier must be a single integer"))); typmod = *tl; if (typmod < 1 || typmod > 2000) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("RAW length must be between 1 and 2000"))); PG_RETURN_INT32(typmod); } Datum raw_typmodout(PG_FUNCTION_ARGS) { int32 typmod = PG_GETARG_INT32(0); if (typmod < 0) PG_RETURN_CSTRING(pstrdup("")); PG_RETURN_CSTRING(psprintf("(%d)", typmod)); } Datum raw(PG_FUNCTION_ARGS) { struct varlena *value = PG_GETARG_VARLENA_PP(0); int32 typmod = PG_GETARG_INT32(1); raw_check_length(value, typmod); PG_RETURN_POINTER(value); }