From 3a7d221a636a2800c00d7bc9ffa5cd205ac2d1cd Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Mon, 3 Aug 2009 00:58:23 +0200
Subject: [PATCH] reduce json/xml duplication + fix duplicate output

---
 src/backend/commands/explain.c |   90 ++++++++++++----------------------------
 1 files changed, 27 insertions(+), 63 deletions(-)

diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 9f86444..53e92c1 100644
*** a/src/backend/commands/explain.c
--- b/src/backend/commands/explain.c
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 427,439 ****
  		if (es->format == EXPLAIN_FORMAT_TEXT)
  			appendStringInfo(es->str, "Total runtime: %.3f ms\n",
  							 1000.0 * totaltime);
! 		else if (es->format == EXPLAIN_FORMAT_XML)
! 			appendStringInfo(es->str,
! 							 "    <Total-Runtime>%.3f</Total-Runtime>\n",
! 							 1000.0 * totaltime);
! 		else if (es->format == EXPLAIN_FORMAT_JSON)
! 			appendStringInfo(es->str, ",\n    \"Total runtime\" : %.3f",
! 							 1000.0 * totaltime);
  	}
  
  	/* Closing boilerplate */
--- 427,439 ----
  		if (es->format == EXPLAIN_FORMAT_TEXT)
  			appendStringInfo(es->str, "Total runtime: %.3f ms\n",
  							 1000.0 * totaltime);
! 		else{
! 			char b[256];
! 			sprintf(b, "%.3f", 1000.0 * totaltime);
! 			es->indent++;
! 			ExplainPropertyText("Total Runtime", b, 1, es);
! 			es->indent--;
! 		}
  	}
  
  	/* Closing boilerplate */
*************** report_triggers(ResultRelInfo *rInfo, bo
*** 535,540 ****
--- 535,543 ----
  		if (OidIsValid(trig->tgconstraint))
  			conname = get_constraint_name(trig->tgconstraint);
  
+ 		es->indent += 3;
+ 		es->needs_separator = 0;
+ 
  		/*
  		 * In text mode, we avoid printing both the trigger name and the
  		 * constraint name unless VERBOSE is specified.  In XML or JSON
*************** report_triggers(ResultRelInfo *rInfo, bo
*** 547,597 ****
  			else
  				appendStringInfoString(es->str, "Trigger");
  		}
! 		else if (es->format == EXPLAIN_FORMAT_XML)
! 		{
! 			appendStringInfoString(es->str, "        <Trigger-Name>");
! 			escape_xml(es->str, trig->tgname);
! 			appendStringInfoString(es->str, "</Trigger-Name>\n");
! 		}
! 		else if (es->format == EXPLAIN_FORMAT_JSON)
  		{
! 			appendStringInfo(es->str, "\n        \"Trigger Name\": ");
! 			escape_json(es->str, trig->tgname);
  		}
  
  		if (conname != NULL)
  		{
  			if (es->format == EXPLAIN_FORMAT_TEXT)
  				appendStringInfo(es->str, " for constraint %s", conname);
! 			else if (es->format == EXPLAIN_FORMAT_XML)
! 			{
! 				appendStringInfoString(es->str, "        <Constraint-Name>");
! 				escape_xml(es->str, conname);
! 				appendStringInfoString(es->str, "</Constraint-Name>\n");
! 			}
! 			else if (es->format == EXPLAIN_FORMAT_JSON)
  			{
! 				appendStringInfo(es->str, "\n        \"Constraint Name\": ");
! 				escape_json(es->str, conname);
  			}
  			pfree(conname);
  		}
- 		else
- 		{
- 			if (es->format == EXPLAIN_FORMAT_TEXT)
- 				appendStringInfo(es->str, "Trigger %s", trig->tgname);
- 			else if (es->format == EXPLAIN_FORMAT_XML)
- 			{
- 				appendStringInfoString(es->str, "        <Trigger-Name>");
- 				escape_xml(es->str, trig->tgname);
- 				appendStringInfoString(es->str, "</Trigger-Name>\n");
- 			}
- 			else if (es->format == EXPLAIN_FORMAT_JSON)
- 			{
- 				appendStringInfo(es->str, "\n        \"Trigger Name\": ");
- 				escape_json(es->str, trig->tgname);
- 			}
- 		}
  
  		if (es->format == EXPLAIN_FORMAT_TEXT)
  		{
--- 550,570 ----
  			else
  				appendStringInfoString(es->str, "Trigger");
  		}
! 		else
  		{
! 			ExplainPropertyText("Trigger Name", trig->tgname, 0, es);
  		}
  
  		if (conname != NULL)
  		{
  			if (es->format == EXPLAIN_FORMAT_TEXT)
  				appendStringInfo(es->str, " for constraint %s", conname);
! 			else
  			{
! 				ExplainPropertyText("Constraint Name", conname, 0, es);
  			}
  			pfree(conname);
  		}
  
  		if (es->format == EXPLAIN_FORMAT_TEXT)
  		{
*************** report_triggers(ResultRelInfo *rInfo, bo
*** 601,630 ****
  			appendStringInfo(es->str, ": time=%.3f calls=%.0f\n",
  							 1000.0 * instr->total, instr->ntuples);
  		}
! 		else if (es->format == EXPLAIN_FORMAT_XML)
! 		{
! 			/* In non-text formats, we always show the relation name. */
! 			appendStringInfoString(es->str, "        <Relation>");
! 			escape_xml(es->str,
! 					   RelationGetRelationName(rInfo->ri_RelationDesc));
! 			appendStringInfoString(es->str, "</Relation>\n");
! 			appendStringInfo(es->str, "        <Time>%.3f</Time>\n",
! 							 1000.0 * instr->total);
! 			appendStringInfo(es->str,"        <Calls>%.0f</Calls>\n",
! 							 instr->ntuples);
! 		}
! 		else if (es->format == EXPLAIN_FORMAT_JSON)
! 		{
! 			/* In non-text formats, we always show the relation name. */
! 			appendStringInfoString(es->str, ",\n        \"Relation\": ");
! 			escape_json(es->str,
! 						RelationGetRelationName(rInfo->ri_RelationDesc));
! 			appendStringInfo(es->str, ",\n        \"Time\": %.3f",
! 							 1000.0 * instr->total);
! 			appendStringInfo(es->str, ",\n        \"Calls\": %.0f",
! 							 instr->ntuples);
  		}
  
  		/* Closing boilerplate for this trigger */
  		if (es->format == EXPLAIN_FORMAT_XML)
  			appendStringInfoString(es->str, "      </Trigger>\n");
--- 574,594 ----
  			appendStringInfo(es->str, ": time=%.3f calls=%.0f\n",
  							 1000.0 * instr->total, instr->ntuples);
  		}
! 		else{
! 			char b[256];
! 			ExplainPropertyText("Relation",
! 								RelationGetRelationName(rInfo->ri_RelationDesc), 0, es);
! 
! 			sprintf(b, "%.3f", 1000.0 * instr->total);
! 			ExplainPropertyText("Time",
! 								b, 1, es);
! 
! 			sprintf(b, "%.0f", instr->ntuples);
! 			ExplainPropertyText("Calls",
! 								b, 1, es);
  		}
  
+ 		es->indent -= 3;
  		/* Closing boilerplate for this trigger */
  		if (es->format == EXPLAIN_FORMAT_XML)
  			appendStringInfoString(es->str, "      </Trigger>\n");
-- 
1.6.3.3.335.ge09a8

