Merge pull request #270 from bholmes/COM_cctor
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlCopyOutState.cs
1 // Npgsql.NpgsqlCopyOutState.cs
2 //
3 // Author:
4 //      Kalle Hallivuori <kato@iki.fi>
5 //
6 //      Copyright (C) 2007 The Npgsql Development Team
7 //      npgsql-general@gborg.postgresql.org
8 //      http://gborg.postgresql.org/project/npgsql/projdisplay.php
9 //
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.
14 // 
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.
20 // 
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.
26
27
28 using System;
29 using System.IO;
30
31 namespace Npgsql
32 {
33         /// <summary>
34         /// Represents an ongoing COPY TO STDOUT operation.
35         /// Provides methods to read data from server or end the operation.
36         /// </summary>
37         internal sealed class NpgsqlCopyOutState : NpgsqlState
38         {
39                 public static readonly NpgsqlCopyOutState Instance = new NpgsqlCopyOutState();
40
41                 //private readonly String CLASSNAME = "NpgsqlCopyOutState";
42
43                 private NpgsqlCopyFormat _copyFormat = null;
44
45                 private NpgsqlCopyOutState()
46                         : base()
47                 {
48                 }
49
50                 /// <summary>
51                 /// Copy format information returned from server.
52                 /// </summary>
53                 public override NpgsqlCopyFormat CopyFormat
54                 {
55                         get { return _copyFormat; }
56                 }
57
58                 /// <summary>
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.
62                 /// </summary>
63                 protected override void StartCopy(NpgsqlConnector context, NpgsqlCopyFormat copyFormat)
64                 {
65                         _copyFormat = copyFormat;
66                         Stream userFeed = context.Mediator.CopyStream;
67                         if (userFeed == null)
68                         {
69                                 context.Mediator.CopyStream = new NpgsqlCopyOutStream(context);
70                         }
71                         else
72                         {
73                                 byte[] buf;
74                                 while ((buf = GetCopyData(context)) != null)
75                                 {
76                                         userFeed.Write(buf, 0, buf.Length);
77                                 }
78                                 userFeed.Close();
79                         }
80                 }
81
82                 /// <summary>
83                 /// Called from NpgsqlOutStream.Read to read copy data from server.
84                 /// </summary>
85                 public override byte[] GetCopyData(NpgsqlConnector context)
86                 {
87                         // polling in COPY would take seconds on Windows
88                         foreach (IServerResponseObject obj in ProcessBackendResponses_Ver_3(context))
89                         {
90                                 if (obj is IDisposable)
91                                 {
92                                         (obj as IDisposable).Dispose();
93                                 }
94                         }
95                         return context.Mediator.ReceivedCopyData;
96                 }
97         }
98 }