Exemple #1
0
        public static int BindParameter(SqliteCommand command, object value)
        {
            //SqliteParameter(DbType dbType, object value)构造函数可正常使用
            //SqliteParameter(DbType dbType, string value)将不能插入数值
            ///我也不知道为什么会这样 (笑)
            if (value == null)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)"")));
            }

            if (value is int)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Int32, value)));
            }
            if (value is string)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.String, value)));
            }
            if (value is byte || value is ushort || value is sbyte || value is short)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Int32, value)));
            }
            if (value is bool)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Int32, (object)((bool)value ? 1 : 0))));
            }
            if (value is uint || value is long)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Int64, value)));
            }
            if (value is float || value is double || value is decimal)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Double, value)));
            }

            if (value.GetType().IsEnum)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Int64, (object)Convert.ToInt32(value))));
            }
            if (value is byte[])
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Binary, value)));
            }
            if (value is Guid)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)((Guid)value).ToString())));
            }

            if (value is TimeSpan)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Int64, value)));
            }
            if (value is DateTime)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"))));
            }
            if (value is DateTimeOffset)
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Int64, value)));
            }

            //数据扩展支持(Array/List/Dict)
            var normal_fullnam = SupportExtend.GetNormalFullName(value.GetType());

            if (SupportExtend.IsSupportType(normal_fullnam))
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)SupportExtend.ToString(value, normal_fullnam))));
            }
            //自定义接口实现
            if (SupportExtend.IsSupportIDataText(value.GetType()))
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.String, (object)((IDataText)value).Save())));
            }
            if (SupportExtend.IsSupportIDataBytes(value.GetType()))
            {
                return(command.Parameters.Add(new SqliteParameter(DbType.Binary, (object)((IDataBytes)value).Save())));
            }

            throw new NotSupportedException("Cannot store type: " + value.GetType());
        }
Exemple #2
0
        private static object ReadValue(SqliteDataReader reader, int index, Type col_type)
        {
            if (reader.IsDBNull(index))
            {
                return(null);
            }

            if (col_type == typeof(string))
            {
                return(reader.GetString(index));
            }
            if (col_type == typeof(int))
            {
                return(reader.GetInt32(index));
            }
            if (col_type == typeof(bool))
            {
                return(reader.GetInt32(index) == 1);
            }
            if (col_type == typeof(float))
            {
                return((float)reader.GetDouble(index));
            }
            if (col_type == typeof(double))
            {
                return(reader.GetDouble(index));
            }
            if (col_type == typeof(decimal))
            {
                return((decimal)reader.GetDouble(index));
            }
            if (col_type == typeof(long))
            {
                return(reader.GetInt64(index));
            }
            if (col_type == typeof(uint))
            {
                return((uint)reader.GetInt64(index));
            }

            if (col_type == typeof(byte))
            {
                return((byte)reader.GetInt32(index));
            }
            if (col_type == typeof(ushort))
            {
                return((ushort)reader.GetInt32(index));
            }
            if (col_type == typeof(short))
            {
                return((short)reader.GetInt32(index));
            }
            if (col_type == typeof(sbyte))
            {
                return((sbyte)reader.GetInt32(index));
            }

            if (col_type.IsEnum)
            {
                return(reader.GetInt32(index));
            }
            if (col_type == typeof(Guid))
            {
                return(new Guid(reader.GetString(index)));
            }
            if (col_type == typeof(byte[]))
            {
                var blob = new byte[reader.GetBytes(index, 0, null, 0, int.MaxValue)];
                reader.GetBytes(index, 0, blob, 0, blob.Length);
                return(blob);
            }

            if (col_type == typeof(TimeSpan))
            {
                return(new TimeSpan(reader.GetInt64(index)));
            }
            if (col_type == typeof(DateTime))
            {
                return(DateTime.Parse(reader.GetString(index)));
            }
            if (col_type == typeof(DateTimeOffset))
            {
                return(new TimeSpan(reader.GetInt64(index)));
            }

            //数据扩展支持(Array/List/Dict)
            var normal_fullnam = SupportExtend.GetNormalFullName(col_type);

            if (SupportExtend.IsSupportType(normal_fullnam))
            {
                var text = reader.GetString(index);
                return(SupportExtend.ToObject(text, normal_fullnam));
            }
            //自定义接口实现
            if (SupportExtend.IsSupportIDataText(col_type))
            {
                var text = reader.GetString(index);
                var obj  = (IDataText)Activator.CreateInstance(col_type);
                obj.Load(text);
                return(obj);
            }
            if (SupportExtend.IsSupportIDataBytes(col_type))
            {
                var blob = new byte[reader.GetBytes(index, 0, null, 0, int.MaxValue)];
                reader.GetBytes(index, 0, blob, 0, blob.Length);
                var obj = (IDataBytes)Activator.CreateInstance(col_type);
                obj.Load(blob);
                return(obj);
            }

            throw new NotSupportedException("Don't know how to read " + col_type);
        }
Exemple #3
0
        public static string SqlType(Column col)
        {
            var type = col.ColumnType;

            if (type == typeof(bool) || type == typeof(byte) || type == typeof(ushort) ||
                type == typeof(sbyte) || type == typeof(short) || type == typeof(int))
            {
                return("INTEGER");
            }
            if (type == typeof(uint) || type == typeof(long))
            {
                return("BIGINT");
            }
            if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))
            {
                return("REAL");
            }
            if (type == typeof(string))
            {
                int?len = col.MaxLength;
                if (len.HasValue)
                {
                    return("VARCHAR (" + len.Value + ")");
                }
                return("VARCHAR");
            }

            if (type.IsEnum)
            {
                return("INTEGER");
            }
            if (type == typeof(byte[]))
            {
                return("BLOB");
            }
            if (type == typeof(Guid))
            {
                return("VARCHAR(36)");
            }

            if (type == typeof(TimeSpan) || type == typeof(DateTimeOffset))
            {
                return("BIGINT");
            }
            if (type == typeof(DateTime))
            {
                return("DATE");
            }

            //数据扩展支持(Array/List/Dict)
            if (SupportExtend.IsSupportType(type))
            {
                return("VARCHAR");
            }
            //自定义接口实现
            if (SupportExtend.IsSupportIDataText(type))
            {
                return("VARCHAR");
            }
            if (SupportExtend.IsSupportIDataBytes(type))
            {
                return("BLOB");
            }

            throw new NotSupportedException("Don't know about " + type);
        }