System.Data.SQLite
Check-in [04900b82e0]
Not logged in

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

Overview
Comment:Prevent the SetTableError virtual table helper method from throwing exceptions.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 04900b82e00cd2ee108ae944b37331684c71c02b
User & Date: mistachkin 2014-02-07 02:39:59
Context
2014-02-07
09:21
Add TypeName_SQLiteProviderServices setting to the example config file. check-in: 14d909f5e7 user: mistachkin tags: trunk
02:39
Prevent the SetTableError virtual table helper method from throwing exceptions. check-in: 04900b82e0 user: mistachkin tags: trunk
2014-02-06
19:58
Update location of the 'version.html' file on the release procedures doc page. check-in: 1823a28959 user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

  4602   4602               /// parameter to the various static error handling methods provided
  4603   4603               /// by the <see cref="SQLiteModule" /> class.
  4604   4604               /// </summary>
  4605   4605               private const bool DefaultLogErrors = true;
  4606   4606   
  4607   4607               ///////////////////////////////////////////////////////////////////
  4608   4608   
         4609  +            /// <summary>
         4610  +            /// This is the value that is always used for the "logExceptions"
         4611  +            /// parameter to the various static error handling methods provided
         4612  +            /// by the <see cref="SQLiteModule" /> class.
         4613  +            /// </summary>
         4614  +            private const bool DefaultLogExceptions = true;
         4615  +
         4616  +            ///////////////////////////////////////////////////////////////////
         4617  +
  4609   4618               /// <summary>
  4610   4619               /// This is the error message text used when the contained
  4611   4620               /// <see cref="SQLiteModule" /> object instance is not available
  4612   4621               /// for any reason.
  4613   4622               /// </summary>
  4614   4623               private const string ModuleNotAvailableErrorMessage =
  4615   4624                   "native module implementation not available";
................................................................................
  4659   4668               /// The value of <see cref="SQLiteErrorCode.Error" />.
  4660   4669               /// </returns>
  4661   4670               private static SQLiteErrorCode ModuleNotAvailableTableError(
  4662   4671                   IntPtr pVtab
  4663   4672                   )
  4664   4673               {
  4665   4674                   SetTableError(null, pVtab, DefaultLogErrors,
  4666         -                    ModuleNotAvailableErrorMessage);
         4675  +                    DefaultLogExceptions, ModuleNotAvailableErrorMessage);
  4667   4676   
  4668   4677                   return SQLiteErrorCode.Error;
  4669   4678               }
  4670   4679   
  4671   4680               ///////////////////////////////////////////////////////////////////
  4672   4681   
  4673   4682               /// <summary>
................................................................................
  4682   4691               /// The value of <see cref="SQLiteErrorCode.Error" />.
  4683   4692               /// </returns>
  4684   4693               private static SQLiteErrorCode ModuleNotAvailableCursorError(
  4685   4694                   IntPtr pCursor
  4686   4695                   )
  4687   4696               {
  4688   4697                   SetCursorError(null, pCursor, DefaultLogErrors,
  4689         -                    ModuleNotAvailableErrorMessage);
         4698  +                    DefaultLogExceptions, ModuleNotAvailableErrorMessage);
  4690   4699   
  4691   4700                   return SQLiteErrorCode.Error;
  4692   4701               }
  4693   4702               #endregion
  4694   4703   
  4695   4704               ///////////////////////////////////////////////////////////////////
  4696   4705   
