/// <summary> /// 获取字段成员集合 /// </summary> /// <param name="fields"></param> /// <param name="isColumn"></param> /// <returns>字段成员集合</returns> internal static LeftArray <Field> Get(FieldIndex[] fields, bool isColumn) { LeftArray <Field> values = new LeftArray <Field>(fields.Length); foreach (FieldIndex field in fields) { Type type = field.Member.FieldType; if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !field.IsIgnore) { MemberAttribute attribute = field.GetAttribute <MemberAttribute>(false); if (attribute == null || attribute.IsSetup) { LogAttribute logAttribute = isColumn ? null : field.GetAttribute <LogAttribute>(false); if (logAttribute == null || logAttribute.IsMember) { if (attribute != null && attribute.IsNowTime && type != typeof(DateTime)) { attribute.IsNowTime = false; } values.Add(new Field(field, attribute)); } } } } return(values); }
internal static SqlDbType formCSharpType(this Type type, MemberAttribute memberAttribute) { SqlDbType value; if (formCSharpTypes.TryGetValue(type.nullableType() ?? type, out value)) { return(value != SqlDbType.DateTime && memberAttribute.NowTimeType != NowTimeType.DateTime2 ? value : SqlDbType.DateTime2); } return(SqlDbType.NVarChar); }
/// <summary> /// 成员信息转换为数据列 /// </summary> /// <param name="name">成员名称</param> /// <param name="type">成员类型</param> /// <param name="memberAttribute">SQL成员信息</param> /// <returns>数据列</returns> internal Column GetColumn(string name, Type type, MemberAttribute memberAttribute) { Column column = TypeAttribute.GetAttribute <ColumnAttribute>(type, false) == null?GetColumn(type, memberAttribute) : new Column { SqlColumnType = type }; column.Name = name; return(column); }
/// <summary> /// 获取数据长度 /// </summary> /// <param name="type"></param> /// <param name="memberAttribute"></param> /// <returns></returns> internal static int getSize(this SqlDbType type, MemberAttribute memberAttribute) { switch (type) { case SqlDbType.Decimal: return(((memberAttribute.DecimalIntegerSize <= 0 ? 18 : (int)memberAttribute.DecimalIntegerSize) << 8) + (memberAttribute.DecimalSize <= 0 ? 2 : memberAttribute.DecimalSize)); case SqlDbType.Money: return(((memberAttribute.DecimalIntegerSize <= 0 ? 15 : (int)memberAttribute.DecimalIntegerSize) << 8) + (memberAttribute.DecimalSize <= 0 ? 2 : memberAttribute.DecimalSize)); case SqlDbType.SmallMoney: return(((memberAttribute.DecimalIntegerSize <= 0 ? 6 : (int)memberAttribute.DecimalIntegerSize) << 8) + (memberAttribute.DecimalSize <= 0 ? 2 : memberAttribute.DecimalSize)); default: return(sizes.Int[(int)type]); } }
/// <summary> /// 字段信息 /// </summary> /// <param name="field">字段信息</param> /// <param name="attribute">数据库成员信息</param> internal Field(FieldIndex field, MemberAttribute attribute) { FieldInfo = field.Member; MemberMapIndex = field.MemberIndex; DataMember = format(attribute, FieldInfo.FieldType, ref IsSqlColumn); if ((NullableDataType = DataMember.DataType) == null) { NullableDataType = FieldInfo.FieldType; } if ((DataReaderMethod = DataReader.GetMethod(DataType = NullableDataType.nullableType() ?? NullableDataType)) == null) { if (IsSqlColumn) { if (isSqlColumn(DataType)) { return; } IsSqlColumn = false; } #if NOJIT DataType = NullableDataType = typeof(string); DataReaderMethod = DataReader.GetMethodInfo; #else DataReaderMethod = DataReader.GetMethod(DataType = NullableDataType = typeof(string)); #endif IsUnknownJson = true; ToSqlCastMethod = jsonSerializeMethod.MakeGenericMethod(FieldInfo.FieldType); ToModelCastMethod = jsonParseMethod.MakeGenericMethod(FieldInfo.FieldType); } else { ToSqlCastMethod = AutoCSer.Emit.CastType.GetMethod(FieldInfo.FieldType, DataType); ToModelCastMethod = AutoCSer.Emit.CastType.GetMethod(DataType, FieldInfo.FieldType); } SqlFieldName = keywordSearcher.SearchLower(FieldInfo.Name) < 0 ? FieldInfo.Name : toSqlName(FieldInfo.Name); if (attribute != null && attribute.IsNowTime && FieldInfo.FieldType == typeof(DateTime)) { IsNowTime = true; } ToSqlMethod = ConstantConverter.GetMethod(DataType); }
/// <summary> /// 字段信息 /// </summary> /// <param name="field">字段信息</param> /// <param name="attribute">数据库成员信息</param> internal Field(FieldIndex field, MemberAttribute attribute) { FieldInfo = field.Member; MemberMapIndex = field.MemberIndex; DataMember = format(attribute, FieldInfo.FieldType, ref IsSqlColumn); if ((NullableDataType = DataMember.DataType) == null) { NullableDataType = FieldInfo.FieldType; } if ((DataReaderDelegate = DataReader.GetDelegate(DataType = NullableDataType.nullableType() ?? NullableDataType)) == null) { if (IsSqlColumn) { if (isSqlColumn(DataType)) { return; } IsSqlColumn = false; } #if NOJIT DataType = NullableDataType = typeof(string); DataReaderMethod = DataReader.GetMethodInfo; #else DataReaderDelegate = DataReader.GetDelegate(DataType = NullableDataType = typeof(string)); #endif IsUnknownJson = true; AutoCSer.Sql.Metadata.GenericType GenericType = AutoCSer.Sql.Metadata.GenericType.Get(FieldInfo.FieldType); ToSqlCastMethod = GenericType.JsonSerializeMethod; ToModelCastMethod = GenericType.JsonDeSerializeMethod; } else { ToSqlCastMethod = AutoCSer.Emit.CastType.GetMethod(FieldInfo.FieldType, DataType); ToModelCastMethod = AutoCSer.Emit.CastType.GetMethod(DataType, FieldInfo.FieldType); } if (attribute != null && attribute.NowTimeType != NowTimeType.None && FieldInfo.FieldType == typeof(DateTime)) { NowTimeType = attribute.NowTimeType; } ToSqlMethod = ConstantConverter.GetMethod(DataType); }
/// <summary> /// 获取数据库成员信息 /// </summary> /// <param name="member">成员信息</param> /// <returns>数据库成员信息</returns> internal static MemberAttribute Get(MemberIndexInfo member) { MemberAttribute value = member.GetAttribute <MemberAttribute>(true); if (value == null || value.DataType == null) { if (member.MemberSystemType.IsEnum) { if (value == null) { value = new MemberAttribute(); } value.DataType = System.Enum.GetUnderlyingType(member.MemberSystemType); } else { MemberAttribute sqlMember = TypeAttribute.GetAttribute <MemberAttribute>(member.MemberSystemType, false); if (sqlMember != null && sqlMember.DataType != null) { if (value == null) { value = new MemberAttribute(); } value.DataType = sqlMember.DataType; if (sqlMember.DataType.nullableType() != null) { value.IsNull = true; } } } } else if (member.MemberSystemType.IsEnum) { Type enumType = System.Enum.GetUnderlyingType(member.MemberSystemType); if (enumType != value.DataType) { value.EnumType = enumType; } } if (value == null) { Type nullableType = member.MemberSystemType.nullableType(); if (nullableType == null) { if (TypeAttribute.GetAttribute <ColumnAttribute>(member.MemberSystemType, false) == null) { Type dataType = member.MemberSystemType.toDataType(); if (dataType != member.MemberSystemType) { value = new MemberAttribute(); value.DataType = dataType; } } } else { value = new MemberAttribute(); value.IsNull = true; Type dataType = nullableType.toDataType(); if (dataType != nullableType) { value.DataType = dataType.toNullableType(); } } } return(value ?? DefaultDataMember); }
/// <summary> /// 获取数据库成员信息集合 /// </summary> /// <param name="members">成员集合</param> /// <returns>数据库成员信息集合</returns> internal static KeyValue <MemberIndexInfo, MemberAttribute>[] GetMembers(MemberIndexInfo[] members) { return(members.getFind(value => value.CanSet && value.CanGet) .GetArray(value => new KeyValue <MemberIndexInfo, MemberAttribute>(value, MemberAttribute.Get(value)))); }
/// <summary> /// 格式化数据库成员信息 /// </summary> /// <param name="value"></param> /// <param name="type"></param> /// <param name="isSqlColumn"></param> /// <returns></returns> private static MemberAttribute format(MemberAttribute value, Type type, ref bool isSqlColumn) { if (type.IsEnum) { Type enumType = System.Enum.GetUnderlyingType(type); if (enumType == typeof(sbyte)) { enumType = typeof(byte); } else if (enumType == typeof(ushort)) { enumType = typeof(short); } else if (enumType == typeof(ulong)) { enumType = typeof(long); } if (value == null) { return new MemberAttribute { DataType = enumType } } ; if (value.DataType == null) { value.DataType = enumType; } else if (enumType != value.DataType) { value.EnumType = enumType; } return(value); } Type nullableType = null; if (type.IsGenericType) { Type genericType = type.GetGenericTypeDefinition(); //if (genericType == typeof(AutoCSer.sql.fileBlockMember<>)) //{ // if (value == null) return new dataMember { DataType = typeof(AutoCSer.io.fileBlockStream.index) }; // value.DataType = typeof(AutoCSer.io.fileBlockStream.index); // return value; //} if (genericType == typeof(Nullable <>)) { nullableType = type.GetGenericArguments()[0]; } } else if (type.IsDefined(typeof(ColumnAttribute), false)) { isSqlColumn = true; return(MemberAttribute.DefaultDataMember); } if (value == null || value.DataType == null) { MemberAttribute sqlMember = TypeAttribute.GetAttribute <MemberAttribute>(type, false); if (sqlMember != null && sqlMember.DataType != null) { if (value == null) { value = new MemberAttribute(); } value.DataType = sqlMember.DataType; if (sqlMember.DataType.IsValueType && sqlMember.DataType.IsGenericType && sqlMember.DataType.GetGenericTypeDefinition() == typeof(Nullable <>)) { value.IsNull = true; } } } if (value == null) { if (nullableType == null) { Type dataType = type.formCSharpType().toCSharpType(); if (dataType != type) { value = new MemberAttribute { DataType = dataType } } ; } else { value = new MemberAttribute { IsNull = true }; Type dataType = nullableType.formCSharpType().toCSharpType(); if (dataType != nullableType) { value.DataType = dataType.toNullableType(); } } } return(value ?? MemberAttribute.DefaultDataMember); }
/// <summary> /// 成员信息转换为数据列 /// </summary> /// <param name="type">成员类型</param> /// <param name="memberAttribute">SQL成员信息</param> /// <returns>数据列</returns> internal abstract Column GetColumn(Type type, MemberAttribute memberAttribute);