예제 #1
0
파일: Column.cs 프로젝트: jeremycook/Tom
 public Column(PropertyInfo prop)
 {
     Field = new Field(prop.PropertyType, prop.Name);
     if (Field.IsMapped)
     {
         var fieldSettings = Settings.Current.GetFieldSettings(prop.PropertyType);
         FieldArguments = fieldSettings.FieldArguments;
         FieldDefault = fieldSettings.FieldDefault;
     }
 }
예제 #2
0
파일: Field.cs 프로젝트: jeremycook/Tom
        public static object DefaultGetObjectValue(Field field, object sqlvalue)
        {
            var objectValue = sqlvalue == DBNull.Value ? null : sqlvalue;

            if (field.IsSecure && objectValue != null)
            {
                var decryptedBytes = Settings.Current.Encryptor.Decrypt(objectValue as byte[]);
                if (field.IsSerialized)
                {
                    objectValue = decryptedBytes;
                }
                else if (field.Type == typeof(byte[]))
                {
                    objectValue = decryptedBytes;
                }
                else if (field.Type == typeof(DateTime) || field.Type == typeof(DateTime?))
                {
                    var ticks = BitConverter.ToInt64(decryptedBytes, 0);
                    objectValue = new DateTime(ticks);
                }
                else
                {
                    string text = Encoding.UTF8.GetString(decryptedBytes);

                    if (field.Type == typeof(Guid) || field.Type == typeof(Guid?))
                    {
                        objectValue = Guid.Parse(text);
                    }
                    else if (field.Type == typeof(DateTimeOffset) || field.Type == typeof(DateTimeOffset?))
                    {
                        objectValue = DateTimeOffset.Parse(text);
                    }
                    else if (field.Type.IsGenericType && field.Type.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        objectValue = Convert.ChangeType(text, field.Type.GetGenericArguments()[0]);
                    }
                    else
                    {
                        objectValue = Convert.ChangeType(text, field.Type);
                    }
                }
            }

            if (field.IsSerialized && objectValue != null)
            {
                switch (field.SqlDbType)
                {
                    case SqlDbType.NVarChar:
                        objectValue = JsonConvert.DeserializeObject((string)objectValue, field.Type);
                        break;
                    case SqlDbType.VarBinary:
                        byte[] data = (byte[])objectValue;
                        using (MemoryStream ms = new MemoryStream(data))
                        using (var reader = new BsonReader(ms))
                        {
                            reader.ReadRootValueAsArray =
                                typeof(IEnumerable).IsAssignableFrom(field.Type) &&
                                !typeof(IDictionary).IsAssignableFrom(field.Type) &&
                                field.Type != typeof(string);

                            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault();
                            objectValue = jsonSerializer.Deserialize(reader, field.Type);
                        }
                        break;
                    default:
                        throw new InvalidOperationException(string.Format("Cannot deserialize from SqlDbType.{0}. Only NVarChar and VarBinary are supported.", field.SqlDbType));
                }
            }

            return objectValue;
        }
예제 #3
0
파일: Field.cs 프로젝트: jeremycook/Tom
        public static IEnumerable<Field> CreateFieldsFromType(Type modelType)
        {
            if (modelType == null)
            {
                throw new ArgumentNullException("modelType");
            }

            var props = modelType.GetProperties();
            var fields = new Field[props.Length];

            var i = 0;
            foreach (var prop in props)
            {
                var field = new Field(prop.PropertyType, prop.Name);

                fields[i++] = field;
            }

            return fields;
        }
예제 #4
0
파일: Field.cs 프로젝트: jeremycook/Tom
        public static object DefaultGetSqlParameterValue(Field field, object objectValue)
        {
            object parameterValue = objectValue ?? field.EmptyValueFactory();

            if (field.IsSerialized && parameterValue != null)
            {
                switch (field.SqlDbType)
                {
                    case SqlDbType.NVarChar:
                        parameterValue = JsonConvert.SerializeObject(parameterValue);
                        break;
                    case SqlDbType.VarBinary:
                        using (MemoryStream ms = new MemoryStream())
                        using (BsonWriter writer = new BsonWriter(ms))
                        {
                            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault();
                            jsonSerializer.Serialize(writer, parameterValue);
                            parameterValue = ms.ToArray();
                        }
                        break;
                    default:
                        throw new InvalidOperationException(string.Format("Cannot serialize to SqlDbType.{0}. Only NVarChar abd VarBinary are supported.", field.SqlDbType));
                }
            }

            if (field.IsSecure && parameterValue != null)
            {
                if (parameterValue is byte[])
                {
                    parameterValue = Settings.Current.Encryptor.Encrypt((byte[])parameterValue);
                }
                else if (parameterValue is DateTime?)
                {
                    // Ticks seems to be the only reliable way to precisely encrypt DateTime.
                    byte[] clearBytes = BitConverter.GetBytes(((DateTime?)parameterValue).Value.Ticks);
                    parameterValue = Settings.Current.Encryptor.Encrypt(clearBytes);
                }
                else if (parameterValue is DateTimeOffset?)
                {
                    byte[] clearBytes = Encoding.UTF8.GetBytes(((DateTimeOffset?)parameterValue).Value.ToString("O"));
                    parameterValue = Settings.Current.Encryptor.Encrypt(clearBytes);
                }
                else
                {
                    byte[] clearBytes = Encoding.UTF8.GetBytes(parameterValue.ToString());
                    parameterValue = Settings.Current.Encryptor.Encrypt(clearBytes);
                }
            }

            return parameterValue ?? DBNull.Value;
        }