Beispiel #1
0
        /// <summary>
        /// Converts an IntPtr array of context arguments to an object array containing the resolved parameters the pointers point to.
        /// </summary>
        /// <remarks>
        /// Parameters passed to functions have only an affinity for a certain data type, there is no underlying schema available
        /// to force them into a certain type.  Therefore the only types you will ever see as parameters are
        /// DBNull.Value, Int64, Double, String or byte[] array.
        /// </remarks>
        /// <param name="nArgs">The number of arguments</param>
        /// <param name="argsptr">A pointer to the array of arguments</param>
        /// <returns>An object array of the arguments once they've been converted to .NET values</returns>
        internal object[] ConvertParams(int nArgs, IntPtr argsptr)
        {
            object[] parms = new object[nArgs];
#if !PLATFORM_COMPACTFRAMEWORK
            IntPtr[] argint = new IntPtr[nArgs];
#else
            int[] argint = new int[nArgs];
#endif
            Marshal.Copy(argsptr, argint, 0, nArgs);

            for (int n = 0; n < nArgs; n++)
            {
                switch (_base.GetParamValueType((IntPtr)argint[n]))
                {
                case TypeAffinity.Null:
                    parms[n] = DBNull.Value;
                    break;

                case TypeAffinity.Int64:
                    parms[n] = _base.GetParamValueInt64((IntPtr)argint[n]);
                    break;

                case TypeAffinity.Double:
                    parms[n] = _base.GetParamValueDouble((IntPtr)argint[n]);
                    break;

                case TypeAffinity.Text:
                    parms[n] = _base.GetParamValueText((IntPtr)argint[n]);
                    break;

                case TypeAffinity.Blob:
                {
                    int    x;
                    byte[] blob;

                    x    = (int)_base.GetParamValueBytes((IntPtr)argint[n], 0, null, 0, 0);
                    blob = new byte[x];
                    _base.GetParamValueBytes((IntPtr)argint[n], 0, blob, 0, x);
                    parms[n] = blob;
                }
                break;

                case TypeAffinity.DateTime: // Never happens here but what the heck, maybe it will one day.
                    parms[n] = _base.ToDateTime(_base.GetParamValueText((IntPtr)argint[n]));
                    break;
                }
            }
            return(parms);
        }
Beispiel #2
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;
            }
        }
Beispiel #3
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;
            }
        }