* FileSystemInfo.cs: corrected COM visibility of UTC properties
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlDataAdapter.cs
1 // created on 1/8/2002 at 23:02
2 //
3 // Npgsql.NpgsqlDataAdapter.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 //
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2.1 of the License, or (at your option) any later version.
17 //
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 // Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27 using System;
28 using System.Data;
29 using System.Data.Common;
30 using System.Resources;
31
32 namespace Npgsql
33 {
34     /// <summary>
35     /// Represents the method that handles the <see cref="Npgsql.NpgsqlDataAdapter.RowUpdated">RowUpdated</see> events.
36     /// </summary>
37     /// <param name="sender">The source of the event.</param>
38     /// <param name="e">A <see cref="Npgsql.NpgsqlRowUpdatedEventArgs">NpgsqlRowUpdatedEventArgs</see> that contains the event data.</param>
39     public delegate void RowUpdatedEventHandler(Object sender, NpgsqlRowUpdatedEventArgs e);
40     
41     /// <summary>
42     /// Represents the method that handles the <see cref="Npgsql.NpgsqlDataAdapter.RowUpdating">RowUpdating</see> events.
43     /// </summary>
44     /// <param name="sender">The source of the event.</param>
45     /// <param name="e">A <see cref="Npgsql.NpgsqlRowUpdatingEventArgs">NpgsqlRowUpdatingEventArgs</see> that contains the event data.</param>
46     public delegate void RowUpdatingEventHandler(Object sender, NpgsqlRowUpdatingEventArgs e);
47     
48     
49     public sealed class NpgsqlDataAdapter : DbDataAdapter, IDbDataAdapter
50     {
51
52         private NpgsqlCommand   _selectCommand;
53         private NpgsqlCommand           _updateCommand;
54         private NpgsqlCommand           _deleteCommand;
55         private NpgsqlCommand           _insertCommand;
56
57         private NpgsqlCommandBuilder cmd_builder;
58
59         // Log support
60         private static readonly String CLASSNAME = "NpgsqlDataAdapter";
61         
62         
63         public event RowUpdatedEventHandler RowUpdated;
64         public event RowUpdatingEventHandler RowUpdating;        
65
66         public NpgsqlDataAdapter()
67         {}
68
69         public NpgsqlDataAdapter(NpgsqlCommand selectCommand)
70         {
71             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
72             _selectCommand = selectCommand;
73             cmd_builder = new NpgsqlCommandBuilder(this);
74         }
75
76         public NpgsqlDataAdapter(String selectCommandText, NpgsqlConnection selectConnection) : this(new NpgsqlCommand(selectCommandText, selectConnection))
77         {}
78
79         public NpgsqlDataAdapter(String selectCommandText, String selectConnectionString) : this(selectCommandText, new NpgsqlConnection(selectConnectionString))
80         {}
81
82
83         protected override RowUpdatedEventArgs CreateRowUpdatedEvent(
84             DataRow dataRow,
85             IDbCommand command,
86             StatementType statementType,
87             DataTableMapping tableMapping
88         )
89         {
90             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CreateRowUpdatedEvent");
91             return new NpgsqlRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
92
93
94
95         }
96
97         protected override RowUpdatingEventArgs CreateRowUpdatingEvent(
98             DataRow dataRow,
99             IDbCommand command,
100             StatementType statementType,
101             DataTableMapping tableMapping
102         )
103         {
104             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CreateRowUpdatingEvent");
105             return new NpgsqlRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
106         }
107
108         protected override void OnRowUpdated(
109             RowUpdatedEventArgs value
110         )
111         {
112             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "OnRowUpdated");
113             //base.OnRowUpdated(value);
114             if ((RowUpdated != null) && (value is NpgsqlRowUpdatedEventArgs))
115                 RowUpdated(this, (NpgsqlRowUpdatedEventArgs) value);
116
117         }
118
119         protected override void OnRowUpdating(
120             RowUpdatingEventArgs value
121         )
122         {
123             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "OnRowUpdating");
124             if ((RowUpdating != null) && (value is NpgsqlRowUpdatingEventArgs))
125                 RowUpdating(this, (NpgsqlRowUpdatingEventArgs) value);
126                         
127             /*switch (value.StatementType)
128             {
129                 case StatementType.Insert:
130                     value.Command = cmd_builder.GetInsertCommand(value.Row);
131                     break;
132                 case StatementType.Update:
133                     value.Command = cmd_builder.GetUpdateCommand(value.Row);
134                     break;
135                 case StatementType.Delete:
136                     value.Command = cmd_builder.GetDeleteCommand(value.Row);
137                     break;
138             }
139             DataColumnMappingCollection columnMappings = value.TableMapping.ColumnMappings;
140             foreach (IDataParameter parameter in value.Command.Parameters)
141             {
142
143                 string dsColumnName = parameter.SourceColumn;
144                 if (columnMappings.Contains(parameter.SourceColumn))
145                 {
146                     DataColumnMapping mapping = columnMappings[parameter.SourceColumn];
147                     if (mapping != null)
148                     {
149                         dsColumnName = mapping.DataSetColumn;
150                     }
151                 }
152                 DataRowVersion rowVersion = DataRowVersion.Default;
153                 if (value.StatementType == StatementType.Update)
154                     rowVersion = parameter.SourceVersion;
155                 if (value.StatementType == StatementType.Delete)
156                     rowVersion = DataRowVersion.Original;
157                 parameter.Value = value.Row [dsColumnName, rowVersion];
158             }
159             value.Row.AcceptChanges ();*/
160             
161         }
162
163         ITableMappingCollection IDataAdapter.TableMappings
164         {
165             get
166             {
167                 return TableMappings;
168             }
169         }
170
171         IDbCommand IDbDataAdapter.DeleteCommand
172         {
173             get
174             {
175                 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IDbDataAdapter.DeleteCommand");
176                 return (NpgsqlCommand) DeleteCommand;
177             }
178
179             set
180             {
181                 DeleteCommand = (NpgsqlCommand) value;
182             }
183         }
184
185
186         public NpgsqlCommand DeleteCommand
187         {
188             get
189             {
190                 return _deleteCommand;
191             }
192
193             set
194             {
195                 _deleteCommand = value;
196             }
197         }
198
199         IDbCommand IDbDataAdapter.SelectCommand
200         {
201             get
202             {
203                 return (NpgsqlCommand) SelectCommand;
204             }
205
206             set
207             {
208                 SelectCommand = (NpgsqlCommand) value;
209             }
210         }
211
212
213         public NpgsqlCommand SelectCommand
214         {
215             get
216             {
217                 return _selectCommand;
218             }
219
220             set
221             {
222                 _selectCommand = value;
223             }
224         }
225
226         IDbCommand IDbDataAdapter.UpdateCommand
227         {
228             get
229             {
230                 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IDbDataAdapter.UpdateCommand");
231                 return (NpgsqlCommand) UpdateCommand;
232             }
233
234             set
235             {
236                 UpdateCommand = (NpgsqlCommand) value;
237             }
238         }
239
240
241         public NpgsqlCommand UpdateCommand
242         {
243             get
244             {
245                 return _updateCommand;
246             }
247
248             set
249             {
250                 _updateCommand = value;
251             }
252         }
253
254         IDbCommand IDbDataAdapter.InsertCommand
255         {
256             get
257             {
258                 return (NpgsqlCommand) InsertCommand;
259             }
260
261             set
262             {
263                 InsertCommand = (NpgsqlCommand) value;
264             }
265         }
266
267
268         public NpgsqlCommand InsertCommand
269         {
270             get
271             {
272                 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "InsertCommand");
273                 return _insertCommand;
274             }
275
276             set
277             {
278                 _insertCommand = value;
279             }
280         }
281
282
283     }
284 }
285
286
287 public class NpgsqlRowUpdatingEventArgs : RowUpdatingEventArgs
288 {
289     public NpgsqlRowUpdatingEventArgs (
290         DataRow dataRow,
291         IDbCommand command,
292         StatementType statementType,
293         DataTableMapping tableMapping
294     ) : base(dataRow, command, statementType, tableMapping)
295
296     {
297     }
298
299 }
300
301 public class NpgsqlRowUpdatedEventArgs : RowUpdatedEventArgs
302 {
303     public NpgsqlRowUpdatedEventArgs (
304         DataRow dataRow,
305         IDbCommand command,
306         StatementType statementType,
307         DataTableMapping tableMapping
308     ) : base(dataRow, command, statementType, tableMapping)
309
310     {
311     }
312
313 }