Example #1
0
        public static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer)
        {
            Type clrType    = p.ColumnType;
            var  interfaces = clrType.GetTypeInfo().ImplementedInterfaces.ToList();

            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) ||
                clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32) ||
                interfaces.Contains(typeof(ISerializable <Boolean>)) ||
                interfaces.Contains(typeof(ISerializable <Byte>)) ||
                interfaces.Contains(typeof(ISerializable <UInt16>)) ||
                interfaces.Contains(typeof(ISerializable <SByte>)) ||
                interfaces.Contains(typeof(ISerializable <Int16>)) ||
                interfaces.Contains(typeof(ISerializable <Int32>)))
            {
                return("integer");
            }
            if (clrType == typeof(UInt32) || clrType == typeof(Int64) ||
                interfaces.Contains(typeof(ISerializable <UInt32>)) ||
                interfaces.Contains(typeof(ISerializable <Int64>)))
            {
                return("bigint");
            }
            if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal) ||
                interfaces.Contains(typeof(ISerializable <Single>)) ||
                interfaces.Contains(typeof(ISerializable <Double>)) ||
                interfaces.Contains(typeof(ISerializable <Decimal>)))
            {
                return("float");
            }
            if (clrType == typeof(String) || interfaces.Contains(typeof(ISerializable <String>)))
            {
                int len = p.MaxStringLength;
                return("varchar(" + len + ")");
            }
            if (clrType == typeof(TimeSpan) || interfaces.Contains(typeof(ISerializable <TimeSpan>)))
            {
                return("bigint");
            }
            if (clrType == typeof(DateTime) || interfaces.Contains(typeof(ISerializable <DateTime>)))
            {
                return(storeDateTimeAsTicks ? "bigint" : "datetime");
            }
            if (clrType.GetTypeInfo().IsEnum)
            {
                return("integer");
            }
            if (clrType == typeof(byte[]) || interfaces.Contains(typeof(ISerializable <byte[]>)))
            {
                return("blob");
            }
            if (clrType == typeof(Guid) || interfaces.Contains(typeof(ISerializable <Guid>)))
            {
                return("varchar(36)");
            }
            if (serializer != null && serializer.CanDeserialize(clrType))
            {
                return("blob");
            }
            throw new NotSupportedException("Don't know about " + clrType);
        }
        public void BlobSerializerExtensionsAsBlobSerializerTest()
        {
            IBlobSerializer blobDelegate = BlobSerializerExtensions.AsBlobSerializer(new byteSerilizer());

            Assert.IsTrue(blobDelegate.CanDeserialize(typeof(string)));
            byte[] myBytes = { 0xaa, 0xbb };
            Assert.AreEqual((new byteSerilizer()).SerializeToBytes(myBytes), blobDelegate.Serialize <string>("nothing"));
            Assert.AreEqual((new byteSerilizer()).Deserialize(myBytes, typeof(string)), blobDelegate.Deserialize(myBytes, typeof(string)));
        }
Example #3
0
        public static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer)
        {
            Type clrType = p.ColumnType;

            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) ||
                clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32))
            {
                return("integer");
            }
            if (clrType == typeof(UInt32) || clrType == typeof(Int64))
            {
                return("bigint");
            }
            if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal))
            {
                return("float");
            }
            if (clrType == typeof(String))
            {
                int len = p.MaxStringLength;
                return("varchar(" + len + ")");
            }
            if (clrType == typeof(TimeSpan))
            {
                return("bigint");
            }
            if (clrType == typeof(DateTime))
            {
                return(storeDateTimeAsTicks ? "bigint" : "datetime");
            }
            if (clrType.IsEnum)
            {
                return("integer");
            }
            if (clrType == typeof(byte[]))
            {
                return("blob");
            }
            if (clrType == typeof(Guid))
            {
                return("varchar(36)");
            }
            if (serializer != null && serializer.CanDeserialize(clrType))
            {
                return("blob");
            }
            throw new NotSupportedException("Don't know about " + clrType);
        }
