Example #1
0
        /// <summary>
        /// 获取自增字段成员
        /// </summary>
        /// <param name="type"></param>
        /// <param name="model"></param>
        /// <returns>自增字段成员</returns>
        public static memberInfo GetIdentity <modeType>(Type type, modeType model) where modeType : dataModel
        {
            fieldIndex[] fields   = (fieldIndex[])typeof(fastCSharp.code.memberIndexGroup <>).MakeGenericType(type).GetMethod("GetFields", BindingFlags.Static | BindingFlags.Public).Invoke(null, new object[] { model.MemberFilter });
            memberInfo   identity = null;
            int          isCase   = 0;

            foreach (fieldIndex field in fields)
            {
                type = field.Member.FieldType;
                if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !field.IsIgnore)
                {
                    dataMember attribute = field.GetSetupAttribute <dataMember>(true, true);
                    if (attribute != null && attribute.IsIdentity)
                    {
                        return(new memberInfo(field.Member, 0));
                    }
                    if (isCase == 0 && field.Member.Name == fastCSharp.config.sql.Default.DefaultIdentityName)
                    {
                        identity = new memberInfo(field.Member, 0);
                        isCase   = 1;
                    }
                    else if (identity == null && field.Member.Name.ToLower() == fastCSharp.config.sql.Default.DefaultIdentityName)
                    {
                        identity = new memberInfo(field.Member, 0);
                    }
                }
            }
            return(identity);
        }
Example #2
0
        /// <summary>
        /// 成员信息转换为数据列
        /// </summary>
        /// <param name="type">成员类型</param>
        /// <param name="sqlMember">SQL成员信息</param>
        /// <returns>数据列</returns>
        internal override column getColumn(Type type, dataMember sqlMember)
        {
            SqlDbType  sqlType    = SqlDbType.NVarChar;
            int        size       = sqlMember.MaxStringLength;
            memberType memberType = sqlMember.DataType != null ? sqlMember.DataType : type;

            if (memberType.IsString)
            {
                if (size > 0)
                {
                    sqlType = SqlDbType.NVarChar;
                }
                else
                {
                    sqlType = SqlDbType.NText;
                    size    = int.MaxValue;
                }
            }
            else
            {
                sqlType = memberType.Type.formCSharpType();
                size    = sqlType.getSize();
            }
            return(new column
            {
                DbType = sqlType,
                Size = size,
                IsNull = (sqlMember.IsDefaultMember && !memberType.IsString ? ((memberType)type).IsNull : sqlMember.IsNull),
                DefaultValue = sqlMember.DefaultValue,
                UpdateValue = sqlMember.UpdateValue
            });
        }
Example #3
0
        /// <summary>
        /// 获取字段成员集合
        /// </summary>
        /// <param name="fields"></param>
        /// <returns>字段成员集合</returns>
        internal static subArray <fieldInfo> GetFields(fieldIndex[] fields)
        {
            subArray <fieldInfo> values = new subArray <fieldInfo>(fields.Length);

            foreach (fieldIndex field in fields)
            {
                Type type = field.Member.FieldType;
                if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !field.IsIgnore)
                {
                    dataMember attribute = field.GetAttribute <dataMember>(true, true);
                    if (attribute == null || attribute.IsSetup)
                    {
                        values.Add(new fieldInfo(field, attribute));
                    }
                }
            }
            return(values);
        }
Example #4
0
        /// <summary>
        /// 获取字段成员集合
        /// </summary>
        /// <param name="type"></param>
        /// <param name="model"></param>
        /// <returns>字段成员集合</returns>
        public static subArray <memberInfo> GetPrimaryKeys <modeType>(Type type, modeType model) where modeType : dataModel
        {
            fieldIndex[]          fields = (fieldIndex[])typeof(fastCSharp.code.memberIndexGroup <>).MakeGenericType(type).GetMethod("GetFields", BindingFlags.Static | BindingFlags.Public).Invoke(null, new object[] { model.MemberFilter });
            subArray <memberInfo> values = new subArray <memberInfo>();

            foreach (fieldIndex field in fields)
            {
                type = field.Member.FieldType;
                if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !field.IsIgnore)
                {
                    dataMember attribute = field.GetSetupAttribute <dataMember>(true, true);
                    if (attribute != null && attribute.PrimaryKeyIndex != 0)
                    {
                        values.Add(new memberInfo(field.Member, attribute.PrimaryKeyIndex));
                    }
                }
            }
            return(values.Sort((left, right) =>
            {
                int value = left.MemberIndex - right.MemberIndex;
                return value == 0 ? left.MemberName.CompareTo(right.MemberName) : value;
            }));
        }
