internal static void SetColumnDbType(DBAdapter.DBAdapterBase dbAdapter, Attribute.FieldInnerAttribute info) { //if (info.FieldType != Attribute.FieldType.数据库字段) //{ // return; //} string defaultValue; Type propertyType = info.PropertyType; var columnType = dbAdapter.GetColumnType(info, out defaultValue); info.ColumnType = columnType; info.DefaultValue = defaultValue; if (info.ColumnType.Contains("{0}")) { throw new Exception(string.Format("属性:{0} 需要指定长度 ColumnType:{1}", info.MemberName, info.ColumnType)); } }
/// <summary> /// 创建列 /// </summary> /// <param name="db"></param> /// <param name="item"></param> /// <returns></returns> internal static string CreateColumn(AbsDBExtend db, Attribute.FieldInnerAttribute item) { var dbAdapter = db._DBAdapter; string result = ""; if (string.IsNullOrEmpty(item.ColumnType)) { throw new Exception("ColumnType is null"); } string str = dbAdapter.GetCreateColumnScript(item); string indexScript = ""; if (item.FieldIndexType != Attribute.FieldIndexType.无) { indexScript = dbAdapter.GetColumnIndexScript(item); } try { db.Execute(str); if (!string.IsNullOrEmpty(indexScript)) { db.Execute(indexScript); } result = string.Format("创建字段:{0} {1} {2}\r\n", item.TableName, item.MemberName, item.PropertyType); var model = System.Activator.CreateInstance(item.ModelType) as IModel; try { model.OnColumnCreated(item.MemberName); } catch (Exception ero) { result = string.Format("添加字段:{0} {1},升级数据时发生错误:{2}\r\n", item.TableName, item.MemberName, ero.Message); } EventLog.Log(result, "", false); } catch (Exception ero) { //EventLog.Log("创建字段时发生错误:" + ero.Message); result = string.Format("创建字段:{0} {1}发生错误:{2}\r\n", item.TableName, item.MemberName, ero.Message); } return(result); }
static void InitProperties(Attribute.TableInnerAttribute table) { if (table.Fields.Count > 0) { return; } var type = table.Type; List <Attribute.FieldInnerAttribute> list = new List <CRL.Attribute.FieldInnerAttribute>(); var fieldDic = new IgnoreCaseDictionary <Attribute.FieldInnerAttribute>(); //string fieldPat = @"^([A-Z][a-z|\d]+)+$"; int n = 0; Attribute.FieldInnerAttribute keyField = null; #region 读取 var typeArry = table.Type.GetProperties().ToList(); //移除重复的 var dic = new Dictionary <string, PropertyInfo>(); foreach (PropertyInfo info in typeArry) { if (!dic.ContainsKey(info.Name)) { dic.Add(info.Name, info); } } foreach (PropertyInfo info in dic.Values) { //if (!System.Text.RegularExpressions.Regex.IsMatch(info.Name, fieldPat)) //{ // throw new CRLException(string.Format("属性名:{0} 不符合规则:{1}", info.Name, fieldPat)); //} //排除没有SET方法的属性 if (info.GetSetMethod() == null) { continue; } Type propertyType = info.PropertyType; var f = new CRL.Attribute.FieldInnerAttribute(); //排除集合类型 if (propertyType.FullName.IndexOf("System.Collections") > -1) { continue; } object[] attrs = info.GetCustomAttributes(typeof(Attribute.FieldAttribute), true); if (attrs != null && attrs.Length > 0) { var atr = attrs[0] as Attribute.FieldAttribute; atr.MemberName = info.Name; f = atr.ToType <Attribute.FieldInnerAttribute>(); } f.SetPropertyInfo(info); f.PropertyType = propertyType; f.MemberName = info.Name; f.TableName = table.TableName; f.ModelType = table.Type; //排除不映射字段 if (!f.MapingField) { continue; } if (propertyType == typeof(System.String)) { if (f.Length == 0) { f.Length = 30; } } if (f.IsPrimaryKey)//保存主键 { table.PrimaryKey = f; f.FieldIndexType = Attribute.FieldIndexType.非聚集唯一; keyField = f; n += 1; } if (!fieldDic.ContainsKey(f.MemberName)) { fieldDic.Add(f.MemberName, f); } list.Add(f); } if (n == 0) { //throw new CRLException(string.Format("对象{0}未设置任何主键", type.Name)); } else if (n > 1) { throw new CRLException(string.Format("对象{0}设置的主键字段太多 {1}", type.Name, n)); } #endregion //主键排前面 if (keyField != null) { list.Remove(keyField); list.Insert(0, keyField); } table.Fields = list; table.FieldsDic = fieldDic; }