Example #4
0
 internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, object value, bool storeDateTimeAsTicks,
                                    IBlobSerializer serializer)
 {
     if (value == null)
     {
         isqLite3Api.BindNull(stmt, index);
     }
     else
     {
         if (value is int)
         {
             isqLite3Api.BindInt(stmt, index, (int)value);
         }
         else if (value is ISerializable <int> )
         {
             isqLite3Api.BindInt(stmt, index, ((ISerializable <int>)value).Serialize());
         }
         else if (value is string)
         {
             isqLite3Api.BindText16(stmt, index, (string)value, -1, NegativePointer);
         }
         else if (value is ISerializable <string> )
         {
             isqLite3Api.BindText16(stmt, index, ((ISerializable <string>)value).Serialize(), -1, NegativePointer);
         }
         else if (value is byte || value is ushort || value is sbyte || value is short)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(value));
         }
         else if (value is ISerializable <byte> )
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable <byte>)value).Serialize()));
         }
         else if (value is ISerializable <ushort> )
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable <ushort>)value).Serialize()));
         }
         else if (value is ISerializable <sbyte> )
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable <sbyte>)value).Serialize()));
         }
         else if (value is ISerializable <short> )
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable <short>)value).Serialize()));
         }
         else if (value is bool)
         {
             isqLite3Api.BindInt(stmt, index, (bool)value ? 1 : 0);
         }
         else if (value is ISerializable <bool> )
         {
             isqLite3Api.BindInt(stmt, index, ((ISerializable <bool>)value).Serialize() ? 1 : 0);
         }
         else if (value is uint || value is long)
         {
             isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(value));
         }
         else if (value is ISerializable <uint> )
         {
             isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable <uint>)value).Serialize()));
         }
         else if (value is ISerializable <long> )
         {
             isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable <long>)value).Serialize()));
         }
         else if (value is float || value is double || value is decimal)
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(value));
         }
         else if (value is ISerializable <float> )
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable <float>)value).Serialize()));
         }
         else if (value is ISerializable <double> )
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable <double>)value).Serialize()));
         }
         else if (value is ISerializable <decimal> )
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable <decimal>)value).Serialize()));
         }
         else if (value is TimeSpan)
         {
             isqLite3Api.BindInt64(stmt, index, ((TimeSpan)value).Ticks);
         }
         else if (value is ISerializable <TimeSpan> )
         {
             isqLite3Api.BindInt64(stmt, index, ((ISerializable <TimeSpan>)value).Serialize().Ticks);
         }
         else if (value is DateTime)
         {
             if (storeDateTimeAsTicks)
             {
                 isqLite3Api.BindInt64(stmt, index, ((DateTime)value).ToUniversalTime().Ticks);
             }
             else
             {
                 isqLite3Api.BindText16(stmt, index, ((DateTime)value).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ"), -1, NegativePointer);
             }
         }
         else if (value is DateTimeOffset)
         {
             isqLite3Api.BindInt64(stmt, index, ((DateTimeOffset)value).UtcTicks);
         }
         else if (value is ISerializable <DateTime> )
         {
             if (storeDateTimeAsTicks)
             {
                 isqLite3Api.BindInt64(stmt, index, ((ISerializable <DateTime>)value).Serialize().ToUniversalTime().Ticks);
             }
             else
             {
                 isqLite3Api.BindText16(stmt, index,
                                        ((ISerializable <DateTime>)value).Serialize().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ"), -1, NegativePointer);
             }
         }
         else if (value.GetType().GetTypeInfo().IsEnum)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(value));
         }
         else if (value is byte[])
         {
             isqLite3Api.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer);
         }
         else if (value is ISerializable <byte[]> )
         {
             isqLite3Api.BindBlob(stmt, index, ((ISerializable <byte[]>)value).Serialize(), ((ISerializable <byte[]>)value).Serialize().Length,
                                  NegativePointer);
         }
         else if (value is Guid)
         {
             isqLite3Api.BindText16(stmt, index, ((Guid)value).ToString(), 72, NegativePointer);
         }
         else if (value is ISerializable <Guid> )
         {
             isqLite3Api.BindText16(stmt, index, ((ISerializable <Guid>)value).Serialize().ToString(), 72, NegativePointer);
         }
         else if (serializer != null && serializer.CanDeserialize(value.GetType()))
         {
             var bytes = serializer.Serialize(value);
             isqLite3Api.BindBlob(stmt, index, bytes, bytes.Length, NegativePointer);
         }
         else
         {
             throw new NotSupportedException("Cannot store type: " + value.GetType());
         }
     }
 }
