1997-11-25 23:07:18 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1998-05-10 01:31:34 +02:00
|
|
|
* parse_type.c
|
1997-11-25 23:07:18 +01:00
|
|
|
* handle type operations for parser
|
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-11-16 23:30:52 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/parse_type.c,v 1.33 2000/11/16 22:30:28 tgl Exp $
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
1997-11-26 02:14:33 +01:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "parser/parse_type.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
/* check to see if a type id is valid,
|
|
|
|
* returns true if it is. By using this call before calling
|
|
|
|
* typeidType or typeidTypeName, more meaningful error messages
|
|
|
|
* can be produced because the caller typically has more context of
|
|
|
|
* what's going on - jolly
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
typeidIsValid(Oid id)
|
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
return SearchSysCacheExists(TYPEOID,
|
1997-11-25 23:07:18 +01:00
|
|
|
ObjectIdGetDatum(id),
|
2000-11-16 23:30:52 +01:00
|
|
|
0, 0, 0);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
1999-05-29 05:17:20 +02:00
|
|
|
/* return a Type structure, given a type id */
|
2000-11-16 23:30:52 +01:00
|
|
|
/* NB: caller must ReleaseSysCache the type tuple when done with it */
|
1997-11-25 23:07:18 +01:00
|
|
|
Type
|
|
|
|
typeidType(Oid id)
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(id),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
1999-02-23 08:54:03 +01:00
|
|
|
elog(ERROR, "Unable to locate type oid %u in catalog", id);
|
1998-09-01 05:29:17 +02:00
|
|
|
return (Type) tup;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return a Type structure, given type name */
|
2000-11-16 23:30:52 +01:00
|
|
|
/* NB: caller must ReleaseSysCache the type tuple when done with it */
|
1997-11-25 23:07:18 +01:00
|
|
|
Type
|
|
|
|
typenameType(char *s)
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
if (s == NULL)
|
2000-11-16 23:30:52 +01:00
|
|
|
elog(ERROR, "typenameType: Null typename");
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(TYPENAME,
|
|
|
|
PointerGetDatum(s),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
1999-02-23 08:54:03 +01:00
|
|
|
elog(ERROR, "Unable to locate type name '%s' in catalog", s);
|
1998-09-01 05:29:17 +02:00
|
|
|
return (Type) tup;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
/* given type (as type struct), return the type OID */
|
1997-11-25 23:07:18 +01:00
|
|
|
Oid
|
|
|
|
typeTypeId(Type tp)
|
|
|
|
{
|
|
|
|
if (tp == NULL)
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "typeTypeId() called with NULL type struct");
|
1998-11-27 20:52:36 +01:00
|
|
|
return tp->t_data->t_oid;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* given type (as type struct), return the length of type */
|
|
|
|
int16
|
|
|
|
typeLen(Type t)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_type typ;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
typ = (Form_pg_type) GETSTRUCT(t);
|
|
|
|
return typ->typlen;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* given type (as type struct), return the value of its 'byval' attribute.*/
|
|
|
|
bool
|
|
|
|
typeByVal(Type t)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_type typ;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
typ = (Form_pg_type) GETSTRUCT(t);
|
|
|
|
return typ->typbyval;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* given type (as type struct), return the name of type */
|
1998-02-26 05:46:47 +01:00
|
|
|
char *
|
1997-11-25 23:07:18 +01:00
|
|
|
typeTypeName(Type t)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_type typ;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
typ = (Form_pg_type) GETSTRUCT(t);
|
2000-06-06 18:50:37 +02:00
|
|
|
/* pstrdup here because result may need to outlive the syscache entry */
|
|
|
|
return pstrdup(NameStr(typ->typname));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* given a type, return its typetype ('c' for 'c'atalog types) */
|
|
|
|
char
|
|
|
|
typeTypeFlag(Type t)
|
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_type typ;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
typ = (Form_pg_type) GETSTRUCT(t);
|
|
|
|
return typ->typtype;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
Oid
|
|
|
|
typeTypeRelid(Type typ)
|
|
|
|
{
|
|
|
|
Form_pg_type typtup;
|
|
|
|
|
|
|
|
typtup = (Form_pg_type) GETSTRUCT(typ);
|
|
|
|
|
|
|
|
return typtup->typrelid;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NOT_USED
|
|
|
|
Oid
|
|
|
|
typeTypElem(Type typ)
|
|
|
|
{
|
|
|
|
Form_pg_type typtup;
|
|
|
|
|
|
|
|
typtup = (Form_pg_type) GETSTRUCT(typ);
|
|
|
|
|
|
|
|
return typtup->typelem;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NOT_USED
|
|
|
|
/* Given a type structure, return the in-conversion function of the type */
|
|
|
|
Oid
|
|
|
|
typeInfunc(Type typ)
|
|
|
|
{
|
|
|
|
Form_pg_type typtup;
|
|
|
|
|
|
|
|
typtup = (Form_pg_type) GETSTRUCT(typ);
|
|
|
|
|
|
|
|
return typtup->typinput;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NOT_USED
|
|
|
|
/* Given a type structure, return the out-conversion function of the type */
|
|
|
|
Oid
|
|
|
|
typeOutfunc(Type typ)
|
|
|
|
{
|
|
|
|
Form_pg_type typtup;
|
|
|
|
|
|
|
|
typtup = (Form_pg_type) GETSTRUCT(typ);
|
|
|
|
|
|
|
|
return typtup->typoutput;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
/* Given a type structure and a string, returns the internal form of
|
|
|
|
that string */
|
1999-08-05 04:33:54 +02:00
|
|
|
Datum
|
|
|
|
stringTypeDatum(Type tp, char *string, int32 atttypmod)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
|
|
|
Oid op;
|
|
|
|
Oid typelem;
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
op = ((Form_pg_type) GETSTRUCT(tp))->typinput;
|
1998-09-01 06:40:42 +02:00
|
|
|
typelem = ((Form_pg_type) GETSTRUCT(tp))->typelem; /* XXX - used for
|
1998-02-26 05:46:47 +01:00
|
|
|
* array_in */
|
2000-05-30 06:25:00 +02:00
|
|
|
return OidFunctionCall3(op,
|
|
|
|
CStringGetDatum(string),
|
|
|
|
ObjectIdGetDatum(typelem),
|
|
|
|
Int32GetDatum(atttypmod));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a type id, returns the out-conversion function of the type */
|
1998-10-08 20:30:52 +02:00
|
|
|
#ifdef NOT_USED
|
1997-11-25 23:07:18 +01:00
|
|
|
Oid
|
1998-02-13 20:46:22 +01:00
|
|
|
typeidOutfunc(Oid type_id)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
|
|
|
HeapTuple typeTuple;
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_type type;
|
1997-11-25 23:07:18 +01:00
|
|
|
Oid outfunc;
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
typeTuple = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(type_id),
|
|
|
|
0, 0, 0);
|
1997-11-25 23:07:18 +01:00
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
1998-02-13 20:46:22 +01:00
|
|
|
elog(ERROR, "typeidOutfunc: Invalid type - oid = %u", type_id);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
type = (Form_pg_type) GETSTRUCT(typeTuple);
|
1997-11-25 23:07:18 +01:00
|
|
|
outfunc = type->typoutput;
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(typeTuple);
|
1998-09-01 05:29:17 +02:00
|
|
|
return outfunc;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1998-10-08 20:30:52 +02:00
|
|
|
#endif
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
/* return a type name, given a typeid */
|
|
|
|
char *
|
|
|
|
typeidTypeName(Oid id)
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
Form_pg_type typetuple;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
tup = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(id),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
elog(ERROR, "Unable to locate type oid %u in catalog", id);
|
|
|
|
typetuple = (Form_pg_type) GETSTRUCT(tup);
|
|
|
|
/*
|
|
|
|
* pstrdup here because result may need to outlive the syscache entry
|
|
|
|
* (eg, it might end up as part of a parse tree that will outlive
|
|
|
|
* the current transaction...)
|
|
|
|
*/
|
|
|
|
result = pstrdup(NameStr(typetuple->typname));
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* given a typeid, return the type's typrelid (associated relation, if any) */
|
1997-11-25 23:07:18 +01:00
|
|
|
Oid
|
|
|
|
typeidTypeRelid(Oid type_id)
|
|
|
|
{
|
|
|
|
HeapTuple typeTuple;
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_type type;
|
2000-11-16 23:30:52 +01:00
|
|
|
Oid result;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
typeTuple = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(type_id),
|
|
|
|
0, 0, 0);
|
1997-11-25 23:07:18 +01:00
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "typeidTypeRelid: Invalid type - oid = %u", type_id);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
type = (Form_pg_type) GETSTRUCT(typeTuple);
|
2000-11-16 23:30:52 +01:00
|
|
|
result = type->typrelid;
|
|
|
|
ReleaseSysCache(typeTuple);
|
|
|
|
return result;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
/* given a type name, return the type's typeid */
|
1997-11-25 23:07:18 +01:00
|
|
|
Oid
|
2000-11-16 23:30:52 +01:00
|
|
|
typenameTypeId(char *s)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
Type typ = typenameType(s);
|
|
|
|
Oid result;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
result = typ->t_data->t_oid;
|
|
|
|
ReleaseSysCache(typ);
|
|
|
|
return result;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|