Skip to content

Commit

Permalink
Open connection only on Closed state, as some connectors test that sp…
Browse files Browse the repository at this point in the history
…ecifically
  • Loading branch information
danielgindi committed Aug 31, 2020
1 parent 86e0bee commit 19e4747
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 34 deletions.
14 changes: 9 additions & 5 deletions SequelNet.Connector.MsSql/MsSqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,21 @@ public override object GetLastInsertID()
return ExecuteScalar(@"SELECT @@identity AS id");
}

public override void SetIdentityInsert(string TableName, bool Enabled)
public override void SetIdentityInsert(string tableName, bool enabled)
{
string sql = string.Format(@"SET IDENTITY_INSERT {0} {1}",
Language.WrapFieldName(TableName), Enabled ? @"ON" : @"OFF");
Language.WrapFieldName(tableName), enabled ? @"ON" : @"OFF");
ExecuteNonQuery(sql);
}

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

public override async Task<bool> CheckIfTableExistsAsync(string tableName)
{
return await ExecuteScalarAsync(@"SELECT name FROM sysObjects WHERE name like " + Language.PrepareValue(tableName)) != null;
}

#endregion
Expand Down
18 changes: 13 additions & 5 deletions SequelNet.Connector.MySql/MySqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,20 @@ public override LanguageFactory Language

public override int ExecuteScript(string querySql)
{
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
if (Connection.State == System.Data.ConnectionState.Closed)
Connection.Open();

MySqlScript script = new MySqlScript((MySqlConnection)Connection, querySql);
return script.Execute();
}

public override Task<int> ExecuteScriptAsync(string querySql, CancellationToken? cancellationToken = null)
public override async Task<int> ExecuteScriptAsync(string querySql, CancellationToken? cancellationToken = null)
{
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
if (Connection.State == System.Data.ConnectionState.Closed)
await Connection.OpenAsync();

MySqlScript script = new MySqlScript((MySqlConnection)Connection, querySql);
return script.ExecuteAsync(cancellationToken ?? CancellationToken.None);
return await script.ExecuteAsync(cancellationToken ?? CancellationToken.None);
}

#endregion
Expand Down Expand Up @@ -163,10 +167,14 @@ public override void SetIdentityInsert(string tableName, bool enabled)

public override bool CheckIfTableExists(string tableName)
{
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
return ExecuteScalar(@"SHOW TABLES LIKE " + Language.PrepareValue(tableName)) != null;
}

public override async Task<bool> CheckIfTableExistsAsync(string tableName)
{
return await ExecuteScalarAsync(@"SHOW TABLES LIKE " + Language.PrepareValue(tableName)) != null;
}

#endregion

#region DB Mutex
Expand Down
6 changes: 5 additions & 1 deletion SequelNet.Connector.MySql2/MySql2Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ public override void SetIdentityInsert(string tableName, bool enabled)

public override bool CheckIfTableExists(string tableName)
{
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
return ExecuteScalar(@"SHOW TABLES LIKE " + Language.PrepareValue(tableName)) != null;
}

public override async Task<bool> CheckIfTableExistsAsync(string tableName)
{
return await ExecuteScalarAsync(@"SHOW TABLES LIKE " + Language.PrepareValue(tableName)) != null;
}

#endregion

#region DB Mutex
Expand Down
10 changes: 7 additions & 3 deletions SequelNet.Connector.OleDb/OleDbConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ public override object GetLastInsertID()
return ExecuteScalar("SELECT @@identity AS id");
}

public override bool CheckIfTableExists(string TableName)
public override bool CheckIfTableExists(string tableName)
{
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
return ExecuteScalar($"SELECT name FROM MSysObjects WHERE name like {Language.PrepareValue(TableName)}") != null;
return ExecuteScalar($"SELECT name FROM MSysObjects WHERE name like {Language.PrepareValue(tableName)}") != null;
}

public override async Task<bool> CheckIfTableExistsAsync(string tableName)
{
return await ExecuteScalarAsync($"SELECT name FROM MSysObjects WHERE name like {Language.PrepareValue(tableName)}") != null;
}

#endregion
Expand Down
12 changes: 8 additions & 4 deletions SequelNet.Connector.PostgreSQL/PostgreSQLConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,19 @@ public override object GetLastInsertID()
return ExecuteScalar("select lastval() AS id");
}

public override void SetIdentityInsert(string TableName, bool Enabled)
public override void SetIdentityInsert(string tableName, bool enabled)
{
// Nothing to do. In PostgreSQL IDENTITY_INSERT is always allowed
}

public override bool CheckIfTableExists(string TableName)
public override bool CheckIfTableExists(string tableName)
{
if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
return ExecuteScalar($"select * from information_schema.tables where table_name= {Language.PrepareValue(TableName)}") != null;
return ExecuteScalar($"select * from information_schema.tables where table_name= {Language.PrepareValue(tableName)}") != null;
}

public override async Task<bool> CheckIfTableExistsAsync(string tableName)
{
return await ExecuteScalarAsync($"select * from information_schema.tables where table_name= {Language.PrepareValue(tableName)}") != null;
}

#endregion
Expand Down
49 changes: 33 additions & 16 deletions SequelNet/Connector/ConnectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,17 @@ public virtual void Close()

public virtual int ExecuteNonQuery(DbCommand command)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
if (Connection.State == ConnectionState.Closed)
Connection.Open();

command.Connection = Connection;
command.Transaction = Transaction;
return command.ExecuteNonQuery();
}