Example #5
0
        private static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks,
                                      IBlobSerializer serializer,
                                      IDictionary <Type, string> extraTypeMappings)
        {
            var clrType    = p.ColumnType;
            var interfaces = clrType.GetTypeInfo().ImplementedInterfaces.ToList();

            string extraMapping;

            if (extraTypeMappings.TryGetValue(clrType, out extraMapping))
            {
                return(extraMapping);
            }

            if (clrType == typeof(bool) || clrType == typeof(byte) || clrType == typeof(ushort) ||
                clrType == typeof(sbyte) || clrType == typeof(short) || clrType == typeof(int) ||
                clrType == typeof(uint) || clrType == typeof(long) ||
                interfaces.Contains(typeof(ISerializable <bool>)) ||
                interfaces.Contains(typeof(ISerializable <byte>)) ||
                interfaces.Contains(typeof(ISerializable <ushort>)) ||
                interfaces.Contains(typeof(ISerializable <sbyte>)) ||
                interfaces.Contains(typeof(ISerializable <short>)) ||
                interfaces.Contains(typeof(ISerializable <int>)) ||
                interfaces.Contains(typeof(ISerializable <uint>)) ||
                interfaces.Contains(typeof(ISerializable <long>)) ||
                interfaces.Contains(typeof(ISerializable <ulong>)))
            {
                return("integer");
            }
            if (clrType == typeof(float) || clrType == typeof(double) || clrType == typeof(decimal) ||
                interfaces.Contains(typeof(ISerializable <float>)) ||
                interfaces.Contains(typeof(ISerializable <double>)) ||
                interfaces.Contains(typeof(ISerializable <decimal>)))
            {
                return("float");
            }
            if (clrType == typeof(string) || interfaces.Contains(typeof(ISerializable <string>)))
            {
                var len = p.MaxStringLength;

                if (len.HasValue)
                {
                    return("varchar(" + len.Value + ")");
                }

                return("varchar");
            }
            if (clrType == typeof(TimeSpan) || interfaces.Contains(typeof(ISerializable <TimeSpan>)))
            {
                return("bigint");
            }
            if (clrType == typeof(DateTime) || interfaces.Contains(typeof(ISerializable <DateTime>)))
            {
                return(storeDateTimeAsTicks ? "bigint" : "datetime");
            }
            if (clrType == typeof(DateTimeOffset))
            {
                return("bigint");
            }
            if (clrType.GetTypeInfo().IsEnum)
            {
                return("integer");
            }
            if (clrType == typeof(byte[]) || interfaces.Contains(typeof(ISerializable <byte[]>)))
            {
                return("blob");
            }
            if (clrType == typeof(Guid) || interfaces.Contains(typeof(ISerializable <Guid>)))
            {
                return("varchar(36)");
            }
            if (serializer != null && serializer.CanDeserialize(clrType))
            {
                return("blob");
            }
            throw new NotSupportedException("Don't know about " + clrType);
        }
 internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, object value,
                                    bool storeDateTimeAsTicks, IBlobSerializer serializer)
 {
     if (value == null)
     {
         isqLite3Api.BindNull(stmt, index);
     }
     else
     {
         if (value is Int32)
         {
             isqLite3Api.BindInt(stmt, index, (int)value);
         }
         else if (value is ISerializable <int> )
         {
             isqLite3Api.BindInt(stmt, index, ((ISerializable <int>)value).Serialize());
         }
         else if (value is String)
         {
             isqLite3Api.BindText16(stmt, index, (string)value, -1, NegativePointer);
         }
         else if (value is ISerializable <String> )
         {
             isqLite3Api.BindText16(stmt, index, ((ISerializable <String>)value).Serialize(), -1, NegativePointer);
         }
         else if (value is Byte || value is UInt16 || value is SByte || value is Int16)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(value));
         }
         else if (value is ISerializable <Byte> )
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable <Byte>)value).Serialize()));
         }
         else if (value is ISerializable <UInt16> )
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable <UInt16>)value).Serialize()));
         }
         else if (value is ISerializable <SByte> )
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable <SByte>)value).Serialize()));
         }
         else if (value is ISerializable <Int16> )
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable <Int16>)value).Serialize()));
         }
         else if (value is Boolean)
         {
             isqLite3Api.BindInt(stmt, index, (bool)value ? 1 : 0);
         }
         else if (value is ISerializable <Boolean> )
         {
             isqLite3Api.BindInt(stmt, index, ((ISerializable <bool>)value).Serialize() ? 1 : 0);
         }
         else if (value is UInt32 || value is Int64)
         {
             isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(value));
         }
         else if (value is ISerializable <UInt32> )
         {
             isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable <UInt32>)value).Serialize()));
         }
         else if (value is ISerializable <Int64> )
         {
             isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable <Int64>)value).Serialize()));
         }
         else if (value is Single || value is Double || value is Decimal)
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(value));
         }
         else if (value is ISerializable <Single> )
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable <Single>)value).Serialize()));
         }
         else if (value is ISerializable <Double> )
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable <Double>)value).Serialize()));
         }
         else if (value is ISerializable <Decimal> )
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable <Decimal>)value).Serialize()));
         }
         else if (value is TimeSpan)
         {
             isqLite3Api.BindInt64(stmt, index, ((TimeSpan)value).Ticks);
         }
         else if (value is ISerializable <TimeSpan> )
         {
             isqLite3Api.BindInt64(stmt, index, ((ISerializable <TimeSpan>)value).Serialize().Ticks);
         }
         else if (value is DateTime)
         {
             if (storeDateTimeAsTicks)
             {
                 isqLite3Api.BindInt64(stmt, index, ((DateTime)value).Ticks);
             }
             else
             {
                 isqLite3Api.BindText16(stmt, index, ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"), -1, NegativePointer);
             }
         }
         else if (value is ISerializable <DateTime> )
         {
             if (storeDateTimeAsTicks)
             {
                 isqLite3Api.BindInt64(stmt, index, ((ISerializable <DateTime>)value).Serialize().Ticks);
             }
             else
             {
                 isqLite3Api.BindText16(stmt, index, ((ISerializable <DateTime>)value).Serialize().ToString("yyyy-MM-dd HH:mm:ss"), -1, NegativePointer);
             }
         }
         else if (value.GetType().GetTypeInfo().IsEnum)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(value));
         }
         else if (value is byte[])
         {
             isqLite3Api.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer);
         }
         else if (value is ISerializable <byte[]> )
         {
             isqLite3Api.BindBlob(stmt, index, ((ISerializable <byte[]>)value).Serialize(), ((ISerializable <byte[]>)value).Serialize().Length, NegativePointer);
         }
         else if (value is Guid)
         {
             isqLite3Api.BindText16(stmt, index, ((Guid)value).ToString(), 72, NegativePointer);
         }
         else if (value is ISerializable <Guid> )
         {
             isqLite3Api.BindText16(stmt, index, ((ISerializable <Guid>)value).Serialize().ToString(), 72, NegativePointer);
         }
         else if (serializer != null && serializer.CanDeserialize(value.GetType()))
         {
             var bytes = serializer.Serialize(value);
             isqLite3Api.BindBlob(stmt, index, bytes, bytes.Length, NegativePointer);
         }
         else
         {
             throw new NotSupportedException("Cannot store type: " + value.GetType());
         }
     }
 }