................................................................................
  6157   6166           /// <param name="pVtab">
  6158   6167           /// The native pointer to the sqlite3_vtab derived structure.
  6159   6168           /// </param>
  6160   6169           /// <param name="logErrors">
  6161   6170           /// Non-zero if this error message should also be logged using the
  6162   6171           /// <see cref="SQLiteLog" /> class.
  6163   6172           /// </param>
         6173  +        /// <param name="logExceptions">
         6174  +        /// Non-zero if caught exceptions should be logged using the
         6175  +        /// <see cref="SQLiteLog" /> class.
         6176  +        /// </param>
  6164   6177           /// <param name="error">
  6165   6178           /// The error message.
  6166   6179           /// </param>
  6167   6180           /// <returns>
  6168   6181           /// Non-zero upon success.
  6169   6182           /// </returns>
  6170   6183           private static bool SetTableError(
  6171   6184               SQLiteModule module,
  6172   6185               IntPtr pVtab,
  6173   6186               bool logErrors,
         6187  +            bool logExceptions,
  6174   6188               string error
  6175   6189               )
  6176   6190           {
  6177   6191               try
  6178   6192               {
  6179   6193                   if (logErrors)
  6180   6194                   {
................................................................................
  6184   6198                   }
  6185   6199               }
  6186   6200               catch
  6187   6201               {
  6188   6202                   // do nothing.
  6189   6203               }
  6190   6204   
  6191         -            if (pVtab == IntPtr.Zero)
  6192         -                return false;
  6193         -
  6194         -            int offset = 0;
  6195         -
  6196         -            offset = SQLiteMarshal.NextOffsetOf(offset, IntPtr.Size,
  6197         -                sizeof(int));
  6198         -
  6199         -            offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
  6200         -                IntPtr.Size);
  6201         -
  6202         -            IntPtr pError = SQLiteMarshal.ReadIntPtr(pVtab, offset);
  6203         -
  6204         -            if (pError != IntPtr.Zero)
  6205         -            {
  6206         -                SQLiteMemory.Free(pError); pError = IntPtr.Zero;
  6207         -                SQLiteMarshal.WriteIntPtr(pVtab, offset, pError);
  6208         -            }
  6209         -
  6210         -            if (error == null)
  6211         -                return true;
  6212         -
  6213   6205               bool success = false;
         6206  +            IntPtr pNewError = IntPtr.Zero;
  6214   6207   
  6215   6208               try
  6216   6209               {
  6217         -                pError = SQLiteString.Utf8IntPtrFromString(error);
  6218         -                SQLiteMarshal.WriteIntPtr(pVtab, offset, pError);
         6210  +                if (pVtab == IntPtr.Zero)
         6211  +                    return false;
         6212  +
         6213  +                int offset = 0;
         6214  +
         6215  +                offset = SQLiteMarshal.NextOffsetOf(offset, IntPtr.Size,
         6216  +                    sizeof(int));
         6217  +
         6218  +                offset = SQLiteMarshal.NextOffsetOf(offset, sizeof(int),
         6219  +                    IntPtr.Size);
         6220  +
         6221  +                IntPtr pOldError = SQLiteMarshal.ReadIntPtr(pVtab, offset);
         6222  +
         6223  +                if (pOldError != IntPtr.Zero)
         6224  +                {
         6225  +                    SQLiteMemory.Free(pOldError); pOldError = IntPtr.Zero;
         6226  +                    SQLiteMarshal.WriteIntPtr(pVtab, offset, pOldError);
         6227  +                }
         6228  +
         6229  +                if (error == null)
         6230  +                    return true;
         6231  +
         6232  +                pNewError = SQLiteString.Utf8IntPtrFromString(error);
         6233  +                SQLiteMarshal.WriteIntPtr(pVtab, offset, pNewError);
  6219   6234                   success = true;
         6235  +            }
         6236  +            catch (Exception e) /* NOTE: Must catch ALL. */
         6237  +            {
         6238  +                try
         6239  +                {
         6240  +                    if (logExceptions)
         6241  +                    {
         6242  +                        SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION,
         6243  +                            String.Format(CultureInfo.CurrentCulture,
         6244  +                            "Caught exception in \"SetTableError\" method: {0}",
         6245  +                            e)); /* throw */
         6246  +                    }
         6247  +                }
         6248  +                catch
         6249  +                {
         6250  +                    // do nothing.
         6251  +                }
  6220   6252               }
  6221   6253               finally
  6222   6254               {
  6223         -                if (!success && (pError != IntPtr.Zero))
         6255  +                if (!success && (pNewError != IntPtr.Zero))
  6224   6256                   {
  6225         -                    SQLiteMemory.Free(pError);
  6226         -                    pError = IntPtr.Zero;
         6257  +                    SQLiteMemory.Free(pNewError);
         6258  +                    pNewError = IntPtr.Zero;
  6227   6259                   }
  6228   6260               }
  6229   6261   
  6230   6262               return success;
  6231   6263           }
  6232   6264   
  6233   6265           ///////////////////////////////////////////////////////////////////////
