Index: System.Data.SQLite/SQLiteFunction.cs ================================================================== --- System.Data.SQLite/SQLiteFunction.cs +++ System.Data.SQLite/SQLiteFunction.cs @@ -45,81 +45,10 @@ /// private const int COR_E_EXCEPTION = unchecked((int)0x80131500); #endregion ///////////////////////////////////////////////////////////////////////// - - #region Private Delegate Types - /// - /// See the Invoke method of this class. - /// - /// - /// See the Invoke method of this class. - /// - /// - /// See the Invoke method of this class. - /// - private delegate object SQLiteFunctionInvokeCallback( - object[] args - ); - - ///////////////////////////////////////////////////////////////////////// - - /// - /// See the Step method of this class. - /// - /// - /// See the Step method of this class. - /// - /// - /// See the Step method of this class. - /// - /// - /// See the Step method of this class. - /// - private delegate void SQLiteFunctionStepCallback( - object[] args, - int stepNumber, - ref object contextData - ); - - ///////////////////////////////////////////////////////////////////////// - - /// - /// See the Final method of this class. - /// - /// - /// See the Final method of this class. - /// - /// - /// See the Final method of this class. - /// - private delegate object SQLiteFunctionFinalCallback( - object contextData - ); - - ///////////////////////////////////////////////////////////////////////// - - /// - /// See the Compare method of this class. - /// - /// - /// See the Compare method of this class. - /// - /// - /// See the Compare method of this class. - /// - /// - /// See the Compare method of this class. - /// - private delegate int SQLiteCollationCompareCallback( - string param1, - string param2 - ); - #endregion - - ///////////////////////////////////////////////////////////////////////// /// /// The base connection this function is attached to /// internal SQLiteBase _base; @@ -132,34 +61,10 @@ /// /// The connection flags associated with this object (this should be the /// same value as the flags associated with the parent connection object). /// private SQLiteConnectionFlags _flags; - - /// - /// The private instance of the SQLiteFunctionInvokeCallback delegate - /// associated with this object. - /// - private SQLiteFunctionInvokeCallback _invokeCallback; - - /// - /// The private instance of the SQLiteFunctionStepCallback delegate - /// associated with this object. - /// - private SQLiteFunctionStepCallback _stepCallback; - - /// - /// The private instance of the SQLiteFunctionFinalCallback delegate - /// associated with this object. - /// - private SQLiteFunctionFinalCallback _finalCallback; - - /// - /// The private instance of the SQLiteCollationCompareCallback delegate - /// associated with this object. - /// - private SQLiteCollationCompareCallback _compareCallback; /// /// Holds a reference to the callback function for user functions /// private SQLiteCallback _InvokeFunc; @@ -248,14 +153,10 @@ } _contextDataList.Clear(); _contextDataList = null; _flags = SQLiteConnectionFlags.None; - _invokeCallback = null; - _stepCallback = null; - _finalCallback = null; - _compareCallback = null; _InvokeFunc = null; _StepFunc = null; _FinalFunc = null; _CompareFunc = null; @@ -467,153 +368,144 @@ } } /// /// Internal scalar callback function, which wraps the raw context pointer and calls the virtual Invoke() method. - /// Does nothing if there is no invoke callback configured. WARNING: Must not throw exceptions. + /// WARNING: Must not throw exceptions. /// /// A raw context pointer /// Number of arguments passed in /// A pointer to the array of arguments internal void ScalarCallback(IntPtr context, int nArgs, IntPtr argsptr) { - if (_invokeCallback != null) + try + { + _context = context; + SetReturnValue(context, + Invoke(ConvertParams(nArgs, argsptr))); /* throw */ + } +#if !PLATFORM_COMPACTFRAMEWORK + catch (Exception e) /* NOTE: Must catch ALL. */ { try { - _context = context; - SetReturnValue(context, - _invokeCallback(ConvertParams(nArgs, argsptr))); /* throw */ - } -#if !PLATFORM_COMPACTFRAMEWORK - catch (Exception e) /* NOTE: Must catch ALL. */ - { - try + if ((_flags & SQLiteConnectionFlags.LogCallbackException) == + SQLiteConnectionFlags.LogCallbackException) { - if ((_flags & SQLiteConnectionFlags.LogCallbackException) == - SQLiteConnectionFlags.LogCallbackException) - { - SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( - "Caught exception in {0} callback: {1}", - typeof(SQLiteFunctionInvokeCallback), e)); /* throw */ - } - } - catch - { - // do nothing. + SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( + "Caught exception in \"Invoke\" method: {0}", + e)); /* throw */ } } + catch + { + // do nothing. + } + } #else - catch /* NOTE: Must catch ALL. */ - { - // do nothing (Windows CE). - } + catch /* NOTE: Must catch ALL. */ + { + // do nothing (Windows CE). + } #endif - } } /// /// Internal collation sequence function, which wraps up the raw string pointers and executes the Compare() virtual function. - /// Does nothing and returns zero if there is no compare callback configured. WARNING: Must not throw exceptions. + /// WARNING: Must not throw exceptions. /// /// Not used /// Length of the string pv1 /// Pointer to the first string to compare /// Length of the string pv2 /// Pointer to the second string to compare /// Returns -1 if the first string is less than the second. 0 if they are equal, or 1 if the first string is greater - /// than the second. Returns 0 if there is no callback wrapper configured or an exception is caught. + /// than the second. Returns 0 if an exception is caught. internal int CompareCallback(IntPtr ptr, int len1, IntPtr ptr1, int len2, IntPtr ptr2) { - if (_compareCallback != null) + try + { + return Compare(SQLiteConvert.UTF8ToString(ptr1, len1), + SQLiteConvert.UTF8ToString(ptr2, len2)); /* throw */ + } +#if !PLATFORM_COMPACTFRAMEWORK + catch (Exception e) /* NOTE: Must catch ALL. */ { try { - return _compareCallback(SQLiteConvert.UTF8ToString(ptr1, len1), - SQLiteConvert.UTF8ToString(ptr2, len2)); /* throw */ - } -#if !PLATFORM_COMPACTFRAMEWORK - catch (Exception e) /* NOTE: Must catch ALL. */ - { - try + if ((_flags & SQLiteConnectionFlags.LogCallbackException) == + SQLiteConnectionFlags.LogCallbackException) { - if ((_flags & SQLiteConnectionFlags.LogCallbackException) == - SQLiteConnectionFlags.LogCallbackException) - { - SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( - "Caught exception in {0} callback: {1}", - typeof(SQLiteCollationCompareCallback), e)); /* throw */ - } - } - catch - { - // do nothing. + SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( + "Caught exception in \"Compare\" method: {0}", + e)); /* throw */ } } + catch + { + // do nothing. + } + } #else - catch /* NOTE: Must catch ALL. */ - { - // do nothing (Windows CE). - } + catch /* NOTE: Must catch ALL. */ + { + // do nothing (Windows CE). + } #endif - } return 0; } /// /// Internal collation sequence function, which wraps up the raw string pointers and executes the Compare() virtual function. - /// Does nothing and returns zero if there is no compare callback configured. WARNING: Must not throw exceptions. + /// WARNING: Must not throw exceptions. /// /// Not used /// Length of the string pv1 /// Pointer to the first string to compare /// Length of the string pv2 /// Pointer to the second string to compare /// Returns -1 if the first string is less than the second. 0 if they are equal, or 1 if the first string is greater - /// than the second. Returns 0 if there is no callback wrapper configured or an exception is caught. + /// than the second. Returns 0 if an exception is caught. internal int CompareCallback16(IntPtr ptr, int len1, IntPtr ptr1, int len2, IntPtr ptr2) { - if (_compareCallback != null) + try + { + return Compare(SQLite3_UTF16.UTF16ToString(ptr1, len1), + SQLite3_UTF16.UTF16ToString(ptr2, len2)); /* throw */ + } +#if !PLATFORM_COMPACTFRAMEWORK + catch (Exception e) /* NOTE: Must catch ALL. */ { try { - return _compareCallback(SQLite3_UTF16.UTF16ToString(ptr1, len1), - SQLite3_UTF16.UTF16ToString(ptr2, len2)); /* throw */ - } -#if !PLATFORM_COMPACTFRAMEWORK - catch (Exception e) /* NOTE: Must catch ALL. */ - { - try + if ((_flags & SQLiteConnectionFlags.LogCallbackException) == + SQLiteConnectionFlags.LogCallbackException) { - if ((_flags & SQLiteConnectionFlags.LogCallbackException) == - SQLiteConnectionFlags.LogCallbackException) - { - SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( - "Caught exception in {0} callback: {1}", - typeof(SQLiteCollationCompareCallback), e)); /* throw */ - } - } - catch - { - // do nothing. + SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( + "Caught exception in \"Compare\" method: {0}", + e)); /* throw */ } } + catch + { + // do nothing. + } + } #else - catch /* NOTE: Must catch ALL. */ - { - // do nothing (Windows CE). - } -#endif + catch /* NOTE: Must catch ALL. */ + { + // do nothing (Windows CE). } +#endif return 0; } /// /// The internal aggregate Step function callback, which wraps the raw context pointer and calls the virtual Step() method. - /// Does nothing if there is no step callback configured. WARNING: Must not throw exceptions. + /// WARNING: Must not throw exceptions. /// /// /// This function takes care of doing the lookups and getting the important information put together to call the Step() function. /// That includes pulling out the user's contextData and updating it after the call is made. We use a sorted list for this so /// binary searches can be done to find the data. @@ -621,130 +513,125 @@ /// A raw context pointer /// Number of arguments passed in /// A pointer to the array of arguments internal void StepCallback(IntPtr context, int nArgs, IntPtr argsptr) { - if (_stepCallback != null) + try + { + AggregateData data = null; + + if (_base != null) + { + IntPtr nAux = _base.AggregateContext(context); + + if ((_contextDataList != null) && + !_contextDataList.TryGetValue(nAux, out data)) + { + data = new AggregateData(); + _contextDataList[nAux] = data; + } + } + + if (data == null) + data = new AggregateData(); + + try + { + _context = context; + Step(ConvertParams(nArgs, argsptr), + data._count, ref data._data); /* throw */ + } + finally + { + data._count++; + } + } +#if !PLATFORM_COMPACTFRAMEWORK + catch (Exception e) /* NOTE: Must catch ALL. */ { try { - AggregateData data = null; - - if (_base != null) + if ((_flags & SQLiteConnectionFlags.LogCallbackException) == + SQLiteConnectionFlags.LogCallbackException) { - IntPtr nAux = _base.AggregateContext(context); - - if ((_contextDataList != null) && - !_contextDataList.TryGetValue(nAux, out data)) - { - data = new AggregateData(); - _contextDataList[nAux] = data; - } - } - - AggregateData newData = (data != null) ? data : new AggregateData(); - - try - { - _context = context; - _stepCallback(ConvertParams(nArgs, argsptr), - newData._count, ref newData._data); /* throw */ - } - finally - { - newData._count++; + SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( + "Caught exception in \"Step\" method: {1}", + e)); /* throw */ } } -#if !PLATFORM_COMPACTFRAMEWORK - catch (Exception e) /* NOTE: Must catch ALL. */ + catch { - try - { - if ((_flags & SQLiteConnectionFlags.LogCallbackException) == - SQLiteConnectionFlags.LogCallbackException) - { - SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( - "Caught exception in {0} callback: {1}", - typeof(SQLiteFunctionStepCallback), e)); /* throw */ - } - } - catch - { - // do nothing. - } + // do nothing. } + } #else - catch /* NOTE: Must catch ALL. */ - { - // do nothing (Windows CE). - } -#endif + catch /* NOTE: Must catch ALL. */ + { + // do nothing (Windows CE). } +#endif } /// /// An internal aggregate Final function callback, which wraps the context pointer and calls the virtual Final() method. - /// Does nothing if there is no final callback configured. WARNING: Must not throw exceptions. + /// WARNING: Must not throw exceptions. /// - /// A raw context pointer - internal void FinalCallback(IntPtr context) + /// A raw context pointer + internal void FinalCallback(IntPtr context) { - if (_finalCallback != null) + try + { + object obj = null; + + if (_base != null) + { + IntPtr n = _base.AggregateContext(context); + AggregateData aggData; + + if ((_contextDataList != null) && + _contextDataList.TryGetValue(n, out aggData)) + { + obj = aggData._data; + _contextDataList.Remove(n); + } + } + + try + { + _context = context; + SetReturnValue(context, Final(obj)); /* throw */ + } + finally + { + IDisposable disp = obj as IDisposable; + if (disp != null) disp.Dispose(); /* throw */ + } + } +#if !PLATFORM_COMPACTFRAMEWORK + catch (Exception e) /* NOTE: Must catch ALL. */ { try { - object obj = null; - - if (_base != null) + if ((_flags & SQLiteConnectionFlags.LogCallbackException) == + SQLiteConnectionFlags.LogCallbackException) { - IntPtr n = _base.AggregateContext(context); - AggregateData aggData; - - if ((_contextDataList != null) && - _contextDataList.TryGetValue(n, out aggData)) - { - obj = aggData._data; - _contextDataList.Remove(n); - } - } - - try - { - _context = context; - SetReturnValue(context, _finalCallback(obj)); /* throw */ - } - finally - { - IDisposable disp = obj as IDisposable; - if (disp != null) disp.Dispose(); /* throw */ + SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( + "Caught exception in \"Final\" method: {1}", + e)); /* throw */ } } -#if !PLATFORM_COMPACTFRAMEWORK - catch (Exception e) /* NOTE: Must catch ALL. */ + catch { - try - { - if ((_flags & SQLiteConnectionFlags.LogCallbackException) == - SQLiteConnectionFlags.LogCallbackException) - { - SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format( - "Caught exception in {0} callback: {1}", - typeof(SQLiteFunctionFinalCallback), e)); /* throw */ - } - } - catch - { - // do nothing. - } + // do nothing. } + } #else - catch /* NOTE: Must catch ALL. */ - { - // do nothing (Windows CE). - } -#endif + catch /* NOTE: Must catch ALL. */ + { + // do nothing (Windows CE). } +#endif } /// /// Using reflection, enumerate all assemblies in the current appdomain looking for classes that /// have a SQLiteFunctionAttribute attribute, and registering them accordingly. @@ -860,14 +747,10 @@ { f = (SQLiteFunction)Activator.CreateInstance(pr._instanceType); f._base = sqlbase; f._flags = flags; - f._invokeCallback = f.Invoke; - f._stepCallback = f.Step; - f._finalCallback = f.Final; - f._compareCallback = f.Compare; f._InvokeFunc = (pr.FuncType == FunctionType.Scalar) ? new SQLiteCallback(f.ScalarCallback) : null; f._StepFunc = (pr.FuncType == FunctionType.Aggregate) ? new SQLiteCallback(f.StepCallback) : null; f._FinalFunc = (pr.FuncType == FunctionType.Aggregate) ? new SQLiteFinalCallback(f.FinalCallback) : null; f._CompareFunc = (pr.FuncType == FunctionType.Collation) ? new SQLiteCollation(f.CompareCallback) : null; f._CompareFunc16 = (pr.FuncType == FunctionType.Collation) ? new SQLiteCollation(f.CompareCallback16) : null;