]> granicus.if.org Git - postgresql/commitdiff
Another go-round with FigureColname, to produce less surprising results
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Oct 2001 21:48:51 +0000 (21:48 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Oct 2001 21:48:51 +0000 (21:48 +0000)
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.

src/backend/parser/parse_target.c

index c40792072b13b7f810ad8448d86ad1b5658df183..801dde811c606c63f966d8e143e528e9ec121001 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * 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 @@
 
 static List *ExpandAllTables(ParseState *pstate);
 static char *FigureColname(Node *node);
+static int FigureColnameInternal(Node *node, char **name);
 
 
 /*
@@ -460,15 +461,28 @@ ExpandAllTables(ParseState *pstate)
 static char *
 FigureColname(Node *node)
 {
+       char   *name = NULL;
+
+       FigureColnameInternal(node, &name);
+       if (name != NULL)
+               return name;
+       /* default result if we can't guess anything */
+       return "?column?";
+}
+
+static int
+FigureColnameInternal(Node *node, char **name)
+{
+       int             strength = 0;
+
        if (node == NULL)
-               return "?column?";
+               return strength;
 
        switch (nodeTag(node))
        {
                case T_Ident:
-                       return ((Ident *) node)->name;
-               case T_A_Const:
-                       return (FigureColname((Node *)((A_Const *) node)->typename));
+                       *name = ((Ident *) node)->name;
+                       return 2;
                case T_Attr:
                        {
                                List       *attrs = ((Attr *) node)->attrs;
@@ -477,36 +491,45 @@ FigureColname(Node *node)
                                {
                                        while (lnext(attrs) != NIL)
                                                attrs = lnext(attrs);
-                                       return strVal(lfirst(attrs));
+                                       *name = strVal(lfirst(attrs));
+                                       return 2;
                                }
                        }
                        break;
                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: 
+                       strength = FigureColnameInternal(((TypeCast *) node)->arg,
+                                                                                        name);
+                       if (strength <= 1)
                        {
-                               char       *name;
-
-                               name = FigureColname(((TypeCast *) node)->arg);
-                               if (strcmp(name, "?column?") == 0)
-                                 name = FigureColname((Node *)((TypeCast *) node)->typename);
-                               return name;
+                               if (((TypeCast *) node)->typename != NULL)
+                               {
+                                       *name = ((TypeCast *) node)->typename->name;
+                                       return 1;
+                               }
                        }
                        break;
                case T_CaseExpr:
+                       strength = FigureColnameInternal(((CaseExpr *) node)->defresult,
+                                                                                        name);
+                       if (strength <= 1)
                        {
-                               char       *name;
-
-                               name = FigureColname(((CaseExpr *) node)->defresult);
-                               if (strcmp(name, "?column?") == 0)
-                                       name = "case";
-                               return name;
+                               *name = "case";
+                               return 1;
                        }
                        break;
-               case T_TypeName:
-                       return ((TypeName *) node)->name;
                default:
                        break;
        }
-       return "?column?";
+
+       return strength;
 }