-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLiteDataAdapter.cs
executable file
·328 lines (290 loc) · 11.6 KB
/
SQLiteDataAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
namespace System.Data.SQLite
{
using System;
using System.Data.Common;
using System.ComponentModel;
/// <summary>
/// SQLite implementation of DbDataAdapter.
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[DefaultEvent("RowUpdated")]
[ToolboxItem("SQLite.Designer.SQLiteDataAdapterToolboxItem, SQLite.Designer, Version=" + SQLite3.DesignerVersion + ", Culture=neutral, PublicKeyToken=db937bc2d44ff139")]
[Designer("Microsoft.VSDesigner.Data.VS.SqlDataAdapterDesigner, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
#endif
public sealed class SQLiteDataAdapter : DbDataAdapter
{
private bool disposeSelect = true;
///////////////////////////////////////////////////////////////////////////////////////////////
private static object _updatingEventPH = new object();
private static object _updatedEventPH = new object();
///////////////////////////////////////////////////////////////////////////////////////////////
#region Public Constructors
/// <overloads>
/// This class is just a shell around the DbDataAdapter. Nothing from
/// DbDataAdapter is overridden here, just a few constructors are defined.
/// </overloads>
/// <summary>
/// Default constructor.
/// </summary>
public SQLiteDataAdapter()
{
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Constructs a data adapter using the specified select command.
/// </summary>
/// <param name="cmd">
/// The select command to associate with the adapter.
/// </param>
public SQLiteDataAdapter(SQLiteCommand cmd)
{
SelectCommand = cmd;
disposeSelect = false;
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Constructs a data adapter with the supplied select command text and
/// associated with the specified connection.
/// </summary>
/// <param name="commandText">
/// The select command text to associate with the data adapter.
/// </param>
/// <param name="connection">
/// The connection to associate with the select command.
/// </param>
public SQLiteDataAdapter(string commandText, SQLiteConnection connection)
{
SelectCommand = new SQLiteCommand(commandText, connection);
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Constructs a data adapter with the specified select command text,
/// and using the specified database connection string.
/// </summary>
/// <param name="commandText">
/// The select command text to use to construct a select command.
/// </param>
/// <param name="connectionString">
/// A connection string suitable for passing to a new SQLiteConnection,
/// which is associated with the select command.
/// </param>
public SQLiteDataAdapter(
string commandText,
string connectionString
)
: this(commandText, connectionString, false)
{
// do nothing.
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Constructs a data adapter with the specified select command text,
/// and using the specified database connection string.
/// </summary>
/// <param name="commandText">
/// The select command text to use to construct a select command.
/// </param>
/// <param name="connectionString">
/// A connection string suitable for passing to a new SQLiteConnection,
/// which is associated with the select command.
/// </param>
/// <param name="parseViaFramework">
/// Non-zero to parse the connection string using the built-in (i.e.
/// framework provided) parser when opening the connection.
/// </param>
public SQLiteDataAdapter(
string commandText,
string connectionString,
bool parseViaFramework
)
{
SQLiteConnection cnn = new SQLiteConnection(
connectionString, parseViaFramework);
SelectCommand = new SQLiteCommand(commandText, cnn);
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
#region IDisposable "Pattern" Members
private bool disposed;
private void CheckDisposed() /* throw */
{
#if THROW_ON_DISPOSED
if (disposed)
throw new ObjectDisposedException(typeof(SQLiteDataAdapter).Name);
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Cleans up resources (native and managed) associated with the current instance.
/// </summary>
/// <param name="disposing">
/// Zero when being disposed via garbage collection; otherwise, non-zero.
/// </param>
protected override void Dispose(bool disposing)
{
try
{
if (!disposed)
{
if (disposing)
{
////////////////////////////////////
// dispose managed resources here...
////////////////////////////////////
if (disposeSelect && (SelectCommand != null))
{
SelectCommand.Dispose();
SelectCommand = null;
}
if (InsertCommand != null)
{
InsertCommand.Dispose();
InsertCommand = null;
}
if (UpdateCommand != null)
{
UpdateCommand.Dispose();
UpdateCommand = null;
}
if (DeleteCommand != null)
{
DeleteCommand.Dispose();
DeleteCommand = null;
}
}
//////////////////////////////////////
// release unmanaged resources here...
//////////////////////////////////////
}
}
finally
{
base.Dispose(disposing);
//
// NOTE: Everything should be fully disposed at this point.
//
disposed = true;
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Row updating event handler
/// </summary>
public event EventHandler<RowUpdatingEventArgs> RowUpdating
{
add
{
CheckDisposed();
#if !PLATFORM_COMPACTFRAMEWORK
EventHandler<RowUpdatingEventArgs> previous = (EventHandler<RowUpdatingEventArgs>)base.Events[_updatingEventPH];
if ((previous != null) && (value.Target is DbCommandBuilder))
{
EventHandler<RowUpdatingEventArgs> handler = (EventHandler<RowUpdatingEventArgs>)FindBuilder(previous);
if (handler != null)
{
base.Events.RemoveHandler(_updatingEventPH, handler);
}
}
#endif
base.Events.AddHandler(_updatingEventPH, value);
}
remove { CheckDisposed(); base.Events.RemoveHandler(_updatingEventPH, value); }
}
#if !PLATFORM_COMPACTFRAMEWORK
internal static Delegate FindBuilder(MulticastDelegate mcd)
{
if (mcd != null)
{
Delegate[] invocationList = mcd.GetInvocationList();
for (int i = 0; i < invocationList.Length; i++)
{
if (invocationList[i].Target is DbCommandBuilder)
{
return invocationList[i];
}
}
}
return null;
}
#endif
/// <summary>
/// Row updated event handler
/// </summary>
public event EventHandler<RowUpdatedEventArgs> RowUpdated
{
add { CheckDisposed(); base.Events.AddHandler(_updatedEventPH, value); }
remove { CheckDisposed(); base.Events.RemoveHandler(_updatedEventPH, value); }
}
/// <summary>
/// Raised by the underlying DbDataAdapter when a row is being updated
/// </summary>
/// <param name="value">The event's specifics</param>
protected override void OnRowUpdating(RowUpdatingEventArgs value)
{
EventHandler<RowUpdatingEventArgs> handler = base.Events[_updatingEventPH] as EventHandler<RowUpdatingEventArgs>;
if (handler != null)
handler(this, value);
}
/// <summary>
/// Raised by DbDataAdapter after a row is updated
/// </summary>
/// <param name="value">The event's specifics</param>
protected override void OnRowUpdated(RowUpdatedEventArgs value)
{
EventHandler<RowUpdatedEventArgs> handler = base.Events[_updatedEventPH] as EventHandler<RowUpdatedEventArgs>;
if (handler != null)
handler(this, value);
}
/// <summary>
/// Gets/sets the select command for this DataAdapter
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[DefaultValue((string)null), Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
#endif
public new SQLiteCommand SelectCommand
{
get { CheckDisposed(); return (SQLiteCommand)base.SelectCommand; }
set { CheckDisposed(); base.SelectCommand = value; }
}
/// <summary>
/// Gets/sets the insert command for this DataAdapter
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[DefaultValue((string)null), Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
#endif
public new SQLiteCommand InsertCommand
{
get { CheckDisposed(); return (SQLiteCommand)base.InsertCommand; }
set { CheckDisposed(); base.InsertCommand = value; }
}
/// <summary>
/// Gets/sets the update command for this DataAdapter
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[DefaultValue((string)null), Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
#endif
public new SQLiteCommand UpdateCommand
{
get { CheckDisposed(); return (SQLiteCommand)base.UpdateCommand; }
set { CheckDisposed(); base.UpdateCommand = value; }
}
/// <summary>
/// Gets/sets the delete command for this DataAdapter
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[DefaultValue((string)null), Editor("Microsoft.VSDesigner.Data.Design.DBCommandEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
#endif
public new SQLiteCommand DeleteCommand
{
get { CheckDisposed(); return (SQLiteCommand)base.DeleteCommand; }
set { CheckDisposed(); base.DeleteCommand = value; }
}
}
}