................................................................................
  6244   6276           /// The <see cref="SQLiteVirtualTable" /> object instance used to
  6245   6277           /// lookup the native pointer to the sqlite3_vtab derived structure.
  6246   6278           /// </param>
  6247   6279           /// <param name="logErrors">
  6248   6280           /// Non-zero if this error message should also be logged using the
  6249   6281           /// <see cref="SQLiteLog" /> class.
  6250   6282           /// </param>
         6283  +        /// <param name="logExceptions">
         6284  +        /// Non-zero if caught exceptions should be logged using the
         6285  +        /// <see cref="SQLiteLog" /> class.
         6286  +        /// </param>
  6251   6287           /// <param name="error">
  6252   6288           /// The error message.
  6253   6289           /// </param>
  6254   6290           /// <returns>
  6255   6291           /// Non-zero upon success.
  6256   6292           /// </returns>
  6257   6293           private static bool SetTableError(
  6258   6294               SQLiteModule module,
  6259   6295               SQLiteVirtualTable table,
  6260   6296               bool logErrors,
         6297  +            bool logExceptions,
  6261   6298               string error
  6262   6299               )
  6263   6300           {
  6264   6301               if (table == null)
  6265   6302                   return false;
  6266   6303   
  6267   6304               IntPtr pVtab = table.NativeHandle;
  6268   6305   
  6269   6306               if (pVtab == IntPtr.Zero)
  6270   6307                   return false;
  6271   6308   
  6272         -            return SetTableError(module, pVtab, logErrors, error);
         6309  +            return SetTableError(
         6310  +                module, pVtab, logErrors, logExceptions, error);
  6273   6311           }
  6274   6312   
  6275   6313           ///////////////////////////////////////////////////////////////////////
  6276   6314   
  6277   6315           /// <summary>
  6278   6316           /// Arranges for the specified error message to be placed into the
  6279   6317           /// zErrMsg field of a sqlite3_vtab derived structure, freeing the
................................................................................
  6287   6325           /// used to get the native pointer to the sqlite3_vtab derived
  6288   6326           /// structure.
  6289   6327           /// </param>
  6290   6328           /// <param name="logErrors">
  6291   6329           /// Non-zero if this error message should also be logged using the
  6292   6330           /// <see cref="SQLiteLog" /> class.
  6293   6331           /// </param>
         6332  +        /// <param name="logExceptions">
         6333  +        /// Non-zero if caught exceptions should be logged using the
         6334  +        /// <see cref="SQLiteLog" /> class.
         6335  +        /// </param>
  6294   6336           /// <param name="error">
  6295   6337           /// The error message.
  6296   6338           /// </param>
  6297   6339           /// <returns>
  6298   6340           /// Non-zero upon success.
  6299   6341           /// </returns>
  6300   6342           private static bool SetCursorError(
  6301   6343               SQLiteModule module,
  6302   6344               IntPtr pCursor,
  6303   6345               bool logErrors,
         6346  +            bool logExceptions,
  6304   6347               string error
  6305   6348               )
  6306   6349           {
  6307   6350               if (pCursor == IntPtr.Zero)
  6308   6351                   return false;
  6309   6352   
  6310   6353               IntPtr pVtab = TableFromCursor(module, pCursor);
  6311   6354   
  6312   6355               if (pVtab == IntPtr.Zero)
  6313   6356                   return false;
  6314   6357   
  6315         -            return SetTableError(module, pVtab, logErrors, error);
         6358  +            return SetTableError(
         6359  +                module, pVtab, logErrors, logExceptions, error);
  6316   6360           }
  6317   6361   
  6318   6362           ///////////////////////////////////////////////////////////////////////
  6319   6363   
  6320   6364           /// <summary>
  6321   6365           /// Arranges for the specified error message to be placed into the
  6322   6366           /// zErrMsg field of a sqlite3_vtab derived structure, freeing the
................................................................................
  6329   6373           /// The <see cref="SQLiteVirtualTableCursor" /> object instance used to
  6330   6374           /// lookup the native pointer to the sqlite3_vtab derived structure.
  6331   6375           /// </param>
  6332   6376           /// <param name="logErrors">
  6333   6377           /// Non-zero if this error message should also be logged using the
  6334   6378           /// <see cref="SQLiteLog" /> class.
  6335   6379           /// </param>
         6380  +        /// <param name="logExceptions">
         6381  +        /// Non-zero if caught exceptions should be logged using the
         6382  +        /// <see cref="SQLiteLog" /> class.
         6383  +        /// </param>
  6336   6384           /// <param name="error">
  6337   6385           /// The error message.
  6338   6386           /// </param>
  6339   6387           /// <returns>
  6340   6388           /// Non-zero upon success.
  6341   6389           /// </returns>
  6342   6390           private static bool SetCursorError(
  6343   6391               SQLiteModule module,
  6344   6392               SQLiteVirtualTableCursor cursor,
  6345   6393               bool logErrors,
         6394  +            bool logExceptions,
  6346   6395               string error
  6347   6396               )
  6348   6397           {
  6349   6398               if (cursor == null)
  6350   6399                   return false;
  6351   6400   
  6352   6401               IntPtr pCursor = cursor.NativeHandle;
  6353   6402   
  6354   6403               if (pCursor == IntPtr.Zero)
  6355   6404                   return false;
  6356   6405   
  6357         -            return SetCursorError(module, pCursor, logErrors, error);
         6406  +            return SetCursorError(
         6407  +                module, pCursor, logErrors, logExceptions, error);
  6358   6408           }
  6359   6409           #endregion
  6360   6410           #endregion
  6361   6411   
  6362   6412           ///////////////////////////////////////////////////////////////////////
  6363   6413   
  6364   6414           #region Protected Members
