/// <summary> /// Internal scalar callback function, which wraps the raw context pointer and calls the virtual Invoke() method. /// WARNING: Must not throw exceptions. /// </summary> /// <param name="context">A raw context pointer</param> /// <param name="nArgs">Number of arguments passed in</param> /// <param name="argsptr">A pointer to the array of arguments</param> internal void ScalarCallback(IntPtr context, int nArgs, IntPtr argsptr) { try { _context = context; SetReturnValue(context, Invoke(ConvertParams(nArgs, argsptr))); /* throw */ } catch (Exception e) /* NOTE: Must catch ALL. */ { try { if ((_flags & SQLiteConnectionFlags.LogCallbackException) == SQLiteConnectionFlags.LogCallbackException) { SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION, String.Format(CultureInfo.CurrentCulture, "Caught exception in \"Invoke\" method: {0}", e)); /* throw */ } } catch { // do nothing. } } }
static DenverDBFactory() { #if (SQLITE_STANDARD || USE_INTEROP_DLL || PLATFORM_COMPACTFRAMEWORK) && PRELOAD_NATIVE_LIBRARY UnsafeNativeMethods.Initialize(); #endif #if INTEROP_LOG if (UnsafeNativeMethods.sqlite3_config_log_interop() == SQLiteErrorCode.Ok) { UnsafeNativeMethods.sqlite3_log( SQLiteErrorCode.Ok, SQLiteConvert.ToUTF8("logging initialized.")); } #endif SQLiteLog.Initialize(); string version = #if NET_40 || NET_45 || NET_451 "4.0.0.0"; #else "3.5.0.0"; #endif _dbProviderServicesType = Type.GetType(String.Format(CultureInfo.InvariantCulture, "System.Data.Common.DbProviderServices, System.Data.Entity, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089", version), false); }
/// <summary> /// The internal aggregate Step function callback, which wraps the raw context pointer and calls the virtual Step() method. /// WARNING: Must not throw exceptions. /// </summary> /// <remarks> /// 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. /// </remarks> /// <param name="context">A raw context pointer</param> /// <param name="nArgs">Number of arguments passed in</param> /// <param name="argsptr">A pointer to the array of arguments</param> internal void StepCallback(IntPtr context, int nArgs, IntPtr argsptr) { 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++; } } catch (Exception e) /* NOTE: Must catch ALL. */ { try { if ((_flags & SQLiteConnectionFlags.LogCallbackException) == SQLiteConnectionFlags.LogCallbackException) { SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION, String.Format(CultureInfo.CurrentCulture, "Caught exception in \"Step\" method: {1}", e)); /* throw */ } } catch { // do nothing. } } }
/// <summary> /// An internal aggregate Final function callback, which wraps the context pointer and calls the virtual Final() method. /// WARNING: Must not throw exceptions. /// </summary> /// <param name="context">A raw context pointer</param> internal void FinalCallback(IntPtr context) { 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 */ } } } catch (Exception e) /* NOTE: Must catch ALL. */ { try { if ((_flags & SQLiteConnectionFlags.LogCallbackException) == SQLiteConnectionFlags.LogCallbackException) { SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION, String.Format(CultureInfo.CurrentCulture, "Caught exception in \"Final\" method: {1}", e)); /* throw */ } } catch { // do nothing. } } }
/// <summary> /// Internal collation sequence function, which wraps up the raw string pointers and executes the Compare() virtual function. /// WARNING: Must not throw exceptions. /// </summary> /// <param name="ptr">Not used</param> /// <param name="len1">Length of the string pv1</param> /// <param name="ptr1">Pointer to the first string to compare</param> /// <param name="len2">Length of the string pv2</param> /// <param name="ptr2">Pointer to the second string to compare</param> /// <returns>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 an exception is caught.</returns> internal int CompareCallback16(IntPtr ptr, int len1, IntPtr ptr1, int len2, IntPtr ptr2) { try { return(Compare(SQLite3_UTF16.UTF16ToString(ptr1, len1), SQLite3_UTF16.UTF16ToString(ptr2, len2))); /* throw */ } catch (Exception e) /* NOTE: Must catch ALL. */ { try { if ((_flags & SQLiteConnectionFlags.LogCallbackException) == SQLiteConnectionFlags.LogCallbackException) { SQLiteLog.LogMessage(SQLiteBase.COR_E_EXCEPTION, String.Format(CultureInfo.CurrentCulture, "Caught exception in \"Compare\" (UTF16) method: {0}", e)); /* throw */ } } catch { // do nothing. } } // // NOTE: This must be done to prevent the core SQLite library from // using our (invalid) result. // if ((_base != null) && _base.IsOpen()) { _base.Cancel(); } return(0); }
/// <summary> /// Perform the bind operation for an individual parameter /// </summary> /// <param name="index">The index of the parameter to bind</param> /// <param name="param">The parameter we're binding</param> private void BindParameter(int index, SQLiteParameter param) { if (param == null) { throw new SQLiteException("Insufficient parameters supplied to the command"); } object obj = param.Value; DbType objType = param.DbType; if ((obj != null) && (objType == DbType.Object)) { objType = SQLiteConvert.TypeToDbType(obj.GetType()); } if ((_flags & SQLiteConnectionFlags.LogPreBind) == SQLiteConnectionFlags.LogPreBind) { IntPtr handle = _sqlite_stmt; SQLiteLog.LogMessage(String.Format( "Binding statement {0} paramter #{1} with database type {2} and raw value {{{3}}}...", handle, index, objType, obj)); } if ((obj == null) || Convert.IsDBNull(obj)) { _sql.Bind_Null(this, _flags, index); return; } CultureInfo invariantCultureInfo = CultureInfo.InvariantCulture; bool invariantText = ((_flags & SQLiteConnectionFlags.BindInvariantText) == SQLiteConnectionFlags.BindInvariantText); if ((_flags & SQLiteConnectionFlags.BindAllAsText) == SQLiteConnectionFlags.BindAllAsText) { if (obj is DateTime) { _sql.Bind_DateTime(this, _flags, index, (DateTime)obj); } else { _sql.Bind_Text(this, _flags, index, invariantText ? SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) : obj.ToString()); } return; } CultureInfo cultureInfo = CultureInfo.CurrentCulture; if ((_flags & SQLiteConnectionFlags.ConvertInvariantText) == SQLiteConnectionFlags.ConvertInvariantText) { cultureInfo = invariantCultureInfo; } switch (objType) { case DbType.Date: case DbType.Time: case DbType.DateTime: // // NOTE: The old method (commented below) does not honor the selected date format // for the connection. // _sql.Bind_DateTime(this, index, Convert.ToDateTime(obj, cultureInfo)); _sql.Bind_DateTime(this, _flags, index, (obj is string) ? _sql.ToDateTime((string)obj) : Convert.ToDateTime(obj, cultureInfo)); break; case DbType.Boolean: _sql.Bind_Int32(this, _flags, index, SQLiteConvert.ToBoolean(obj, cultureInfo, true) ? 1 : 0); break; case DbType.SByte: _sql.Bind_Int32(this, _flags, index, Convert.ToSByte(obj, cultureInfo)); break; case DbType.Int16: _sql.Bind_Int32(this, _flags, index, Convert.ToInt16(obj, cultureInfo)); break; case DbType.Int32: _sql.Bind_Int32(this, _flags, index, Convert.ToInt32(obj, cultureInfo)); break; case DbType.Int64: _sql.Bind_Int64(this, _flags, index, Convert.ToInt64(obj, cultureInfo)); break; case DbType.Byte: _sql.Bind_UInt32(this, _flags, index, Convert.ToByte(obj, cultureInfo)); break; case DbType.UInt16: _sql.Bind_UInt32(this, _flags, index, Convert.ToUInt16(obj, cultureInfo)); break; case DbType.UInt32: _sql.Bind_UInt32(this, _flags, index, Convert.ToUInt32(obj, cultureInfo)); break; case DbType.UInt64: _sql.Bind_UInt64(this, _flags, index, Convert.ToUInt64(obj, cultureInfo)); break; case DbType.Single: case DbType.Double: case DbType.Currency: //case DbType.Decimal: // Dont store decimal as double ... loses precision _sql.Bind_Double(this, _flags, index, Convert.ToDouble(obj, cultureInfo)); break; case DbType.Binary: _sql.Bind_Blob(this, _flags, index, (byte[])obj); break; case DbType.Guid: if (_command.Connection._binaryGuid == true) { _sql.Bind_Blob(this, _flags, index, ((Guid)obj).ToByteArray()); } else { _sql.Bind_Text(this, _flags, index, invariantText ? SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) : obj.ToString()); } break; case DbType.Decimal: // Dont store decimal as double ... loses precision _sql.Bind_Text(this, _flags, index, Convert.ToDecimal(obj, cultureInfo).ToString(invariantCultureInfo)); break; default: _sql.Bind_Text(this, _flags, index, invariantText ? SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) : obj.ToString()); break; } }