2009-05-13 Atsushi Enomoto <atsushi@ximian.com>
[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         private String                  _sqlSent;
62         private Int32                   _commandTimeout;
63         
64
65         public NpgsqlMediator()
66         {
67             _require_ready_for_query = true;
68
69             _errors = new ArrayList();
70             _notices = new ArrayList();
71             _resultSets = new ArrayList();
72             _responses = new ArrayList();
73             _notifications = new ArrayList();
74             _parameters = new ListDictionary(CaseInsensitiveComparer.Default);
75             _backend_key_data = null;
76             _sqlSent = String.Empty;
77             _commandTimeout = 20;
78         }
79
80         public void ResetExpectations()
81         {
82             _require_ready_for_query = true;
83         }
84
85         public void ResetResponses()
86         {
87             _errors.Clear();
88             _notices.Clear();
89             _resultSets.Clear();
90             _responses.Clear();
91             _notifications.Clear();
92             _parameters.Clear();
93             _backend_key_data = null;
94             _sqlSent = String.Empty;
95             _commandTimeout = 20;
96         }
97
98
99
100
101         public Boolean RequireReadyForQuery
102         {
103             get
104             {
105                 return _require_ready_for_query;
106             }
107             set
108             {
109                 _require_ready_for_query = value;
110             }
111         }
112
113
114
115         public NpgsqlRowDescription LastRowDescription
116         {
117             get
118             {
119                 return _rd;
120             }
121         }
122
123         public ArrayList ResultSets
124         {
125             get
126             {
127                 return _resultSets;
128             }
129         }
130
131         public ArrayList CompletedResponses
132         {
133             get
134             {
135                 return _responses;
136             }
137         }
138
139         public ArrayList Errors
140         {
141             get
142             {
143                 return _errors;
144             }
145         }
146
147         public ArrayList Notices
148         {
149             get
150             {
151                 return _notices;
152             }
153         }
154
155         public ArrayList Notifications
156         {
157             get
158             {
159                 return _notifications;
160             }
161         }
162
163         public ListDictionary Parameters
164         {
165             get
166             {
167                 return _parameters;
168             }
169         }
170
171         public NpgsqlBackEndKeyData BackendKeyData
172         {
173             get
174             {
175                 return _backend_key_data;
176             }
177         }
178         
179         public String SqlSent
180         {
181             set
182             {
183                 _sqlSent = value;
184             }
185             
186             get
187             {
188                 return _sqlSent;
189             }
190         }
191         
192         public Int32 CommandTimeout
193         {
194             set
195             {
196                 _commandTimeout = value;
197             }
198             
199             get
200             {
201                 return _commandTimeout;
202             }
203         
204         }
205
206         public void AddNotification(NpgsqlNotificationEventArgs data)
207         {
208             _notifications.Add(data);
209         }
210
211         public void AddCompletedResponse(String response)
212         {
213             if (_rd != null)
214             {
215                 // Finished receiving the resultset. Add it to the buffer.
216                 _resultSets.Add(new NpgsqlResultSet(_rd, _rows));
217
218                 // Add a placeholder response.
219                 _responses.Add(null);
220
221                 // Discard the RowDescription.
222                 _rd = null;
223             }
224             else
225             {
226                 // Add a placeholder resultset.
227                 _resultSets.Add(null);
228                 // It was just a non query string. Just add the response.
229                 _responses.Add(response);
230             }
231
232         }
233
234         public void AddRowDescription(NpgsqlRowDescription rowDescription)
235         {
236             _rd = rowDescription;
237             _rows = new ArrayList();
238         }
239
240         public void AddAsciiRow(NpgsqlAsciiRow asciiRow)
241         {
242             _rows.Add(asciiRow);
243         }
244
245         public void AddBinaryRow(NpgsqlBinaryRow binaryRow)
246         {
247             _rows.Add(binaryRow);
248         }
249
250
251         public void SetBackendKeydata(NpgsqlBackEndKeyData keydata)
252         {
253             _backend_key_data = keydata;
254         }
255
256         public void AddParameterStatus(String Key, NpgsqlParameterStatus PS)
257         {
258             _parameters[Key] = PS;
259         }
260     }
261 }