Ejemplo n.º 1
0
		public Task<int> CommitQueryBatchAsync(
			QueryBatch queryBatch,
			int batchSize = 0,
			CancellationToken cancellationToken = default,
			[CallerMemberName] string callerMemberName = null,
			[CallerFilePath] string callerFilePath = null,
			[CallerLineNumber] int callerLineNumber = 0
		)
		{
			string commandString;
			int cumulativeResult = 0;
			int queryBatchCount = queryBatch.queryList.Count;
			int index = -1;

			if (batchSize == 0) batchSize = this.BatchSize;
			int shortBatchSize = batchSize / 3;

			var callerIdentity = callerIdentityDelegate();

			var batches = queryBatch.queryList.GroupBy(_ =>
			{
				if (queryBatchCount - index - 1 > shortBatchSize)
					++index;
				return index / batchSize;
			});

			using (var ts = DbContext.CreateTransactionScope())
			{
				using (var connWrapper = this.GetWrappedConnection())
				{
					var conn = connWrapper.Connection;

					foreach (var batch in batches)
					{
						if (conn.State != ConnectionState.Open) conn.Open();

						using (var sqlCommandSetWrapper = new SqlCommandSetWrapper())
						{
							foreach (var element in batch)
							{
								commandString = string.Concat(CommandExtensions.CMD_HEADER_QUERYBATCH, element.Item1, CommandExtensions.CMD_FOOTER);
								var command = new SqlCommand(commandString);
								if (element.Item2 != null)
								{
									command.SetParametersFromDictionary(element.Item2);
								}

								command.SetupMetaParameters(callerIdentity.UserIdAsBytes, callerMemberName, callerFilePath, callerLineNumber);
								sqlCommandSetWrapper.Append(command);
							}//foreach element loop

							sqlCommandSetWrapper.Connection = conn;
							sqlCommandSetWrapper.CommandTimeout = 0; // set infinite timeout for all sql commands in SqlCommandSet
							cumulativeResult += sqlCommandSetWrapper.ExecuteNonQuery();
						}// using SqlCommandSetWrapper
						if (cancellationToken.IsCancellationRequested) break;
					}//foreach batch loop
				}//connWrapper
				ts.Complete();
			}//ts
			return Task.FromResult(cumulativeResult);
		}// CommitQueryBatchAsync()
Ejemplo n.º 2
0
		}// AddQuery() - QueryInfo

		public QueryBatch Append(QueryBatch queryBatch)
		{
			this.queryList.AddRange(queryBatch.queryList);
			return this;
		}// Append()