1 // Npgsql.NpgsqlCopyOutState.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 TO STDOUT operation.
35 /// Provides methods to read data from server or end the operation.
37 internal sealed class NpgsqlCopyOutState : NpgsqlState
39 public static readonly NpgsqlCopyOutState Instance = new NpgsqlCopyOutState();
41 //private readonly String CLASSNAME = "NpgsqlCopyOutState";
43 private NpgsqlCopyFormat _copyFormat = null;
45 private NpgsqlCopyOutState()
51 /// Copy format information returned from server.
53 public override NpgsqlCopyFormat CopyFormat
55 get { return _copyFormat; }
59 /// Called from NpgsqlState.ProcessBackendResponses upon CopyOutResponse.
60 /// If CopyStream is already set, it is used to write data received from server, after which the copy ends.
61 /// Otherwise CopyStream is set to a readable NpgsqlCopyOutStream that receives data from server.
63 protected override void StartCopy(NpgsqlConnector context, NpgsqlCopyFormat copyFormat)
65 _copyFormat = copyFormat;
66 Stream userFeed = context.Mediator.CopyStream;
69 context.Mediator.CopyStream = new NpgsqlCopyOutStream(context);
74 while ((buf = GetCopyData(context)) != null)
76 userFeed.Write(buf, 0, buf.Length);
83 /// Called from NpgsqlOutStream.Read to read copy data from server.
85 public override byte[] GetCopyData(NpgsqlConnector context)
87 // polling in COPY would take seconds on Windows
88 foreach (IServerResponseObject obj in ProcessBackendResponses_Ver_3(context))
90 if (obj is IDisposable)
92 (obj as IDisposable).Dispose();
95 return context.Mediator.ReceivedCopyData;