System.Data.SQLite
Check-in [5033bacee1]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Modularize various aspects of the virtual table handling in the example SQLiteVirtualTableCursorEnumerable class.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | virtualTables
Files: files | file ages | folders
SHA1: 5033bacee1d04e58b47a5d307fc54e337a33ea5c
User & Date: mistachkin 2013-06-21 06:06:27
Context
2013-06-21
06:27
Fix disposal semantics for classes derived from the SQLiteModuleBase class. Use the String.Format method consistently. Enable virtual table related native memory allocation tracking on the .NET Compact Framework. check-in: f9041807b3 user: mistachkin tags: virtualTables
06:06
Modularize various aspects of the virtual table handling in the example SQLiteVirtualTableCursorEnumerable class. check-in: 5033bacee1 user: mistachkin tags: virtualTables
05:39
Move the native memory allocation wrapper methods into the new SQLiteMemory class. check-in: c16bb56cfd user: mistachkin tags: virtualTables
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to System.Data.SQLite/SQLiteModuleBase.cs.

2312
2313
2314
2315
2316
2317
2318


















2319
2320
2321
2322
2323
2324
2325
                    SQLiteMemory.Free(pError);
                    pError = IntPtr.Zero;
                }
            }

            return success;
        }



















        ///////////////////////////////////////////////////////////////////////

        protected virtual bool SetCursorError(
            SQLiteVirtualTableCursor cursor,
            string error
            )







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
                    SQLiteMemory.Free(pError);
                    pError = IntPtr.Zero;
                }
            }

            return success;
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual bool SetTableError(
            SQLiteVirtualTable table,
            string error
            )
        {
            if (table == null)
                return false;

            IntPtr pVtab = table.NativeHandle;

            if (pVtab == IntPtr.Zero)
                return false;

            return SetTableError(pVtab, error);
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual bool SetCursorError(
            SQLiteVirtualTableCursor cursor,
            string error
            )

Changes to System.Data.SQLite/SQLiteModuleEnumerable.cs.

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
...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
...
292
293
294
295
296
297
298



299
300
301
302
303
304
305
...
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
...
333
334
335
336
337
338
339
340
341
342
343

344
345
346
347



348
349
350
351
352
353
354
...
361
362
363
364
365
366
367
368
369
370
371

372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
...
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
        ///////////////////////////////////////////////////////////////////////

        #region Private Data
        /// <summary>
        /// The <see cref="IEnumerable" /> instance containing the backing data
        /// for the virtual table.
        /// </summary>
        private IEnumerable collection;
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Public Constructors
        public SQLiteModuleEnumerable(
            string name,
            IEnumerable collection
            )
            : base(name)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");

            this.collection = collection;
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Protected Methods
        protected virtual SQLiteErrorCode CursorTypeMismatchError(
            SQLiteVirtualTableCursor cursor
            )
        {
            SetCursorError(cursor, "not an \"enumerable\" cursor");
            return SQLiteErrorCode.Error;
        }











































        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region ISQLiteManagedModule Members
        public override SQLiteErrorCode Create(
            SQLiteConnection connection,
................................................................................
            SQLiteIndex index
            )
        {
            CheckDisposed();

            if (!SetDefaultEstimatedCost(index))
            {
                SetTableError(table.NativeHandle,
                    "failed to set default estimated cost");

                return SQLiteErrorCode.Error;
            }

            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////
................................................................................
            SQLiteVirtualTable table,
            ref SQLiteVirtualTableCursor cursor
            )
        {
            CheckDisposed();

            cursor = new SQLiteVirtualTableCursorEnumerable(
                table, collection.GetEnumerator());

            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////

        public override SQLiteErrorCode Close(
................................................................................

            SQLiteVirtualTableCursorEnumerable enumerableCursor =
                cursor as SQLiteVirtualTableCursorEnumerable;

            if (enumerableCursor == null)
                return CursorTypeMismatchError(cursor);




            enumerableCursor.MoveNext(); /* IGNORED */
            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////

        public override bool Eof(
................................................................................
        {
            CheckDisposed();

            SQLiteVirtualTableCursorEnumerable enumerableCursor =
                cursor as SQLiteVirtualTableCursorEnumerable;

            if (enumerableCursor == null)
            {
                CursorTypeMismatchError(cursor);
                return true;
            }

            return enumerableCursor.EndOfEnumerator;
        }

        ///////////////////////////////////////////////////////////////////////

        public override SQLiteErrorCode Column(
................................................................................
            SQLiteVirtualTableCursorEnumerable enumerableCursor =
                cursor as SQLiteVirtualTableCursorEnumerable;

            if (enumerableCursor == null)
                return CursorTypeMismatchError(cursor);

            if (enumerableCursor.EndOfEnumerator)
            {
                context.SetNull();
                return SQLiteErrorCode.Ok;
            }


            object current = enumerableCursor.Current;

            context.SetString((current != null) ? current.ToString() : null);




            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////

        public override SQLiteErrorCode RowId(
................................................................................
            SQLiteVirtualTableCursorEnumerable enumerableCursor =
                cursor as SQLiteVirtualTableCursorEnumerable;

            if (enumerableCursor == null)
                return CursorTypeMismatchError(cursor);

            if (enumerableCursor.EndOfEnumerator)
            {
                SetCursorError(cursor, "already hit end of enumerable cursor");
                return SQLiteErrorCode.Error;
            }


            object current = enumerableCursor.Current;

            rowId = (current != null) ? current.GetHashCode() : 0;

            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////

        public override SQLiteErrorCode Update(
            SQLiteVirtualTable table,
            SQLiteValue[] values,
            ref long rowId
            )
        {
            CheckDisposed();

            SetTableError(table.NativeHandle, String.Format(
                "virtual table \"{0}\" is read-only", table.TableName));

            return SQLiteErrorCode.Error;
        }

        ///////////////////////////////////////////////////////////////////////

................................................................................
            string newName
            )
        {
            CheckDisposed();

            if (!table.Rename(newName))
            {
                SetTableError(table.NativeHandle, String.Format(
                    "failed to rename virtual table \"{0}\" to \"{1}\"",
                    table.TableName, newName));

                return SQLiteErrorCode.Error;
            }

            return SQLiteErrorCode.Ok;
        }







|







|



|
|

|













>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
|
<







 







|







 







>
>
>







 







<
|
<
<







 







<
<
<
<
>



|
>
>
>







 







<
<
<
<
>



|
<













|







 







|
|







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
...
231
232
233
234
235
236
237

238

239
240
241
242
243
244
245
...
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
...
352
353
354
355
356
357
358

359


360
361
362
363
364
365
366
...
374
375
376
377
378
379
380




381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
...
402
403
404
405
406
407
408




409
410
411
412
413

414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
...
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
        ///////////////////////////////////////////////////////////////////////

        #region Private Data
        /// <summary>
        /// The <see cref="IEnumerable" /> instance containing the backing data
        /// for the virtual table.
        /// </summary>
        private IEnumerable enumerable;
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Public Constructors
        public SQLiteModuleEnumerable(
            string name,
            IEnumerable enumerable
            )
            : base(name)
        {
            if (enumerable == null)
                throw new ArgumentNullException("enumerable");

            this.enumerable = enumerable;
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Protected Methods
        protected virtual SQLiteErrorCode CursorTypeMismatchError(
            SQLiteVirtualTableCursor cursor
            )
        {
            SetCursorError(cursor, "not an \"enumerable\" cursor");
            return SQLiteErrorCode.Error;
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual SQLiteErrorCode CursorEndOfEnumeratorError(
            SQLiteVirtualTableCursor cursor
            )
        {
            SetCursorError(cursor, "already hit end of enumerator");
            return SQLiteErrorCode.Error; 
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual string GetStringFromObject(
            object value
            )
        {
            if (value == null)
                return null;

            return value.ToString();
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual long GetRowIdFromObject(
            object value
            )
        {
            if (value == null)
                return 0;

            return value.GetHashCode();
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual bool CodeToEofResult(
            SQLiteErrorCode returnCode
            )
        {
            return (returnCode == SQLiteErrorCode.Ok) ? false : true;
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region ISQLiteManagedModule Members
        public override SQLiteErrorCode Create(
            SQLiteConnection connection,
................................................................................
            SQLiteIndex index
            )
        {
            CheckDisposed();

            if (!SetDefaultEstimatedCost(index))
            {

                SetTableError(table, "failed to set default estimated cost");

                return SQLiteErrorCode.Error;
            }

            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////
................................................................................
            SQLiteVirtualTable table,
            ref SQLiteVirtualTableCursor cursor
            )
        {
            CheckDisposed();

            cursor = new SQLiteVirtualTableCursorEnumerable(
                table, enumerable.GetEnumerator());

            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////

        public override SQLiteErrorCode Close(
................................................................................

            SQLiteVirtualTableCursorEnumerable enumerableCursor =
                cursor as SQLiteVirtualTableCursorEnumerable;

            if (enumerableCursor == null)
                return CursorTypeMismatchError(cursor);

            if (enumerableCursor.EndOfEnumerator)
                return CursorEndOfEnumeratorError(cursor);

            enumerableCursor.MoveNext(); /* IGNORED */
            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////

        public override bool Eof(
................................................................................
        {
            CheckDisposed();

            SQLiteVirtualTableCursorEnumerable enumerableCursor =
                cursor as SQLiteVirtualTableCursorEnumerable;

            if (enumerableCursor == null)

                return CodeToEofResult(CursorTypeMismatchError(cursor));



            return enumerableCursor.EndOfEnumerator;
        }

        ///////////////////////////////////////////////////////////////////////

        public override SQLiteErrorCode Column(
................................................................................
            SQLiteVirtualTableCursorEnumerable enumerableCursor =
                cursor as SQLiteVirtualTableCursorEnumerable;

            if (enumerableCursor == null)
                return CursorTypeMismatchError(cursor);

            if (enumerableCursor.EndOfEnumerator)




                return CursorEndOfEnumeratorError(cursor);

            object current = enumerableCursor.Current;

            if (current != null)
                context.SetString(GetStringFromObject(current));
            else
                context.SetNull();

            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////

        public override SQLiteErrorCode RowId(
................................................................................
            SQLiteVirtualTableCursorEnumerable enumerableCursor =
                cursor as SQLiteVirtualTableCursorEnumerable;

            if (enumerableCursor == null)
                return CursorTypeMismatchError(cursor);

            if (enumerableCursor.EndOfEnumerator)




                return CursorEndOfEnumeratorError(cursor);

            object current = enumerableCursor.Current;

            rowId = GetRowIdFromObject(current);

            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////

        public override SQLiteErrorCode Update(
            SQLiteVirtualTable table,
            SQLiteValue[] values,
            ref long rowId
            )
        {
            CheckDisposed();

            SetTableError(table, String.Format(
                "virtual table \"{0}\" is read-only", table.TableName));

            return SQLiteErrorCode.Error;
        }

        ///////////////////////////////////////////////////////////////////////

................................................................................
            string newName
            )
        {
            CheckDisposed();

            if (!table.Rename(newName))
            {
                SetTableError(table, String.Format(
                    "failed to rename virtual table from \"{0}\" to \"{1}\"",
                    table.TableName, newName));

                return SQLiteErrorCode.Error;
            }

            return SQLiteErrorCode.Ok;
        }