コード例 #1
0
ファイル: Column.cs プロジェクト: AutoCSer/AutoCSer
        static Column()
        {
            Type type = typeof(valueType);

            if (type.IsEnum || !type.IsValueType)
            {
                AutoCSer.LogHelper.Error(type.fullName() + " 非值类型,不能用作数据列", LogLevel.Error | LogLevel.AutoCSer);
                return;
            }
            attribute = TypeAttribute.GetAttribute <ColumnAttribute>(type, true) ?? ColumnAttribute.Default;
            foreach (MethodInfo method in type.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic))
            {
#if NOJIT
                if (typeof(ICustom).IsAssignableFrom(method.ReturnType)
#else
                if (typeof(ICustom <valueType>).IsAssignableFrom(method.ReturnType)
#endif
                    && method.GetParameters().Length == 0 && method.IsDefined(typeof(ColumnAttribute), false))
                {
                    object customValue = method.Invoke(null, null);
                    if (customValue != null)
                    {
#if NOJIT
                        custom = (ICustom)customValue;
#else
                        custom = (ICustom <valueType>)customValue;
#endif
                        return;
                    }
                }
            }
            Fields      = Field.Get(MemberIndexGroup <valueType> .GetFields(attribute.MemberFilters), true).ToArray();
            dataColumns = new AutoCSer.Threading.LockDictionary <HashString, KeyValue <string, Type>[]>();
            AutoCSer.Memory.Common.AddClearCache(dataColumns.Clear, typeof(Column <valueType>), 60 * 60);
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
        public StringParser()
        {
            Type t = GetType();

            ParseMethods = t.GetMethods().Where(x => TypeAttribute.HasAttribute(x)).ToDictionary(
                (m) => TypeAttribute.GetAttribute(m).Type,
                (m) => new Func <string, object>((str) =>
                                                 Try.Func <object>(
                                                     () => m.Invoke(this, new object[] { str }),
                                                     (ex) => { throw ex.InnerException; }
                                                     )));
        }
コード例 #4
0
ファイル: ModelTable.cs プロジェクト: zhangzheng1205/AutoCSer
        /// <summary>
        /// 获取数据库表格操作工具
        /// </summary>
        /// <returns>数据库表格操作工具</returns>
        /// <param name="isCreateCacheWait">是否等待创建缓存</param>
        public static ModelTable <modelType> Get(bool isCreateCacheWait = false)
        {
            Type           type      = typeof(modelType);
            TableAttribute attribute = TypeAttribute.GetAttribute <TableAttribute>(type, false);

            if (attribute != null)// && Array.IndexOf(ConfigLoader.Config.CheckConnectionNames, attribute.ConnectionType) != -1
            {
                ModelTable <modelType> table = new ModelTable <modelType>(attribute, isCreateCacheWait);
                if (!table.IsError)
                {
                    return(table);
                }
            }
            return(null);
        }
コード例 #5
0
        static Model()
        {
            Type type = typeof(valueType);

            attribute   = TypeAttribute.GetAttribute <ModelAttribute>(type, true) ?? ModelAttribute.Default;
            Fields      = Field.Get(MemberIndexGroup <valueType> .GetFields(attribute.MemberFilters), false).ToArray();
            Identity    = Field.GetIdentity(Fields);
            PrimaryKeys = Field.GetPrimaryKeys(Fields).ToArray();
            MemberMap   = new MemberMap <valueType>();
            foreach (Field field in Fields)
            {
                MemberMap.SetMember(field.MemberMapIndex);
            }
            if (Identity != null)
            {
                IdentitySqlName = Identity.SqlFieldName;
#if NOJIT
                new identity(Identity.Field).Get(out GetIdentity, out SetIdentity);
                Action <valueType, int> setter32;
                new identity32(Identity.Field).Get(out GetIdentity32, out setter32);
#else
                DynamicMethod dynamicMethod = new DynamicMethod("GetSqlIdentity", typeof(long), new Type[] { type }, type, true);
                ILGenerator   generator     = dynamicMethod.GetILGenerator();
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldfld, Identity.FieldInfo);
                if (Identity.FieldInfo.FieldType != typeof(long) && Identity.FieldInfo.FieldType != typeof(ulong))
                {
                    generator.Emit(OpCodes.Conv_I8);
                }
                generator.Emit(OpCodes.Ret);
                GetIdentity = (Func <valueType, long>)dynamicMethod.CreateDelegate(typeof(Func <valueType, long>));

                dynamicMethod = new DynamicMethod("SetSqlIdentity", null, new Type[] { type, typeof(long) }, type, true);
                generator     = dynamicMethod.GetILGenerator();
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldarg_1);
                if (Identity.FieldInfo.FieldType != typeof(long) && Identity.FieldInfo.FieldType != typeof(ulong))
                {
                    generator.Emit(OpCodes.Conv_I4);
                }
                generator.Emit(OpCodes.Stfld, Identity.FieldInfo);
                generator.Emit(OpCodes.Ret);
                SetIdentity = (Action <valueType, long>)dynamicMethod.CreateDelegate(typeof(Action <valueType, long>));

                GetIdentity32 = getIdentityGetter32("GetSqlIdentity32", Identity.FieldInfo);
#endif
            }
        }
