System.Data.SQLite
Check-in [ea5335378e]
Not logged in

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

Overview
Comment:More work on input/output marshalling for the sqlite3_index_info structure. Also, refactoring to allow better portability to the .NET Compact Framework.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | virtualTables
Files: files | file ages | folders
SHA1: ea5335378e162d63a312cbb1166e8754d85a33cb
User & Date: mistachkin 2013-06-18 06:07:22
Context
2013-06-18
06:12
Fix compilation errors for the .NET Compact Framework build. check-in: e64388aacd user: mistachkin tags: virtualTables
06:07
More work on input/output marshalling for the sqlite3_index_info structure. Also, refactoring to allow better portability to the .NET Compact Framework. check-in: ea5335378e user: mistachkin tags: virtualTables
2013-06-15
02:46
More work in progress. check-in: 8a682d20dc user: mistachkin tags: virtualTables
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

422
423
424
425
426
427
428
429
430

431
432
433
434
435
436

437
438
439

440
441
442
443
444








445
446
447
448
449
450
451
...
546
547
548
549
550
551
552







































































































553
554
555
556
557
558
559
...
581
582
583
584
585
586
587






















588
589
590
591
592
593
594
...
682
683
684
685
686
687
688



689
690
691
692
693
694
695
...
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
...
758
759
760
761
762
763
764
765
766

767
768
769
770
771
772



















773
774
775
776
777
778
779
...
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
....
1053
1054
1055
1056
1057
1058
1059





1060
1061
1062
1063
1064
1065
1066
        {
            get { return outputs; }
        }
    }

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

    public class SQLiteVirtualTableCursor
    {

        public SQLiteVirtualTableCursor(
            IntPtr nativeHandle
            )
        {
            this.nativeHandle = nativeHandle;
        }


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


        private IntPtr nativeHandle;
        public IntPtr NativeHandle
        {
            get { return nativeHandle; }
        }








    }

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

    public interface ISQLiteNativeModule
    {
        // https://www.sqlite.org/vtab.html
................................................................................
            return module;
        }

        private static int ThirtyBits = 0x3fffffff;

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








































































































        internal static byte[] GetUtf8BytesFromString(string value)
        {
            if (value == null)
                return null;

            return Utf8Encoding.GetBytes(value);
        }
................................................................................

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

        private static void Free(IntPtr pMemory)
        {
            UnsafeNativeMethods.sqlite3_free(pMemory);
        }























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

        internal static byte[] BytesFromIntPtr(IntPtr pValue, int length)
        {
            if (pValue == IntPtr.Zero)
                return null;
................................................................................

            if (bytes == null)
                return IntPtr.Zero;

            int length = bytes.Length;

            result = Allocate(length + 1);




            Marshal.Copy(bytes, 0, result, length);
            Marshal.WriteByte(result, length, 0);

            return result;
        }

................................................................................

            SQLiteValue[] result = new SQLiteValue[nData];

            for (int index = 0, offset = 0;
                    index < result.Length;
                    index++, offset += IntPtr.Size)
            {
#if !PLATFORM_COMPACTFRAMEWORK
                IntPtr pData = Marshal.ReadIntPtr(apData, offset);
#else
                IntPtr pData = Marshal.ReadIntPtr(IntPtrForOffset(
                    apData, offset));
#endif

                result[index] = (pData != IntPtr.Zero) ?
                    new SQLiteValue(pData) : null;
            }

            return result;
        }
................................................................................
                result[index] = new SQLiteValue(values[index]);

            return result;
        }

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

        private static UnsafeNativeMethods.sqlite3_index_info IndexFromIntPtr(
            IntPtr pIndex

            )
        {
            if (pIndex == IntPtr.Zero)
                return new UnsafeNativeMethods.sqlite3_index_info();

            return new UnsafeNativeMethods.sqlite3_index_info();



















        }

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

        private static void IndexOutputsToIntPtr(
            UnsafeNativeMethods.sqlite3_index_info index,
            SQLiteIndexOutputs indexOutputs
................................................................................
            }

            return sqliteBase.DeclareVirtualTable(this, sql, ref error);
        }

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

#if PLATFORM_COMPACTFRAMEWORK
        protected virtual IntPtr IntPtrForOffset(
            IntPtr pointer,
            int offset
            )
        {
            return new IntPtr(pointer.ToInt64() + offset);
        }
