Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 613e21c281
Fetching contributors…

Cannot retrieve contributors at this time

227 lines (193 sloc) 7.602 kB
using System;
using System.Data;
using System.IO;
using FluentMigrator.Builders.Execute;
namespace FluentMigrator.Runner.Processors.SqlServer
{
public sealed class SqlServer2000Processor : ProcessorBase
{
private readonly IDbFactory factory;
public IDbConnection Connection { get; private set; }
public IDbTransaction Transaction { get; private set; }
public bool WasCommitted { get; private set; }
public SqlServer2000Processor(IDbConnection connection, IMigrationGenerator generator, IAnnouncer announcer, IMigrationProcessorOptions options, IDbFactory factory)
: base(generator, announcer, options)
{
this.factory = factory;
Connection = connection;
connection.Open();
BeginTransaction();
}
public override string DatabaseType
{
get { return "SqlServer2000"; }
}
public override bool SchemaExists(string schemaName)
{
return true;
}
public override bool TableExists(string schemaName, string tableName)
{
try
{
return Exists("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{0}'", FormatSqlEscape(tableName));
}
catch (Exception e)
{
Console.WriteLine(e);
}
return false;
}
public override bool ColumnExists(string schemaName, string tableName, string columnName)
{
return Exists("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{1}' AND COLUMN_NAME = '{2}'", FormatSqlEscape(tableName), FormatSqlEscape(columnName));
}
public override bool ConstraintExists(string schemaName, string tableName, string constraintName)
{
return Exists("SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_CATALOG = DB_NAME() AND TABLE_NAME = '{1}' AND CONSTRAINT_NAME = '{2}'", FormatSqlEscape(tableName), FormatSqlEscape(constraintName));
}
public override bool IndexExists(string schemaName, string tableName, string indexName)
{
return Exists("SELECT NULL FROM sysindexes WHERE name = '{0}'", FormatSqlEscape(indexName));
}
public override DataSet ReadTableData(string schemaName, string tableName)
{
return Read("SELECT * FROM [{0}]", tableName);
}
public override DataSet Read(string template, params object[] args)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
var ds = new DataSet();
using (var command = factory.CreateCommand(String.Format(template, args), Connection, Transaction))
{
var adapter = factory.CreateDataAdapter(command);
adapter.Fill(ds);
return ds;
}
}
public override bool Exists(string template, params object[] args)
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
using (var command = factory.CreateCommand(String.Format(template, args), Connection, Transaction))
using (var reader = command.ExecuteReader())
{
return reader.Read();
}
}
public override void BeginTransaction()
{
Announcer.Say("Beginning Transaction");
Transaction = Connection.BeginTransaction();
}
public override void CommitTransaction()
{
Announcer.Say("Committing Transaction");
Transaction.Commit();
WasCommitted = true;
CloseConnection();
}
public override void RollbackTransaction()
{
Announcer.Say("Rolling back transaction");
Transaction.Rollback();
WasCommitted = true;
CloseConnection();
}
public override void CloseConnection()
{
if (Connection.State != ConnectionState.Closed)
{
Connection.Close();
}
}
public override void Execute(string template, params object[] args)
{
Process(String.Format(template, args));
}
protected override void Process(string sql)
{
Announcer.Sql(sql);
if (Options.PreviewOnly || string.IsNullOrEmpty(sql))
return;
if (Connection.State != ConnectionState.Open)
Connection.Open();
if (sql.Contains("GO"))
{
ExecuteBatchNonQuery(sql);
}
else
{
ExecuteNonQuery(sql);
}
}
private void ExecuteNonQuery(string sql)
{
using (var command = factory.CreateCommand(sql, Connection, Transaction))
{
try
{
command.CommandTimeout = Options.Timeout;
command.ExecuteNonQuery();
}
catch (Exception ex)
{
using (var message = new StringWriter())
{
message.WriteLine("An error occured executing the following sql:");
message.WriteLine(sql);
message.WriteLine("The error was {0}", ex.Message);
throw new Exception(message.ToString(), ex);
}
}
}
}
private void ExecuteBatchNonQuery(string sql)
{
sql += "\nGO"; // make sure last batch is executed.
string sqlBatch = string.Empty;
using (var command = factory.CreateCommand(string.Empty, Connection, Transaction))
{
try
{
foreach (string line in sql.Split(new[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries))
{
if (line.ToUpperInvariant().Trim() == "GO")
{
if (!string.IsNullOrEmpty(sqlBatch))
{
command.CommandText = sqlBatch;
command.ExecuteNonQuery();
sqlBatch = string.Empty;
}
}
else
{
sqlBatch += line + "\n";
}
}
}
catch (Exception ex)
{
using (var message = new StringWriter())
{
message.WriteLine("An error occured executing the following sql:");
message.WriteLine(sql);
message.WriteLine("The error was {0}", ex.Message);
throw new Exception(message.ToString(), ex);
}
}
}
}
public override void Process(PerformDBOperationExpression expression)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
if (expression.Operation != null)
expression.Operation(Connection, Transaction);
}
private static string FormatSqlEscape(string sql)
{
return sql.Replace("'", "''");
}
}
}
Jump to Line
Something went wrong with that request. Please try again.