The attached patch contains the following modifications for SqlTask:
- 'append' boolean attribute. Indicates whether to overwrite output file or to append to it. Similar to the one in Ant's SqlExec task. - 'showheaders' boolean attribute. Display schema information or not. Similar to the one in Ant's SqlExec task. - 'quotechar' string attribute. Character(s) to place arround column values to prevent collisions with for instance the comma character. Defaults to empty string. - changed column separator from " " (two spaces) to ", " (comma-space, without the quotes). Similar to Ant's SqlExec task.
Regards, Roelof Blom.
Index: src/Tasks/SqlTask.cs =================================================================== RCS file: /cvsroot/nantcontrib/NAntContrib/src/Tasks/SqlTask.cs,v retrieving revision 1.12 diff -u -r1.12 SqlTask.cs --- src/Tasks/SqlTask.cs 16 Oct 2003 08:00:27 -0000 1.12 +++ src/Tasks/SqlTask.cs 26 Apr 2004 12:10:45 -0000 @@ -114,6 +114,9 @@ private int _commandTimeout; TextWriter _outputWriter; private bool _expandProps = true; + private bool _append = false; + private bool _showHeaders = true; + private string _quoteChar = ""; #endregion Private Instance Fields @@ -230,6 +233,38 @@ set { _useTransaction = value; } } + /// <summary> + /// Whether output should be appended to or overwrite + /// an existing file. Defaults to false. + /// </summary> + [TaskAttribute("append")] + [BooleanValidator()] + public bool Append { + get { return _append; } + set { _append = value; } + } + + /// <summary> + /// If set to <see langword="true" />, prints headers for result sets. + /// The default is <see langword="true" />. + /// </summary> + [TaskAttribute("showheaders")] + [BooleanValidator()] + public bool ShowHeaders { + get { return _showHeaders; } + set { _showHeaders = value; } + } + + /// <summary> + /// Gets or sets the character or characters to surround result columns with when printing, + /// the default is an empty string. + /// </summary> + [TaskAttribute("quotechar")] + public string QuoteChar { + get { return _quoteChar; } + set { _quoteChar = value; } + } + #endregion Public Instance Properties #region Protected Instance Properties @@ -268,7 +303,11 @@ protected override void ExecuteTask() { if (Output != null) { try { - _outputWriter = new StreamWriter(File.OpenWrite(Output)); + if( Append ) { + _outputWriter = File.AppendText(Output); + } else { + _outputWriter = File.CreateText(Output); + } } catch (IOException ex) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Cannot open output file '{0}'.", Output), Location, ex); @@ -391,31 +430,42 @@ private void ProcessResults(IDataReader results, TextWriter writer) { try { do { - // output header - DataTable schema = results.GetSchemaTable(); - if (schema != null) { - writer.WriteLine(); - - int totalHeaderSize = 0; - foreach (DataRow row in schema.Rows) { - string columnName = row["ColumnName"].ToString(); - writer.Write(columnName + new string(' ', 2)); - totalHeaderSize += columnName.Length + 2; - } - - writer.WriteLine(); - - if (totalHeaderSize > 2) { - writer.WriteLine(new String('-', totalHeaderSize - 2)); + if( ShowHeaders ) { + // output header + DataTable schema = results.GetSchemaTable(); + if (schema != null) { + writer.WriteLine(); + + int totalHeaderSize = 0; + foreach (DataRow row in schema.Rows) { + string columnName = row["ColumnName"].ToString(); + writer.Write(columnName + new string(' ', 2)); + totalHeaderSize += columnName.Length + 2; + } + + writer.WriteLine(); + + if (totalHeaderSize > 2) { + writer.WriteLine(new String('-', totalHeaderSize - 2)); + } } } // output results while (results.Read()) { + bool first = true; + StringBuilder line = new StringBuilder( 100 ); for (int i = 0; i < results.FieldCount; i++ ) { - writer.Write(results[i].ToString() + new string(' ', 2)); + if( first ) { + first = false; + } else { + line.Append( ", " ); + } + line.Append( QuoteChar ); + line.Append( results[i].ToString() ); + line.Append( QuoteChar ); } - writer.WriteLine(); + writer.WriteLine( line.ToString() ); } writer.WriteLine(); } while (results.NextResult());