#endif

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

        protected virtual bool SetTableError(
            IntPtr pVtab,
            string error
            )
        {
            if (pVtab == IntPtr.Zero)
                return false;

            int offset = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_module)) + sizeof(int);

#if !PLATFORM_COMPACTFRAMEWORK
            IntPtr pError = Marshal.ReadIntPtr(pVtab, offset);
#else
            IntPtr pError = Marshal.ReadIntPtr(IntPtrForOffset(pVtab, offset));
#endif

            if (pError != IntPtr.Zero)
            {
                Free(pError); pError = IntPtr.Zero;

#if !PLATFORM_COMPACTFRAMEWORK
                Marshal.WriteIntPtr(pVtab, offset, pError);
#else
                Marshal.WriteIntPtr(IntPtrForOffset(pVtab, offset), pError);
#endif
            }

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteIntPtr(pVtab, offset, Utf8IntPtrFromString(error));
#else
            Marshal.WriteIntPtr(IntPtrForOffset(pVtab, offset),
                Utf8IntPtrFromString(error));
#endif

            return true;
        }
        #endregion

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

        #region ISQLiteNativeModule Members
................................................................................
        public SQLiteErrorCode xBestIndex(
            IntPtr pVtab,
            IntPtr pIndex
            )
        {
            try
            {





                if (BestIndex(null) == SQLiteErrorCode.Ok)
                {
                    return SQLiteErrorCode.Ok;
                }
            }
            catch (Exception e) /* NOTE: Must catch ALL. */
            {







|

>






>



>





>
>
>
>
>
>
>
>







 







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







 







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







 







>
>
>







 







<
|
<
<
<
<







 







|
|
>



|

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<











<
|
<
<
<




<
<
|
<
<
<


<
|
<
<
<
<
<







 







>
>
>
>
>







422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
...
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
...
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
...
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
...
864
865
866
867
868
869
870

871




872
873
874
875
876
877
878
...
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
....
1068
1069
1070
1071
1072
1073
1074












1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085

1086



1087
1088
1089
1090


1091



1092
1093

1094





1095
1096
1097
1098
1099
1100
1101
....
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
        {
            get { return outputs; }
        }
    }

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

    public class SQLiteVirtualTableCursor : ISQLiteNativeHandle
    {
        #region Public Constructors
        public SQLiteVirtualTableCursor(
            IntPtr nativeHandle
            )
        {
            this.nativeHandle = nativeHandle;
        }
        #endregion

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

        #region ISQLiteNativeHandle Members
        private IntPtr nativeHandle;
        public IntPtr NativeHandle
        {
            get { return nativeHandle; }
        }
        #endregion
    }

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

    public interface ISQLiteNativeHandle
    {
        IntPtr NativeHandle { get; }
    }

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

    public interface ISQLiteNativeModule
    {
        // https://www.sqlite.org/vtab.html
................................................................................
            return module;
        }

        private static int ThirtyBits = 0x3fffffff;

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

#if PLATFORM_COMPACTFRAMEWORK
        protected virtual IntPtr IntPtrForOffset(
            IntPtr pointer,
            int offset
            )
        {
            return new IntPtr(pointer.ToInt64() + offset);
        }
#endif

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

        private static int MarshalReadInt32(
            IntPtr pointer,
            int offset
            )
        {
#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadInt32(pointer, offset);
#else
            return Marshal.ReadInt32(IntPtrForOffset(pointer, offset));
#endif
        }

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

        private static double MarshalReadDouble(
            IntPtr pointer,
            int offset
            )
        {
#if !PLATFORM_COMPACTFRAMEWORK
            return BitConverter.Int64BitsToDouble(Marshal.ReadInt64(
                pointer, offset));
#else
            return BitConverter.ToDouble(BitConverter.GetBytes(
                Marshal.ReadInt64(IntPtrForOffset(pointer, offset)), 0);
#endif
        }

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

        private static IntPtr MarshalReadIntPtr(
            IntPtr pointer,
            int offset
            )
        {
#if !PLATFORM_COMPACTFRAMEWORK
            return Marshal.ReadIntPtr(pointer, offset);
#else
            return Marshal.ReadIntPtr(IntPtrForOffset(pointer, offset));
#endif
        }

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

        private static void MarshalWriteInt32(
            IntPtr pointer,
            int offset,
            int value
            )
        {
#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteInt32(pointer, offset, value);
#else
            Marshal.WriteInt32(IntPtrForOffset(pointer, offset), value);
#endif
        }

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

        private static void MarshalWriteDouble(
            IntPtr pointer,
            int offset,
            double value
            )
        {
#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteInt64(pointer, offset,
                BitConverter.DoubleToInt64Bits(value));
#else
            Marshal.WriteInt64(IntPtrForOffset(pointer, offset),
                BitConverter.ToInt64(BitConverter.GetBytes(value)));
#endif
        }

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

        private static void MarshalWriteIntPtr(
            IntPtr pointer,
            int offset,
            IntPtr value
            )
        {
#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteIntPtr(pointer, offset, value);
#else
            Marshal.WriteIntPtr(IntPtrForOffset(pointer, offset), value);
#endif
        }

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

        internal static byte[] GetUtf8BytesFromString(string value)
        {
            if (value == null)
                return null;

            return Utf8Encoding.GetBytes(value);
        }
................................................................................

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

        private static void Free(IntPtr pMemory)
        {
            UnsafeNativeMethods.sqlite3_free(pMemory);
        }

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

        private static IntPtr BytesToIntPtr(byte[] value)
        {
            if (value == null)
                return IntPtr.Zero;

            int length = value.Length;

            if (length == 0)
                return IntPtr.Zero;

            IntPtr result = Allocate(length);

            if (result == IntPtr.Zero)
                return IntPtr.Zero;

            Marshal.Copy(value, 0, result, length);

            return result;
        }

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

        internal static byte[] BytesFromIntPtr(IntPtr pValue, int length)
        {
            if (pValue == IntPtr.Zero)
                return null;
................................................................................

            if (bytes == null)
                return IntPtr.Zero;

            int length = bytes.Length;

            result = Allocate(length + 1);

            if (result == IntPtr.Zero)
                return IntPtr.Zero;

            Marshal.Copy(bytes, 0, result, length);
            Marshal.WriteByte(result, length, 0);

            return result;
        }

................................................................................

            SQLiteValue[] result = new SQLiteValue[nData];

            for (int index = 0, offset = 0;
                    index < result.Length;
                    index++, offset += IntPtr.Size)
            {

                IntPtr pData = MarshalReadIntPtr(apData, offset);





                result[index] = (pData != IntPtr.Zero) ?
                    new SQLiteValue(pData) : null;
            }

            return result;
        }