コード例 #6
0
        static Getter()
        {
            Type type = typeof(valueType);

            if (type.IsArray || type.IsEnum || type.IsPointer || type.IsInterface || typeof(Delegate).IsAssignableFrom(type))
            {
                return;
            }
            foreach (AttributeMethod methodInfo in AttributeMethod.GetStatic(type))
            {
                if (methodInfo.Method.ReturnType == typeof(void))
                {
                    ParameterInfo[] parameters = methodInfo.Method.GetParameters();
                    if (parameters.Length == 2 && parameters[0].ParameterType == type && parameters[1].ParameterType == typeof(NameValueCollection))
                    {
                        if (methodInfo.GetAttribute <Form.CustomAttribute>() != null)
                        {
                            getter = (Action <valueType, NameValueCollection>)Delegate.CreateDelegate(typeof(Action <valueType, NameValueCollection>), methodInfo.Method);
                            return;
                        }
                    }
                }
            }
            Form.FormAttribute     attribute = TypeAttribute.GetAttribute <Form.FormAttribute>(type, true) ?? Form.FormAttribute.AllMember;
            LeftArray <FieldIndex> fields    = Emit.Pub.GetFields <valueType, MemberAttribute>(attribute.Filter, attribute.IsAllMember);

            if ((memberCount = fields.Length) != 0)
            {
#if NOJIT
                getter = new MemberGetter(ref fields).Get;
#else
                GetterDynamicMethod dynamicMethod = new GetterDynamicMethod(type);
                foreach (FieldIndex member in fields)
                {
                    dynamicMethod.Push(member.Member);
                }
                getter = (Action <valueType, NameValueCollection>)dynamicMethod.Create <Action <valueType, NameValueCollection> >();
#endif
            }
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
        /// <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);
        }
コード例 #9
0
        /// <summary>
        /// 添加SQL列类型
        /// </summary>
        /// <param name="type">SQL列类型</param>
        private void append(Type type)
        {
            foreach (KeyValue <MemberIndexInfo, MemberAttribute> member in Field.GetMemberIndexs(type, TypeAttribute.GetAttribute <ColumnAttribute>(type, false)))
            {
                Column column = Client.GetColumn(member.Key.Member.Name, member.Key.MemberSystemType, member.Value);
                if (column.SqlColumnType == null)
                {
                    Columns.Add(column);
                }
                else
                {
                    foreach (Column sqlColumn in getNoLock(column.SqlColumnType))
                    {
                        Column copyColumn = AutoCSer.MemberCopy.Copyer <Column> .MemberwiseClone(sqlColumn);

                        copyColumn.Name = column.Name + "_" + copyColumn.Name;
                        Columns.Add(copyColumn);
                    }
                }
            }
        }
