示例#1
0
        /// <summary>
        /// Bind the parameter to an <see cref="ISQLiteValue"/>.
        /// </summary>
        /// <param name="This">The bind parameter.</param>
        /// <param name="value">A <see cref="ISQLiteValue"/>.</param>
        public static void Bind(this IBindParameter This, ISQLiteValue value)
        {
            Contract.Requires(This != null);
            Contract.Requires(value != null);

            switch (value.SQLiteType)
            {
            case SQLiteType.Blob:
                if (value is ZeroBlob)
                {
                    This.BindZeroBlob(value.Length);
                }
                else
                {
                    This.Bind(value.ToBlob());
                }
                return;

            case SQLiteType.Null:
                This.BindNull();
                return;

            case SQLiteType.Text:
                This.Bind(value.ToString());
                return;

            case SQLiteType.Float:
                This.Bind(value.ToDouble());
                return;

            case SQLiteType.Integer:
                This.Bind(value.ToInt64());
                return;
            }
        }
        public static void TryBind(this IStatement statement, int index, bool?value)
        {
            IBindParameter bindParam = statement.BindParameters[index];

            if (value == null)
            {
                bindParam.BindNull();
            }
            else
            {
                bindParam.Bind(value.Value);
            }
        }
        public static void TryBind(this IStatement statement, int index, string value)
        {
            IBindParameter bindParam = statement.BindParameters[index];

            if (value == null)
            {
                bindParam.BindNull();
            }
            else
            {
                bindParam.Bind(value.AsSpan());
            }
        }
示例#4
0
        /// <summary>
        /// Bind the parameter to a value based upon its runtime type.
        /// </summary>
        /// <param name="This">The bind parameter.</param>
        /// <param name="obj">
        /// An object that is either <see langword="null"/> or any numeric type, <see cref="string"/>,
        /// byte[], <see cref="ISQLiteValue"/> or <see cref="Stream"/>.
        /// </param>
        /// <exception cref="ArgumentException">
        /// If the <see cref="Type"/> of the value is not supported
        /// -or-
        /// A non-readable stream is provided as a value.
        /// </exception>
        public static void Bind(this IBindParameter This, object obj)
        {
            Contract.Requires(This != null);

            // I miss F# pattern matching
            if (obj == null)
            {
                This.BindNull();
                return;
            }

            Type t = obj.GetType();

            if (typeof(string) == t)
            {
                This.Bind((string)obj);
            }
            else if (
                (typeof(Int32) == t) ||
                (typeof(Boolean) == t) ||
                (typeof(Byte) == t) ||
                (typeof(UInt16) == t) ||
                (typeof(Int16) == t) ||
                (typeof(sbyte) == t) ||
                (typeof(Int64) == t) ||
                (typeof(UInt32) == t))
            {
                This.Bind((long)Convert.ChangeType(obj, typeof(long)));
            }
            else if ((typeof(double) == t) || (typeof(float) == t) || (typeof(decimal) == t))
            {
                This.Bind((double)Convert.ChangeType(obj, typeof(double)));
            }
            else if (typeof(byte[]) == t)
            {
                This.Bind((byte[])obj);
            }
            else if (t.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISQLiteValue)))
            {
                This.Bind((ISQLiteValue)obj);
            }
            else if (obj is TimeSpan span)
            {
                This.Bind(span);
            }
            else if (obj is DateTime time)
            {
                This.Bind(time);
            }
            else if (obj is DateTimeOffset offset)
            {
                This.Bind(offset);
            }
            else if (obj is Guid guid)
            {
                This.Bind(guid);
            }
            else if (obj is Stream stream)
            {
                This.Bind(stream);
            }
            else if (obj is Uri uri)
            {
                This.Bind(uri);
            }
            else
            {
                ThrowHelper.ThrowArgumentException($"Invalid type conversion {t}");
            }
        }
        public static void TryBindNull(this IStatement statement, int index)
        {
            IBindParameter bindParam = statement.BindParameters[index];

            bindParam.BindNull();
        }