ASF Bugzilla – Attachment 11372 Details for
Bug 28681
[PATCH] Xml output for the SQL task
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
SQLExec.java.diff
SQLExec.java.diff (text/plain), 5.96 KB, created by
Nicola Ken Barozzi
on 2004-04-29 10:56:25 UTC
(
hide
)
Description:
SQLExec.java.diff
Filename:
MIME Type:
Creator:
Nicola Ken Barozzi
Created:
2004-04-29 10:56:25 UTC
Size:
5.96 KB
patch
obsolete
>Index: SQLExec.java >=================================================================== >RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/taskdefs/SQLExec.java,v >retrieving revision 1.67 >diff -u -r1.67 SQLExec.java >--- SQLExec.java 23 Feb 2004 19:28:23 -0000 1.67 >+++ SQLExec.java 29 Apr 2004 10:50:46 -0000 >@@ -175,6 +175,13 @@ > */ > private boolean escapeProcessing = true; > >+ /** >+ * Argument to Statement.setEscapeProcessing >+ * >+ * @since Ant 1.6 >+ */ >+ private String outputFormat = "csv"; >+ > /** > * Set the name of the SQL file to be run. > * Required unless statements are enclosed in the build file >@@ -313,6 +320,21 @@ > public void setEscapeProcessing(boolean enable) { > escapeProcessing = enable; > } >+ >+ /** >+ * Set escape processing for statements. >+ * @param type the format type of the output, currently supports csv and xml. >+ * @since Ant 1.6 >+ */ >+ public void setOutputFormat(String type) { >+ if(type.equals("xml") || type.equals("csv")){ >+ outputFormat = type; >+ } >+ else{ >+ log("outputType "+type+" not supported, defaulting to csv format.", Project.MSG_WARN); >+ outputFormat = "csv"; >+ } >+ } > > /** > * Load the sql file and then execute it >@@ -542,7 +564,9 @@ > > if (print) { > StringBuffer line = new StringBuffer(); >+ if(outputFormat.equals("xml")) line.append("<!--"); > line.append(updateCountTotal + " rows affected"); >+ if(outputFormat.equals("xml")) line.append("-->"); > out.println(line); > } > >@@ -572,40 +596,103 @@ > rs = statement.getResultSet(); > if (rs != null) { > log("Processing new result set.", Project.MSG_VERBOSE); >+ boolean xml = outputFormat.equals("xml")? true: false; >+ boolean csv = outputFormat.equals("csv")? true: false; >+ > ResultSetMetaData md = rs.getMetaData(); > int columnCount = md.getColumnCount(); > StringBuffer line = new StringBuffer(); >+ >+ if(xml) { >+ out.println("<?xml version=\"1.0\" ?>"); >+ out.println("<sqlresults>"); >+ } >+ > if (showheaders) { >+ if(xml) out.println("<headers>"); >+ > for (int col = 1; col < columnCount; col++) { >- line.append(md.getColumnName(col)); >- line.append(","); >+ if(xml) line.append("<header>"); >+ line.append(escape(md.getColumnName(col),xml)); >+ if(xml) >+ line.append("</header>"); >+ else >+ line.append(","); > } >- line.append(md.getColumnName(columnCount)); >+ >+ if(xml) line.append("<header>"); >+ line.append(escape(md.getColumnName(columnCount),xml)); >+ if(xml) line.append("</header>"); > out.println(line); >+ if(xml) out.println("</headers>"); > line = new StringBuffer(); > } > while (rs.next()) { > boolean first = true; >+ if(xml) out.println("<row>"); > for (int col = 1; col <= columnCount; col++) { >- String columnValue = rs.getString(col); >- if (columnValue != null) { >- columnValue = columnValue.trim(); >- } >- >- if (first) { >- first = false; >- } else { >- line.append(","); >+ if(xml) line.append("<cell header=\""); >+ if(xml) line.append(escape(md.getColumnName(col),xml)); >+ if(xml) line.append("\">"); >+ String columnValue = escape(rs.getString(col),xml).trim(); >+ if(csv) { >+ if (first) { >+ first = false; >+ } else { >+ line.append(","); >+ } > } > line.append(columnValue); >+ if(xml) line.append("</cell>"); > } > out.println(line); >+ if(xml) out.println("</row>"); > line = new StringBuffer(); > } >+ >+ if(xml) { >+ out.println("</sqlresults>"); >+ } > } > out.println(); > } > >+ private String escape(String str, boolean xml) { >+ if (str == null) { >+ return ""; >+ } >+ >+ if(!xml){ >+ return str; >+ } >+ >+ StringBuffer buf = new StringBuffer(str.length() * 2); >+ int i; >+ for (i = 0; i < str.length(); ++i) { >+ char ch = str.charAt(i); >+ int intValue = ch; >+ >+ if(intValue == 34){ >+ buf.append("""); >+ } else if(intValue == 38){ >+ buf.append("&"); >+ } else if(intValue == 60){ >+ buf.append("<"); >+ } else if(intValue == 62){ >+ buf.append(">"); >+ } else if(intValue == 39){ >+ buf.append("'"); >+ } else if (ch > 0x7F) { >+ buf.append("&#"); >+ buf.append(intValue); >+ buf.append(";"); >+ } else { >+ buf.append(ch); >+ } >+ } >+ return buf.toString(); >+ } >+ > /** > * The action a task should perform on an error, > * one of "continue", "stop" and "abort"
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 28681
: 11372 |
11425
|
11433
|
11661