1 // Npgsql.NpgsqlReadyState.cs
4 // Dave Joyner <d4ljoyn@yahoo.com>
6 // Copyright (C) 2002 The Npgsql Development Team
7 // npgsql-general@gborg.postgresql.org
8 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 using System
.Net
.Sockets
;
29 using System
.Resources
;
35 internal sealed class NpgsqlReadyState
: NpgsqlState
37 private static NpgsqlReadyState _instance
= null;
40 // Flush and Sync messages. It doesn't need to be created every time it is called.
41 private static readonly NpgsqlFlush _flushMessage
= new NpgsqlFlush();
43 private static readonly NpgsqlSync _syncMessage
= new NpgsqlSync();
45 private readonly String CLASSNAME
= "NpgsqlReadyState";
47 private NpgsqlReadyState() : base()
50 public static NpgsqlReadyState Instance
54 if ( _instance
== null )
56 _instance
= new NpgsqlReadyState();
64 public override void Query( NpgsqlConnector context
, NpgsqlCommand command
)
67 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Query");
69 String commandText
= command
.GetCommandText();
70 NpgsqlEventLog
.LogMsg(resman
, "Log_QuerySent", LogLevel
.Debug
, commandText
);
72 // Send the query request to backend.
74 NpgsqlQuery query
= new NpgsqlQuery(commandText
, context
.BackendProtocolVersion
);
75 BufferedStream stream
= new BufferedStream(context
.Stream
);
76 query
.WriteToStream(stream
, context
.Encoding
);
79 ProcessBackendResponses(context
);
83 public override void Parse(NpgsqlConnector context
, NpgsqlParse parse
)
85 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Parse");
86 BufferedStream stream
= new BufferedStream(context
.Stream
);
87 parse
.WriteToStream(stream
, context
.Encoding
);
92 public override void Sync(NpgsqlConnector context
)
94 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Sync");
95 _syncMessage
.WriteToStream(context
.Stream
, context
.Encoding
);
96 context
.Stream
.Flush();
97 ProcessBackendResponses(context
);
100 public override void Flush(NpgsqlConnector context
)
102 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Flush");
103 _flushMessage
.WriteToStream(context
.Stream
, context
.Encoding
);
104 ProcessBackendResponses(context
);
107 public override void Bind(NpgsqlConnector context
, NpgsqlBind bind
)
109 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Bind");
110 BufferedStream stream
= new BufferedStream(context
.Stream
);
111 bind
.WriteToStream(stream
, context
.Encoding
);
116 public override void Execute(NpgsqlConnector context
, NpgsqlExecute execute
)
119 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Execute");
120 NpgsqlDescribe describe
= new NpgsqlDescribe('P', execute
.PortalName
);
121 BufferedStream stream
= new BufferedStream(context
.Stream
);
122 describe
.WriteToStream(stream
, context
.Encoding
);
123 execute
.WriteToStream(stream
, context
.Encoding
);
128 public override void Close( NpgsqlConnector context
)
130 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Close");
131 Stream stream
= context
.Stream
;
132 stream
.WriteByte((Byte
)'X');
133 if (context
.BackendProtocolVersion
>= ProtocolVersion
.Version3
)
134 PGUtil
.WriteInt32(stream
, 4);
141 context
.Stream
= null;
142 ChangeState( context
, NpgsqlClosedState
.Instance
);