1 // created on 1/8/2002 at 23:02
3 // Npgsql.NpgsqlDataAdapter.cs
6 // Francisco Jr. (fxjrlists@yahoo.com.br)
8 // Copyright (C) 2002 The Npgsql Development Team
9 // npgsql-general@gborg.postgresql.org
10 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
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.
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.
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
29 using System
.Data
.Common
;
30 using System
.Resources
;
35 /// Represents the method that handles the <see cref="Npgsql.NpgsqlDataAdapter.RowUpdated">RowUpdated</see> events.
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 NpgsqlRowUpdatedEventHandler(Object sender
, NpgsqlRowUpdatedEventArgs e
);
42 /// Represents the method that handles the <see cref="Npgsql.NpgsqlDataAdapter.RowUpdating">RowUpdating</see> events.
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 NpgsqlRowUpdatingEventHandler(Object sender
, NpgsqlRowUpdatingEventArgs e
);
49 public sealed class NpgsqlDataAdapter
: DbDataAdapter
, IDbDataAdapter
52 private NpgsqlCommand _selectCommand
;
53 private NpgsqlCommand _updateCommand
;
54 private NpgsqlCommand _deleteCommand
;
55 private NpgsqlCommand _insertCommand
;
57 private NpgsqlCommandBuilder cmd_builder
;
60 private static readonly String CLASSNAME
= "NpgsqlDataAdapter";
63 public event NpgsqlRowUpdatedEventHandler RowUpdated
;
64 public event NpgsqlRowUpdatingEventHandler RowUpdating
;
66 public NpgsqlDataAdapter()
69 public NpgsqlDataAdapter(NpgsqlCommand selectCommand
)
71 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, CLASSNAME
);
72 _selectCommand
= selectCommand
;
73 cmd_builder
= new NpgsqlCommandBuilder(this);
76 public NpgsqlDataAdapter(String selectCommandText
, NpgsqlConnection selectConnection
) : this(new NpgsqlCommand(selectCommandText
, selectConnection
))
79 public NpgsqlDataAdapter(String selectCommandText
, String selectConnectionString
) : this(selectCommandText
, new NpgsqlConnection(selectConnectionString
))
83 protected override RowUpdatedEventArgs
CreateRowUpdatedEvent(
86 StatementType statementType
,
87 DataTableMapping tableMapping
90 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "CreateRowUpdatedEvent");
91 return new NpgsqlRowUpdatedEventArgs(dataRow
, command
, statementType
, tableMapping
);
97 protected override RowUpdatingEventArgs
CreateRowUpdatingEvent(
100 StatementType statementType
,
101 DataTableMapping tableMapping
104 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "CreateRowUpdatingEvent");
105 return new NpgsqlRowUpdatingEventArgs(dataRow
, command
, statementType
, tableMapping
);
108 protected override void OnRowUpdated(
109 RowUpdatedEventArgs
value
112 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "OnRowUpdated");
113 //base.OnRowUpdated(value);
114 if ((RowUpdated
!= null) && (value is NpgsqlRowUpdatedEventArgs
))
115 RowUpdated(this, (NpgsqlRowUpdatedEventArgs
) value);
119 protected override void OnRowUpdating(
120 RowUpdatingEventArgs
value
123 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "OnRowUpdating");
124 if ((RowUpdating
!= null) && (value is NpgsqlRowUpdatingEventArgs
))
125 RowUpdating(this, (NpgsqlRowUpdatingEventArgs
) value);
127 /*switch (value.StatementType)
129 case StatementType.Insert:
130 value.Command = cmd_builder.GetInsertCommand(value.Row);
132 case StatementType.Update:
133 value.Command = cmd_builder.GetUpdateCommand(value.Row);
135 case StatementType.Delete:
136 value.Command = cmd_builder.GetDeleteCommand(value.Row);
139 DataColumnMappingCollection columnMappings = value.TableMapping.ColumnMappings;
140 foreach (IDataParameter parameter in value.Command.Parameters)
143 string dsColumnName = parameter.SourceColumn;
144 if (columnMappings.Contains(parameter.SourceColumn))
146 DataColumnMapping mapping = columnMappings[parameter.SourceColumn];
149 dsColumnName = mapping.DataSetColumn;
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];
159 value.Row.AcceptChanges ();*/
163 ITableMappingCollection IDataAdapter
.TableMappings
167 return TableMappings
;
171 IDbCommand IDbDataAdapter
.DeleteCommand
175 NpgsqlEventLog
.LogPropertyGet(LogLevel
.Debug
, CLASSNAME
, "IDbDataAdapter.DeleteCommand");
176 return (NpgsqlCommand
) DeleteCommand
;
181 DeleteCommand
= (NpgsqlCommand
) value;
186 public NpgsqlCommand DeleteCommand
190 return _deleteCommand
;
195 _deleteCommand
= value;
199 IDbCommand IDbDataAdapter
.SelectCommand
203 return (NpgsqlCommand
) SelectCommand
;
208 SelectCommand
= (NpgsqlCommand
) value;
213 public NpgsqlCommand SelectCommand
217 return _selectCommand
;
222 _selectCommand
= value;
226 IDbCommand IDbDataAdapter
.UpdateCommand
230 NpgsqlEventLog
.LogPropertyGet(LogLevel
.Debug
, CLASSNAME
, "IDbDataAdapter.UpdateCommand");
231 return (NpgsqlCommand
) UpdateCommand
;
236 UpdateCommand
= (NpgsqlCommand
) value;
241 public NpgsqlCommand UpdateCommand
245 return _updateCommand
;
250 _updateCommand
= value;
254 IDbCommand IDbDataAdapter
.InsertCommand
258 return (NpgsqlCommand
) InsertCommand
;
263 InsertCommand
= (NpgsqlCommand
) value;
268 public NpgsqlCommand InsertCommand
272 NpgsqlEventLog
.LogPropertyGet(LogLevel
.Debug
, CLASSNAME
, "InsertCommand");
273 return _insertCommand
;
278 _insertCommand
= value;
287 public class NpgsqlRowUpdatingEventArgs
: RowUpdatingEventArgs
289 public NpgsqlRowUpdatingEventArgs (
292 StatementType statementType
,
293 DataTableMapping tableMapping
294 ) : base(dataRow
, command
, statementType
, tableMapping
)
300 public class NpgsqlRowUpdatedEventArgs
: RowUpdatedEventArgs
302 public NpgsqlRowUpdatedEventArgs (
305 StatementType statementType
,
306 DataTableMapping tableMapping
307 ) : base(dataRow
, command
, statementType
, tableMapping
)