/// <summary> /// Prepares the command for execution by generating any necessary SQL. /// </summary> /// <param name="materializer"></param> /// <returns><see cref="AccessCommandExecutionToken" /></returns> public override CommandExecutionToken <OleDbCommand, OleDbParameter> Prepare(Materializer <OleDbCommand, OleDbParameter> materializer) { if (materializer == null) { throw new ArgumentNullException(nameof(materializer), $"{nameof(materializer)} is null."); } if (!Table.IsTable) { throw new MappingException($"Cannot perform an update operation on the view {Table.Name}."); } if (!Table.HasPrimaryKey && !m_Options.HasFlag(UpdateOptions.UseKeyAttribute) && KeyColumns.Count == 0) { throw new MappingException($"Cannot perform an update operation on {Table.Name} unless UpdateOptions.UseKeyAttribute or .WithKeys() is specified."); } var sqlBuilder = Table.CreateSqlBuilder(StrictMode); sqlBuilder.ApplyArgumentValue(DataSource, ArgumentValue, m_Options); sqlBuilder.ApplyDesiredColumns(materializer.DesiredColumns()); if (KeyColumns.Count > 0) { sqlBuilder.OverrideKeys(KeyColumns); } var sql = new StringBuilder($"UPDATE {Table.Name.ToQuotedString()}"); sqlBuilder.BuildSetClause(sql, " SET ", null, null); sqlBuilder.BuildWhereClause(sql, " WHERE ", null); sql.Append(";"); var updateCommand = new AccessCommandExecutionToken(DataSource, "Update " + Table.Name, sql.ToString(), sqlBuilder.GetParametersKeysLast()).CheckUpdateRowCount(m_Options); updateCommand.ExecutionMode = AccessCommandExecutionMode.NonQuery; var desiredColumns = materializer.DesiredColumns(); if (desiredColumns == Materializer.NoColumns) { return(updateCommand); } if (m_Options.HasFlag(UpdateOptions.ReturnOldValues)) { var result = PrepareRead(desiredColumns, "before"); result.NextCommand = updateCommand; return(result); } else { updateCommand.NextCommand = PrepareRead(desiredColumns, "after"); return(updateCommand); } }
public override CommandExecutionToken <OleDbCommand, OleDbParameter> Prepare(Materializer <OleDbCommand, OleDbParameter> materializer) { if (materializer == null) { throw new ArgumentNullException(nameof(materializer), $"{nameof(materializer)} is null."); } var sqlBuilder = m_Table.CreateSqlBuilder(StrictMode); sqlBuilder.ApplyDesiredColumns(materializer.DesiredColumns()); List <OleDbParameter> parameters; var sql = new StringBuilder(); sql.Append("DELETE FROM " + m_Table.Name.ToQuotedString()); if (m_FilterValue != null) { sql.Append(" WHERE " + sqlBuilder.ApplyFilterValue(m_FilterValue, m_FilterOptions)); parameters = sqlBuilder.GetParameters(); } else if (!string.IsNullOrWhiteSpace(m_WhereClause)) { sql.Append(" WHERE " + m_WhereClause); parameters = SqlBuilder.GetParameters <OleDbParameter>(m_ArgumentValue); parameters.AddRange(sqlBuilder.GetParameters()); } else { parameters = sqlBuilder.GetParameters(); } sql.Append(";"); if (m_Parameters != null) { parameters.AddRange(m_Parameters); } var deleteCommand = new AccessCommandExecutionToken(DataSource, "Delete from " + m_Table.Name, sql.ToString(), parameters); var desiredColumns = materializer.DesiredColumns(); if (desiredColumns == Materializer.NoColumns) { return(deleteCommand); } var result = PrepareRead(desiredColumns); result.NextCommand = deleteCommand; return(result); //var sqlBuilder2 // sqlBuilder.BuildSelectClause(sql, "SELECT ", null, " FROM " + m_Table.Name.ToQuotedString()); // sql.AppendLine(" WHERE " + m_WhereClause + ";"); }
/// <summary> /// Prepares the command for execution by generating any necessary SQL. /// </summary> /// <param name="materializer"></param> /// <returns><see cref="AccessCommandExecutionToken" /></returns> public override CommandExecutionToken <OleDbCommand, OleDbParameter> Prepare(Materializer <OleDbCommand, OleDbParameter> materializer) { if (materializer == null) { throw new ArgumentNullException(nameof(materializer), $"{nameof(materializer)} is null."); } var sqlBuilder = m_Table.CreateSqlBuilder(StrictMode); sqlBuilder.ApplyArgumentValue(DataSource, m_NewValues, m_Options); sqlBuilder.ApplyDesiredColumns(materializer.DesiredColumns()); var sql = new StringBuilder($"UPDATE {m_Table.Name.ToQuotedString()}"); sqlBuilder.BuildSetClause(sql, " SET ", null, null); sql.Append(" WHERE " + m_WhereClause); sql.Append(";"); var parameters = sqlBuilder.GetParameters(); if (m_Parameters != null) { parameters.AddRange(m_Parameters); } var updateCommand = new AccessCommandExecutionToken(DataSource, "Update " + m_Table.Name, sql.ToString(), parameters).CheckUpdateRowCount(m_Options, m_ExpectedRowCount); updateCommand.ExecutionMode = AccessCommandExecutionMode.NonQuery; var desiredColumns = materializer.DesiredColumns(); if (desiredColumns == Materializer.NoColumns) { return(updateCommand); } if (m_Options.HasFlag(UpdateOptions.ReturnOldValues)) { var result = PrepareRead(desiredColumns); result.NextCommand = updateCommand; return(result); } else { updateCommand.NextCommand = PrepareRead(desiredColumns); return(updateCommand); } }
/// <summary> /// Prepares the command for execution by generating any necessary SQL. /// </summary> /// <param name="materializer"></param> /// <returns><see cref="AccessCommandExecutionToken" /></returns> public override CommandExecutionToken <OleDbCommand, OleDbParameter> Prepare(Materializer <OleDbCommand, OleDbParameter> materializer) { if (materializer == null) { throw new ArgumentNullException(nameof(materializer), $"{nameof(materializer)} is null."); } var desiredColumns = materializer.DesiredColumns(); var identityInsert = m_Options.HasFlag(InsertOptions.IdentityInsert); var sqlBuilder = Table.CreateSqlBuilder(StrictMode); sqlBuilder.ApplyArgumentValue(DataSource, ArgumentValue, m_Options); sqlBuilder.ApplyDesiredColumns(desiredColumns); if (KeyColumns.Count > 0) { sqlBuilder.OverrideKeys(KeyColumns); } var sql = new StringBuilder(); sqlBuilder.BuildInsertClause(sql, $"INSERT INTO {Table.Name.ToQuotedString()} (", null, ")", identityInsert); sqlBuilder.BuildValuesClause(sql, " VALUES (", ")", identityInsert); sql.Append(";"); var result = new AccessCommandExecutionToken(DataSource, "Insert into " + Table.Name, sql.ToString(), sqlBuilder.GetParameters()); if (desiredColumns == Materializer.AutoSelectDesiredColumns) { result.ExecutionMode = AccessCommandExecutionMode.NonQuery; result.NextCommand = new AccessCommandExecutionToken(DataSource, "Fetch autonumber", "SELECT @@IDENTITY", new List <OleDbParameter>()); } else if (desiredColumns.Count > 0) { result.ExecutionMode = AccessCommandExecutionMode.NonQuery; result.NextCommand = new AccessCommandExecutionToken(DataSource, "Fetch autonumber", "SELECT @@IDENTITY", new List <OleDbParameter>()); result.NextCommand.ExecutionMode = AccessCommandExecutionMode.ExecuteScalarAndForward; result.NextCommand.ForwardResult = value => { result.NextCommand.NextCommand = PrepareNext(desiredColumns, value); }; } return(result); }
/// <summary> /// Prepares the command for execution by generating any necessary SQL. /// </summary> /// <param name="materializer"></param> /// <returns><see cref="AccessCommandExecutionToken" /></returns> public override CommandExecutionToken <OleDbCommand, OleDbParameter> Prepare(Materializer <OleDbCommand, OleDbParameter> materializer) { if (materializer == null) { throw new ArgumentNullException(nameof(materializer), $"{nameof(materializer)} is null."); } SqlBuilder.CheckForOverlaps(m_NewValues, m_WhereArgumentValue, "The same parameter '{0}' appears in both the newValue object and the where clause argument. Rename the parameter in the where expression to resolve the conflict."); SqlBuilder.CheckForOverlaps(m_NewValues, m_FilterValue, "The same parameter '{0}' appears in both the newValue object and the filter object. Use an update expression or where expression to resolve the conflict."); SqlBuilder.CheckForOverlaps(m_UpdateArgumentValue, m_WhereArgumentValue, "The same parameter '{0}' appears in both the update expression argument and the where clause argument. Rename the parameter in the where expression to resolve the conflict."); SqlBuilder.CheckForOverlaps(m_UpdateArgumentValue, m_FilterValue, "The same parameter '{0}' appears in both the update expression argument and the filter object. Use an update expression or where expression to resolve the conflict."); var sqlBuilder = m_Table.CreateSqlBuilder(StrictMode); sqlBuilder.ApplyArgumentValue(DataSource, m_NewValues, m_Options, false); sqlBuilder.ApplyDesiredColumns(materializer.DesiredColumns()); var parameters = new List <OleDbParameter>(); var sql = new StringBuilder("UPDATE " + m_Table.Name.ToQuotedString()); if (m_UpdateExpression == null) { sqlBuilder.BuildSetClause(sql, " SET ", null, null); } else { sql.Append(" SET " + m_UpdateExpression); parameters.AddRange(SqlBuilder.GetParameters <OleDbParameter>(m_UpdateArgumentValue)); } if (m_FilterValue != null) { sql.Append(" WHERE " + sqlBuilder.ApplyAnonymousFilterValue(m_FilterValue, m_FilterOptions, true)); parameters = sqlBuilder.GetParameters(DataSource); } else if (!string.IsNullOrWhiteSpace(m_WhereClause)) { sql.Append(" WHERE " + m_WhereClause); parameters.AddRange(sqlBuilder.GetParameters(DataSource)); parameters.AddRange(SqlBuilder.GetParameters <OleDbParameter>(m_WhereArgumentValue)); } else { parameters.AddRange(sqlBuilder.GetParameters(DataSource)); } sql.Append(";"); if (m_Parameters != null) { parameters.AddRange(m_Parameters); } var updateCommand = new AccessCommandExecutionToken(DataSource, "Update " + m_Table.Name, sql.ToString(), parameters).CheckUpdateRowCount(m_Options, m_ExpectedRowCount); updateCommand.ExecutionMode = AccessCommandExecutionMode.NonQuery; var desiredColumns = materializer.DesiredColumns(); if (desiredColumns == Materializer.NoColumns) { return(updateCommand); } if (m_Options.HasFlag(UpdateOptions.ReturnOldValues)) { var result = PrepareRead(desiredColumns); result.NextCommand = updateCommand; return(result); } else { updateCommand.NextCommand = PrepareRead(desiredColumns); return(updateCommand); } }