using IBatisNet.Common.Exceptions; using IBatisNet.DataAccess; using IBatisNet.DataAccess.DaoSessionHandlers; using IBatisNet.DataAccess.Interfaces; using IBatisNet.DataMapper; using IBatisNet.DataMapper.Commands; using IBatisNet.DataMapper.Configuration.ParameterMapping; using IBatisNet.DataMapper.MappedStatements; using IBatisNet.DataMapper.Scope; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Data; using System.Text; namespace Core { public class IbatisDaoExceptionHelper { // Methods public IbatisDaoExceptionHelper() { } public static string GetStatementDetail(ISqlMapper sqlMap, string statementName, object parameterObject) { try { StringBuilder builder = new StringBuilder(); StringBuilder builder2 = new StringBuilder(); MappedStatement mappedStatement = (MappedStatement)sqlMap.MappedStatements[statementName]; ISqlMapSession localSession = sqlMap.LocalSession; RequestScope request = mappedStatement.Statement.Sql.GetRequestScope(mappedStatement, parameterObject, localSession); IDbCommand command = new DbCommandDecorator(localSession.CreateCommand(mappedStatement.Statement.CommandType), request); string preparedSql = request.PreparedStatement.PreparedSql; StringCollection dbParametersName = request.PreparedStatement.DbParametersName; IDbDataParameter[] dbParameters = request.PreparedStatement.DbParameters; int count = dbParametersName.Count; for (int i = 0; i < count; i++) { IDbDataParameter parameter = dbParameters[i]; ParameterProperty mapping = request.ParameterMap.GetProperty(i); builder.Append(parameter.ParameterName); builder.Append("=["); builder2.Append(parameter.ParameterName); builder2.Append("=["); builder.Append(mapping.PropertyName); builder.Append(","); IDbDataParameter dataParameter = command.CreateParameter(); request.ParameterMap.SetParameter(mapping, dataParameter, parameterObject); if (dataParameter.Value == DBNull.Value) { builder.Append("null"); builder.Append("], "); builder2.Append("System.DBNull, null"); builder2.Append("], "); } else { builder.Append(dataParameter.Value.ToString()); builder.Append("], "); builder2.Append(dataParameter.DbType.ToString()); builder2.Append(", "); builder2.Append(dataParameter.Value.GetType().ToString()); builder2.Append("], "); } } string str2 = "Statement Id: [" + mappedStatement.Statement.Id + "] Parameters: [0]"; string str3 = ""; if (builder2.Length != 0) { str2 = "Statement Id: [" + mappedStatement.Statement.Id + "] Parameters: [" + builder.ToString(0, builder.Length - 2) + "]"; str3 = "Statement Id: [" + mappedStatement.Statement.Id + "] Types: [" + builder2.ToString(0, builder2.Length - 2) + "]"; } return (preparedSql + Environment.NewLine + str2 + (!string.IsNullOrEmpty(str3) ? (Environment.NewLine + str3) : "")); } catch { } return "can‘t get the sql error info"; } } [Serializable]//可序列化 public class BaseSqlMapDao1 : IDao { protected const int PAGE_SIZE = 4; protected string sqlMapNamespace; public BaseSqlMapDao1() { } protected string WrapStatementName(string statementName) { return (this.sqlMapNamespace + "." + statementName); } protected ISqlMapper GetLocalSqlMap() { SqlMapDaoSession localDaoSession = (SqlMapDaoSession)this.DaoManager.LocalDaoSession; return localDaoSession.SqlMap; } public IDaoManager DaoManager { get; set; } protected object ExecuteDelete(string statementName, object parameterObject) { object obj2; ISqlMapper localSqlMap = this.GetLocalSqlMap(); try { obj2 = localSqlMap.Delete(statementName, parameterObject); } catch (Exception exception) { string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject); IBatisNetException exception2 = new IBatisNetException("Error executing delete ‘" + statementName + "‘. Cause: " + exception.Message, exception); exception2.Data.Add("sqlTrace", str); throw exception2; } return obj2; } protected object ExecuteInsert(string statementName, object parameterObject) { object obj2; ISqlMapper localSqlMap = this.GetLocalSqlMap(); try { obj2 = localSqlMap.Insert(statementName, parameterObject); } catch (Exception exception) { string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject); IBatisNetException exception2 = new IBatisNetException("Error executing insert ‘" + statementName + "‘. Cause: " + exception.Message, exception); exception2.Data.Add("sqlTrace", str); throw exception2; } return obj2; } protected int ExecuteUpdate(string statementName, object parameterObject) { int num; ISqlMapper localSqlMap = this.GetLocalSqlMap(); try { num = localSqlMap.Update(statementName, parameterObject); } catch (Exception exception) { string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject); IBatisNetException exception2 = new IBatisNetException("Error update ‘" + statementName + "‘. Cause: " + exception.Message, exception); exception2.Data.Add("sqlTrace", str); throw exception2; } return num; } protected int ExecuteUpdate(string statementName, object parameterObject) { int num; ISqlMapper localSqlMap = this.GetLocalSqlMap(); try { num = localSqlMap.Update(statementName, parameterObject); } catch (Exception exception) { string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject); IBatisNetException exception2 = new IBatisNetException("Error update ‘" + statementName + "‘. Cause: " + exception.Message, exception); exception2.Data.Add("sqlTrace", str); throw exception2; } return num; } protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject) { IList<T> list; ISqlMapper localSqlMap = this.GetLocalSqlMap(); try { list = localSqlMap.QueryForList<T>(statementName, parameterObject); } catch (Exception exception) { string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject); IBatisNetException exception2 = new IBatisNetException("Error executing query ‘" + statementName + "‘ for list. Cause: " + exception.Message, exception); exception2.Data.Add("sqlTrace", str); throw exception2; } return list; } } }
时间: 2024-10-12 15:18:34