1 // Npgsql.NpgsqlCopyInStream.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 /// Stream for writing data to a table on a PostgreSQL version 7.4 or newer database during an active COPY FROM STDIN operation.
35 /// <b>Passes data exactly as is and when given</b>, so see to it that you use server encoding, correct format and reasonably sized writes!
37 internal class NpgsqlCopyInStream
: Stream
39 private NpgsqlConnector _context
;
40 private long _bytesPassed
= 0;
43 /// True while this stream can be used to write copy data to server
47 get { return _context != null && _context.CurrentState is NpgsqlCopyInState && _context.Mediator.CopyStream == this; }
51 /// Created only by NpgsqlCopyInState.StartCopy()
53 internal NpgsqlCopyInStream(NpgsqlConnector context
)
61 public override bool CanRead
69 public override bool CanWrite
77 public override bool CanSeek
83 /// Number of bytes written so far
85 public override long Length
87 get { return _bytesPassed; }
91 /// Number of bytes written so far; not settable
93 public override long Position
95 get { return _bytesPassed; }
96 set { throw new NotSupportedException("Tried to set Position of network stream " + this); }
100 /// Successfully completes copying data to server. Returns after operation is finished.
101 /// Does nothing if this stream is not the active copy operation writer.
103 public override void Close()
105 if (_context
!= null)
109 _context
.CurrentState
.SendCopyDone(_context
);
111 if (_context
.Mediator
.CopyStream
== this)
113 _context
.Mediator
.CopyStream
= null;
120 /// Withdraws an already started copy operation. The operation will fail with given error message.
121 /// Does nothing if this stream is not the active copy operation writer.
123 public void Cancel(string message
)
127 NpgsqlConnector c
= _context
;
129 c
.Mediator
.CopyStream
= null;
130 c
.CurrentState
.SendCopyFail(_context
, message
?? "Cancel Copy");
135 /// Writes given bytes to server.
136 /// Fails if this stream is not the active copy operation writer.
138 public override void Write(byte[] buf
, int off
, int len
)
142 throw new ObjectDisposedException("Writing to closed " + this);
144 _context
.CurrentState
.SendCopyData(_context
, buf
, off
, len
);
149 /// Flushes stream contents to server.
150 /// Fails if this stream is not the active copy operation writer.
152 public override void Flush()
156 throw new ObjectDisposedException("Flushing closed " + this);
158 _context
.Stream
.Flush();
164 public override int Read(byte[] buf
, int off
, int len
)
166 throw new NotSupportedException("Tried to read non-readable " + this);
172 public override long Seek(long pos
, SeekOrigin so
)
174 throw new NotSupportedException("Tried to seek non-seekable " + this);
180 public override void SetLength(long len
)
182 throw new NotSupportedException("Tried to set length of network stream " + this);