Skip to content

Commit

Permalink
Some refactoring, removed duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed May 5, 2018
1 parent 89e1b3e commit 1e6d6db
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 691 deletions.
237 changes: 64 additions & 173 deletions dg.Sql.Connector.MsSql/MsSqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,144 +23,120 @@ public static SqlConnection CreateSqlConnection(string connectionStringKey)
return new SqlConnection(FindConnectionString(connectionStringKey));
}

private SqlConnection _Connection = null;

public MsSqlConnector()
{
_Connection = CreateSqlConnection(null);
Connection = CreateSqlConnection(null);
}
public MsSqlConnector(string connectionStringKey)
{
_Connection = CreateSqlConnection(connectionStringKey);
Connection = CreateSqlConnection(connectionStringKey);
}
~MsSqlConnector()
{
Dispose(false);
}

public override void Close()
{
try
{
if (_Connection != null && _Connection.State != ConnectionState.Closed)
{
try
{
while (HasTransaction)
RollbackTransaction();
}
catch { /*ignore errors here*/ }

_Connection.Close();
}
}
catch { }
if (_Connection != null) _Connection.Dispose();
_Connection = null;
}

public override DbConnection Connection
{
get { return _Connection; }
}

#endregion

#region IDisposable

public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Close();
}
// Now clean up Native Resources (Pointers)
}

#endregion

#region Executing

public override int ExecuteNonQuery(string querySql)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
using (SqlCommand command = new SqlCommand(querySql, _Connection, _Transaction))
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();

using (var command = new SqlCommand(querySql, (SqlConnection)Connection, Transaction as SqlTransaction))
{
return command.ExecuteNonQuery();
}
}

public override int ExecuteNonQuery(DbCommand command)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
command.Connection = _Connection;
command.Transaction = _Transaction;
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
command.Connection = Connection;
command.Transaction = Transaction;
return command.ExecuteNonQuery();
}

public override object ExecuteScalar(string querySql)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
using (SqlCommand command = new SqlCommand(querySql, _Connection, _Transaction))
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();

using (var command = new SqlCommand(querySql, (SqlConnection)Connection, Transaction as SqlTransaction))
{
return command.ExecuteScalar();
}
}

public override object ExecuteScalar(DbCommand command)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
command.Connection = _Connection;
command.Transaction = _Transaction;
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
command.Connection = Connection;
command.Transaction = Transaction;
return command.ExecuteScalar();
}

public override DataReaderBase ExecuteReader(string querySql)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
using (SqlCommand command = new SqlCommand(querySql, _Connection, _Transaction))
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();

var command = new SqlCommand(querySql, (SqlConnection)Connection, Transaction as SqlTransaction);
try
{
return new DataReaderBase(command.ExecuteReader());
return new DataReaderBase(command.ExecuteReader(), command);
}
catch (Exception ex)
{
command.Dispose();
throw ex;
}
}

public override DataReaderBase ExecuteReader(string querySql, bool attachConnectionToReader)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
using (SqlCommand command = new SqlCommand(querySql, _Connection, _Transaction))
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();

var command = new SqlCommand(querySql, (SqlConnection)Connection, Transaction as SqlTransaction);
try
{
return new DataReaderBase(command.ExecuteReader(), command, attachConnectionToReader ? this : null);
}
catch (Exception ex)
{
return new DataReaderBase(command.ExecuteReader(), attachConnectionToReader ? this : null);
command.Dispose();
throw ex;
}
}

public override DataReaderBase ExecuteReader(DbCommand command)
public override DataReaderBase ExecuteReader(DbCommand command, bool attachCommandToReader = false, bool attachConnectionToReader = false)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
command.Connection = _Connection;
command.Transaction = _Transaction;
return new DataReaderBase(((SqlCommand)command).ExecuteReader());
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();

command.Connection = Connection;
command.Transaction = Transaction;

return new DataReaderBase(
command.ExecuteReader(),
attachCommandToReader ? command : null,
attachConnectionToReader ? this : null);
}

public override DataReaderBase ExecuteReader(DbCommand command, bool attachConnectionToReader)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
command.Connection = _Connection;
command.Transaction = _Transaction;
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
command.Connection = Connection;
command.Transaction = Transaction;
return new DataReaderBase(((SqlCommand)command).ExecuteReader(), attachConnectionToReader ? this : null);
}

public override DataSet ExecuteDataSet(string querySql)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
using (SqlCommand command = new SqlCommand(querySql, _Connection, _Transaction))
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();

using (var command = new SqlCommand(querySql, (SqlConnection)Connection, Transaction as SqlTransaction))
{
DataSet dataSet = new DataSet();
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
using (var adapter = new SqlDataAdapter(command))
{
adapter.Fill(dataSet);
}
Expand All @@ -170,14 +146,16 @@ public override DataSet ExecuteDataSet(string querySql)

public override DataSet ExecuteDataSet(DbCommand command)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
command.Connection = _Connection;
command.Transaction = _Transaction;
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
command.Connection = Connection;
command.Transaction = Transaction;
DataSet dataSet = new DataSet();
using (SqlDataAdapter adapter = new SqlDataAdapter((SqlCommand)command))

using (var adapter = new SqlDataAdapter((SqlCommand)command))
{
adapter.Fill(dataSet);
}

return dataSet;
}

