Skip to content

Commit

Permalink
#45 #43 WIP Started work on nesting of batches and transaction suppor…
Browse files Browse the repository at this point in the history
…t in batches. The SQL generation is mostly complete, but the code still needs to handle errors raised correctly.

- A SqlBatch now stores a list of IBatchItem instead of just SqlBatchCommands. Both SqlBatch and SqlBatchCommand implement this interface.
- Heavily refactored the pre-processing to support nesting of batches and transactions, using the new IBatchItem interface.
- Added the ServerVersion to DatabaseSchema.
- The Connection class caches the DatabaseSchema for its connection string.
- Changed the common connection code to return Connection objects instead of just the connection string.
  This is used to get the ServerVersion from its DatabaseSchema, so better SQL can be generated if the version supports it.
  • Loading branch information
billings7 committed May 16, 2017
1 parent e6757ca commit 5f8bbd3
Show file tree
Hide file tree
Showing 18 changed files with 1,643 additions and 419 deletions.
84 changes: 84 additions & 0 deletions Database/BatchProcessArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using WebApplications.Utilities.Annotations;
using WebApplications.Utilities.Threading;

namespace WebApplications.Utilities.Database
{
/// <summary>
/// Arguments for processing a <see cref="SqlBatch"/>.
/// </summary>
internal class BatchProcessArgs
{
[NotNull]
public readonly Version ServerVersion;

[NotNull]
public readonly SqlStringBuilder SqlBuilder = new SqlStringBuilder();

[NotNull]
public readonly List<DbParameter> AllParameters = new List<DbParameter>();

[NotNull]
public readonly Dictionary<IOut, DbParameter> OutParameters = new Dictionary<IOut, DbParameter>();

[NotNull]
public readonly Dictionary<IOut, SqlBatchCommand> OutParameterCommands = new Dictionary<IOut, SqlBatchCommand>();

[NotNull]
public readonly Dictionary<SqlBatchCommand, IReadOnlyList<(DbBatchParameter param, IOut output)>> CommandOutParams =
new Dictionary<SqlBatchCommand, IReadOnlyList<(DbBatchParameter, IOut)>>();

[NotNull]
public readonly HashSet<AsyncSemaphore> ConnectionSemaphores = new HashSet<AsyncSemaphore>();

[NotNull]
public readonly HashSet<AsyncSemaphore> LoadBalConnectionSemaphores = new HashSet<AsyncSemaphore>();

[NotNull]
public readonly HashSet<AsyncSemaphore> DatabaseSemaphores = new HashSet<AsyncSemaphore>();

[NotNull]
public readonly Stack<string, string> TransactionStack = new Stack<string, string>();

public bool InTransaction => TransactionStack.Count > 0;

public CommandBehavior Behavior = CommandBehavior.SequentialAccess;

public ushort CommandIndex;

public BatchProcessArgs([NotNull] Version serverVersion)
{
ServerVersion = serverVersion;
}

[NotNull]
public AsyncSemaphore[] GetSemaphores()
{
AsyncSemaphore[] semaphores;
// Concat the semaphores to a single array
int semaphoreCount =
ConnectionSemaphores.Count +
LoadBalConnectionSemaphores.Count +
DatabaseSemaphores.Count;
if (semaphoreCount < 1)
semaphores = Array<AsyncSemaphore>.Empty;
else
{
semaphores = new AsyncSemaphore[semaphoreCount];
int i = 0;

// NOTE! Do NOT reorder these without also reordering the semaphores in SqlProgramCommand.WaitSemaphoresAsync
foreach (AsyncSemaphore semaphore in ConnectionSemaphores)
semaphores[i++] = semaphore;
foreach (AsyncSemaphore semaphore in LoadBalConnectionSemaphores)
semaphores[i++] = semaphore;
foreach (AsyncSemaphore semaphore in DatabaseSemaphores)
semaphores[i++] = semaphore;
}
return semaphores;
}
}
}
12 changes: 11 additions & 1 deletion Database/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public Connection([NotNull] string connectionString, double weight, AsyncSemapho
[CanBeNull]
internal readonly AsyncSemaphore Semaphore;

/// <summary>
/// The database schema for this connection.
/// </summary>
internal DatabaseSchema CachedSchema;

/// <summary>
/// Returns a new connection with the <see cref="Weight"/> increased by <paramref name="weight"/>.
/// </summary>
Expand Down Expand Up @@ -248,7 +253,12 @@ public Task<DatabaseSchema> GetSchema(
bool forceReload,
CancellationToken cancellationToken = default(CancellationToken))
{
return DatabaseSchema.GetOrAdd(this, false, cancellationToken);
if (CachedSchema == null)
return DatabaseSchema.GetOrAdd(this, forceReload, cancellationToken);

return forceReload
? CachedSchema.ReLoad(cancellationToken)
: Task.FromResult(CachedSchema);
}

#region Equalities
Expand Down
62 changes: 62 additions & 0 deletions Database/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#region © Copyright Web Applications (UK) Ltd, 2017. All rights reserved.
// Copyright (c) 2017, Web Applications UK Ltd
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Web Applications UK Ltd nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL WEB APPLICATIONS UK LTD BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion

namespace WebApplications.Utilities.Database
{
internal static class Constants
{
internal static class BatchState
{
internal const int Building = 0;
internal const int Executing = 1;
internal const int Completed = 2;
}

internal static class ExecuteState
{
/// <summary>
/// Indicates the next record set will be for the output parameters for the command.
/// </summary>
internal const string Output = "Output";

/// <summary>
/// Indicates an error has occurred.
/// </summary>
internal const string Error = "Error";

/// <summary>
/// Indicates the last Error should be re-thrown.
/// </summary>
internal const string ReThrow = "ReThrow";

/// <summary>
/// Indicates the command has ended.
/// </summary>
internal const string End = "End";
}
}
}
34 changes: 16 additions & 18 deletions Database/DbBatchDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,7 @@ public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancella
=> BaseReaderOpen().IsDBNullAsync(ordinal, cancellationToken);

/// <summary>Closes the <see cref="T:System.Data.Common.DbDataReader" /> object.</summary>
public override void Close()
{
State = BatchReaderState.Closed;
// TODO event handler?
}
public override void Close() => State = BatchReaderState.Closed;

/// <summary>Releases the managed resources used by the <see cref="T:System.Data.Common.DbDataReader" /> and optionally releases the unmanaged resources.</summary>
/// <param name="disposing">true to release managed and unmanaged resources; false to release only unmanaged resources.</param>
Expand Down Expand Up @@ -702,20 +698,22 @@ internal SqlBatchDataReader([NotNull] SqlDataReader baseReader, CommandBehavior
/// <returns>An <see cref="XmlReader"/> for reading XML from the current record set.</returns>
protected internal override XmlReader GetXmlReader()
{
if (BaseReader.FieldCount == 1)
switch (BaseReader.GetDataTypeName(0))
{
case "ntext":
case "nvarchar":
return XmlReader.Create(new SqlBatchDataTextReader(this), _xmlReaderSettings);
case "xml":
// ReSharper disable once AssignNullToNotNullAttribute
return BaseReader.Read()
? BaseReader.GetXmlReader(0)
: XmlReader.Create(new StringReader(string.Empty), _xmlReaderSettings);
}
if (BaseReader.FieldCount != 1)
throw new InvalidOperationException("The command must return a single column.");

switch (BaseReader.GetDataTypeName(0))
{
case "ntext":
case "nvarchar":
return XmlReader.Create(new SqlBatchDataTextReader(this), _xmlReaderSettings);
case "xml":
// ReSharper disable once AssignNullToNotNullAttribute
return BaseReader.Read()
? BaseReader.GetXmlReader(0)
: XmlReader.Create(new StringReader(string.Empty), _xmlReaderSettings);
}

throw new InvalidOperationException("TODO");
throw new InvalidOperationException("The command must return an Xml result.");
}
}
}
48 changes: 48 additions & 0 deletions Database/IBatchItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#region © Copyright Web Applications (UK) Ltd, 2017. All rights reserved.
// Copyright (c) 2017, Web Applications UK Ltd
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Web Applications UK Ltd nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL WEB APPLICATIONS UK LTD BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion

