1 // Npgsql.NpgsqlCopyInState.cs
4 // Kalle Hallivuori <kato@iki.fi>
6 // Copyright (C) 2007 The Npgsql Development Team
7 // npgsql-general@gborg.postgresql.org
8 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
10 // Permission to use, copy, modify, and distribute this software and its
11 // documentation for any purpose, without fee, and without a written
12 // agreement is hereby granted, provided that the above copyright notice
13 // and this paragraph and the following two paragraphs appear in all copies.
15 // IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
16 // FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
17 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
18 // DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
19 // THE POSSIBILITY OF SUCH DAMAGE.
21 // THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
22 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23 // AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
24 // ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
25 // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 /// Represents an ongoing COPY FROM STDIN operation.
35 /// Provides methods to push data to server and end or cancel the operation.
37 internal sealed class NpgsqlCopyInState
: NpgsqlState
39 public static readonly NpgsqlCopyInState Instance
= new NpgsqlCopyInState();
41 private NpgsqlCopyFormat _copyFormat
= null;
43 private NpgsqlCopyInState()
49 /// Copy format information returned from server.
51 public override NpgsqlCopyFormat CopyFormat
53 get { return _copyFormat; }
57 /// Called from NpgsqlState.ProcessBackendResponses upon CopyInResponse.
58 /// If CopyStream is already set, it is used to read data to push to server, after which the copy is completed.
59 /// Otherwise CopyStream is set to a writable NpgsqlCopyInStream that calls SendCopyData each time it is written to.
61 protected override void StartCopy(NpgsqlConnector context
, NpgsqlCopyFormat copyFormat
)
63 _copyFormat
= copyFormat
;
64 Stream userFeed
= context
.Mediator
.CopyStream
;
67 context
.Mediator
.CopyStream
= new NpgsqlCopyInStream(context
);
71 // copy all of user feed to server at once
72 int bufsiz
= context
.Mediator
.CopyBufferSize
;
73 byte[] buf
= new byte[bufsiz
];
75 while ((len
= userFeed
.Read(buf
, 0, bufsiz
)) > 0)
77 SendCopyData(context
, buf
, 0, len
);
79 SendCopyDone(context
);
84 /// Sends given packet to server as a CopyData message.
85 /// Does not check for notifications! Use another thread for that.
87 public override void SendCopyData(NpgsqlConnector context
, byte[] buf
, int off
, int len
)
89 Stream toServer
= context
.Stream
;
90 toServer
.WriteByte((byte) FrontEndMessageCode
.CopyData
);
91 PGUtil
.WriteInt32(toServer
, len
+ 4);
92 toServer
.Write(buf
, off
, len
);
96 /// Sends CopyDone message to server. Handles responses, ie. may throw an exception.
98 public override void SendCopyDone(NpgsqlConnector context
)
100 Stream toServer
= context
.Stream
;
101 toServer
.WriteByte((byte) FrontEndMessageCode
.CopyDone
);
102 PGUtil
.WriteInt32(toServer
, 4); // message without data
104 ProcessBackendResponses(context
);
108 /// Sends CopyFail message to server. Handles responses, ie. should always throw an exception:
109 /// in CopyIn state the server responds to CopyFail with an error response;
110 /// outside of a CopyIn state the server responds to CopyFail with an error response;
111 /// without network connection or whatever, there's going to eventually be a failure, timeout or user intervention.
113 public override void SendCopyFail(NpgsqlConnector context
, String message
)
115 Stream toServer
= context
.Stream
;
116 toServer
.WriteByte((byte) FrontEndMessageCode
.CopyFail
);
117 byte[] buf
= ENCODING_UTF8
.GetBytes((message
?? string.Empty
) + '\x00');
118 PGUtil
.WriteInt32(toServer
, 4 + buf
.Length
);
119 toServer
.Write(buf
, 0, buf
.Length
);
121 ProcessBackendResponses(context
);