2002-10-31 Tim Coleman (tim@timcoleman.com)
[mono.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsClient / TdsDataAdapter.cs
1 //
2 // Mono.Data.TdsClient.TdsDataAdapter.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
8 //
9
10 using System;
11 using System.ComponentModel;
12 using System.Data;
13 using System.Data.Common;
14
15 namespace Mono.Data.TdsClient
16 {
17         /// <summary>
18         /// Represents a set of command-related properties that are used 
19         /// to fill the DataSet and update a data source, all this 
20         /// from a SQL database.
21         /// </summary>
22         public sealed class TdsDataAdapter : DbDataAdapter, IDbDataAdapter 
23         {
24                 #region Fields
25         
26                 TdsCommand deleteCommand;
27                 TdsCommand insertCommand;
28                 TdsCommand selectCommand;
29                 TdsCommand updateCommand;
30
31                 static readonly object EventRowUpdated = new object(); 
32                 static readonly object EventRowUpdating = new object(); 
33
34                 #endregion
35
36                 #region Constructors
37                 
38                 public TdsDataAdapter ()        
39                         : this (new TdsCommand ())
40                 {
41                 }
42
43                 public TdsDataAdapter (TdsCommand selectCommand) 
44                 {
45                         DeleteCommand = new TdsCommand ();
46                         InsertCommand = new TdsCommand ();
47                         SelectCommand = selectCommand;
48                         UpdateCommand = new TdsCommand ();
49                 }
50
51                 public TdsDataAdapter (string selectCommandText, TdsConnection selectConnection) 
52                         : this (new TdsCommand (selectCommandText, selectConnection))
53                 { 
54                 }
55
56                 public TdsDataAdapter (string selectCommandText, string selectConnectionString)
57                         : this (selectCommandText, new TdsConnection (selectConnectionString))
58                 {
59                 }
60
61                 #endregion
62
63                 #region Properties
64
65                 public TdsCommand DeleteCommand {
66                         get { return deleteCommand; }
67                         set { deleteCommand = value; }
68                 }
69
70                 public TdsCommand InsertCommand {
71                         get { return insertCommand; }
72                         set { insertCommand = value; }
73                 }
74
75                 public TdsCommand SelectCommand {
76                         get { return selectCommand; }
77                         set { selectCommand = value; }
78                 }
79
80                 public TdsCommand UpdateCommand {
81                         get { return updateCommand; }
82                         set { updateCommand = value; }
83                 }
84
85                 IDbCommand IDbDataAdapter.DeleteCommand {
86                         get { return DeleteCommand; }
87                         set { 
88                                 if (!(value is TdsCommand)) 
89                                         throw new ArgumentException ();
90                                 DeleteCommand = (TdsCommand)value;
91                         }
92                 }
93
94                 IDbCommand IDbDataAdapter.InsertCommand {
95                         get { return InsertCommand; }
96                         set { 
97                                 if (!(value is TdsCommand)) 
98                                         throw new ArgumentException ();
99                                 InsertCommand = (TdsCommand)value;
100                         }
101                 }
102
103                 IDbCommand IDbDataAdapter.SelectCommand {
104                         get { return SelectCommand; }
105                         set { 
106                                 if (!(value is TdsCommand)) 
107                                         throw new ArgumentException ();
108                                 SelectCommand = (TdsCommand)value;
109                         }
110                 }
111
112                 IDbCommand IDbDataAdapter.UpdateCommand {
113                         get { return UpdateCommand; }
114                         set { 
115                                 if (!(value is TdsCommand)) 
116                                         throw new ArgumentException ();
117                                 UpdateCommand = (TdsCommand)value;
118                         }
119                 }
120
121
122                 ITableMappingCollection IDataAdapter.TableMappings {
123                         get { return TableMappings; }
124                 }
125
126                 #endregion // Properties
127
128                 #region Methods
129
130                 protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) 
131                 {
132                         return new TdsRowUpdatedEventArgs (dataRow, command, statementType, tableMapping);
133                 }
134
135
136                 protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) 
137                 {
138                         return new TdsRowUpdatingEventArgs (dataRow, command, statementType, tableMapping);
139                 }
140
141                 protected override void OnRowUpdated (RowUpdatedEventArgs value) 
142                 {
143                         TdsRowUpdatedEventHandler handler = (TdsRowUpdatedEventHandler) Events[EventRowUpdated];
144                         if ((handler != null) && (value is TdsRowUpdatedEventArgs))
145                                 handler (this, (TdsRowUpdatedEventArgs) value);
146                 }
147
148                 protected override void OnRowUpdating (RowUpdatingEventArgs value) 
149                 {
150                         TdsRowUpdatingEventHandler handler = (TdsRowUpdatingEventHandler) Events[EventRowUpdating];
151                         if ((handler != null) && (value is TdsRowUpdatingEventArgs))
152                                 handler (this, (TdsRowUpdatingEventArgs) value);
153                 }
154
155                 #endregion // Methods
156
157                 #region Events and Delegates
158
159                 public event TdsRowUpdatedEventHandler RowUpdated {
160                         add { Events.AddHandler (EventRowUpdated, value); }
161                         remove { Events.RemoveHandler (EventRowUpdated, value); }
162                 }
163
164                 public event TdsRowUpdatingEventHandler RowUpdating {
165                         add { Events.AddHandler (EventRowUpdating, value); }
166                         remove { Events.RemoveHandler (EventRowUpdating, value); }
167                 }
168
169                 #endregion // Events and Delegates
170
171         }
172 }