IDataReader ExecuteReader(DbCommand command) { DbDataReader reader = null; try { reader = command.ExecuteReader(); } catch (DbException ex) { var exceptionContext = new DbExceptionContextInfo { SqlException = new QueryException(ex.Message, ex), Message = "unable to select data.", Sql = command.CommandText, }; throw ExceptionHelper.Convert(log, exceptionConverter, exceptionContext); } if (!this.dbContext.Driver.AllowsMultipleOpenReaders) { var table = reader.ToDataTable(); reader = table.CreateDataReader(); } return(reader); }
IEnumerable <T> Project <T>(IDataReader reader, Func <FieldReader, T> fnProjector) { var items = new List <T>(); var freader = new FieldReader(reader); while (reader.Read()) { T item = default(T); try { item = fnProjector(freader); items.Add(item); } catch (Exception ex) { var exceptionContext = new DbExceptionContextInfo { SqlException = new ProjectionException(ex.Message, ex), Message = "data bind exception.", }; throw ExceptionHelper.Convert(log, exceptionConverter, exceptionContext); } } return(items); }
IEnumerable <T> ExecuteBatch <T>(string commandText, NamedParameter[] parameters, IEnumerable <object[]> paramSets, Func <FieldReader, T> fnProjector) { log.LogCommand(commandText, parameters, null); var items = new List <T>(); try { using (DbCommand cmd = this.GetCommand(commandText, parameters, null)) { //if (dialect.SupportPreparingCommands) // cmd.Prepare(); foreach (var paramValues in paramSets) { log.LogParameters(parameters, paramValues); log.LogMessage(""); cmd.Parameters.Clear(); SetParameterValues(parameters, cmd, paramValues); using (var reader = cmd.ExecuteReader()) { var freader = new FieldReader(reader); if (reader.Read()) { items.Add(fnProjector(freader)); } else { items.Add(default(T)); } } } } } catch (ConnectionException) { throw; } catch (DBConcurrencyException) { throw; } catch (Exception ex) { var exceptionContext = new DbExceptionContextInfo { Sql = commandText, SqlException = new PersistenceException(ex.Message, ex), }; throw ExceptionHelper.Convert(log, exceptionConverter, exceptionContext); } return(items); }
public int ExecuteNonQuery(CommandContext ctx) { //this.log.AddCommand(ctx.CommandText, ctx.Parameters, ctx.ParameterValues); using (var cmd = this.GetCommand(ctx.CommandText, ctx.Parameters, ctx.ParameterValues)) { var cmdSql = ctx.CommandText; var cmdParameters = ctx.Parameters.JsonSerialize(); Exception err = null; try { this.rowsAffected = cmd.ExecuteNonQuery(); if (ctx.SupportsVersionCheck && rowsAffected == 0) { throw new ConcurrencyException(ctx.Instance, ctx.OperationType); } } catch (Exception ex) { err = ex; var exceptionContext = new DbExceptionContextInfo { EntityName = ctx.EntityType.FullName, //Message = "unable to select data.", Entity = ctx.Instance, Sql = cmd.CommandText, Paramsters = cmdParameters }; switch (ctx.OperationType) { case OperationType.Insert: exceptionContext.SqlException = new InsertException(ex.Message, ex); break; case OperationType.Update: exceptionContext.SqlException = new UpdateException(ex.Message, ex); break; case OperationType.Delete: exceptionContext.SqlException = new DeleteException(ex.Message, ex); break; } throw ExceptionHelper.Convert(log, exceptionConverter, exceptionContext, ctx.ParameterValues, ctx.Parameters); } finally { //log.Log(); LogSql(err, cmdSql, cmdParameters); } } return(this.rowsAffected); }
IDataReader ExecuteReader(DbCommand command) { DbDataReader reader = null; Exception err = null; var cmdSql = command.CommandText; var cmdParameters = string.Empty; if (command.Parameters != null && command.Parameters.Count > 0) { cmdParameters = command.Parameters.JsonSerialize(); } try { reader = command.ExecuteReader(); } catch (DbException ex) { var exceptionContext = new DbExceptionContextInfo { SqlException = new QueryException(ex.Message, ex), Message = "unable to select data.", Sql = command.CommandText, Paramsters = (command.Parameters == null ? "" :command.Parameters.JsonSerialize()) }; err = exceptionContext.SqlException; throw ExceptionHelper.Convert(log, exceptionConverter, exceptionContext); } finally { LogSql(err, cmdSql, cmdParameters); } if (!this.dbContext.Driver.AllowsMultipleOpenReaders) { var table = reader.ToDataTable(); reader = table.CreateDataReader(); } return(reader); }
IEnumerable <int> ExecuteBatch(string commandText, NamedParameter[] parameters, IEnumerable <object[]> paramSets) { DataTable dataTable = new DataTable(); for (int i = 0, n = parameters.Length; i < n; i++) { var qp = parameters[i]; dataTable.Columns.Add(qp.Name, NLite.Reflection.TypeHelper.GetNonNullableType(qp.Type)); } log.LogMessage("-- Start SQL Batching --"); log.LogMessage(""); log.LogCommand(commandText, parameters, null); foreach (var paramValues in paramSets) { dataTable.Rows.Add(paramValues); log.LogParameters(parameters, paramValues); log.LogMessage(""); } var count = dataTable.Rows.Count; var result = new int[count]; if (count > 0) { int n; try { var dataAdapter = this.dbContext.dbConfiguration.DbProviderFactory.CreateDataAdapter(); //dataAdapter.UpdateBatchSize = batchSize; var cmd = this.GetCommand(commandText, parameters, null); for (int i = 0, m = parameters.Length; i < m; i++) { cmd.Parameters[i].SourceColumn = parameters[i].Name; } dataAdapter.InsertCommand = cmd;//系统会根据InsertCommand自动生成相关的DeleteCommand等 dataAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None; n = dataAdapter.Update(dataTable); } catch (ConnectionException) { throw; } catch (DBConcurrencyException) { throw; } catch (Exception ex) { var exceptionContext = new DbExceptionContextInfo { Sql = commandText, SqlException = new PersistenceException(ex.Message, ex), }; throw ExceptionHelper.Convert(log, exceptionConverter, exceptionContext); } for (int i = 0; i < count; i++) { result[i] = i < n ? 1 : 0; } dataTable.Rows.Clear(); } log.LogMessage("-- End SQL Batching --"); log.LogMessage(""); return(result); }
IEnumerable <T> ExecuteBatch <T>(string commandText, NamedParameter[] parameters, IEnumerable <object[]> paramSets, Func <FieldReader, T> fnProjector) { //log.AddMessage("开始时间:"+DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); //log.AddCommand(commandText, parameters, null); var cmdSql = string.Empty; var cmdParameters = string.Empty; Exception err = null; var items = new List <T>(); try { using (DbCommand cmd = this.GetCommand(commandText, parameters, null)) { cmdSql = cmd.CommandText; cmdParameters = paramSets.JsonSerialize(); foreach (var paramValues in paramSets) { cmd.Parameters.Clear(); SetParameterValues(parameters, cmd, paramValues); using (var reader = cmd.ExecuteReader()) { var freader = new FieldReader(reader); if (reader.Read()) { items.Add(fnProjector(freader)); } else { items.Add(default(T)); } } } } } catch (ConnectionException ex) { err = ex; throw; } catch (DBConcurrencyException ex) { err = ex; throw; } catch (Exception ex) { err = ex; var exceptionContext = new DbExceptionContextInfo { Sql = commandText, SqlException = new PersistenceException(ex.Message, ex), }; throw ExceptionHelper.Convert(log, exceptionConverter, exceptionContext); } finally { LogSql(err, cmdSql, cmdParameters); } return(items); }
IEnumerable <int> ExecuteBatch(string commandText, NamedParameter[] parameters, IEnumerable <object[]> paramSets) { DataTable dataTable = new DataTable(); for (int i = 0, n = parameters.Length; i < n; i++) { var qp = parameters[i]; dataTable.Columns.Add(qp.Name, UWay.Skynet.Cloud.Reflection.TypeHelper.GetNonNullableType(qp.Type)); } var cmdSql = commandText; var cmdParameters = new StringBuilder(); Exception err = null; cmdParameters.AppendLine(parameters.JsonSerialize()); foreach (var paramValues in paramSets) { dataTable.Rows.Add(paramValues); cmdParameters.AppendLine(paramValues.JsonSerialize()); } var count = dataTable.Rows.Count; var result = new int[count]; if (count > 0) { int n; try { var dataAdapter = this.dbContext.dbConfiguration.DbProviderFactory.CreateDataAdapter(); var cmd = this.GetCommand(commandText, parameters, null); for (int i = 0, m = parameters.Length; i < m; i++) { cmd.Parameters[i].SourceColumn = parameters[i].Name; } dataAdapter.InsertCommand = cmd;//系统会根据InsertCommand自动生成相关的DeleteCommand等 dataAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None; n = dataAdapter.Update(dataTable); } catch (ConnectionException ex) { err = ex; throw; } catch (DBConcurrencyException ex) { err = ex; throw; } catch (Exception ex) { err = ex; var exceptionContext = new DbExceptionContextInfo { Sql = commandText, SqlException = new PersistenceException(ex.Message, ex), }; //log.LogError("Batch update operation unkown exception", ex); throw ExceptionHelper.Convert(log, exceptionConverter, exceptionContext); } finally { LogSql(err, cmdSql, cmdParameters.ToString()); } for (int i = 0; i < count; i++) { result[i] = i < n ? 1 : 0; } dataTable.Rows.Clear(); } return(result); }