Example #5
0
            /// <summary>
            /// 字段信息
            /// </summary>
            /// <param name="field">字段信息</param>
            /// <param name="attribute">数据库成员信息</param>
            internal fieldInfo(fieldIndex field, dataMember attribute)
            {
                Field          = field.Member;
                MemberMapIndex = field.MemberIndex;
                DataMember     = dataMember.FormatSql(attribute, Field.FieldType, ref IsSqlColumn);
                if ((NullableDataType = DataMember.DataType) == null)
                {
                    NullableDataType = Field.FieldType;
                }
                if ((DataReaderMethod = fastCSharp.emit.pub.GetDataReaderMethod(DataType = NullableDataType.nullableType() ?? NullableDataType)) == null)
                {
                    if (IsSqlColumn)
                    {
                        if (isSqlColumn(DataType))
                        {
                            return;
                        }
                        IsSqlColumn = false;
                    }
#if NOJIT
                    DataType         = NullableDataType = typeof(string);
                    DataReaderMethod = fastCSharp.emit.pub.GetDataReaderMethodInfo;
#else
                    DataReaderMethod = fastCSharp.emit.pub.GetDataReaderMethod(DataType = NullableDataType = typeof(string));
#endif
                    IsUnknownJson     = true;
                    ToSqlCastMethod   = fastCSharp.emit.jsonSerializer.SqlToJsonMethod.MakeGenericMethod(Field.FieldType);
                    ToModelCastMethod = fastCSharp.emit.jsonParser.SqlParseMethod.MakeGenericMethod(Field.FieldType);
                }
                else
                {
                    ToSqlCastMethod   = fastCSharp.emit.pub.GetCastMethod(Field.FieldType, DataType);
                    ToModelCastMethod = fastCSharp.emit.pub.GetCastMethod(DataType, Field.FieldType);
                }
                SqlFieldName = new fastCSharp.stateSearcher.asciiSearcher(keywords).Search(Field.Name.ToLower()) < 0 ? Field.Name : toSqlName(Field.Name);
                ToSqlMethod  = fastCSharp.emit.pub.GetSqlConverterMethod(DataType);
            }
Example #6
0
 /// <summary>
 /// 字段信息
 /// </summary>
 /// <param name="field"></param>
 /// <param name="attribute"></param>
 public fieldInfo(fieldIndex field, dataMember attribute)
 {
     Field          = field.Member;
     DataMember     = attribute;
     MemberMapIndex = field.MemberIndex;
 }
