Fix ruleutils to print "INSERT INTO foo DEFAULT VALUES" correctly.
authorTom Lane <[email protected]>
Fri, 19 Oct 2012 17:40:34 +0000 (13:40 -0400)
committerTom Lane <[email protected]>
Fri, 19 Oct 2012 17:40:34 +0000 (13:40 -0400)
Per bug #7615 from Marko Tiikkaja.  Apparently nobody ever tried this
case before ...

src/backend/utils/adt/ruleutils.c

index 6b57a8c9a5d06490f8af1d9ae7af95ede58b5cf5..7ac2848186ddaf21e620755d5eee38dacfc86f2a 100644 (file)
@@ -2297,8 +2297,8 @@ get_insert_query_def(Query *query, deparse_context *context)
    List       *strippedexprs;
 
    /*
-    * If it's an INSERT ... SELECT or VALUES (...), (...), ... there will be
-    * a single RTE for the SELECT or VALUES.
+    * If it's an INSERT ... SELECT or multi-row VALUES, there will be a
+    * single RTE for the SELECT or VALUES.  Plain VALUES has neither.
     */
    foreach(l, query->rtable)
    {
@@ -2332,7 +2332,7 @@ get_insert_query_def(Query *query, deparse_context *context)
        context->indentLevel += PRETTYINDENT_STD;
        appendStringInfoChar(buf, ' ');
    }
-   appendStringInfo(buf, "INSERT INTO %s (",
+   appendStringInfo(buf, "INSERT INTO %s ",
                     generate_relation_name(rte->relid));
 
    /*
@@ -2349,6 +2349,8 @@ get_insert_query_def(Query *query, deparse_context *context)
        values_cell = NULL;
    strippedexprs = NIL;
    sep = "";
+   if (query->targetList)
+       appendStringInfoChar(buf, '(');
    foreach(l, query->targetList)
    {
        TargetEntry *tle = (TargetEntry *) lfirst(l);
@@ -2385,7 +2387,8 @@ get_insert_query_def(Query *query, deparse_context *context)
                                                       context, true));
        }
    }
-   appendStringInfo(buf, ") ");
+   if (query->targetList)
+       appendStringInfo(buf, ") ");
 
    if (select_rte)
    {
@@ -2398,7 +2401,7 @@ get_insert_query_def(Query *query, deparse_context *context)
        /* Add the multi-VALUES expression lists */
        get_values_def(values_rte->values_lists, context);
    }
-   else
+   else if (strippedexprs)
    {
        /* Add the single-VALUES expression list */
        appendContextKeyword(context, "VALUES (",
@@ -2406,6 +2409,11 @@ get_insert_query_def(Query *query, deparse_context *context)
        get_rule_expr((Node *) strippedexprs, context, false);
        appendStringInfoChar(buf, ')');
    }
+   else
+   {
+       /* No expressions, so it must be DEFAULT VALUES */
+       appendStringInfo(buf, "DEFAULT VALUES");
+   }
 
    /* Add RETURNING if present */
    if (query->returningList)