Skip to content

Commit

Permalink
DNET-196, DNET-197
Browse files Browse the repository at this point in the history
  • Loading branch information
cincuranet committed Nov 22, 2008
1 parent 8c71e01 commit aa6ca18
Showing 1 changed file with 111 additions and 43 deletions.
154 changes: 111 additions & 43 deletions NETProvider/source/FirebirdSql/Data/Isql/FbBatchExecution.cs
Expand Up @@ -22,7 +22,7 @@
using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Globalization;

using FirebirdSql.Data.FirebirdClient;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class FbBatchExecution

#region · Fields ·

private StringCollection sqlStatements;
private FbStatementCollection sqlStatements;
private FbConnection sqlConnection;
private FbTransaction sqlTransaction;
private FbConnectionStringBuilder connectionString;
Expand All @@ -67,13 +67,13 @@ public class FbBatchExecution
/// <summary>
/// Represents the list of SQL statements for batch execution.
/// </summary>
public StringCollection SqlStatements
public FbStatementCollection SqlStatements
{
get
{
if (this.sqlStatements == null)
{
this.sqlStatements = new StringCollection();
this.sqlStatements = new FbStatementCollection();
}
return this.sqlStatements;
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public virtual void Execute(bool autoCommit)

foreach (string sqlStatement in this.SqlStatements)
{
if (sqlStatement == null || sqlStatement.Length == 0)
if (string.IsNullOrEmpty(sqlStatement))
{
continue;
}
Expand All @@ -174,27 +174,34 @@ public virtual void Execute(bool autoCommit)
FbDataReader dataReader = null;
SqlStatementType statementType = FbBatchExecution.GetStatementType(sqlStatement);

// Update command configuration
this.ProvideCommand().CommandText = sqlStatement;

// Check how transactions are going to be handled
if (statementType == SqlStatementType.Insert ||
statementType == SqlStatementType.Update ||
statementType == SqlStatementType.Delete)
if (!(statementType == SqlStatementType.Connect ||
statementType == SqlStatementType.CreateDatabase ||
statementType == SqlStatementType.Disconnect ||
statementType == SqlStatementType.DropDatabase))
{
// DML commands should be inside a transaction
if (this.sqlTransaction == null)
// Update command configuration
this.ProvideCommand();
this.sqlCommand.CommandText = sqlStatement;

// Check how transactions are going to be handled
if (statementType == SqlStatementType.Insert ||
statementType == SqlStatementType.Update ||
statementType == SqlStatementType.Delete)
{
this.sqlTransaction = this.sqlConnection.BeginTransaction();
// DML commands should be inside a transaction
if (this.sqlTransaction == null)
{
this.sqlTransaction = this.sqlConnection.BeginTransaction();
}
this.sqlCommand.Transaction = this.sqlTransaction;
}
else if (this.sqlTransaction != null && !(statementType == SqlStatementType.Commit || statementType == SqlStatementType.Rollback))
{
// Non DML Statements should be executed using
// implicit transaction support
this.sqlTransaction.Commit();
this.sqlTransaction = null;
}
this.sqlCommand.Transaction = this.sqlTransaction;
}
else if (this.sqlTransaction != null && (statementType != SqlStatementType.Commit && statementType != SqlStatementType.Rollback))
{
// Non DML Statements should be executed using
// implicit transaction support
this.sqlTransaction.Commit();
this.sqlTransaction = null;
}

try
Expand Down Expand Up @@ -235,18 +242,23 @@ public virtual void Execute(bool autoCommit)

this.ConnectToDatabase(sqlStatement);

requiresNewConnection = false;
this.requiresNewConnection = false;

// raise the event
this.OnCommandExecuted(sqlStatement, null, -1);
break;

case SqlStatementType.CreateDatabase:
#if (!NET_CF)
throw new NotImplementedException();
#else
throw new NotSupportedException();
#endif
// raise the event
this.OnCommandExecuting(null);

this.CreateDatabase(sqlStatement);

this.requiresNewConnection = false;

// raise the event
this.OnCommandExecuted(sqlStatement, null, -1);
break;

case SqlStatementType.CreateDomain:
case SqlStatementType.CreateException:
Expand All @@ -269,7 +281,7 @@ public virtual void Execute(bool autoCommit)
this.OnCommandExecuting(this.sqlCommand);

rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit);
requiresNewConnection = false;
this.requiresNewConnection = false;

// raise the event
this.OnCommandExecuted(sqlStatement, null, rowsAffected);
Expand All @@ -279,16 +291,28 @@ public virtual void Execute(bool autoCommit)
break;

case SqlStatementType.Disconnect:
// raise the event
this.OnCommandExecuting(null);

this.sqlConnection.Close();
FbConnection.ClearPool(this.sqlConnection);
this.requiresNewConnection = false;

// raise the event
this.OnCommandExecuted(sqlStatement, null, -1);
break;

case SqlStatementType.DropDatabase:
#if (!NET_CF)
throw new NotImplementedException();
#else
throw new NotSupportedException();
#endif
// raise the event
this.OnCommandExecuting(null);

FbConnection.DropDatabase(this.connectionString.ToString());
//this.sqlConnection = null;
this.requiresNewConnection = true;

// raise the event
this.OnCommandExecuted(null, null, -1);
break;

case SqlStatementType.DropDomain:
case SqlStatementType.DropException:
Expand Down Expand Up @@ -502,11 +526,55 @@ protected internal void CreateDatabase(string createDbStatement)
// [LENGTH [=] int [PAGE[S]]]
// [DEFAULT CHARACTER SET charset]
// [<secondary_file>];
#if (!NET_CF)
throw new NotImplementedException();
#else
throw new NotSupportedException();
#endif
int pageSize = 0;
StringParser parser = new StringParser(createDbStatement, false);
parser.Token = " ";
parser.ParseNext();
if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "CREATE")
{
throw new Exception("Malformed isql CREATE statement. Expected keyword CREATE but something else was found.");
}
parser.ParseNext(); // {DATABASE | SCHEMA}
parser.ParseNext();
this.connectionString.Database = parser.Result.Replace("'", "");
while (parser.ParseNext() != -1)
{
switch (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture))
{
case "USER":
parser.ParseNext();
this.connectionString.UserID = parser.Result.Replace("'", "");
break;

case "PASSWORD":
parser.ParseNext();
this.connectionString.Password = parser.Result.Replace("'", "");
break;

case "PAGE_SIZE":
parser.ParseNext();
if (parser.Result.Trim() == "=")
parser.ParseNext();
int.TryParse(parser.Result, out pageSize);
break;

case "DEFAULT":
parser.ParseNext();
if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "CHARACTER")
throw new Exception("Expected the keyword CHARACTER but something else was found.");

parser.ParseNext();
if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
throw new Exception("Expected the keyword SET but something else was found.");

parser.ParseNext();
this.connectionString.Charset = parser.Result;
break;
}
}
FbConnection.CreateDatabase(this.connectionString.ToString(), pageSize, true, false);
this.requiresNewConnection = true;
this.ProvideConnection();
}

/// <summary>
Expand Down Expand Up @@ -546,9 +614,9 @@ protected internal FbConnection ProvideConnection()
{
if (requiresNewConnection)
{
if ((this.sqlConnection != null) ||
(this.sqlConnection.State != ConnectionState.Closed) ||
(this.sqlConnection.State != ConnectionState.Broken))
if ((this.sqlConnection != null) &&
((this.sqlConnection.State != ConnectionState.Closed) ||
(this.sqlConnection.State != ConnectionState.Broken)))
{
this.sqlConnection.Close();
}
Expand Down

0 comments on commit aa6ca18

Please sign in to comment.