private static string SerializeColumnToString <T>(T column) { if (column == null) { return("null"); } var columnType = column.GetType(); var result = new StringBuilder(); if (EntityTypeDescriptor.IsSimpleType(columnType) || EntityTypeDescriptor.IsNullableType(columnType)) { result.Append(column); } else if (EntityTypeDescriptor.IsSupportedEnumerable(columnType)) { var enumerable = column as IEnumerable; foreach (var item in enumerable) { result.AppendFormat("{0},", SerializeColumnToString(item)); } } else { var typeDescription = EntityTypeDescriptor.GetTypeDescription(columnType); foreach (var item in typeDescription.Columns) { result.AppendFormat("{0}={1}\n", item.ColumnAttribute.Name ?? item.ColumnInfo.Name, SerializeColumnToString(item.ColumnInfo.GetValue(column, null))); } } return(result.ToString()); }
private static object ConvertValue(object value, Type columnType) { if (value == null) { return(null); } if (columnType.IsEnum) { return(Enum.Parse(columnType, value.ToString())); } if (columnType == typeof(DateTime)) { return(DateTime.Parse(value.ToString())); } if (columnType == typeof(DateTimeOffset)) { return(DateTimeOffset.Parse(value.ToString())); } if (EntityTypeDescriptor.IsNullableType(columnType)) { return(Convert.ChangeType(value, Nullable.GetUnderlyingType(columnType))); } return(Convert.ChangeType(value, columnType)); }
private static void AppendPrimaryKey <TEntity>(TEntity entity, NodeReference node) { var typeDescription = EntityTypeDescriptor.GetTypeDescription(entity.GetType()); var identityKeys = typeDescription.IdentityKeys; if (identityKeys.Any()) { //according to the validation rules var identityKey = identityKeys.Single(); var newSeed = IncrementIdentity(node); var propertyType = identityKey.ColumnInfo.PropertyType; propertyType = !EntityTypeDescriptor.IsNullableType(propertyType) ? propertyType : Nullable.GetUnderlyingType(propertyType); var correctValue = Convert.ChangeType(newSeed, propertyType); identityKey.ColumnInfo.SetValue(entity, correctValue, null); node.AppendSubscript(newSeed); } else { var hash = PrimaryKeyCalculator.GetPrimaryKey(entity); if (IsKeyExists(hash, node)) { throw new GlobalsDbException("Violation of Primary Key constraint. Cannot insert duplicate key"); } node.SetSubscriptCount(0); node.AppendSubscript(hash); } }
private static void WriteComplexEntity <TEntity>(TEntity entity, NodeReference node) { if (entity == null) { return; } var entityType = entity.GetType(); var underlyingType = EntityTypeDescriptor.IsNullableType(entityType) ? Nullable.GetUnderlyingType(entityType) : entityType; var columns = EntityTypeDescriptor.GetTypeDescription(underlyingType).Columns; foreach (var column in columns) { node.AppendSubscript(column.ColumnAttribute.Name ?? column.ColumnInfo.Name); if (column.IsSimpleColumn) { var value = column.ColumnInfo.GetValue(entity, null); if (value != null) { if (column.IsNullableColumn) { node.Set(value != null ? value.ToString() : null); } else { node.Set(value.ToString()); } } } else if (column.IsArrayColumn) { var array = column.ColumnInfo.GetValue(entity, null) as Array; WriteArray(node, array); } else if (column.IsEnumerableColumn) { var items = column.ColumnInfo.GetValue(entity, null) as IEnumerable; if (items != null) { WriteEnumerable(node, items); } } else { WriteComplexEntity(column.ColumnInfo.GetValue(entity, null), node); } node.SetSubscriptCount(node.GetSubscriptCount() - 1); } }
private static object ReadComplexNode(NodeReference node, Type objectType) { var isNullable = EntityTypeDescriptor.IsNullableType(objectType); if (isNullable && IsNullableComplexColumnEmpty(node)) { return(null); } var underlyingType = !isNullable ? objectType : Nullable.GetUnderlyingType(objectType); var typeDescription = EntityTypeDescriptor.GetTypeDescription(underlyingType); var instance = InstanceCreator.CreateInstance(underlyingType); foreach (var column in typeDescription.Columns) { var subscript = column.ColumnAttribute.Name ?? column.ColumnInfo.Name; if (column.IsSimpleColumn) { node.AppendSubscript(subscript); var nodeValue = node.GetObject(); column.ColumnInfo.SetValue(instance, ConvertValue(nodeValue, column.ColumnInfo.PropertyType), null); } else if (column.IsArrayColumn) { node.AppendSubscript(subscript); column.ColumnInfo.SetValue(instance, ReadArray(node, column.ColumnInfo.PropertyType), null); } else if (column.IsEnumerableColumn) { node.AppendSubscript(subscript); column.ColumnInfo.SetValue(instance, ReadEnumerable(node, column.ColumnInfo.PropertyType), null); } else if (column.IsComplexColumn) { node.AppendSubscript(subscript); column.ColumnInfo.SetValue(instance, ReadComplexNode(node, column.ColumnInfo.PropertyType), null); } node.SetSubscriptCount(node.GetSubscriptCount() - 1); } return(instance); }
private static object PerformBinaryOperation(object left, Type leftDataType, object right, Type rightDataType, ExpressionType nodeType) { var leftNode = left as NodeReference; var rightNode = right as NodeReference; var leftData = (leftNode == null) ? left : DatabaseManager.ReadNode(leftNode, leftDataType); var rightData = (rightNode == null) ? right : DatabaseManager.ReadNode(rightNode, rightDataType); if ((EntityTypeDescriptor.IsNullableType(leftDataType) || EntityTypeDescriptor.IsNullableType(rightDataType)) && (leftData == null || rightData == null)) { return(ResolveNullableOperation(leftData, rightData, nodeType)); } return(BinaryOperationHandler[nodeType](leftData, rightData)); }
private static Type ResolveParameterType(Type type, Type parentType) { if (EntityTypeDescriptor.IsNullableType(type)) { return(type); } if (type == typeof(IQueryable)) { return(parentType.GetGenericArguments().First()); } var genericArguments = type.GetGenericArguments(); if (genericArguments.Length == 1) { return(genericArguments.Single()); } return(type); }
private static object ResolveColumn(ColumnAttribute columnAttribute, MemberExpression expression, IEnumerable <NodeReference> references) { if (columnAttribute != null) { foreach (var nodeReference in references) { nodeReference.AppendSubscript(columnAttribute.Name ?? expression.Member.Name); } return(references); } if (EntityTypeDescriptor.IsSupportedEnumerable(expression.Expression.Type) && expression.Member.Name == "Count") { var result = references.Select(DatabaseManager.GetEnumerableCount).ToList(); return(result); } if (EntityTypeDescriptor.IsArrayType(expression.Expression.Type) && expression.Member.Name == "Length") { var result = references.Select(DatabaseManager.GetEnumerableCount).ToList(); return(result); } if (EntityTypeDescriptor.IsArrayType(expression.Expression.Type) && expression.Member.Name == "LongLength") { var result = references.Select(DatabaseManager.GetEnumerableCount).ToList(); return(result); } if (EntityTypeDescriptor.IsNullableType(expression.Expression.Type) && expression.Member.Name == "Value") { return(references); } var parentType = expression.Expression.Type; var data = DatabaseManager.ReadNodes(references, parentType) as IEnumerable; return(ProcessLoadedEntityMembers(data, expression)); }
internal static object GetDefaultValue(Type instanceType) { return(((!instanceType.IsValueType) || (EntityTypeDescriptor.IsNullableType(instanceType))) ? null : CreateInstance(instanceType)); }