Skip to content

Commit

Permalink
Add batch command API (without implementation).
Browse files Browse the repository at this point in the history
Signed-off-by: Bradley Grainger <[email protected]>
  • Loading branch information
bgrainger committed May 15, 2019
1 parent a1d32f9 commit fb1a4a5
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/MySqlConnector/MySql.Data.MySqlClient/DbBatchCommandList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;

namespace MySql.Data.MySqlClient
{
internal sealed class DbBatchCommandList : IList<DbBatchCommand>
{
public DbBatchCommandList(List<MySqlDbBatchCommand> batchCommands) => m_batchCommands = batchCommands;

public void Add(DbBatchCommand item) => m_batchCommands.Add((MySqlDbBatchCommand) item);
public void Clear() => m_batchCommands.Clear();
public bool Contains(DbBatchCommand item) => m_batchCommands.Contains((MySqlDbBatchCommand) item);
public IEnumerator<DbBatchCommand> GetEnumerator() => m_batchCommands.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public bool Remove(DbBatchCommand item) => m_batchCommands.Remove((MySqlDbBatchCommand) item);
public int Count => m_batchCommands.Count;
public bool IsReadOnly => false;
public int IndexOf(DbBatchCommand item) => m_batchCommands.IndexOf((MySqlDbBatchCommand) item);
public void Insert(int index, DbBatchCommand item) => m_batchCommands.Insert(index, (MySqlDbBatchCommand) item);
public void RemoveAt(int index) => m_batchCommands.RemoveAt(index);

public DbBatchCommand this[int index]
{
get => m_batchCommands[index];
set => m_batchCommands[index] = (MySqlDbBatchCommand) value;
}

public void CopyTo(DbBatchCommand[] array, int arrayIndex)
{
for (var i = 0; i < m_batchCommands.Count; i++)
array[arrayIndex + i] = m_batchCommands[i];
}

readonly List<MySqlDbBatchCommand> m_batchCommands;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public sealed class MySqlClientFactory : DbProviderFactory
public static void Register() => DbProviderFactories.RegisterFactory(InvariantName, Instance);
#endif

public DbBatch CreateBatch() => new MySqlDbBatch();
public DbBatchCommand CreateBatchCommand() => new MySqlDbBatchCommand();
public bool CanCreateBatch => false;

private MySqlClientFactory()
{
}
Expand Down
7 changes: 7 additions & 0 deletions src/MySqlConnector/MySql.Data.MySqlClient/MySqlConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ private SchemaProvider GetSchemaProvider()

public event MySqlInfoMessageEventHandler InfoMessage;

public DbBatch CreateBatch() => CreateDbBatch();
private MySqlDbBatch CreateDbBatch() => new MySqlDbBatch(this);

public DbBatchCommand CreateBatchCommand() => CreateDbBatchCommand();
private MySqlDbBatchCommand CreateDbBatchCommand() => new MySqlDbBatchCommand();
public bool CanCreateBatch => false;

protected override void Dispose(bool disposing)
{
try
Expand Down
132 changes: 132 additions & 0 deletions src/MySqlConnector/MySql.Data.MySqlClient/MySqlDbBatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;

#if NET45 || NET461 || NET471 || NETSTANDARD1_3 || NETSTANDARD2_0 || NETCOREAPP2_1
namespace System.Data.Common
{
public abstract class DbBatch : IDisposable
{
public IList<DbBatchCommand> BatchCommands => DbBatchCommands;
protected abstract IList<DbBatchCommand> DbBatchCommands { get; }

#region Execution (mirrors DbCommand)

public abstract DbDataReader ExecuteReader();
public abstract Task<DbDataReader> ExecuteReaderAsync(CancellationToken cancellationToken = default);

public abstract int ExecuteNonQuery();
public abstract Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken = default);

public abstract object ExecuteScalar();
public abstract Task<object> ExecuteScalarAsync(CancellationToken cancellationToken = default);

#endregion

#region Execution properties (mirrors DbCommand)

public abstract int Timeout { get; set; }

// Delegates to DbConnection
public DbConnection Connection { get; set; }
protected abstract DbConnection DbConnection { get; set; }

// Delegates to DbTransaction
public DbTransaction Transaction { get; set; }
protected abstract DbTransaction DbTransaction { get; set; }

#endregion

#region Other methods mirroring DbCommand

public abstract void Prepare();
public abstract Task PrepareAsync(CancellationToken cancellationToken = default);
public abstract void Cancel();
public abstract Task CancelAsync(CancellationToken cancellationToken = default);

#endregion

#region Standard dispose pattern

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing) { }

#endregion
}
}
#endif

namespace MySql.Data.MySqlClient
{
public sealed class MySqlDbBatch : DbBatch
{
public MySqlDbBatch()
: this(null, null)
{
}

public MySqlDbBatch(MySqlConnection connection = null, MySqlTransaction transaction = null)
{
Connection = connection;
Transaction = transaction;
m_batchCommands = new List<MySqlDbBatchCommand>();
}

public new MySqlConnection Connection { get; set; }
public new MySqlTransaction Transaction { get; set; }

protected override DbConnection DbConnection
{
get => Connection;
set => Connection = (MySqlConnection) value;
}

protected override DbTransaction DbTransaction
{
get => Transaction;
set => Transaction = (MySqlTransaction) value;
}

protected override IList<DbBatchCommand> DbBatchCommands
{
get
{
if (m_dbBatchCommands is null)
m_dbBatchCommands = new DbBatchCommandList(m_batchCommands);
return m_dbBatchCommands;
}
}

public override DbDataReader ExecuteReader() => throw new NotImplementedException();

public override Task<DbDataReader> ExecuteReaderAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException();

public override int ExecuteNonQuery() => throw new NotImplementedException();

public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException();

public override object ExecuteScalar() => throw new NotImplementedException();

public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException();

public override int Timeout { get; set; }

public override void Prepare() => throw new NotImplementedException();

public override Task PrepareAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException();

public override void Cancel() => throw new NotImplementedException();

public override Task CancelAsync(CancellationToken cancellationToken = default) => throw new NotImplementedException();

readonly List<MySqlDbBatchCommand> m_batchCommands;
IList<DbBatchCommand> m_dbBatchCommands;
}
}
58 changes: 58 additions & 0 deletions src/MySqlConnector/MySql.Data.MySqlClient/MySqlDbBatchCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#if NET45 || NET461 || NET471 || NETSTANDARD1_3 || NETSTANDARD2_0 || NETCOREAPP2_1
namespace System.Data.Common
{
public abstract class DbBatchCommand
{
public abstract string CommandText { get; set; }
public abstract CommandType CommandType { get; set; }
public abstract CommandBehavior CommandBehavior { get; set; }
public abstract int RecordsAffected { get; set; }

public DbParameterCollection Parameters => DbParameterCollection;
protected abstract DbParameterCollection DbParameterCollection { get; }
}
}
#endif

namespace MySql.Data.MySqlClient
{
public sealed class MySqlDbBatchCommand : DbBatchCommand
{
public MySqlDbBatchCommand()
: this(null)
{
}

public MySqlDbBatchCommand(string commandText)
{
CommandText = commandText;
CommandType = CommandType.Text;
}

public override string CommandText { get; set; }
public override CommandType CommandType { get; set; }
public override CommandBehavior CommandBehavior { get; set; }
public override int RecordsAffected { get; set; }
protected override DbParameterCollection DbParameterCollection => Parameters;

public new MySqlParameterCollection Parameters
{
get
{
if (m_parameterCollection is null)
m_parameterCollection = new MySqlParameterCollection();
return m_parameterCollection;
}
}

MySqlParameterCollection m_parameterCollection;
}
}

0 comments on commit fb1a4a5

Please sign in to comment.