Example #7
0
        public static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer)
        {
            Type clrType = p.ColumnType;
            var interfaces = clrType.GetTypeInfo().ImplementedInterfaces.ToList();

            if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) ||
                clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32) ||
                interfaces.Contains(typeof(ISerializable<Boolean>)) ||
                interfaces.Contains(typeof(ISerializable<Byte>)) ||
                interfaces.Contains(typeof(ISerializable<UInt16>)) ||
                interfaces.Contains(typeof(ISerializable<SByte>)) ||
                interfaces.Contains(typeof(ISerializable<Int16>)) ||
                interfaces.Contains(typeof(ISerializable<Int32>)))
            {
                return "integer";
            }
            if (clrType == typeof(UInt32) || clrType == typeof(Int64) ||
                interfaces.Contains(typeof(ISerializable<UInt32>)) ||
                interfaces.Contains(typeof(ISerializable<Int64>)))
            {
                return "bigint";
            }
            if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal) ||
                interfaces.Contains(typeof(ISerializable<Single>)) ||
                interfaces.Contains(typeof(ISerializable<Double>)) ||
                interfaces.Contains(typeof(ISerializable<Decimal>)))
            {
                return "float";
            }
            if (clrType == typeof(String) || interfaces.Contains(typeof(ISerializable<String>)))
            {
                int len = p.MaxStringLength;
                return "varchar(" + len + ")";
            }
            if (clrType == typeof(TimeSpan) || interfaces.Contains(typeof(ISerializable<TimeSpan>)))
            {
                return "bigint";
            }
            if (clrType == typeof(DateTime) || interfaces.Contains(typeof(ISerializable<DateTime>)))
            {
                return storeDateTimeAsTicks ? "bigint" : "datetime";
            }
            if (clrType.GetTypeInfo().IsEnum)
            {
                return "integer";
            }
            if (clrType == typeof(byte[]) || interfaces.Contains(typeof(ISerializable<byte[]>)))
            {
                return "blob";
            }
            if (clrType == typeof(Guid) || interfaces.Contains(typeof(ISerializable<Guid>)))
            {
                return "varchar(36)";
            }
            if (serializer != null && serializer.CanDeserialize(clrType))
            {
                return "blob";
            }
            throw new NotSupportedException("Don't know about " + clrType);
        }
