Make System.Web.Script.Serialization.JavaScriptSerializer.ConvertToType(Type, object...
[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 = new NpgsqlReadyState();
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                 return _instance;
55             }
56         }
57
58
59
60         public override void Query( NpgsqlConnector context, NpgsqlCommand command )
61         {
62
63             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Query");
64
65
66
67             //String commandText = command.GetCommandText();
68             //NpgsqlEventLog.LogMsg(resman, "Log_QuerySent", LogLevel.Debug, commandText);
69
70             // Send the query request to backend.
71
72             NpgsqlQuery query = new NpgsqlQuery(command, context.BackendProtocolVersion);
73
74             query.WriteToStream(context.Stream, context.Encoding);
75             context.Stream.Flush();
76
77             ProcessBackendResponses(context);
78
79         }
80
81         public override void Parse(NpgsqlConnector context, NpgsqlParse parse)
82         {
83             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Parse");
84                         
85             Stream stream = context.Stream;
86             parse.WriteToStream(stream, context.Encoding);
87             //stream.Flush();
88         }
89
90
91         public override void Sync(NpgsqlConnector context)
92         {
93             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Sync");
94             _syncMessage.WriteToStream(context.Stream, context.Encoding);
95             context.Stream.Flush();
96             ProcessBackendResponses(context);
97         }
98
99         public override void Flush(NpgsqlConnector context)
100         {
101             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Flush");
102             _flushMessage.WriteToStream(context.Stream, context.Encoding);
103             context.Stream.Flush();
104             ProcessBackendResponses(context);
105         }
106
107         public override void Bind(NpgsqlConnector context, NpgsqlBind bind)
108         {
109             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Bind");
110             
111             Stream stream = context.Stream;
112             
113             bind.WriteToStream(stream, context.Encoding);
114             //stream.Flush();
115
116         }
117         
118         public override void Describe(NpgsqlConnector context, NpgsqlDescribe describe)
119         {
120             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Describe");
121             describe.WriteToStream(context.Stream, context.Encoding);
122             //context.Stream.Flush();
123         }
124
125         public override void Execute(NpgsqlConnector context, NpgsqlExecute execute)
126         {
127
128             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Execute");
129             NpgsqlDescribe describe = new NpgsqlDescribe('P', execute.PortalName);
130             Stream stream = context.Stream;
131             describe.WriteToStream(stream, context.Encoding);
132             execute.WriteToStream(stream, context.Encoding);
133             //stream.Flush();
134             Sync(context);
135         }
136
137         public override void Close( NpgsqlConnector context )
138         {
139             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Close");
140             Stream stream = context.Stream;
141             stream.WriteByte((Byte)'X');
142             if (context.BackendProtocolVersion >= ProtocolVersion.Version3)
143                 PGUtil.WriteInt32(stream, 4);
144             stream.Flush();
145
146             try
147             {
148                 stream.Close();
149             }
150             catch {}
151
152             context.Stream = null;
153         ChangeState( context, NpgsqlClosedState.Instance )
154             ;
155         }
156     }
157 }