................................................................................
  6899   6949           ///////////////////////////////////////////////////////////////////////
  6900   6950   
  6901   6951           private bool logExceptions;
  6902   6952           /// <summary>
  6903   6953           /// Returns or sets a boolean value indicating whether exceptions
  6904   6954           /// caught in the
  6905   6955           /// <see cref="ISQLiteNativeModule.xDisconnect" /> method,
  6906         -        /// <see cref="ISQLiteNativeModule.xDestroy" /> method, and the
  6907         -        /// <see cref="Dispose()" /> method should be logged using the
         6956  +        /// the <see cref="ISQLiteNativeModule.xDestroy" /> method,
         6957  +        /// the <see cref="SetTableError(IntPtr,string)" /> method,
         6958  +        /// the <see cref="SetTableError(SQLiteVirtualTable,string)" /> method,
         6959  +        /// and the <see cref="Dispose()" /> method should be logged using the
  6908   6960           /// <see cref="SQLiteLog" /> class.
  6909   6961           /// </summary>
  6910   6962           protected virtual bool LogExceptionsNoThrow
  6911   6963           {
  6912   6964               get { return logExceptions; }
  6913   6965               set { logExceptions = value; }
  6914   6966           }
................................................................................
  6932   6984           /// Non-zero upon success.
  6933   6985           /// </returns>
  6934   6986           protected virtual bool SetTableError(
  6935   6987               IntPtr pVtab,
  6936   6988               string error
  6937   6989               )
  6938   6990           {
  6939         -            return SetTableError(this, pVtab, LogErrorsNoThrow, error);
         6991  +            return SetTableError(
         6992  +                this, pVtab, LogErrorsNoThrow, LogExceptionsNoThrow, error);
  6940   6993           }
  6941   6994   
  6942   6995           ///////////////////////////////////////////////////////////////////////
  6943   6996   
  6944   6997           /// <summary>
  6945   6998           /// Arranges for the specified error message to be placed into the
  6946   6999           /// zErrMsg field of a sqlite3_vtab derived structure, freeing the
................................................................................
  6957   7010           /// Non-zero upon success.
  6958   7011           /// </returns>
  6959   7012           protected virtual bool SetTableError(
  6960   7013               SQLiteVirtualTable table,
  6961   7014               string error
  6962   7015               )
  6963   7016           {
  6964         -            return SetTableError(this, table, LogErrorsNoThrow, error);
         7017  +            return SetTableError(
         7018  +                this, table, LogErrorsNoThrow, LogExceptionsNoThrow, error);
  6965   7019           }
  6966   7020   
  6967   7021           ///////////////////////////////////////////////////////////////////////
  6968   7022   
  6969   7023           /// <summary>
  6970   7024           /// Arranges for the specified error message to be placed into the
  6971   7025           /// zErrMsg field of a sqlite3_vtab derived structure, freeing the
................................................................................
  6982   7036           /// Non-zero upon success.
  6983   7037           /// </returns>
  6984   7038           protected virtual bool SetCursorError(
  6985   7039               SQLiteVirtualTableCursor cursor,
  6986   7040               string error
  6987   7041               )
  6988   7042           {
  6989         -            return SetCursorError(this, cursor, LogErrorsNoThrow, error);
         7043  +            return SetCursorError(
         7044  +                this, cursor, LogErrorsNoThrow, LogExceptionsNoThrow, error);
  6990   7045           }
  6991   7046           #endregion
  6992   7047   
  6993   7048           ///////////////////////////////////////////////////////////////////////
  6994   7049   
  6995   7050           #region Index Handling Helper Methods
  6996   7051           /// <summary>