Example #8
0
        private static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks,
                                      IBlobSerializer serializer,
                                      IDictionary <Type, string> extraTypeMappings)
        {
            var clrType    = p.ColumnType;
            var interfaces = clrType.GetTypeInfo().ImplementedInterfaces.ToList();

            string extraMapping;

            if (extraTypeMappings.TryGetValue(clrType, out extraMapping))
            {
                return(extraMapping);
            }

            //http://www.sqlite.org/datatype3.html
            if (clrType == typeof(bool) || clrType == typeof(byte) || clrType == typeof(ushort) ||
                clrType == typeof(sbyte) || clrType == typeof(short) || clrType == typeof(int) ||
                clrType == typeof(uint) || clrType == typeof(long) ||
                interfaces.Contains(typeof(ISerializable <bool>)) ||
                interfaces.Contains(typeof(ISerializable <byte>)) ||
                interfaces.Contains(typeof(ISerializable <ushort>)) ||
                interfaces.Contains(typeof(ISerializable <sbyte>)) ||
                interfaces.Contains(typeof(ISerializable <short>)) ||
                interfaces.Contains(typeof(ISerializable <int>)) ||
                interfaces.Contains(typeof(ISerializable <uint>)) ||
                interfaces.Contains(typeof(ISerializable <long>)) ||
                interfaces.Contains(typeof(ISerializable <ulong>)))
            {
                return("integer");
            }
            if (clrType == typeof(float) || clrType == typeof(double) || clrType == typeof(decimal) ||
                interfaces.Contains(typeof(ISerializable <float>)) ||
                interfaces.Contains(typeof(ISerializable <double>)) ||
                interfaces.Contains(typeof(ISerializable <decimal>)))
            {
                return("real");
            }
            if (clrType == typeof(string) || interfaces.Contains(typeof(ISerializable <string>)) ||
                clrType == typeof(XElement) || interfaces.Contains(typeof(ISerializable <XElement>))
                )
            {
                return("text");
            }
            if (clrType == typeof(TimeSpan) || interfaces.Contains(typeof(ISerializable <TimeSpan>)))
            {
                return("integer");
            }
            if (clrType == typeof(DateTime) || interfaces.Contains(typeof(ISerializable <DateTime>)))
            {
                return(storeDateTimeAsTicks ? "integer" : "numeric");
            }
            if (clrType == typeof(DateTimeOffset) || interfaces.Contains(typeof(ISerializable <DateTimeOffset>)))
            {
                return("integer");
            }
            if (clrType.GetTypeInfo().IsEnum)
            {
                return("integer");
            }
            if (clrType == typeof(byte[]) || interfaces.Contains(typeof(ISerializable <byte[]>)))
            {
                return("blob");
            }
            if (clrType == typeof(Guid) || interfaces.Contains(typeof(ISerializable <Guid>)))
            {
                return("text");
            }
            if (serializer != null && serializer.CanDeserialize(clrType))
            {
                return("blob");
            }
            throw new NotSupportedException("Don't know about " + clrType);
        }
