Commit 7ecc40c2 authored by Tom Lane's avatar Tom Lane

Another go-round with FigureColname, to produce less surprising results

for nested typecasts.  It now produces a column header of 'timestamptz'
for 'SELECT CURRENT_TIMESTAMP', rather than 'text' as it was doing for
awhile there.
parent 71f2993c
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.73 2001/09/28 08:09:09 thomas Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.74 2001/10/08 21:48:51 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
static List *ExpandAllTables(ParseState *pstate); static List *ExpandAllTables(ParseState *pstate);
static char *FigureColname(Node *node); static char *FigureColname(Node *node);
static int FigureColnameInternal(Node *node, char **name);
/* /*
...@@ -460,15 +461,28 @@ ExpandAllTables(ParseState *pstate) ...@@ -460,15 +461,28 @@ ExpandAllTables(ParseState *pstate)
static char * static char *
FigureColname(Node *node) FigureColname(Node *node)
{ {
if (node == NULL) char *name = NULL;
FigureColnameInternal(node, &name);
if (name != NULL)
return name;
/* default result if we can't guess anything */
return "?column?"; return "?column?";
}
static int
FigureColnameInternal(Node *node, char **name)
{
int strength = 0;
if (node == NULL)
return strength;
switch (nodeTag(node)) switch (nodeTag(node))
{ {
case T_Ident: case T_Ident:
return ((Ident *) node)->name; *name = ((Ident *) node)->name;
case T_A_Const: return 2;
return (FigureColname((Node *)((A_Const *) node)->typename));
case T_Attr: case T_Attr:
{ {
List *attrs = ((Attr *) node)->attrs; List *attrs = ((Attr *) node)->attrs;
...@@ -477,36 +491,45 @@ FigureColname(Node *node) ...@@ -477,36 +491,45 @@ FigureColname(Node *node)
{ {
while (lnext(attrs) != NIL) while (lnext(attrs) != NIL)
attrs = lnext(attrs); attrs = lnext(attrs);
return strVal(lfirst(attrs)); *name = strVal(lfirst(attrs));
return 2;
} }
} }
break; break;
case T_FuncCall: case T_FuncCall:
return ((FuncCall *) node)->funcname; *name = ((FuncCall *) node)->funcname;
return 2;
case T_A_Const:
if (((A_Const *) node)->typename != NULL)
{
*name = ((A_Const *) node)->typename->name;
return 1;
}
break;
case T_TypeCast: case T_TypeCast:
strength = FigureColnameInternal(((TypeCast *) node)->arg,
name);
if (strength <= 1)
{ {
char *name; if (((TypeCast *) node)->typename != NULL)
{
name = FigureColname(((TypeCast *) node)->arg); *name = ((TypeCast *) node)->typename->name;
if (strcmp(name, "?column?") == 0) return 1;
name = FigureColname((Node *)((TypeCast *) node)->typename); }
return name;
} }
break; break;
case T_CaseExpr: case T_CaseExpr:
strength = FigureColnameInternal(((CaseExpr *) node)->defresult,
name);
if (strength <= 1)
{ {
char *name; *name = "case";
return 1;
name = FigureColname(((CaseExpr *) node)->defresult);
if (strcmp(name, "?column?") == 0)
name = "case";
return name;
} }
break; break;
case T_TypeName:
return ((TypeName *) node)->name;
default: default:
break; break;
} }
return "?column?";
return strength;
} }
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