示例#1
0
        /// <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 */
            }
#if !PLATFORM_COMPACTFRAMEWORK
            catch (Exception e) /* NOTE: Must catch ALL. */
            {
                try
                {
                    if ((_flags & SQLiteConnectionFlags.LogCallbackException) ==
                        SQLiteConnectionFlags.LogCallbackException)
                    {
                        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).
            }
#endif
        }
示例#2
0
 /// <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.
         }
     }
 }
示例#3
0
        ///////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Will provide a <see cref="IServiceProvider" /> object in .NET 3.5.
        /// </summary>
        /// <param name="serviceType">The class or interface type to query for.</param>
        /// <returns></returns>
        object IServiceProvider.GetService(Type serviceType)
        {
            if (serviceType == typeof(ISQLiteSchemaExtensions) ||
                (_dbProviderServicesType != null && serviceType == _dbProviderServicesType))
            {
                object result = GetSQLiteProviderServicesInstance();

                if (SQLite3.ForceLogLifecycle())
                {
                    SQLiteLog.LogMessage(HelperMethods.StringFormat(
                                             CultureInfo.CurrentCulture,
                                             "Success of \"{0}\" from SQLiteFactory.GetService(\"{1}\")...",
                                             (result != null) ? result.ToString() : "<null>",
                                             (serviceType != null) ? serviceType.ToString() : "<null>"));
                }

                return(result);
            }

            if (SQLite3.ForceLogLifecycle())
            {
                SQLiteLog.LogMessage(HelperMethods.StringFormat(
                                         CultureInfo.CurrentCulture,
                                         "Failure of SQLiteFactory.GetService(\"{0}\")...",
                                         (serviceType != null) ? serviceType.ToString() : "<null>"));
            }

            return(null);
        }
示例#4
0
        /// <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++;
                }
            }
#if !PLATFORM_COMPACTFRAMEWORK
            catch (Exception e) /* NOTE: Must catch ALL. */
            {
                try
                {
                    if ((_flags & SQLiteConnectionFlags.LogCallbackException) ==
                        SQLiteConnectionFlags.LogCallbackException)
                    {
                        SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format(
                                                 CultureInfo.CurrentCulture,
                                                 "Caught exception in \"Step\" method: {1}",
                                                 e)); /* throw */
                    }
                }
                catch
                {
                    // do nothing.
                }
            }
#else
            catch /* NOTE: Must catch ALL. */
            {
                // do nothing (Windows CE).
            }
#endif
        }
示例#5
0
        /// <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 */
                    }
                }
            }
#if !PLATFORM_COMPACTFRAMEWORK
            catch (Exception e) /* NOTE: Must catch ALL. */
            {
                try
                {
                    if ((_flags & SQLiteConnectionFlags.LogCallbackException) ==
                        SQLiteConnectionFlags.LogCallbackException)
                    {
                        SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format(
                                                 CultureInfo.CurrentCulture,
                                                 "Caught exception in \"Final\" method: {1}",
                                                 e)); /* throw */
                    }
                }
                catch
                {
                    // do nothing.
                }
            }
#else
            catch /* NOTE: Must catch ALL. */
            {
                // do nothing (Windows CE).
            }
#endif
        }
示例#6
0
        /// <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 */
            }
#if !PLATFORM_COMPACTFRAMEWORK
            catch (Exception e) /* NOTE: Must catch ALL. */
            {
                try
                {
                    if ((_flags & SQLiteConnectionFlags.LogCallbackException) ==
                        SQLiteConnectionFlags.LogCallbackException)
                    {
                        SQLiteLog.LogMessage(COR_E_EXCEPTION, String.Format(
                                                 CultureInfo.CurrentCulture,
                                                 "Caught exception in \"Compare\" (UTF16) method: {0}",
                                                 e)); /* throw */
                    }
                }
                catch
                {
                    // do nothing.
                }
            }
#else
            catch /* NOTE: Must catch ALL. */
            {
                // do nothing (Windows CE).
            }
#endif

            //
            // NOTE: This must be done to prevent the core SQLite library from
            //       using our (invalid) result.
            //
            if (_base != null)
            {
                _base.Cancel();
            }

            return(0);
        }
