PageRenderTime 71ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/YUI/YUI.Core/Dao/Query/ProcSection.cs

http://yuicore.googlecode.com/
C# | 427 lines | 256 code | 42 blank | 129 comment | 12 complexity | 3467781a01c1fd0ac2a9f31fbcd5b5e9 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.Common;
  6. using YUI.Core.Dao.Session;
  7. using YUI.Core.Dao.Source;
  8. using YUI.Core.Common;
  9. namespace YUI.Core.Dao.Query
  10. {
  11. public class ProcSection : IProcSection
  12. {
  13. private DbProvider dbProvider;
  14. private DbCommand dbCommand;
  15. private DbTrans dbTran;
  16. private List<string> outParameters = new List<string>();
  17. internal ProcSection(string procName, SQLParameter[] parameters, DbProvider dbProvider, DbTrans dbTran)
  18. {
  19. this.dbProvider = dbProvider;
  20. this.dbTran = dbTran;
  21. this.dbCommand = dbProvider.CreateProcCommand(procName, parameters);
  22. }
  23. /// <summary>
  24. /// ?????????Proc??
  25. /// </summary>
  26. /// <param name="parameters"></param>
  27. /// <returns></returns>
  28. public ProcSection AddParameters(params DbParameter[] parameters)
  29. {
  30. foreach (DbParameter parameter in parameters)
  31. {
  32. dbProvider.AddParameter(dbCommand, parameter);
  33. }
  34. return this;
  35. }
  36. /// <summary>
  37. /// ???????????Proc??
  38. /// </summary>
  39. /// <param name="parameterName"></param>
  40. /// <param name="dbType"></param>
  41. /// <param name="value"></param>
  42. /// <returns></returns>
  43. public ProcSection AddInputParameter(string parameterName, DbType dbType, object value)
  44. {
  45. dbProvider.AddInputParameter(dbCommand, parameterName, dbType, value);
  46. return this;
  47. }
  48. /// <summary>
  49. /// ???????????Proc??
  50. /// </summary>
  51. /// <param name="parameterName"></param>
  52. /// <param name="dbType"></param>
  53. /// <param name="value"></param>
  54. /// <returns></returns>
  55. public ProcSection AddInputParameter(string parameterName, DbType dbType, int size, object value)
  56. {
  57. dbProvider.AddInputParameter(dbCommand, parameterName, dbType, size, value);
  58. return this;
  59. }
  60. /// <summary>
  61. /// ???????????Proc??
  62. /// </summary>
  63. /// <param name="parameterName"></param>
  64. /// <param name="dbType"></param>
  65. /// <param name="size"></param>
  66. /// <returns></returns>
  67. public ProcSection AddOutputParameter(string parameterName, DbType dbType, int size)
  68. {
  69. outParameters.Add(parameterName);
  70. dbProvider.AddOutputParameter(dbCommand, parameterName, dbType, size);
  71. return this;
  72. }
  73. /// <summary>
  74. /// ?????????????Proc??
  75. /// </summary>
  76. /// <param name="parameterName"></param>
  77. /// <param name="dbType"></param>
  78. /// <param name="value"></param>
  79. /// <param name="size"></param>
  80. /// <returns></returns>
  81. public ProcSection AddInputOutputParameter(string parameterName, DbType dbType, object value, int size)
  82. {
  83. outParameters.Add(parameterName);
  84. dbProvider.AddInputOutputParameter(dbCommand, parameterName, dbType, value, size);
  85. return this;
  86. }
  87. /// <summary>
  88. /// ???????????Proc??
  89. /// </summary>
  90. /// <param name="parameterName"></param>
  91. /// <param name="dbType"></param>
  92. /// <returns></returns>
  93. public ProcSection AddReturnValueParameter(string parameterName, DbType dbType)
  94. {
  95. outParameters.Add(parameterName);
  96. dbProvider.AddReturnValueParameter(dbCommand, parameterName, dbType);
  97. return this;
  98. }
  99. #region ??????
  100. /// <summary>
  101. /// ????Proc
  102. /// </summary>
  103. /// <returns></returns>
  104. public int Execute()
  105. {
  106. return dbProvider.ExecuteNonQuery(dbCommand, dbTran);
  107. }
  108. /// <summary>
  109. /// ????Proc???????
  110. /// </summary>
  111. /// <typeparam name="T"></typeparam>
  112. /// <returns></returns>
  113. public T ToSingle<T>()
  114. where T : Entity
  115. {
  116. ISourceList<T> list = GetList<T>(dbCommand, dbTran);
  117. if (list.Count == 0)
  118. {
  119. return default(T);
  120. }
  121. else
  122. {
  123. return list[0];
  124. }
  125. }
  126. /// <summary>
  127. /// ????Proc???????
  128. /// </summary>
  129. /// <typeparam name="T"></typeparam>
  130. /// <returns></returns>
  131. public ISourceList<T> ToList<T>()
  132. where T : Entity
  133. {
  134. return GetList<T>(dbCommand, dbTran);
  135. }
  136. /// <summary>
  137. /// ????Proc???????
  138. /// </summary>
  139. /// <typeparam name="TResult"></typeparam>
  140. /// <returns></returns>
  141. public IArrayList<TResult> ToListResult<TResult>()
  142. {
  143. return GetListResult<TResult>(dbCommand, dbTran);
  144. }
  145. /// <summary>
  146. /// ????Proc?????DbReader
  147. /// </summary>
  148. /// <returns></returns>
  149. public ISourceReader ToReader()
  150. {
  151. return dbProvider.ExecuteReader(dbCommand, dbTran);
  152. }
  153. /// <summary>
  154. /// ????Proc?????DataTable
  155. /// </summary>
  156. /// <returns></returns>
  157. public ISourceTable ToTable()
  158. {
  159. DataTable dt = dbProvider.ExecuteDataTable(dbCommand, dbTran);
  160. return new SourceTable(dt);
  161. }
  162. /// <summary>
  163. /// ????Sql???????DataSet
  164. /// </summary>
  165. /// <returns></returns>
  166. public DataSet ToDataSet()
  167. {
  168. return dbProvider.ExecuteDataSet(dbCommand, dbTran);
  169. }
  170. /// <summary>
  171. /// ????Proc??????
  172. /// </summary>
  173. /// <returns></returns>
  174. public object ToScalar()
  175. {
  176. return dbProvider.ExecuteScalar(dbCommand, dbTran);
  177. }
  178. /// <summary>
  179. /// ????Proc??????
  180. /// </summary>
  181. /// <typeparam name="TResult"></typeparam>
  182. /// <returns></returns>
  183. public TResult ToScalar<TResult>()
  184. {
  185. object obj = this.ToScalar();
  186. return DataUtils.ConvertValue<TResult>(obj);
  187. }
  188. #endregion
  189. #region ?????
  190. /// <summary>
  191. /// ????Proc,??????
  192. /// </summary>
  193. /// <param name="outValues"></param>
  194. /// <returns></returns>
  195. public int Execute(out IDictionary<string, object> outValues)
  196. {
  197. int value = dbProvider.ExecuteNonQuery(dbCommand, dbTran);
  198. GetOutputParameterValues(dbCommand, out outValues);
  199. return value;
  200. }
  201. /// <summary>
  202. /// ????Proc??????,??????
  203. /// </summary>
  204. /// <typeparam name="T"></typeparam>
  205. /// <param name="outValues"></param>
  206. /// <returns></returns>
  207. public T ToSingle<T>(out IDictionary<string, object> outValues)
  208. where T : Entity
  209. {
  210. ISourceList<T> list = GetList<T>(dbCommand, dbTran);
  211. GetOutputParameterValues(dbCommand, out outValues);
  212. if (list.Count == 0)
  213. {
  214. return default(T);
  215. }
  216. else
  217. {
  218. return list[0];
  219. }
  220. }
  221. /// <summary>
  222. /// ????Proc??????,??????
  223. /// </summary>
  224. /// <typeparam name="T"></typeparam>
  225. /// <param name="outValues"></param>
  226. /// <returns></returns>
  227. public ISourceList<T> ToList<T>(out IDictionary<string, object> outValues)
  228. where T : Entity
  229. {
  230. ISourceList<T> list = GetList<T>(dbCommand, dbTran);
  231. GetOutputParameterValues(dbCommand, out outValues);
  232. return list;
  233. }
  234. /// <summary>
  235. /// ????Proc???????
  236. /// </summary>
  237. /// <typeparam name="TResult"></typeparam>
  238. /// <param name="outValues"></param>
  239. /// <returns></returns>
  240. public IArrayList<TResult> ToListResult<TResult>(out IDictionary<string, object> outValues)
  241. {
  242. IArrayList<TResult> results = GetListResult<TResult>(dbCommand, dbTran);
  243. GetOutputParameterValues(dbCommand, out outValues);
  244. return results;
  245. }
  246. /// <summary>
  247. /// ????Proc????DbReader,??????
  248. /// </summary>
  249. /// <param name="outValues"></param>
  250. /// <returns></returns>
  251. public ISourceReader ToReader(out IDictionary<string, object> outValues)
  252. {
  253. ISourceReader reader = dbProvider.ExecuteReader(dbCommand, dbTran);
  254. GetOutputParameterValues(dbCommand, out outValues);
  255. return reader;
  256. }
  257. /// <summary>
  258. /// ????Proc????DataTable,??????
  259. /// </summary>
  260. /// <param name="outValues"></param>
  261. /// <returns></returns>
  262. public ISourceTable ToTable(out IDictionary<string, object> outValues)
  263. {
  264. DataTable dataTable = dbProvider.ExecuteDataTable(dbCommand, dbTran);
  265. GetOutputParameterValues(dbCommand, out outValues);
  266. return new SourceTable(dataTable);
  267. }
  268. /// <summary>
  269. /// ????Proc????DataSet,??????
  270. /// </summary>
  271. /// <param name="outValues"></param>
  272. /// <returns></returns>
  273. public DataSet ToDataSet(out IDictionary<string, object> outValues)
  274. {
  275. DataSet dataSet = dbProvider.ExecuteDataSet(dbCommand, dbTran);
  276. GetOutputParameterValues(dbCommand, out outValues);
  277. return dataSet;
  278. }
  279. /// <summary>
  280. /// ????Proc?????,??????
  281. /// </summary>
  282. /// <param name="outValues"></param>
  283. /// <returns></returns>
  284. public object ToScalar(out IDictionary<string, object> outValues)
  285. {
  286. object obj = dbProvider.ExecuteScalar(dbCommand, dbTran);
  287. GetOutputParameterValues(dbCommand, out outValues);
  288. return obj;
  289. }
  290. /// <summary>
  291. /// ????Proc?????,??????
  292. /// </summary>
  293. /// <typeparam name="TResult"></typeparam>
  294. /// <param name="outValues"></param>
  295. /// <returns></returns>
  296. public TResult ToScalar<TResult>(out IDictionary<string, object> outValues)
  297. {
  298. object obj = this.ToScalar(out outValues);
  299. return DataUtils.ConvertValue<TResult>(obj);
  300. }
  301. #endregion
  302. #region ????
  303. private ISourceList<T> GetList<T>(DbCommand cmd, DbTrans dbTran)
  304. where T : Entity
  305. {
  306. try
  307. {
  308. using (ISourceReader reader = dbProvider.ExecuteReader(cmd, dbTran))
  309. {
  310. ISourceList<T> list = new SourceList<T>();
  311. FastCreateInstanceHandler creator = DataUtils.CreateHandler(typeof(T));
  312. while (reader.Read())
  313. {
  314. T t = (T)creator();
  315. t.SetAllValues(reader);
  316. t.Attach();
  317. list.Add(t);
  318. }
  319. reader.Close();
  320. return list;
  321. }
  322. }
  323. catch
  324. {
  325. throw;
  326. }
  327. }
  328. private IArrayList<TResult> GetListResult<TResult>(DbCommand cmd, DbTrans dbTran)
  329. {
  330. try
  331. {
  332. using (ISourceReader reader = dbProvider.ExecuteReader(cmd, dbTran))
  333. {
  334. IArrayList<TResult> list = new ArrayList<TResult>();
  335. if (typeof(TResult) == typeof(object[]))
  336. {
  337. while (reader.Read())
  338. {
  339. List<object> objs = new List<object>();
  340. for (int row = 0; row < reader.FieldCount; row++)
  341. {
  342. objs.Add(reader.GetValue(row));
  343. }
  344. TResult result = (TResult)(objs.ToArray() as object);
  345. list.Add(result);
  346. }
  347. }
  348. else
  349. {
  350. while (reader.Read())
  351. {
  352. list.Add(reader.GetValue<TResult>(0));
  353. }
  354. }
  355. reader.Close();
  356. return list;
  357. }
  358. }
  359. catch
  360. {
  361. throw;
  362. }
  363. }
  364. private void GetOutputParameterValues(DbCommand cmd, out IDictionary<string, object> outValues)
  365. {
  366. try
  367. {
  368. IDictionary<string, object> returnValues = new Dictionary<string, object>();
  369. foreach (string outParameter in outParameters)
  370. {
  371. DbParameter Parameter = dbProvider.GetParameter(cmd, outParameter);
  372. object value = Parameter.Value;
  373. if (value == DBNull.Value) value = null;
  374. returnValues.Add(Parameter.ParameterName.Substring(1), value);
  375. }
  376. outValues = returnValues;
  377. }
  378. catch
  379. {
  380. throw;
  381. }
  382. }
  383. #endregion
  384. }
  385. }