2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlMediator.cs
1 // created on 30/7/2002 at 00:31
2
3 // Npgsql.NpgsqlMediator.cs
4 //
5 // Author:
6 //      Francisco Jr. (fxjrlists@yahoo.com.br)
7 //
8 //      Copyright (C) 2002 The Npgsql Development Team
9 //      npgsql-general@gborg.postgresql.org
10 //      http://gborg.postgresql.org/project/npgsql/projdisplay.php
11 //
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26 using System;
27 using System.Text;
28 using System.Collections;
29 using System.Collections.Specialized;
30
31 namespace Npgsql
32 {
33     ///<summary>
34     /// This class is responsible for serving as bridge between the backend
35     /// protocol handling and the core classes. It is used as the mediator for
36     /// exchanging data generated/sent from/to backend.
37     /// </summary>
38     ///
39     internal sealed class NpgsqlMediator
40     {
41         //
42         // Expectations that depend on context.
43         // Non-default values must be set before collecting responses.
44         //
45         // Some kinds of messages only get one response, and do not
46         // expect a ready_for_query response.
47         private bool                  _require_ready_for_query;
48
49         //
50         // Responses collected from the backend.
51         //
52         private ArrayList                                                       _errors;
53         private ArrayList                                                       _notices;
54         private ArrayList                                                       _resultSets;
55         private ArrayList                                                       _responses;
56         private ArrayList             _notifications;
57         private ListDictionary        _parameters;
58         private NpgsqlBackEndKeyData  _backend_key_data;
59         private NpgsqlRowDescription    _rd;
60         private ArrayList                                                       _rows;
61
62         public NpgsqlMediator()
63         {
64             _require_ready_for_query = true;
65
66             _errors = new ArrayList();
67             _notices = new ArrayList();
68             _resultSets = new ArrayList();
69             _responses = new ArrayList();
70             _notifications = new ArrayList();
71             _parameters = new ListDictionary(CaseInsensitiveComparer.Default);
72             _backend_key_data = null;
73         }
74
75         public void ResetExpectations()
76         {
77             _require_ready_for_query = true;
78         }
79
80         public void ResetResponses()
81         {
82             _errors.Clear();
83             _notices.Clear();
84             _resultSets.Clear();
85             _responses.Clear();
86             _notifications.Clear();
87             _parameters.Clear();
88             _backend_key_data = null;
89         }
90
91
92
93
94         public Boolean RequireReadyForQuery
95         {
96             get
97             {
98                 return _require_ready_for_query;
99             }
100             set
101             {
102                 _require_ready_for_query = value;
103             }
104         }
105
106
107
108         public NpgsqlRowDescription LastRowDescription
109         {
110             get
111             {
112                 return _rd;
113             }
114         }
115
116         public ArrayList ResultSets
117         {
118             get
119             {
120                 return _resultSets;
121             }
122         }
123
124         public ArrayList CompletedResponses
125         {
126             get
127             {
128                 return _responses;
129             }
130         }
131
132         public ArrayList Errors
133         {
134             get
135             {
136                 return _errors;
137             }
138         }
139
140         public ArrayList Notices
141         {
142             get
143             {
144                 return _notices;
145             }
146         }
147
148         public ArrayList Notifications
149         {
150             get
151             {
152                 return _notifications;
153             }
154         }
155
156         public ListDictionary Parameters
157         {
158             get
159             {
160                 return _parameters;
161             }
162         }
163
164         public NpgsqlBackEndKeyData BackendKeyData
165         {
166             get
167             {
168                 return _backend_key_data;
169             }
170         }
171
172         public void AddNotification(NpgsqlNotificationEventArgs data)
173         {
174             _notifications.Add(data);
175         }
176
177         public void AddCompletedResponse(String response)
178         {
179             if (_rd != null)
180             {
181                 // Finished receiving the resultset. Add it to the buffer.
182                 _resultSets.Add(new NpgsqlResultSet(_rd, _rows));
183
184                 // Add a placeholder response.
185                 _responses.Add(null);
186
187                 // Discard the RowDescription.
188                 _rd = null;
189             }
190             else
191             {
192                 // Add a placeholder resultset.
193                 _resultSets.Add(null);
194                 // It was just a non query string. Just add the response.
195                 _responses.Add(response);
196             }
197
198         }
199
200         public void AddRowDescription(NpgsqlRowDescription rowDescription)
201         {
202             _rd = rowDescription;
203             _rows = new ArrayList();
204         }
205
206         public void AddAsciiRow(NpgsqlAsciiRow asciiRow)
207         {
208             _rows.Add(asciiRow);
209         }
210
211         public void AddBinaryRow(NpgsqlBinaryRow binaryRow)
212         {
213             _rows.Add(binaryRow);
214         }
215
216
217         public void SetBackendKeydata(NpgsqlBackEndKeyData keydata)
218         {
219             _backend_key_data = keydata;
220         }
221
222         public void AddParameterStatus(String Key, NpgsqlParameterStatus PS)
223         {
224             _parameters[Key] = PS;
225         }
226     }
227 }