using WebApplications.Utilities.Annotations;

namespace WebApplications.Utilities.Database
{
/// <summary>
/// Interface to an item in a batch.
/// </summary>
internal interface IBatchItem
{
/// <summary>
/// Processes the item to be executed.
/// </summary>
/// <param name="uid">The uid.</param>
/// <param name="connectionString">The connection string.</param>
/// <param name="args">The arguments.</param>
void Process(
[NotNull] string uid,
[NotNull] string connectionString,
BatchProcessArgs args);
}
}
63 changes: 63 additions & 0 deletions Database/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Database/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,25 @@
<data name="SqlBatchDataTextReader_Closed" xml:space="preserve">
<value>The reader is closed.</value>
</data>
<data name="SqlBatchParametersCollection_GetOrAddParameter_OnlyAllowed65536" xml:space="preserve">
<value>Only allowed 65536 parameters per command.</value>
</data>
<data name="SqlBatch_AddCommand_OnlyAllowed65536" xml:space="preserve">
<value>Only allowed 65536 commands per batch.</value>
</data>
<data name="SqlBatch_RootBatch_Executing" xml:space="preserve">
<value>Cannot add a batch which is executing.</value>
</data>
<data name="SqlBatch_RootBatch_Completed" xml:space="preserve">
<value>Cannot add a batch which is completed.</value>
</data>
<data name="SqlBatch_RootBatch_AlreadyAdded" xml:space="preserve">
<value>The batch has already been added to another batch.</value>
</data>
<data name="SqlBatch_CreateTransaction_UnspecifiedIsoLvl" xml:space="preserve">
<value>Cannot have an isolation level of Unspecified.</value>
</data>
<data name="SqlBatch_Process_IsolationLevelNotSupported" xml:space="preserve">
<value>The IsolationLevel enumeration value, {0}, is not supported.</value>
</data>
</root>
Loading

0 comments on commit 5f8bbd3

Please sign in to comment.