MDataRow.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Collections;
  6. using System.ComponentModel;
  7. namespace Ant.ORM.Table
  8. {
  9. /// <summary>
  10. /// 一行记录
  11. /// </summary>
  12. public class MDataRow : List<MDataCell>, IDataRecord, System.ComponentModel.ICustomTypeDescriptor
  13. {
  14. public MDataRow()
  15. : base()
  16. {
  17. }
  18. private string _TableName;
  19. public string TableName
  20. {
  21. get
  22. {
  23. return _TableName;
  24. }
  25. set
  26. {
  27. _TableName = value;
  28. }
  29. }
  30. /// <summary>
  31. /// 输入枚举型数据
  32. /// </summary>
  33. public MDataCell this[object filed]
  34. {
  35. get
  36. {
  37. if (filed is Enum || filed is int)
  38. {
  39. return base[(int)filed];
  40. }
  41. return this[filed.ToString()];
  42. }
  43. }
  44. public MDataCell this[string key]
  45. {
  46. get
  47. {
  48. MDataCell dataCell = null;
  49. for (int i = 0; i < base.Count; i++)
  50. {
  51. if (string.Compare(base[i]._CellStruct.ColumnName, key, true) == 0)
  52. {
  53. dataCell = base[i];
  54. break;
  55. }
  56. }
  57. return dataCell;
  58. }
  59. }
  60. /// <summary>
  61. /// 此方法为Emit所调用编写。
  62. /// </summary>
  63. public object GetItemValue(int index)
  64. {
  65. MDataCell cell = this[index];
  66. if (cell == null || cell.Value == null || cell.Value == DBNull.Value)
  67. {
  68. return null;
  69. }
  70. return cell.Value;
  71. }
  72. /// <summary>
  73. /// 取值
  74. /// </summary>
  75. public T Get<T>(object key)
  76. {
  77. object value = this[key].Value;
  78. if (value == null || value == DBNull.Value)
  79. {
  80. return default(T);
  81. }
  82. switch (typeof(T).Name)
  83. {
  84. case "Int32":
  85. value = Convert.ToInt32(value);
  86. break;
  87. //case "Single":
  88. //case "Double":
  89. //case "Decimal":
  90. //case "DateTime":
  91. // break;
  92. //default:
  93. case "String":
  94. value = value.ToString();
  95. break;
  96. }
  97. return (T)value;
  98. }
  99. /// <summary>
  100. /// 将行的数据行的值全重置为Null
  101. /// </summary>
  102. public new void Clear()
  103. {
  104. for (int i = 0; i < this.Count; i++)
  105. {
  106. this[i]._CellValue.Value = null;
  107. this[i]._CellValue.IsChange = false;
  108. this[i]._CellValue.IsNull = true;
  109. }
  110. }
  111. public void Set(object key, object value)
  112. {
  113. MDataCell cell = this[key];
  114. if (cell != null)
  115. {
  116. cell.Value = value;
  117. }
  118. }
  119. public MDataTable ToTable()
  120. {
  121. MDataTable mTable = new MDataTable(_TableName);
  122. mTable.LoadRow(this);
  123. return mTable;
  124. }
  125. /// <summary>
  126. /// 从别的行加载值[不改变自身结构及引用]
  127. /// </summary>
  128. /// <param name="row"></param>
  129. internal void LoadValue(MDataRow row)
  130. {
  131. if (row != null)
  132. {
  133. MDataCell myCell;
  134. foreach (MDataCell cell in row)
  135. {
  136. myCell = this[cell._CellStruct.ColumnName];
  137. if (myCell == null)
  138. {
  139. myCell = new MDataCell(ref cell._CellStruct);
  140. this.Add(myCell);
  141. }
  142. myCell._CellValue.Value = cell._CellValue.Value;
  143. myCell._CellValue.IsNull = cell._CellValue.IsNull;
  144. myCell._CellValue.IsChange = cell._CellValue.IsChange;
  145. }
  146. }
  147. }
  148. /// <summary>
  149. /// 转成实体
  150. /// </summary>
  151. /// <typeparam name="T">实体名称</typeparam>
  152. public T ToEntity<T>()
  153. {
  154. T obj = (T)Activator.CreateInstance(typeof(T));
  155. System.Reflection.PropertyInfo[] pis = obj.GetType().GetProperties();
  156. object propValue = null;
  157. MDataCell cell = null;
  158. for (int i = 0; i < pis.Length; i++)
  159. {
  160. cell = this[pis[i].Name];
  161. if (cell == null)
  162. {
  163. continue;
  164. }
  165. propValue = cell.Value;
  166. if (propValue == null || propValue == DBNull.Value)
  167. {
  168. continue;
  169. }
  170. pis[i].SetValue(obj, propValue, null);
  171. }
  172. return obj;
  173. }
  174. #region ICloneable 成员
  175. public MDataRow Clone()
  176. {
  177. MDataRow row = new MDataRow();
  178. for (int i = 0; i < base.Count; i++)
  179. {
  180. CellStruct mcb = base[i]._CellStruct;
  181. MDataCell mdc = new MDataCell(ref mcb);
  182. mdc.Value = base[i].Value;
  183. mdc._CellValue.IsChange = false;
  184. row.Add(mdc);
  185. }
  186. row.TableName = this.TableName;
  187. return row;
  188. }
  189. #endregion
  190. #region IDataRecord 成员
  191. int IDataRecord.FieldCount
  192. {
  193. get
  194. {
  195. return base.Count;
  196. }
  197. }
  198. bool IDataRecord.GetBoolean(int i)
  199. {
  200. return (bool)this[i].Value;
  201. }
  202. byte IDataRecord.GetByte(int i)
  203. {
  204. return (byte)this[i].Value;
  205. }
  206. long IDataRecord.GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
  207. {
  208. throw new Exception(AppConst.Global_NotImplemented);
  209. }
  210. char IDataRecord.GetChar(int i)
  211. {
  212. return (char)this[i].Value;
  213. }
  214. long IDataRecord.GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
  215. {
  216. return (long)this[i].Value;
  217. }
  218. IDataReader IDataRecord.GetData(int i)
  219. {
  220. throw new Exception(AppConst.Global_NotImplemented);
  221. }
  222. string IDataRecord.GetDataTypeName(int i)
  223. {
  224. return "";
  225. //return this[i]._CellValue.ValueType.Name;
  226. }
  227. DateTime IDataRecord.GetDateTime(int i)
  228. {
  229. return (DateTime)this[i].Value;
  230. }
  231. decimal IDataRecord.GetDecimal(int i)
  232. {
  233. return (decimal)this[i].Value;
  234. }
  235. double IDataRecord.GetDouble(int i)
  236. {
  237. return (double)this[i].Value;
  238. }
  239. Type IDataRecord.GetFieldType(int i)
  240. {
  241. return this[i]._CellStruct.ValueType;
  242. }
  243. float IDataRecord.GetFloat(int i)
  244. {
  245. return (float)this[i].Value;
  246. }
  247. Guid IDataRecord.GetGuid(int i)
  248. {
  249. return (Guid)this[i].Value;
  250. }
  251. short IDataRecord.GetInt16(int i)
  252. {
  253. return (short)this[i].Value;
  254. }
  255. int IDataRecord.GetInt32(int i)
  256. {
  257. return (int)this[i].Value;
  258. }
  259. long IDataRecord.GetInt64(int i)
  260. {
  261. return (long)this[i].Value;
  262. }
  263. string IDataRecord.GetName(int i)
  264. {
  265. return (string)this[i].Value;
  266. }
  267. int IDataRecord.GetOrdinal(string name)
  268. {
  269. return (int)this[name].Value;
  270. }
  271. string IDataRecord.GetString(int i)
  272. {
  273. return (string)this[i].Value;
  274. }
  275. object IDataRecord.GetValue(int i)
  276. {
  277. return this[i].Value;
  278. }
  279. int IDataRecord.GetValues(object[] values)
  280. {
  281. return 0;
  282. }
  283. bool IDataRecord.IsDBNull(int i)
  284. {
  285. return this[i].Value == DBNull.Value;
  286. }
  287. object IDataRecord.this[string name]
  288. {
  289. get
  290. {
  291. return this[name].Value;
  292. }
  293. }
  294. object IDataRecord.this[int i]
  295. {
  296. get
  297. {
  298. return this[i].Value;
  299. }
  300. }
  301. #endregion
  302. #region ICustomTypeDescriptor 成员
  303. public System.ComponentModel.AttributeCollection GetAttributes()
  304. {
  305. return null;
  306. }
  307. public string GetClassName()
  308. {
  309. return "MDataRow";
  310. }
  311. public string GetComponentName()
  312. {
  313. return "CYQ.Data";
  314. }
  315. public TypeConverter GetConverter()
  316. {
  317. throw new Exception(AppConst.Global_NotImplemented);
  318. }
  319. public EventDescriptor GetDefaultEvent()
  320. {
  321. throw new Exception(AppConst.Global_NotImplemented);
  322. }
  323. public PropertyDescriptor GetDefaultProperty()
  324. {
  325. throw new Exception(AppConst.Global_NotImplemented);
  326. }
  327. public object GetEditor(Type editorBaseType)
  328. {
  329. throw new Exception(AppConst.Global_NotImplemented);
  330. }
  331. public EventDescriptorCollection GetEvents(Attribute[] attributes)
  332. {
  333. throw new Exception(AppConst.Global_NotImplemented);
  334. }
  335. public EventDescriptorCollection GetEvents()
  336. {
  337. throw new Exception(AppConst.Global_NotImplemented);
  338. }
  339. int index = 0;
  340. PropertyDescriptorCollection properties;
  341. public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
  342. {
  343. if (index == 1)
  344. {
  345. return properties;
  346. }
  347. index++;
  348. properties = new PropertyDescriptorCollection(null);
  349. foreach (MDataCell mdc in this)
  350. {
  351. properties.Add(new MDataProperty(mdc, null));
  352. }
  353. return properties;
  354. }
  355. public PropertyDescriptorCollection GetProperties()
  356. {
  357. return GetProperties(null);
  358. }
  359. public object GetPropertyOwner(System.ComponentModel.PropertyDescriptor pd)
  360. {
  361. return this;
  362. }
  363. #endregion
  364. }
  365. }