2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlReadyState.cs
1 // Npgsql.NpgsqlReadyState.cs
2 //
3 // Author:
4 //      Dave Joyner <d4ljoyn@yahoo.com>
5 //
6 //      Copyright (C) 2002 The Npgsql Development Team
7 //      npgsql-general@gborg.postgresql.org
8 //      http://gborg.postgresql.org/project/npgsql/projdisplay.php
9 //
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.
14 //
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.
19 //
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
23
24
25 using System;
26 using System.IO;
27 using System.Net;
28 using System.Net.Sockets;
29 using System.Resources;
30
31 namespace Npgsql
32 {
33
34
35     internal sealed class NpgsqlReadyState : NpgsqlState
36     {
37         private static NpgsqlReadyState _instance = null;
38
39
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();
42
43         private static readonly NpgsqlSync _syncMessage = new NpgsqlSync();
44
45         private readonly String CLASSNAME = "NpgsqlReadyState";
46
47         private NpgsqlReadyState() : base()
48         { }
49
50         public static NpgsqlReadyState Instance
51         {
52             get
53             {
54                 if ( _instance == null )
55                 {
56                     _instance = new NpgsqlReadyState();
57                 }
58                 return _instance;
59             }
60         }
61
62
63
64         public override void Query( NpgsqlConnector context, NpgsqlCommand command )
65         {
66
67             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Query");
68
69
70
71             //String commandText = command.GetCommandText();
72             //NpgsqlEventLog.LogMsg(resman, "Log_QuerySent", LogLevel.Debug, commandText);
73
74             // Send the query request to backend.
75
76             NpgsqlQuery query = new NpgsqlQuery(command, context.BackendProtocolVersion);
77
78             BufferedStream stream = new BufferedStream(context.Stream);
79             query.WriteToStream(stream, context.Encoding);
80             stream.Flush();
81
82             ProcessBackendResponses(context);
83
84         }
85
86         public override void Parse(NpgsqlConnector context, NpgsqlParse parse)
87         {
88             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Parse");
89             BufferedStream stream = new BufferedStream(context.Stream);
90             parse.WriteToStream(stream, context.Encoding);
91             stream.Flush();
92         }
93
94
95         public override void Sync(NpgsqlConnector context)
96         {
97             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Sync");
98             _syncMessage.WriteToStream(context.Stream, context.Encoding);
99             context.Stream.Flush();
100             ProcessBackendResponses(context);
101         }
102
103         public override void Flush(NpgsqlConnector context)
104         {
105             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Flush");
106             _flushMessage.WriteToStream(context.Stream, context.Encoding);
107             ProcessBackendResponses(context);
108         }
109
110         public override void Bind(NpgsqlConnector context, NpgsqlBind bind)
111         {
112             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Bind");
113             BufferedStream stream = new BufferedStream(context.Stream);
114             bind.WriteToStream(stream, context.Encoding);
115             stream.Flush();
116
117         }
118
119         public override void Execute(NpgsqlConnector context, NpgsqlExecute execute)
120         {
121
122             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Execute");
123             NpgsqlDescribe describe = new NpgsqlDescribe('P', execute.PortalName);
124             BufferedStream stream = new BufferedStream(context.Stream);
125             describe.WriteToStream(stream, context.Encoding);
126             execute.WriteToStream(stream, context.Encoding);
127             stream.Flush();
128             Sync(context);
129         }
130
131         public override void Close( NpgsqlConnector context )
132         {
133             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Close");
134             Stream stream = context.Stream;
135             stream.WriteByte((Byte)'X');
136             if (context.BackendProtocolVersion >= ProtocolVersion.Version3)
137                 PGUtil.WriteInt32(stream, 4);
138             stream.Flush();
139
140             try
141             {
142                 stream.Close();
143             }
144             catch {}
145
146             context.Stream = null;
147         ChangeState( context, NpgsqlClosedState.Instance )
148             ;
149         }
150     }
151 }