private void Upsert(object target, UpsertQuery insertQuery) { insertQuery.Target = target; var dataContext = insertQuery.DataContext; using (var dbCommand = insertQuery.GetCommand()) { // log first command dataContext.WriteLog(dbCommand.Command); // we may have two commands int rowsCount = dbCommand.Command.ExecuteNonQuery(); // the second reads output parameters if (!string.IsNullOrEmpty(insertQuery.IdQuerySql.ToString())) { var outputCommandTransaction = new ParameterizedQuery(dataContext, insertQuery.IdQuerySql, insertQuery.PrimaryKeyParameters); outputCommandTransaction.Target = target; var outputCommand = outputCommandTransaction.GetCommandTransactional(false); // then run commands outputCommand.Command.Transaction = dbCommand.Command.Transaction; // log second command dataContext.WriteLog(outputCommand.Command); using (var dataReader = outputCommand.Command.ExecuteReader()) { if (!dataReader.Read()) { throw new InvalidOperationException("Could not retrieve data for inserted row on " + target.GetType()); } int outputParameterIndex = 0; for (IEnumerator <ObjectOutputParameterExpression> output = insertQuery.OutputParameters.GetEnumerator(); output.MoveNext(); ++outputParameterIndex) { var outputDbParameter = dataReader.GetValue(outputParameterIndex); SetOutputParameterValue(target, output.Current, outputDbParameter); } } } dbCommand.Commit(); } }
/// <summary> /// Performs an update /// </summary> /// <param name="target">Entity to be flushed</param> /// <param name="updateQuery">SQL update query</param> /// <param name="modifiedMembers">List of modified members, or null to update all members</param> public void Update(object target, UpsertQuery updateQuery, IList <MemberInfo> modifiedMembers) { Upsert(target, updateQuery); }
/// <summary> /// Runs an InsertQuery on a provided object /// </summary> /// <param name="target"></param> /// <param name="insertQuery"></param> public void Insert(object target, UpsertQuery insertQuery) { Upsert(target, insertQuery); }