private static List <IValidationError> ValidateRules <TModel>( PropertyInfo property, ParsedModel <TModel> parsedModel, ColumnMapping columnMapping) where TModel : new() { var modelValidationErrors = new List <IValidationError>(); var propertyValue = property.GetValue(parsedModel.Value); GetRulesOfType <IGenericRule>(columnMapping) .ForEach(genericRule => genericRule .Validate(columnMapping.ColumnIndex, parsedModel.RowIndex, columnMapping.DisplayName, property.Name, propertyValue) .OnFailure(modelValidationErrors.Add)); GetRulesOfType <IComparableRule>(columnMapping) .ForEach(comparableRule => comparableRule .Validate(columnMapping.ColumnIndex, parsedModel.RowIndex, columnMapping.DisplayName, property.Name, propertyValue) .OnFailure(modelValidationErrors.Add)); GetRulesOfType <ICustomRule>(columnMapping) .ForEach(customRule => customRule .Validate(columnMapping.ColumnIndex, parsedModel.RowIndex, columnMapping.DisplayName, property.Name, propertyValue) .OnFailure(modelValidationErrors.Add)); return(modelValidationErrors); }
private int Insert(Type type, object obj) { ParsedModel pm = GetParsedModel(type); Dictionary <string, FieldAttribute> fas = pm.StorageableFields; string columns = ""; string values = ""; int len = fas.Count; FieldAttribute fa; PropertyInfo pi; bool notLast; int i = 0; foreach (string key in fas.Keys) { i++; fa = fas[key]; pi = fa.PropertyInfo; if (fa.IsPrimaryKey) { if (fa.IdentityInsert) { continue; } else { object temp = pi.GetValue(obj); if (temp == null || temp.ToString() == "" || (int)temp == 0) { int new_id = int.Parse(GetMax(type, fa.FieldName, pm.Model.BaseID).ToString()) + 1; pi.SetValue(obj, Convert.ChangeType(new_id, pi.PropertyType)); } } } notLast = i < len; columns += "[" + fa.FieldName + "]" + (notLast ? "," : ""); // 非空字段,检测是否为null,是null则应用默认值 object temp_value = pi.GetValue(obj); if (temp_value == null) { temp_value = fa.Default; } values += "'" + temp_value + "'" + (notLast ? "," : ""); } string sql_str = string.Format("insert into [{0}] ({1}) values ({2});select @@IDENTITY", pm.Model.TableName, columns, values); SqlResult sr = Get(sql_str); if (pm.Model.IdentityInsert) { if ((pi = type.GetProperty(pm.Model.PrimaryKey)) != null) { pi.SetValue(obj, Convert.ChangeType(sr.ScalarValue, pi.PropertyType)); } } return(1); }
/// <summary> /// Initializes a new instance of the <see cref="ModelViewer"/> class. /// </summary> /// <param name="temppm">The temppm.</param> /// <remarks></remarks> public ModelViewer(ParsedModel temppm) { // Set the initial size of our form this.ClientSize = new Size(400, 300); // And its caption this.Text = "Model Viewer"; pm = temppm; this.MouseDown += ModelViewer_MouseDown; this.MouseMove += this.ModelViewer_MouseDownx; this.MouseUp += this.ModelViewer_MouseUp; Main(); }
public int Update <T>(T obj) where T : class, new() { ParsedModel pm = GetParsedModel(typeof(T)); PropertyInfo pi; FieldAttribute fa; string id = ""; if ((pi = typeof(T).GetProperty(pm.Model.PrimaryKey)) != null) { id = pi.GetValue(obj).ToString(); } if (id == "") { throw new Exception(string.Format("更新表数据时,未向实例提供主键值,表:{0},主键:{1}", typeof(T).Name, pm.Model.PrimaryKey)); } Dictionary <string, FieldAttribute> fas = pm.StorageableFields; string keyvalues = ""; int len = fas.Count; bool notLast; int i = 0; foreach (string key in fas.Keys) { i++; fa = fas[key]; pi = fa.PropertyInfo; if (fa.IsPrimaryKey) { continue; } notLast = i < len; // 非空字段,检测是否为null,是null则应用默认值 object temp_value = pi.GetValue(obj); if (temp_value == null) { temp_value = fa.Default; } keyvalues += string.Format("[{0}]='{1}'", fa.FieldName, temp_value) + (notLast ? "," : ""); } string sql_str = string.Format("update [{0}] set {1} where [{2}]='{3}'", pm.Model.TableName, keyvalues, pm.Model.PrimaryKey, id); return(Set(sql_str).EffectedLineCount); }
public Stream Parse(InputModel model) { var reversedNumber = model.NumberAsString.Reverse(); var parsedModel = new ParsedModel(); var actions = new Dictionary<int, Action<ParsedModel, string>> { {0, (pm, value) => pm.Ones = value}, {1, (pm, value) => pm.Tens = value}, {2, (pm, value) => pm.Hundreds = value}, {3, (pm, value) => pm.Thousands = value}, {4, (pm, value) => pm.TenThousands = value}, {5, (pm, value) => pm.HundredThousands = value}, {6, (pm, value) => pm.Millions = value}, {7, (pm, value) => pm.TenMillions = value}, {8, (pm, value) => pm.HundredMillions = value}, {9, (pm, value) => pm.Billions = value}, {10, (pm, value) => pm.TenBillions = value}, {11, (pm, value) => pm.HundredBillions = value} }; }
private void CheckFields(Type model_type) { ParsedModel pm = GetParsedModel(model_type); ModelAttribute ma = GetParsedModel(model_type).Model; Dictionary <string, FieldAttribute> fas = pm.StorageableFields; DataBaseUpdateLogs.Add(string.Format("表{0}已存在,正在对比字段", ma.TableName)); foreach (string key in fas.Keys) { FieldAttribute fa = fas[key]; if (TestFieldExists(ma.TableName, fa.FieldName)) { CheckFieldDataType(ma.TableName, fa.FieldName, fa.DataType, fa.IsPrimaryKey, ma.IdentityInsert, fa.Nullable); } else { AddField(ma.TableName, fa.FieldName, fa.DataType, fa.IsPrimaryKey, ma.IdentityInsert, fa.Nullable); } } }
private void CreateTable(Type model_type) { ParsedModel pm = GetParsedModel(model_type); ModelAttribute ma = GetParsedModel(model_type).Model; Dictionary <string, FieldAttribute> fas = pm.StorageableFields; string columns_str = ""; int len = fas.Count; int i = 0; foreach (string key in fas.Keys) { i++; FieldAttribute fa = fas[key]; columns_str += string.Format("{0} {1} {2} {3}", fa.FieldName, fa.DataType, fa.IsPrimaryKey? (ma.IdentityInsert? "identity(1,1)" : "") + " PRIMARY KEY" : "", fa.Nullable? "" : "NOT NULL"); if (i != len) { columns_str += ","; } } string table_str = string.Format("create table {0} ({1})", ma.TableName, columns_str); Set(table_str); DataBaseUpdateLogs.Add("创建表" + ma.TableName); }
// 获得解析后的Model数据 public static ParsedModel GetParsedModel(Type type) { if (!ParsedModels.ContainsKey(type)) { ParsedModel temp = new ParsedModel(); ModelAttribute m = type.GetCustomAttribute <ModelAttribute>(); if (m == null) { throw new Exception(string.Format("模型类{0}未使用ModelAttribute,[Model(TableName = \"t_article\", PrimaryKey = \"id\", IdentityInsert = true)]", type.Name)); } else { temp.Model = m; } temp.WebableFields = new Dictionary <string, FieldAttribute>(); temp.StorageableFields = new Dictionary <string, FieldAttribute>(); PropertyInfo[] properties = type.GetProperties(); // 获得此模型的公共属性 foreach (PropertyInfo pi in properties) { FieldAttribute fa = pi.GetCustomAttribute <FieldAttribute>(); if (fa == null) { fa = new FieldAttribute(); } if (fa.FieldName == null || fa.FieldName == "") { fa.FieldName = pi.Name; // 检测是否指定了数据库表名 } if (fa.FieldName == m.PrimaryKey) // 检测是否是主键 { fa.SetIsPrimaryKey(true); fa.SetIdentityInsert(m.IdentityInsert); } if (fa.IsPrimaryKey) // 如果是主键则不能为空 { fa.Nullable = false; } else if (fa.Nullable == false && fa.Default == null) // 如果可为空,则必须指定默认值 { throw new Exception(string.Format("模型类{0}的字段{1}不可为空,需要提供Default默认值", m.TableName, pi.Name)); } fa.SetPropertyInfo(pi); if (fa.DataType == null) { fa.DataType = GetDataType(fa.PropertyInfo.PropertyType); // 如果未指定数据类型,则指定默认类型 } if (fa.Storageable) { temp.StorageableFields.Add(pi.Name, fa); } if (fa.Webable) { temp.WebableFields.Add(pi.Name, fa); } } ParsedModels.Add(type, temp); } return(ParsedModels[type]); }
/// <summary> /// The make mesh. /// </summary> /// <param name="pm">The pm.</param> /// <param name="chunknumber">The chunknumber.</param> /// <param name="device">The device.</param> /// <returns></returns> /// <remarks></remarks> public static Mesh MakeMesh(ref ParsedModel pm, int chunknumber, ref Device device) { short[] indices; int[] facecount = new int[pm.RawDataMetaChunks[chunknumber].SubMeshInfo.Length]; int[] facestart = new int[pm.RawDataMetaChunks[chunknumber].SubMeshInfo.Length]; if (pm.RawDataMetaChunks[chunknumber].FaceCount * 3 != pm.RawDataMetaChunks[chunknumber].Indices.Length) { short[] tempindicesx = new short[pm.RawDataMetaChunks[chunknumber].FaceCount * 3]; int tempc = 0; for (int x = 0; x < pm.RawDataMetaChunks[chunknumber].SubMeshInfo.Length; x++) { short[] tempindices = DecompressIndices( pm.RawDataMetaChunks[chunknumber].Indices, pm.RawDataMetaChunks[chunknumber].SubMeshInfo[x].IndiceStart, pm.RawDataMetaChunks[chunknumber].SubMeshInfo[x].IndiceCount); Array.ConstrainedCopy(tempindices, 0, tempindicesx, tempc, tempindices.Length); facecount[x] = tempindices.Length / 3; facestart[x] = tempc; tempc += tempindices.Length; } indices = new short[tempindicesx.Length]; Array.Copy(tempindicesx, indices, tempindicesx.Length); } else { indices = pm.RawDataMetaChunks[chunknumber].Indices; for (int x = 0; x < pm.RawDataMetaChunks[chunknumber].SubMeshInfo.Length; x++) { facecount[x] = pm.RawDataMetaChunks[chunknumber].SubMeshInfo[x].IndiceCount / 3; facestart[x] = pm.RawDataMetaChunks[chunknumber].SubMeshInfo[x].IndiceStart / 3; } } Mesh m = new Mesh( indices.Length / 3, pm.RawDataMetaChunks[chunknumber].Vertices.Count, MeshFlags.Managed, HaloVertex.FVF, device); VertexBuffer vb = m.VertexBuffer; HaloVertex[] tempv = new HaloVertex[pm.RawDataMetaChunks[chunknumber].Vertices.Count]; for (int x = 0; x < pm.RawDataMetaChunks[chunknumber].Vertices.Count; x++) { tempv[x].Position = pm.RawDataMetaChunks[chunknumber].Vertices[x]; tempv[x].Normal = pm.RawDataMetaChunks[chunknumber].Normals[x]; tempv[x].Tu0 = pm.RawDataMetaChunks[chunknumber].UVs[x].X; tempv[x].Tv0 = pm.RawDataMetaChunks[chunknumber].UVs[x].Y; } vb.SetData(tempv, 0, LockFlags.None); vb.Unlock(); IndexBuffer ibx = m.IndexBuffer; ibx.SetData(indices, 0, LockFlags.None); return(m); }