System.Data.SQLite
Check-in [82d24cdca3]
Not logged in

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

Overview
Comment:Also move the static SQLiteIndex marshalling methods into the SQLiteIndex class itself.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 82d24cdca30b6d936b31e17ce7ecab347acfa066
User & Date: mistachkin 2013-07-05 22:44:40
Context
2013-07-06
00:16
Enhance the comments for the SQLiteModuleNoop class. check-in: bf02fc9d80 user: mistachkin tags: trunk
2013-07-05
22:44
Also move the static SQLiteIndex marshalling methods into the SQLiteIndex class itself. check-in: 82d24cdca3 user: mistachkin tags: trunk
22:36
Move the static SQLiteValue marshalling method into the SQLiteValue class itself. check-in: bd1c5de6ec user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

   327    327           {
   328    328               pValue = IntPtr.Zero;
   329    329           }
   330    330           #endregion
   331    331   
   332    332           ///////////////////////////////////////////////////////////////////////
   333    333   
   334         -        #region Internal Methods
          334  +        #region Internal Marshal Helper Methods
   335    335           /// <summary>
   336    336           /// Converts a logical array of native pointers to native sqlite3_value
   337    337           /// structures into a managed array of <see cref="SQLiteValue" />
   338    338           /// object instances.
   339    339           /// </summary>
   340    340           /// <param name="argc">
   341    341           /// The number of elements in the logical array of native sqlite3_value