Example #9
0
        private static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks,
            IBlobSerializer serializer,
            IDictionary<Type, string> extraTypeMappings)
        {
            var clrType = p.ColumnType;
            var interfaces = clrType.GetTypeInfo().ImplementedInterfaces.ToList();

            string extraMapping;
            if (extraTypeMappings.TryGetValue(clrType, out extraMapping))
            {
                return extraMapping;
            }

            if (clrType == typeof (bool) || clrType == typeof (byte) || clrType == typeof (ushort) ||
                clrType == typeof (sbyte) || clrType == typeof (short) || clrType == typeof (int) ||
                clrType == typeof (uint) || clrType == typeof (long) ||
                interfaces.Contains(typeof (ISerializable<bool>)) ||
                interfaces.Contains(typeof (ISerializable<byte>)) ||
                interfaces.Contains(typeof (ISerializable<ushort>)) ||
                interfaces.Contains(typeof (ISerializable<sbyte>)) ||
                interfaces.Contains(typeof (ISerializable<short>)) ||
                interfaces.Contains(typeof (ISerializable<int>)) ||
                interfaces.Contains(typeof (ISerializable<uint>)) ||
                interfaces.Contains(typeof (ISerializable<long>)) ||
                interfaces.Contains(typeof (ISerializable<ulong>)))
            {
                return "integer";
            }
            if (clrType == typeof (float) || clrType == typeof (double) || clrType == typeof (decimal) ||
                interfaces.Contains(typeof (ISerializable<float>)) ||
                interfaces.Contains(typeof (ISerializable<double>)) ||
                interfaces.Contains(typeof (ISerializable<decimal>)))
            {
                return "float";
            }
            if (clrType == typeof (string) || interfaces.Contains(typeof (ISerializable<string>)))
            {
                var len = p.MaxStringLength;

                if (len.HasValue)
                {
                    return "varchar(" + len.Value + ")";
                }

                return "varchar";
            }
            if (clrType == typeof (TimeSpan) || interfaces.Contains(typeof (ISerializable<TimeSpan>)))
            {
                return "bigint";
            }
            if (clrType == typeof (DateTime) || interfaces.Contains(typeof (ISerializable<DateTime>)))
            {
                return storeDateTimeAsTicks ? "bigint" : "datetime";
            }
            if (clrType == typeof (DateTimeOffset))
            {
                return "bigint";
            }
            if (clrType.GetTypeInfo().IsEnum)
            {
                return "integer";
            }
            if (clrType == typeof (byte[]) || interfaces.Contains(typeof (ISerializable<byte[]>)))
            {
                return "blob";
            }
            if (clrType == typeof (Guid) || interfaces.Contains(typeof (ISerializable<Guid>)))
            {
                return "varchar(36)";
            }
            if (serializer != null && serializer.CanDeserialize(clrType))
            {
                return "blob";
            }
            throw new NotSupportedException("Don't know about " + clrType);
        }
