2 // System.Web.UI.WebControls.SqlDataSourceView
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Sanjay Gupta (gsanjay@novell.com)
9 // (C) Novell, Inc. (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Collections
;
35 using System
.Collections
.Specialized
;
38 using System
.ComponentModel
;
39 using System
.Data
.SqlClient
;
41 namespace System
.Web
.UI
.WebControls
{
42 public class SqlDataSourceView
: DataSourceView
, IStateManager
{
45 SqlConnection connection
;
47 public SqlDataSourceView (SqlDataSource owner
, string name
)
51 connection
= new SqlConnection (owner
.ConnectionString
);
54 public int Delete (IDictionary keys
, IDictionary oldValues
)
56 return ExecuteDelete (keys
, oldValues
);
59 [MonoTODO ("Handle keys and oldValues and parameters")]
60 protected override int ExecuteDelete(IDictionary keys
, IDictionary oldValues
)
62 command
= new SqlCommand (this.DeleteCommand
, connection
);
64 int result
= command
.ExecuteNonQuery ();
69 public int Insert (IDictionary values
)
71 return Insert (values
);
74 [MonoTODO ("Handle values and parameters")]
75 protected override int ExecuteInsert (IDictionary values
)
77 command
= new SqlCommand (this.InsertCommand
, connection
);
79 int result
= command
.ExecuteNonQuery ();
84 public IEnumerable
Select (DataSourceSelectArguments arguments
)
86 return ExecuteSelect (arguments
);
89 [MonoTODO("Extra method to keep things compiling, need to remove later")]
90 public IEnumerable
Select()
92 throw new NotImplementedException ("Not required");
95 [MonoTODO ("Handle arguments")]
96 protected internal override IEnumerable
ExecuteSelect (
97 DataSourceSelectArguments arguments
)
99 command
= new SqlCommand (this.SelectCommand
, connection
);
101 SqlDataReader reader
= command
.ExecuteReader ();
102 //return reader.GetEnumerator ();
103 throw new NotImplementedException ("SqlDataReader doesnt implements GetEnumerator method yet");
106 public int Update(IDictionary keys
, IDictionary values
,
107 IDictionary oldValues
)
109 return ExecuteUpdate (keys
, values
, oldValues
);
112 [MonoTODO ("Handle keys, values and oldValues")]
113 protected override int ExecuteUpdate (IDictionary keys
,
114 IDictionary values
, IDictionary oldValues
)
116 command
= new SqlCommand (this.UpdateCommand
, connection
);
118 int result
= command
.ExecuteNonQuery();
124 void IStateManager
.LoadViewState (object savedState
)
126 LoadViewState (savedState
);
129 object IStateManager
.SaveViewState ()
131 return SaveViewState ();
134 void IStateManager
.TrackViewState ()
139 protected virtual void LoadViewState (object savedState
)
141 object [] vs
= savedState
as object [];
145 if (vs
[0] != null) ((IStateManager
) deleteParameters
).LoadViewState (vs
[0]);
146 if (vs
[1] != null) ((IStateManager
) filterParameters
).LoadViewState (vs
[1]);
147 if (vs
[2] != null) ((IStateManager
) insertParameters
).LoadViewState (vs
[2]);
148 if (vs
[3] != null) ((IStateManager
) selectParameters
).LoadViewState (vs
[3]);
149 if (vs
[4] != null) ((IStateManager
) updateParameters
).LoadViewState (vs
[4]);
150 if (vs
[5] != null) ((IStateManager
) viewState
).LoadViewState (vs
[5]);
153 protected virtual object SaveViewState ()
155 object [] vs
= new object [6];
157 if (deleteParameters
!= null) vs
[0] = ((IStateManager
) deleteParameters
).SaveViewState ();
158 if (filterParameters
!= null) vs
[1] = ((IStateManager
) filterParameters
).SaveViewState ();
159 if (insertParameters
!= null) vs
[2] = ((IStateManager
) insertParameters
).SaveViewState ();
160 if (selectParameters
!= null) vs
[3] = ((IStateManager
) selectParameters
).SaveViewState ();
161 if (updateParameters
!= null) vs
[4] = ((IStateManager
) updateParameters
).SaveViewState ();
162 if (viewState
!= null) vs
[5] = ((IStateManager
) viewState
).SaveViewState ();
164 foreach (object o
in vs
)
165 if (o
!= null) return vs
;
169 protected virtual void TrackViewState ()
173 if (deleteParameters
!= null) ((IStateManager
) deleteParameters
).TrackViewState ();
174 if (filterParameters
!= null) ((IStateManager
) filterParameters
).TrackViewState ();
175 if (insertParameters
!= null) ((IStateManager
) insertParameters
).TrackViewState ();
176 if (selectParameters
!= null) ((IStateManager
) selectParameters
).TrackViewState ();
177 if (updateParameters
!= null) ((IStateManager
) updateParameters
).TrackViewState ();
178 if (viewState
!= null) ((IStateManager
) viewState
).TrackViewState ();
181 protected bool IsTrackingViewState
{
182 get { return tracking; }
185 bool IStateManager
.IsTrackingViewState
{
186 get { return IsTrackingViewState; }
189 public string DeleteCommand
{
191 string val
= ViewState
["DeleteCommand"] as string;
192 return val
== null ? "" : val
;
194 set { ViewState ["DeleteCommand"] = value; }
197 public string FilterExpression
{
199 string val
= ViewState
["FilterExpression"] as string;
200 return val
== null ? "" : val
;
202 set { ViewState ["FilterExpression"] = value; }
205 public string InsertCommand
{
207 string val
= ViewState
["InsertCommand"] as string;
208 return val
== null ? "" : val
;
210 set { ViewState ["InsertCommand"] = value; }
213 public string SelectCommand
{
215 string val
= ViewState
["SelectCommand"] as string;
216 return val
== null ? "" : val
;
218 set { ViewState ["SelectCommand"] = value; }
221 public string UpdateCommand
{
223 string val
= ViewState
["UpdateCommand"] as string;
224 return val
== null ? "" : val
;
226 set { ViewState ["UpdateCommand"] = value; }
229 public string SortExpression
{
231 string val
= ViewState
["SortExpression"] as string;
232 return val
== null ? "" : val
;
234 set { ViewState ["SortExpression"] = value; }
237 public override bool CanDelete
{
238 get { return DeleteCommand != ""; }
241 public override bool CanInsert
{
242 get { return UpdateCommand != ""; }
245 public override bool CanSort
{
246 get { return owner.DataSourceMode == SqlDataSourceMode.DataSet; }
249 public override bool CanUpdate
{
250 get { return UpdateCommand != ""; }
253 EventHandlerList events
;
254 protected EventHandlerList Events
{
257 events
= new EventHandlerList ();
263 void ParametersChanged (object source
, EventArgs args
)
265 OnDataSourceViewChanged (EventArgs
.Empty
);
268 ParameterCollection
GetParameterCollection (ref ParameterCollection output
)
273 output
= new ParameterCollection ();
274 output
.ParametersChanged
+= new EventHandler (ParametersChanged
);
276 if (IsTrackingViewState
)
277 ((IStateManager
) output
).TrackViewState ();
282 public ParameterCollection DeleteParameters
{
283 get { return GetParameterCollection (ref deleteParameters); }
286 public ParameterCollection FilterParameters
{
287 get { return GetParameterCollection (ref filterParameters); }
290 public ParameterCollection InsertParameters
{
291 get { return GetParameterCollection (ref insertParameters); }
294 public ParameterCollection SelectParameters
{
295 get { return GetParameterCollection (ref selectParameters); }
298 public ParameterCollection UpdateParameters
{
299 get { return GetParameterCollection (ref updateParameters); }
303 public override string Name
{
307 protected virtual string ParameterPrefix
{
312 protected StateBag ViewState
{
314 if (viewState
!= null)
317 viewState
= new StateBag ();
318 if (IsTrackingViewState
)
319 viewState
.TrackViewState ();
325 ParameterCollection deleteParameters
;
326 ParameterCollection filterParameters
;
327 ParameterCollection insertParameters
;
328 ParameterCollection selectParameters
;
329 ParameterCollection updateParameters
;
336 static readonly object EventDataSourceViewChanged
= new object ();
338 protected virtual void OnDataSourceViewChanged (EventArgs e
)
340 if (events
== null) return;
341 EventHandler h
= events
[EventDataSourceViewChanged
] as EventHandler
;
346 public event EventHandler DataSourceViewChanged
{
347 add { Events.AddHandler (EventDataSourceViewChanged, value); }
348 remove { Events.RemoveHandler (EventDataSourceViewChanged, value); }
352 static readonly object EventDeleted
= new object ();
353 protected virtual void OnDeleted (SqlDataSourceStatusEventArgs e
)
355 if (events
== null) return;
356 SqlDataSourceStatusEventHandler h
= events
[EventDeleted
] as SqlDataSourceStatusEventHandler
;
361 public event SqlDataSourceStatusEventHandler Deleted
{
362 add { Events.AddHandler (EventDeleted, value); }
363 remove { Events.RemoveHandler (EventDeleted, value); }
366 static readonly object EventDeleting
= new object ();
367 protected virtual void OnDeleting (SqlDataSourceCommandEventArgs e
)
369 if (events
== null) return;
370 SqlDataSourceCommandEventHandler h
= events
[EventDeleting
] as SqlDataSourceCommandEventHandler
;
374 public event SqlDataSourceCommandEventHandler Deleting
{
375 add { Events.AddHandler (EventDeleting, value); }
376 remove { Events.RemoveHandler (EventDeleting, value); }
381 static readonly object EventInserted
= new object ();
382 protected virtual void OnInserted (SqlDataSourceStatusEventArgs e
)
384 if (events
== null) return;
385 SqlDataSourceStatusEventHandler h
= events
[EventInserted
] as SqlDataSourceStatusEventHandler
;
390 public event SqlDataSourceStatusEventHandler Inserted
{
391 add { Events.AddHandler (EventInserted, value); }
392 remove { Events.RemoveHandler (EventInserted, value); }
395 static readonly object EventInserting
= new object ();
396 protected virtual void OnInserting (SqlDataSourceCommandEventArgs e
)
398 if (events
== null) return;
399 SqlDataSourceCommandEventHandler h
= events
[EventInserting
] as SqlDataSourceCommandEventHandler
;
403 public event SqlDataSourceCommandEventHandler Inserting
{
404 add { Events.AddHandler (EventInserting, value); }
405 remove { Events.RemoveHandler (EventInserting, value); }
410 static readonly object EventSelected
= new object ();
411 protected virtual void OnSelected (SqlDataSourceStatusEventArgs e
)
413 if (events
== null) return;
414 SqlDataSourceStatusEventHandler h
= events
[EventSelected
] as SqlDataSourceStatusEventHandler
;
419 public event SqlDataSourceStatusEventHandler Selected
{
420 add { Events.AddHandler (EventSelected, value); }
421 remove { Events.RemoveHandler (EventSelected, value); }
424 static readonly object EventSelecting
= new object ();
425 protected virtual void OnSelecting (SqlDataSourceCommandEventArgs e
)
427 if (events
== null) return;
428 SqlDataSourceCommandEventHandler h
= events
[EventSelecting
] as SqlDataSourceCommandEventHandler
;
432 public event SqlDataSourceCommandEventHandler Selecting
{
433 add { Events.AddHandler (EventSelecting, value); }
434 remove { Events.RemoveHandler (EventSelecting, value); }
439 static readonly object EventUpdated
= new object ();
440 protected virtual void OnUpdated (SqlDataSourceStatusEventArgs e
)
442 if (events
== null) return;
443 SqlDataSourceStatusEventHandler h
= events
[EventUpdated
] as SqlDataSourceStatusEventHandler
;
448 public event SqlDataSourceStatusEventHandler Updated
{
449 add { Events.AddHandler (EventUpdated, value); }
450 remove { Events.RemoveHandler (EventUpdated, value); }
453 static readonly object EventUpdating
= new object ();
454 protected virtual void OnUpdating (SqlDataSourceCommandEventArgs e
)
456 if (events
== null) return;
457 SqlDataSourceCommandEventHandler h
= events
[EventUpdating
] as SqlDataSourceCommandEventHandler
;
461 public event SqlDataSourceCommandEventHandler Updating
{
462 add { Events.AddHandler (EventUpdating, value); }
463 remove { Events.RemoveHandler (EventUpdating, value); }