................................................................................
  1072   1072               int nOrderBy
  1073   1073               )
  1074   1074           {
  1075   1075               inputs = new SQLiteIndexInputs(nConstraint, nOrderBy);
  1076   1076               outputs = new SQLiteIndexOutputs(nConstraint);
  1077   1077           }
  1078   1078           #endregion
         1079  +
         1080  +        ///////////////////////////////////////////////////////////////////////
         1081  +
         1082  +        #region Internal Marshal Helper Methods
         1083  +        /// <summary>
         1084  +        /// Converts a native pointer to a native sqlite3_index_info structure
         1085  +        /// into a new <see cref="SQLiteIndex" /> object instance.
         1086  +        /// </summary>
         1087  +        /// <param name="pIndex">
         1088  +        /// The native pointer to the native sqlite3_index_info structure to
         1089  +        /// convert.
         1090  +        /// </param>
         1091  +        /// <param name="index">
         1092  +        /// Upon success, this parameter will be modified to contain the newly
         1093  +        /// created <see cref="SQLiteIndex" /> object instance.
         1094  +        /// </param>
         1095  +        internal static void FromIntPtr(
         1096  +            IntPtr pIndex,
         1097  +            ref SQLiteIndex index
         1098  +            )
         1099  +        {
         1100  +            if (pIndex == IntPtr.Zero)
         1101  +                return;
         1102  +
         1103  +            int offset = 0;
         1104  +
         1105  +            int nConstraint = SQLiteMarshal.ReadInt32(pIndex, offset);
         1106  +
         1107  +            offset += sizeof(int);
         1108  +
         1109  +            IntPtr pConstraint = SQLiteMarshal.ReadIntPtr(pIndex, offset);
         1110  +
         1111  +            offset += IntPtr.Size;
         1112  +
         1113  +            int nOrderBy = SQLiteMarshal.ReadInt32(pIndex, offset);
         1114  +
         1115  +            offset += sizeof(int);
         1116  +
         1117  +            IntPtr pOrderBy = SQLiteMarshal.ReadIntPtr(pIndex, offset);
         1118  +
         1119  +            index = new SQLiteIndex(nConstraint, nOrderBy);
         1120  +
         1121  +            int sizeOfConstraintType = Marshal.SizeOf(typeof(
         1122  +                UnsafeNativeMethods.sqlite3_index_constraint));
         1123  +
         1124  +            for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
         1125  +            {
         1126  +                UnsafeNativeMethods.sqlite3_index_constraint constraint =
         1127  +                    new UnsafeNativeMethods.sqlite3_index_constraint();
         1128  +
         1129  +                Marshal.PtrToStructure(SQLiteMarshal.IntPtrForOffset(
         1130  +                    pConstraint, iConstraint * sizeOfConstraintType),
         1131  +                    constraint);
         1132  +
         1133  +                index.Inputs.Constraints[iConstraint] =
         1134  +                    new SQLiteIndexConstraint(constraint);
         1135  +            }
         1136  +
         1137  +            int sizeOfOrderByType = Marshal.SizeOf(typeof(
         1138  +                UnsafeNativeMethods.sqlite3_index_orderby));
         1139  +
         1140  +            for (int iOrderBy = 0; iOrderBy < nOrderBy; iOrderBy++)
         1141  +            {
         1142  +                UnsafeNativeMethods.sqlite3_index_orderby orderBy =
         1143  +                    new UnsafeNativeMethods.sqlite3_index_orderby();
         1144  +
         1145  +                Marshal.PtrToStructure(SQLiteMarshal.IntPtrForOffset(
         1146  +                    pOrderBy, iOrderBy * sizeOfOrderByType), orderBy);
         1147  +
         1148  +                index.Inputs.OrderBys[iOrderBy] =
         1149  +                    new SQLiteIndexOrderBy(orderBy);
         1150  +            }
         1151  +        }
         1152  +
         1153  +        ///////////////////////////////////////////////////////////////////////
         1154  +
         1155  +        /// <summary>
         1156  +        /// Populates the outputs of a pre-allocated native sqlite3_index_info
         1157  +        /// structure using an existing <see cref="SQLiteIndex" /> object
         1158  +        /// instance.
         1159  +        /// </summary>
         1160  +        /// <param name="index">
         1161  +        /// The existing <see cref="SQLiteIndex" /> object instance containing
         1162  +        /// the output data to use.
         1163  +        /// </param>
         1164  +        /// <param name="pIndex">
         1165  +        /// The native pointer to the pre-allocated native sqlite3_index_info
         1166  +        /// structure.
         1167  +        /// </param>
         1168  +        internal static void ToIntPtr(
         1169  +            SQLiteIndex index,
         1170  +            IntPtr pIndex
         1171  +            )
         1172  +        {
         1173  +            if ((index == null) || (index.Inputs == null) ||
         1174  +                (index.Inputs.Constraints == null) ||
         1175  +                (index.Outputs == null) ||
         1176  +                (index.Outputs.ConstraintUsages == null))
         1177  +            {
         1178  +                return;
         1179  +            }
         1180  +
         1181  +            if (pIndex == IntPtr.Zero)
         1182  +                return;
         1183  +
         1184  +            int offset = 0;
         1185  +
         1186  +            int nConstraint = SQLiteMarshal.ReadInt32(pIndex, offset);
         1187  +
         1188  +            if (nConstraint != index.Inputs.Constraints.Length)
         1189  +                return;
         1190  +
         1191  +            if (nConstraint != index.Outputs.ConstraintUsages.Length)
         1192  +                return;
         1193  +
         1194  +            offset += sizeof(int) + IntPtr.Size + sizeof(int) + IntPtr.Size;
         1195  +
         1196  +            IntPtr pConstraintUsage = SQLiteMarshal.ReadIntPtr(pIndex, offset);
         1197  +
         1198  +            int sizeOfConstraintUsageType = Marshal.SizeOf(typeof(
         1199  +                UnsafeNativeMethods.sqlite3_index_constraint_usage));
         1200  +
         1201  +            for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
         1202  +            {
         1203  +                UnsafeNativeMethods.sqlite3_index_constraint_usage constraintUsage =
         1204  +                    new UnsafeNativeMethods.sqlite3_index_constraint_usage(
         1205  +                        index.Outputs.ConstraintUsages[iConstraint]);
         1206  +
         1207  +                Marshal.StructureToPtr(
         1208  +                    constraintUsage, SQLiteMarshal.IntPtrForOffset(
         1209  +                    pConstraintUsage, iConstraint * sizeOfConstraintUsageType),
         1210  +                    false);
         1211  +
         1212  +                index.Outputs.ConstraintUsages[iConstraint] =
         1213  +                    new SQLiteIndexConstraintUsage(constraintUsage);
         1214  +            }
         1215  +
         1216  +            offset += IntPtr.Size;
         1217  +
         1218  +            SQLiteMarshal.WriteInt32(pIndex, offset,
         1219  +                index.Outputs.IndexNumber);
         1220  +
         1221  +            offset += sizeof(int);
         1222  +
         1223  +            SQLiteMarshal.WriteIntPtr(pIndex, offset,
         1224  +                SQLiteString.Utf8IntPtrFromString(index.Outputs.IndexString));
         1225  +
         1226  +            offset += IntPtr.Size;
         1227  +
         1228  +            //
         1229  +            // NOTE: We just allocated the IndexString field; therefore, we
         1230  +            //       need to set the NeedToFreeIndexString field to non-zero.
         1231  +            //
         1232  +            SQLiteMarshal.WriteInt32(pIndex, offset, 1);
         1233  +
         1234  +            offset += sizeof(int);
         1235  +
         1236  +            SQLiteMarshal.WriteInt32(pIndex, offset,
         1237  +                index.Outputs.OrderByConsumed);
         1238  +
         1239  +            offset += sizeof(int);
         1240  +
         1241  +            SQLiteMarshal.WriteDouble(pIndex, offset,
         1242  +                index.Outputs.EstimatedCost);
         1243  +        }
         1244  +        #endregion
  1079   1245   
  1080   1246           ///////////////////////////////////////////////////////////////////////
  1081   1247   
  1082   1248           #region Public Properties
  1083   1249           private SQLiteIndexInputs inputs;
  1084   1250           /// <summary>
  1085   1251           /// The <see cref="SQLiteIndexInputs" /> object instance containing
