Make System.Web.Script.Serialization.JavaScriptSerializer.ConvertToType(Type, object...
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlCopyInState.cs
1 // Npgsql.NpgsqlCopyInState.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 FROM STDIN operation.
35         /// Provides methods to push data to server and end or cancel the operation.
36         /// </summary>
37         internal sealed class NpgsqlCopyInState : NpgsqlState
38         {
39                 public static readonly NpgsqlCopyInState Instance = new NpgsqlCopyInState();
40
41                 private NpgsqlCopyFormat _copyFormat = null;
42
43                 private NpgsqlCopyInState()
44                         : base()
45                 {
46                 }
47
48                 /// <summary>
49                 /// Copy format information returned from server.
50                 /// </summary>
51                 public override NpgsqlCopyFormat CopyFormat
52                 {
53                         get { return _copyFormat; }
54                 }
55
56                 /// <summary>
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.
60                 /// </summary>
61                 protected override void StartCopy(NpgsqlConnector context, NpgsqlCopyFormat copyFormat)
62                 {
63                         _copyFormat = copyFormat;
64                         Stream userFeed = context.Mediator.CopyStream;
65                         if (userFeed == null)
66                         {
67                                 context.Mediator.CopyStream = new NpgsqlCopyInStream(context);
68                         }
69                         else
70                         {
71                                 // copy all of user feed to server at once
72                                 int bufsiz = context.Mediator.CopyBufferSize;
73                                 byte[] buf = new byte[bufsiz];
74                                 int len;
75                                 while ((len = userFeed.Read(buf, 0, bufsiz)) > 0)
76                                 {
77                                         SendCopyData(context, buf, 0, len);
78                                 }
79                                 SendCopyDone(context);
80                         }
81                 }
82
83                 /// <summary>
84                 /// Sends given packet to server as a CopyData message.
85                 /// Does not check for notifications! Use another thread for that.
86                 /// </summary>
87                 public override void SendCopyData(NpgsqlConnector context, byte[] buf, int off, int len)
88                 {
89                         Stream toServer = context.Stream;
90                         toServer.WriteByte((byte) FrontEndMessageCode.CopyData);
91                         PGUtil.WriteInt32(toServer, len + 4);
92                         toServer.Write(buf, off, len);
93                 }
94
95                 /// <summary>
96                 /// Sends CopyDone message to server. Handles responses, ie. may throw an exception.
97                 /// </summary>
98                 public override void SendCopyDone(NpgsqlConnector context)
99                 {
100                         Stream toServer = context.Stream;
101                         toServer.WriteByte((byte) FrontEndMessageCode.CopyDone);
102                         PGUtil.WriteInt32(toServer, 4); // message without data
103                         toServer.Flush();
104                         ProcessBackendResponses(context);
105                 }
106
107                 /// <summary>
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.
112                 /// </summary>
113                 public override void SendCopyFail(NpgsqlConnector context, String message)
114                 {
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);
120                         toServer.Flush();
121                         ProcessBackendResponses(context);
122                 }
123         }
124 }