Commit 469673f9 authored by Tom Lane's avatar Tom Lane

Fix format_type() to display correct lengths for BIT/BIT VARYING.

Also, make it depend on type OIDs rather than type names for more
consistency with rest of backend.
parent e67ff6b6
/* $Header: /cvsroot/pgsql/src/backend/utils/adt/format_type.c,v 1.2 2000/07/09 21:30:12 petere Exp $ */ /*-------------------------------------------------------------------------
*
* format_type.c
* Display type names "nicely".
*
*
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/format_type.c,v 1.3 2000/08/21 18:23:18 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h" #include "postgres.h"
#include <ctype.h> #include <ctype.h>
#include <stdarg.h>
#include "fmgr.h" #include "fmgr.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/syscache.h" #include "utils/syscache.h"
#define streq(a, b) (strcmp((a), (b))==0)
#define MAX_INT32_LEN 11 #define MAX_INT32_LEN 11
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
static char *format_type_internal(Oid type_oid, int32 typemod);
static char * static char *
psnprintf(size_t len, const char * fmt, ...) psnprintf(size_t len, const char * fmt, ...)
{ {
...@@ -31,11 +45,6 @@ psnprintf(size_t len, const char * fmt, ...) ...@@ -31,11 +45,6 @@ psnprintf(size_t len, const char * fmt, ...)
} }
static char *
format_type_internal(Oid type_oid, int32 typemod, bool with_typemod);
/* /*
* SQL function: format_type(type_oid, typemod) * SQL function: format_type(type_oid, typemod)
* *
...@@ -55,29 +64,29 @@ Datum ...@@ -55,29 +64,29 @@ Datum
format_type(PG_FUNCTION_ARGS) format_type(PG_FUNCTION_ARGS)
{ {
Oid type_oid; Oid type_oid;
bool with_typemod; int32 typemod;
int32 typemod = 0;
char *result; char *result;
if (PG_ARGISNULL(0)) if (PG_ARGISNULL(0))
PG_RETURN_NULL(); PG_RETURN_NULL();
type_oid = DatumGetObjectId(PG_GETARG_DATUM(0)); type_oid = PG_GETARG_OID(0);
with_typemod = !PG_ARGISNULL(1); if (!PG_ARGISNULL(1))
if (with_typemod)
typemod = PG_GETARG_INT32(1); typemod = PG_GETARG_INT32(1);
else
typemod = -1; /* default typmod */
result = format_type_internal(type_oid, typemod, with_typemod); result = format_type_internal(type_oid, typemod);
PG_RETURN_TEXT_P(_textin(result)); PG_RETURN_DATUM(_textin(result));
} }
static char * static char *
format_type_internal(Oid type_oid, int32 typemod, bool with_typemod) format_type_internal(Oid type_oid, int32 typemod)
{ {
bool with_typemod = (typemod >= 0);
HeapTuple tuple; HeapTuple tuple;
Oid array_base_type; Oid array_base_type;
int16 typlen; int16 typlen;
...@@ -86,13 +95,13 @@ format_type_internal(Oid type_oid, int32 typemod, bool with_typemod) ...@@ -86,13 +95,13 @@ format_type_internal(Oid type_oid, int32 typemod, bool with_typemod)
char *buf; char *buf;
if (type_oid == InvalidOid) if (type_oid == InvalidOid)
return "-"; return pstrdup("-");
tuple = SearchSysCacheTuple(TYPEOID, ObjectIdGetDatum(type_oid), tuple = SearchSysCacheTuple(TYPEOID, ObjectIdGetDatum(type_oid),
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple))
return "???"; return pstrdup("???");
array_base_type = ((Form_pg_type) GETSTRUCT(tuple))->typelem; array_base_type = ((Form_pg_type) GETSTRUCT(tuple))->typelem;
typlen = ((Form_pg_type) GETSTRUCT(tuple))->typlen; typlen = ((Form_pg_type) GETSTRUCT(tuple))->typlen;
...@@ -102,97 +111,104 @@ format_type_internal(Oid type_oid, int32 typemod, bool with_typemod) ...@@ -102,97 +111,104 @@ format_type_internal(Oid type_oid, int32 typemod, bool with_typemod)
ObjectIdGetDatum(array_base_type), ObjectIdGetDatum(array_base_type),
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple))
return "???[]"; return pstrdup("???[]");
is_array = true; is_array = true;
type_oid = array_base_type;
} }
else else
is_array = false; is_array = false;
name = NameStr(((Form_pg_type) GETSTRUCT(tuple))->typname);
if (streq(name, "bit")) switch (type_oid)
{ {
if (with_typemod) case BOOLOID:
buf = psnprintf(5 + MAX_INT32_LEN + 1, "bit(%d)", (int) typemod - 4); buf = pstrdup("boolean");
else break;
buf = pstrdup("bit");
} case BPCHAROID:
if (with_typemod)
else if (streq(name, "bool")) buf = psnprintf(11 + MAX_INT32_LEN + 1, "character(%d)",
buf = pstrdup("boolean"); (int) (typemod - VARHDRSZ));
else
else if (streq(name, "bpchar")) buf = pstrdup("character");
{ break;
if (with_typemod)
buf = psnprintf(11 + MAX_INT32_LEN + 1, "character(%d)", (int) typemod - 4); case CHAROID:
else /* This char type is the single-byte version. You have to
buf = pstrdup("character"); * double-quote it to get at it in the parser.
} */
buf = pstrdup("\"char\"");
/* This char type is the single-byte version. You have to break;
* double-quote it to get at it in the parser. */
else if (streq(name, "char")) case FLOAT4OID:
buf = pstrdup("\"char\""); buf = pstrdup("real");
break;
else if (streq(name, "float4"))
buf = pstrdup("real"); case FLOAT8OID:
buf = pstrdup("double precision");
else if (streq(name, "float8")) break;
buf = pstrdup("double precision");
case INT2OID:
else if (streq(name, "int2")) buf = pstrdup("smallint");
buf = pstrdup("smallint"); break;
else if (streq(name, "int4")) case INT4OID:
buf = pstrdup("integer"); buf = pstrdup("integer");
break;
else if (streq(name, "int8"))
buf = pstrdup("bigint"); case INT8OID:
buf = pstrdup("bigint");
else if (streq(name, "numeric")) break;
{
if (with_typemod) case NUMERICOID:
buf = psnprintf(10 + 2 * MAX_INT32_LEN + 1, "numeric(%d,%d)", if (with_typemod)
((typemod - VARHDRSZ) >> 16) & 0xffff, buf = psnprintf(10 + 2 * MAX_INT32_LEN + 1, "numeric(%d,%d)",
(typemod - VARHDRSZ) & 0xffff); ((typemod - VARHDRSZ) >> 16) & 0xffff,
else (typemod - VARHDRSZ) & 0xffff);
buf = pstrdup("numeric"); else
} buf = pstrdup("numeric");
break;
else if (streq(name, "timetz"))
buf = pstrdup("time with time zone"); case TIMETZOID:
buf = pstrdup("time with time zone");
else if (streq(name, "varbit")) break;
{
if (with_typemod) case VARBITOID:
buf = psnprintf(13 + MAX_INT32_LEN + 1, "bit varying(%d)", (int) typemod - 4); if (with_typemod)
else buf = psnprintf(13 + MAX_INT32_LEN + 1, "bit varying(%d)",
buf = pstrdup("bit varying"); (int) typemod);
} else
buf = pstrdup("bit varying");
else if (streq(name, "varchar")) break;
{
if (with_typemod) case VARCHAROID:
buf = psnprintf(19 + MAX_INT32_LEN + 1, "character varying(%d)", (int) typemod - 4); if (with_typemod)
else buf = psnprintf(19 + MAX_INT32_LEN + 1,
buf = pstrdup("character varying"); "character varying(%d)",
} (int) (typemod - VARHDRSZ));
else
else buf = pstrdup("character varying");
{ break;
if (strspn(name, "abcdefghijklmnopqrstuvwxyz0123456789_") != strlen(name)
|| isdigit((int) name[0])) case ZPBITOID:
buf = psnprintf(strlen(name) + 3, "\"%s\"", name); if (with_typemod)
else buf = psnprintf(5 + MAX_INT32_LEN + 1, "bit(%d)",
buf = name; (int) typemod);
else
buf = pstrdup("bit");
break;
default:
name = NameStr(((Form_pg_type) GETSTRUCT(tuple))->typname);
if (strspn(name, "abcdefghijklmnopqrstuvwxyz0123456789_") != strlen(name)
|| isdigit((int) name[0]))
buf = psnprintf(strlen(name) + 3, "\"%s\"", name);
else
buf = pstrdup(name);
break;
} }
if (is_array) if (is_array)
{ buf = psnprintf(strlen(buf) + 3, "%s[]", buf);
char * buf2 = psnprintf(strlen(buf) + 3, "%s[]", buf);
buf = buf2;
}
return buf; return buf;
} }
...@@ -209,10 +225,10 @@ format_type_internal(Oid type_oid, int32 typemod, bool with_typemod) ...@@ -209,10 +225,10 @@ format_type_internal(Oid type_oid, int32 typemod, bool with_typemod)
Datum Datum
oidvectortypes(PG_FUNCTION_ARGS) oidvectortypes(PG_FUNCTION_ARGS)
{ {
int numargs;
int num;
Oid *oidArray = (Oid *) PG_GETARG_POINTER(0); Oid *oidArray = (Oid *) PG_GETARG_POINTER(0);
char *result; char *result;
int numargs;
int num;
size_t total; size_t total;
size_t left; size_t left;
...@@ -231,7 +247,7 @@ oidvectortypes(PG_FUNCTION_ARGS) ...@@ -231,7 +247,7 @@ oidvectortypes(PG_FUNCTION_ARGS)
for (num = 0; num < numargs; num++) for (num = 0; num < numargs; num++)
{ {
char * typename = format_type_internal(oidArray[num], 0, false); char * typename = format_type_internal(oidArray[num], -1);
if (left < strlen(typename) + 2) if (left < strlen(typename) + 2)
{ {
...@@ -249,5 +265,5 @@ oidvectortypes(PG_FUNCTION_ARGS) ...@@ -249,5 +265,5 @@ oidvectortypes(PG_FUNCTION_ARGS)
left -= strlen(typename); left -= strlen(typename);
} }
PG_RETURN_TEXT_P(_textin(result)); PG_RETURN_DATUM(_textin(result));
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment