Exemplo n.º 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;
            }
        }
Exemplo n.º 2
0
        internal static void SetResult(this sqlite3_context ctx, ISQLiteValue value)
        {
            switch (value.SQLiteType)
            {
            case SQLiteType.Blob:
                if (value is ZeroBlob)
                {
                    raw.sqlite3_result_zeroblob(ctx, value.Length);
                }
                else
                {
                    raw.sqlite3_result_blob(ctx, value.ToBlob());
                }
                return;

            case SQLiteType.Null:
                raw.sqlite3_result_null(ctx);
                return;

            case SQLiteType.Text:
                raw.sqlite3_result_text(ctx, value.ToString());
                return;

            case SQLiteType.Float:
                raw.sqlite3_result_double(ctx, value.ToDouble());
                return;

            case SQLiteType.Integer:
                raw.sqlite3_result_int64(ctx, value.ToInt64());
                return;
            }
        }
        /// <summary>
        /// Returns the SQLiteValue as a <see cref="Guid"/>.
        /// </summary>
        public static Guid ToGuid(this ISQLiteValue This)
        {
            Contract.Requires(This != null);

            if (!Utf8Parser.TryParse(This.ToBlob(), out Guid value, out _))
            {
                ThrowHelper.ThrowFormatException();
            }

            return(value);
        }
        private void compare(ISQLiteValue expected, ISQLiteValue test)
        {
            Assert.Equal(test.Length, expected.Length);
            Assert.Equal(test.SQLiteType, expected.SQLiteType);

            Assert.Equal(test.ToDouble(), expected.ToDouble(), 15);
            Assert.Equal(test.ToInt64(), expected.ToInt64());
            Assert.Equal(test.ToInt(), expected.ToInt());
            Assert.Equal(test.ToString(), expected.ToString());
            Assert.Equal(test.ToBlob().ToArray(), expected.ToBlob().ToArray());
        }
Exemplo n.º 5
0
        private void compare(ISQLiteValue expected, ISQLiteValue test)
        {
            Assert.Equal(expected.Length, test.Length);
            Assert.Equal(expected.SQLiteType, test.SQLiteType);

            // FIXME: Testing double equality is imprecise
            //Assert.Equal(expected.ToDouble(), test.ToDouble());
            Assert.Equal(expected.ToInt64(), test.ToInt64());
            Assert.Equal(expected.ToInt(), test.ToInt());
            Assert.Equal(expected.ToString(), test.ToString());
            Assert.Equal(expected.ToBlob(), test.ToBlob());
        }
        private void compare(ISQLiteValue expected, ISQLiteValue test)
        {
            Assert.Equal(expected.Length, test.Length);
            Assert.Equal(expected.SQLiteType, test.SQLiteType);

            // FIXME: Testing double equality is imprecise
            //Assert.Equal(expected.ToDouble(), test.ToDouble());
            Assert.Equal(expected.ToInt64(), test.ToInt64());
            Assert.Equal(expected.ToInt(), test.ToInt());
            Assert.Equal(expected.ToString(), test.ToString());
            Assert.Equal(expected.ToBlob(), test.ToBlob());
        }
Exemplo n.º 7
0
        internal static void SetResult(this sqlite3_context ctx, ISQLiteValue value)
        {
            switch (value.SQLiteType)
            {
                case SQLiteType.Blob:
                    if (value is ZeroBlob)
                    {
                        raw.sqlite3_result_zeroblob(ctx, value.Length);
                    }
                    else
                    {
                        raw.sqlite3_result_blob(ctx, value.ToBlob());
                    }
                    return;

                case SQLiteType.Null:
                    raw.sqlite3_result_null(ctx);
                    return;

                case SQLiteType.Text:
                    raw.sqlite3_result_text(ctx, value.ToString());
                    return;

                case SQLiteType.Float:
                    raw.sqlite3_result_double(ctx, value.ToDouble());
                    return;

                case SQLiteType.Integer:
                    raw.sqlite3_result_int64(ctx, value.ToInt64());
                    return;
            }
        }
Exemplo n.º 8
0
        private static object ToObject(this ISQLiteValue value, Type clrType)
        {
            // Just in case the clrType is nullable
            clrType = clrType.GetUnderlyingType();

            if (value.SQLiteType == SQLiteType.Null)
            {
                return(null);
            }
            else if (clrType == typeof(String))
            {
                return(value.ToString());
            }
            else if (clrType == typeof(Int32))
            {
                return(value.ToInt());
            }
            else if (clrType == typeof(Boolean))
            {
                return(value.ToBool());
            }
            else if (clrType == typeof(double))
            {
                return(value.ToDouble());
            }
            else if (clrType == typeof(float))
            {
                return(value.ToFloat());
            }
            else if (clrType == typeof(TimeSpan))
            {
                return(value.ToTimeSpan());
            }
            else if (clrType == typeof(DateTime))
            {
                return(value.ToDateTime());
            }
            else if (clrType == typeof(DateTimeOffset))
            {
                return(value.ToDateTimeOffset());
            }
            else if (clrType.GetTypeInfo().IsEnum)
            {
                return(value.ToInt());
            }
            else if (clrType == typeof(Int64))
            {
                return(value.ToInt64());
            }
            else if (clrType == typeof(UInt32))
            {
                return(value.ToUInt32());
            }
            else if (clrType == typeof(decimal))
            {
                return(value.ToDecimal());
            }
            else if (clrType == typeof(Byte))
            {
                return(value.ToByte());
            }
            else if (clrType == typeof(UInt16))
            {
                return(value.ToUInt16());
            }
            else if (clrType == typeof(Int16))
            {
                return(value.ToShort());
            }
            else if (clrType == typeof(sbyte))
            {
                return(value.ToSByte());
            }
            else if (clrType == typeof(byte[]))
            {
                return(value.ToBlob());
            }
            else if (clrType == typeof(Guid))
            {
                return(value.ToGuid());
            }
            else if (clrType == typeof(Uri))
            {
                return(value.ToUri());
            }
            else
            {
                throw new NotSupportedException($"Don't know how to read {clrType}");
            }
        }
Exemplo n.º 9
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;
            }
        }