private void WriteDeleteLogicalCommon(IEnumerable <Parameter> methodParameters, string fromClause, string whereClause, string columnsToReturn) { var partialMethodName = CodeWriterUtils.ConvertParametersToMethodNameByPart(methodParameters, codeWriterSettings); using (classBuilder.BeginNest($"private {table.NetName} {CodeWriterUtils.BaseMethodNameDelete}LogicalBy{partialMethodName}({CodeWriterUtils.PrintParametersForSignature(methodParameters)})")) { var logicalDeleteColumn = table.Columns.Single(c => logicalDeleteColumns.Contains(c.Name)); var setClause = $"\\\"{logicalDeleteColumn.Name}\\\" = NOW()"; var thisTableAlias = codeWriterSettings.Namer.NameToAcronym(table.Name); var sqlCommandText = $"\"UPDATE ONLY \\\"{table.Name}\\\" AS {thisTableAlias} SET {setClause}"; if (!string.IsNullOrEmpty(fromClause)) { sqlCommandText = $"{sqlCommandText} FROM {fromClause}"; } sqlCommandText = $"{sqlCommandText} WHERE {whereClause} RETURNING {columnsToReturn};\";"; var entityVariableName = "deletedEntity"; classBuilder.AppendLine($"{table.NetName} {entityVariableName} = null;"); WriteBeginCreateConnection(); WriteBeginCreateCommand(); classBuilder.AppendLine($"command.CommandText = {sqlCommandText};"); WriteAddParametersToCommand(methodParameters); WriteBeginExecuteReader(); WriteBeginReadReaderSingle(); // TODO: KE - consider throwing here if multiple rows were modified! It should never be the case except for bad data even though the schema allows it classBuilder.AppendLine($"{entityVariableName} = ({table.NetName}){_serviceProviderFieldName}.GetService(typeof({table.NetName}));"); WriteParseEntityFromReader(entityVariableName); classBuilder.EndNest(); classBuilder.EndNest(); classBuilder.EndNest(); classBuilder.EndNest(); WriteReturnEntityIfNotNull(entityVariableName); } }
private void WriteDeletePhysicalCommon(IEnumerable <Parameter> methodParameters, string usingClause, string whereClause) { var partialMethodName = CodeWriterUtils.ConvertParametersToMethodNameByPart(methodParameters, codeWriterSettings); classBuilder.BeginNest($"private int {CodeWriterUtils.BaseMethodNameDelete}PhysicalBy{partialMethodName}({CodeWriterUtils.PrintParametersForSignature(methodParameters)})"); var thisTableAlias = codeWriterSettings.Namer.NameToAcronym(table.Name); var sqlCommandText = $"\"DELETE FROM ONLY \\\"{table.Name}\\\" AS {thisTableAlias}"; if (!string.IsNullOrEmpty(usingClause)) { sqlCommandText = $"{sqlCommandText} USING {usingClause}"; } sqlCommandText = $"{sqlCommandText} WHERE {whereClause};\""; WriteBeginCreateConnection(); WriteBeginCreateCommand(); classBuilder.AppendLine($"command.CommandText = {sqlCommandText};"); WriteAddParametersToCommand(methodParameters); classBuilder.BeginNest("try"); classBuilder.AppendLine($"return command.ExecuteNonQuery();"); classBuilder.EndNest(); classBuilder.BeginNest("catch (Exception e)"); classBuilder.BeginNest($"if (e is Npgsql.PostgresException pge && pge.SqlState == \"23503\")"); classBuilder.AppendLine("throw new EntityHasDependenciesException();"); classBuilder.EndNest(); classBuilder.AppendLine(""); classBuilder.AppendLine("throw;"); classBuilder.EndNest(); classBuilder.EndNest(); classBuilder.EndNest(); // TODO: KE - consider throwing here if multiple rows were modified! It should never be the case except for bad data even though the schema allows it classBuilder.EndNest(); }
private void WriteDeleteCommon(IEnumerable <Parameter> methodParameters, bool isLogicalDelete) { var partialMethodName = CodeWriterUtils.ConvertParametersToMethodNameByPart(methodParameters, codeWriterSettings); WriteDeleteMethodSummary(methodParameters); var methodSignature = CodeWriterUtils.GetDeleteMethodSignature(table, codeWriterSettings, methodParameters); using (classBuilder.BeginNest($"public {methodSignature}")) { CodeWriterUtils.WriteEntryLogging(classBuilder, methodSignature); if (isLogicalDelete) { classBuilder.AppendLine($"var deletedEntity = {CodeWriterUtils.BaseMethodNameDelete}LogicalBy{partialMethodName}({PrintParametersForCall(methodParameters)});"); classBuilder.BeginNest($"if (deletedEntity == null)"); classBuilder.AppendLine($"throw new EntityNotFoundException();"); classBuilder.EndNest(); classBuilder.AppendLine(""); classBuilder.AppendLine("return deletedEntity;"); } else { classBuilder.AppendLine($"int countRowsAffected = {CodeWriterUtils.BaseMethodNameDelete}PhysicalBy{partialMethodName}({PrintParametersForCall(methodParameters)});"); classBuilder.BeginNest("if (countRowsAffected == 0)"); classBuilder.AppendLine("throw new EntityNotFoundException();"); classBuilder.EndNest(); classBuilder.AppendLine(""); classBuilder.BeginNest("if (countRowsAffected != 1)"); classBuilder.AppendLine("throw new Exception(\"Delete affected more than one row.\");"); classBuilder.EndNest(); classBuilder.AppendLine(""); classBuilder.AppendLine("return null;"); } CodeWriterUtils.WriteExitLogging(classBuilder, methodSignature); } }