Example #7
0
            public static bool Verify(modelType value, MemberMap memberMap, Table sqlTool)
            {
#if NOJIT
                if (fields != null)
                {
                    object[] sqlColumnParameters = null, castParameters = null;
                    foreach (sqlModel.verifyField field in fields)
                    {
                        AutoCSer.code.cSharp.sqlModel.fieldInfo fieldInfo = field.Field;
                        if (memberMap.IsMember(fieldInfo.MemberMapIndex))
                        {
                            object memberValue = fieldInfo.Field.GetValue(value);
                            if (field.IsSqlVerify)
                            {
                                if (!fieldInfo.Field.FieldType.IsValueType && memberValue == null)
                                {
                                    return(false);
                                }
                                if (!(bool)((AutoCSer.emit.sqlTable.ISqlVerify)memberValue).IsSqlVeify())
                                {
                                    return(false);
                                }
                            }
                            else if (fieldInfo.IsSqlColumn)
                            {
                                if (sqlColumnParameters == null)
                                {
                                    sqlColumnParameters = new object[] { null, sqlTool, null }
                                }
                                ;
                                sqlColumnParameters[0] = memberValue;
                                sqlColumnParameters[2] = fieldInfo.Field.Name;
                                if (!(bool)field.SqlColumnMethod.Invoke(null, sqlColumnParameters))
                                {
                                    return(false);
                                }
                            }
                            else if (fieldInfo.DataType == typeof(string))
                            {
                                if (fieldInfo.ToSqlCastMethod != null)
                                {
                                    if (castParameters == null)
                                    {
                                        castParameters = new object[1];
                                    }
                                    castParameters[0] = memberValue;
                                    memberValue       = fieldInfo.ToSqlCastMethod.Invoke(null, castParameters);
                                }
                                dataMember dataMember = fieldInfo.DataMember;
                                if (!sqlTool.StringVerify(fieldInfo.Field.Name, (string)memberValue, dataMember.MaxStringLength, dataMember.IsAscii, dataMember.IsNull))
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                if (fieldInfo.ToSqlCastMethod != null && !fieldInfo.IsUnknownJson)
                                {
                                    if (castParameters == null)
                                    {
                                        castParameters = new object[1];
                                    }
                                    castParameters[0] = memberValue;
                                    memberValue       = fieldInfo.ToSqlCastMethod.Invoke(null, castParameters);
                                }
                                if (memberValue == null)
                                {
                                    sqlTool.NullVerify(fieldInfo.Field.Name);
                                    return(false);
                                }
                            }
                        }
                    }
                }
                return(true);
#else
                return(verifyer == null || verifyer(value, memberMap, sqlTool));
#endif
            }
Example #8
0
            public static bool Verify(valueType value, Table sqlTool, string columnName)
            {
#if NOJIT
                if (fields != null)
                {
                    string[] columnNames         = GetColumnNames(columnName);
                    object[] sqlColumnParameters = null, castParameters = null;
                    object   objectValue         = value;
                    int      index = 0;
                    foreach (sqlColumn.verifyDynamicMethod.field field in fields)
                    {
                        AutoCSer.code.cSharp.sqlModel.fieldInfo fieldInfo = field.Field;
                        object memberValue = fieldInfo.Field.GetValue(objectValue);
                        if (fieldInfo.IsSqlColumn)
                        {
                            if (sqlColumnParameters == null)
                            {
                                sqlColumnParameters = new object[] { null, sqlTool, null }
                            }
                            ;
                            sqlColumnParameters[0] = memberValue;
                            sqlColumnParameters[2] = columnNames[index];
                            if (!(bool)field.SqlColumnMethod.Invoke(null, sqlColumnParameters))
                            {
                                return(false);
                            }
                        }
                        else if (fieldInfo.DataType == typeof(string))
                        {
                            if (fieldInfo.ToSqlCastMethod != null)
                            {
                                if (castParameters == null)
                                {
                                    castParameters = new object[1];
                                }
                                castParameters[0] = memberValue;
                                memberValue       = fieldInfo.ToSqlCastMethod.Invoke(null, castParameters);
                            }
                            dataMember dataMember = fieldInfo.DataMember;
                            if (!sqlTool.StringVerify(columnNames[index], (string)memberValue, dataMember.MaxStringLength, dataMember.IsAscii, dataMember.IsNull))
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            if (fieldInfo.ToSqlCastMethod != null && !fieldInfo.IsUnknownJson)
                            {
                                if (castParameters == null)
                                {
                                    castParameters = new object[1];
                                }
                                castParameters[0] = memberValue;
                                memberValue       = fieldInfo.ToSqlCastMethod.Invoke(null, castParameters);
                            }
                            if (memberValue == null)
                            {
                                sqlTool.NullVerify(columnNames[index]);
                                return(false);
                            }
                        }
                        ++index;
                    }
                    return(true);
                }
#else
                if (verifyer != null)
                {
                    return(verifyer(value, sqlTool, GetColumnNames(columnName)));
                }
#endif
                return(custom == null || custom.Verify(value, sqlTool, columnName));
            }