internal static string CreateColumn(DBExtend helper, Attribute.FieldAttribute item) { var dbAdapter = helper._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 { helper.Execute(str); if (!string.IsNullOrEmpty(indexScript)) { helper.Execute(indexScript); } result += string.Format("创建字段:{0}\r\n", item.Name); } catch (Exception ero) { //CoreHelper.EventLog.Log("创建字段时发生错误:" + ero.Message); result += string.Format("创建字段:{0} 发生错误:{1}\r\n", item.Name, ero.Message); } return(result); }
internal static void SetColumnDbType(DBAdapter.DBAdapterBase dbAdapter, Attribute.FieldAttribute 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 CRLException(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.FieldAttribute item) { var dbAdapter = db._DBAdapter; string result = ""; if (string.IsNullOrEmpty(item.ColumnType)) { throw new CRLException("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); } CoreHelper.EventLog.Log(result, "", false); } catch (Exception ero) { //CoreHelper.EventLog.Log("创建字段时发生错误:" + ero.Message); result = string.Format("创建字段:{0} {1}发生错误:{2}\r\n", item.TableName, item.MemberName, ero.Message); } return(result); }
internal static void SetColumnDbType(DBAdapter.DBAdapterBase dbAdapter, Attribute.FieldAttribute info, Dictionary <Type, string> dic) { if (info.FieldType != Attribute.FieldType.数据库字段) { return; } string defaultValue; Type propertyType = info.PropertyType; if (propertyType.FullName.IndexOf("System.") > -1 && !dic.ContainsKey(propertyType)) { throw new Exception(string.Format("找不到对应的字段类型映射 {0} 在 {1}", propertyType, dbAdapter)); } 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.Name, info.ColumnType)); } }
static void SetProperties(Attribute.TableAttribute table) { if (table.Fields.Count > 0) { return; } var type = table.Type; List <Attribute.FieldAttribute> list = new List <CRL.Attribute.FieldAttribute>(); var fieldDic = new IgnoreCaseDictionary <Attribute.FieldAttribute>(); //string fieldPat = @"^([A-Z][a-z|\d]+)+$"; int n = 0; Attribute.FieldAttribute 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; Attribute.FieldAttribute f = new CRL.Attribute.FieldAttribute(); //排除集合类型 if (propertyType.FullName.IndexOf("System.Collections") > -1) { continue; } object[] attrs = info.GetCustomAttributes(typeof(Attribute.FieldAttribute), true); if (attrs != null && attrs.Length > 0) { f = attrs[0] as Attribute.FieldAttribute; } f.SetPropertyInfo(info); f.PropertyType = propertyType; f.MemberName = info.Name; f.TableName = table.TableName; f.ModelType = table.Type; //if (!string.IsNullOrEmpty(f.VirtualField)) //{ // if (SettingConfig.StringFormat != null) // { // f.VirtualField = SettingConfig.StringFormat(f.VirtualField); // } // f.VirtualField = f.VirtualField.Replace("$", "{" + type.FullName + "}");//虚拟字段使用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 (f.FieldType != Attribute.FieldType.关联字段) //{ fieldDic.Add(f.MemberName, f); //} f.MapingNameFormat = string.Format("[{0}]", f.MapingName); 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; }