using CYQ.Data.SQL;
using System.Data.SqlClient;
using System.Data;
using System;
using CYQ.Data.Table;
using CYQ.Data.Aop;
using System.Collections.Generic;
using CYQ.Data.Tool;
using System.Data.Common;
namespace CYQ.Data
{
///
/// Manipulate:sql / procedure
///操作:SQL或存储过程
///
public class MProc : IDisposable
{
///
/// Change MAction to MProc
/// 将一个MAction 转换成一个MProc。
///
public static implicit operator MProc(MAction action)
{
return new MProc(action.dalHelper);
}
internal DbBase dalHelper;
private NoSqlCommand _noSqlCommand;
private InterAop _aop = new InterAop();
// private AopInfo _aopInfo = new AopInfo();
private string _procName = string.Empty;
private bool _isProc = true;
private string _debugInfo = string.Empty;
///
/// 原始传进进来的链接。
///
private string _conn = string.Empty;
///
/// Get or set debugging information [need to set App Config.Debug.Open DebugInfo to ture]
///获取或设置调试信息[需要设置AppConfig.Debug.OpenDebugInfo为ture]
///
public string DebugInfo
{
get
{
if (dalHelper != null)
{
return dalHelper.debugInfo.ToString();
}
return _debugInfo;
}
}
///
/// The database type
/// 数据库类型
///
public DalType DalType
{
get
{
return dalHelper.dalType;
}
}
///
/// The database name
/// 数据库名称
///
public string DataBase
{
get
{
return dalHelper.DataBase;
}
}
///
/// The database version
/// 数据库的版本号
///
public string DalVersion
{
get
{
return dalHelper.Version;
}
}
///
/// The number of rows affected when executing a SQL command (-2 is an exception)
/// 执行SQL命令时受影响的行数(-2则为异常)。
///
public int RecordsAffected
{
get
{
return dalHelper.recordsAffected;
}
}
///
/// The database connection string
///数据库链接字符串
///
public string ConnectionString
{
get
{
if (dalHelper != null)
{
return dalHelper.conn;
}
return string.Empty;
}
}
///
/// Command Timeout[seconds]
///命令超时设置[单位秒]
///
public int TimeOut
{
get
{
if (dalHelper.Com != null)
{
return dalHelper.Com.CommandTimeout;
}
return -1;
}
set
{
if (dalHelper.Com != null)
{
dalHelper.Com.CommandTimeout = value;
}
}
}
///
/// Instantiation
/// 实例化
///
/// Parameters: sql or procedure
/// 参数:SQL语句或存储过程名称
public MProc(object procNameOrSql)
{
Init(procNameOrSql, AppConfig.DB.DefaultConn);
}
public MProc(object procNameOrSql, string conn)
{
Init(procNameOrSql, conn);
}
internal MProc(DbBase dbBase)
{
_procName = string.Empty;
_conn = dbBase.conn;
SetDbBase(dbBase);
}
private void Init(object procNameOrSql, string conn)
{
#region 分析是Sql或者存储过程
if (procNameOrSql != null)
{
if (procNameOrSql is Enum)
{
Type t = procNameOrSql.GetType();
string enumName = t.Name;
if (enumName != "ProcNames")
{
if (enumName.Length > 1 && enumName[1] == '_')
{
conn = enumName.Substring(2).Replace("Enum", "Conn");
}
else
{
string[] items = t.FullName.Split('.');
if (items.Length > 1)
{
conn = items[items.Length - 2] + "Conn";
items = null;
}
}
}
t = null;
}
_procName = procNameOrSql.ToString().Trim();
_isProc = _procName.IndexOf(' ') == -1;//不包含空格
}
#endregion
_conn = conn;
SetDbBase(DalCreate.CreateDal(conn));
}
private void SetDbBase(DbBase dbBase)
{
dalHelper = dbBase;
if (dalHelper.IsOnExceptionEventNull)
{
dalHelper.OnExceptionEvent += new DbBase.OnException(helper_OnExceptionEvent);
}
switch (dalHelper.dalType)
{
case DalType.Txt:
case DalType.Xml:
_noSqlCommand = new NoSqlCommand(_procName, dalHelper);
break;
}
//Aop.IAop myAop = Aop.InterAop.Instance.GetFromConfig();//试图从配置文件加载自定义Aop
//if (myAop != null)
//{
// SetAop(myAop);
//}
}
///
/// IsClearParameters
/// 是否清除参数
///
public void ResetProc(object procNameOrSql, bool isClearPara)
{
_procName = procNameOrSql.ToString().Trim();
if (isClearPara)
{
dalHelper.ClearParameters();
}
_isProc = _procName.IndexOf(' ') == -1;//不包含空格
switch (dalHelper.dalType)
{
case DalType.Txt:
case DalType.Xml:
_noSqlCommand = null;
_noSqlCommand = new NoSqlCommand(_procName, dalHelper);
break;
}
}
///
/// Toggle tProc Action: To switch between other sql/procedure, use this method
/// 切换操作:如需操作其它语句或存储过程,通过此方法切换
///
/// Parameters: sql or procedure
/// 参数:存储过程名或Sql语句
public void ResetProc(object procNameOrSql)
{
ResetProc(procNameOrSql, true);
}
private AopResult SetAopResult(AopEnum action)
{
if (_aop.IsLoadAop)
{
_aop.Para.MProc = this;
_aop.Para.ProcName = _procName;
_aop.Para.IsProc = _isProc;
if (dalHelper.Com != null)
{
_aop.Para.DBParameters = dalHelper.Com.Parameters;
}
_aop.Para.IsTransaction = dalHelper.isOpenTrans;
return _aop.Begin(action);
}
return AopResult.Default;
}
///
/// Get MDataTable
///
public MDataTable ExeMDataTable()
{
CheckDisposed();
AopResult aopResult = SetAopResult(AopEnum.ExeMDataTable);
if (aopResult == AopResult.Return)
{
return _aop.Para.Table;
}
else
{
if (aopResult != AopResult.Break)
{
switch (dalHelper.dalType)
{
case DalType.Txt:
case DalType.Xml:
_aop.Para.Table = _noSqlCommand.ExeMDataTable();
break;
default:
_aop.Para.Table = dalHelper.ExeDataReader(_procName, _isProc);
_aop.Para.Table.Columns.dalType = DalType;
// dalHelper.ResetConn();//重置Slave
break;
}
_aop.Para.Table.Conn = _conn;
_aop.Para.IsSuccess = _aop.Para.Table.Rows.Count > 0;
}
if (aopResult != AopResult.Default)
{
_aop.End(AopEnum.ExeMDataTable);
}
return _aop.Para.Table;
}
}
///
/// Get MDataTables
///
public List ExeMDataTableList()
{
CheckDisposed();
AopResult aopResult = SetAopResult(AopEnum.ExeMDataTableList);
if (aopResult == AopResult.Return)
{
return _aop.Para.TableList;
}
else
{
if (aopResult != AopResult.Break)
{
List dtList = new List();
switch (dalHelper.dalType)
{
case DalType.Txt:
case DalType.Xml:
case DalType.Oracle:
if (_isProc && dalHelper.dalType == DalType.Oracle)
{
goto isProc;
}
foreach (string sql in _procName.TrimEnd(';').Split(';'))
{
MDataTable dt = null;
if (dalHelper.dalType == DalType.Oracle)
{
dt = dalHelper.ExeDataReader(sql, false);
}
else
{
_noSqlCommand.CommandText = sql;
dt = _noSqlCommand.ExeMDataTable();
}
if (dt != null)
{
dtList.Add(dt);
}
}
break;
default:
isProc:
DbDataReader reader = dalHelper.ExeDataReader(_procName, _isProc);
if (reader != null)
{
do
{
dtList.Add(MDataTable.CreateFrom(reader));
}
while (reader.NextResult());
reader.Close();
reader.Dispose();
reader = null;
}
break;
}
_aop.Para.TableList = dtList;
_aop.Para.IsSuccess = dtList.Count > 0;
}
if (aopResult != AopResult.Default)
{
_aop.End(AopEnum.ExeMDataTableList);
}
return _aop.Para.TableList;
}
}
///
/// Returns the number of rows affected [used to insert update or delete], and returns -2 if an exception is executed
/// 返回受影响的行数[用于更新或删除],执行异常时返回-2
///
public int ExeNonQuery()
{
CheckDisposed();
AopResult aopResult = SetAopResult(AopEnum.ExeNonQuery);
if (aopResult == AopResult.Return)
{
return _aop.Para.RowCount;
}
else
{
if (aopResult != AopResult.Break)
{
switch (dalHelper.dalType)
{
case DalType.Txt:
case DalType.Xml:
_aop.Para.RowCount = _noSqlCommand.ExeNonQuery();
break;
default:
_aop.Para.RowCount = dalHelper.ExeNonQuery(_procName, _isProc);
break;
}
_aop.Para.IsSuccess = _aop.Para.RowCount > 0;
}
if (aopResult != AopResult.Default)
{
_aop.End(AopEnum.ExeNonQuery);
}
return _aop.Para.RowCount;
}
}
///
/// Returns the value of the first column of the first row
/// 返回首行首列的值
///
public T ExeScalar()
{
CheckDisposed();
AopResult aopResult = SetAopResult(AopEnum.ExeScalar);
if (aopResult == AopResult.Default || aopResult == AopResult.Continue)
{
switch (dalHelper.dalType)
{
case DalType.Txt:
case DalType.Xml:
_aop.Para.ExeResult = _noSqlCommand.ExeScalar();
break;
default:
_aop.Para.ExeResult = dalHelper.ExeScalar(_procName, _isProc);
break;
}
_aop.Para.IsSuccess = _aop.Para.ExeResult != null;
}
if (aopResult == AopResult.Continue || aopResult == AopResult.Break)
{
_aop.End(AopEnum.ExeScalar);
}
if (_aop.Para.ExeResult == null || _aop.Para.ExeResult == DBNull.Value)
{
return default(T);
}
Type t = typeof(T);
object value = _aop.Para.ExeResult;
switch (t.Name)
{
case "Int32":
int intValue = 0;
if (!int.TryParse(Convert.ToString(value), out intValue))
{
return default(T);
}
value = intValue;
break;
default:
try
{
value = StaticTool.ChangeType(value, t);
}
catch
{
}
break;
}
return (T)value;
}
///
/// Set Input Para
/// 设置存储过程Input参数
///
public MProc Set(object paraName, object value)
{
dalHelper.AddParameters(Convert.ToString(paraName), value); return this;
}
public MProc Set(object paraName, object value, DbType dbType)
{
dalHelper.AddParameters(Convert.ToString(paraName), value, dbType, -1, ParameterDirection.Input); return this;
}
///
/// 设置特殊自定义参数
///
public MProc SetCustom(object paraName, ParaType paraType)
{
return SetCustom(paraName, paraType, null, null);
}
///
/// Set the stored procedure OutPut, the return value and other special types of parameters
/// 设置存储过程OutPut、返回值等特殊类型参数
///
public MProc SetCustom(object paraName, ParaType paraType, object value)
{
return SetCustom(paraName, paraType, value, null);
}
/// MSSQL The name of the user-defined table typeMSSQL的用户定义表类型的名称
public MProc SetCustom(object paraName, ParaType paraType, object value, string typeName)
{
dalHelper.AddCustomePara(Convert.ToString(paraName), paraType, value, typeName); return this;
}
///
/// Clear Parameters
/// 清除存储过程参数
///
public void Clear()
{
dalHelper.ClearParameters();
}
///
/// Get Procedure Return Value
/// 存储过程的返回值
///
public int ReturnValue
{
get
{
return dalHelper.ReturnValue;
}
}
///
/// The OutPut value for the stored procedure: Dictionary for multiple values
/// 存储过程的OutPut值:多个值时为Dictionary
///
public object OutPutValue
{
get
{
return dalHelper.OutPutValue;
}
}
#region Aop 相关操作
///
/// Set Aop State
/// 设置Aop状态
///
public MProc SetAopState(AopOp op)
{
_aop.aopOp = op;
return this;
}
///
/// Pass additional parameters for Aop use
/// 传递额外的参数供Aop使用
///
public MProc SetAopPara(object para)
{
_aop.Para.AopPara = para;
return this;
}
void helper_OnExceptionEvent(string errorMsg)
{
_aop.OnError(errorMsg);
}
#endregion
#region 事务操作
///
/// Set the transaction level
/// 设置事务级别
///
/// IsolationLevel
public MProc SetTransLevel(IsolationLevel level)
{
dalHelper.TranLevel = level; return this;
}
///
/// Begin Transation
/// 开始事务
///
public void BeginTransation()
{
dalHelper.isOpenTrans = true;
}
///
/// Commit Transation
/// 提交事务
///
public bool EndTransation()
{
if (dalHelper != null && dalHelper.isOpenTrans)
{
return dalHelper.EndTransaction();
}
return false;
}
///
/// RollBack Transation
/// 事务回滚
///
public bool RollBack()
{
if (dalHelper != null && dalHelper.isOpenTrans)
{
return dalHelper.RollBack();
}
return false;
}
#endregion
#region IDisposable 成员
///
/// Dispose
/// 释放资源
///
public void Dispose()
{
hasDisposed = true;
if (dalHelper != null)
{
if (!dalHelper.IsOnExceptionEventNull)
{
dalHelper.OnExceptionEvent -= new DbBase.OnException(helper_OnExceptionEvent);
}
_debugInfo = dalHelper.debugInfo.ToString();
dalHelper.Dispose();
dalHelper = null;
}
if (_noSqlCommand != null)
{
_noSqlCommand.Dispose();
}
}
bool hasDisposed = false;
private void CheckDisposed()
{
if (hasDisposed)
{
Error.Throw("The current object 'MProc' has been disposed");
}
}
#endregion
}
}