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
|
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-07-20 13:17:11 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/parse_type.c,v 1.13 1998/07/20 11:17:11 momjian Exp $
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
#include "nodes/nodes.h"
|
|
|
|
#include "nodes/parsenodes.h"
|
|
|
|
#include "nodes/primnodes.h"
|
|
|
|
#include "parser/parse_node.h"
|
|
|
|
|
1997-11-26 02:14:33 +01:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "parser/parse_target.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)
|
|
|
|
{
|
|
|
|
return (SearchSysCacheTuple(TYPOID,
|
|
|
|
ObjectIdGetDatum(id),
|
|
|
|
0, 0, 0) != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return a type name, given a typeid */
|
1998-02-26 05:46:47 +01:00
|
|
|
char *
|
1997-11-25 23:07:18 +01:00
|
|
|
typeidTypeName(Oid id)
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
TypeTupleForm typetuple;
|
|
|
|
|
|
|
|
if (!(tup = SearchSysCacheTuple(TYPOID, ObjectIdGetDatum(id),
|
|
|
|
0, 0, 0)))
|
|
|
|
{
|
1998-02-27 20:44:51 +01:00
|
|
|
elog(ERROR, "type id lookup of %u failed", id);
|
1997-11-25 23:07:18 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
typetuple = (TypeTupleForm) GETSTRUCT(tup);
|
|
|
|
return (typetuple->typname).data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return a Type structure, given an typid */
|
|
|
|
Type
|
|
|
|
typeidType(Oid id)
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
if (!(tup = SearchSysCacheTuple(TYPOID, ObjectIdGetDatum(id),
|
|
|
|
0, 0, 0)))
|
|
|
|
{
|
1998-02-27 20:44:51 +01:00
|
|
|
elog(ERROR, "type id lookup of %u failed", id);
|
1997-11-25 23:07:18 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return ((Type) tup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return a Type structure, given type name */
|
|
|
|
Type
|
|
|
|
typenameType(char *s)
|
|
|
|
{
|
|
|
|
HeapTuple tup;
|
|
|
|
|
|
|
|
if (s == NULL)
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "type(): Null type");
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
if (!(tup = SearchSysCacheTuple(TYPNAME, PointerGetDatum(s), 0, 0, 0)))
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "type name lookup of %s failed", s);
|
1997-11-25 23:07:18 +01:00
|
|
|
return ((Type) tup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* given type, return the type OID */
|
|
|
|
Oid
|
|
|
|
typeTypeId(Type tp)
|
|
|
|
{
|
|
|
|
if (tp == NULL)
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "typeTypeId() called with NULL type struct");
|
1997-11-25 23:07:18 +01:00
|
|
|
return (tp->t_oid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* given type (as type struct), return the length of type */
|
|
|
|
int16
|
|
|
|
typeLen(Type t)
|
|
|
|
{
|
|
|
|
TypeTupleForm typ;
|
|
|
|
|
|
|
|
typ = (TypeTupleForm) GETSTRUCT(t);
|
|
|
|
return (typ->typlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* given type (as type struct), return the value of its 'byval' attribute.*/
|
|
|
|
bool
|
|
|
|
typeByVal(Type t)
|
|
|
|
{
|
|
|
|
TypeTupleForm typ;
|
|
|
|
|
|
|
|
typ = (TypeTupleForm) GETSTRUCT(t);
|
|
|
|
return (typ->typbyval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
TypeTupleForm typ;
|
|
|
|
|
|
|
|
typ = (TypeTupleForm) GETSTRUCT(t);
|
|
|
|
return (typ->typname).data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* given a type, return its typetype ('c' for 'c'atalog types) */
|
|
|
|
char
|
|
|
|
typeTypeFlag(Type t)
|
|
|
|
{
|
|
|
|
TypeTupleForm typ;
|
|
|
|
|
|
|
|
typ = (TypeTupleForm) GETSTRUCT(t);
|
|
|
|
return (typ->typtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a type structure and a string, returns the internal form of
|
|
|
|
that string */
|
|
|
|
char *
|
1998-07-12 23:29:40 +02:00
|
|
|
stringTypeString(Type tp, char *string, int32 atttypmod)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
|
|
|
Oid op;
|
|
|
|
Oid typelem;
|
|
|
|
|
|
|
|
op = ((TypeTupleForm) GETSTRUCT(tp))->typinput;
|
1998-02-26 05:46:47 +01:00
|
|
|
typelem = ((TypeTupleForm) GETSTRUCT(tp))->typelem; /* XXX - used for
|
|
|
|
* array_in */
|
1998-01-17 00:21:07 +01:00
|
|
|
return ((char *) fmgr(op, string, typelem, atttypmod));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a type id, returns the out-conversion function of the type */
|
|
|
|
Oid
|
1998-02-13 20:46:22 +01:00
|
|
|
typeidOutfunc(Oid type_id)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
|
|
|
HeapTuple typeTuple;
|
|
|
|
TypeTupleForm type;
|
|
|
|
Oid outfunc;
|
|
|
|
|
|
|
|
typeTuple = SearchSysCacheTuple(TYPOID,
|
|
|
|
ObjectIdGetDatum(type_id),
|
|
|
|
0, 0, 0);
|
|
|
|
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
|
|
|
|
|
|
|
type = (TypeTupleForm) GETSTRUCT(typeTuple);
|
|
|
|
outfunc = type->typoutput;
|
|
|
|
return (outfunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
Oid
|
|
|
|
typeidTypeRelid(Oid type_id)
|
|
|
|
{
|
|
|
|
HeapTuple typeTuple;
|
|
|
|
TypeTupleForm type;
|
|
|
|
Oid infunc;
|
|
|
|
|
|
|
|
typeTuple = SearchSysCacheTuple(TYPOID,
|
|
|
|
ObjectIdGetDatum(type_id),
|
|
|
|
0, 0, 0);
|
|
|
|
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
|
|
|
|
|
|
|
type = (TypeTupleForm) GETSTRUCT(typeTuple);
|
|
|
|
infunc = type->typrelid;
|
|
|
|
return (infunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
Oid
|
|
|
|
typeTypeRelid(Type typ)
|
|
|
|
{
|
|
|
|
TypeTupleForm typtup;
|
|
|
|
|
|
|
|
typtup = (TypeTupleForm) GETSTRUCT(typ);
|
|
|
|
|
|
|
|
return (typtup->typrelid);
|
|
|
|
}
|
|
|
|
|
|
|
|
Oid
|
|
|
|
typeidTypElem(Oid type_id)
|
|
|
|
{
|
|
|
|
HeapTuple typeTuple;
|
|
|
|
TypeTupleForm type;
|
|
|
|
|
|
|
|
if (!(typeTuple = SearchSysCacheTuple(TYPOID,
|
|
|
|
ObjectIdGetDatum(type_id),
|
|
|
|
0, 0, 0)))
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "type id lookup of %u failed", type_id);
|
1997-11-25 23:07:18 +01:00
|
|
|
type = (TypeTupleForm) GETSTRUCT(typeTuple);
|
|
|
|
|
|
|
|
return (type->typelem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given the attribute type of an array return the arrtribute type of
|
|
|
|
an element of the array */
|
|
|
|
|
|
|
|
Oid
|
|
|
|
GetArrayElementType(Oid typearray)
|
|
|
|
{
|
|
|
|
HeapTuple type_tuple;
|
|
|
|
TypeTupleForm type_struct_array;
|
|
|
|
|
|
|
|
type_tuple = SearchSysCacheTuple(TYPOID,
|
|
|
|
ObjectIdGetDatum(typearray),
|
|
|
|
0, 0, 0);
|
|
|
|
|
|
|
|
if (!HeapTupleIsValid(type_tuple))
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "GetArrayElementType: Cache lookup failed for type %d",
|
1997-11-25 23:07:18 +01:00
|
|
|
typearray);
|
|
|
|
|
|
|
|
/* get the array type struct from the type tuple */
|
|
|
|
type_struct_array = (TypeTupleForm) GETSTRUCT(type_tuple);
|
|
|
|
|
|
|
|
if (type_struct_array->typelem == InvalidOid)
|
|
|
|
{
|
1998-01-05 04:35:55 +01:00
|
|
|
elog(ERROR, "GetArrayElementType: type %s is not an array",
|
1998-07-20 13:17:11 +02:00
|
|
|
type_struct_array->typname);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (type_struct_array->typelem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a type id, returns the in-conversion function of the type */
|
|
|
|
Oid
|
1998-02-13 20:46:22 +01:00
|
|
|
typeidInfunc(Oid type_id)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
|
|
|
HeapTuple typeTuple;
|
|
|
|
TypeTupleForm type;
|
|
|
|
Oid infunc;
|
|
|
|
|
|
|
|
typeTuple = SearchSysCacheTuple(TYPOID,
|
|
|
|
ObjectIdGetDatum(type_id),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
1998-02-13 20:46:22 +01:00
|
|
|
elog(ERROR, "typeidInfunc: Invalid type - oid = %u", type_id);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
type = (TypeTupleForm) GETSTRUCT(typeTuple);
|
|
|
|
infunc = type->typinput;
|
|
|
|
return (infunc);
|
|
|
|
}
|