コード例 #10
0
        static HeaderQueryTypeParser()
        {
            Type type = typeof(valueType);

            AutoCSer.Json.ParseAttribute attribute = TypeAttribute.GetAttribute <AutoCSer.Json.ParseAttribute>(type, true) ?? AutoCSer.Json.Parser.AllMemberAttribute;
            FieldIndex             defaultMember   = null;
            LeftArray <FieldIndex> fields          = AutoCSer.Json.ParseMethodCache.GetFields(MemberIndexGroup <valueType> .GetFields(attribute.MemberFilters), attribute, ref defaultMember);
            LeftArray <KeyValue <PropertyIndex, MethodInfo> > properties = AutoCSer.Json.ParseMethodCache.GetProperties(MemberIndexGroup <valueType> .GetProperties(attribute.MemberFilters), attribute);

            memberParsers = new TryParse[fields.Length + properties.Length + (defaultMember == null ? 0 : 1)];
            string[] names = new string[memberParsers.Length];
            int      index = 0, nameLength = 0, maxNameLength = 0;

            foreach (FieldIndex member in fields)
            {
#if NOJIT
                TryParse tryParse = new HeaderQueryFieldParser(member.Member).Parser();
#else
                ILGenerator   generator;
                DynamicMethod memberDynamicMethod = HeaderQueryParser.CreateDynamicMethod(type, member.Member.Name, member.Member.FieldType, out generator);
                generator.Emit(OpCodes.Stfld, member.Member);
                generator.Emit(OpCodes.Ret);
                TryParse tryParse = (TryParse)memberDynamicMethod.CreateDelegate(typeof(TryParse));
#endif
                memberParsers[index] = tryParse;
                if (member.Member.Name.Length > maxNameLength)
                {
                    maxNameLength = member.Member.Name.Length;
                }
                nameLength += (names[index++] = member.Member.Name).Length;
                if (member == defaultMember)
                {
                    memberParsers[names.Length - 1] = tryParse;
                    names[names.Length - 1]         = string.Empty;
                }
            }
            foreach (KeyValue <PropertyIndex, MethodInfo> member in properties)
            {
#if NOJIT
                memberParsers[index] = new HeaderQueryPropertyParser(member.Key.Member).Parser();
#else
                ILGenerator   generator;
                DynamicMethod memberDynamicMethod = HeaderQueryParser.CreateDynamicMethod(type, member.Key.Member.Name, member.Key.Member.PropertyType, out generator);
                generator.call(member.Value);
                generator.Emit(OpCodes.Ret);
                memberParsers[index] = (TryParse)memberDynamicMethod.CreateDelegate(typeof(TryParse));
#endif
                if (member.Key.Member.Name.Length > maxNameLength)
                {
                    maxNameLength = member.Key.Member.Name.Length;
                }
                nameLength += (names[index++] = member.Key.Member.Name).Length;
            }
            if (maxNameLength > short.MaxValue || nameLength == 0)
            {
                memberNames = Unmanaged.NullByte8;
            }
            else
            {
                memberNames = new Pointer {
                    Data = Unmanaged.GetStatic(nameLength + (names.Length - (defaultMember == null ? 0 : 1)) * sizeof(short) + sizeof(short), false)
                };
                byte *write = memberNames.Byte;
                foreach (string name in names)
                {
                    if (name.Length != 0)
                    {
                        *(short *)write = (short)name.Length;

                        fixed(char *nameFixed = name) StringExtension.WriteBytesNotNull(nameFixed, name.Length, write + sizeof(short));

                        write += sizeof(short) + name.Length;
                    }
                }
                *(short *)write = 0;
            }
            memberSearcher = new HeaderQueryParseStateSearcher(AutoCSer.Json.StateSearcher.GetMemberSearcher(type, names));
        }