public virtual async Task<int> ExecuteNonQueryAsync(DbCommand command, CancellationToken? cancellationToken = null)
{
if (Connection.State != ConnectionState.Open)
if (Connection.State == ConnectionState.Closed)
await Connection.OpenAsync().ConfigureAwait(false);

command.Connection = Connection;
Expand All @@ -180,15 +182,17 @@ public virtual async Task<int> ExecuteNonQueryAsync(DbCommand command, Cancellat

public virtual object ExecuteScalar(DbCommand command)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
if (Connection.State == ConnectionState.Closed)
Connection.Open();

command.Connection = Connection;
command.Transaction = Transaction;
return command.ExecuteScalar();
}

public virtual async Task<object> ExecuteScalarAsync(DbCommand command, CancellationToken? cancellationToken = null)
{
if (Connection.State != ConnectionState.Open)
if (Connection.State == ConnectionState.Closed)
await Connection.OpenAsync().ConfigureAwait(false);

command.Connection = Connection;
Expand All @@ -205,7 +209,8 @@ internal virtual DataReader ExecuteReader(

try
{
if (Connection.State != ConnectionState.Open) Connection.Open();
if (Connection.State == ConnectionState.Closed)
Connection.Open();

command.Connection = Connection;
command.Transaction = Transaction;
Expand Down Expand Up @@ -244,7 +249,7 @@ internal virtual async Task<DataReader> ExecuteReaderAsync(

try
{
if (Connection.State != ConnectionState.Open)
if (Connection.State == ConnectionState.Closed)
await Connection.OpenAsync().ConfigureAwait(false);

command.Connection = Connection;
Expand Down Expand Up @@ -285,7 +290,9 @@ public Task<DataReader> ExecuteReaderAsync(DbCommand command, CancellationToken

public virtual DataSet ExecuteDataSet(DbCommand command)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
if (Connection.State == ConnectionState.Closed)
Connection.Open();

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

Expand All @@ -301,7 +308,8 @@ public virtual DataSet ExecuteDataSet(DbCommand command)

public virtual int ExecuteNonQuery(string querySql)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
if (Connection.State == ConnectionState.Closed)
Connection.Open();

using (var command = Factory.NewCommand(querySql, Connection, Transaction))
{
Expand All @@ -311,7 +319,7 @@ public virtual int ExecuteNonQuery(string querySql)

public virtual async Task<int> ExecuteNonQueryAsync(string querySql, CancellationToken? cancellationToken = null)
{
if (Connection.State != ConnectionState.Open)
if (Connection.State == ConnectionState.Closed)
await Connection.OpenAsync().ConfigureAwait(false);

using (var command = Factory.NewCommand(querySql, Connection, Transaction))
Expand All @@ -323,7 +331,8 @@ public virtual async Task<int> ExecuteNonQueryAsync(string querySql, Cancellatio

public virtual object ExecuteScalar(string querySql)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
if (Connection.State == ConnectionState.Closed)
Connection.Open();

using (var command = Factory.NewCommand(querySql, Connection, Transaction))
{
Expand All @@ -333,7 +342,7 @@ public virtual object ExecuteScalar(string querySql)

public virtual async Task<object> ExecuteScalarAsync(string querySql, CancellationToken? cancellationToken = null)
{
if (Connection.State != ConnectionState.Open)
if (Connection.State == ConnectionState.Closed)
await Connection.OpenAsync().ConfigureAwait(false);

using (var command = Factory.NewCommand(querySql, Connection, Transaction))
Expand All @@ -345,7 +354,8 @@ public virtual async Task<object> ExecuteScalarAsync(string querySql, Cancellati

public virtual DataReader ExecuteReader(string querySql, CommandBehavior commandBehavior = CommandBehavior.Default)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
if (Connection.State == ConnectionState.Closed)
Connection.Open();

var command = Factory.NewCommand(querySql, Connection, Transaction);
return ExecuteReader(command, true, commandBehavior);
Expand All @@ -356,7 +366,7 @@ public virtual async Task<DataReader> ExecuteReaderAsync(
CommandBehavior commandBehavior = CommandBehavior.Default,
CancellationToken? cancellationToken = null)
{
if (Connection.State != ConnectionState.Open)
if (Connection.State == ConnectionState.Closed)
await Connection.OpenAsync().ConfigureAwait(false);

var command = Factory.NewCommand(querySql, Connection, Transaction);
Expand All @@ -377,7 +387,8 @@ public Task<DataReader> ExecuteReaderAsync(

public virtual DataSet ExecuteDataSet(string querySql)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
if (Connection.State == ConnectionState.Closed)
Connection.Open();

using (var cmd = Factory.NewCommand(querySql, Connection, Transaction))
{
Expand Down Expand Up @@ -409,6 +420,8 @@ virtual public void SetIdentityInsert(string tableName, bool enabled) { }

abstract public bool CheckIfTableExists(string tableName);

abstract public Task<bool> CheckIfTableExistsAsync(string tableName);

/// <summary>
/// Synonym for Language.EscapeLike(expression)
/// </summary>
Expand All @@ -430,7 +443,9 @@ public virtual bool BeginTransaction()
{
try
{
if (_Connection.State != ConnectionState.Open) _Connection.Open();
if (_Connection.State == ConnectionState.Closed)
_Connection.Open();

_Transaction = _Connection.BeginTransaction();
if (_Transactions == null) _Transactions = new Stack<DbTransaction>(1);
_Transactions.Push(_Transaction);
Expand All @@ -444,7 +459,9 @@ public virtual bool BeginTransaction(IsolationLevel IsolationLevel)
{
try
{
if (_Connection.State != ConnectionState.Open) _Connection.Open();
if (_Connection.State == ConnectionState.Closed)
_Connection.Open();

_Transaction = _Connection.BeginTransaction(IsolationLevel);
if (_Transactions == null) _Transactions = new Stack<DbTransaction>(1);
_Transactions.Push(_Transaction);
Expand Down

0 comments on commit 19e4747

Please sign in to comment.