/// <summary> /// Generates a WHERE clause that uses the key columns of the table to uniquely identity /// the row that will be updated. /// </summary> /// <param name="parameterize"> /// Whether or not to generate a parameterized where clause. If <c>true</c> verbatim values /// will be replaced with paremeters (like @Param12). The parameters must be added to the /// SqlCommand used to execute the commit. /// </param> /// <returns>A <see cref="WhereClause"/> object</returns> protected WhereClause GetWhereClause(bool parameterize) { WhereClause output = new WhereClause(); if (!AssociatedObjectMetadata.KeyColumns.Any()) { throw new InvalidOperationException(SR.EditDataColumnNoKeyColumns); } IList <DbCellValue> row = AssociatedResultSet.GetRow(RowId); foreach (EditColumnMetadata col in AssociatedObjectMetadata.KeyColumns) { // Put together a clause for the value of the cell DbCellValue cellData = row[col.Ordinal]; string cellDataClause; if (cellData.IsNull) { cellDataClause = "IS NULL"; } else { if (cellData.RawObject is byte[] || col.DbColumn.DataTypeName.Equals("TEXT", StringComparison.OrdinalIgnoreCase) || col.DbColumn.DataTypeName.Equals("NTEXT", StringComparison.OrdinalIgnoreCase)) { // Special cases for byte[] and TEXT/NTEXT types cellDataClause = "IS NOT NULL"; } else { // General case is to just use the value from the cell if (parameterize) { // Add a parameter and parameterized clause component // NOTE: We include the row ID to make sure the parameter is unique if // we execute multiple row edits at once. string paramName = $"@Param{RowId}{col.Ordinal}"; cellDataClause = $"= {paramName}"; SqlParameter parameter = new SqlParameter(paramName, col.DbColumn.SqlDbType) { Value = cellData.RawObject }; output.Parameters.Add(parameter); } else { // Add the clause component with the formatted value cellDataClause = $"= {ToSqlScript.FormatValue(cellData, col.DbColumn)}"; } } } string completeComponent = $"({col.EscapedName} {cellDataClause})"; output.ClauseComponents.Add(completeComponent); } return(output); }
/// <summary> /// Applies the changes to the associated result set after successfully executing the /// change on the database /// </summary> /// <param name="dataReader"> /// Reader returned from the execution of the command to update a row. Should contain /// a single row that represents all the values of the row. /// </param> public override Task ApplyChanges(DbDataReader dataReader) { Validate.IsNotNull(nameof(dataReader), dataReader); return(AssociatedResultSet.UpdateRow(RowId, dataReader)); }
/// <summary> /// Applies the changes to the associated result set after successfully executing the /// change on the database /// </summary> /// <param name="dataReader"> /// Reader returned from the execution of the command to insert a new row. Should NOT /// contain any rows. /// </param> public override Task ApplyChanges(DbDataReader dataReader) { // Take the result set and remove the row from it AssociatedResultSet.RemoveRow(RowId); return(Task.FromResult(0)); }
/// <summary> /// Constructs a new RowUpdate to be added to the cache. /// </summary> /// <param name="rowId">Internal ID of the row that will be updated with this object</param> /// <param name="associatedResultSet">Result set for the rows of the object to update</param> /// <param name="associatedMetadata">Metadata provider for the object to update</param> public RowUpdate(long rowId, ResultSet associatedResultSet, EditTableMetadata associatedMetadata) : base(rowId, associatedResultSet, associatedMetadata) { cellUpdates = new ConcurrentDictionary <int, CellUpdate>(); associatedRow = AssociatedResultSet.GetRow(rowId); }