-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
18 changed files
with
1,643 additions
and
419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.