/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
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.Common;
- using YUI.Core.Dao.Session;
- using YUI.Core.Dao.Source;
- using YUI.Core.Common;
-
- namespace YUI.Core.Dao.Query
- {
- public class ProcSection : IProcSection
- {
- private DbProvider dbProvider;
- private DbCommand dbCommand;
- private DbTrans dbTran;
- private List<string> outParameters = new List<string>();
-
- internal ProcSection(string procName, SQLParameter[] parameters, DbProvider dbProvider, DbTrans dbTran)
- {
- this.dbProvider = dbProvider;
- this.dbTran = dbTran;
- this.dbCommand = dbProvider.CreateProcCommand(procName, parameters);
- }
-
- /// <summary>
- /// ?????????Proc??
- /// </summary>
- /// <param name="parameters"></param>
- /// <returns></returns>
- public ProcSection AddParameters(params DbParameter[] parameters)
- {
- foreach (DbParameter parameter in parameters)
- {
- dbProvider.AddParameter(dbCommand, parameter);
- }
- return this;
- }
-
- /// <summary>
- /// ???????????Proc??
- /// </summary>
- /// <param name="parameterName"></param>
- /// <param name="dbType"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public ProcSection AddInputParameter(string parameterName, DbType dbType, object value)
- {
- dbProvider.AddInputParameter(dbCommand, parameterName, dbType, value);
- return this;
- }
-
- /// <summary>
- /// ???????????Proc??
- /// </summary>
- /// <param name="parameterName"></param>
- /// <param name="dbType"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public ProcSection AddInputParameter(string parameterName, DbType dbType, int size, object value)
- {
- dbProvider.AddInputParameter(dbCommand, parameterName, dbType, size, value);
- return this;
- }
-
- /// <summary>
- /// ???????????Proc??
- /// </summary>
- /// <param name="parameterName"></param>
- /// <param name="dbType"></param>
- /// <param name="size"></param>
- /// <returns></returns>
- public ProcSection AddOutputParameter(string parameterName, DbType dbType, int size)
- {
- outParameters.Add(parameterName);
- dbProvider.AddOutputParameter(dbCommand, parameterName, dbType, size);
- return this;
- }
-
- /// <summary>
- /// ?????????????Proc??
- /// </summary>
- /// <param name="parameterName"></param>
- /// <param name="dbType"></param>
- /// <param name="value"></param>
- /// <param name="size"></param>
- /// <returns></returns>
- public ProcSection AddInputOutputParameter(string parameterName, DbType dbType, object value, int size)
- {
- outParameters.Add(parameterName);
- dbProvider.AddInputOutputParameter(dbCommand, parameterName, dbType, value, size);
- return this;
- }
-
- /// <summary>
- /// ???????????Proc??
- /// </summary>
- /// <param name="parameterName"></param>
- /// <param name="dbType"></param>
- /// <returns></returns>
- public ProcSection AddReturnValueParameter(string parameterName, DbType dbType)
- {
- outParameters.Add(parameterName);
- dbProvider.AddReturnValueParameter(dbCommand, parameterName, dbType);
- return this;
- }
-
- #region ??????
-
- /// <summary>
- /// ????Proc
- /// </summary>
- /// <returns></returns>
- public int Execute()
- {
- return dbProvider.ExecuteNonQuery(dbCommand, dbTran);
- }
-
- /// <summary>
- /// ????Proc???????
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public T ToSingle<T>()
- where T : Entity
- {
- ISourceList<T> list = GetList<T>(dbCommand, dbTran);
- if (list.Count == 0)
- {
- return default(T);
- }
- else
- {
- return list[0];
- }
- }
-
- /// <summary>
- /// ????Proc???????
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public ISourceList<T> ToList<T>()
- where T : Entity
- {
- return GetList<T>(dbCommand, dbTran);
- }
-
- /// <summary>
- /// ????Proc???????
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <returns></returns>
- public IArrayList<TResult> ToListResult<TResult>()
- {
- return GetListResult<TResult>(dbCommand, dbTran);
- }
-
- /// <summary>
- /// ????Proc?????DbReader
- /// </summary>
- /// <returns></returns>
- public ISourceReader ToReader()
- {
- return dbProvider.ExecuteReader(dbCommand, dbTran);
- }
-
- /// <summary>
- /// ????Proc?????DataTable
- /// </summary>
- /// <returns></returns>
- public ISourceTable ToTable()
- {
- DataTable dt = dbProvider.ExecuteDataTable(dbCommand, dbTran);
- return new SourceTable(dt);
- }
-
- /// <summary>
- /// ????Sql???????DataSet
- /// </summary>
- /// <returns></returns>
- public DataSet ToDataSet()
- {
- return dbProvider.ExecuteDataSet(dbCommand, dbTran);
- }
-
- /// <summary>
- /// ????Proc??????
- /// </summary>
- /// <returns></returns>
- public object ToScalar()
- {
- return dbProvider.ExecuteScalar(dbCommand, dbTran);
- }
-
- /// <summary>
- /// ????Proc??????
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <returns></returns>
- public TResult ToScalar<TResult>()
- {
- object obj = this.ToScalar();
- return DataUtils.ConvertValue<TResult>(obj);
- }
- #endregion
-
- #region ?????
-
- /// <summary>
- /// ????Proc,??????
- /// </summary>
- /// <param name="outValues"></param>
- /// <returns></returns>
- public int Execute(out IDictionary<string, object> outValues)
- {
- int value = dbProvider.ExecuteNonQuery(dbCommand, dbTran);
- GetOutputParameterValues(dbCommand, out outValues);
- return value;
- }
-
- /// <summary>
- /// ????Proc??????,??????
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="outValues"></param>
- /// <returns></returns>
- public T ToSingle<T>(out IDictionary<string, object> outValues)
- where T : Entity
- {
- ISourceList<T> list = GetList<T>(dbCommand, dbTran);
- GetOutputParameterValues(dbCommand, out outValues);
- if (list.Count == 0)
- {
- return default(T);
- }
- else
- {
- return list[0];
- }
- }
-
- /// <summary>
- /// ????Proc??????,??????
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="outValues"></param>
- /// <returns></returns>
- public ISourceList<T> ToList<T>(out IDictionary<string, object> outValues)
- where T : Entity
- {
- ISourceList<T> list = GetList<T>(dbCommand, dbTran);
- GetOutputParameterValues(dbCommand, out outValues);
- return list;
- }
-
- /// <summary>
- /// ????Proc???????
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="outValues"></param>
- /// <returns></returns>
- public IArrayList<TResult> ToListResult<TResult>(out IDictionary<string, object> outValues)
- {
- IArrayList<TResult> results = GetListResult<TResult>(dbCommand, dbTran);
- GetOutputParameterValues(dbCommand, out outValues);
- return results;
- }
-
- /// <summary>
- /// ????Proc????DbReader,??????
- /// </summary>
- /// <param name="outValues"></param>
- /// <returns></returns>
- public ISourceReader ToReader(out IDictionary<string, object> outValues)
- {
- ISourceReader reader = dbProvider.ExecuteReader(dbCommand, dbTran);
- GetOutputParameterValues(dbCommand, out outValues);
- return reader;
- }
-
- /// <summary>
- /// ????Proc????DataTable,??????
- /// </summary>
- /// <param name="outValues"></param>
- /// <returns></returns>
- public ISourceTable ToTable(out IDictionary<string, object> outValues)
- {
- DataTable dataTable = dbProvider.ExecuteDataTable(dbCommand, dbTran);
- GetOutputParameterValues(dbCommand, out outValues);
- return new SourceTable(dataTable);
- }
-
- /// <summary>
- /// ????Proc????DataSet,??????
- /// </summary>
- /// <param name="outValues"></param>
- /// <returns></returns>
- public DataSet ToDataSet(out IDictionary<string, object> outValues)
- {
- DataSet dataSet = dbProvider.ExecuteDataSet(dbCommand, dbTran);
- GetOutputParameterValues(dbCommand, out outValues);
- return dataSet;
- }
-
- /// <summary>
- /// ????Proc?????,??????
- /// </summary>
- /// <param name="outValues"></param>
- /// <returns></returns>
- public object ToScalar(out IDictionary<string, object> outValues)
- {
- object obj = dbProvider.ExecuteScalar(dbCommand, dbTran);
- GetOutputParameterValues(dbCommand, out outValues);
- return obj;
- }
-
- /// <summary>
- /// ????Proc?????,??????
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="outValues"></param>
- /// <returns></returns>
- public TResult ToScalar<TResult>(out IDictionary<string, object> outValues)
- {
- object obj = this.ToScalar(out outValues);
- return DataUtils.ConvertValue<TResult>(obj);
- }
-
- #endregion
-
- #region ????
-
- private ISourceList<T> GetList<T>(DbCommand cmd, DbTrans dbTran)
- where T : Entity
- {
- try
- {
- using (ISourceReader reader = dbProvider.ExecuteReader(cmd, dbTran))
- {
- ISourceList<T> list = new SourceList<T>();
- FastCreateInstanceHandler creator = DataUtils.CreateHandler(typeof(T));
-
- while (reader.Read())
- {
- T t = (T)creator();
- t.SetAllValues(reader);
- t.Attach();
- list.Add(t);
- }
-
- reader.Close();
-
- return list;
- }
- }
- catch
- {
- throw;
- }
- }
-
- private IArrayList<TResult> GetListResult<TResult>(DbCommand cmd, DbTrans dbTran)
- {
- try
- {
- using (ISourceReader reader = dbProvider.ExecuteReader(cmd, dbTran))
- {
- IArrayList<TResult> list = new ArrayList<TResult>();
-
- if (typeof(TResult) == typeof(object[]))
- {
- while (reader.Read())
- {
- List<object> objs = new List<object>();
- for (int row = 0; row < reader.FieldCount; row++)
- {
- objs.Add(reader.GetValue(row));
- }
-
- TResult result = (TResult)(objs.ToArray() as object);
- list.Add(result);
- }
- }
- else
- {
- while (reader.Read())
- {
- list.Add(reader.GetValue<TResult>(0));
- }
- }
-
- reader.Close();
-
- return list;
- }
- }
- catch
- {
- throw;
- }
- }
-
- private void GetOutputParameterValues(DbCommand cmd, out IDictionary<string, object> outValues)
- {
- try
- {
- IDictionary<string, object> returnValues = new Dictionary<string, object>();
- foreach (string outParameter in outParameters)
- {
- DbParameter Parameter = dbProvider.GetParameter(cmd, outParameter);
- object value = Parameter.Value;
- if (value == DBNull.Value) value = null;
- returnValues.Add(Parameter.ParameterName.Substring(1), value);
- }
- outValues = returnValues;
- }
- catch
- {
- throw;
- }
- }
-
- #endregion
-
- }
- }