2000-08-21 20:23:18 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* format_type.c
|
|
|
|
* Display type names "nicely".
|
|
|
|
*
|
|
|
|
*
|
2010-01-02 17:58:17 +01:00
|
|
|
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
|
2000-08-21 20:23:18 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-01-02 17:58:17 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/format_type.c,v 1.52 2010/01/02 16:57:53 momjian Exp $
|
2000-08-21 20:23:18 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-07-07 21:24:43 +02:00
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2002-05-02 01:06:41 +02:00
|
|
|
#include "catalog/namespace.h"
|
2000-07-07 21:24:43 +02:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "utils/builtins.h"
|
2002-05-02 01:06:41 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2006-07-11 18:35:33 +02:00
|
|
|
#include "utils/numeric.h"
|
2000-07-07 21:24:43 +02:00
|
|
|
#include "utils/syscache.h"
|
2001-09-21 17:27:38 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
|
2000-07-07 21:24:43 +02:00
|
|
|
#define MAX_INT32_LEN 11
|
|
|
|
|
2001-11-19 20:51:20 +01:00
|
|
|
static char *format_type_internal(Oid type_oid, int32 typemod,
|
2002-09-04 22:31:48 +02:00
|
|
|
bool typemod_given, bool allow_invalid);
|
2006-12-30 22:21:56 +01:00
|
|
|
static char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
|
2002-09-04 22:31:48 +02:00
|
|
|
static char *
|
|
|
|
psnprintf(size_t len, const char *fmt,...)
|
2001-11-19 20:51:20 +01:00
|
|
|
/* This lets gcc check the format string for consistency. */
|
|
|
|
__attribute__((format(printf, 2, 3)));
|
2000-07-07 21:24:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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,
|
2002-05-02 01:06:41 +02:00
|
|
|
* double quoted if it contains funny characters or matches a keyword.
|
2000-07-07 21:24:43 +02:00
|
|
|
*
|
2001-11-19 20:51:20 +01:00
|
|
|
* If typemod is NULL then we are formatting a type name in a context where
|
2002-09-04 22:31:48 +02:00
|
|
|
* no typemod is available, eg a function argument or result type. This
|
2001-11-19 20:51:20 +01:00
|
|
|
* 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
|
2002-09-04 22:31:48 +02:00
|
|
|
* produce CREATE TABLE commands that recreate the original state. But
|
2001-11-19 20:51:20 +01:00
|
|
|
* given NULL typemod, we assume that the parser's interpretation of
|
|
|
|
* typemod doesn't matter, and so we are willing to output a slightly
|
2002-09-04 22:31:48 +02:00
|
|
|
* "prettier" representation of the same type. For example, type = bpchar
|
2001-11-19 20:51:20 +01:00
|
|
|
* 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.
|
2000-07-07 21:24:43 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
format_type(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2000-07-09 23:30:21 +02:00
|
|
|
Oid type_oid;
|
2000-08-21 20:23:18 +02:00
|
|
|
int32 typemod;
|
2000-07-09 23:30:21 +02:00
|
|
|
char *result;
|
2000-07-07 21:24:43 +02:00
|
|
|
|
2001-11-19 20:51:20 +01:00
|
|
|
/* Since this function is not strict, we must test for null args */
|
2000-07-07 21:24:43 +02:00
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2000-08-21 20:23:18 +02:00
|
|
|
type_oid = PG_GETARG_OID(0);
|
2000-07-07 21:24:43 +02:00
|
|
|
|
2001-11-19 20:51:20 +01:00
|
|
|
if (PG_ARGISNULL(1))
|
|
|
|
result = format_type_internal(type_oid, -1, false, true);
|
2000-08-21 20:23:18 +02:00
|
|
|
else
|
2001-11-19 20:51:20 +01:00
|
|
|
{
|
|
|
|
typemod = PG_GETARG_INT32(1);
|
|
|
|
result = format_type_internal(type_oid, typemod, true, true);
|
|
|
|
}
|
2000-07-09 23:30:21 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(result));
|
2000-07-09 23:30:21 +02:00
|
|
|
}
|
|
|
|
|
2001-08-09 20:28:18 +02:00
|
|
|
/*
|
|
|
|
* This version is for use within the backend in error messages, etc.
|
|
|
|
* One difference is that it will fail for an invalid type.
|
2001-10-08 21:55:07 +02:00
|
|
|
*
|
|
|
|
* The result is always a palloc'd string.
|
2001-08-09 20:28:18 +02:00
|
|
|
*/
|
|
|
|
char *
|
|
|
|
format_type_be(Oid type_oid)
|
|
|
|
{
|
2001-11-19 20:51:20 +01:00
|
|
|
return format_type_internal(type_oid, -1, false, false);
|
2001-08-09 20:28:18 +02:00
|
|
|
}
|
|
|
|
|
2001-10-08 21:55:07 +02:00
|
|
|
/*
|
|
|
|
* This version allows a nondefault typemod to be specified.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
format_type_with_typemod(Oid type_oid, int32 typemod)
|
|
|
|
{
|
2001-11-19 20:51:20 +01:00
|
|
|
return format_type_internal(type_oid, typemod, true, false);
|
2001-10-08 21:55:07 +02:00
|
|
|
}
|
|
|
|
|
2001-08-09 20:28:18 +02:00
|
|
|
|
|
|
|
|
2000-07-09 23:30:21 +02:00
|
|
|
static char *
|
2001-11-19 20:51:20 +01:00
|
|
|
format_type_internal(Oid type_oid, int32 typemod,
|
|
|
|
bool typemod_given, bool allow_invalid)
|
2000-07-09 23:30:21 +02:00
|
|
|
{
|
2001-11-19 20:51:20 +01:00
|
|
|
bool with_typemod = typemod_given && (typemod >= 0);
|
2000-07-09 23:30:21 +02:00
|
|
|
HeapTuple tuple;
|
2002-05-02 01:06:41 +02:00
|
|
|
Form_pg_type typeform;
|
2000-07-09 23:30:21 +02:00
|
|
|
Oid array_base_type;
|
|
|
|
bool is_array;
|
|
|
|
char *buf;
|
|
|
|
|
2001-08-09 20:28:18 +02:00
|
|
|
if (type_oid == InvalidOid && allow_invalid)
|
2000-08-21 20:23:18 +02:00
|
|
|
return pstrdup("-");
|
2000-07-09 23:30:21 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
tuple = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(type_oid),
|
|
|
|
0, 0, 0);
|
2000-07-07 21:24:43 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
2001-08-09 20:28:18 +02:00
|
|
|
{
|
|
|
|
if (allow_invalid)
|
|
|
|
return pstrdup("???");
|
|
|
|
else
|
2003-07-27 06:53:12 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", type_oid);
|
2001-08-09 20:28:18 +02:00
|
|
|
}
|
2002-05-02 01:06:41 +02:00
|
|
|
typeform = (Form_pg_type) GETSTRUCT(tuple);
|
2000-07-07 21:24:43 +02:00
|
|
|
|
2002-03-20 20:45:13 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Check if it's an array (and not a domain --- we don't want to show the
|
|
|
|
* substructure of a domain type). Fixed-length array types such as
|
|
|
|
* "name" shouldn't get deconstructed either. As of Postgres 8.1, rather
|
|
|
|
* than checking typlen we check the toast property, and don't deconstruct
|
|
|
|
* "plain storage" array types --- this is because we don't want to show
|
|
|
|
* oidvector as oid[].
|
2002-03-20 20:45:13 +01:00
|
|
|
*/
|
2002-05-02 01:06:41 +02:00
|
|
|
array_base_type = typeform->typelem;
|
2002-03-19 03:18:25 +01:00
|
|
|
|
2002-05-02 01:06:41 +02:00
|
|
|
if (array_base_type != InvalidOid &&
|
2005-03-29 02:17:27 +02:00
|
|
|
typeform->typstorage != 'p' &&
|
2007-04-02 05:49:42 +02:00
|
|
|
typeform->typtype != TYPTYPE_DOMAIN)
|
2000-07-07 21:24:43 +02:00
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
/* Switch our attention to the array element type */
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
tuple = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(array_base_type),
|
|
|
|
0, 0, 0);
|
2000-07-07 21:24:43 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
2001-08-09 20:28:18 +02:00
|
|
|
{
|
|
|
|
if (allow_invalid)
|
|
|
|
return pstrdup("???[]");
|
|
|
|
else
|
2003-07-27 06:53:12 +02:00
|
|
|
elog(ERROR, "cache lookup failed for type %u", type_oid);
|
2001-08-09 20:28:18 +02:00
|
|
|
}
|
2002-05-02 01:06:41 +02:00
|
|
|
typeform = (Form_pg_type) GETSTRUCT(tuple);
|
2000-08-21 20:23:18 +02:00
|
|
|
type_oid = array_base_type;
|
2002-05-02 01:06:41 +02:00
|
|
|
is_array = true;
|
2000-07-07 21:24:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
is_array = false;
|
2000-07-09 23:30:21 +02:00
|
|
|
|
2002-05-02 01:06:41 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* See if we want to special-case the output for certain built-in types.
|
|
|
|
* Note that these special cases should all correspond to special
|
|
|
|
* productions in gram.y, to ensure that the type name will be taken as a
|
|
|
|
* system type, not a user type of the same name.
|
2002-05-02 01:06:41 +02:00
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* If we do not provide a special-case output here, the type name will be
|
2005-10-15 04:49:52 +02:00
|
|
|
* handled the same way as a user type name --- in particular, it will be
|
|
|
|
* double-quoted if it matches any lexer keyword. This behavior is
|
2002-09-04 22:31:48 +02:00
|
|
|
* essential for some cases, such as types "bit" and "char".
|
2002-05-02 01:06:41 +02:00
|
|
|
*/
|
|
|
|
buf = NULL; /* flag for no special case */
|
|
|
|
|
2000-08-21 20:23:18 +02:00
|
|
|
switch (type_oid)
|
2000-07-07 21:24:43 +02:00
|
|
|
{
|
2001-11-12 22:04:46 +01:00
|
|
|
case BITOID:
|
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("bit", typemod, typeform->typmodout);
|
2001-11-19 20:51:20 +01:00
|
|
|
else if (typemod_given)
|
2001-11-12 22:04:46 +01:00
|
|
|
{
|
|
|
|
/*
|
2001-11-19 20:51:20 +01:00
|
|
|
* bit with typmod -1 is not the same as BIT, which means
|
2005-10-15 04:49:52 +02:00
|
|
|
* BIT(1) per SQL spec. Report it as the quoted typename so
|
|
|
|
* that parser will not assign a bogus typmod.
|
2001-11-12 22:04:46 +01:00
|
|
|
*/
|
|
|
|
}
|
2001-11-19 20:51:20 +01:00
|
|
|
else
|
|
|
|
buf = pstrdup("bit");
|
2001-11-12 22:04:46 +01:00
|
|
|
break;
|
|
|
|
|
2000-08-21 20:23:18 +02:00
|
|
|
case BOOLOID:
|
|
|
|
buf = pstrdup("boolean");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BPCHAROID:
|
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("character", typemod, typeform->typmodout);
|
2001-11-19 20:51:20 +01:00
|
|
|
else if (typemod_given)
|
2001-11-12 22:04:46 +01:00
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* bpchar with typmod -1 is not the same as CHARACTER, which
|
|
|
|
* means CHARACTER(1) per SQL spec. Report it as bpchar so
|
|
|
|
* that parser will not assign a bogus typmod.
|
2001-11-12 22:04:46 +01:00
|
|
|
*/
|
|
|
|
}
|
2001-11-19 20:51:20 +01:00
|
|
|
else
|
|
|
|
buf = pstrdup("character");
|
2000-08-21 20:23:18 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FLOAT4OID:
|
|
|
|
buf = pstrdup("real");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FLOAT8OID:
|
|
|
|
buf = pstrdup("double precision");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INT2OID:
|
|
|
|
buf = pstrdup("smallint");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INT4OID:
|
|
|
|
buf = pstrdup("integer");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INT8OID:
|
|
|
|
buf = pstrdup("bigint");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NUMERICOID:
|
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("numeric", typemod, typeform->typmodout);
|
2000-08-21 20:23:18 +02:00
|
|
|
else
|
|
|
|
buf = pstrdup("numeric");
|
|
|
|
break;
|
|
|
|
|
2001-10-23 22:12:54 +02:00
|
|
|
case INTERVALOID:
|
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("interval", typemod, typeform->typmodout);
|
2001-10-23 22:12:54 +02:00
|
|
|
else
|
|
|
|
buf = pstrdup("interval");
|
|
|
|
break;
|
|
|
|
|
2001-10-03 20:32:42 +02:00
|
|
|
case TIMEOID:
|
2001-10-04 19:52:24 +02:00
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("time", typemod, typeform->typmodout);
|
2001-10-03 20:32:42 +02:00
|
|
|
else
|
|
|
|
buf = pstrdup("time without time zone");
|
|
|
|
break;
|
|
|
|
|
2000-08-21 20:23:18 +02:00
|
|
|
case TIMETZOID:
|
2001-10-04 19:52:24 +02:00
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("time", typemod, typeform->typmodout);
|
2001-10-03 20:32:42 +02:00
|
|
|
else
|
|
|
|
buf = pstrdup("time with time zone");
|
2000-08-21 20:23:18 +02:00
|
|
|
break;
|
|
|
|
|
2001-02-05 18:35:04 +01:00
|
|
|
case TIMESTAMPOID:
|
2001-10-04 19:52:24 +02:00
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("timestamp", typemod, typeform->typmodout);
|
2001-10-03 20:32:42 +02:00
|
|
|
else
|
|
|
|
buf = pstrdup("timestamp without time zone");
|
2001-09-28 10:09:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TIMESTAMPTZOID:
|
2001-10-04 19:52:24 +02:00
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("timestamp", typemod, typeform->typmodout);
|
2001-10-03 20:32:42 +02:00
|
|
|
else
|
|
|
|
buf = pstrdup("timestamp with time zone");
|
2001-02-05 18:35:04 +01:00
|
|
|
break;
|
|
|
|
|
2000-08-21 20:23:18 +02:00
|
|
|
case VARBITOID:
|
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("bit varying", typemod, typeform->typmodout);
|
2000-08-21 20:23:18 +02:00
|
|
|
else
|
|
|
|
buf = pstrdup("bit varying");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VARCHAROID:
|
|
|
|
if (with_typemod)
|
2006-12-30 22:21:56 +01:00
|
|
|
buf = printTypmod("character varying", typemod, typeform->typmodout);
|
2000-08-21 20:23:18 +02:00
|
|
|
else
|
|
|
|
buf = pstrdup("character varying");
|
|
|
|
break;
|
2002-05-02 01:06:41 +02:00
|
|
|
}
|
2000-08-21 20:23:18 +02:00
|
|
|
|
2002-05-02 01:06:41 +02:00
|
|
|
if (buf == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Default handling: report the name as it appears in the catalog.
|
2005-10-15 04:49:52 +02:00
|
|
|
* Here, we must qualify the name if it is not visible in the search
|
|
|
|
* path, and we must double-quote it if it's not a standard identifier
|
|
|
|
* or if it matches any keyword.
|
2002-05-02 01:06:41 +02:00
|
|
|
*/
|
|
|
|
char *nspname;
|
|
|
|
char *typname;
|
|
|
|
|
|
|
|
if (TypeIsVisible(type_oid))
|
|
|
|
nspname = NULL;
|
|
|
|
else
|
|
|
|
nspname = get_namespace_name(typeform->typnamespace);
|
|
|
|
|
|
|
|
typname = NameStr(typeform->typname);
|
|
|
|
|
|
|
|
buf = quote_qualified_identifier(nspname, typname);
|
2006-12-30 22:21:56 +01:00
|
|
|
|
|
|
|
if (with_typemod)
|
|
|
|
buf = printTypmod(buf, typemod, typeform->typmodout);
|
2000-07-07 21:24:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array)
|
2000-08-21 20:23:18 +02:00
|
|
|
buf = psnprintf(strlen(buf) + 3, "%s[]", buf);
|
2000-07-07 21:24:43 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
|
2000-07-09 23:30:21 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-30 22:21:56 +01:00
|
|
|
/*
|
|
|
|
* Add typmod decoration to the basic type name
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
printTypmod(const char *typname, int32 typmod, Oid typmodout)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
char *res;
|
2006-12-30 22:21:56 +01:00
|
|
|
|
|
|
|
/* Shouldn't be called if typmod is -1 */
|
|
|
|
Assert(typmod >= 0);
|
|
|
|
|
|
|
|
if (typmodout == InvalidOid)
|
|
|
|
{
|
|
|
|
/* Default behavior: just print the integer typmod with parens */
|
|
|
|
res = psnprintf(strlen(typname) + MAX_INT32_LEN + 3, "%s(%d)",
|
|
|
|
typname, (int) typmod);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use the type-specific typmodout procedure */
|
2007-11-15 22:14:46 +01:00
|
|
|
char *tmstr;
|
2006-12-30 22:21:56 +01:00
|
|
|
|
|
|
|
tmstr = DatumGetCString(OidFunctionCall1(typmodout,
|
|
|
|
Int32GetDatum(typmod)));
|
|
|
|
res = psnprintf(strlen(typname) + strlen(tmstr) + 1, "%s%s",
|
|
|
|
typname, tmstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-25 20:05:54 +02:00
|
|
|
/*
|
2002-08-24 17:00:47 +02:00
|
|
|
* type_maximum_size --- determine maximum width of a variable-width column
|
2000-08-25 20:05:54 +02:00
|
|
|
*
|
2001-09-21 17:27:38 +02:00
|
|
|
* If the max width is indeterminate, return -1. In particular, we return
|
2000-08-25 20:05:54 +02:00
|
|
|
* -1 for any type not known to this routine. We assume the caller has
|
2002-08-24 17:00:47 +02:00
|
|
|
* already determined that the type is a variable-width type, so it's not
|
2000-08-25 20:05:54 +02:00
|
|
|
* necessary to look up the type's pg_type tuple here.
|
|
|
|
*
|
|
|
|
* This may appear unrelated to format_type(), but in fact the two routines
|
|
|
|
* share knowledge of the encoding of typmod for different types, so it's
|
2006-12-30 22:21:56 +01:00
|
|
|
* convenient to keep them together. (XXX now that most of this knowledge
|
|
|
|
* has been pushed out of format_type into the typmodout functions, it's
|
|
|
|
* interesting to wonder if it's worth trying to factor this code too...)
|
2000-08-25 20:05:54 +02:00
|
|
|
*/
|
|
|
|
int32
|
|
|
|
type_maximum_size(Oid type_oid, int32 typemod)
|
|
|
|
{
|
2001-10-23 22:12:54 +02:00
|
|
|
if (typemod < 0)
|
2000-08-25 20:05:54 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (type_oid)
|
|
|
|
{
|
|
|
|
case BPCHAROID:
|
|
|
|
case VARCHAROID:
|
|
|
|
/* typemod includes varlena header */
|
2002-08-29 09:22:30 +02:00
|
|
|
|
2001-09-21 17:27:38 +02:00
|
|
|
/* typemod is in characters not bytes */
|
|
|
|
return (typemod - VARHDRSZ) *
|
|
|
|
pg_encoding_max_length(GetDatabaseEncoding())
|
|
|
|
+ VARHDRSZ;
|
2000-08-25 20:05:54 +02:00
|
|
|
|
|
|
|
case NUMERICOID:
|
|
|
|
/* precision (ie, max # of digits) is in upper bits of typmod */
|
|
|
|
if (typemod > VARHDRSZ)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int precision = ((typemod - VARHDRSZ) >> 16) & 0xffff;
|
2000-08-25 20:05:54 +02:00
|
|
|
|
|
|
|
/* Numeric stores 2 decimal digits/byte, plus header */
|
|
|
|
return (precision + 1) / 2 + NUMERIC_HDRSZ;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VARBITOID:
|
2001-05-22 18:37:17 +02:00
|
|
|
case BITOID:
|
2000-08-25 20:05:54 +02:00
|
|
|
/* typemod is the (max) number of bits */
|
2001-03-22 05:01:46 +01:00
|
|
|
return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
|
2000-08-25 20:05:54 +02:00
|
|
|
+ 2 * sizeof(int32);
|
|
|
|
}
|
|
|
|
|
2001-09-21 17:27:38 +02:00
|
|
|
/* Unknown type, or unlimited-width type such as 'text' */
|
2000-08-25 20:05:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-07-09 23:30:21 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* oidvectortypes - converts a vector of type OIDs to "typname" list
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
oidvectortypes(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-03-29 02:17:27 +02:00
|
|
|
oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
|
2000-07-09 23:30:21 +02:00
|
|
|
char *result;
|
2005-03-29 02:17:27 +02:00
|
|
|
int numargs = oidArray->dim1;
|
2000-08-21 20:23:18 +02:00
|
|
|
int num;
|
2000-07-09 23:30:21 +02:00
|
|
|
size_t total;
|
|
|
|
size_t left;
|
|
|
|
|
|
|
|
total = 20 * numargs + 1;
|
|
|
|
result = palloc(total);
|
|
|
|
result[0] = '\0';
|
|
|
|
left = total - 1;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-07-09 23:30:21 +02:00
|
|
|
for (num = 0; num < numargs; num++)
|
|
|
|
{
|
2005-03-29 02:17:27 +02:00
|
|
|
char *typename = format_type_internal(oidArray->values[num], -1,
|
2001-11-19 20:51:20 +01:00
|
|
|
false, true);
|
|
|
|
size_t slen = strlen(typename);
|
2000-07-09 23:30:21 +02:00
|
|
|
|
2001-11-19 20:51:20 +01:00
|
|
|
if (left < (slen + 2))
|
2000-07-09 23:30:21 +02:00
|
|
|
{
|
2001-11-19 20:51:20 +01:00
|
|
|
total += slen + 2;
|
2000-07-09 23:30:21 +02:00
|
|
|
result = repalloc(result, total);
|
2001-11-19 20:51:20 +01:00
|
|
|
left += slen + 2;
|
2000-07-09 23:30:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (num > 0)
|
|
|
|
{
|
|
|
|
strcat(result, ", ");
|
|
|
|
left -= 2;
|
|
|
|
}
|
|
|
|
strcat(result, typename);
|
2001-11-19 20:51:20 +01:00
|
|
|
left -= slen;
|
2000-07-09 23:30:21 +02:00
|
|
|
}
|
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(result));
|
2000-07-07 21:24:43 +02:00
|
|
|
}
|
2001-11-19 20:51:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* snprintf into a palloc'd string */
|
|
|
|
static char *
|
2002-09-04 22:31:48 +02:00
|
|
|
psnprintf(size_t len, const char *fmt,...)
|
2001-11-19 20:51:20 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
buf = palloc(len);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, len, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|