diff --git a/experiments/Peregrine/Peregrine/Ado/PeregrineCommand.cs b/experiments/Peregrine/Peregrine/Ado/PeregrineCommand.cs index 53cf8a9..cb29867 100644 --- a/experiments/Peregrine/Peregrine/Ado/PeregrineCommand.cs +++ b/experiments/Peregrine/Peregrine/Ado/PeregrineCommand.cs @@ -12,8 +12,15 @@ namespace Peregrine.Ado public class PeregrineCommand : DbCommand { private readonly PGSession _session; + private readonly PeregrineDataReader _reader; + private readonly ReadBuffer _readBuffer; - public PeregrineCommand(PGSession session) => _session = session; + public PeregrineCommand(PGSession session) + { + _session = session; + _readBuffer = session.ReadBuffer; + _reader = new PeregrineDataReader(session.ReadBuffer); + } public Task PrepareAsync() => _session.PrepareAsync("p", CommandText); @@ -21,26 +28,23 @@ namespace Peregrine.Ado CommandBehavior behavior, CancellationToken cancellationToken) { await _session.ExecuteAsync("p"); + await _readBuffer.ReceiveAsync(); - var readBuffer = _session.ReadBuffer; + var message = _readBuffer.ReadMessage(); - await readBuffer.ReceiveAsync(); - - var message = readBuffer.ReadMessage(); - - switch (message.Type) + switch (message) { case MessageType.BindComplete: - return new PeregrineDataReader(readBuffer); + return _reader; case MessageType.ErrorResponse: - throw new InvalidOperationException(readBuffer.ReadErrorMessage()); + throw new InvalidOperationException(_readBuffer.ReadErrorMessage()); default: - throw new NotImplementedException(message.Type.ToString()); + throw new NotImplementedException(message.ToString()); } } - + public override int ExecuteNonQuery() => throw new NotImplementedException(); diff --git a/experiments/Peregrine/Peregrine/Ado/PeregrineDataReader.cs b/experiments/Peregrine/Peregrine/Ado/PeregrineDataReader.cs index 7cb8ad1..e0fd1b1 100644 --- a/experiments/Peregrine/Peregrine/Ado/PeregrineDataReader.cs +++ b/experiments/Peregrine/Peregrine/Ado/PeregrineDataReader.cs @@ -23,12 +23,12 @@ namespace Peregrine.Ado { var message = _readBuffer.ReadMessage(); - switch (message.Type) + switch (message) { case MessageType.DataRow: { // Column count - _readBuffer.ReadShort(); + _readBuffer.SkipShort(); return _trueResult; } @@ -40,7 +40,7 @@ namespace Peregrine.Ado throw new InvalidOperationException(_readBuffer.ReadErrorMessage()); default: - throw new NotImplementedException(message.Type.ToString()); + throw new NotImplementedException(message.ToString()); } } diff --git a/experiments/Peregrine/Peregrine/PGSession.cs b/experiments/Peregrine/Peregrine/PGSession.cs index 58f6884..34f3154 100644 --- a/experiments/Peregrine/Peregrine/PGSession.cs +++ b/experiments/Peregrine/Peregrine/PGSession.cs @@ -69,7 +69,7 @@ namespace Peregrine var message = _readBuffer.ReadMessage(); - switch (message.Type) + switch (message) { case MessageType.ParseComplete: break; @@ -78,7 +78,7 @@ namespace Peregrine throw new InvalidOperationException(_readBuffer.ReadErrorMessage()); default: - throw new NotImplementedException(message.Type.ToString()); + throw new NotImplementedException(message.ToString()); } } @@ -160,7 +160,7 @@ namespace Peregrine var message = _readBuffer.ReadMessage(); - switch (message.Type) + switch (message) { case MessageType.BindComplete: goto read; @@ -191,7 +191,7 @@ namespace Peregrine throw new InvalidOperationException(_readBuffer.ReadErrorMessage()); default: - throw new NotImplementedException(message.Type.ToString()); + throw new NotImplementedException(message.ToString()); } } @@ -219,7 +219,7 @@ namespace Peregrine var message = _readBuffer.ReadMessage(); - switch (message.Type) + switch (message) { case MessageType.AuthenticationRequest: { @@ -261,10 +261,10 @@ namespace Peregrine case MessageType.EmptyQueryResponse: case MessageType.ParameterStatus: case MessageType.ReadyForQuery: - throw new NotImplementedException($"Unhandled MessageType '{message.Type}'"); + throw new NotImplementedException($"Unhandled MessageType '{message}'"); default: - throw new InvalidOperationException($"Unexpected MessageType '{message.Type}'"); + throw new InvalidOperationException($"Unexpected MessageType '{message}'"); } } diff --git a/experiments/Peregrine/Peregrine/ReadBuffer.cs b/experiments/Peregrine/Peregrine/ReadBuffer.cs index 9ce5166..a613cba 100644 --- a/experiments/Peregrine/Peregrine/ReadBuffer.cs +++ b/experiments/Peregrine/Peregrine/ReadBuffer.cs @@ -3,8 +3,6 @@ using System; using System.Buffers.Binary; -using System.IO; -using System.Threading.Tasks; namespace Peregrine { @@ -23,7 +21,17 @@ namespace Peregrine _awaitableSocket = awaitableSocket; } - internal (MessageType Type, int Length) ReadMessage() + internal MessageType ReadMessage() + { + var messageType = (MessageType)ReadByte(); + + // Skip length + _position += sizeof(int); + + return messageType; + } + + internal (MessageType Type, int Length) ReadMessageWithLength() { var messageType = (MessageType)ReadByte(); var length = ReadInt() - 4; @@ -68,6 +76,11 @@ namespace Peregrine return bs; } + public void SkipShort() + { + _position += sizeof(short); + } + public short ReadShort() { var result = BinaryPrimitives.ReadInt16BigEndian(_buffer.Span.Slice(_position, 2));