-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteBlob.cs
More file actions
executable file
·368 lines (311 loc) · 12 KB
/
Copy pathSQLiteBlob.cs
File metadata and controls
executable file
·368 lines (311 loc) · 12 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Joe Mistachkin (joe@mistachkin.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
namespace System.Data.SQLite
{
using System;
/// <summary>
/// Represents a single SQL blob in SQLite.
/// </summary>
public sealed class SQLiteBlob : IDisposable
{
#region Private Data
/// <summary>
/// The underlying SQLite object this blob is bound to.
/// </summary>
internal SQLiteBase _sql;
/// <summary>
/// The actual blob handle.
/// </summary>
internal SQLiteBlobHandle _sqlite_blob;
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
#region Private Constructors
/// <summary>
/// Initializes the blob.
/// </summary>
/// <param name="sqlbase">The base SQLite object.</param>
/// <param name="blob">The blob handle.</param>
private SQLiteBlob(
SQLiteBase sqlbase,
SQLiteBlobHandle blob
)
{
_sql = sqlbase;
_sqlite_blob = blob;
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
#region Static "Factory" Methods
/// <summary>
/// Creates a <see cref="SQLiteBlob" /> object. This will not work
/// for tables that were created WITHOUT ROWID -OR- if the query
/// does not include the "rowid" column or one of its aliases -OR-
/// if the <see cref="SQLiteDataReader" /> was not created with the
/// <see cref="CommandBehavior.KeyInfo" /> flag.
/// </summary>
/// <param name="dataReader">
/// The <see cref="SQLiteDataReader" /> instance with a result set
/// containing the desired blob column.
/// </param>
/// <param name="i">
/// The index of the blob column.
/// </param>
/// <param name="readOnly">
/// Non-zero to open the blob object for read-only access.
/// </param>
/// <returns>
/// The newly created <see cref="SQLiteBlob" /> instance -OR- null
/// if an error occurs.
/// </returns>
public static SQLiteBlob Create(
SQLiteDataReader dataReader,
int i,
bool readOnly
)
{
SQLiteConnection connection = SQLiteDataReader.GetConnection(
dataReader);
if (connection == null)
throw new InvalidOperationException("Connection not available");
SQLite3 sqlite3 = connection._sql as SQLite3;
if (sqlite3 == null)
throw new InvalidOperationException("Connection has no wrapper");
SQLiteConnectionHandle handle = sqlite3._sql;
if (handle == null)
throw new InvalidOperationException("Connection has an invalid handle.");
long? rowId = dataReader.GetRowId(i);
if (rowId == null)
throw new InvalidOperationException("No RowId is available");
SQLiteBlobHandle blob = null;
try
{
// do nothing.
}
finally /* NOTE: Thread.Abort() protection. */
{
IntPtr ptrBlob = IntPtr.Zero;
SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3_blob_open(
sqlite3._sql, SQLiteConvert.ToUTF8(
dataReader.GetDatabaseName(i)), SQLiteConvert.ToUTF8(
dataReader.GetTableName(i)), SQLiteConvert.ToUTF8(
dataReader.GetName(i)), (long)rowId, readOnly ? 0 : 1,
ref ptrBlob);
if (rc != SQLiteErrorCode.Ok)
throw new SQLiteException(rc, null);
blob = new SQLiteBlobHandle(handle, ptrBlob);
}
SQLiteConnection.OnChanged(null, new ConnectionEventArgs(
SQLiteConnectionEventType.NewCriticalHandle, null,
null, null, dataReader, blob, null, new object[] {
typeof(SQLiteBlob), dataReader, i, readOnly }));
return new SQLiteBlob(sqlite3, blob);
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
#region Private Methods
/// <summary>
/// Throws an exception if the blob object does not appear to be open.
/// </summary>
private void CheckOpen()
{
if (_sqlite_blob == IntPtr.Zero)
throw new InvalidOperationException("Blob is not open");
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Throws an exception if an invalid read/write parameter is detected.
/// </summary>
/// <param name="buffer">
/// When reading, this array will be populated with the bytes read from
/// the underlying database blob. When writing, this array contains new
/// values for the specified portion of the underlying database blob.
/// </param>
/// <param name="count">
/// The number of bytes to read or write.
/// </param>
/// <param name="offset">
/// The byte offset, relative to the start of the underlying database
/// blob, where the read or write operation will begin.
/// </param>
private void VerifyParameters(
byte[] buffer,
int count,
int offset
)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
if (offset < 0)
throw new ArgumentException("Negative offset not allowed.");
if (count < 0)
throw new ArgumentException("Negative count not allowed.");
if (count > buffer.Length)
throw new ArgumentException("Buffer is too small.");
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
#region Public Methods
/// <summary>
/// Retargets this object to an underlying database blob for a
/// different row; the database, table, and column remain exactly
/// the same. If this operation fails for any reason, this blob
/// object is automatically disposed.
/// </summary>
/// <param name="rowId">
/// The integer identifier for the new row.
/// </param>
public void Reopen(
long rowId
)
{
CheckDisposed();
CheckOpen();
SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3_blob_reopen(
_sqlite_blob, rowId);
if (rc != SQLiteErrorCode.Ok)
{
Dispose();
throw new SQLiteException(rc, null);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Queries the total number of bytes for the underlying database blob.
/// </summary>
/// <returns>
/// The total number of bytes for the underlying database blob.
/// </returns>
public int GetCount()
{
CheckDisposed();
CheckOpen();
return UnsafeNativeMethods.sqlite3_blob_bytes(_sqlite_blob);
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Reads data from the underlying database blob.
/// </summary>
/// <param name="buffer">
/// This array will be populated with the bytes read from the
/// underlying database blob.
/// </param>
/// <param name="count">
/// The number of bytes to read.
/// </param>
/// <param name="offset">
/// The byte offset, relative to the start of the underlying
/// database blob, where the read operation will begin.
/// </param>
public void Read(
byte[] buffer,
int count,
int offset
)
{
CheckDisposed();
CheckOpen();
VerifyParameters(buffer, count, offset);
SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3_blob_read(
_sqlite_blob, buffer, count, offset);
if (rc != SQLiteErrorCode.Ok)
throw new SQLiteException(rc, null);
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Writes data into the underlying database blob.
/// </summary>
/// <param name="buffer">
/// This array contains the new values for the specified portion of
/// the underlying database blob.
/// </param>
/// <param name="count">
/// The number of bytes to write.
/// </param>
/// <param name="offset">
/// The byte offset, relative to the start of the underlying
/// database blob, where the write operation will begin.
/// </param>
public void Write(
byte[] buffer,
int count,
int offset
)
{
CheckDisposed();
CheckOpen();
VerifyParameters(buffer, count, offset);
SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3_blob_write(
_sqlite_blob, buffer, count, offset);
if (rc != SQLiteErrorCode.Ok)
throw new SQLiteException(rc, null);
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Closes the blob, freeing the associated resources.
/// </summary>
public void Close()
{
Dispose();
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
#region IDisposable Members
/// <summary>
/// Disposes and finalizes the blob.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
#region IDisposable "Pattern" Members
private bool disposed;
private void CheckDisposed() /* throw */
{
#if THROW_ON_DISPOSED
if (disposed)
throw new ObjectDisposedException(typeof(SQLiteBlob).Name);
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////
private void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
////////////////////////////////////
// dispose managed resources here...
////////////////////////////////////
if (_sqlite_blob != null)
{
_sqlite_blob.Dispose();
_sqlite_blob = null;
}
_sql = null;
}
//////////////////////////////////////
// release unmanaged resources here...
//////////////////////////////////////
disposed = true;
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
#region Destructor
/// <summary>
/// The destructor.
/// </summary>
~SQLiteBlob()
{
Dispose(false);
}
#endregion
}
}