-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteModuleCommon.cs
More file actions
executable file
·286 lines (256 loc) · 9.53 KB
/
Copy pathSQLiteModuleCommon.cs
File metadata and controls
executable file
·286 lines (256 loc) · 9.53 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
/********************************************************
* 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!
********************************************************/
using System.Globalization;
#region Non-Generic Classes
namespace System.Data.SQLite
{
#region SQLiteModuleCommon Class
/// <summary>
/// This class contains some virtual methods that may be useful for other
/// virtual table classes. It specifically does NOT implement any of the
/// <see cref="ISQLiteManagedModule" /> interface methods.
/// </summary>
public class SQLiteModuleCommon : SQLiteModuleNoop /* NOT SEALED */
{
#region Private Constants
/// <summary>
/// The CREATE TABLE statement used to declare the schema for the
/// virtual table.
/// </summary>
private static readonly string declareSql =
HelperMethods.StringFormat(
CultureInfo.InvariantCulture, "CREATE TABLE {0}(x);",
typeof(SQLiteModuleCommon).Name);
#endregion
///////////////////////////////////////////////////////////////////////
#region Private Data
/// <summary>
/// Non-zero if different object instances with the same value should
/// generate different row identifiers, where applicable. This has no
/// effect on the .NET Compact Framework.
/// </summary>
private bool objectIdentity;
#endregion
///////////////////////////////////////////////////////////////////////
#region Public Constructors
/// <summary>
/// Constructs an instance of this class.
/// </summary>
/// <param name="name">
/// The name of the module. This parameter cannot be null.
/// </param>
public SQLiteModuleCommon(
string name
)
: this(name, false)
{
// do nothing.
}
///////////////////////////////////////////////////////////////////////
/// <summary>
/// Constructs an instance of this class.
/// </summary>
/// <param name="name">
/// The name of the module. This parameter cannot be null.
/// </param>
/// <param name="objectIdentity">
/// Non-zero if different object instances with the same value should
/// generate different row identifiers, where applicable. This
/// parameter has no effect on the .NET Compact Framework.
/// </param>
public SQLiteModuleCommon(
string name,
bool objectIdentity
)
: base(name)
{
this.objectIdentity = objectIdentity;
}
#endregion
///////////////////////////////////////////////////////////////////////
#region Protected Methods
/// <summary>
/// Determines the SQL statement used to declare the virtual table.
/// This method should be overridden in derived classes if they require
/// a custom virtual table schema.
/// </summary>
/// <returns>
/// The SQL statement used to declare the virtual table -OR- null if it
/// cannot be determined.
/// </returns>
protected virtual string GetSqlForDeclareTable()
{
return declareSql;
}
///////////////////////////////////////////////////////////////////////
/// <summary>
/// Sets the table error message to one that indicates the virtual
/// table cursor is of the wrong type.
/// </summary>
/// <param name="cursor">
/// The <see cref="SQLiteVirtualTableCursor" /> object instance.
/// </param>
/// <param name="type">
/// The <see cref="Type" /> that the virtual table cursor should be.
/// </param>
/// <returns>
/// The value of <see cref="SQLiteErrorCode.Error" />.
/// </returns>
protected virtual SQLiteErrorCode CursorTypeMismatchError(
SQLiteVirtualTableCursor cursor,
Type type
)
{
if (type != null)
{
SetCursorError(cursor, HelperMethods.StringFormat(
CultureInfo.CurrentCulture, "not a \"{0}\" cursor",
type));
}
else
{
SetCursorError(cursor, "cursor type mismatch");
}
return SQLiteErrorCode.Error;
}
///////////////////////////////////////////////////////////////////////
/// <summary>
/// Determines the string to return as the column value for the object
/// instance value.
/// </summary>
/// <param name="cursor">
/// The <see cref="SQLiteVirtualTableCursor" /> object instance
/// associated with the previously opened virtual table cursor to be
/// used.
/// </param>
/// <param name="value">
/// The object instance to return a string representation for.
/// </param>
/// <returns>
/// The string representation of the specified object instance or null
/// upon failure.
/// </returns>
protected virtual string GetStringFromObject(
SQLiteVirtualTableCursor cursor,
object value
)
{
if (value == null)
return null;
if (value is string)
return (string)value;
return value.ToString();
}
///////////////////////////////////////////////////////////////////////
/// <summary>
/// Constructs an <see cref="Int64" /> unique row identifier from two
/// <see cref="Int32" /> values. The first <see cref="Int32" /> value
/// must contain the row sequence number for the current row and the
/// second value must contain the hash code of the key column value
/// for the current row.
/// </summary>
/// <param name="rowIndex">
/// The integer row sequence number for the current row.
/// </param>
/// <param name="hashCode">
/// The hash code of the key column value for the current row.
/// </param>
/// <returns>
/// The unique row identifier or zero upon failure.
/// </returns>
protected virtual long MakeRowId(
int rowIndex,
int hashCode
)
{
long result = rowIndex;
result <<= 32; /* typeof(int) bits */
result |= (long)(uint)hashCode;
return result;
}
///////////////////////////////////////////////////////////////////////
/// <summary>
/// Determines the unique row identifier for the current row.
/// </summary>
/// <param name="cursor">
/// The <see cref="SQLiteVirtualTableCursor" /> object instance
/// associated with the previously opened virtual table cursor to be
/// used.
/// </param>
/// <param name="value">
/// The object instance to return a unique row identifier for.
/// </param>
/// <returns>
/// The unique row identifier or zero upon failure.
/// </returns>
protected virtual long GetRowIdFromObject(
SQLiteVirtualTableCursor cursor,
object value
)
{
int rowIndex = (cursor != null) ? cursor.GetRowIndex() : 0;
int hashCode = SQLiteMarshal.GetHashCode(value, objectIdentity);
return MakeRowId(rowIndex, hashCode);
}
#endregion
///////////////////////////////////////////////////////////////////////
#region IDisposable "Pattern" Members
private bool disposed;
/// <summary>
/// Throws an <see cref="ObjectDisposedException" /> if this object
/// instance has been disposed.
/// </summary>
private void CheckDisposed() /* throw */
{
#if THROW_ON_DISPOSED
if (disposed)
{
throw new ObjectDisposedException(
typeof(SQLiteModuleCommon).Name);
}
#endif
}
///////////////////////////////////////////////////////////////////////
/// <summary>
/// Disposes of this object instance.
/// </summary>
/// <param name="disposing">
/// Non-zero if this method is being called from the
/// <see cref="IDisposable.Dispose" /> method. Zero if this method is
/// being called from the finalizer.
/// </param>
protected override void Dispose(bool disposing)
{
try
{
if (!disposed)
{
//if (disposing)
//{
// ////////////////////////////////////
// // dispose managed resources here...
// ////////////////////////////////////
//}
//////////////////////////////////////
// release unmanaged resources here...
//////////////////////////////////////
}
}
finally
{
base.Dispose(disposing);
//
// NOTE: Everything should be fully disposed at this point.
//
disposed = true;
}
}
#endregion
}
#endregion
}
#endregion