public void SqlStream_ExecuteNonQuery_Enumerates_Entire_Stream() { int actual = 0; int expected = 100; var connection = new Mock <ISqlStreamConnection>(); connection.Setup(c => c.ExecuteNonQuery(It.IsAny <SqlCommand>())).Callback <SqlCommand>((cmd) => { var stream = cmd.Parameters["@stream"].Value as SqlStructuredParameterWrapper <StreamSchema>; Assert.IsNotNull(stream, "@stream parameter cannot be null."); foreach (var item in stream) { actual++; } }); using (SqlStream <StreamSchema> target = new SqlStream <StreamSchema>(connection.Object, SqlStreamBehavior.CloseConnection, 10)) { target.Parameters.Add("@userid", SqlDbType.Int).Value = 1; target.Parameters.AddStructured <StreamSchema>("@stream", "dbo.StreamUDT", target) .Map(src => src.Id, "Id", SqlDbType.Int) .Map(src => src.ProductName, "ProductName", SqlDbType.VarChar, 255) .Map(src => Convert.ToDecimal(src.Price), "Price", SqlDbType.Decimal, 9, 3); for (int i = 0; i < expected; i++) { target.Write(new StreamSchema { Id = i, ProductName = String.Format("Product {0}", i), Price = (i + 1.0) - 0.01 }); //simulate I/O Thread.Sleep(3); } } Assert.AreEqual(expected, actual, "Data wasn't streamed."); }
public void SqlStream_Integration_Test_With_Output_Parameter() { int actual = 0; int expected = 100; using (SqlStream <StreamSchema> target = new SqlStream <StreamSchema>(new SqlStreamConnection("Server=(local);Database=tempdb;Trusted_Connection=Yes;"), SqlStreamBehavior.CloseConnection, 10)) { target.StoredProcedureName = "dbo.TVPTestProc"; target.Parameters.AddStructured <StreamSchema>("@stream", "dbo.StreamSchema", target) .Map(src => src.Id, "Id", SqlDbType.Int) .Map(src => src.ProductName, "ProductName", SqlDbType.VarChar, 255) .Map(src => Convert.ToDecimal(src.Price), "Price", SqlDbType.Decimal, 9, 3); target.Parameters.Add("@userid", SqlDbType.Int).Value = 1; var output = target.Parameters.Add("@resultCount", SqlDbType.Int); output.Direction = ParameterDirection.InputOutput; output.Value = 0; for (int i = 0; i < expected; i++) { target.Write(new StreamSchema { Id = i, ProductName = String.Format("Product {0}", i), Price = (i + 1.0) - 0.01 }); } // need to wait for Close() or Dispose() before checking output parameters target.Close(); actual = Convert.ToInt32(output.Value); } Assert.AreEqual(expected, actual, "Data wasn't streamed."); }
/// <summary> /// Persists the specified stream of data in the queue. /// </summary> /// <param name="data">The stream containing the data to be persisted.</param> /// <returns>A data transfer object carrying the details of the persisted queue item.</returns> public PersistenceQueueItemInfo Enqueue(Stream data) { Guard.ArgumentNotNull(data, "data"); Guard.ArgumentNotNullOrEmptyString(this.dbConnectionString, "dbConnectionString"); var callToken = TraceManager.DataAccessComponent.TraceIn(StreamingMode, StreamingDataType); var scopeStartEnqueueMain = TraceManager.DataAccessComponent.TraceStartScope(Resources.ScopeSqlAzurePersistenceQueueEnqueueMain, callToken); try { PersistenceQueueItemInfo queueItemInfo = null; Guid txGuid = default(Guid); using (var txScope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.MaxValue)) using (var dbConnection = new ReliableSqlConnection(this.dbConnectionString, this.connectionRetryPolicy, this.commandRetryPolicy)) { if (StreamingDataType == StreamingDataType.Raw) { var scopeStartExecuteQuery = TraceManager.DataAccessComponent.TraceStartScope(Resources.ScopeSqlAzurePersistenceQueueExecuteCommand, callToken); using (IDbCommand newItemCommand = CustomSqlCommandFactory.SqlAzurePersistenceQueue.CreateNewCommand(dbConnection)) { TraceManager.DataAccessComponent.TraceCommand(newItemCommand); txGuid = dbConnection.ExecuteCommand <Guid>(newItemCommand); using (IDbCommand readDataCommand = CustomSqlCommandFactory.SqlAzurePersistenceQueue.CreateQueueItemReadCommand(dbConnection, txGuid)) using (IDbCommand writeDataCommand = CustomSqlCommandFactory.SqlAzurePersistenceQueue.CreateQueueItemWriteCommand(dbConnection, txGuid)) using (IDbCommand getDataSizeCommand = CustomSqlCommandFactory.SqlAzurePersistenceQueue.CreateQueueItemGetSizeCommand(dbConnection, txGuid)) using (SqlStream sqlStream = new SqlStream(dbConnection, readDataCommand as SqlCommand, writeDataCommand as SqlCommand, getDataSizeCommand as SqlCommand)) { BinaryReader dataReader = new BinaryReader(data); byte[] buffer = new byte[this.initialBufferSize]; int bytesRead = 0; do { var scopeStartBufferRead = TraceManager.DataAccessComponent.TraceStartScope(Resources.ScopeSqlAzurePersistenceQueueBufferedReadBytes, callToken); bytesRead = dataReader.ReadBuffered(buffer, 0, this.initialBufferSize); TraceManager.DataAccessComponent.TraceEndScope(Resources.ScopeSqlAzurePersistenceQueueBufferedReadBytes, scopeStartBufferRead, callToken); if (bytesRead > 0) { TraceManager.DataAccessComponent.TraceInfo(TraceLogMessages.SqlStreamWriteOperationDetails, bytesRead); var scopeStartSqlWriteData = TraceManager.DataAccessComponent.TraceStartScope(Resources.ScopeSqlAzurePersistenceQueueWriteData, callToken); sqlStream.Write(buffer, 0, bytesRead); TraceManager.DataAccessComponent.TraceEndScope(Resources.ScopeSqlAzurePersistenceQueueWriteData, scopeStartSqlWriteData, callToken); } }while (bytesRead > 0); } } using (IDbCommand enqueueCommand = CustomSqlCommandFactory.SqlAzurePersistenceQueue.CreateEnqueueCommand(dbConnection, txGuid)) { TraceManager.DataAccessComponent.TraceCommand(enqueueCommand); dbConnection.ExecuteCommand(enqueueCommand); SqlCommandView <EnqueueCommandInspector> enqueueCommandView = new SqlCommandView <EnqueueCommandInspector>(enqueueCommand); queueItemInfo = new PersistenceQueueItemInfo(txGuid, enqueueCommandView.Inspector.QueueItemSize, enqueueCommandView.Inspector.QueueItemType); } txScope.Complete(); TraceManager.DataAccessComponent.TraceEndScope(Resources.ScopeSqlAzurePersistenceQueueExecuteCommand, scopeStartExecuteQuery, callToken); } return(queueItemInfo); } } catch (Exception ex) { TraceManager.DataAccessComponent.TraceError(ex, callToken); throw; } finally { TraceManager.DataAccessComponent.TraceEndScope(Resources.ScopeSqlAzurePersistenceQueueEnqueueMain, scopeStartEnqueueMain, callToken); TraceManager.DataAccessComponent.TraceOut(callToken); } }