Update of /cvsroot/nantcontrib/NAntContrib/src/Tasks
In directory usw-pr-cvs1:/tmp/cvs-serv859/src/Tasks
Modified Files:
SqlTask.cs
Log Message:
Merged SqlTask changes proposed by Jay Turpin and Sean Demartino
Index: SqlTask.cs
===================================================================
RCS file: /cvsroot/nantcontrib/NAntContrib/src/Tasks/SqlTask.cs,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** SqlTask.cs 30 Aug 2002 02:50:15 -0000 1.7
--- SqlTask.cs 29 Oct 2002 02:29:58 -0000 1.8
***************
*** 29,34 ****
using NAnt.Contrib.Util;
! namespace NAnt.Contrib.Tasks
! {
/// <summary>
--- 29,34 ----
using NAnt.Contrib.Util;
! namespace NAnt.Contrib.Tasks
! {
/// <summary>
***************
*** 38,42 ****
/// You can specify a set of sql statements inside the
/// sql element, or execute them from a text file that contains them. You can also
! /// choose to execute the statements in a single batch, or execute them one by one
/// (even inside a transaction, if you want to).
/// </remarks>
--- 38,42 ----
/// You can specify a set of sql statements inside the
/// sql element, or execute them from a text file that contains them. You can also
! /// choose to execute the statements in a single batch, or execute them one by one
/// (even inside a transaction, if you want to).
/// </remarks>
***************
*** 44,48 ****
/// <para>Execute a set of statements inside a transaction</para>
/// <code><![CDATA[
! /// <sql
/// connstring="Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=Pruebas; Integrated Security=SSPI"
/// transaction="true"
--- 44,48 ----
/// <para>Execute a set of statements inside a transaction</para>
/// <code><![CDATA[
! /// <sql
/// connstring="Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=Pruebas; Integrated Security=SSPI"
/// transaction="true"
***************
*** 52,63 ****
/// INSERT INTO jobs (job_desc, min_lvl, max_lvl) VALUES('Other Job', 09, 43);
/// SELECT * FROM jobs;
! /// </sql>
/// ]]></code>
/// </example>
/// <example>
! /// <para>Execute a set of statements from a file and
/// write all query results to a file</para>
/// <code><![CDATA[
! /// <sql
/// connstring="Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=Pruebas; Integrated Security=SSPI"
/// transaction="true"
--- 52,63 ----
/// INSERT INTO jobs (job_desc, min_lvl, max_lvl) VALUES('Other Job', 09, 43);
/// SELECT * FROM jobs;
! /// </sql>
/// ]]></code>
/// </example>
/// <example>
! /// <para>Execute a set of statements from a file and
/// write all query results to a file</para>
/// <code><![CDATA[
! /// <sql
/// connstring="Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=Pruebas; Integrated Security=SSPI"
/// transaction="true"
***************
*** 72,76 ****
/// <para>Execute a SQL script generated by SQL Server Enterprise Manager</para>
/// <code><![CDATA[
! /// <sql
/// connstring="Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=Pruebas; Integrated Security=SSPI"
/// transaction="true"
--- 72,76 ----
/// <para>Execute a SQL script generated by SQL Server Enterprise Manager</para>
/// <code><![CDATA[
! /// <sql
/// connstring="Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=Pruebas; Integrated Security=SSPI"
/// transaction="true"
***************
*** 94,107 ****
private bool _useTransaction = true;
private string _output;
! private string _statements;
private bool _batch = true;
private bool _expandProps = true;
/// <summary>
! /// Connection string used to access database.
/// This should be an OleDB connection string.
/// </summary>
[ TaskAttribute("connstring", Required=true) ]
! public string ConnectionString {
get { return _connectionString; }
set { _connectionString = value; }
--- 94,108 ----
private bool _useTransaction = true;
private string _output;
! public string _embeddedSqlStatements;
private bool _batch = true;
+ TextWriter _outputWriter = null;
private bool _expandProps = true;
/// <summary>
! /// Connection string used to access database.
/// This should be an OleDB connection string.
/// </summary>
[ TaskAttribute("connstring", Required=true) ]
! public string ConnectionString {
get { return _connectionString; }
set { _connectionString = value; }
***************
*** 159,164 ****
/// Default is Normal.
/// </remarks>
! [ TaskAttribute("delimstyle", Required=true) ]
! public DelimiterStyle DelimiterStyle {
get { return _delimiterStyle; }
set { _delimiterStyle = value; }
--- 160,165 ----
/// Default is Normal.
/// </remarks>
! [ TaskAttribute("delimstyle", Required=true) ]
! public DelimiterStyle DelimiterStyle {
get { return _delimiterStyle; }
set { _delimiterStyle = value; }
***************
*** 170,174 ****
/// </summary>
[ TaskAttribute("print"), BooleanValidator() ]
! public bool Print {
get { return _print; }
set { _print = value; }
--- 171,175 ----
/// </summary>
[ TaskAttribute("print"), BooleanValidator() ]
! public bool Print {
get { return _print; }
set { _print = value; }
***************
*** 190,194 ****
/// </summary>
[ TaskAttribute("transaction"), BooleanValidator() ]
! public bool UseTransaction {
get { return _useTransaction; }
set { _useTransaction = value; }
--- 191,195 ----
/// </summary>
[ TaskAttribute("transaction"), BooleanValidator() ]
! public bool UseTransaction {
get { return _useTransaction; }
set { _useTransaction = value; }
***************
*** 197,208 ****
///<summary>
! ///Initializes task and ensures the
///supplied attributes are valid.
///</summary>
///<param name="taskNode">Xml node used to define this task instance.</param>
! protected override void InitializeTask(System.Xml.XmlNode taskNode)
{
! _statements = ((System.Xml.XmlElement)taskNode).InnerText;
! if ( (_statements=="") && (Source==null) ) {
throw new BuildException("No source file or statements have been specified.", Location);
}
--- 198,209 ----
///<summary>
! ///Initializes task and ensures the
///supplied attributes are valid.
///</summary>
///<param name="taskNode">Xml node used to define this task instance.</param>
! protected override void InitializeTask(System.Xml.XmlNode taskNode)
{
! _embeddedSqlStatements = ((System.Xml.XmlElement)taskNode).InnerText;
! if ( (_embeddedSqlStatements=="") && (Source==null) ) {
throw new BuildException("No source file or statements have been specified.", Location);
}
***************
*** 212,238 ****
/// This is where the work is done
/// </summary>
! protected override void ExecuteTask()
{
bool closeWriter = false;
! TextWriter writer = null;
! if ( Output != null ) {
! try {
! writer = new StreamWriter(File.OpenWrite(Output));
closeWriter = true;
! } catch ( IOException ioe ) {
throw new BuildException("Cannot Open output file " + ioe.Message);
}
! } else {
! writer = Console.Out;
}
- SqlHelper sqlHelper
- = new SqlHelper(ConnectionString, UseTransaction);
try {
! if ( Batch )
! ExecuteStatementsInBatch(sqlHelper, writer);
! else
! ExecuteStatements(sqlHelper, writer);
} catch ( Exception e ) {
sqlHelper.Close(false);
--- 213,256 ----
/// This is where the work is done
/// </summary>
! protected override void ExecuteTask()
{
bool closeWriter = false;
!
! if ( Output != null ) {
! try {
! _outputWriter = new StreamWriter(File.OpenWrite(Output));
closeWriter = true;
! } catch ( IOException ioe ) {
throw new BuildException("Cannot Open output file " + ioe.Message);
}
! } else {
! _outputWriter = Console.Out;
}
+ StringBuilder message = new StringBuilder();
+ message.Append ("SQL Task:" + Environment.NewLine);
+ message.Append ("Connection String: " + ConnectionString + Environment.NewLine);
+ message.Append ("Use Transaction?: " + UseTransaction.ToString() + Environment.NewLine);
+ message.Append ("Batch Sql Statements?: " + this.Batch.ToString() + Environment.NewLine);
+ message.Append ("Batch Delimiter: " + this.Delimiter + Environment.NewLine);
+ message.Append ("Delimiter Style: " + this.DelimiterStyle.ToString() + Environment.NewLine);
+ message.Append ("Fail On Error?: " + this.FailOnError.ToString() + Environment.NewLine);
+ message.Append ("Source script file: " + this.Source + Environment.NewLine);
+ message.Append ("Output file: " + this.Output + Environment.NewLine);
+
+ Log.WriteLineIf(this.Verbose == true, message.ToString());
+
+ if ( this.Verbose ) {
+ _outputWriter.WriteLine(message.ToString());
+ }
+
+ SqlHelper sqlHelper = new SqlHelper(ConnectionString, UseTransaction);
+ sqlHelper.Connection.InfoMessage += new OleDbInfoMessageEventHandler(SqlMessageHandler);
try {
! if ( Batch )
! ExecuteStatementsInBatch(sqlHelper);
! else
! ExecuteStatements(sqlHelper);
} catch ( Exception e ) {
sqlHelper.Close(false);
***************
*** 242,246 ****
sqlHelper.Close(true);
if ( closeWriter ) {
! writer.Close();
}
}
--- 260,264 ----
sqlHelper.Close(true);
if ( closeWriter ) {
! _outputWriter.Close();
}
}
***************
*** 251,267 ****
/// </summary>
/// <param name="sqlHelper"></param>
! /// <param name="writer"></param>
! private void ExecuteStatements(SqlHelper sqlHelper, TextWriter writer)
{
SqlStatementList list = CreateStatementList();
if ( Source == null ) {
! list.ParseSql(_statements);
} else {
list.ParseSqlFromFile(Source);
}
! foreach ( string statement in list ) {
! IDataReader results = sqlHelper.Execute(statement);
! ProcessResults(results, writer);
}
}
--- 269,304 ----
/// </summary>
/// <param name="sqlHelper"></param>
! private void ExecuteStatements(SqlHelper sqlHelper)
{
SqlStatementList list = CreateStatementList();
+
if ( Source == null ) {
! list.ParseSql(_embeddedSqlStatements);
} else {
list.ParseSqlFromFile(Source);
}
! foreach ( string statement in list )
! {
!
! Log.WriteLineIf(this.Verbose, "SQL Statement:");
! Log.WriteLineIf(this.Verbose, statement);
!
! if ( this.Verbose ) {
! _outputWriter.WriteLine();
! _outputWriter.WriteLine("SQL Statement:");
! _outputWriter.WriteLine(statement);
! }
!
! IDataReader results = null;
! try {
! results = sqlHelper.Execute(statement);
! } catch (Exception error) {
! Log.WriteLine("SQL Error: " + error.Message);
! Log.WriteLine("Statement: " + statement);
! } finally {
! ProcessResults(results, _outputWriter);
! }
!
}
}
***************
*** 271,289 ****
/// </summary>
/// <param name="sqlHelper"></param>
! /// <param name="writer"></param>
! private void ExecuteStatementsInBatch(SqlHelper sqlHelper, TextWriter writer)
{
SqlStatementList list = CreateStatementList();
! SqlStatementAdapter adapter
= new SqlStatementAdapter(list);
string sql = null;
if ( Source == null ) {
! sql = adapter.AdaptSql(_statements);
} else {
sql = adapter.AdaptSqlFile(Source);
}
IDataReader results = sqlHelper.Execute(sql);
! ProcessResults(results, writer);
}
--- 308,335 ----
/// </summary>
/// <param name="sqlHelper"></param>
! private void ExecuteStatementsInBatch(SqlHelper sqlHelper)
{
SqlStatementList list = CreateStatementList();
! SqlStatementAdapter adapter
= new SqlStatementAdapter(list);
+
string sql = null;
if ( Source == null ) {
! sql = adapter.AdaptSql(_embeddedSqlStatements);
} else {
sql = adapter.AdaptSqlFile(Source);
}
+ Log.WriteLineIf(this.Verbose, "SQL Statement:");
+ Log.WriteLineIf(this.Verbose, sql);
+
+ if ( this.Verbose ) {
+ _outputWriter.WriteLine();
+ _outputWriter.WriteLine("SQL Statement:");
+ _outputWriter.WriteLine(sql);
+ }
+
IDataReader results = sqlHelper.Execute(sql);
! ProcessResults(results, _outputWriter);
}
***************
*** 315,319 ****
writer.WriteLine();
}
! // writer.WriteLine();
} while ( results.NextResult() );
--- 361,365 ----
writer.WriteLine();
}
! writer.WriteLine();
} while ( results.NextResult() );
***************
*** 330,335 ****
private SqlStatementList CreateStatementList()
{
! SqlStatementList list =
new SqlStatementList(Delimiter, DelimiterStyle);
if ( ExpandProperties ) {
list.Properties = Project.Properties;
--- 376,382 ----
private SqlStatementList CreateStatementList()
{
! SqlStatementList list =
new SqlStatementList(Delimiter, DelimiterStyle);
+
if ( ExpandProperties ) {
list.Properties = Project.Properties;
***************
*** 338,341 ****
--- 385,393 ----
}
+ private void SqlMessageHandler(object sender, OleDbInfoMessageEventArgs e)
+ {
+ _outputWriter.WriteLine(e.Message);
+ Log.WriteLineIf(this.Verbose, e.Message);
+ }
} // class SqlTask
|