................................................................................
  4123   4289   #if !PLATFORM_COMPACTFRAMEWORK
  4124   4290               Marshal.WriteIntPtr(pointer, offset, value);
  4125   4291   #else
  4126   4292               Marshal.WriteIntPtr(IntPtrForOffset(pointer, offset), value);
  4127   4293   #endif
  4128   4294           }
  4129   4295           #endregion
  4130         -
  4131         -        ///////////////////////////////////////////////////////////////////////
  4132         -
  4133         -        #region SQLiteIndex Helper Methods
  4134         -        /// <summary>
  4135         -        /// Converts a native pointer to a native sqlite3_index_info structure
  4136         -        /// into a new <see cref="SQLiteIndex" /> object instance.
  4137         -        /// </summary>
  4138         -        /// <param name="pIndex">
  4139         -        /// The native pointer to the native sqlite3_index_info structure to
  4140         -        /// convert.
  4141         -        /// </param>
  4142         -        /// <param name="index">
  4143         -        /// Upon success, this parameter will be modified to contain the newly
  4144         -        /// created <see cref="SQLiteIndex" /> object instance.
  4145         -        /// </param>
  4146         -        public static void IndexFromIntPtr(
  4147         -            IntPtr pIndex,
  4148         -            ref SQLiteIndex index
  4149         -            )
  4150         -        {
  4151         -            if (pIndex == IntPtr.Zero)
  4152         -                return;
  4153         -
  4154         -            int offset = 0;
  4155         -
  4156         -            int nConstraint = ReadInt32(pIndex, offset);
  4157         -
  4158         -            offset += sizeof(int);
  4159         -
  4160         -            IntPtr pConstraint = ReadIntPtr(pIndex, offset);
  4161         -
  4162         -            offset += IntPtr.Size;
  4163         -
  4164         -            int nOrderBy = ReadInt32(pIndex, offset);
  4165         -
  4166         -            offset += sizeof(int);
  4167         -
  4168         -            IntPtr pOrderBy = ReadIntPtr(pIndex, offset);
  4169         -
  4170         -            index = new SQLiteIndex(nConstraint, nOrderBy);
  4171         -
  4172         -            int sizeOfConstraintType = Marshal.SizeOf(typeof(
  4173         -                UnsafeNativeMethods.sqlite3_index_constraint));
  4174         -
  4175         -            for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
  4176         -            {
  4177         -                UnsafeNativeMethods.sqlite3_index_constraint constraint =
  4178         -                    new UnsafeNativeMethods.sqlite3_index_constraint();
  4179         -
  4180         -                Marshal.PtrToStructure(IntPtrForOffset(pConstraint,
  4181         -                    iConstraint * sizeOfConstraintType), constraint);
  4182         -
  4183         -                index.Inputs.Constraints[iConstraint] =
  4184         -                    new SQLiteIndexConstraint(constraint);
  4185         -            }
  4186         -
  4187         -            int sizeOfOrderByType = Marshal.SizeOf(typeof(
  4188         -                UnsafeNativeMethods.sqlite3_index_orderby));
  4189         -
  4190         -            for (int iOrderBy = 0; iOrderBy < nOrderBy; iOrderBy++)
  4191         -            {
  4192         -                UnsafeNativeMethods.sqlite3_index_orderby orderBy =
  4193         -                    new UnsafeNativeMethods.sqlite3_index_orderby();
  4194         -
  4195         -                Marshal.PtrToStructure(IntPtrForOffset(pOrderBy,
  4196         -                    iOrderBy * sizeOfOrderByType), orderBy);
  4197         -
  4198         -                index.Inputs.OrderBys[iOrderBy] =
  4199         -                    new SQLiteIndexOrderBy(orderBy);
  4200         -            }
  4201         -        }
  4202         -
  4203         -        ///////////////////////////////////////////////////////////////////////
  4204         -
  4205         -        /// <summary>
  4206         -        /// Populates the outputs of a pre-allocated native sqlite3_index_info
  4207         -        /// structure using an existing <see cref="SQLiteIndex" /> object
  4208         -        /// instance.
  4209         -        /// </summary>
  4210         -        /// <param name="index">
  4211         -        /// The existing <see cref="SQLiteIndex" /> object instance containing
  4212         -        /// the output data to use.
  4213         -        /// </param>
  4214         -        /// <param name="pIndex">
  4215         -        /// The native pointer to the pre-allocated native sqlite3_index_info
  4216         -        /// structure.
  4217         -        /// </param>
  4218         -        public static void IndexToIntPtr(
  4219         -            SQLiteIndex index,
  4220         -            IntPtr pIndex
  4221         -            )
  4222         -        {
  4223         -            if ((index == null) || (index.Inputs == null) ||
  4224         -                (index.Inputs.Constraints == null) ||
  4225         -                (index.Outputs == null) ||
  4226         -                (index.Outputs.ConstraintUsages == null))
  4227         -            {
  4228         -                return;
  4229         -            }
  4230         -
  4231         -            if (pIndex == IntPtr.Zero)
  4232         -                return;
  4233         -
  4234         -            int offset = 0;
  4235         -
  4236         -            int nConstraint = ReadInt32(pIndex, offset);
  4237         -
  4238         -            if (nConstraint != index.Inputs.Constraints.Length)
  4239         -                return;
  4240         -
  4241         -            if (nConstraint != index.Outputs.ConstraintUsages.Length)
  4242         -                return;
  4243         -
  4244         -            offset += sizeof(int) + IntPtr.Size + sizeof(int) + IntPtr.Size;
  4245         -
  4246         -            IntPtr pConstraintUsage = ReadIntPtr(pIndex, offset);
  4247         -
  4248         -            int sizeOfConstraintUsageType = Marshal.SizeOf(typeof(
  4249         -                UnsafeNativeMethods.sqlite3_index_constraint_usage));
  4250         -
  4251         -            for (int iConstraint = 0; iConstraint < nConstraint; iConstraint++)
  4252         -            {
  4253         -                UnsafeNativeMethods.sqlite3_index_constraint_usage constraintUsage =
  4254         -                    new UnsafeNativeMethods.sqlite3_index_constraint_usage(
  4255         -                        index.Outputs.ConstraintUsages[iConstraint]);
  4256         -
  4257         -                Marshal.StructureToPtr(
  4258         -                    constraintUsage, IntPtrForOffset(pConstraintUsage,
  4259         -                    iConstraint * sizeOfConstraintUsageType), false);
  4260         -
  4261         -                index.Outputs.ConstraintUsages[iConstraint] =
  4262         -                    new SQLiteIndexConstraintUsage(constraintUsage);
  4263         -            }
  4264         -
  4265         -            offset += IntPtr.Size;
  4266         -
  4267         -            WriteInt32(pIndex, offset, index.Outputs.IndexNumber);
  4268         -
  4269         -            offset += sizeof(int);
  4270         -
  4271         -            WriteIntPtr(pIndex, offset, SQLiteString.Utf8IntPtrFromString(
  4272         -                index.Outputs.IndexString));
  4273         -
  4274         -            offset += IntPtr.Size;
  4275         -
  4276         -            WriteInt32(pIndex, offset, 1); /* NOTE: We just allocated it. */
  4277         -
  4278         -            offset += sizeof(int);
  4279         -
  4280         -            WriteInt32(pIndex, offset, index.Outputs.OrderByConsumed);
  4281         -
  4282         -            offset += sizeof(int);
  4283         -
  4284         -            WriteDouble(pIndex, offset, index.Outputs.EstimatedCost);
  4285         -        }
  4286         -        #endregion
  4287   4296       }
  4288   4297       #endregion
  4289   4298   
  4290   4299       ///////////////////////////////////////////////////////////////////////////
  4291   4300   
  4292   4301       #region SQLiteModule Base Class
  4293   4302       /// <summary>
................................................................................
  6660   6669               {
  6661   6670                   SQLiteVirtualTable table = TableFromIntPtr(pVtab);
  6662   6671   
  6663   6672                   if (table != null)
  6664   6673                   {
  6665   6674                       SQLiteIndex index = null;
  6666   6675   
  6667         -                    SQLiteMarshal.IndexFromIntPtr(pIndex, ref index);
         6676  +                    SQLiteIndex.FromIntPtr(pIndex, ref index);
  6668   6677   
  6669   6678                       if (BestIndex(table, index) == SQLiteErrorCode.Ok)
  6670   6679                       {
  6671         -                        SQLiteMarshal.IndexToIntPtr(index, pIndex);
         6680  +                        SQLiteIndex.ToIntPtr(index, pIndex);
  6672   6681                           return SQLiteErrorCode.Ok;
  6673   6682                       }
  6674   6683                   }
  6675   6684               }
  6676   6685               catch (Exception e) /* NOTE: Must catch ALL. */
  6677   6686               {
  6678   6687                   SetTableError(pVtab, e.ToString());