| | 1 | | /* |
| | 2 | | * Copyright 2017 Stanislav Muhametsin. All rights Reserved. |
| | 3 | | * |
| | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | 5 | | * you may not use this file except in compliance with the License. |
| | 6 | | * You may obtain a copy of the License at |
| | 7 | | * |
| | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 9 | | * |
| | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | 13 | | * implied. |
| | 14 | | * |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | | using AsyncEnumeration.Abstractions; |
| | 19 | | using AsyncEnumeration.Observability; |
| | 20 | | using CBAM.Abstractions; |
| | 21 | | using System; |
| | 22 | | using System.Collections.Generic; |
| | 23 | | using System.Text; |
| | 24 | | using System.Threading; |
| | 25 | | using System.Threading.Tasks; |
| | 26 | | using UtilPack; |
| | 27 | |
|
| | 28 | | namespace CBAM.Abstractions |
| | 29 | | { |
| | 30 | | /// <summary> |
| | 31 | | /// This is common interface for any kind of connection to the potentially remote resource (e.g. SQL or HTTP or LDAP |
| | 32 | | /// </summary> |
| | 33 | | /// <typeparam name="TStatement">The type of objects used to manipulate or query remote resource.</typeparam> |
| | 34 | | /// <typeparam name="TStatementInformation">The type of objects describing <typeparamref name="TStatement"/>s.</typep |
| | 35 | | /// <typeparam name="TStatementCreationArgs">The type of object used to create an instance of <typeparamref name="TSt |
| | 36 | | /// <typeparam name="TEnumerableItem">The type of object representing the response of manipulation or querying remote |
| | 37 | | /// <typeparam name="TVendorFunctionality">The type of object describing vendor-specific information.</typeparam> |
| | 38 | | public interface Connection<in TStatement, TStatementInformation, in TStatementCreationArgs, out TEnumerableItem, out |
| | 39 | | where TVendorFunctionality : ConnectionVendorFunctionality<TStatement, TStatementCreationArgs> |
| | 40 | | where TStatement : TStatementInformation |
| | 41 | | { |
| | 42 | | /// <summary> |
| | 43 | | /// Prepares object that will manipulate or query remote resource to be ready for execution. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="statement">The statement, which describes querying or manipulating the remote resource.</param> |
| | 46 | | /// <returns><see cref="IAsyncEnumerable{T}"/> to be used to execute the statement.</returns> |
| | 47 | | /// <remarks>This method does not execute the <paramref name="statement"/>. The first call to <see cref="IAsyncEnu |
| | 48 | | IAsyncEnumerable<TEnumerableItem> PrepareStatementForExecution( TStatementInformation statement ); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Gets the <see cref="ConnectionVendorFunctionality{TStatement, TStatementCreationArgs}"/> of this connection. |
| | 52 | | /// </summary> |
| | 53 | | /// <value>The <see cref="ConnectionVendorFunctionality{TStatement, TStatementCreationArgs}"/> of this connection. |
| | 54 | | TVendorFunctionality VendorFunctionality { get; } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// This property controls whether the enumerables returned by <see cref="PrepareStatementForExecution"/> method i |
| | 58 | | /// </summary> |
| | 59 | | /// <value>Whether the enumerables returned by <see cref="PrepareStatementForExecution"/> method implement <see cr |
| | 60 | | Boolean DisableEnumerableObservability { get; set; } |
| | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// This interface represents vendor-specific functionality that is required by <see cref="Connection{TStatement, TSt |
| | 65 | | /// </summary> |
| | 66 | | /// <typeparam name="TStatement">The type of statement object used to manipulate or query the remote resource.</typep |
| | 67 | | /// <typeparam name="TStatementCreationArgs">The type of parameters used to create statement object.</typeparam> |
| | 68 | | public interface ConnectionVendorFunctionality<out TStatement, in TStatementCreationArgs> |
| | 69 | | { |
| | 70 | | /// <summary> |
| | 71 | | /// Creates a modifiable statement object, which can be customized and parametrized as needed in order to manipula |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="creationArgs">The object that contains information that will not be customizable in resulting sta |
| | 74 | | /// <returns>Customizable statement object.</returns> |
| | 75 | | TStatement CreateStatementBuilder( TStatementCreationArgs creationArgs ); |
| | 76 | | } |
| | 77 | | } |
| | 78 | |
|
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// This class contains extension methods for types defined in this assembly. |
| | 82 | | /// </summary> |
| | 83 | | public static partial class E_CBAM |
| | 84 | | { |
| | 85 | | /// <summary> |
| | 86 | | /// This is shortcut method to create a new statement from the <see cref="Connection{TStatement, TStatementInformatio |
| | 87 | | /// </summary> |
| | 88 | | /// <typeparam name="TStatement">The type of objects used to manipulate or query remote resource.</typeparam> |
| | 89 | | /// <typeparam name="TStatementInformation">The type of objects describing <typeparamref name="TStatement"/>s.</typep |
| | 90 | | /// <typeparam name="TStatementCreationArgs">The type of object used to create an instance of <typeparamref name="TSt |
| | 91 | | /// <typeparam name="TEnumerableItem">The type of object representing the response of manipulation or querying remote |
| | 92 | | /// <typeparam name="TVendorFunctionality">The type of object describing vendor-specific information.</typeparam> |
| | 93 | | /// <param name="connection">This <see cref="Connection{TStatement, TStatementInformation, TStatementCreationArgs, TE |
| | 94 | | /// <param name="creationArgs">The statement builder creation parameters.</param> |
| | 95 | | /// <returns>A new instance of statement builder with given <paramref name="creationArgs"/>.</returns> |
| | 96 | | /// <exception cref="NullReferenceException">If this <see cref="Connection{TStatement, TStatementInformation, TStatem |
| | 97 | | public static TStatement CreateStatementBuilder<TStatement, TStatementInformation, TStatementCreationArgs, TEnumerabl |
| | 98 | | where TVendorFunctionality : ConnectionVendorFunctionality<TStatement, TStatementCreationArgs> |
| | 99 | | where TStatement : TStatementInformation |
| | 100 | | { |
| 17 | 101 | | return connection.VendorFunctionality.CreateStatementBuilder( creationArgs ); |
| | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// This is shortcut method to directly prepare statement from its starting parameters without using builder. |
| | 106 | | /// </summary> |
| | 107 | | /// <typeparam name="TStatement">The type of objects used to manipulate or query remote resource.</typeparam> |
| | 108 | | /// <typeparam name="TStatementInformation">The type of objects describing <typeparamref name="TStatement"/>s.</typep |
| | 109 | | /// <typeparam name="TStatementCreationArgs">The type of object used to create an instance of <typeparamref name="TSt |
| | 110 | | /// <typeparam name="TEnumerableItem">The type of object representing the response of manipulation or querying remote |
| | 111 | | /// <typeparam name="TVendorFunctionality">The type of object describing vendor-specific information.</typeparam> |
| | 112 | | /// <param name="connection">This <see cref="Connection{TStatement, TStatementInformation, TStatementCreationArgs, TE |
| | 113 | | /// <param name="creationArgs">The statement builder creation parameters.</param> |
| | 114 | | /// <returns>A new instance of <see cref="IAsyncEnumerator{T}"/>, ready to be executed.</returns> |
| | 115 | | /// <exception cref="NullReferenceException">If this <see cref="Connection{TStatement, TStatementInformation, TStatem |
| | 116 | | public static IAsyncEnumerable<TEnumerableItem> PrepareStatementForExecution<TStatement, TStatementInformation, TStat |
| | 117 | | where TVendorFunctionality : ConnectionVendorFunctionality<TStatement, TStatementCreationArgs> |
| | 118 | | where TStatement : TStatementInformation |
| | 119 | | { |
| 8 | 120 | | return connection.PrepareStatementForExecution( connection.CreateStatementBuilder( creationArgs ) ); |
| | 121 | | } |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// This is shortcut method to create statement builder from creation parameters, prepare statement builder for execu |
| | 125 | | /// </summary> |
| | 126 | | /// <typeparam name="TStatement">The type of objects used to manipulate or query remote resource.</typeparam> |
| | 127 | | /// <typeparam name="TStatementInformation">The type of objects describing <typeparamref name="TStatement"/>s.</typep |
| | 128 | | /// <typeparam name="TStatementCreationArgs">The type of object used to create an instance of <typeparamref name="TSt |
| | 129 | | /// <typeparam name="TEnumerableItem">The type of object representing the response of manipulation or querying remote |
| | 130 | | /// <typeparam name="TVendorFunctionality">The type of object describing vendor-specific information.</typeparam> |
| | 131 | | /// <param name="connection">This <see cref="Connection{TStatement, TStatementInformation, TStatementCreationArgs, TE |
| | 132 | | /// <param name="creationArgs">The creation parameters for statement builder.</param> |
| | 133 | | /// <param name="action">Optional synchronous callback to execute after execution has started, and before it is ended |
| | 134 | | /// <returns>A task which will have enumerated the <see cref="IAsyncEnumerator{T}"/> returned by <see cref="Connectio |
| | 135 | | /// <exception cref="NullReferenceException">If this <see cref="Connection{TStatement, TStatementInformation, TStatem |
| | 136 | | public static ValueTask<Int64> ExecuteAndIgnoreResults<TStatement, TStatementInformation, TStatementCreationArgs, TEn |
| | 137 | | where TVendorFunctionality : ConnectionVendorFunctionality<TStatement, TStatementCreationArgs> |
| | 138 | | where TStatement : TStatementInformation |
| | 139 | | { |
| 4 | 140 | | return connection.ExecuteAndIgnoreResults( connection.CreateStatementBuilder( creationArgs ), action ); |
| | 141 | | } |
| | 142 | |
|
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// This is shortcut method to prepare statement and execute it while ignoring any possibly returned results when enc |
| | 146 | | /// </summary> |
| | 147 | | /// <typeparam name="TStatement">The type of objects used to manipulate or query remote resource.</typeparam> |
| | 148 | | /// <typeparam name="TStatementInformation">The type of objects describing <typeparamref name="TStatement"/>s.</typep |
| | 149 | | /// <typeparam name="TStatementCreationArgs">The type of object used to create an instance of <typeparamref name="TSt |
| | 150 | | /// <typeparam name="TEnumerableItem">The type of object representing the response of manipulation or querying remote |
| | 151 | | /// <typeparam name="TVendorFunctionality">The type of object describing vendor-specific information.</typeparam> |
| | 152 | | /// <param name="connection">This <see cref="Connection{TStatement, TStatementInformation, TStatementCreationArgs, TE |
| | 153 | | /// <param name="statement">The statement builder.</param> |
| | 154 | | /// <param name="action">Optional synchronous callback to execute after execution has started, and before it is ended |
| | 155 | | /// <returns>A task which will have enumerated the <see cref="IAsyncEnumerator{T}"/> returned by <see cref="Connectio |
| | 156 | | /// <exception cref="NullReferenceException">If this <see cref="Connection{TStatement, TStatementInformation, TStatem |
| | 157 | | public static ValueTask<Int64> ExecuteAndIgnoreResults<TStatement, TStatementInformation, TStatementCreationArgs, TEn |
| | 158 | | where TVendorFunctionality : ConnectionVendorFunctionality<TStatement, TStatementCreationArgs> |
| | 159 | | where TStatement : TStatementInformation |
| | 160 | | { |
| 7 | 161 | | var enumerable = connection.PrepareStatementForExecution( statement ); |
| 7 | 162 | | if ( action != null ) |
| | 163 | | { |
| 0 | 164 | | var observable = enumerable.AsObservable(); |
| 0 | 165 | | observable.BeforeEnumerationEnd += ( args ) => action(); |
| 0 | 166 | | enumerable = observable; |
| | 167 | | } |
| | 168 | |
|
| 7 | 169 | | return enumerable.EnumerateAsync(); |
| | 170 | | } |
| | 171 | |
|
| | 172 | | // For some reason the implicit cast of UtilPack.EitherOr struct is not always picked up by compiler, so these two me |
| | 173 | |
|
| | 174 | | ///// <summary> |
| | 175 | | ///// This is shortcut method to directly prepare statement from its starting parameters without using builder. |
| | 176 | | ///// </summary> |
| | 177 | | ///// <typeparam name="TStatement">The type of objects used to manipulate or query remote resource.</typeparam> |
| | 178 | | ///// <typeparam name="TStatementInformation">The type of objects describing <typeparamref name="TStatement"/>s.</typ |
| | 179 | | ///// <typeparam name="TEnumerableItem">The type of object representing the response of manipulation or querying remo |
| | 180 | | ///// <typeparam name="TVendorFunctionality">The type of object describing vendor-specific information.</typeparam> |
| | 181 | | ///// <typeparam name="TEnumerable">The actual type of <see cref="IAsyncEnumerable{T}"/> returned by <see cref="Conne |
| | 182 | | ///// <typeparam name="T1">The first possible type of statement creation parameters.</typeparam> |
| | 183 | | ///// <typeparam name="T2">The second possible type of statement creation parameters.</typeparam> |
| | 184 | | ///// <param name="connection">This <see cref="Connection{TStatement, TStatementInformation, TStatementCreationArgs, |
| | 185 | | ///// <param name="creationArgs">The statement builder creation parameters.</param> |
| | 186 | | ///// <returns>A new instance of <see cref="AsyncEnumerator{T}"/>, ready to be executed.</returns> |
| | 187 | | ///// <exception cref="NullReferenceException">If this <see cref="Connection{TStatement, TStatementInformation, TStat |
| | 188 | | //public static TEnumerable PrepareStatementForExecution<TStatement, TStatementInformation, TEnumerableItem, TVendorF |
| | 189 | | // where TVendorFunctionality : ConnectionVendorFunctionality<TStatement, EitherOr<T1, T2>> |
| | 190 | | // where TStatement : TStatementInformation |
| | 191 | | // where TEnumerable : IAsyncEnumerable<TEnumerableItem> |
| | 192 | | //{ |
| | 193 | | // return connection.PrepareStatementForExecution( connection.CreateStatementBuilder( creationArgs ) ); |
| | 194 | | //} |
| | 195 | |
|
| | 196 | | ///// <summary> |
| | 197 | | ///// This is shortcut method to directly prepare statement from its starting parameters without using builder. |
| | 198 | | ///// </summary> |
| | 199 | | ///// <typeparam name="TStatement">The type of objects used to manipulate or query remote resource.</typeparam> |
| | 200 | | ///// <typeparam name="TStatementInformation">The type of objects describing <typeparamref name="TStatement"/>s.</typ |
| | 201 | | ///// <typeparam name="TEnumerableItem">The type of object representing the response of manipulation or querying remo |
| | 202 | | ///// <typeparam name="TVendorFunctionality">The type of object describing vendor-specific information.</typeparam> |
| | 203 | | ///// <typeparam name="TEnumerable">The actual type of <see cref="IAsyncEnumerable{T}"/> returned by <see cref="Conne |
| | 204 | | ///// <typeparam name="T1">The first possible type of statement creation parameters.</typeparam> |
| | 205 | | ///// <typeparam name="T2">The second possible type of statement creation parameters.</typeparam> |
| | 206 | | ///// <param name="connection">This <see cref="Connection{TStatement, TStatementInformation, TStatementCreationArgs, |
| | 207 | | ///// <param name="creationArgs">The statement builder creation parameters.</param> |
| | 208 | | ///// <returns>A new instance of <see cref="AsyncEnumerator{T}"/>, ready to be executed.</returns> |
| | 209 | | ///// <exception cref="NullReferenceException">If this <see cref="Connection{TStatement, TStatementInformation, TStat |
| | 210 | | //public static TEnumerable PrepareStatementForExecution<TStatement, TStatementInformation, TEnumerableItem, TVendorF |
| | 211 | | // where TVendorFunctionality : ConnectionVendorFunctionality<TStatement, EitherOr<T1, T2>> |
| | 212 | | // where TStatement : TStatementInformation |
| | 213 | | // where TEnumerable : IAsyncEnumerable<TEnumerableItem> |
| | 214 | | //{ |
| | 215 | | // return connection.PrepareStatementForExecution( connection.CreateStatementBuilder( creationArgs ) ); |
| | 216 | | //} |
| | 217 | |
|
| | 218 | |
|
| | 219 | |
|
| | 220 | |
|
| | 221 | | } |