/// <summary> /// 二进制序列化 /// </summary> /// <param name="entity">实体类实例</param> /// <returns>字节数组</returns> public static byte[] BinarySerialize(DMEDb_EntityBase entity) { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); Type factEntityType = entity.GetType(); DMEDb_EntityFields ef = EntityFieldsCache.Item(factEntityType); byte b; //写入实体类标记 bw.Write(ENTITY_ITEM_FLAG); for (int i = 0; i < entity.PropertyValues.Length; i++) { object obj = entity.PropertyValues[i]; //if (obj == System.DBNull.Value) obj = null;//DBNull.Value在Convert 的时候会失败 //为每个属性添加null标记 if (obj == System.DBNull.Value || obj == null) b = 0; else b = 1; bw.Write(b);//写入是否 NULL标记 if (b == 1) { Type propertyType = ef.GetPropertyType(entity.PropertyNames[i]); if (propertyType == null) throw new Exception("DME.DataBase实体类序列化错误:未知的实体属性类型,请检查实体类的属性和字段定义是否匹配。"); switch (propertyType.Name ) { case "Int32": bw.Write(Convert.ToInt32(obj)); break; case "String": bw.Write(Convert.ToString(obj)); break; case "DateTime": bw.Write(((DateTime)obj).ToBinary()); break; case "Int16": bw.Write(Convert.ToInt16(obj)); break; case "Int64": bw.Write(Convert.ToInt64(obj)); break; case "Single": bw.Write(Convert.ToSingle(obj)); break; case "Double": bw.Write(Convert.ToDouble(obj)); break; case "Decimal": bw.Write(Convert.ToDecimal(obj)); break; case "Boolean": bw.Write(Convert.ToBoolean(obj)); break; case "Byte": bw.Write(Convert.ToByte(obj)); break; case "Char": bw.Write(Convert.ToChar(obj)); break; case "Byte[]": Byte[] buffer = (Byte[])obj; bw.Write(buffer.Length);//写入字节序列的长度 if (buffer.Length > 0) bw.Write(buffer); break; } } } bw.Close(); ms.Close(); return ms.ToArray(); }
public DMEDb_EntityCommand(DMEDb_EntityBase entity, DMEDb_CommonDB db) { this.currEntity = entity; this.currDb = db; }
/// <summary> /// 使用一个实体对象,初始化条件表达式 /// </summary> /// <param name="e">实体对象实例</param> public DMEDb_OQL2(DMEDb_EntityBase e) { this.CurrEntity = e; this.CurrEntity.PropertyGetting += new EventHandler<PropertyGettingEventArgs>(CurrEntity_PropertyGetting); }
private DMEDb_JoinEntity Join(DMEDb_EntityBase e,string joinTypeString) { this.joinedEntitys.Add(e); this.isJoinOpt = true; this.joinedString = string.Format (" {0} [" + e.TableName + "] On ",joinTypeString ); e.PropertyGetting += new EventHandler<PropertyGettingEventArgs>(e_PropertyGetting); DMEDb_JoinEntity je = new DMEDb_JoinEntity(this); return je; }
/// <summary> /// 右连接查询 /// </summary> /// <param name="e">要连接的实体对象</param> /// <returns>连接对象</returns> public DMEDb_JoinEntity RightJoin(DMEDb_EntityBase e) { return Join(e, "Left Join"); }
/// <summary> /// 内连接查询 /// </summary> /// <param name="e">要连接的实体对象</param> /// <returns>连接对象</returns> public DMEDb_JoinEntity Join(DMEDb_EntityBase e) { return Join(e, "Inner Join"); }
/// <summary> /// 静态实体对象表达式 /// </summary> /// <param name="e">实体对象实例</param> /// <returns>实体对象查询表达式</returns> public static DMEDb_OQL From(DMEDb_EntityBase e) { return new DMEDb_OQL(e); }
/// <summary> /// 使用多个实体类进行连接查询的条件 /// </summary> /// <param name="e"></param> /// <param name="joinedEntitys"></param> public DMEDb_OQLCompare(DMEDb_EntityBase e,params DMEDb_EntityBase [] joinedEntitys) { this.CurrEntity = e; this.CurrEntity.PropertyGetting += new EventHandler<PropertyGettingEventArgs>(CurrEntity_PropertyGetting); //处理多个实体类 if (joinedEntitys != null && joinedEntitys.Length > 0) { this.joinedEntityList = new List<DMEDb_EntityBase>(); foreach (DMEDb_EntityBase item in joinedEntitys) { this.joinedEntityList.Add(item); item.PropertyGetting += new EventHandler<PropertyGettingEventArgs>(CurrEntity_PropertyGetting); } } }
/// <summary> /// 使用一个实体对象初始化本类 /// </summary> /// <param name="e"></param> public DMEDb_OQLCompare(DMEDb_EntityBase e) { this.CurrEntity = e; //this.CurrEntity.ToCompareFields = true; this.CurrEntity.PropertyGetting += new EventHandler<PropertyGettingEventArgs>(CurrEntity_PropertyGetting); }
/// <summary> /// 使用一个实体对象初始化实体对象查询表达式 /// </summary> /// <param name="e">实体对象</param> public DMEDb_OQL(DMEDb_EntityBase e) { this.currEntity = e; this.Condition = new DMEDb_OQL2(e); this.EntityMap = e.EntityMap; this.sql_table = this.currEntity.TableName; this.currEntity.PropertyGetting += new EventHandler<PropertyGettingEventArgs>(currEntity_PropertyGetting); }
protected internal static void BinaryDeserializeCommon(BinaryReader br, DMEDb_EntityFields ef,DMEDb_EntityBase factEntity) { int flag = br.ReadInt32(); if (flag != DMEDb_Serialize.ENTITY_ITEM_FLAG) throw new Exception("反序列化错误:不是有效的实体类数据格式!"); for (int i = 0; i < factEntity.PropertyValues.Length; i++) { object obj = null; if (br.ReadByte() == 0) { obj = DBNull.Value; } else { Type propertyType = ef.GetPropertyType(factEntity.PropertyNames[i]); if (propertyType == null) throw new Exception("DME.DataBase实体类序列化错误:未知的实体属性类型,请检查实体类的属性和字段定义是否匹配。"); switch (propertyType.Name) { case "Int32": obj = br.ReadInt32(); break; case "String": obj = br.ReadString();//继续读一个字符串 break; case "DateTime": obj = DateTime.FromBinary(br.ReadInt64()); break; case "Int16": obj = br.ReadInt16(); break; case "Int64": obj = br.ReadInt64(); break; case "Single": obj = br.ReadSingle(); break; case "Double": obj = br.ReadDouble(); break; case "Decimal": obj = br.ReadDecimal(); break; case "Boolean": obj = br.ReadBoolean(); break; case "Byte": obj = br.ReadByte(); break; case "Char": obj = br.ReadChar(); break; case "Byte[]": int length = br.ReadInt32(); if (length > 0) obj = br.ReadBytes(length); break; } } factEntity.PropertyValues[i] = obj; } }