| | | 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 CBAM.Abstractions; |
| | | 20 | | using CBAM.SQL; |
| | | 21 | | using System; |
| | | 22 | | using System.Collections.Generic; |
| | | 23 | | using System.Linq; |
| | | 24 | | using System.Text; |
| | | 25 | | using System.Threading; |
| | | 26 | | using System.Threading.Tasks; |
| | | 27 | | using UtilPack; |
| | | 28 | | using UtilPack.TabularData; |
| | | 29 | | |
| | | 30 | | namespace CBAM.SQL |
| | | 31 | | { |
| | | 32 | | /// <summary> |
| | | 33 | | /// This interfaces extends the generic CBAM <see cref="Connection{TStatement, TStatementInformation, TStatementCreat |
| | | 34 | | /// Furthermore, all generic type arguments of <see cref="Connection{TStatement, TStatementInformation, TStatementCre |
| | | 35 | | /// </summary> |
| | | 36 | | public interface SQLConnection : Connection<SQLStatementBuilder, SQLStatementBuilderInformation, String, SQLStatement |
| | | 37 | | { |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the <see cref="SQL.DatabaseMetadata"/> object describing the database this <see cref="SQLConnection"/> is |
| | | 40 | | /// </summary> |
| | | 41 | | /// <value>The <see cref="SQL.DatabaseMetadata"/> object describing the database this <see cref="SQLConnection"/> |
| | | 42 | | /// <seealso cref="SQL.DatabaseMetadata"/> |
| | | 43 | | DatabaseMetadata DatabaseMetadata { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Potentially asynchronously gets the value indicating whether this <see cref="SQLConnection"/> is read-only, . |
| | | 47 | | /// </summary> |
| | | 48 | | /// <returns>A task which on completion will have value indicating whether this <see cref="SQLConnection"/> is rea |
| | | 49 | | ValueTask<Boolean> GetReadOnlyAsync(); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Asynchronously sets the value indicating whether this <see cref="SQLConnection"/> is read-only. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="isReadOnly">Whether this connection should be read-only.</param> |
| | | 55 | | /// <returns>A task which on completion has set the value indicating whether this <see cref="SQLConnection"/> is r |
| | | 56 | | ValueTask<Int64> SetReadOnlyAsync( Boolean isReadOnly ); |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Potentially asynchronously gets the value indicating current transaction isolation level. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <returns>A task which on completion will have value indicating current transaction isolation level.</returns> |
| | | 62 | | /// <seealso cref="TransactionIsolationLevel"/> |
| | | 63 | | ValueTask<TransactionIsolationLevel> GetDefaultTransactionIsolationLevelAsync(); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Asynchronously sets the value indicating current transaction isolation level. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="level">The new transaction isolation level.</param> |
| | | 69 | | /// <returns>A task which on completion has set the value indicating current transaction isolation level. The retu |
| | | 70 | | /// <seealso cref="TransactionIsolationLevel"/> |
| | | 71 | | ValueTask<Int64> SetDefaultTransactionIsolationLevelAsync( TransactionIsolationLevel level ); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Given current <see cref="SQLStatementExecutionResult"/> and context of reading SQL from some source (<see cref |
| | | 75 | | /// This is used by <see cref="E_CBAM.ExecuteStatementsFromStreamAsync(SQLConnection, MemorizingPotentiallyAsyncRe |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="reader">The source where SQL statement originated.</param> |
| | | 78 | | /// <param name="statementInformation">The <see cref="SQLStatementBuilderInformation"/> about current statement.</ |
| | | 79 | | /// <param name="executionResult">The <see cref="SQLStatementExecutionResult"/> encountered when enumerating <see |
| | | 80 | | /// <returns>A task which should return <c>true</c> on completion if anything was done to <paramref name="reader"/ |
| | | 81 | | ValueTask<Boolean> ProcessStatementResultPassively( MemorizingPotentiallyAsyncReader<Char?, Char> reader, SQLState |
| | | 82 | | |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// This interface extends generic CBAM interface <see cref="ConnectionVendorFunctionality{TStatement, TStatementCrea |
| | | 87 | | /// This functionality has mostly to do with SQL syntax. |
| | | 88 | | /// </summary> |
| | | 89 | | public interface SQLConnectionVendorFunctionality : ConnectionVendorFunctionality<SQLStatementBuilder, String> |
| | | 90 | | { |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Given a string, escapes any characters in it so that it can be interpreted as literal string in this SQL vendo |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="str">The string to escape. May be <c>null</c>.</param> |
| | | 96 | | /// <returns>Escaped string.</returns> |
| | | 97 | | String EscapeLiteral( String str ); |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Given a <see cref="PeekablePotentiallyAsyncReader{TValue}"/> (which can wrap a normal <see cref="String"/>), t |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="reader">The reader to advance.</param> |
| | | 103 | | /// <returns>A task which will complete after advance is over. Return value is currently not used, since <see cref |
| | | 104 | | ValueTask<Boolean> TryAdvanceReaderOverSingleStatement( PeekablePotentiallyAsyncReader<Char?> reader ); |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Returns <c>true</c> if given character is ignored when it appears at start of SQL string. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="c">The character to check.</param> |
| | | 110 | | /// <returns><c>true</c> if <paramref name="c"/> is ignored when it appears at start of SQL string.</returns> |
| | | 111 | | Boolean CanTrimBegin( Char c ); |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Returns <c>true</c> if given character is ignored when it appears at end of SQL string. |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="c">The character to check.</param> |
| | | 117 | | /// <returns><c>true</c> if <paramref name="c"/> is ignored when it appears at end of SQL string.</returns> |
| | | 118 | | Boolean CanTrimEnd( Char c ); |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// This enumeration describes the transaction isolation levels in relational databases. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <seealso href="https://en.wikipedia.org/wiki/Isolation_(database_systems)"/> |
| | | 125 | | public enum TransactionIsolationLevel |
| | | 126 | | { |
| | | 127 | | /// <summary> |
| | | 128 | | /// Indicates the <c>READ UNCOMMITTED</c> isolation level. |
| | | 129 | | /// </summary> |
| | | 130 | | ReadUncommitted, |
| | | 131 | | /// <summary> |
| | | 132 | | /// Indicates the <c>READ COMMITTED</c> isolation level. |
| | | 133 | | /// </summary> |
| | | 134 | | ReadCommitted, |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Indicates the <c>REPEATABLE READ</c> isolation level. |
| | | 138 | | /// </summary> |
| | | 139 | | RepeatableRead, |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Indicates the <c>SERIALIZABLE</c> isolation level. |
| | | 143 | | /// </summary> |
| | | 144 | | Serializable |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// This is common interface for items enumerated by <see cref="IAsyncEnumerable{T}"/> returned by <see cref="Connect |
| | | 149 | | /// </summary> |
| | | 150 | | /// <seealso cref="SQLDataRow"/> |
| | | 151 | | /// <seealso cref="SingleCommandExecutionResult"/> |
| | | 152 | | /// <seealso cref="BatchCommandExecutionResult"/> |
| | | 153 | | public interface SQLStatementExecutionResult |
| | | 154 | | { |
| | | 155 | | /// <summary> |
| | | 156 | | /// Gets the warnings issued by backend during last call of <see cref="IAsyncEnumerator{T}.WaitForNextAsync"/>. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <value>The warnings issued by backend during last call of <see cref="IAsyncEnumerator{T}.WaitForNextAsync"/>.< |
| | | 159 | | SQLException[] Warnings { get; } |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// This interface extends <see cref="SQLStatementExecutionResult"/> to provide additional information when backend h |
| | | 164 | | /// </summary> |
| | | 165 | | public interface SingleCommandExecutionResult : SQLStatementExecutionResult |
| | | 166 | | { |
| | | 167 | | /// <summary> |
| | | 168 | | /// Gets the amount of rows affected by the single SQL statement. |
| | | 169 | | /// </summary> |
| | | 170 | | /// <value>The amount of rows affected by the single SQL statement.</value> |
| | | 171 | | Int32 AffectedRows { get; } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Gets the vendor-specific command tag string (e.g. <c>"INSERT"</c>, <c>"UPDATE"</c> etc) of the executed SQL st |
| | | 175 | | /// </summary> |
| | | 176 | | /// <value>The vendor-specific command tag string (e.g. <c>"INSERT"</c>, <c>"UPDATE"</c> etc) of the executed SQL |
| | | 177 | | String CommandTag { get; } |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// This interface extends <see cref="SQLStatementExecutionResult"/> to provide additional information when backend h |
| | | 182 | | /// </summary> |
| | | 183 | | /// <seealso cref="SQLStatementBuilder.AddBatch"/> |
| | | 184 | | public interface BatchCommandExecutionResult : SQLStatementExecutionResult |
| | | 185 | | { |
| | | 186 | | /// <summary> |
| | | 187 | | /// Gets the amount of rows affected by each executed SQL statement. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <value>The amount of rows affected by each executed SQL statement.</value> |
| | | 190 | | Int32[] AffectedRows { get; } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Gets the vendor-specific command tag string (e.g. <c>"INSERT"</c>, <c>"UPDATE"</c> etc) of the executed SQL st |
| | | 194 | | /// </summary> |
| | | 195 | | /// <value>The vendor-specific command tag string (e.g. <c>"INSERT"</c>, <c>"UPDATE"</c> etc) of the executed SQL |
| | | 196 | | String CommandTag { get; } |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// This interface extends <see cref="SQLStatementExecutionResult"/> to provide additional information about single r |
| | | 201 | | /// Furthermore, this interface extends <see cref="AsyncDataRow"/> to provide access to the columns of the data row. |
| | | 202 | | /// </summary> |
| | | 203 | | public interface SQLDataRow : AsyncDataRow, SQLStatementExecutionResult |
| | | 204 | | { |
| | | 205 | | |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// This enumeration is used by <see cref="E_CBAM.ExecuteStatementsFromStreamAsync(SQLConnection, MemorizingPotential |
| | | 210 | | /// </summary> |
| | | 211 | | public enum WhenExceptionInMultipleStatements |
| | | 212 | | { |
| | | 213 | | /// <summary> |
| | | 214 | | /// This value indicates that an exception should be simply re-thrown. |
| | | 215 | | /// </summary> |
| | | 216 | | Rethrow, |
| | | 217 | | /// <summary> |
| | | 218 | | /// This value indicates that exception should be ignored, and current transaction should be rollbacked (no SQL co |
| | | 219 | | /// The SQL statement processing will continue. |
| | | 220 | | /// </summary> |
| | | 221 | | Continue, |
| | | 222 | | /// <summary> |
| | | 223 | | /// This value indicates that exception should be ignored, and new transaction should be started (current transact |
| | | 224 | | /// The SQL statement processing will continue. |
| | | 225 | | /// </summary> |
| | | 226 | | RollbackAndStartNew |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// This class contains extension methods for types defined in this assembly. |
| | | 233 | | /// </summary> |
| | | 234 | | public static partial class E_CBAM |
| | | 235 | | { |
| | | 236 | | /// <summary> |
| | | 237 | | /// This method is a shortcut of calling <see cref="M:E_UtilPack.OfType{T, U}(IAsyncEnumerable{T}, OfTypeInfo{U})"/> |
| | | 238 | | /// </summary> |
| | | 239 | | /// <param name="enumerable">This SQL <see cref="IAsyncEnumerable{T}"/>.</param> |
| | | 240 | | /// <returns>Asynchronous enumerable which only returns <see cref="SQLDataRow"/>s and filters out all other items.</r |
| | | 241 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | | 242 | | public static IAsyncEnumerable<SQLDataRow> IncludeDataRowsOnly( this IAsyncEnumerable<SQLStatementExecutionResult> en |
| | | 243 | | { |
| | 87 | 244 | | return enumerable.Of().Type<SQLDataRow>(); |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | |
| | | 248 | | //public static Task ExecuteQueryAsync( this SQLConnection connection, SQLStatementBuilder stmt, Action<SQLDataRow> a |
| | | 249 | | //{ |
| | | 250 | | // return connection.PrepareStatementForExecution( stmt ).EnumerateAsync( res => action( res as SQLDataRow ) ); |
| | | 251 | | //} |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Shortcut method to get some value from first seen <see cref="SQLDataRow"/> of <see cref="IAsyncEnumerator{T}"/> r |
| | | 255 | | /// </summary> |
| | | 256 | | /// <typeparam name="T">The type of the value to return.</typeparam> |
| | | 257 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 258 | | /// <param name="statement">The <see cref="SQLStatementBuilder"/> to execute.</param> |
| | | 259 | | /// <param name="extractor">The asynchronous callback to get value from <see cref="SQLDataRow"/>.</param> |
| | | 260 | | /// <returns>A task which will return value of <paramref name="extractor"/> if at least one <see cref="SQLDataRow"/> |
| | | 261 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 262 | | /// <exception cref="ArgumentNullException">If <paramref name="statement"/> or <paramref name="extractor"/> is <c>nul |
| | | 263 | | public static async ValueTask<T> GetFirstOrDefaultAsync<T>( this SQLConnection connection, SQLStatementBuilder statem |
| | | 264 | | { |
| | 0 | 265 | | ArgumentValidator.ValidateNotNullReference( connection ); |
| | 0 | 266 | | ArgumentValidator.ValidateNotNull( nameof( extractor ), extractor ); |
| | 0 | 267 | | return await connection.PrepareStatementForExecution( statement ) |
| | 0 | 268 | | .IncludeDataRowsOnly() |
| | 0 | 269 | | .Select( row => extractor( row ) ) |
| | 0 | 270 | | .FirstOrDefaultAsync(); |
| | 0 | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// Shortcut method to get some value from first seen <see cref="SQLDataRow"/> of <see cref="IAsyncEnumerator{T}"/> r |
| | | 275 | | /// </summary> |
| | | 276 | | /// <typeparam name="T">The type of the value to return.</typeparam> |
| | | 277 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 278 | | /// <param name="sql">The SQL string.</param> |
| | | 279 | | /// <param name="extractor">The asynchronous callback to get value from <see cref="SQLDataRow"/>.</param> |
| | | 280 | | /// <returns>A task which will return value of <paramref name="extractor"/> if at least one <see cref="SQLDataRow"/> |
| | | 281 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 282 | | /// <exception cref="ArgumentNullException">If <paramref name="sql"/> or <paramref name="extractor"/> is <c>null</c>. |
| | | 283 | | public static ValueTask<T> GetFirstOrDefaultAsync<T>( this SQLConnection connection, String sql, Func<AsyncDataRow, V |
| | | 284 | | { |
| | 0 | 285 | | return connection.GetFirstOrDefaultAsync( connection.VendorFunctionality.CreateStatementBuilder( sql ), extractor |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | /// <summary> |
| | | 289 | | /// Shortcut method to get some value from first seen <see cref="SQLDataRow"/> of <see cref="IAsyncEnumerator{T}"/> r |
| | | 290 | | /// This method lets optionally specify a callback to extract value from single <see cref="AsyncDataColumn"/>, and al |
| | | 291 | | /// </summary> |
| | | 292 | | /// <typeparam name="T">The type of the value to return.</typeparam> |
| | | 293 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 294 | | /// <param name="statement">The <see cref="SQLStatementBuilder"/> to execute.</param> |
| | | 295 | | /// <param name="parameterIndex">The index of the <see cref="AsyncDataColumn"/> to pass to <paramref name="extractor" |
| | | 296 | | /// <param name="extractor">The optional asynchronous callback to use to extract the value from <see cref="AsyncDataC |
| | | 297 | | /// <returns>A task which will return value of <paramref name="extractor"/> if at least one <see cref="SQLDataRow"/> |
| | | 298 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 299 | | /// <exception cref="ArgumentNullException">If <paramref name="statement"/> is <c>null</c>.</exception> |
| | | 300 | | public static async ValueTask<T> GetFirstOrDefaultAsync<T>( this SQLConnection connection, SQLStatementBuilder statem |
| | | 301 | | { |
| | 75 | 302 | | return await connection |
| | 75 | 303 | | .PrepareStatementForExecution( statement ) |
| | 75 | 304 | | .IncludeDataRowsOnly() |
| | 153 | 305 | | .Select( async row => await ( extractor?.Invoke( row.GetColumn( parameterIndex ) ) ?? row.GetValueAsync<T>( par |
| | 75 | 306 | | .FirstOrDefaultAsync(); |
| | 76 | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Shortcut method to get some value from first seen <see cref="SQLDataRow"/> of <see cref="IAsyncEnumerator{T}"/> r |
| | | 311 | | /// This method lets optionally specify a callback to extract value from single <see cref="AsyncDataColumn"/>, and al |
| | | 312 | | /// </summary> |
| | | 313 | | /// <typeparam name="T">The type of the value to return.</typeparam> |
| | | 314 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 315 | | /// <param name="sql">The <see cref="SQLStatementBuilder"/> to execute.</param> |
| | | 316 | | /// <param name="parameterIndex">The index of the <see cref="AsyncDataColumn"/> to pass to <paramref name="extractor" |
| | | 317 | | /// <param name="extractor">The optional asynchronous callback to use to extract the value from <see cref="AsyncDataC |
| | | 318 | | /// <returns>A task which will return value of <paramref name="extractor"/> if at least one <see cref="SQLDataRow"/> |
| | | 319 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 320 | | /// <exception cref="ArgumentNullException">If <paramref name="sql"/> is <c>null</c>.</exception> |
| | | 321 | | public static ValueTask<T> GetFirstOrDefaultAsync<T>( this SQLConnection connection, String sql, Int32 parameterIndex |
| | | 322 | | { |
| | 35 | 323 | | return connection.GetFirstOrDefaultAsync( connection.VendorFunctionality.CreateStatementBuilder( sql ), parameterI |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// This is helper method to perform some action on <see cref="SQLConnection"/> and make sure that the connection is |
| | | 328 | | /// </summary> |
| | | 329 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 330 | | /// <param name="action">The asynchronous callback to use <see cref="SQLConnection"/></param> |
| | | 331 | | /// <returns>A task which on completion has executed given <paramref name="action"/> callback.</returns> |
| | | 332 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 333 | | /// <exception cref="ArgumentNullException">If <paramref name="action"/> is <c>null</c>.</exception> |
| | | 334 | | public static Task DoWriteStatements( this SQLConnection connection, Func<SQLConnection, Task> action ) |
| | | 335 | | { |
| | 0 | 336 | | return connection.DoStatements( action, false ); |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | /// <summary> |
| | | 340 | | /// This is helper method to perform some asynchronous action on <see cref="SQLConnection"/> and make sure that the c |
| | | 341 | | /// Then, some value whhich is obtained by the asynchronous action, is returned. |
| | | 342 | | /// </summary> |
| | | 343 | | /// <typeparam name="T">The type of return value of callback</typeparam> |
| | | 344 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 345 | | /// <param name="func">The asynchronous callback to use <see cref="SQLConnection"/> and return value of type <typepar |
| | | 346 | | /// <returns>A task which on completion has executed given <paramref name="func"/> callback.</returns> |
| | | 347 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 348 | | /// <exception cref="ArgumentNullException">If <paramref name="func"/> is <c>null</c>.</exception> |
| | | 349 | | public static ValueTask<T> DoWriteStatements<T>( this SQLConnection connection, Func<SQLConnection, ValueTask<T>> fun |
| | | 350 | | { |
| | 0 | 351 | | return connection.DoStatements( func, false ); |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | /// <summary> |
| | | 355 | | /// This is helper method to perform some action on <see cref="SQLConnection"/> and make sure that the connection is |
| | | 356 | | /// </summary> |
| | | 357 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 358 | | /// <param name="action">The asynchronous callback to use <see cref="SQLConnection"/></param> |
| | | 359 | | /// <returns>A task which on completion has executed given <paramref name="action"/> callback.</returns> |
| | | 360 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 361 | | /// <exception cref="ArgumentNullException">If <paramref name="action"/> is <c>null</c>.</exception> |
| | | 362 | | public static Task DoReadStatements( this SQLConnection connection, Func<SQLConnection, Task> action ) |
| | | 363 | | { |
| | 0 | 364 | | return connection.DoStatements( action, true ); |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | /// <summary> |
| | | 368 | | /// This is helper method to perform some asynchronous action on <see cref="SQLConnection"/> and make sure that the c |
| | | 369 | | /// Then, some value whhich is obtained by the asynchronous action, is returned. |
| | | 370 | | /// </summary> |
| | | 371 | | /// <typeparam name="T">The type of return value of callback</typeparam> |
| | | 372 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 373 | | /// <param name="func">The asynchronous callback to use <see cref="SQLConnection"/> and return value of type <typepar |
| | | 374 | | /// <returns>A task which on completion has executed given <paramref name="func"/> callback.</returns> |
| | | 375 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 376 | | /// <exception cref="ArgumentNullException">If <paramref name="func"/> is <c>null</c>.</exception> |
| | | 377 | | public static ValueTask<T> DoReadStatements<T>( this SQLConnection connection, Func<SQLConnection, ValueTask<T>> func |
| | | 378 | | { |
| | 0 | 379 | | return connection.DoStatements( func, true ); |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | /// <summary> |
| | | 383 | | /// This is generic method to execute some asynchronous callback for this <see cref="SQLConnection"/> and make sure t |
| | | 384 | | /// </summary> |
| | | 385 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 386 | | /// <param name="action">The asynchronous callback to use <see cref="SQLConnection"/>.</param> |
| | | 387 | | /// <param name="readOnly">Whether to set <see cref="SQLConnection"/> in readonly mode before calling <paramref name= |
| | | 388 | | /// <returns>A task which on completion has executed given <paramref name="action"/> callback.</returns> |
| | | 389 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 390 | | /// <exception cref="ArgumentNullException">If <paramref name="action"/> is <c>null</c>.</exception> |
| | | 391 | | public static async Task DoStatements( this SQLConnection connection, Func<SQLConnection, Task> action, Boolean readO |
| | | 392 | | { |
| | 0 | 393 | | ArgumentValidator.ValidateNotNull( nameof( action ), action ); |
| | 0 | 394 | | var needToChange = await connection.GetReadOnlyAsync() != readOnly; |
| | 0 | 395 | | if ( needToChange ) |
| | | 396 | | { |
| | 0 | 397 | | await connection.SetReadOnlyAsync( readOnly ); |
| | | 398 | | } |
| | | 399 | | try |
| | | 400 | | { |
| | 0 | 401 | | await action( connection ); |
| | | 402 | | } |
| | | 403 | | finally |
| | | 404 | | { |
| | 0 | 405 | | if ( needToChange ) |
| | | 406 | | { |
| | 0 | 407 | | await connection.SetReadOnlyAsync( !readOnly ); |
| | | 408 | | } |
| | | 409 | | } |
| | 0 | 410 | | } |
| | | 411 | | |
| | | 412 | | /// <summary> |
| | | 413 | | /// This is generic method to execute some asynchronous callback for this <see cref="SQLConnection"/> and make sure t |
| | | 414 | | /// </summary> |
| | | 415 | | /// <typeparam name="T">The return type of the task of the asynchronous callback.</typeparam> |
| | | 416 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 417 | | /// <param name="func">The asynchronous callback to use <see cref="SQLConnection"/>.</param> |
| | | 418 | | /// <param name="readOnly">Whether to set <see cref="SQLConnection"/> in readonly mode before calling <paramref name= |
| | | 419 | | /// <returns>A task which on completion has executed given <paramref name="func"/> callback, and returns the result o |
| | | 420 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 421 | | /// <exception cref="ArgumentNullException">If <paramref name="func"/> is <c>null</c>.</exception> |
| | | 422 | | public static async ValueTask<T> DoStatements<T>( this SQLConnection connection, Func<SQLConnection, ValueTask<T>> fu |
| | | 423 | | { |
| | 0 | 424 | | ArgumentValidator.ValidateNotNull( nameof( func ), func ); |
| | 0 | 425 | | var needToChange = await connection.GetReadOnlyAsync() != readOnly; |
| | 0 | 426 | | if ( needToChange ) |
| | | 427 | | { |
| | 0 | 428 | | await connection.SetReadOnlyAsync( readOnly ); |
| | | 429 | | } |
| | | 430 | | try |
| | | 431 | | { |
| | 0 | 432 | | return await func( connection ); |
| | | 433 | | } |
| | | 434 | | finally |
| | | 435 | | { |
| | 0 | 436 | | if ( needToChange ) |
| | | 437 | | { |
| | 0 | 438 | | await connection.SetReadOnlyAsync( !readOnly ); |
| | | 439 | | } |
| | | 440 | | } |
| | 0 | 441 | | } |
| | | 442 | | |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// This method can be used to read SQL statements from stream (e.g. a file) and passively process each statement wit |
| | | 446 | | /// </summary> |
| | | 447 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 448 | | /// <param name="stream">The <see cref="System.IO.Stream"/> containing the SQL statements.</param> |
| | | 449 | | /// <param name="encoding">The <see cref="Encoding"/> to use when reading textual SQL statemetns from <paramref name= |
| | | 450 | | /// <param name="streamMaxBufferCount">The amount of characters to read for one statement until the buffer is cleared |
| | | 451 | | /// <param name="streamReadChunkCount">The amount bytes to read in one chunk from given <paramref name="stream"/>.</p |
| | | 452 | | /// <param name="onException">Optional callback to react when <see cref="SQLException"/> occurs during passive proces |
| | | 453 | | /// <param name="token">Optional <see cref="CancellationToken"/> to use when creating <see cref="StreamReaderWithResi |
| | | 454 | | /// <returns>A task which will on completion return amount of statements read.</returns> |
| | | 455 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 456 | | /// <exception cref="ArgumentNullException">If <paramref name="stream"/> is <c>null</c>.</exception> |
| | | 457 | | /// <seealso cref="ExecuteStatementsFromStreamAsync(SQLConnection, MemorizingPotentiallyAsyncReader{Char?, Char}, Fun |
| | | 458 | | /// <seealso cref="WhenExceptionInMultipleStatements"/> |
| | | 459 | | public static async ValueTask<Int64> ExecuteStatementsFromStreamAsync( |
| | | 460 | | this SQLConnection connection, |
| | | 461 | | System.IO.Stream stream, |
| | | 462 | | Encoding encoding, |
| | | 463 | | Int32 streamMaxBufferCount = 1024, |
| | | 464 | | Int32 streamReadChunkCount = 1024, |
| | | 465 | | Func<SQLException, WhenExceptionInMultipleStatements> onException = null, |
| | | 466 | | CancellationToken token = default |
| | | 467 | | ) |
| | | 468 | | { |
| | 0 | 469 | | ArgumentValidator.ValidateNotNullReference( connection ); |
| | | 470 | | |
| | 0 | 471 | | var streamReader = StreamFactory.CreateUnlimitedReader( |
| | 0 | 472 | | stream, |
| | 0 | 473 | | token: token, |
| | 0 | 474 | | chunkSize: streamReadChunkCount |
| | 0 | 475 | | ); |
| | 0 | 476 | | var charReader = ReaderFactory.NewNullableMemorizingValueReader( |
| | 0 | 477 | | new StreamCharacterReaderLogic( ( encoding ?? new UTF8Encoding( false, false ) ).CreateDefaultEncodingInfo() ), |
| | 0 | 478 | | streamReader |
| | 0 | 479 | | ); |
| | 0 | 480 | | using ( charReader.ClearStreamWhenStreamBufferTooBig( streamReader, streamMaxBufferCount ) ) |
| | | 481 | | { |
| | 0 | 482 | | return await connection.ExecuteStatementsFromStreamAsync( charReader, onException: onException ); |
| | | 483 | | } |
| | 0 | 484 | | } |
| | | 485 | | |
| | | 486 | | |
| | | 487 | | /// <summary> |
| | | 488 | | /// This method can be used to read SQL statements from <see cref="MemorizingPotentiallyAsyncReader{TValue, TBufferIt |
| | | 489 | | /// </summary> |
| | | 490 | | /// <param name="connection">This <see cref="SQLConnection"/>.</param> |
| | | 491 | | /// <param name="reader">The <see cref="MemorizingPotentiallyAsyncReader{TValue, TBufferItem}"/> reader to use to rea |
| | | 492 | | /// <param name="onException">Optional callback to react when <see cref="SQLException"/> occurs during passive proces |
| | | 493 | | /// <returns>A task which will on completion return amount of statements read.</returns> |
| | | 494 | | /// <exception cref="NullReferenceException">If this <see cref="SQLConnection"/> is <c>null</c>.</exception> |
| | | 495 | | /// <exception cref="ArgumentNullException">If <paramref name="reader"/> is <c>null</c>.</exception> |
| | | 496 | | /// <seealso cref="ExecuteStatementsFromStreamAsync(SQLConnection, System.IO.Stream, Encoding, Int32, Int32, Func{SQL |
| | | 497 | | /// <seealso cref="WhenExceptionInMultipleStatements"/> |
| | | 498 | | /// <seealso cref="MemorizingPotentiallyAsyncReader{TValue, TBufferItem}"/> |
| | | 499 | | public static async ValueTask<Int64> ExecuteStatementsFromStreamAsync( |
| | | 500 | | this SQLConnection connection, |
| | | 501 | | MemorizingPotentiallyAsyncReader<Char?, Char> reader, |
| | | 502 | | Func<SQLException, WhenExceptionInMultipleStatements> onException = null |
| | | 503 | | ) |
| | | 504 | | { |
| | | 505 | | Int32 charsRead; |
| | 0 | 506 | | var totalStatements = 0; |
| | 0 | 507 | | var vendorFunc = connection.VendorFunctionality; |
| | 0 | 508 | | ArgumentValidator.ValidateNotNull( nameof( reader ), reader ); |
| | | 509 | | do |
| | | 510 | | { |
| | 0 | 511 | | reader.ClearBuffer(); |
| | 0 | 512 | | await vendorFunc.TryAdvanceReaderOverSingleStatement( reader ); |
| | 0 | 513 | | charsRead = reader.BufferCount; |
| | 0 | 514 | | if ( charsRead > 0 ) |
| | | 515 | | { |
| | 0 | 516 | | var start = 0; |
| | 0 | 517 | | var count = reader.BufferCount; |
| | | 518 | | // Trim begin |
| | 0 | 519 | | while ( count > 0 && vendorFunc.CanTrimBegin( reader.Buffer[start] ) ) |
| | | 520 | | { |
| | 0 | 521 | | ++start; |
| | 0 | 522 | | --count; |
| | | 523 | | } |
| | | 524 | | // Trim end |
| | 0 | 525 | | while ( count > 0 && vendorFunc.CanTrimEnd( reader.Buffer[start + count - 1] ) ) |
| | | 526 | | { |
| | 0 | 527 | | --count; |
| | | 528 | | } |
| | | 529 | | |
| | 0 | 530 | | if ( count > 0 ) |
| | | 531 | | { |
| | 0 | 532 | | WhenExceptionInMultipleStatements? whenException = null; |
| | 0 | 533 | | var stmt = connection.CreateStatementBuilder( new String( reader.Buffer, start, count ) ); |
| | 0 | 534 | | var enumerable = connection.PrepareStatementForExecution( stmt ); |
| | 0 | 535 | | var stmtInfo = stmt.StatementBuilderInformation; |
| | | 536 | | try |
| | | 537 | | { |
| | 0 | 538 | | await enumerable.EnumerateAsync( res => |
| | 0 | 539 | | { |
| | 0 | 540 | | connection.ProcessStatementResultPassively( reader, stmtInfo, res ); |
| | 0 | 541 | | } ); |
| | 0 | 542 | | } |
| | 0 | 543 | | catch ( SQLException sqle ) |
| | | 544 | | { |
| | | 545 | | try |
| | | 546 | | { |
| | 0 | 547 | | whenException = onException?.Invoke( sqle ); |
| | 0 | 548 | | } |
| | 0 | 549 | | catch |
| | | 550 | | { |
| | | 551 | | // Ignore |
| | 0 | 552 | | } |
| | | 553 | | |
| | 0 | 554 | | if ( !whenException.HasValue || whenException == WhenExceptionInMultipleStatements.Rethrow ) |
| | | 555 | | { |
| | 0 | 556 | | throw; |
| | | 557 | | } |
| | 0 | 558 | | } |
| | | 559 | | |
| | 0 | 560 | | if ( whenException.HasValue ) |
| | | 561 | | { |
| | | 562 | | // Have to issue ROLLBACK statement in order to continue from errors |
| | | 563 | | // Except that transaction is automatically rollbacked when an error occurs. |
| | | 564 | | //await connection.ExecuteNonQueryAsync( "ROLLBACK" ); |
| | | 565 | | |
| | 0 | 566 | | if ( whenException.Value == WhenExceptionInMultipleStatements.RollbackAndStartNew ) |
| | | 567 | | { |
| | | 568 | | // TODO additional optional parameter to specify additional parameters to BEGIN TRANSACTION (isolat |
| | | 569 | | // or, create DSL to SQLConnection |
| | 0 | 570 | | await connection.ExecuteAndIgnoreResults( "BEGIN TRANSACTION" ); |
| | | 571 | | } |
| | | 572 | | } |
| | | 573 | | |
| | 0 | 574 | | ++totalStatements; |
| | 0 | 575 | | } |
| | | 576 | | } |
| | | 577 | | |
| | 0 | 578 | | } while ( charsRead > 0 ); |
| | | 579 | | |
| | 0 | 580 | | return totalStatements; |
| | 0 | 581 | | } |
| | | 582 | | |
| | | 583 | | } |