Example #10
0
 public static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks, IBlobSerializer serializer)
 {
     Type clrType = p.ColumnType;
     if (clrType == typeof (Boolean) || clrType == typeof (Byte) || clrType == typeof (UInt16) ||
         clrType == typeof (SByte) || clrType == typeof (Int16) || clrType == typeof (Int32))
     {
         return "integer";
     }
     if (clrType == typeof (UInt32) || clrType == typeof (Int64))
     {
         return "bigint";
     }
     if (clrType == typeof (Single) || clrType == typeof (Double) || clrType == typeof (Decimal))
     {
         return "float";
     }
     if (clrType == typeof (String))
     {
         int len = p.MaxStringLength;
         return "varchar(" + len + ")";
     }
     if (clrType == typeof (TimeSpan))
     {
         return "bigint";
     }
     if (clrType == typeof (DateTime))
     {
         return storeDateTimeAsTicks ? "bigint" : "datetime";
     }
     if (clrType.IsEnum)
     {
         return "integer";
     }
     if (clrType == typeof (byte[]))
     {
         return "blob";
     }
     if (clrType == typeof (Guid))
     {
         return "varchar(36)";
     }
     if (serializer != null && serializer.CanDeserialize(clrType))
     {
         return "blob";
     }
     throw new NotSupportedException("Don't know about " + clrType);
 }
 private static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, object value, bool storeDateTimeAsTicks,
                                   IBlobSerializer serializer)
 {
     if (value == null)
     {
         isqLite3Api.BindNull(stmt, index);
     }
     else
     {
         if (value is int)
         {
             isqLite3Api.BindInt(stmt, index, (int)value);
         }
         else if (value is ISerializable <int> serializable)
         {
             isqLite3Api.BindInt(stmt, index, (serializable).Serialize());
         }
         else if (value is string valueAsString)
         {
             isqLite3Api.BindText16(stmt, index, valueAsString, -1, NegativePointer);
         }
         else if (value is ISerializable <string> stringSerializable)
         {
             isqLite3Api.BindText16(stmt, index, (stringSerializable).Serialize(), -1, NegativePointer);
         }
         else if (value is byte || value is ushort || value is sbyte || value is short)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(value));
         }
         else if (value is ISerializable <byte> byteSerializable)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(byteSerializable.Serialize()));
         }
         else if (value is ISerializable <ushort> ushortSerializable)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(ushortSerializable.Serialize()));
         }
         else if (value is ISerializable <sbyte> sbyteSerializable)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(sbyteSerializable.Serialize()));
         }
         else if (value is ISerializable <short> shortSerializable)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32((shortSerializable).Serialize()));
         }
         else if (value is bool valueAsBool)
         {
             isqLite3Api.BindInt(stmt, index, valueAsBool ? 1 : 0);
         }
         else if (value is ISerializable <bool> boolSerializable)
         {
             isqLite3Api.BindInt(stmt, index, boolSerializable.Serialize() ? 1 : 0);
         }
         else if (value is uint || value is long)
         {
             isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(value));
         }
         else if (value is ISerializable <uint> uintSerializable)
         {
             isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(uintSerializable.Serialize()));
         }
         else if (value is ISerializable <long> longSerializable)
         {
             isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(longSerializable.Serialize()));
         }
         else if (value is float || value is double || value is decimal)
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(value));
         }
         else if (value is ISerializable <float> floatSerializable)
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(floatSerializable.Serialize()));
         }
         else if (value is ISerializable <double> doubleSerializable)
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(doubleSerializable.Serialize()));
         }
         else if (value is ISerializable <decimal> decimSerializable)
         {
             isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(decimSerializable.Serialize()));
         }
         else if (value is TimeSpan timeSpan)
         {
             isqLite3Api.BindInt64(stmt, index, timeSpan.Ticks);
         }
         else if (value is ISerializable <TimeSpan> timeSerializable)
         {
             isqLite3Api.BindInt64(stmt, index, timeSerializable.Serialize().Ticks);
         }
         else if (value is DateTime dateTime)
         {
             if (storeDateTimeAsTicks)
             {
                 var ticks = dateTime.ToUniversalTime().Ticks;
                 isqLite3Api.BindInt64(stmt, index, ticks);
             }
             else
             {
                 var val = ((DateTime)value).ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture);
                 isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer);
             }
         }
         else if (value is DateTimeOffset dateTimeOffset)
         {
             isqLite3Api.BindInt64(stmt, index, dateTimeOffset.UtcTicks);
         }
         else if (value is ISerializable <DateTime> datetSerializable)
         {
             if (storeDateTimeAsTicks)
             {
                 var ticks = datetSerializable.Serialize().ToUniversalTime().Ticks;
                 isqLite3Api.BindInt64(stmt, index, ticks);
             }
             else
             {
                 var val = datetSerializable.Serialize().ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture);
                 isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer);
             }
         }
         else if (value.GetType().GetTypeInfo().IsEnum)
         {
             isqLite3Api.BindInt(stmt, index, Convert.ToInt32(value));
         }
         else if (value is byte[] bytes2)
         {
             isqLite3Api.BindBlob(stmt, index, bytes2, bytes2.Length, NegativePointer);
         }
         else if (value is ISerializable <byte[]> byteSerializable2)
         {
             var serialized = byteSerializable2.Serialize();
             isqLite3Api.BindBlob(stmt, index, serialized, serialized.Length, NegativePointer);
         }
         else if (value is Guid guid)
         {
             isqLite3Api.BindText16(stmt, index, guid.ToString(), 72, NegativePointer);
         }
         else if (value is ISerializable <Guid> guiSerializable)
         {
             isqLite3Api.BindText16(stmt, index, guiSerializable.Serialize().ToString(), 72, NegativePointer);
         }
         else if (serializer != null && serializer.CanDeserialize(value.GetType()))
         {
             var bytes = serializer.Serialize(value);
             isqLite3Api.BindBlob(stmt, index, bytes, bytes.Length, NegativePointer);
         }
         else
         {
             throw new NotSupportedException("Cannot store type: " + value.GetType());
         }
     }
 }