/* * oidvectortypes - converts a vector of type OIDs to "typname" list */ datum_t oidvectortypes(PG_FUNC_ARGS) { oid_vector_s *oidArray = (oid_vector_s *) ARG_POINTER(0); char *result; int numargs = oidArray->dim1; int num; size_t total; size_t left; total = 20 * numargs + 1; result = palloc(total); result[0] = '\0'; left = total - 1; for (num = 0; num < numargs; num++) { char *typename = format_type_internal(oidArray->values[num], -1, false, true); size_t slen = strlen(typename); if (left < (slen + 2)) { total += slen + 2; result = repalloc(result, total); left += slen + 2; } if (num > 0) { strcat(result, ", "); left -= 2; } strcat(result, typename); left -= slen; } RET_TEXT_P(cstring_to_text(result)); }
/* * SQL function: format_type(type_oid, typemod) * * `type_oid' is from pg_type.oid, `typemod' is from * pg_attribute.atttypmod. This function will get the type name and * format it and the modifier to canonical SQL format, if the type is * a standard type. Otherwise you just get pg_type.typname back, * double quoted if it contains funny characters or matches a keyword. * * If typemod is NULL then we are formatting a type name in a context where * no typemod is available, eg a function argument or result type. This * yields a slightly different result from specifying typemod = -1 in some * cases. Given typemod = -1 we feel compelled to produce an output that * the parser will interpret as having typemod -1, so that pg_dump will * produce CREATE TABLE commands that recreate the original state. But * given NULL typemod, we assume that the parser's interpretation of * typemod doesn't matter, and so we are willing to output a slightly * "prettier" representation of the same type. For example, type = bpchar * and typemod = NULL gets you "character", whereas typemod = -1 gets you * "bpchar" --- the former will be interpreted as character(1) by the * parser, which does not yield typemod -1. * * XXX encoding a meaning in typemod = NULL is ugly; it'd have been * cleaner to make two functions of one and two arguments respectively. * Not worth changing it now, however. */ datum_t format_type(PG_FUNC_ARGS) { oid_t type_oid; int32 typemod; char *result; /* Since this function is not strict, we must test for null args */ if (PG_ARG_ISNULL(0)) RET_NULL(); type_oid = ARG_OID(0); if (PG_ARG_ISNULL(1)) { result = format_type_internal(type_oid, -1, false, true); } else { typemod = ARG_INT32(1); result = format_type_internal(type_oid, typemod, true, true); } RET_TEXT_P(cstring_to_text(result)); }
/* * SQL function: format_type(type_oid, typemod) * * `type_oid' is from pg_type.oid, `typemod' is from * pg_attribute.atttypmod. This function will get the type name and * format it and the modifier to canonical SQL format, if the type is * a standard type. Otherwise you just get pg_type.typname back, * double quoted if it contains funny characters or matches a keyword. * * If typemod is NULL then we are formatting a type name in a context where * no typemod is available, eg a function argument or result type. This * yields a slightly different result from specifying typemod = -1 in some * cases. Given typemod = -1 we feel compelled to produce an output that * the parser will interpret as having typemod -1, so that pg_dump will * produce CREATE TABLE commands that recreate the original state. But * given NULL typemod, we assume that the parser's interpretation of * typemod doesn't matter, and so we are willing to output a slightly * "prettier" representation of the same type. For example, type = bpchar * and typemod = NULL gets you "character", whereas typemod = -1 gets you * "bpchar" --- the former will be interpreted as character(1) by the * parser, which does not yield typemod -1. * * XXX encoding a meaning in typemod = NULL is ugly; it'd have been * cleaner to make two functions of one and two arguments respectively. * Not worth changing it now, however. */ Datum format_type(PG_FUNCTION_ARGS) { Oid type_oid; int32 typemod; char *result; /* Since this function is not strict, we must test for null args */ if (PG_ARGISNULL(0)) PG_RETURN_NULL(); type_oid = PG_GETARG_OID(0); if (PG_ARGISNULL(1)) result = format_type_internal(type_oid, -1, false, true); else { typemod = PG_GETARG_INT32(1); result = format_type_internal(type_oid, typemod, true, true); } PG_RETURN_DATUM(_textin(result)); }
/* * This version is for use within the backend in error messages, etc. * One difference is that it will fail for an invalid type. * * The result is always a palloc'd string. */ char * format_type_be(Oid type_oid) { return format_type_internal(type_oid, -1, false, false); }
/* * This version allows a nondefault typemod to be specified. */ char * format_type_with_typemod(Oid type_oid, int32 typemod) { return format_type_internal(type_oid, typemod, true, false); }
char * format_type_be_qualified(Oid type_oid) { return format_type_internal(type_oid, -1, false, false, true); }