示例#7
0
        /// <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 CompareCallback(IntPtr ptr, int len1, IntPtr ptr1, int len2, IntPtr ptr2)
        {
            try
            {
                return(Compare(SQLiteConvert.UTF8ToString(ptr1, len1),
                               SQLiteConvert.UTF8ToString(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\" (UTF8) 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);
        }
示例#8
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");
            }

            if (HelperMethods.HasFlags(
                    _flags, SQLiteConnectionFlags.UseConnectionBindValueCallbacks))
            {
                bool complete;

                InvokeBindValueCallback(index, param, out complete);

                if (complete)
                {
                    return;
                }
            }

            object obj     = param.Value;
            DbType objType = param.DbType;

            if ((obj != null) && (objType == DbType.Object))
            {
                objType = SQLiteConvert.TypeToDbType(obj.GetType());
            }

            if (SQLite3.ForceLogPrepare() || HelperMethods.LogPreBind(_flags))
            {
                IntPtr handle = _sqlite_stmt;

                SQLiteLog.LogMessage(HelperMethods.StringFormat(
                                         CultureInfo.CurrentCulture,
                                         "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 = HelperMethods.HasFlags(
                _flags, SQLiteConnectionFlags.BindInvariantText);

            CultureInfo cultureInfo = CultureInfo.CurrentCulture;

            if (HelperMethods.HasFlags(
                    _flags, SQLiteConnectionFlags.ConvertInvariantText))
            {
                cultureInfo = invariantCultureInfo;
            }

            if (HelperMethods.HasFlags(
                    _flags, 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) :
                                   SQLiteConvert.ToStringWithProvider(obj, cultureInfo));
                }

                return;
            }

            bool invariantDecimal = HelperMethods.HasFlags(
                _flags, SQLiteConnectionFlags.BindInvariantDecimal);

            if (HelperMethods.HasFlags(
                    _flags, SQLiteConnectionFlags.BindDecimalAsText))
            {
                if (obj is Decimal)
                {
                    _sql.Bind_Text(this, _flags, index, invariantText || invariantDecimal ?
                                   SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) :
                                   SQLiteConvert.ToStringWithProvider(obj, cultureInfo));

                    return;
                }
            }

            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_Boolean(this, _flags, index, SQLiteConvert.ToBoolean(obj, cultureInfo, true));
                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:
                _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) :
                                   SQLiteConvert.ToStringWithProvider(obj, cultureInfo));
                }
                break;

            case DbType.Decimal:     // Dont store decimal as double ... loses precision
                _sql.Bind_Text(this, _flags, index, invariantText || invariantDecimal ?
                               SQLiteConvert.ToStringWithProvider(Convert.ToDecimal(obj, cultureInfo), invariantCultureInfo) :
                               SQLiteConvert.ToStringWithProvider(Convert.ToDecimal(obj, cultureInfo), cultureInfo));
                break;

            default:
                _sql.Bind_Text(this, _flags, index, invariantText ?
                               SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) :
                               SQLiteConvert.ToStringWithProvider(obj, cultureInfo));
                break;
            }
        }
示例#9
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;
            }

            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, obj.ToString());
                }

                return;
            }

            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.CurrentCulture));
                _sql.Bind_DateTime(this, _flags, index, (obj is string) ?
                                   _sql.ToDateTime((string)obj) : Convert.ToDateTime(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.Boolean:
                _sql.Bind_Int32(this, _flags, index, ToBoolean(obj, CultureInfo.CurrentCulture) ? 1 : 0);
                break;

            case DbType.SByte:
                _sql.Bind_Int32(this, _flags, index, Convert.ToSByte(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.Int16:
                _sql.Bind_Int32(this, _flags, index, Convert.ToInt16(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.Int32:
                _sql.Bind_Int32(this, _flags, index, Convert.ToInt32(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.Int64:
                _sql.Bind_Int64(this, _flags, index, Convert.ToInt64(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.Byte:
                _sql.Bind_UInt32(this, _flags, index, Convert.ToByte(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.UInt16:
                _sql.Bind_UInt32(this, _flags, index, Convert.ToUInt16(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.UInt32:
                _sql.Bind_UInt32(this, _flags, index, Convert.ToUInt32(obj, CultureInfo.CurrentCulture));
                break;

            case DbType.UInt64:
                _sql.Bind_UInt64(this, _flags, index, Convert.ToUInt64(obj, CultureInfo.CurrentCulture));
                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.CurrentCulture));
                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, obj.ToString());
                }

                break;

            case DbType.Decimal: // Dont store decimal as double ... loses precision
                _sql.Bind_Text(this, _flags, index, Convert.ToDecimal(obj, CultureInfo.CurrentCulture).ToString(CultureInfo.InvariantCulture));
                break;

            default:
                _sql.Bind_Text(this, _flags, index, obj.ToString());
                break;
            }
        }