/// <summary> /// 初始化数据库 /// </summary> public static void InitDB() { //if (_isInit) //{ // return; //} Type type = typeof(T); DataAccessLoader.AppendModelAssembly(type.Assembly); DataAccessLoader.InitConfig(); _db = GetDB(); Type baseType = typeof(BQLEntityTableHandle); PropertyInfo[] infos = type.GetProperties(BindingFlags.Public | BindingFlags.Static); foreach (PropertyInfo info in infos) { Type objType = info.PropertyType; if (!objType.IsSubclassOf(baseType)) { continue; } BQLEntityTableHandle handle = FastValueGetSet.GetGetMethodInfo(info.Name, type).Invoke(null, new object[] { }) as BQLEntityTableHandle; AddToDB(handle); } StaticConnection.ClearCacheOperate(_db); #if DEBUG _db.SqlOutputer.OnOutputerCreate += new Buffalo.DB.MessageOutPuters.CreateOutputerHandle(SqlOutputer_OnOutputerCreate); #endif }
/// <summary> /// 插入一个实体 /// </summary> /// <param name="entity">要插入的实体</param> /// <param name="fillIdentity">是否填充主键</param> /// <returns></returns> public static object Insert(EntityBase entity, bool fillIdentity) { Type type = CH.GetRealType(entity); string name = type.FullName; object bo = DataAccessLoader.GetBoInstance(name); FastInvokeHandler inv = FastValueGetSet.GetCustomerMethodInfo(bo.GetType(), "Insert", new Type[] { type, typeof(bool) }); return(inv(bo, new object[] { entity, fillIdentity })); }
/// <summary> /// 获取唯一 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lstScope">条件</param> /// <returns></returns> public static T GetUnique <T>(ScopeList lstScope) where T : EntityBase, new() { string name = typeof(T).FullName; BusinessModelBase <T> bo = DataAccessLoader.GetBoInstance(name) as BusinessModelBase <T>; if (bo == null) { throw new MissingMemberException("找不到:" + name + " 对应的业务类"); } return(bo.GetUnique(lstScope)); }
/// <summary> /// 根据ID获取实体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id">id</param> /// <returns></returns> public static T GetById <T>(object id) where T : EntityBase, new() { string name = typeof(T).FullName; BusinessModelBase <T> bo = DataAccessLoader.GetBoInstance(name) as BusinessModelBase <T>; if (bo == null) { throw new MissingMemberException("找不到:" + name + " 对应的业务类"); } return(bo.GetEntityById(id)); }
/// <summary> /// 获取当前类关联的DB信息 /// </summary> /// <returns></returns> private static DBInfo GetDB() { Type cType = typeof(T); DataBaseAttribute att = FastInvoke.GetClassAttribute <DataBaseAttribute>(cType); if (att == null) { throw new Exception(cType.FullName + "类还没配置DataBaseAttribute标签"); } string dbName = att.DataBaseName; DataAccessLoader.InitConfig(); return(DataAccessLoader.GetDBInfo(dbName)); }
/// <summary> /// 获取唯一 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="condition">条件</param> /// <returns></returns> public static T GetUnique <T>(BQLCondition condition) where T : EntityBase, new() { string name = typeof(T).FullName; BusinessModelBase <T> bo = DataAccessLoader.GetBoInstance(name) as BusinessModelBase <T>; if (bo == null) { throw new MissingMemberException("找不到:" + name + " 对应的业务类"); } ScopeList lstScope = new ScopeList(); if (!CommonMethods.IsNull(condition)) { lstScope.Add(condition); } return(bo.GetUnique(lstScope)); }
/// <summary> /// 填充实体信息 /// </summary> /// <param name="doc"></param> /// <param name="dicRelation"></param> internal static void FillEntityInfo(XmlDocument doc, TableAttribute tableAtt) { XmlNodeList nodes = doc.GetElementsByTagName("class"); if (nodes.Count > 0) { XmlNode node = nodes[0]; XmlAttribute att = node.Attributes["TableName"]; if (att != null) { tableAtt.TableName = att.InnerText; } att = node.Attributes["BelongDB"]; if (att != null) { tableAtt.BelongDB = att.InnerText; } att = node.Attributes["Description"]; if (att != null) { tableAtt.Description = att.InnerText; } att = node.Attributes["lazy"]; if (att != null) { tableAtt.AllowLazy = att.InnerText == "1"; } att = node.Attributes["UseCache"]; if (att != null) { DBInfo db = DataAccessLoader.GetDBInfo(tableAtt.BelongDB); if (db != null) { if (att.InnerText == "1") { db.QueryCache.SetCacheTable(tableAtt.TableName); } } } } }
/// <summary> /// 初始化类型的属性信息 /// </summary> /// <param name="type">类型</param> /// <returns>如果已经初始化过侧返回false</returns> private static EntityInfoHandle InitEntityPropertyInfos(Type type, Dictionary <string, EntityConfigInfo> dicConfigs) { if (type == null) { return(null); } string fullName = type.FullName; TableAttribute tableAtt = new TableAttribute(); CreateInstanceHandler createrHandle = null; //实例化本类型的句柄 if (!type.IsGenericType) { createrHandle = FastValueGetSet.GetCreateInstanceHandlerWithOutCache(type); } Dictionary <string, EntityPropertyInfo> dicPropertys = new Dictionary <string, EntityPropertyInfo>(); Dictionary <string, EntityMappingInfo> dicMapping = new Dictionary <string, EntityMappingInfo>(); Dictionary <string, EntityParam> dicParamsInfo = new Dictionary <string, EntityParam>(); Dictionary <string, TableRelationAttribute> dicRelationInfo = new Dictionary <string, TableRelationAttribute>(); FillEntityInfos(dicParamsInfo, dicRelationInfo, type, tableAtt, dicConfigs); DBInfo db = DataAccessLoader.GetDBInfo(tableAtt.BelongDB); IDBAdapter idb = db.CurrentDbAdapter; EntityInfoHandle classInfo = new EntityInfoHandle(type, createrHandle, tableAtt, db); Dictionary <string, bool> dicNotFoundParam = new Dictionary <string, bool>(); Dictionary <string, bool> dicNotFoundRelation = new Dictionary <string, bool>(); FillNotFoundField(dicParamsInfo, dicRelationInfo, dicNotFoundParam, dicNotFoundRelation); //属性信息句柄 List <FieldInfoHandle> lstFields = FieldInfoHandle.GetFieldInfos(type, FastValueGetSet.AllBindingFlags, true); DataBaseOperate oper = db.DefaultOperate; ///读取属性别名 foreach (FieldInfoHandle finf in lstFields) { ///通过属性来反射 EntityParam ep = null; if (dicParamsInfo.TryGetValue(finf.FieldName, out ep)) { //if (tableAtt.IsParamNameUpper) //{ // ep.ParamName = ep.ParamName.ToUpper(); //} string proName = ep.PropertyName; //GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(finf); //SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(finf); if (finf.HasGetHandle || finf.HasSetHandle) { PropertyInfo pinfo = type.GetProperty(ep.PropertyName, FastValueGetSet.AllBindingFlags); EntityPropertyInfo entityProperty = new EntityPropertyInfo( classInfo, finf.GetHandle, finf.SetHandle, ep, finf.FieldType, finf.FieldName, finf.BelongFieldInfo, pinfo); dicPropertys.Add(proName, entityProperty); dicNotFoundParam.Remove(finf.FieldName); } } else { TableRelationAttribute tableMappingAtt = null; if (dicRelationInfo.TryGetValue(finf.FieldName, out tableMappingAtt)) { Type targetType = DefaultType.GetRealValueType(finf.FieldType); tableMappingAtt.SetEntity(type, targetType); //GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(finf); //SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(finf); PropertyInfo pinfo = type.GetProperty(tableMappingAtt.PropertyName, FastValueGetSet.AllBindingFlags); EntityMappingInfo entityMappingInfo = new EntityMappingInfo( type, finf.GetHandle, finf.SetHandle, tableMappingAtt, finf.FieldName, finf.FieldType, finf.BelongFieldInfo, pinfo); dicMapping.Add(tableMappingAtt.PropertyName, entityMappingInfo); dicNotFoundRelation.Remove(finf.FieldName); } } } if (dicNotFoundParam.Count > 0 || dicNotFoundRelation.Count > 0) { StringBuilder message = new StringBuilder(); foreach (KeyValuePair <string, bool> kvp in dicNotFoundParam) { message.Append(kvp.Key + "、"); } foreach (KeyValuePair <string, bool> kvp in dicNotFoundRelation) { message.Append(kvp.Key + "、"); } if (message.Length > 0) { message.Remove(message.Length - 1, 1); } message.Insert(0, "类:" + type.FullName + " 找不到字段"); throw new MissingFieldException(message.ToString()); } classInfo.SetInfoHandles(dicPropertys, dicMapping); FillAttributeInfo(type, classInfo); _dicClass[fullName] = classInfo; return(classInfo); }
/// <summary> /// 获取默认的Key信息 /// </summary> /// <returns></returns> internal static KeyWordInfomation GetKeyInfo() { _putKeyInfo.DBInfo = DataAccessLoader.GetFristDBInfo(); return(_putKeyInfo); }