Commit 25dfba39 authored by Tom Lane's avatar Tom Lane

Make ruleutils.c use format_type for printing typenames. Minor tweaks

in quoting rules and recognition of implicit type coercions.
parent 7677fe0a
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.115 2002/04/16 23:08:11 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.116 2002/04/28 00:49:12 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1062,8 +1062,8 @@ exprIsLengthCoercion(Node *expr, int32 *coercedTypmod) ...@@ -1062,8 +1062,8 @@ exprIsLengthCoercion(Node *expr, int32 *coercedTypmod)
} }
/* /*
* Furthermore, the name of the function must be the same as the * Furthermore, the name and namespace of the function must be the same
* argument/result type's name. * as its result type's name/namespace (cf. find_coercion_function).
*/ */
typeTuple = SearchSysCache(TYPEOID, typeTuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(procStruct->prorettype), ObjectIdGetDatum(procStruct->prorettype),
...@@ -1072,9 +1072,9 @@ exprIsLengthCoercion(Node *expr, int32 *coercedTypmod) ...@@ -1072,9 +1072,9 @@ exprIsLengthCoercion(Node *expr, int32 *coercedTypmod)
elog(ERROR, "cache lookup for type %u failed", elog(ERROR, "cache lookup for type %u failed",
procStruct->prorettype); procStruct->prorettype);
typeStruct = (Form_pg_type) GETSTRUCT(typeTuple); typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
if (strncmp(NameStr(procStruct->proname), if (strcmp(NameStr(procStruct->proname),
NameStr(typeStruct->typname), NameStr(typeStruct->typname)) != 0 ||
NAMEDATALEN) != 0) procStruct->pronamespace != typeStruct->typnamespace)
{ {
ReleaseSysCache(procTuple); ReleaseSysCache(procTuple);
ReleaseSysCache(typeTuple); ReleaseSysCache(typeTuple);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* back to source text * back to source text
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.99 2002/04/25 02:56:55 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.100 2002/04/28 00:49:13 tgl Exp $
* *
* This software is copyrighted by Jan Wieck - Hamburg. * This software is copyrighted by Jan Wieck - Hamburg.
* *
...@@ -1805,23 +1805,12 @@ get_rule_expr(Node *node, deparse_context *context) ...@@ -1805,23 +1805,12 @@ get_rule_expr(Node *node, deparse_context *context)
case T_RelabelType: case T_RelabelType:
{ {
RelabelType *relabel = (RelabelType *) node; RelabelType *relabel = (RelabelType *) node;
HeapTuple typetup;
Form_pg_type typeStruct;
char *extval;
appendStringInfoChar(buf, '('); appendStringInfoChar(buf, '(');
get_rule_expr(relabel->arg, context); get_rule_expr(relabel->arg, context);
typetup = SearchSysCache(TYPEOID, appendStringInfo(buf, ")::%s",
ObjectIdGetDatum(relabel->resulttype), format_type_with_typemod(relabel->resulttype,
0, 0, 0); relabel->resulttypmod));
if (!HeapTupleIsValid(typetup))
elog(ERROR, "cache lookup of type %u failed",
relabel->resulttype);
typeStruct = (Form_pg_type) GETSTRUCT(typetup);
extval = pstrdup(NameStr(typeStruct->typname));
appendStringInfo(buf, ")::%s", quote_identifier(extval));
pfree(extval);
ReleaseSysCache(typetup);
} }
break; break;
...@@ -2095,13 +2084,15 @@ strip_type_coercion(Node *expr, Oid resultType) ...@@ -2095,13 +2084,15 @@ strip_type_coercion(Node *expr, Oid resultType)
elog(ERROR, "cache lookup for proc %u failed", func->funcid); elog(ERROR, "cache lookup for proc %u failed", func->funcid);
procStruct = (Form_pg_proc) GETSTRUCT(procTuple); procStruct = (Form_pg_proc) GETSTRUCT(procTuple);
/* Double-check func has one arg and correct result type */ /* Double-check func has one arg and correct result type */
/* Also, it must be an implicit coercion function */
if (procStruct->pronargs != 1 || if (procStruct->pronargs != 1 ||
procStruct->prorettype != resultType) procStruct->prorettype != resultType ||
!procStruct->proimplicit)
{ {
ReleaseSysCache(procTuple); ReleaseSysCache(procTuple);
return expr; return expr;
} }
/* See if function has same name as its result type */ /* See if function has same name/namespace as its result type */
typeTuple = SearchSysCache(TYPEOID, typeTuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(procStruct->prorettype), ObjectIdGetDatum(procStruct->prorettype),
0, 0, 0); 0, 0, 0);
...@@ -2109,9 +2100,9 @@ strip_type_coercion(Node *expr, Oid resultType) ...@@ -2109,9 +2100,9 @@ strip_type_coercion(Node *expr, Oid resultType)
elog(ERROR, "cache lookup for type %u failed", elog(ERROR, "cache lookup for type %u failed",
procStruct->prorettype); procStruct->prorettype);
typeStruct = (Form_pg_type) GETSTRUCT(typeTuple); typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
if (strncmp(NameStr(procStruct->proname), if (strcmp(NameStr(procStruct->proname),
NameStr(typeStruct->typname), NameStr(typeStruct->typname)) != 0 ||
NAMEDATALEN) != 0) procStruct->pronamespace != typeStruct->typnamespace)
{ {
ReleaseSysCache(procTuple); ReleaseSysCache(procTuple);
ReleaseSysCache(typeTuple); ReleaseSysCache(typeTuple);
...@@ -2179,14 +2170,6 @@ get_const_expr(Const *constval, deparse_context *context) ...@@ -2179,14 +2170,6 @@ get_const_expr(Const *constval, deparse_context *context)
char *extval; char *extval;
char *valptr; char *valptr;
typetup = SearchSysCache(TYPEOID,
ObjectIdGetDatum(constval->consttype),
0, 0, 0);
if (!HeapTupleIsValid(typetup))
elog(ERROR, "cache lookup of type %u failed", constval->consttype);
typeStruct = (Form_pg_type) GETSTRUCT(typetup);
if (constval->constisnull) if (constval->constisnull)
{ {
/* /*
...@@ -2194,13 +2177,19 @@ get_const_expr(Const *constval, deparse_context *context) ...@@ -2194,13 +2177,19 @@ get_const_expr(Const *constval, deparse_context *context)
* prevents misdecisions about the type, but it ensures that our * prevents misdecisions about the type, but it ensures that our
* output is a valid b_expr. * output is a valid b_expr.
*/ */
extval = pstrdup(NameStr(typeStruct->typname)); appendStringInfo(buf, "NULL::%s",
appendStringInfo(buf, "NULL::%s", quote_identifier(extval)); format_type_with_typemod(constval->consttype, -1));
pfree(extval);
ReleaseSysCache(typetup);
return; return;
} }
typetup = SearchSysCache(TYPEOID,
ObjectIdGetDatum(constval->consttype),
0, 0, 0);
if (!HeapTupleIsValid(typetup))
elog(ERROR, "cache lookup of type %u failed", constval->consttype);
typeStruct = (Form_pg_type) GETSTRUCT(typetup);
extval = DatumGetCString(OidFunctionCall3(typeStruct->typoutput, extval = DatumGetCString(OidFunctionCall3(typeStruct->typoutput,
constval->constvalue, constval->constvalue,
ObjectIdGetDatum(typeStruct->typelem), ObjectIdGetDatum(typeStruct->typelem),
...@@ -2251,9 +2240,9 @@ get_const_expr(Const *constval, deparse_context *context) ...@@ -2251,9 +2240,9 @@ get_const_expr(Const *constval, deparse_context *context)
/* These types can be left unlabeled */ /* These types can be left unlabeled */
break; break;
default: default:
extval = pstrdup(NameStr(typeStruct->typname)); appendStringInfo(buf, "::%s",
appendStringInfo(buf, "::%s", quote_identifier(extval)); format_type_with_typemod(constval->consttype,
pfree(extval); -1));
break; break;
} }
...@@ -2583,8 +2572,8 @@ const char * ...@@ -2583,8 +2572,8 @@ const char *
quote_identifier(const char *ident) quote_identifier(const char *ident)
{ {
/* /*
* Can avoid quoting if ident starts with a lowercase letter and * Can avoid quoting if ident starts with a lowercase letter or underscore
* contains only lowercase letters, digits, and underscores, *and* is * and contains only lowercase letters, digits, and underscores, *and* is
* not any SQL keyword. Otherwise, supply quotes. * not any SQL keyword. Otherwise, supply quotes.
*/ */
bool safe; bool safe;
...@@ -2594,7 +2583,7 @@ quote_identifier(const char *ident) ...@@ -2594,7 +2583,7 @@ quote_identifier(const char *ident)
* would like to use <ctype.h> macros here, but they might yield * would like to use <ctype.h> macros here, but they might yield
* unwanted locale-specific results... * unwanted locale-specific results...
*/ */
safe = (ident[0] >= 'a' && ident[0] <= 'z'); safe = ((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_');
if (safe) if (safe)
{ {
const char *ptr; const char *ptr;
......
This diff is collapsed.
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