Expand All @@ -199,7 +177,7 @@ public MsSqlVersion GetVersionData()
if (_Version == null)
{
MsSqlVersion version;
if (_Map_ConnStr_Version.TryGetValue(_Connection.ConnectionString, out version))
if (_Map_ConnStr_Version.TryGetValue(Connection.ConnectionString, out version))
{
_Version = version;
}
Expand All @@ -208,7 +186,7 @@ public MsSqlVersion GetVersionData()
try
{
version = new MsSqlVersion();
using (DataReaderBase reader = ExecuteReader("SELECT SERVERPROPERTY('ProductVersion'), SERVERPROPERTY('ProductLevel'), SERVERPROPERTY('Edition')"))
using (var reader = ExecuteReader("SELECT SERVERPROPERTY('ProductVersion'), SERVERPROPERTY('ProductLevel'), SERVERPROPERTY('Edition')"))
{
if (reader.Read())
{
Expand All @@ -218,7 +196,7 @@ public MsSqlVersion GetVersionData()
}
}
_Version = version;
_Map_ConnStr_Version[_Connection.ConnectionString] = _Version;
_Map_ConnStr_Version[Connection.ConnectionString] = _Version;
}
catch { }
}
Expand All @@ -238,7 +216,7 @@ public override bool SupportsSelectPaging()

public SqlConnection GetUnderlyingConnection()
{
return _Connection;
return (SqlConnection)Connection;
}

public override object GetLastInsertID()
Expand All @@ -255,99 +233,12 @@ public override void SetIdentityInsert(string TableName, bool Enabled)

public override bool CheckIfTableExists(string TableName)
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
return ExecuteScalar(@"SELECT name FROM sysObjects WHERE name like " + PrepareValue(TableName)) != null;
}

#endregion

#region Transactions

private SqlTransaction _Transaction = null;
private Stack<SqlTransaction> _Transactions = null;

public override bool BeginTransaction()
{
try
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
_Transaction = _Connection.BeginTransaction();
if (_Transactions == null) _Transactions = new Stack<SqlTransaction>(1);
_Transactions.Push(_Transaction);
return (_Transaction != null);
}
catch (SqlException) { }
return false;
}

public override bool BeginTransaction(IsolationLevel IsolationLevel)
{
try
{
if (_Connection.State != System.Data.ConnectionState.Open) _Connection.Open();
_Transaction = _Connection.BeginTransaction(IsolationLevel);
if (_Transactions == null) _Transactions = new Stack<SqlTransaction>(1);
_Transactions.Push(_Transaction);
}
catch (SqlException) { return false; }
return (_Transaction != null);
}

public override bool CommitTransaction()
{
if (_Transaction == null) return false;
else
{
_Transactions.Pop();

try
{
_Transaction.Commit();
}
catch (SqlException) { return false; }

if (_Transactions.Count > 0) _Transaction = _Transactions.Peek();
else _Transaction = null;
return true;
}
}

public override bool RollbackTransaction()
{
if (_Transaction == null) return false;
else
{
_Transactions.Pop();

try
{
_Transaction.Rollback();
}
catch (SqlException) { return false; }

if (_Transactions.Count > 0) _Transaction = _Transactions.Peek();
else _Transaction = null;
return true;
}
}

public override bool HasTransaction
{
get { return _Transaction != null; }
}

public override int CurrentTransactions
{
get { return _Transactions == null ? 0 : _Transactions.Count; }
}

public override DbTransaction Transaction
{
get { return _Transaction; }
}

#endregion

#region Preparing values for SQL

public override string WrapFieldName(string fieldName)
Expand Down Expand Up @@ -531,7 +422,7 @@ public override bool GetLock(string lockName, TimeSpan timeout, SqlMutexOwner ow
break;
}

SqlCommand sqlCommand = new SqlCommand("sp_getapplock", _Connection);
SqlCommand sqlCommand = new SqlCommand("sp_getapplock", (SqlConnection)Connection);
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandTimeout = (int)Math.Ceiling(timeout.TotalSeconds);

Expand Down Expand Up @@ -563,7 +454,7 @@ public override bool ReleaseLock(string lockName, SqlMutexOwner owner = SqlMutex
break;
}

SqlCommand sqlCommand = new SqlCommand("sp_releaseapplock", _Connection);
SqlCommand sqlCommand = new SqlCommand("sp_releaseapplock", (SqlConnection)Connection);
sqlCommand.CommandType = CommandType.StoredProcedure;

sqlCommand.Parameters.AddWithValue("Resource", lockName);
Expand Down
Loading

0 comments on commit 1e6d6db

Please sign in to comment.