................................................................................
                result[index] = new SQLiteValue(values[index]);

            return result;
        }

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

        private static void IndexFromIntPtr(
            IntPtr pIndex,
            ref UnsafeNativeMethods.sqlite3_index_info index
            )
        {
            if (pIndex == IntPtr.Zero)
                return;

            Type type = typeof(UnsafeNativeMethods.sqlite3_index_info);

            int nConstraint = MarshalReadInt32(pIndex, 0);
            int nOrderBy = MarshalReadInt32(pIndex, sizeof(int) + IntPtr.Size);
















        }

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

        private static void IndexOutputsToIntPtr(
            UnsafeNativeMethods.sqlite3_index_info index,
            SQLiteIndexOutputs indexOutputs
................................................................................
            }

            return sqliteBase.DeclareVirtualTable(this, sql, ref error);
        }

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













        protected virtual bool SetTableError(
            IntPtr pVtab,
            string error
            )
        {
            if (pVtab == IntPtr.Zero)
                return false;

            int offset = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_module)) + sizeof(int);


            IntPtr pError = MarshalReadIntPtr(pVtab, offset);




            if (pError != IntPtr.Zero)
            {
                Free(pError); pError = IntPtr.Zero;


                MarshalWriteIntPtr(pVtab, offset, pError);



            }


            MarshalWriteIntPtr(pVtab, offset, Utf8IntPtrFromString(error));





            return true;
        }
        #endregion

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

        #region ISQLiteNativeModule Members
................................................................................
        public SQLiteErrorCode xBestIndex(
            IntPtr pVtab,
            IntPtr pIndex
            )
        {
            try
            {





                if (BestIndex(null) == SQLiteErrorCode.Ok)
                {
                    return SQLiteErrorCode.Ok;
                }
            }
            catch (Exception e) /* NOTE: Must catch ALL. */
            {